Skip to content

Commit

Permalink
test that blocklist blocks connections to peers
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed Dec 31, 2014
1 parent 889e041 commit e3404f7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/torrent.js
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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": {
Expand Down
76 changes: 76 additions & 0 deletions 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')
})
})
})

0 comments on commit e3404f7

Please sign in to comment.