diff --git a/package.json b/package.json index 45237a5..c0f2289 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,9 @@ "devDependencies": { "bittorrent-protocol": "^2.0.0", "brfs": "^1.2.0", - "parse-torrent": "^5.7.1", "standard": "^6.0.4", "tape": "^4.0.0", + "webtorrent-fixtures": "^1.3.0", "zuul": "^3.8.0" }, "keywords": [ diff --git a/test/basic.js b/test/basic.js index 6fab1e6..31c19fd 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,13 +1,9 @@ var bencode = require('bencode') -var fs = require('fs') -var path = require('path') +var fixtures = require('webtorrent-fixtures') var Protocol = require('bittorrent-protocol') var test = require('tape') var ut_metadata = require('../') -// Used in multiple tests -var metadata = fs.readFileSync(path.join(__dirname, 'torrents/leaves-magnet.torrent')) - test('wire.use(ut_metadata())', function (t) { var wire = new Protocol() wire.pipe(wire) @@ -25,11 +21,11 @@ test('wire.use(ut_metadata(metadata))', function (t) { var wire = new Protocol() wire.pipe(wire) - wire.use(ut_metadata(metadata)) + wire.use(ut_metadata(fixtures.leavesMetadata.torrent)) t.ok(wire.ut_metadata) t.ok(wire.ut_metadata.fetch) t.ok(wire.ut_metadata.cancel) - t.equal(wire.ut_metadata.metadata.toString('hex'), bencode.encode(bencode.decode(metadata).info).toString('hex')) + t.equal(wire.ut_metadata.metadata.toString('hex'), bencode.encode(bencode.decode(fixtures.leavesMetadata.torrent).info).toString('hex')) t.end() }) diff --git a/test/fetch.js b/test/fetch.js index 0ab9602..b425791 100644 --- a/test/fetch.js +++ b/test/fetch.js @@ -1,18 +1,9 @@ var bencode = require('bencode') -var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var fixtures = require('webtorrent-fixtures') var Protocol = require('bittorrent-protocol') var test = require('tape') var ut_metadata = require('../') -// Used in multiple tests -var metadata = fs.readFileSync(path.join(__dirname, 'torrents/leaves-magnet.torrent')) -var parsedTorrent = parseTorrent(metadata) - -var largeMetadata = fs.readFileSync(path.join(__dirname, 'torrents/ubuntu-12.04.4-alternate-amd64.iso.torrent')) -var largeParsedTorrent = parseTorrent(largeMetadata) - var id1 = new Buffer('01234567890123456789') var id2 = new Buffer('12345678901234567890') @@ -23,18 +14,18 @@ test('fetch()', function (t) { var wire2 = new Protocol() wire1.pipe(wire2).pipe(wire1) - wire1.use(ut_metadata(metadata)) // wire1 already has metadata + wire1.use(ut_metadata(fixtures.leavesMetadata.torrent)) // wire1 already has metadata wire2.use(ut_metadata()) // wire2 does not wire2.ut_metadata.fetch() wire2.ut_metadata.on('metadata', function (_metadata) { // got metadata! - t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(metadata).info }).toString('hex')) + t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.leavesMetadata.torrent).info }).toString('hex')) }) wire2.on('handshake', function (infoHash, peerId, extensions) { - wire2.handshake(parsedTorrent.infoHash, id2) + wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2) }) wire2.on('extended', function (ext) { @@ -50,7 +41,7 @@ test('fetch()', function (t) { } }) - wire1.handshake(parsedTorrent.infoHash, id1) + wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1) }) test('fetch() from peer without metadata', function (t) { @@ -80,7 +71,7 @@ test('fetch() from peer without metadata', function (t) { }) wire2.on('handshake', function (infoHash, peerId, extensions) { - wire2.handshake(parsedTorrent.infoHash, id2) + wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2) }) wire2.on('extended', function (ext) { @@ -93,7 +84,7 @@ test('fetch() from peer without metadata', function (t) { } }) - wire1.handshake(parsedTorrent.infoHash, id1) + wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1) }) test('fetch when peer gets metadata later (setMetadata)', function (t) { @@ -106,7 +97,7 @@ test('fetch when peer gets metadata later (setMetadata)', function (t) { wire1.use(ut_metadata()) // wire1 starts without metadata process.nextTick(function () { - wire1.ut_metadata.setMetadata(metadata) // wire1 gets metadata later + wire1.ut_metadata.setMetadata(fixtures.leavesMetadata.torrent) // wire1 gets metadata later process.nextTick(function () { // wire2 does not start with metadata, but connects to wire1 after it gets metadata @@ -115,11 +106,11 @@ test('fetch when peer gets metadata later (setMetadata)', function (t) { wire2.ut_metadata.on('metadata', function (_metadata) { // got metadata! - t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(metadata).info }).toString('hex')) + t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.leavesMetadata.torrent).info }).toString('hex')) }) wire2.on('handshake', function (infoHash, peerId, extensions) { - wire2.handshake(parsedTorrent.infoHash, id2) + wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2) }) wire2.on('extended', function (ext) { @@ -135,7 +126,7 @@ test('fetch when peer gets metadata later (setMetadata)', function (t) { } }) - wire1.handshake(parsedTorrent.infoHash, id1) + wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1) }) }) }) @@ -147,25 +138,25 @@ test('fetch() large torrent', function (t) { var wire2 = new Protocol() wire1.pipe(wire2).pipe(wire1) - wire1.use(ut_metadata(largeMetadata)) // wire1 already has metadata + wire1.use(ut_metadata(fixtures.sintel.torrent)) // wire1 already has metadata wire2.use(ut_metadata()) // wire2 does not wire2.ut_metadata.fetch() wire2.ut_metadata.on('metadata', function (_metadata) { // got metadata! - t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(largeMetadata).info }).toString('hex')) + t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.sintel.torrent).info }).toString('hex')) }) wire2.on('handshake', function (infoHash, peerId, extensions) { - wire2.handshake(largeParsedTorrent.infoHash, id2) + wire2.handshake(fixtures.sintel.parsedTorrent.infoHash, id2) }) wire2.on('extended', function (ext) { if (ext === 'handshake') { t.pass('got extended handshake') } else if (ext === 'ut_metadata') { - // note: this should get called twice, once for each block of the ubuntu metadata + // note: this should get called twice, once for each block of the sintel metadata t.pass('got extended ut_metadata message') // this is emitted for consistency's sake, but it's ignored @@ -176,7 +167,7 @@ test('fetch() large torrent', function (t) { } }) - wire1.handshake(largeParsedTorrent.infoHash, id1) + wire1.handshake(fixtures.sintel.parsedTorrent.infoHash, id1) }) test('discard invalid metadata', function (t) { @@ -186,7 +177,7 @@ test('discard invalid metadata', function (t) { var wire2 = new Protocol() wire1.pipe(wire2).pipe(wire1) - var invalidMetadata = metadata.slice(0) + var invalidMetadata = fixtures.leavesMetadata.torrent.slice(0) invalidMetadata[55] = 65 // mess up a byte in the info block wire1.use(ut_metadata(invalidMetadata)) @@ -203,8 +194,8 @@ test('discard invalid metadata', function (t) { }) wire2.on('handshake', function (infoHash, peerId, extensions) { - wire2.handshake(parsedTorrent.infoHash, id2) + wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2) }) - wire1.handshake(parsedTorrent.infoHash, id1) + wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1) }) diff --git a/test/torrents/leaves-magnet.torrent b/test/torrents/leaves-magnet.torrent deleted file mode 100644 index 7299de5..0000000 Binary files a/test/torrents/leaves-magnet.torrent and /dev/null differ diff --git a/test/torrents/ubuntu-12.04.4-alternate-amd64.iso.torrent b/test/torrents/ubuntu-12.04.4-alternate-amd64.iso.torrent deleted file mode 100644 index ba12d7e..0000000 Binary files a/test/torrents/ubuntu-12.04.4-alternate-amd64.iso.torrent and /dev/null differ