test: add test for ws tracker for filter tests

This commit is contained in:
Feross Aboukhadijeh 2016-02-29 17:39:03 -08:00
parent 5dcdf9c4c1
commit 3342c20b6b
2 changed files with 47 additions and 69 deletions

View File

@ -1,23 +1,25 @@
var Server = require('../').Server
exports.createServer = function (t, serverType, cb) {
var server = new Server({
http: serverType === 'http',
udp: serverType === 'udp',
ws: serverType === 'ws'
})
exports.createServer = function (t, opts, cb) {
if (typeof opts === 'string') opts = { serverType: opts }
opts.http = (opts.serverType === 'http')
opts.udp = (opts.serverType === 'udp')
opts.ws = (opts.serverType === 'ws')
var server = new Server(opts)
server.on('error', function (err) { t.error(err) })
server.on('warning', function (err) { t.error(err) })
server.listen(0, function () {
var port = server[serverType].address().port
var port = server[opts.serverType].address().port
var announceUrl
if (serverType === 'http') {
if (opts.serverType === 'http') {
announceUrl = 'http://127.0.0.1:' + port + '/announce'
} else if (serverType === 'udp') {
} else if (opts.serverType === 'udp') {
announceUrl = 'udp://127.0.0.1:' + port
} else if (serverType === 'ws') {
} else if (opts.serverType === 'ws') {
announceUrl = 'ws://127.0.0.1:' + port
}

View File

@ -1,8 +1,8 @@
var Client = require('../')
var common = require('./common')
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var path = require('path')
var Server = require('../').Server
var test = require('tape')
var bitlove = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent'))
@ -15,55 +15,38 @@ var peerId = new Buffer('01234567890123456789')
function testFilterOption (t, serverType) {
t.plan(8)
var opts = serverType === 'http' ? { udp: false, ws: false } : { http: false, ws: false }
var opts = { serverType: serverType } // this is test-suite-only option
opts.filter = function (infoHash, params, cb) {
process.nextTick(function () {
cb(infoHash !== parsedBitlove.infoHash)
})
}
var server = new Server(opts)
server.on('error', function (err) {
t.error(err)
})
server.listen(0, function () {
var port = server[serverType].address().port
var announceUrl = serverType === 'http'
? 'http://127.0.0.1:' + port + '/announce'
: 'udp://127.0.0.1:' + port
common.createServer(t, opts, function (server, announceUrl) {
parsedBitlove.announce = [ announceUrl ]
parsedLeaves.announce = [ announceUrl ]
var client = new Client(peerId, port, parsedBitlove)
var client = new Client(peerId, 6881, parsedBitlove, { wrtc: {} })
client.on('error', function (err) {
t.error(err)
})
client.on('error', function (err) { t.error(err) })
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.once('warning', function (err) {
t.ok(/disallowed info_hash/.test(err.message), 'got client warning')
client.destroy(function () {
t.pass('client destroyed')
client = new Client(peerId, port, parsedLeaves)
client = new Client(peerId, 6881, parsedLeaves, { wrtc: {} })
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) {
t.error(err)
})
client.on('warning', function (err) {
t.error(err)
})
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
client.on('update', function () {
t.pass('got announce')
client.destroy(function () {
t.pass('client destroyed')
})
server.close(function () {
t.pass('server closed')
})
client.destroy(function () { t.pass('client destroyed') })
server.close(function () { t.pass('server closed') })
})
server.on('start', function () {
@ -74,6 +57,7 @@ function testFilterOption (t, serverType) {
})
})
server.removeAllListeners('warning')
server.once('warning', function (err) {
t.ok(/disallowed info_hash/.test(err.message), 'got server warning')
t.equal(Object.keys(server.torrents).length, 0)
@ -91,58 +75,45 @@ test('udp: filter option blocks tracker from tracking torrent', function (t) {
testFilterOption(t, 'udp')
})
test('ws: filter option blocks tracker from tracking torrent', function (t) {
testFilterOption(t, 'ws')
})
function testFilterCustomError (t, serverType) {
t.plan(8)
var opts = serverType === 'http' ? { udp: false, ws: false } : { http: false, ws: false }
var opts = { serverType: serverType } // this is test-suite-only option
opts.filter = function (infoHash, params, cb) {
process.nextTick(function () {
if (infoHash === parsedBitlove.infoHash) cb(new Error('bitlove blocked'))
else cb(true)
})
}
var server = new Server(opts)
server.on('error', function (err) {
t.error(err)
})
server.listen(0, function () {
var port = server[serverType].address().port
var announceUrl = serverType === 'http'
? 'http://127.0.0.1:' + port + '/announce'
: 'udp://127.0.0.1:' + port
common.createServer(t, opts, function (server, announceUrl) {
parsedBitlove.announce = [ announceUrl ]
parsedLeaves.announce = [ announceUrl ]
var client = new Client(peerId, port, parsedBitlove)
var client = new Client(peerId, 6881, parsedBitlove, { wrtc: {} })
client.on('error', function (err) {
t.error(err)
})
client.on('error', function (err) { t.error(err) })
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.once('warning', function (err) {
t.ok(/bitlove blocked/.test(err.message), 'got client warning')
client.destroy(function () {
t.pass('client destroyed')
client = new Client(peerId, port, parsedLeaves)
client = new Client(peerId, 6881, parsedLeaves, { wrtc: {} })
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) {
t.error(err)
})
client.on('warning', function (err) {
t.error(err)
})
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
client.on('update', function () {
t.pass('got announce')
client.destroy(function () {
t.pass('client destroyed')
})
server.close(function () {
t.pass('server closed')
})
client.destroy(function () { t.pass('client destroyed') })
server.close(function () { t.pass('server closed') })
})
server.on('start', function () {
@ -153,6 +124,7 @@ function testFilterCustomError (t, serverType) {
})
})
server.removeAllListeners('warning')
server.once('warning', function (err) {
t.ok(/bitlove blocked/.test(err.message), 'got server warning')
t.equal(Object.keys(server.torrents).length, 0)
@ -169,3 +141,7 @@ test('http: filter option with custom error', function (t) {
test('udp: filter option filter option with custom error', function (t) {
testFilterCustomError(t, 'udp')
})
test('ws: filter option filter option with custom error', function (t) {
testFilterCustomError(t, 'ws')
})