diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2014-12-31 11:50:01 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2014-12-31 12:08:01 +0300 |
commit | e3404f7a75283d9a76fbf80558a7aec292aea53c (patch) | |
tree | 278886f18b1d5a8d046e208ca13962d352517f26 | |
parent | 889e04156709321ff694679b7ea064f18bbb08e3 (diff) |
test that blocklist blocks connections to peers
-rw-r--r-- | lib/torrent.js | 5 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test/blocklist.js | 76 |
3 files changed, 80 insertions, 3 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index e6bd82e..9f6edde 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -331,8 +331,9 @@ Torrent.prototype.addPeer = function (peer) { var self = this // TODO: extract IP address from peer object and check blocklist - if (typeof peer === 'string' && - self.client.blocked && self.client.blocked.contains(addrToIPPort(peer)[0])) { + if (typeof peer === 'string' + && self.client.blocked + && self.client.blocked.contains(addrToIPPort(peer)[0])) { self.numBlockedPeers += 1 self.emit('blocked-peer', peer) } else { diff --git a/package.json b/package.json index 7c18ea0..fb5704c 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "ut_metadata": "^2.1.0", "ut_pex": "^1.0.1", "webtorrent-swarm": "0.x", - "windows-no-runnable": "~0.0.6", + "windows-no-runnable": "0.0.6", "zero-fill": "^2.1.0" }, "devDependencies": { diff --git a/test/blocklist.js b/test/blocklist.js new file mode 100644 index 0000000..1c7199d --- /dev/null +++ b/test/blocklist.js @@ -0,0 +1,76 @@ +var auto = require('run-auto') +var fs = require('fs') +var parseTorrent = require('parse-torrent') +var test = require('tape') +var TrackerServer = require('bittorrent-tracker/server') +var WebTorrent = require('../') + +var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') +var leavesParsed = parseTorrent(leavesTorrent) + +// TODO: test blocklist blocking messages to DHT nodes +// TODO: test all ways to specify blocklists + +test('blocklist blocks connections to peers', function (t) { + t.plan(8) + + auto({ + tracker: function (cb) { + var tracker = new TrackerServer({ udp: false }) + + tracker.listen(function (port) { + var announceUrl = 'http://127.0.0.1:' + port + '/announce' + + // Overwrite announce with our local tracker + leavesParsed.announce = [ announceUrl ] + leavesParsed.announceList = [[ announceUrl ]] + + cb(null, tracker) + }) + + tracker.on('start', function () { + t.pass('client connected to tracker') // 2x, once for each client + }) + }, + + client1: ['tracker', function (cb) { + var client1 = new WebTorrent({ dht: false }) + client1.on('error', function (err) { t.fail(err) }) + + var torrent1 = client1.add(leavesParsed) + + torrent1.on('peer', function () { + t.pass('client1 found itself') + cb(null, client1) + }) + }], + + client2: ['client1', function (cb) { + var client2 = new WebTorrent({ + dht: false, + blocklist: [ '127.0.0.1' ] + }) + client2.on('error', function (err) { t.fail(err) }) + + var torrent2 = client2.add(leavesParsed) + + torrent2.on('blocked-peer', function () { + t.pass('client2 blocked connection to client1 and client2') + cb(null, client2) + }) + }] + + }, function (err, r) { + if (err) throw err + + r.tracker.close(function () { + t.pass('tracker closed') + }) + r.client1.destroy(function () { + t.pass('client1 destroyed') + }) + r.client2.destroy(function () { + t.pass('client2 destroyed') + }) + }) +}) |