diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2015-01-04 00:15:41 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2015-01-04 00:15:41 +0300 |
commit | fcf1912c93c1d2d53a489323ac15e6babd566875 (patch) | |
tree | 38fca0d99964ac272f8f6ed17733cde1e2d67693 | |
parent | 07fd715c2518e0bdb801cb622c2c977c2cd716d7 (diff) |
Add DHT magnet uri test, udp tracker magnet uri test
-rw-r--r-- | test/download-dht-magnet.js | 98 | ||||
-rw-r--r-- | test/download-dht-torrent.js | 97 | ||||
-rw-r--r-- | test/download-tracker-magnet.js | 106 | ||||
-rw-r--r-- | test/download-tracker-torrent.js | 106 | ||||
-rw-r--r-- | test/download.js | 278 |
5 files changed, 407 insertions, 278 deletions
diff --git a/test/download-dht-magnet.js b/test/download-dht-magnet.js new file mode 100644 index 0000000..f76a8de --- /dev/null +++ b/test/download-dht-magnet.js @@ -0,0 +1,98 @@ +var auto = require('run-auto') +var DHT = require('bittorrent-dht/server') +var fs = require('fs') +var parseTorrent = require('parse-torrent') +var test = require('tape') +var WebTorrent = require('../') + +var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' +var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') +var leavesParsed = parseTorrent(leavesTorrent) + +test('Download using DHT (via magnet uri)', function (t) { + t.plan(7) + + // remove trackers from .torrent file + leavesParsed.announce = [] + leavesParsed.announceList = [] + + var dhtServer = new DHT({ bootstrap: false }) + dhtServer.on('error', function (err) { + t.fail(err) + }) + + var magnetUri = 'magnet:?xt=urn:btih:' + leavesParsed.infoHash + + auto({ + dhtPort: function (cb) { + dhtServer.listen(function (port) { + cb(null, port) + }) + }, + client1: ['dhtPort', function (cb, r) { + var client1 = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + }) + client1.on('error', function (err) { t.fail(err) }) + + client1.add(leavesParsed) + + var announced, wroteStorage + function maybeDone (err) { + if ((announced && wroteStorage) || err) cb(err, client1) + } + + client1.on('torrent', function (torrent) { + // torrent metadata has been fetched -- sanity check it + t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') + + var names = [ 'Leaves of Grass by Walt Whitman.epub' ] + t.deepEqual(torrent.files.map(function (file) { return file.name }), names) + + torrent.on('dhtAnnounce', function () { + announced = true + maybeDone(null) + }) + + torrent.storage.load(fs.createReadStream(leavesFile), function (err) { + wroteStorage = true + maybeDone(err) + }) + }) + }], + + client2: ['client1', function (cb, r) { + var client2 = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + }) + client2.on('error', function (err) { t.fail(err) }) + + client2.add(magnetUri) + + client2.on('torrent', function (torrent) { + torrent.files.forEach(function (file) { + file.createReadStream() + }) + + torrent.once('done', function () { + t.pass('client2 downloaded torrent from client1') + cb(null, client2) + }) + }) + }], + + }, function (err, r) { + t.error(err) + r.client1.destroy(function () { + t.pass('client1 destroyed') + }) + r.client2.destroy(function () { + t.pass('client2 destroyed') + }) + dhtServer.destroy(function () { + t.pass('dht server destroyed') + }) + }) +}) diff --git a/test/download-dht-torrent.js b/test/download-dht-torrent.js new file mode 100644 index 0000000..74f8971 --- /dev/null +++ b/test/download-dht-torrent.js @@ -0,0 +1,97 @@ +var auto = require('run-auto') +var WebTorrent = require('../') +var DHT = require('bittorrent-dht/server') +var fs = require('fs') +var parseTorrent = require('parse-torrent') +var test = require('tape') + +var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' +var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') +var leavesParsed = parseTorrent(leavesTorrent) + +test('Download using DHT (via .torrent file)', function (t) { + t.plan(7) + + // remove trackers from .torrent file + leavesParsed.announce = [] + leavesParsed.announceList = [] + + var dhtServer = new DHT({ bootstrap: false }) + + dhtServer.on('error', function (err) { + t.fail(err) + }) + + auto({ + dhtPort: function (cb) { + dhtServer.listen(function (port) { + cb(null, port) + }) + }, + client1: ['dhtPort', function (cb, r) { + var client1 = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + }) + client1.on('error', function (err) { t.fail(err) }) + + client1.add(leavesParsed) + + var announced, wroteStorage + function maybeDone (err) { + if ((announced && wroteStorage) || err) cb(err, client1) + } + + client1.on('torrent', function (torrent) { + // torrent metadata has been fetched -- sanity check it + t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') + + var names = [ 'Leaves of Grass by Walt Whitman.epub' ] + t.deepEqual(torrent.files.map(function (file) { return file.name }), names) + + torrent.on('dhtAnnounce', function () { + announced = true + maybeDone(null) + }) + + torrent.storage.load(fs.createReadStream(leavesFile), function (err) { + wroteStorage = true + maybeDone(err) + }) + }) + }], + + client2: ['client1', function (cb, r) { + var client2 = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + }) + client2.on('error', function (err) { t.fail(err) }) + + client2.add(leavesParsed) + + client2.on('torrent', function (torrent) { + torrent.files.forEach(function (file) { + file.createReadStream() + }) + + torrent.once('done', function () { + t.pass('client2 downloaded torrent from client1') + cb(null, client2) + }) + }) + }], + + }, function (err, r) { + t.error(err) + r.client1.destroy(function () { + t.pass('client1 destroyed') + }) + r.client2.destroy(function () { + t.pass('client2 destroyed') + }) + dhtServer.destroy(function () { + t.pass('dht server destroyed') + }) + }) +}) diff --git a/test/download-tracker-magnet.js b/test/download-tracker-magnet.js new file mode 100644 index 0000000..8d44abc --- /dev/null +++ b/test/download-tracker-magnet.js @@ -0,0 +1,106 @@ +var auto = require('run-auto') +var fs = require('fs') +var parseTorrent = require('parse-torrent') +var test = require('tape') +var TrackerServer = require('bittorrent-tracker/server') +var WebTorrent = require('../') + +var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' +var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') +var leavesParsed = parseTorrent(leavesTorrent) + +test('Download using UDP tracker (via magnet uri)', function (t) { + magnetDownloadTest(t, 'udp') +}) + +test('Download using HTTP tracker (via magnet uri)', function (t) { + magnetDownloadTest(t, 'http') +}) + +function magnetDownloadTest (t, serverType) { + t.plan(8) + + var trackerStartCount = 0 + var magnetUri + + auto({ + tracker: function (cb) { + var tracker = new TrackerServer( + serverType === 'udp' ? { http: false } : { udp: false } + ) + + tracker.on('error', function (err) { + t.fail(err) + }) + + tracker.on('start', function () { + trackerStartCount += 1 + }) + + tracker.listen(function (port) { + var announceUrl = serverType === 'http' + ? 'http://127.0.0.1:' + port + '/announce' + : 'udp://127.0.0.1:' + port + + leavesParsed.announce = [ announceUrl ] + leavesParsed.announceList = [[ announceUrl ]] + magnetUri = 'magnet:?xt=urn:btih:' + leavesParsed.infoHash + '&tr=' + encodeURIComponent(announceUrl) + cb(null, tracker) + }) + }, + + client1: ['tracker', function (cb) { + var client1 = new WebTorrent({ dht: false }) + client1.on('error', 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') + + var names = [ + 'Leaves of Grass by Walt Whitman.epub' + ] + + t.deepEqual(torrent.files.map(function (file) { return file.name }), names) + + torrent.storage.load(fs.createReadStream(leavesFile), function (err) { + cb(err, client1) + }) + }) + }], + + client2: ['client1', function (cb) { + var client2 = new WebTorrent({ dht: false }) + client2.on('error', function (err) { t.fail(err) }) + + client2.add(magnetUri) + + client2.on('torrent', function (torrent) { + torrent.files.forEach(function (file) { + file.createReadStream() + }) + + torrent.once('done', function () { + t.pass('client2 downloaded torrent from client1') + cb(null, client2) + }) + }) + }] + + }, function (err, r) { + t.error(err) + t.equal(trackerStartCount, 2) + + r.tracker.close(function () { + t.pass('tracker closed') + }) + r.client1.destroy(function () { + t.pass('client1 destroyed') + }) + r.client2.destroy(function () { + t.pass('client2 destroyed') + }) + }) +} diff --git a/test/download-tracker-torrent.js b/test/download-tracker-torrent.js new file mode 100644 index 0000000..0838b0b --- /dev/null +++ b/test/download-tracker-torrent.js @@ -0,0 +1,106 @@ +var auto = require('run-auto') +var fs = require('fs') +var parseTorrent = require('parse-torrent') +var test = require('tape') +var TrackerServer = require('bittorrent-tracker/server') +var WebTorrent = require('../') + +var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' +var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') +var leavesParsed = parseTorrent(leavesTorrent) + +test('Download using UDP tracker (via .torrent file)', function (t) { + torrentDownloadTest(t, 'udp') +}) + +test('Download using HTTP tracker (via .torrent file)', function (t) { + torrentDownloadTest(t, 'http') +}) + +function torrentDownloadTest (t, serverType) { + t.plan(8) + + var trackerStartCount = 0 + + auto({ + tracker: function (cb) { + var tracker = new TrackerServer( + serverType === 'udp' ? { http: false } : { udp: false } + ) + + tracker.on('error', function (err) { + t.fail(err) + }) + + tracker.on('start', function () { + trackerStartCount += 1 + }) + + tracker.listen(function (port) { + var announceUrl = serverType === 'http' + ? 'http://127.0.0.1:' + port + '/announce' + : 'udp://127.0.0.1:' + port + + // Overwrite announce with our local tracker + leavesParsed.announce = [ announceUrl ] + leavesParsed.announceList = [[ announceUrl ]] + + cb(null, tracker) + }) + }, + + client1: ['tracker', function (cb) { + var client1 = new WebTorrent({ dht: false }) + client1.on('error', 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') + + var names = [ + 'Leaves of Grass by Walt Whitman.epub' + ] + + t.deepEqual(torrent.files.map(function (file) { return file.name }), names) + + torrent.storage.load(fs.createReadStream(leavesFile), function (err) { + cb(err, client1) + }) + }) + }], + + client2: ['client1', function (cb) { + var client2 = new WebTorrent({ dht: false }) + client2.on('error', function (err) { t.fail(err) }) + + client2.add(leavesParsed) + + client2.on('torrent', function (torrent) { + torrent.files.forEach(function (file) { + file.createReadStream() + }) + + torrent.once('done', function () { + t.pass('client2 downloaded torrent from client1') + cb(null, client2) + }) + }) + }] + + }, function (err, r) { + t.error(err) + t.equal(trackerStartCount, 2) + + r.tracker.close(function () { + t.pass('tracker closed') + }) + r.client1.destroy(function () { + t.pass('client1 destroyed') + }) + r.client2.destroy(function () { + t.pass('client2 destroyed') + }) + }) +} diff --git a/test/download.js b/test/download.js deleted file mode 100644 index 3e895e4..0000000 --- a/test/download.js +++ /dev/null @@ -1,278 +0,0 @@ -var auto = require('run-auto') -var WebTorrent = require('../') -var DHT = require('bittorrent-dht/client') -var fs = require('fs') -var parseTorrent = require('parse-torrent') -var test = require('tape') -var TrackerServer = require('bittorrent-tracker/server') - -var leavesFile = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' -var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') -var leavesParsed = parseTorrent(leavesTorrent) - -function downloadTrackerTest (t, serverType) { - t.plan(8) - - var trackerStartCount = 0 - - auto({ - tracker: function (cb) { - var tracker = new TrackerServer( - serverType === 'udp' ? { http: false } : { udp: false } - ) - - tracker.on('error', function (err) { - t.fail(err) - }) - - tracker.on('start', function () { - trackerStartCount += 1 - }) - - tracker.listen(function (port) { - var announceUrl = serverType === 'http' - ? 'http://127.0.0.1:' + port + '/announce' - : 'udp://127.0.0.1:' + port - - // Overwrite announce with our local tracker - leavesParsed.announce = [ announceUrl ] - leavesParsed.announceList = [[ announceUrl ]] - - cb(null, tracker) - }) - }, - - client1: ['tracker', function (cb) { - var client1 = new WebTorrent({ dht: false }) - client1.on('error', 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') - - var names = [ - 'Leaves of Grass by Walt Whitman.epub' - ] - - t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - - torrent.storage.load(fs.createReadStream(leavesFile), function (err) { - cb(err, client1) - }) - }) - }], - - client2: ['client1', function (cb) { - var client2 = new WebTorrent({ dht: false }) - client2.on('error', function (err) { t.fail(err) }) - - client2.add(leavesParsed) - - client2.on('torrent', function (torrent) { - torrent.files.forEach(function (file) { - file.createReadStream() - }) - - torrent.once('done', function () { - t.pass('client2 downloaded torrent from client1') - cb(null, client2) - }) - }) - }] - - }, function (err, r) { - t.error(err) - t.equal(trackerStartCount, 2) - - r.tracker.close(function () { - t.pass('tracker closed') - }) - r.client1.destroy(function () { - t.pass('client1 destroyed') - }) - r.client2.destroy(function () { - t.pass('client2 destroyed') - }) - }) -} - -test('Simple download using UDP tracker', function (t) { - downloadTrackerTest(t, 'udp') -}) - -test('Simple download using HTTP tracker', function (t) { - downloadTrackerTest(t, 'http') -}) - -test('Simple download using a tracker (only) via a magnet uri', function (t) { - t.plan(8) - - var trackerStartCount = 0 - - var magnetUri - auto({ - tracker: function (cb) { - var tracker = new TrackerServer('udp') - - tracker.on('error', function (err) { - t.fail(err) - }) - - tracker.on('start', function () { - trackerStartCount += 1 - }) - - tracker.listen(function (port) { - var announceUrl = 'udp://127.0.0.1:' + port - leavesParsed.announce = [ announceUrl ] - leavesParsed.announceList = [[ announceUrl ]] - magnetUri = 'magnet:?xt=urn:btih:' + leavesParsed.infoHash + '&tr=' + encodeURIComponent(announceUrl) - cb(null, tracker) - }) - }, - - client1: ['tracker', function (cb) { - var client1 = new WebTorrent({ dht: false }) - client1.on('error', 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') - - var names = [ - 'Leaves of Grass by Walt Whitman.epub' - ] - - t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - - torrent.storage.load(fs.createReadStream(leavesFile), function (err) { - cb(err, client1) - }) - }) - }], - - client2: ['client1', function (cb) { - var client2 = new WebTorrent({ dht: false }) - client2.on('error', function (err) { t.fail(err) }) - - client2.add(magnetUri) - - client2.on('torrent', function (torrent) { - torrent.files.forEach(function (file) { - file.createReadStream() - }) - - torrent.once('done', function () { - t.pass('client2 downloaded torrent from client1') - cb(null, client2) - }) - }) - }] - - }, function (err, r) { - t.error(err) - t.equal(trackerStartCount, 2) - - r.tracker.close(function () { - t.pass('tracker closed') - }) - r.client1.destroy(function () { - t.pass('client1 destroyed') - }) - r.client2.destroy(function () { - t.pass('client2 destroyed') - }) - }) -}) - -test('Simple download using DHT', function (t) { - t.plan(7) - - // no trackers - leavesParsed.announce = [] - leavesParsed.announceList = [] - - // TODO: use actual DHT server here, instead of client - var dhtServer = new DHT({ bootstrap: false }) - - dhtServer.on('error', function (err) { - t.fail(err) - }) - - auto({ - dhtPort: function (cb) { - dhtServer.listen(function (port) { - cb(null, port) - }) - }, - client1: ['dhtPort', function (cb, r) { - var client1 = new WebTorrent({ - tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort } - }) - client1.on('error', function (err) { t.fail(err) }) - - client1.add(leavesParsed) - - var announced, wroteStorage - function maybeDone (err) { - if ((announced && wroteStorage) || err) cb(err, client1) - } - - client1.on('torrent', function (torrent) { - // torrent metadata has been fetched -- sanity check it - t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') - - var names = [ 'Leaves of Grass by Walt Whitman.epub' ] - t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - - torrent.on('dhtAnnounce', function () { - announced = true - maybeDone(null) - }) - - torrent.storage.load(fs.createReadStream(leavesFile), function (err) { - wroteStorage = true - maybeDone(err) - }) - }) - }], - - client2: ['client1', function (cb, r) { - var client2 = new WebTorrent({ - tracker: false, - dht: { bootstrap: '127.0.0.1:' + r.dhtPort } - }) - client2.on('error', function (err) { t.fail(err) }) - - client2.add(leavesParsed) - - client2.on('torrent', function (torrent) { - torrent.files.forEach(function (file) { - file.createReadStream() - }) - - torrent.once('done', function () { - t.pass('client2 downloaded torrent from client1') - cb(null, client2) - }) - }) - }], - - }, function (err, r) { - t.error(err) - r.client1.destroy(function () { - t.pass('client1 destroyed') - }) - r.client2.destroy(function () { - t.pass('client2 destroyed') - }) - dhtServer.destroy(function () { - t.pass('dht server destroyed') - }) - }) -}) |