From 1da859430e3a425ea60764327e13f047c3c47766 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 19 Dec 2015 15:14:13 +1300 Subject: [PATCH 1/8] test: `torrent.announce` must always be an array --- test/basic.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/test/basic.js b/test/basic.js index 64472b47..0577d99d 100644 --- a/test/basic.js +++ b/test/basic.js @@ -116,7 +116,7 @@ test('client.add: parsed torrent, from `parse-torrent`', function (t) { }) test('client.add: parsed torrent, with string type announce property', function (t) { - t.plan(6) + t.plan(7) var client = new WebTorrent({ dht: false, tracker: false }) @@ -131,7 +131,44 @@ test('client.add: parsed torrent, with string type announce property', function torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - t.equal(torrent.magnetURI, common.leaves.magnetURI + '&tr=' + encodeURIComponent('http://tracker.local:80')) + + var expectedMagnetURI = common.leaves.magnetURI + + '&tr=' + encodeURIComponent('http://tracker.local:80') + t.equal(torrent.magnetURI, expectedMagnetURI) + + // `torrent.announce` must always be an array + t.deepEqual(torrent.announce, [ 'http://tracker.local:80' ]) + + client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +test('client.add: parsed torrent, with array type announce property', function (t) { + t.plan(7) + + var client = new WebTorrent({ dht: false, tracker: false }) + + client.on('error', function (err) { t.fail(err) }) + client.on('warning', function (err) { t.fail(err) }) + + var parsedTorrent = extend(common.leaves.parsedTorrent) + parsedTorrent.announce = [ 'http://tracker.local:80', 'http://tracker.local:81' ] + + var torrent = client.add(parsedTorrent) + t.equal(client.torrents.length, 1) + + torrent.on('infoHash', function () { + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + + var expectedMagnetURI = common.leaves.magnetURI + + '&tr=' + encodeURIComponent('http://tracker.local:80') + + '&tr=' + encodeURIComponent('http://tracker.local:81') + t.equal(torrent.magnetURI, expectedMagnetURI) + + t.deepEqual(torrent.announce, [ 'http://tracker.local:80', 'http://tracker.local:81' ]) client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) From 70586c065756e88914a66bbe7379a4f927f16032 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 19 Dec 2015 15:22:34 +1300 Subject: [PATCH 2/8] test: blocklist-dht: use run-series this is simpler --- package.json | 1 + test/blocklist-dht.js | 45 ++++++++++++++++++++----------------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 029d0efd..b2e46267 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "browserify": "^12.0.1", "finalhandler": "^0.4.0", "run-auto": "^1.0.0", + "run-series": "^1.0.2", "serve-static": "^1.9.3", "simple-get": "^1.0.0", "standard": "^5.1.0", diff --git a/test/blocklist-dht.js b/test/blocklist-dht.js index bded83cb..0233c7cb 100644 --- a/test/blocklist-dht.js +++ b/test/blocklist-dht.js @@ -1,30 +1,27 @@ -var auto = require('run-auto') var common = require('./common') var DHT = require('bittorrent-dht/server') var networkAddress = require('network-address') +var series = require('run-series') var test = require('tape') var WebTorrent = require('../') test('blocklist blocks peers discovered via DHT', function (t) { t.plan(8) - var dhtServer = new DHT({ bootstrap: false }) + var dhtServer, client1, client2 - dhtServer.on('error', function (err) { t.fail(err) }) - dhtServer.on('warning', function (err) { t.fail(err) }) - - auto({ - dhtPort: function (cb) { - dhtServer.listen(function () { - var port = dhtServer.address().port - cb(null, port) - }) + series([ + function (cb) { + dhtServer = new DHT({ bootstrap: false }) + dhtServer.on('error', function (err) { t.fail(err) }) + dhtServer.on('warning', function (err) { t.fail(err) }) + dhtServer.listen(cb) }, - client1: ['dhtPort', function (cb, r) { - var client1 = new WebTorrent({ + function (cb) { + client1 = new WebTorrent({ tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } }) client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) @@ -54,14 +51,14 @@ test('blocklist blocks peers discovered via DHT', function (t) { var torrentReady = false var announced = false function maybeDone () { - if (torrentReady && announced) cb(null, client1) + if (torrentReady && announced) cb(null) } - }], + }, - client2: ['client1', function (cb, r) { - var client2 = new WebTorrent({ + function (cb) { + client2 = new WebTorrent({ tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort }, + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port }, blocklist: [ '127.0.0.1', networkAddress.ipv4() ] }) client2.on('error', function (err) { t.fail(err) }) @@ -75,24 +72,24 @@ test('blocklist blocks peers discovered via DHT', function (t) { torrent2.on('dhtAnnounce', function () { t.pass('client2 announced to dht') - cb(null, client2) + cb(null) }) torrent2.on('peer', function (addr) { t.fail('client2 should not find any peers') }) - }] + } - }, function (err, r) { + ], function (err, r) { if (err) throw err dhtServer.destroy(function (err) { t.error(err, 'dht server destroyed') }) - r.client1.destroy(function (err) { + client1.destroy(function (err) { t.error(err, 'client1 destroyed') }) - r.client2.destroy(function (err) { + client2.destroy(function (err) { t.error(err, 'client2 destroyed') }) }) From ec67911d287eb23d8e47483704a45c2a2ed97a80 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 19 Dec 2015 15:22:50 +1300 Subject: [PATCH 3/8] alphabetize package.json using fixpack --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index b2e46267..b99208ed 100644 --- a/package.json +++ b/package.json @@ -84,15 +84,15 @@ }, "homepage": "http://webtorrent.io", "keywords": [ - "torrent", "bittorrent", "bittorrent client", - "streaming", "download", + "mad science", + "streaming", + "torrent", "webrtc", "webrtc data", - "webtorrent", - "mad science" + "webtorrent" ], "license": "MIT", "main": "index.js", From 13eaafed2c0563da0d94778d5e22f1bf1f37fb5c Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 19 Dec 2015 15:32:12 +1300 Subject: [PATCH 4/8] test/blocklist-tracker: test cleanup --- test/blocklist-tracker.js | 73 +++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/test/blocklist-tracker.js b/test/blocklist-tracker.js index 39269a15..0d334414 100644 --- a/test/blocklist-tracker.js +++ b/test/blocklist-tracker.js @@ -1,84 +1,91 @@ -var auto = require('run-auto') -var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var common = require('./common') +var extend = require('xtend') +var series = require('run-series') var test = require('tape') var TrackerServer = require('bittorrent-tracker/server') var WebTorrent = require('../') -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - test('blocklist blocks peers discovered via tracker', function (t) { t.plan(8) - auto({ - tracker: function (cb) { - var tracker = new TrackerServer({ udp: false, ws: false }) + var parsedTorrent = extend(common.leaves.parsedTorrent) + var tracker, client1, client2 + + series([ + function (cb) { + tracker = new TrackerServer({ udp: false, ws: false }) tracker.listen(function () { var port = tracker.http.address().port var announceUrl = 'http://127.0.0.1:' + port + '/announce' // Overwrite announce with our local tracker - leavesParsed.announce = [ announceUrl ] + parsedTorrent.announce = announceUrl - cb(null, tracker) + cb(null) }) - tracker.on('start', function () { - t.pass('client connected to tracker') // 2x, once for each client + tracker.once('start', function () { + t.pass('client1 connected to tracker') + + tracker.once('start', function () { + t.pass('client2 connected to tracker') + }) }) }, - client1: ['tracker', function (cb) { - var client1 = new WebTorrent({ dht: false }) + function (cb) { + client1 = new WebTorrent({ dht: false }) client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - var torrent1 = client1.add(leavesParsed) + var torrent1 = client1.add(parsedTorrent) torrent1.on('peer', function () { t.pass('client1 found itself') - cb(null, client1) + cb(null) }) torrent1.on('blockedPeer', function () { t.fail('client1 should not block any peers') }) - }], + }, - client2: ['client1', function (cb) { - var client2 = new WebTorrent({ + function (cb) { + client2 = new WebTorrent({ dht: false, blocklist: [ '127.0.0.1' ] }) client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) - var torrent2 = client2.add(leavesParsed) + var torrent2 = client2.add(parsedTorrent) + + torrent2.once('blockedPeer', function () { + t.pass('client2 blocked first peer') - torrent2.on('blockedPeer', function () { - t.pass('client2 blocked connection') // 2x, once for each client - cb(null, client2) + torrent2.once('blockedPeer', function () { + t.pass('client2 blocked second peer') + cb(null) + }) }) torrent2.on('peer', function () { t.fail('client2 should not find any peers') }) - }] + } - }, function (err, r) { + ], function (err, r) { if (err) throw err - r.tracker.close(function () { - t.pass('tracker closed') + tracker.close(function (err) { + t.error(err, 'tracker closed') }) - r.client1.destroy(function () { - t.pass('client1 destroyed') + client1.destroy(function (err) { + t.error(err, 'client1 destroyed') }) - r.client2.destroy(function () { - t.pass('client2 destroyed') + client2.destroy(function (err) { + t.error(err, 'client2 destroyed') }) }) }) From d5ba4e0260d21657780c065c71ed3c5648df5944 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 19 Dec 2015 16:04:09 +1300 Subject: [PATCH 5/8] style --- bin/cmd.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 52a34a58..f65452f5 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -617,13 +617,15 @@ function gracefulExit () { clivas.line('\n{green:webtorrent is gracefully exiting...}') - if (client) { - if (argv['on-exit']) cp.exec(argv['on-exit']).unref() - client.destroy(function (err) { - if (err) return fatalError(err) - // Quit after 1 second. This shouldn't be necessary, node never quits even though - // there's nothing in the event loop when `wrtc` (webtorrent-hybrid) is used :( - setTimeout(function () { process.exit(0) }, 1000).unref() - }) - } + if (!client) return + + if (argv['on-exit']) cp.exec(argv['on-exit']).unref() + + client.destroy(function (err) { + if (err) return fatalError(err) + + // Quit after 1 second. This is only necessary for `webtorrent-hybrid` since + // the `wrtc` package makes node never quit :( + setTimeout(function () { process.exit(0) }, 1000).unref() + }) } From 728579c043dd027dc4557c6ce90a92e57518fbfb Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 19 Dec 2015 16:10:40 +1300 Subject: [PATCH 6/8] torrent server only call internal `server.close` once If the user calls `server.close()` on the http server returned by `torrnet.createServer()` then we should not call it in `server.destroy()` or node will return an error --- lib/server.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/server.js b/lib/server.js index dd9a5292..4b700a2c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -12,6 +12,7 @@ function Server (torrent, opts) { var server = http.createServer(opts) var sockets = [] + var closed = false server.on('connection', function (socket) { socket.setTimeout(36000000) @@ -22,11 +23,20 @@ function Server (torrent, opts) { }) }) + var _close = server.close + server.close = function (cb) { + closed = true + _close.call(server, cb) + } + server.destroy = function (cb) { sockets.forEach(function (socket) { socket.destroy() }) - server.close(cb) + + // Only call `server.close` if user has not called it already + if (closed) process.nextTick(cb) + else server.close(cb) } server.on('request', function (req, res) { From 748347861fc024dd6451be76d646a22ed7d1369b Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 22 Dec 2015 10:46:04 +1300 Subject: [PATCH 7/8] test: All remaining tests use new fixtures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Tests use new fixtures in common.js - Tests ensure that destroy callbacks don’t fire an error --- package.json | 1 - test/blocklist-dht.js | 7 +-- test/blocklist-tracker.js | 11 ++-- test/blocklist.js | 68 ++++++++++++++-------- test/common.js | 33 +++++------ test/download-dht-magnet.js | 94 +++++++++++++++---------------- test/download-dht-torrent.js | 80 ++++++++++++-------------- test/download-private-dht.js | 91 +++++++++++++----------------- test/download-tracker-magnet.js | 96 ++++++++++++++++---------------- test/download-tracker-torrent.js | 89 ++++++++++++++--------------- test/download-webseed-magnet.js | 69 +++++++++++------------ test/download-webseed-torrent.js | 50 ++++++++--------- test/duplicates.js | 44 ++++++++------- test/extensions.js | 10 ++-- test/metadata.js | 13 ++--- test/server.js | 12 ++-- 16 files changed, 374 insertions(+), 394 deletions(-) diff --git a/package.json b/package.json index b99208ed..403fbc0b 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,6 @@ "brfs": "^1.2.0", "browserify": "^12.0.1", "finalhandler": "^0.4.0", - "run-auto": "^1.0.0", "run-series": "^1.0.2", "serve-static": "^1.9.3", "simple-get": "^1.0.0", diff --git a/test/blocklist-dht.js b/test/blocklist-dht.js index 0233c7cb..852dc179 100644 --- a/test/blocklist-dht.js +++ b/test/blocklist-dht.js @@ -6,7 +6,7 @@ var test = require('tape') var WebTorrent = require('../') test('blocklist blocks peers discovered via DHT', function (t) { - t.plan(8) + t.plan(9) var dhtServer, client1, client2 @@ -80,9 +80,8 @@ test('blocklist blocks peers discovered via DHT', function (t) { }) } - ], function (err, r) { - if (err) throw err - + ], function (err) { + t.error(err) dhtServer.destroy(function (err) { t.error(err, 'dht server destroyed') }) diff --git a/test/blocklist-tracker.js b/test/blocklist-tracker.js index 0d334414..11560695 100644 --- a/test/blocklist-tracker.js +++ b/test/blocklist-tracker.js @@ -6,7 +6,7 @@ var TrackerServer = require('bittorrent-tracker/server') var WebTorrent = require('../') test('blocklist blocks peers discovered via tracker', function (t) { - t.plan(8) + t.plan(9) var parsedTorrent = extend(common.leaves.parsedTorrent) var tracker, client1, client2 @@ -75,11 +75,10 @@ test('blocklist blocks peers discovered via tracker', function (t) { }) } - ], function (err, r) { - if (err) throw err - - tracker.close(function (err) { - t.error(err, 'tracker closed') + ], function (err) { + t.error(err) + tracker.close(function () { + t.pass('tracker closed') }) client1.destroy(function (err) { t.error(err, 'client1 destroyed') diff --git a/test/blocklist.js b/test/blocklist.js index 2f4e165c..1471ca9b 100644 --- a/test/blocklist.js +++ b/test/blocklist.js @@ -30,29 +30,33 @@ function assertReachable (t, torrent, addr) { } test('blocklist (single IP)', function (t) { - t.plan(8) + t.plan(9) var client = new WebTorrent({ dht: false, tracker: false, blocklist: [ '1.2.3.4' ] }) - .on('error', function (err) { t.fail(err) }) - .on('warning', function (err) { t.fail(err) }) - .on('ready', function () { + client.on('error', function (err) { t.fail(err) }) + client.on('warning', function (err) { t.fail(err) }) + + // blocklist isn't fully loaded until `ready` event + client.on('ready', function () { client.add(leavesParsed, function (torrent) { assertBlocked(t, torrent, '1.2.3.4:1234') assertBlocked(t, torrent, '1.2.3.4:6969') assertReachable(t, torrent, '1.1.1.1:1234') assertReachable(t, torrent, '1.1.1.1:6969') - client.destroy() + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) }) }) }) test('blocklist (array of IPs)', function (t) { - t.plan(12) + t.plan(13) var client = new WebTorrent({ dht: false, @@ -70,7 +74,9 @@ test('blocklist (array of IPs)', function (t) { assertReachable(t, torrent, '1.1.1.1:1234') assertReachable(t, torrent, '1.1.1.1:6969') - client.destroy() + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) }) }) }) @@ -115,7 +121,7 @@ function assertList (t, torrent) { } test('blocklist (array of IP ranges)', function (t) { - t.plan(48) + t.plan(49) var client = new WebTorrent({ dht: false, tracker: false, @@ -129,13 +135,15 @@ test('blocklist (array of IP ranges)', function (t) { .on('ready', function () { client.add(leavesParsed, function (torrent) { assertList(t, torrent) - client.destroy() + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) }) }) }) test('blocklist (http url)', function (t) { - t.plan(49) + t.plan(51) var server = http.createServer(function (req, res) { // Check that WebTorrent declares a user agent t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1) @@ -156,15 +164,19 @@ test('blocklist (http url)', function (t) { .on('ready', function () { client.add(leavesParsed, function (torrent) { assertList(t, torrent) - client.destroy() - server.close() + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) + server.close(function () { + t.pass('server closed') + }) }) }) }) }) test('blocklist (http url with gzip encoding)', function (t) { - t.plan(49) + t.plan(51) var server = http.createServer(function (req, res) { // Check that WebTorrent declares a user agent t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1) @@ -188,15 +200,19 @@ test('blocklist (http url with gzip encoding)', function (t) { .on('ready', function () { client.add(leavesParsed, function (torrent) { assertList(t, torrent) - client.destroy() - server.close() + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) + server.close(function () { + t.pass('server closed') + }) }) }) }) }) test('blocklist (http url with deflate encoding)', function (t) { - t.plan(49) + t.plan(51) var server = http.createServer(function (req, res) { // Check that WebTorrent declares a user agent t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1) @@ -220,15 +236,19 @@ test('blocklist (http url with deflate encoding)', function (t) { .on('ready', function () { client.add(leavesParsed, function (torrent) { assertList(t, torrent) - client.destroy() - server.close() + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) + server.close(function () { + t.pass('server closed') + }) }) }) }) }) test('blocklist (fs path)', function (t) { - t.plan(48) + t.plan(49) var client = new WebTorrent({ dht: false, tracker: false, @@ -239,13 +259,15 @@ test('blocklist (fs path)', function (t) { .on('ready', function () { client.add(leavesParsed, function (torrent) { assertList(t, torrent) - client.destroy() + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) }) }) }) test('blocklist (fs path with gzip)', function (t) { - t.plan(48) + t.plan(49) var client = new WebTorrent({ dht: false, tracker: false, @@ -256,7 +278,9 @@ test('blocklist (fs path with gzip)', function (t) { .on('ready', function () { client.add(leavesParsed, function (torrent) { assertList(t, torrent) - client.destroy() + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) }) }) }) diff --git a/test/common.js b/test/common.js index c65ccb14..a2e14ad6 100644 --- a/test/common.js +++ b/test/common.js @@ -1,3 +1,5 @@ +// Torrent and content test files. Content is Public Domain or Creative Commons. + var fs = require('fs') var path = require('path') var parseTorrent = require('parse-torrent') @@ -9,12 +11,8 @@ module.exports = { torrentPath: path.join(__dirname, 'torrents', 'leaves.torrent'), content: fs.readFileSync(path.join(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')), torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent')), - parsedTorrent: parseTorrent( - fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent')) - ), - magnetURI: parseTorrent.toMagnetURI(parseTorrent( - fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent')) - )) + parsedTorrent: parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent'))), + magnetURI: parseTorrent.toMagnetURI(parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent')))) }, // Folder which contains single file @@ -22,12 +20,8 @@ module.exports = { contentPath: path.join(__dirname, 'content', 'folder'), torrentPath: path.join(__dirname, 'torrents', 'folder.torrent'), torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent')), - parsedTorrent: parseTorrent( - fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent')) - ), - magnetURI: parseTorrent.toMagnetURI(parseTorrent( - fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent')) - )) + parsedTorrent: parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent'))), + magnetURI: parseTorrent.toMagnetURI(parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent')))) }, // Folder which contains multiple files @@ -35,11 +29,14 @@ module.exports = { contentPath: path.join(__dirname, 'content', 'numbers'), torrentPath: path.join(__dirname, 'torrents', 'numbers.torrent'), torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent')), - parsedTorrent: parseTorrent( - fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent')) - ), - magnetURI: parseTorrent.toMagnetURI(parseTorrent( - fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent')) - )) + parsedTorrent: parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent'))), + magnetURI: parseTorrent.toMagnetURI(parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent')))) + }, + + // Torrent file with "private" flag + bunny: { + torrentPath: path.join(__dirname, 'torrents', 'big-buck-bunny-private.torrent'), + torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'big-buck-bunny-private.torrent')), + parsedTorrent: parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'big-buck-bunny-private.torrent'))) } } diff --git a/test/download-dht-magnet.js b/test/download-dht-magnet.js index b9c08690..02e9e17a 100644 --- a/test/download-dht-magnet.js +++ b/test/download-dht-magnet.js @@ -1,50 +1,36 @@ -var auto = require('run-auto') +var common = require('./common') var DHT = require('bittorrent-dht/server') var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var series = require('run-series') var test = require('tape') var WebTorrent = require('../') -var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') -var leavesFile = fs.readFileSync(leavesPath) -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - -// remove trackers from .torrent file -leavesParsed.announce = [] - test('Download using DHT (via magnet uri)', function (t) { t.plan(10) var dhtServer = new DHT({ bootstrap: false }) + dhtServer.on('error', function (err) { t.fail(err) }) dhtServer.on('warning', function (err) { t.fail(err) }) - var magnetUri = 'magnet:?xt=urn:btih:' + leavesParsed.infoHash + var magnetUri = 'magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash + var client1, client2 - auto({ - dhtPort: function (cb) { - dhtServer.listen(function () { - var port = dhtServer.address().port - cb(null, port) - }) + series([ + function (cb) { + dhtServer.listen(cb) }, - client1: ['dhtPort', function (cb, r) { - var client1 = new WebTorrent({ + + function (cb) { + client1 = new WebTorrent({ tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } }) + client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - var announced = false - var loaded = false - function maybeDone () { - if (announced && loaded) cb(null, client1) - } - - var torrent = client1.add(leavesParsed) + var torrent = client1.add(common.leaves.parsedTorrent) torrent.on('dhtAnnounce', function () { announced = true @@ -58,32 +44,33 @@ test('Download using DHT (via magnet uri)', function (t) { var names = [ 'Leaves of Grass by Walt Whitman.epub' ] t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - torrent.load(fs.createReadStream(leavesPath), function (err) { + torrent.load(fs.createReadStream(common.leaves.contentPath), function (err) { t.error(err) loaded = true maybeDone() }) }) - }], - client2: ['client1', function (cb, r) { - var client2 = new WebTorrent({ + var announced = false + var loaded = false + function maybeDone () { + if (announced && loaded) cb(null, client1) + } + }, + + function (cb) { + client2 = new WebTorrent({ tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } }) + client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) - var gotBuffer = false - var gotDone = false - function maybeDone () { - if (gotBuffer && gotDone) cb(null, client2) - } - - client2.add(magnetUri, function (torrent) { + client2.on('torrent', function (torrent) { torrent.files[0].getBuffer(function (err, buf) { t.error(err) - t.deepEqual(buf, leavesFile, 'downloaded correct content') + t.deepEqual(buf, common.leaves.content, 'downloaded correct content') gotBuffer = true maybeDone() @@ -96,17 +83,26 @@ test('Download using DHT (via magnet uri)', function (t) { maybeDone() }) }) - }] - }, function (err, r) { + + client2.add(magnetUri) + + var gotBuffer = false + var gotDone = false + function maybeDone () { + if (gotBuffer && gotDone) cb(null, client2) + } + } + ], function (err) { t.error(err) - r.client1.destroy(function () { - t.pass('client1 destroyed') + + client1.destroy(function (err) { + t.error(err, 'client1 destroyed') }) - r.client2.destroy(function () { - t.pass('client2 destroyed') + client2.destroy(function (err) { + t.error(err, 'client2 destroyed') }) - dhtServer.destroy(function () { - t.pass('dht server destroyed') + dhtServer.destroy(function (err) { + t.error(err, 'dht server destroyed') }) }) }) diff --git a/test/download-dht-torrent.js b/test/download-dht-torrent.js index 99856298..5888bbeb 100644 --- a/test/download-dht-torrent.js +++ b/test/download-dht-torrent.js @@ -1,19 +1,10 @@ -var auto = require('run-auto') +var common = require('./common') var DHT = require('bittorrent-dht/server') var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var series = require('run-series') var test = require('tape') var WebTorrent = require('../') -var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') -var leavesFile = fs.readFileSync(leavesPath) -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - -// remove trackers from .torrent file -leavesParsed.announce = [] - test('Download using DHT (via .torrent file)', function (t) { t.plan(8) @@ -22,28 +13,23 @@ test('Download using DHT (via .torrent file)', function (t) { dhtServer.on('error', function (err) { t.fail(err) }) dhtServer.on('warning', function (err) { t.fail(err) }) - auto({ - dhtPort: function (cb) { - dhtServer.listen(function () { - var port = dhtServer.address().port - cb(null, port) - }) + var client1, client2 + + series([ + function (cb) { + dhtServer.listen(cb) }, - client1: ['dhtPort', function (cb, r) { - var client1 = new WebTorrent({ + + function (cb) { + client1 = new WebTorrent({ tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } }) + client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - var torrent = client1.add(leavesParsed) - - var announced = false - var loaded = false - function maybeDone (err) { - if ((announced && loaded) || err) cb(err, client1) - } + var torrent = client1.add(common.leaves.parsedTorrent) torrent.on('ready', function () { // torrent metadata has been fetched -- sanity check it @@ -52,7 +38,7 @@ test('Download using DHT (via .torrent file)', function (t) { var names = [ 'Leaves of Grass by Walt Whitman.epub' ] t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - torrent.load(fs.createReadStream(leavesPath), function (err) { + torrent.load(fs.createReadStream(common.leaves.contentPath), function (err) { loaded = true maybeDone(err) }) @@ -62,23 +48,28 @@ test('Download using DHT (via .torrent file)', function (t) { announced = true maybeDone(null) }) - }], - client2: ['client1', function (cb, r) { - var client2 = new WebTorrent({ + var announced = false + var loaded = false + function maybeDone (err) { + if ((announced && loaded) || err) cb(err, client1) + } + }, + + function (cb) { + client2 = new WebTorrent({ tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } }) + client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) - client2.add(leavesParsed) - client2.on('torrent', function (torrent) { torrent.files.forEach(function (file) { file.getBuffer(function (err, buf) { if (err) throw err - t.deepEqual(buf, leavesFile, 'downloaded correct content') + t.deepEqual(buf, common.leaves.content, 'downloaded correct content') gotBuffer = true maybeDone() }) @@ -96,17 +87,20 @@ test('Download using DHT (via .torrent file)', function (t) { if (torrentDone && gotBuffer) cb(null, client2) } }) - }] - }, function (err, r) { + + client2.add(common.leaves.parsedTorrent) + } + ], function (err) { t.error(err) - r.client1.destroy(function () { - t.pass('client1 destroyed') + + client1.destroy(function (err) { + t.error(err, 'client1 destroyed') }) - r.client2.destroy(function () { - t.pass('client2 destroyed') + client2.destroy(function (err) { + t.error(err, 'client2 destroyed') }) - dhtServer.destroy(function () { - t.pass('dht server destroyed') + dhtServer.destroy(function (err) { + t.error(err, 'dht server destroyed') }) }) }) diff --git a/test/download-private-dht.js b/test/download-private-dht.js index 63ad74c1..812f57c9 100644 --- a/test/download-private-dht.js +++ b/test/download-private-dht.js @@ -1,46 +1,34 @@ -var auto = require('run-auto') +var common = require('./common') var DHT = require('bittorrent-dht/server') -var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var series = require('run-series') var test = require('tape') var WebTorrent = require('../') -var bunnyTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'big-buck-bunny-private.torrent')) -var bunnyParsed = parseTorrent(bunnyTorrent) - -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - -// remove trackers from .torrent file -bunnyParsed.announce = [] -leavesParsed.announce = [] - test('private torrent should not use DHT', function (t) { - t.plan(3) + t.plan(4) var dhtServer = new DHT({ bootstrap: false }) dhtServer.on('error', function (err) { t.fail(err) }) dhtServer.on('warning', function (err) { t.fail(err) }) - auto({ - dhtPort: function (cb) { - dhtServer.listen(function () { - var port = dhtServer.address().port - cb(null, port) - }) + var client + + series([ + function (cb) { + dhtServer.listen(cb) }, - client: ['dhtPort', function (cb, r) { - var client = new WebTorrent({ + function (cb) { + client = new WebTorrent({ tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } }) + client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add(bunnyParsed) + var torrent = client.add(common.bunny.parsedTorrent) torrent.on('dhtAnnounce', function () { t.fail('client announced to dht') @@ -49,48 +37,46 @@ test('private torrent should not use DHT', function (t) { client.on('torrent', function () { if (!torrent.discovery.dht && !torrent.swarm.handshakeOpts.dht) { t.pass('dht is disabled for this torrent') - cb(null, client) + cb(null) } }) - }] - - }, function (err, r) { - if (err) throw err + } + ], function (err) { + t.error(err) - dhtServer.destroy(function () { - t.pass('dht server destroyed') + dhtServer.destroy(function (err) { + t.error(err, 'dht server destroyed') }) - r.client.destroy(function () { - t.pass('client destroyed') + client.destroy(function (err) { + t.error(err, 'client destroyed') }) }) }) test('public torrent should use DHT', function (t) { - t.plan(3) + t.plan(4) var dhtServer = new DHT({ bootstrap: false }) dhtServer.on('error', function (err) { t.fail(err) }) dhtServer.on('warning', function (err) { t.fail(err) }) - auto({ - dhtPort: function (cb) { - dhtServer.listen(function () { - var port = dhtServer.address().port - cb(null, port) - }) + var client + + series([ + function (cb) { + dhtServer.listen(cb) }, - client: ['dhtPort', function (cb, r) { - var client = new WebTorrent({ + function (cb) { + client = new WebTorrent({ tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } }) client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add(leavesParsed) + var torrent = client.add(common.leaves.parsedTorrent) torrent.on('dhtAnnounce', function () { t.pass('client announced to dht') @@ -102,16 +88,15 @@ test('public torrent should use DHT', function (t) { t.fail('dht server is null') } }) - }] - - }, function (err, r) { - if (err) throw err + } + ], function (err) { + t.error(err) - dhtServer.destroy(function () { - t.pass('dht server destroyed') + dhtServer.destroy(function (err) { + t.error(err, 'dht server destroyed') }) - r.client.destroy(function () { - t.pass('client destroyed') + client.destroy(function (err) { + t.error(err, 'client destroyed') }) }) }) diff --git a/test/download-tracker-magnet.js b/test/download-tracker-magnet.js index b4c89a23..9f240670 100644 --- a/test/download-tracker-magnet.js +++ b/test/download-tracker-magnet.js @@ -1,16 +1,11 @@ -var auto = require('run-auto') +var common = require('./common') +var extend = require('xtend') var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var series = require('run-series') var test = require('tape') var TrackerServer = require('bittorrent-tracker/server') var WebTorrent = require('../') -var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') -var leavesFile = fs.readFileSync(leavesPath) -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - test('Download using UDP tracker (via magnet uri)', function (t) { magnetDownloadTest(t, 'udp') }) @@ -22,41 +17,40 @@ test('Download using HTTP tracker (via magnet uri)', function (t) { function magnetDownloadTest (t, serverType) { t.plan(9) + var tracker = new TrackerServer( + serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } + ) + + tracker.on('error', function (err) { t.fail(err) }) + tracker.on('warning', function (err) { t.fail(err) }) + var trackerStartCount = 0 - var magnetUri + tracker.on('start', function () { + trackerStartCount += 1 + }) - auto({ - tracker: function (cb) { - var tracker = new TrackerServer( - serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } - ) + var parsedTorrent = extend(common.leaves.parsedTorrent) + var magnetUri, client1, client2 - tracker.on('error', function (err) { t.fail(err) }) - tracker.on('warning', function (err) { t.fail(err) }) + series([ + function (cb) { + tracker.listen(cb) + }, - tracker.on('start', function () { - trackerStartCount += 1 - }) + function (cb) { + var port = tracker[serverType].address().port + var announceUrl = serverType === 'http' + ? 'http://127.0.0.1:' + port + '/announce' + : 'udp://127.0.0.1:' + port - tracker.listen(function () { - var port = tracker[serverType].address().port - var announceUrl = serverType === 'http' - ? 'http://127.0.0.1:' + port + '/announce' - : 'udp://127.0.0.1:' + port + parsedTorrent.announce = [ announceUrl ] + magnetUri = 'magnet:?xt=urn:btih:' + parsedTorrent.infoHash + '&tr=' + encodeURIComponent(announceUrl) - leavesParsed.announce = [ announceUrl ] - magnetUri = 'magnet:?xt=urn:btih:' + leavesParsed.infoHash + '&tr=' + encodeURIComponent(announceUrl) - cb(null, tracker) - }) - }, + client1 = new WebTorrent({ dht: false }) - client1: ['tracker', function (cb) { - var client1 = new WebTorrent({ dht: false }) client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - client1.add(leavesParsed) - client1.on('torrent', function (torrent) { // torrent metadata has been fetched -- sanity check it t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') @@ -67,24 +61,25 @@ function magnetDownloadTest (t, serverType) { t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - torrent.load(fs.createReadStream(leavesPath), function (err) { - cb(err, client1) + torrent.load(fs.createReadStream(common.leaves.contentPath), function (err) { + cb(err) }) }) - }], - client2: ['client1', function (cb) { - var client2 = new WebTorrent({ dht: false }) + client1.add(parsedTorrent) + }, + + function (cb) { + client2 = new WebTorrent({ dht: false }) + client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) - client2.add(magnetUri) - client2.on('torrent', function (torrent) { torrent.files.forEach(function (file) { file.getBuffer(function (err, buf) { if (err) throw err - t.deepEqual(buf, leavesFile, 'downloaded correct content') + t.deepEqual(buf, common.leaves.content, 'downloaded correct content') gotBuffer = true maybeDone() }) @@ -99,23 +94,26 @@ function magnetDownloadTest (t, serverType) { var gotBuffer = false var torrentDone = false function maybeDone () { - if (gotBuffer && torrentDone) cb(null, client2) + if (gotBuffer && torrentDone) cb(null) } }) - }] - }, function (err, r) { + client2.add(magnetUri) + } + + ], function (err) { t.error(err) + t.equal(trackerStartCount, 2) - r.tracker.close(function () { + tracker.close(function () { t.pass('tracker closed') }) - r.client1.destroy(function () { - t.pass('client1 destroyed') + client1.destroy(function (err) { + t.error(err, 'client1 destroyed') }) - r.client2.destroy(function () { - t.pass('client2 destroyed') + client2.destroy(function (err) { + t.error(err, 'client2 destroyed') }) }) } diff --git a/test/download-tracker-torrent.js b/test/download-tracker-torrent.js index fdbf559e..1c312369 100644 --- a/test/download-tracker-torrent.js +++ b/test/download-tracker-torrent.js @@ -1,16 +1,11 @@ -var auto = require('run-auto') +var common = require('./common') +var extend = require('xtend') var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var series = require('run-series') var test = require('tape') var TrackerServer = require('bittorrent-tracker/server') var WebTorrent = require('../') -var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') -var leavesFile = fs.readFileSync(leavesPath) -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - test('Download using UDP tracker (via .torrent file)', function (t) { torrentDownloadTest(t, 'udp') }) @@ -23,39 +18,39 @@ function torrentDownloadTest (t, serverType) { t.plan(9) var trackerStartCount = 0 + var parsedTorrent = extend(common.leaves.parsedTorrent) - auto({ - tracker: function (cb) { - var tracker = new TrackerServer( - serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } - ) - - tracker.on('error', function (err) { t.fail(err) }) - tracker.on('warning', function (err) { t.fail(err) }) + var tracker = new TrackerServer( + serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } + ) - tracker.on('start', function () { - trackerStartCount += 1 - }) + tracker.on('error', function (err) { t.fail(err) }) + tracker.on('warning', function (err) { t.fail(err) }) - tracker.listen(function () { - var port = tracker[serverType].address().port - var announceUrl = serverType === 'http' - ? 'http://127.0.0.1:' + port + '/announce' - : 'udp://127.0.0.1:' + port + tracker.on('start', function () { + trackerStartCount += 1 + }) - // Overwrite announce with our local tracker - leavesParsed.announce = [ announceUrl ] + var client1, client2 - cb(null, tracker) - }) + series([ + function (cb) { + tracker.listen(cb) }, - client1: ['tracker', function (cb) { - var client1 = new WebTorrent({ dht: false }) + function (cb) { + client1 = new WebTorrent({ dht: false }) client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - client1.add(leavesParsed) + var port = tracker[serverType].address().port + + var announceUrl = serverType === 'http' + ? 'http://127.0.0.1:' + port + '/announce' + : 'udp://127.0.0.1:' + port + + // Overwrite announce with our local tracker + parsedTorrent.announce = [ announceUrl ] client1.on('torrent', function (torrent) { // torrent metadata has been fetched -- sanity check it @@ -67,24 +62,24 @@ function torrentDownloadTest (t, serverType) { t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - torrent.load(fs.createReadStream(leavesPath), function (err) { - cb(err, client1) - }) + torrent.load(fs.createReadStream(common.leaves.contentPath), cb) }) - }], - client2: ['client1', function (cb) { - var client2 = new WebTorrent({ dht: false }) + client1.add(parsedTorrent) + }, + + function (cb) { + client2 = new WebTorrent({ dht: false }) client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) - client2.add(leavesParsed) + client2.add(parsedTorrent) client2.on('torrent', function (torrent) { torrent.files.forEach(function (file) { file.getBuffer(function (err, buf) { if (err) throw err - t.deepEqual(buf, leavesFile, 'downloaded correct content') + t.deepEqual(buf, common.leaves.content, 'downloaded correct content') gotBuffer = true maybeDone() }) @@ -99,23 +94,23 @@ function torrentDownloadTest (t, serverType) { var gotBuffer = false var torrentDone = false function maybeDone () { - if (gotBuffer && torrentDone) cb(null, client2) + if (gotBuffer && torrentDone) cb(null) } }) - }] + } - }, function (err, r) { + ], function (err) { t.error(err) t.equal(trackerStartCount, 2) - r.tracker.close(function () { + tracker.close(function () { t.pass('tracker closed') }) - r.client1.destroy(function () { - t.pass('client1 destroyed') + client1.destroy(function (err) { + t.error(err, 'client1 destroyed') }) - r.client2.destroy(function () { - t.pass('client2 destroyed') + client2.destroy(function (err) { + t.error(err, 'client2 destroyed') }) }) } diff --git a/test/download-webseed-magnet.js b/test/download-webseed-magnet.js index ca11b0f3..18089890 100644 --- a/test/download-webseed-magnet.js +++ b/test/download-webseed-magnet.js @@ -1,49 +1,41 @@ -var auto = require('run-auto') +var common = require('./common') var finalhandler = require('finalhandler') -var fs = require('fs') var http = require('http') -var parseTorrent = require('parse-torrent') var path = require('path') +var series = require('run-series') var serveStatic = require('serve-static') var test = require('tape') var WebTorrent = require('../') -var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') -var leavesFilename = 'Leaves of Grass by Walt Whitman.epub' -var leavesFile = fs.readFileSync(leavesPath) -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - -// remove trackers from .torrent file -leavesParsed.announce = [] - test('Download using webseed (via magnet uri)', function (t) { t.plan(9) + var parsedTorrent = common.leaves.parsedTorrent + var serve = serveStatic(path.join(__dirname, 'content')) var httpServer = http.createServer(function (req, res) { var done = finalhandler(req, res) serve(req, res, done) }) - var magnetUri + var client1, client2 httpServer.on('error', function (err) { t.fail(err) }) - auto({ - httpPort: function (cb) { + series([ + function (cb) { httpServer.listen(cb) }, - client1: ['httpPort', function (cb) { - var client1 = new WebTorrent({ tracker: false, dht: false }) + + function (cb) { + client1 = new WebTorrent({ tracker: false, dht: false }) + client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - client1.add(leavesParsed) - var gotTorrent = false var gotListening = false function maybeDone () { - if (gotTorrent && gotListening) cb(null, client1) + if (gotTorrent && gotListening) cb(null) } client1.on('torrent', function (torrent) { @@ -65,22 +57,25 @@ test('Download using webseed (via magnet uri)', function (t) { gotListening = true maybeDone() }) - }], - client2: ['client1', 'httpPort', function (cb, r) { - var webSeedUrl = 'http://localhost:' + httpServer.address().port + '/' + leavesFilename - magnetUri = 'magnet:?xt=urn:btih:' + leavesParsed.infoHash + - '&ws=' + encodeURIComponent(webSeedUrl) - var client2 = new WebTorrent({ tracker: false, dht: false }) + client1.add(parsedTorrent) + }, + + function (cb) { + client2 = new WebTorrent({ tracker: false, dht: false }) client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) + var webSeedUrl = 'http://localhost:' + httpServer.address().port + '/' + common.leaves.parsedTorrent.name + var magnetUri = 'magnet:?xt=urn:btih:' + parsedTorrent.infoHash + + '&ws=' + encodeURIComponent(webSeedUrl) + client2.on('torrent', function (torrent) { torrent.files.forEach(function (file) { file.getBuffer(function (err, buf) { t.error(err) - t.deepEqual(buf, leavesFile, 'downloaded correct content') + t.deepEqual(buf, common.leaves.content, 'downloaded correct content') gotBuffer = true maybeDone() }) @@ -95,23 +90,23 @@ test('Download using webseed (via magnet uri)', function (t) { var gotBuffer = false var torrentDone = false function maybeDone () { - if (gotBuffer && torrentDone) cb(null, client2) + if (gotBuffer && torrentDone) cb(null) } }) - client2.add(magnetUri) - client2.on('listening', function (port, torrent) { - torrent.addPeer('127.0.0.1:' + r.client1.torrentPort) + torrent.addPeer('127.0.0.1:' + client1.address().port) }) - }] - }, function (err, r) { + + client2.add(magnetUri) + } + ], function (err) { t.error(err) - r.client1.destroy(function () { - t.pass('client destroyed') + client1.destroy(function (err) { + t.error(err, 'client destroyed') }) - r.client2.destroy(function () { - t.pass('client destroyed') + client2.destroy(function (err) { + t.error(err, 'client destroyed') }) httpServer.close(function () { t.pass('http server closed') diff --git a/test/download-webseed-torrent.js b/test/download-webseed-torrent.js index 7cbc8895..0caa4e7b 100644 --- a/test/download-webseed-torrent.js +++ b/test/download-webseed-torrent.js @@ -1,43 +1,37 @@ -var auto = require('run-auto') +var common = require('./common') +var extend = require('xtend') var finalhandler = require('finalhandler') -var fs = require('fs') var http = require('http') -var parseTorrent = require('parse-torrent') var path = require('path') +var series = require('run-series') var serveStatic = require('serve-static') var test = require('tape') var WebTorrent = require('../') -var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') -var leavesFilename = 'Leaves of Grass by Walt Whitman.epub' -var leavesFile = fs.readFileSync(leavesPath) -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - -// remove trackers from .torrent file -leavesParsed.announce = [] - test('Download using webseed (via .torrent file)', function (t) { t.plan(6) - var serve = serveStatic(path.join(__dirname, 'content')) + var parsedTorrent = extend(common.leaves.parsedTorrent) + var httpServer = http.createServer(function (req, res) { var done = finalhandler(req, res) - serve(req, res, done) + serveStatic(path.join(__dirname, 'content'))(req, res, done) }) + var client httpServer.on('error', function (err) { t.fail(err) }) - auto({ - httpPort: function (cb) { + series([ + function (cb) { httpServer.listen(cb) }, - client: ['httpPort', function (cb) { - leavesParsed.urlList.push( - 'http://localhost:' + httpServer.address().port + '/' + leavesFilename - ) - var client = new WebTorrent({ tracker: false, dht: false }) + function (cb) { + parsedTorrent.urlList = [ + 'http://localhost:' + httpServer.address().port + '/' + common.leaves.parsedTorrent.name + ] + + client = new WebTorrent({ tracker: false, dht: false }) client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) @@ -46,7 +40,7 @@ test('Download using webseed (via .torrent file)', function (t) { torrent.files.forEach(function (file) { file.getBuffer(function (err, buf) { t.error(err) - t.deepEqual(buf, leavesFile, 'downloaded correct content') + t.deepEqual(buf, common.leaves.content, 'downloaded correct content') gotBuffer = true maybeDone() }) @@ -61,16 +55,16 @@ test('Download using webseed (via .torrent file)', function (t) { var gotBuffer = false var torrentDone = false function maybeDone () { - if (gotBuffer && torrentDone) cb(null, client) + if (gotBuffer && torrentDone) cb(null) } }) - client.add(leavesParsed) - }] - }, function (err, r) { + client.add(parsedTorrent) + } + ], function (err) { t.error(err) - r.client.destroy(function () { - t.pass('client destroyed') + client.destroy(function (err) { + t.error(err, 'client destroyed') }) httpServer.close(function () { t.pass('http server closed') diff --git a/test/duplicates.js b/test/duplicates.js index a89f649a..b19355ea 100644 --- a/test/duplicates.js +++ b/test/duplicates.js @@ -1,52 +1,54 @@ -var fs = require('fs') -var path = require('path') +var common = require('./common') var test = require('tape') var WebTorrent = require('../') -var leavesBook = fs.readFileSync(path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')) - test('client.seed followed by duplicate client.add', function (t) { - t.plan(3) - - var opts = { - name: 'Leaves of Grass by Walt Whitman.epub' - } + t.plan(5) var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.seed(leavesBook, opts, function (torrent1) { + client.seed(common.leaves.content, { + name: 'Leaves of Grass by Walt Whitman.epub' + }, function (torrent1) { + t.equal(client.torrents.length, 1) + client.add(torrent1.infoHash, function (torrent2) { t.equal(torrent1.infoHash, torrent2.infoHash) t.equal(client.torrents.length, 1) - client.destroy(function () { - t.pass('destroyed client') + + client.destroy(function (err) { + t.error(err, 'destroyed client') + t.equal(client.torrents.length, 0) }) }) }) }) -test('client.seed followed by duplicate client.add, twice', function (t) { - t.plan(5) - - var opts = { - name: 'Leaves of Grass by Walt Whitman.epub' - } +test('client.seed followed by two duplicate client.add calls', function (t) { + t.plan(7) var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.seed(leavesBook, opts, function (torrent1) { + client.seed(common.leaves.content, { + name: 'Leaves of Grass by Walt Whitman.epub' + }, function (torrent1) { + t.equal(client.torrents.length, 1) + client.add(torrent1.infoHash, function (torrent2) { t.equal(torrent1.infoHash, torrent2.infoHash) t.equal(client.torrents.length, 1) + client.add(torrent1.infoHash, function (torrent2) { t.equal(torrent1.infoHash, torrent2.infoHash) t.equal(client.torrents.length, 1) - client.destroy(function () { - t.pass('destroyed client') + + client.destroy(function (err) { + t.error(err, 'destroyed client') + t.equal(client.torrents.length, 0) }) }) }) diff --git a/test/extensions.js b/test/extensions.js index 82943d75..4278a133 100644 --- a/test/extensions.js +++ b/test/extensions.js @@ -25,11 +25,11 @@ test('extension support', function (t) { ) if (extendedHandshakes === 2) { - client1.destroy(function () { - t.pass('client1 destroyed') + client1.destroy(function (err) { + t.error(err, 'client1 destroyed') }) - client2.destroy(function () { - t.pass('client2 destroyed') + client2.destroy(function (err) { + t.error(err, 'client2 destroyed') }) } } @@ -55,7 +55,7 @@ test('extension support', function (t) { wire.use(Extension) }) client2.on('listening', function () { - torrent2.addPeer('127.0.0.1:' + client1.torrentPort) + torrent2.addPeer('127.0.0.1:' + client1.address().port) }) }) }) diff --git a/test/metadata.js b/test/metadata.js index efcfbab2..891cb215 100644 --- a/test/metadata.js +++ b/test/metadata.js @@ -11,12 +11,11 @@ test('ut_metadata transfer', function (t) { t.plan(6) var client1 = new WebTorrent({ dht: false, tracker: false }) + var client2 = new WebTorrent({ dht: false, tracker: false }) client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - var client2 = new WebTorrent({ dht: false, tracker: false }) - client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) @@ -36,16 +35,16 @@ test('ut_metadata transfer', function (t) { client2.on('listening', function (port, torrent2) { // manually add the peer - torrent2.addPeer('127.0.0.1:' + client1.torrentPort) + torrent2.addPeer('127.0.0.1:' + client1.address().port) client2.on('torrent', function () { t.deepEqual(torrent1.info, torrent2.info) - client1.destroy(function () { - t.pass('client1 destroyed') + client1.destroy(function (err) { + t.error(err, 'client1 destroyed') }) - client2.destroy(function () { - t.pass('client2 destroyed') + client2.destroy(function (err) { + t.error(err, 'client2 destroyed') }) }) }) diff --git a/test/server.js b/test/server.js index 8571a9ea..cd5801db 100644 --- a/test/server.js +++ b/test/server.js @@ -29,17 +29,21 @@ test('torrent.createServer: programmatic http server', function (t) { // Index page should list files in the torrent get.concat(host + '/', function (err, data) { - t.error(err) + t.error(err, 'got http response for /') data = data.toString() t.ok(data.indexOf('Leaves of Grass by Walt Whitman.epub') !== -1) // Verify file content for first (and only) file get.concat(host + '/0', function (err, data) { - t.error(err) + t.error(err, 'got http response for /0') t.deepEqual(data, common.leaves.content) - server.close(function () { t.pass('server closed') }) - client.destroy(function () { t.pass('client destroyed') }) + server.close(function () { + t.pass('server closed') + }) + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) }) }) }) From 0fd98edb7286688649d5715ce1cd3ca84b1ad35f Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 22 Dec 2015 11:32:24 +1300 Subject: [PATCH 8/8] test: remove sample trackers --- test/cmd.js | 2 +- test/torrents/big-buck-bunny-private.torrent | Bin 17270 -> 17058 bytes 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cmd.js b/test/cmd.js index 3cece10c..cf7dc3c3 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -70,7 +70,7 @@ test('Command line: webtorrent info /path/to/file.torrent', function (t) { test('Command line: webtorrent info magnet_uri', function (t) { t.plan(2) - var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80' + var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.example.com%2Fannounce&tr=http%3A%2F%2Ftracker.example2.com%2Fannounce&tr=udp%3A%2F%2Ftracker.example3.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.example4.com%3A80&tr=udp%3A%2F%2Ftracker.example5.com%3A80&tr=udp%3A%2F%2Ftracker.example6.com%3A80' cp.exec(CMD + ' info "' + leavesMagnetURI + '"', function (err, data) { t.error(err) diff --git a/test/torrents/big-buck-bunny-private.torrent b/test/torrents/big-buck-bunny-private.torrent index 25018e7ddc8c39a81fd0816dc8da39e7f00aac37..da15903df944b1fb7c9e7b5d87de9c21c6df1484 100644 GIT binary patch delta 12 Tcmey?#<-}JkuhcC)rC#~BmxC= delta 225 zcmZ3~%J{8~u|CDZDlsoFzcep7)x^ZAG^M~wU%#X%F*!T6NH4!2H7_Z%q$IznC^fG{ zFF8Ng%ECY&rrgjNu2VNBv$!NDheQKXbBry*(5x&dP0GnkPQtJ(HPze-Xn!uy9md91 oPMPTnPNm7&Kq4=%Qb!@UG%qbDzdW@_p)9qiI5R(QBjZ9R0I{D^m;e9(