diff --git a/README.md b/README.md index 2a19129..f06ebbf 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ var Client = require('bittorrent-tracker') var parseTorrent = require('parse-torrent') var fs = require('fs') -var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent') +var torrent = fs.readFileSync(__dirname + '/torrents/name.torrent') var parsedTorrent = parseTorrent(torrent) // { infoHash: 'xxx', length: xx, announce: ['xx', 'xx'] } var peerId = new Buffer('01234567890123456789') diff --git a/package.json b/package.json index c4bb576..d5e8826 100644 --- a/package.json +++ b/package.json @@ -42,10 +42,10 @@ "xtend": "^4.0.0" }, "devDependencies": { - "magnet-uri": "^5.0.0", - "parse-torrent": "^5.0.0", + "magnet-uri": "^5.1.3", "standard": "^6.0.4", - "tape": "^4.0.0" + "tape": "^4.0.0", + "webtorrent-fixtures": "^1.3.0" }, "keywords": [ "bittorrent", diff --git a/test/client-large-torrent.js b/test/client-large-torrent.js index e0c77bf..b63d482 100644 --- a/test/client-large-torrent.js +++ b/test/client-large-torrent.js @@ -1,17 +1,16 @@ var Client = require('../') var common = require('./common') -var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var extend = require('xtend') +var fixtures = require('webtorrent-fixtures') var test = require('tape') -var torrent = fs.readFileSync(path.join(__dirname, 'torrents/sintel-5gb.torrent')) -var parsedTorrent = parseTorrent(torrent) var peerId = new Buffer('01234567890123456789') function testLargeTorrent (t, serverType) { t.plan(9) + var parsedTorrent = extend(fixtures.sintel.parsedTorrent) + common.createServer(t, serverType, function (server, announceUrl) { parsedTorrent.announce = [ announceUrl ] var client = new Client(peerId, 6881, parsedTorrent, { wrtc: {} }) diff --git a/test/client-magnet.js b/test/client-magnet.js index a08fe29..246209d 100644 --- a/test/client-magnet.js +++ b/test/client-magnet.js @@ -1,15 +1,16 @@ var Client = require('../') var common = require('./common') +var fixtures = require('webtorrent-fixtures') var magnet = require('magnet-uri') var test = require('tape') -var uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub' -var parsedTorrent = magnet(uri) var peerId = new Buffer('01234567890123456789') function testMagnet (t, serverType) { t.plan(9) + var parsedTorrent = magnet(fixtures.leaves.magnetURI) + common.createServer(t, serverType, function (server, announceUrl) { parsedTorrent.announce = [ announceUrl ] diff --git a/test/client.js b/test/client.js index 349ff51..e8488e8 100644 --- a/test/client.js +++ b/test/client.js @@ -1,12 +1,9 @@ var Client = require('../') var common = require('./common') -var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var extend = require('xtend') +var fixtures = require('webtorrent-fixtures') var test = require('tape') -var torrent = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent')) -var parsedTorrent = parseTorrent(torrent) var peerId1 = new Buffer('01234567890123456789') var peerId2 = new Buffer('12345678901234567890') var peerId3 = new Buffer('23456789012345678901') @@ -14,6 +11,9 @@ var port = 6881 function testClientStart (t, serverType) { t.plan(4) + + var parsedTorrent = extend(fixtures.leaves.parsedTorrent) + common.createServer(t, serverType, function (server, announceUrl) { parsedTorrent.announce = [ announceUrl ] var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} }) @@ -54,6 +54,9 @@ test('ws: client.start()', function (t) { function testClientStop (t, serverType) { t.plan(3) + + var parsedTorrent = extend(fixtures.leaves.parsedTorrent) + common.createServer(t, serverType, function (server, announceUrl) { parsedTorrent.announce = [ announceUrl ] var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} }) @@ -94,6 +97,9 @@ test('ws: client.stop()', function (t) { function testClientUpdate (t, serverType) { t.plan(4) + + var parsedTorrent = extend(fixtures.leaves.parsedTorrent) + common.createServer(t, serverType, function (server, announceUrl) { parsedTorrent.announce = [ announceUrl ] var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} }) @@ -141,6 +147,9 @@ test('ws: client.update()', function (t) { function testClientScrape (t, serverType) { t.plan(4) + + var parsedTorrent = extend(fixtures.leaves.parsedTorrent) + common.createServer(t, serverType, function (server, announceUrl) { parsedTorrent.announce = [ announceUrl ] var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} }) @@ -178,6 +187,9 @@ test('udp: client.scrape()', function (t) { function testClientAnnounceWithParams (t, serverType) { t.plan(5) + + var parsedTorrent = extend(fixtures.leaves.parsedTorrent) + common.createServer(t, serverType, function (server, announceUrl) { parsedTorrent.announce = [ announceUrl ] var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} }) @@ -220,6 +232,9 @@ test('ws: client.announce() with params', function (t) { function testClientGetAnnounceOpts (t, serverType) { t.plan(5) + + var parsedTorrent = extend(fixtures.leaves.parsedTorrent) + common.createServer(t, serverType, function (server, announceUrl) { parsedTorrent.announce = [ announceUrl ] var opts = { @@ -268,6 +283,9 @@ test('ws: client `opts.getAnnounceOpts`', function (t) { function testClientAnnounceWithNumWant (t, serverType) { t.plan(4) + + var parsedTorrent = extend(fixtures.leaves.parsedTorrent) + common.createServer(t, serverType, function (server, announceUrl) { parsedTorrent.announce = [ announceUrl ] var client1 = new Client(peerId1, port, parsedTorrent, { wrtc: {} }) diff --git a/test/filter.js b/test/filter.js index a9518c3..33fb106 100644 --- a/test/filter.js +++ b/test/filter.js @@ -1,33 +1,29 @@ var Client = require('../') var common = require('./common') -var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var extend = require('xtend') +var fixtures = require('webtorrent-fixtures') var test = require('tape') -var bitlove = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent')) -var parsedBitlove = parseTorrent(bitlove) - -var leaves = fs.readFileSync(path.join(__dirname, 'torrents/leaves.torrent')) -var parsedLeaves = parseTorrent(leaves) - var peerId = new Buffer('01234567890123456789') function testFilterOption (t, serverType) { t.plan(8) + var parsedAlice = extend(fixtures.alice.parsedTorrent) + var parsedLeaves = extend(fixtures.leaves.parsedTorrent) + var opts = { serverType: serverType } // this is test-suite-only option opts.filter = function (infoHash, params, cb) { process.nextTick(function () { - cb(infoHash !== parsedBitlove.infoHash) + cb(infoHash !== parsedAlice.infoHash) }) } common.createServer(t, opts, function (server, announceUrl) { - parsedBitlove.announce = [ announceUrl ] + parsedAlice.announce = [ announceUrl ] parsedLeaves.announce = [ announceUrl ] - var client = new Client(peerId, 6881, parsedBitlove, { wrtc: {} }) + var client = new Client(peerId, 6881, parsedAlice, { wrtc: {} }) client.on('error', function (err) { t.error(err) }) if (serverType === 'ws') common.mockWebsocketTracker(client) @@ -82,25 +78,28 @@ test('ws: filter option blocks tracker from tracking torrent', function (t) { function testFilterCustomError (t, serverType) { t.plan(8) + var parsedLeaves = extend(fixtures.leaves.parsedTorrent) + var parsedAlice = extend(fixtures.alice.parsedTorrent) + var opts = { serverType: serverType } // this is test-suite-only option opts.filter = function (infoHash, params, cb) { process.nextTick(function () { - if (infoHash === parsedBitlove.infoHash) cb(new Error('bitlove blocked')) + if (infoHash === parsedAlice.infoHash) cb(new Error('alice blocked')) else cb(true) }) } common.createServer(t, opts, function (server, announceUrl) { - parsedBitlove.announce = [ announceUrl ] + parsedAlice.announce = [ announceUrl ] parsedLeaves.announce = [ announceUrl ] - var client = new Client(peerId, 6881, parsedBitlove, { wrtc: {} }) + var client = new Client(peerId, 6881, parsedAlice, { wrtc: {} }) client.on('error', function (err) { t.error(err) }) if (serverType === 'ws') common.mockWebsocketTracker(client) client.once('warning', function (err) { - t.ok(/bitlove blocked/.test(err.message), 'got client warning') + t.ok(/alice blocked/.test(err.message), 'got client warning') client.destroy(function () { t.pass('client destroyed') @@ -126,7 +125,7 @@ function testFilterCustomError (t, serverType) { server.removeAllListeners('warning') server.once('warning', function (err) { - t.ok(/bitlove blocked/.test(err.message), 'got server warning') + t.ok(/alice blocked/.test(err.message), 'got server warning') t.equal(Object.keys(server.torrents).length, 0) }) diff --git a/test/scrape.js b/test/scrape.js index 49f06fb..ce7e578 100644 --- a/test/scrape.js +++ b/test/scrape.js @@ -2,27 +2,19 @@ var bencode = require('bencode') var Client = require('../') var commonLib = require('../lib/common') var commonTest = require('./common') -var fs = require('fs') +var extend = require('xtend') +var fixtures = require('webtorrent-fixtures') var get = require('simple-get') -var parseTorrent = require('parse-torrent') -var path = require('path') var test = require('tape') -var infoHash1 = 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa' -var binaryInfoHash1 = commonLib.hexToBinary(infoHash1) -var infoHash2 = 'bbb67059ed6bd08362da625b3ae77f6f4a075bbb' -var binaryInfoHash2 = commonLib.hexToBinary(infoHash2) - -var bitlove = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent')) -var parsedBitlove = parseTorrent(bitlove) -var binaryBitlove = commonLib.hexToBinary(parsedBitlove.infoHash) - var peerId = new Buffer('01234567890123456789') function testSingle (t, serverType) { + var parsedTorrent = extend(fixtures.leaves.parsedTorrent) + commonTest.createServer(t, serverType, function (server, announceUrl) { - parsedBitlove.announce = [ announceUrl ] - var client = new Client(peerId, 6881, parsedBitlove) + parsedTorrent.announce = [ announceUrl ] + var client = new Client(peerId, 6881, parsedTorrent) client.on('error', function (err) { t.error(err) @@ -36,7 +28,7 @@ function testSingle (t, serverType) { client.on('scrape', function (data) { t.equal(data.announce, announceUrl) - t.equal(data.infoHash, parsedBitlove.infoHash) + t.equal(data.infoHash, parsedTorrent.infoHash) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') t.equal(typeof data.downloaded, 'number') @@ -58,10 +50,10 @@ test('udp: single info_hash scrape', function (t) { function clientScrapeStatic (t, serverType) { commonTest.createServer(t, serverType, function (server, announceUrl) { - Client.scrape(announceUrl, infoHash1, function (err, data) { + Client.scrape(announceUrl, fixtures.leaves.parsedTorrent.infoHash, function (err, data) { t.error(err) t.equal(data.announce, announceUrl) - t.equal(data.infoHash, infoHash1) + t.equal(data.infoHash, fixtures.leaves.parsedTorrent.infoHash) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') t.equal(typeof data.downloaded, 'number') @@ -81,6 +73,9 @@ test('udp: scrape using Client.scrape static method', function (t) { }) function clientScrapeMulti (t, serverType) { + var infoHash1 = fixtures.leaves.parsedTorrent.infoHash + var infoHash2 = fixtures.alice.parsedTorrent.infoHash + commonTest.createServer(t, serverType, function (server, announceUrl) { Client.scrape(announceUrl, [ infoHash1, infoHash2 ], function (err, results) { t.error(err) @@ -115,6 +110,9 @@ test('udp: MULTI scrape using Client.scrape static method', function (t) { test('server: multiple info_hash scrape (manual http request)', function (t) { t.plan(13) + var binaryInfoHash1 = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash) + var binaryInfoHash2 = commonLib.hexToBinary(fixtures.alice.parsedTorrent.infoHash) + commonTest.createServer(t, 'http', function (server, announceUrl) { var scrapeUrl = announceUrl.replace('/announce', '/scrape') @@ -148,13 +146,17 @@ test('server: multiple info_hash scrape (manual http request)', function (t) { test('server: all info_hash scrape (manual http request)', function (t) { t.plan(10) + + var parsedTorrent = extend(fixtures.leaves.parsedTorrent) + var binaryInfoHash = commonLib.hexToBinary(parsedTorrent.infoHash) + commonTest.createServer(t, 'http', function (server, announceUrl) { var scrapeUrl = announceUrl.replace('/announce', '/scrape') - parsedBitlove.announce = [ announceUrl ] + parsedTorrent.announce = [ announceUrl ] // announce a torrent to the tracker - var client = new Client(peerId, 6881, parsedBitlove) + var client = new Client(peerId, 6881, parsedTorrent) client.on('error', function (err) { t.error(err) }) client.on('warning', function (err) { t.error(err) }) @@ -170,10 +172,10 @@ test('server: all info_hash scrape (manual http request)', function (t) { t.ok(data.files) t.equal(Object.keys(data.files).length, 1) - t.ok(data.files[binaryBitlove]) - t.equal(typeof data.files[binaryBitlove].complete, 'number') - t.equal(typeof data.files[binaryBitlove].incomplete, 'number') - t.equal(typeof data.files[binaryBitlove].downloaded, 'number') + t.ok(data.files[binaryInfoHash]) + t.equal(typeof data.files[binaryInfoHash].complete, 'number') + t.equal(typeof data.files[binaryInfoHash].incomplete, 'number') + t.equal(typeof data.files[binaryInfoHash].downloaded, 'number') client.destroy(function () { t.pass('client destroyed') }) server.close(function () { t.pass('server closed') }) diff --git a/test/torrents/bitlove-intro.torrent b/test/torrents/bitlove-intro.torrent deleted file mode 100644 index 1c91bf1..0000000 Binary files a/test/torrents/bitlove-intro.torrent and /dev/null differ diff --git a/test/torrents/leaves.torrent b/test/torrents/leaves.torrent deleted file mode 100644 index f5541af..0000000 Binary files a/test/torrents/leaves.torrent and /dev/null differ diff --git a/test/torrents/sintel-5gb.torrent b/test/torrents/sintel-5gb.torrent deleted file mode 100644 index 49c35de..0000000 Binary files a/test/torrents/sintel-5gb.torrent and /dev/null differ