ut_metadata/test/fetch.js

202 lines
6.1 KiB
JavaScript
Raw Normal View History

2016-02-05 22:32:52 +00:00
var bencode = require('bencode')
2016-03-16 19:33:57 +00:00
var fixtures = require('webtorrent-fixtures')
2014-03-23 03:34:50 +00:00
var Protocol = require('bittorrent-protocol')
var test = require('tape')
2016-04-24 09:32:47 +01:00
var utMetadata = require('../')
2014-03-23 03:34:50 +00:00
var id1 = new Buffer('01234567890123456789')
var id2 = new Buffer('12345678901234567890')
test('fetch()', function (t) {
t.plan(3)
var wire1 = new Protocol()
var wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1)
2016-04-24 09:32:47 +01:00
wire1.use(utMetadata(fixtures.leavesMetadata.torrent)) // wire1 already has metadata
wire2.use(utMetadata()) // wire2 does not
2014-03-23 03:34:50 +00:00
2014-03-26 09:03:13 +00:00
wire2.ut_metadata.fetch()
2014-03-23 03:34:50 +00:00
2014-03-26 09:03:13 +00:00
wire2.ut_metadata.on('metadata', function (_metadata) {
2014-03-23 03:34:50 +00:00
// got metadata!
2016-03-16 19:33:57 +00:00
t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.leavesMetadata.torrent).info }).toString('hex'))
2014-03-23 03:34:50 +00:00
})
wire2.on('handshake', function (infoHash, peerId, extensions) {
2016-03-16 19:33:57 +00:00
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
2014-03-23 03:34:50 +00:00
})
wire2.on('extended', function (ext) {
if (ext === 'handshake') {
t.pass('got extended handshake')
} else if (ext === 'ut_metadata') {
t.pass('got extended ut_metadata message')
// this is emitted for consistency's sake, but it's ignored
// by the user since the ut_metadata package handles the
// complexities internally
} else {
t.fail('unexpected handshake type')
}
})
2016-03-16 19:33:57 +00:00
wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1)
2014-03-23 03:34:50 +00:00
})
test('fetch() from peer without metadata', function (t) {
2014-03-23 04:10:23 +00:00
t.plan(2)
2014-03-23 03:34:50 +00:00
var wire1 = new Protocol()
var wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1)
2016-04-24 09:32:47 +01:00
wire1.use(utMetadata()) // neither wire has metadata
wire2.use(utMetadata())
2014-03-23 03:34:50 +00:00
2014-03-26 09:03:13 +00:00
wire2.ut_metadata.fetch()
2014-03-23 03:34:50 +00:00
2014-03-26 09:03:13 +00:00
wire2.ut_metadata.on('metadata', function () {
2014-03-23 03:34:50 +00:00
t.fail('No "metadata" event should fire')
})
2014-03-26 09:03:13 +00:00
wire1.ut_metadata.onMessage = function () {
2014-03-23 03:34:50 +00:00
t.fail('No messages should be sent to wire1')
// No messages should be sent because wire1 never sent metadata_size in the
// extended handshake, so he doesn't have metadata
}
2015-12-03 02:24:10 +00:00
wire2.ut_metadata.on('warning', function () {
2014-03-23 04:10:23 +00:00
t.pass('got warning about peer missing metadata')
})
2014-03-23 03:34:50 +00:00
wire2.on('handshake', function (infoHash, peerId, extensions) {
2016-03-16 19:33:57 +00:00
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
2014-03-23 03:34:50 +00:00
})
wire2.on('extended', function (ext) {
if (ext === 'handshake') {
t.pass('got extended handshake')
} else if (ext === 'ut_metadata') {
t.fail('should not get extended ut_metadata message')
} else {
t.fail('unexpected handshake type')
}
})
2016-03-16 19:33:57 +00:00
wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1)
2014-03-23 05:15:57 +00:00
})
2014-06-04 05:27:33 +01:00
test('fetch when peer gets metadata later (setMetadata)', function (t) {
t.plan(3)
var wire1 = new Protocol()
var wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1)
2016-04-24 09:32:47 +01:00
wire1.use(utMetadata()) // wire1 starts without metadata
2014-06-04 05:27:33 +01:00
process.nextTick(function () {
2016-03-16 19:33:57 +00:00
wire1.ut_metadata.setMetadata(fixtures.leavesMetadata.torrent) // wire1 gets metadata later
2014-06-04 05:27:33 +01:00
process.nextTick(function () {
// wire2 does not start with metadata, but connects to wire1 after it gets metadata
2016-04-24 09:32:47 +01:00
wire2.use(utMetadata())
2014-06-04 05:27:33 +01:00
wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function (_metadata) {
// got metadata!
2016-03-16 19:33:57 +00:00
t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.leavesMetadata.torrent).info }).toString('hex'))
2014-06-04 05:27:33 +01:00
})
wire2.on('handshake', function (infoHash, peerId, extensions) {
2016-03-16 19:33:57 +00:00
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
2014-06-04 05:27:33 +01:00
})
wire2.on('extended', function (ext) {
if (ext === 'handshake') {
t.pass('got extended handshake')
} else if (ext === 'ut_metadata') {
t.pass('got extended ut_metadata message')
// this is emitted for consistency's sake, but it's ignored
// by the user since the ut_metadata package handles the
// complexities internally
} else {
t.fail('unexpected handshake type')
}
})
2016-03-16 19:33:57 +00:00
wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1)
2014-06-04 05:27:33 +01:00
})
})
})
test('fetch() large torrent', function (t) {
t.plan(4)
var wire1 = new Protocol()
var wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1)
2016-04-24 09:32:47 +01:00
wire1.use(utMetadata(fixtures.sintel.torrent)) // wire1 already has metadata
wire2.use(utMetadata()) // wire2 does not
wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function (_metadata) {
// got metadata!
2016-03-16 19:33:57 +00:00
t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.sintel.torrent).info }).toString('hex'))
})
wire2.on('handshake', function (infoHash, peerId, extensions) {
2016-03-16 19:33:57 +00:00
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') {
2016-03-16 19:33:57 +00:00
// note: this should get called twice, once for each block of the sintel metadata
t.pass('got extended ut_metadata message')
2014-05-11 22:32:49 +01:00
// this is emitted for consistency's sake, but it's ignored
// by the user since the ut_metadata package handles the
// complexities internally
} else {
t.fail('unexpected handshake type')
}
})
2016-03-16 19:33:57 +00:00
wire1.handshake(fixtures.sintel.parsedTorrent.infoHash, id1)
})
2014-03-23 05:15:57 +00:00
test('discard invalid metadata', function (t) {
t.plan(1)
var wire1 = new Protocol()
var wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1)
2016-03-16 19:33:57 +00:00
var invalidMetadata = fixtures.leavesMetadata.torrent.slice(0)
2014-05-11 22:32:49 +01:00
invalidMetadata[55] = 65 // mess up a byte in the info block
2014-03-23 05:15:57 +00:00
2016-04-24 09:32:47 +01:00
wire1.use(utMetadata(invalidMetadata))
wire2.use(utMetadata())
2014-03-23 05:15:57 +00:00
2014-03-26 09:03:13 +00:00
wire2.ut_metadata.fetch()
2014-03-23 05:15:57 +00:00
2014-03-26 09:03:13 +00:00
wire2.ut_metadata.on('metadata', function () {
2014-03-23 05:15:57 +00:00
t.fail('No "metadata" event should fire')
})
2015-12-03 02:24:10 +00:00
wire2.ut_metadata.on('warning', function () {
2014-03-23 05:15:57 +00:00
t.pass('got warning because peer sent reject too much')
})
wire2.on('handshake', function (infoHash, peerId, extensions) {
2016-03-16 19:33:57 +00:00
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
2014-03-23 05:15:57 +00:00
})
2016-03-16 19:33:57 +00:00
wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1)
2014-03-23 05:15:57 +00:00
})