From 748347861fc024dd6451be76d646a22ed7d1369b Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 22 Dec 2015 10:46:04 +1300 Subject: 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 --- 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 +++-- 15 files changed, 374 insertions(+), 393 deletions(-) (limited to 'test') diff --git a/test/blocklist-dht.js b/test/blocklist-dht.js index 0233c7c..852dc17 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 0d33441..1156069 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 2f4e165..1471ca9 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 c65ccb1..a2e14ad 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 b9c0869..02e9e17 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 9985629..5888bbe 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 63ad74c..812f57c 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 b4c89a2..9f24067 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 fdbf559..1c31236 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 ca11b0f..1808989 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 7cbc889..0caa4e7 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 a89f649..b19355e 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 82943d7..4278a13 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 efcfbab..891cb21 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 8571a9e..cd5801d 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') + }) }) }) }) -- cgit v1.2.3