ut_metadata/test/fetch.js

252 lines
6.6 KiB
JavaScript
Raw Permalink Normal View History

2018-08-28 18:34:08 +01:00
const { leavesMetadata, sintel } = require('webtorrent-fixtures')
const bencode = require('bencode')
const Protocol = require('bittorrent-protocol')
const test = require('tape')
const utMetadata = require('../')
2014-03-23 03:34:50 +00:00
2018-08-28 18:34:08 +01:00
const id1 = Buffer.from('01234567890123456789')
const id2 = Buffer.from('12345678901234567890')
2014-03-23 03:34:50 +00:00
2018-08-28 18:34:08 +01:00
test('fetch()', t => {
2014-03-23 03:34:50 +00:00
t.plan(3)
2018-08-28 18:34:08 +01:00
const wire1 = new Protocol()
const wire2 = new Protocol()
2014-03-23 03:34:50 +00:00
wire1.pipe(wire2).pipe(wire1)
2018-08-28 18:34:08 +01:00
wire1.use(utMetadata(leavesMetadata.torrent)) // wire1 already has metadata
2016-04-24 09:32:47 +01:00
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
2018-08-28 18:34:08 +01:00
wire2.ut_metadata.on('metadata', _metadata => {
2014-03-23 03:34:50 +00:00
// got metadata!
2018-08-28 18:34:08 +01:00
t.equal(
_metadata.toString('hex'),
bencode.encode({
info: bencode.decode(leavesMetadata.torrent).info
}).toString('hex')
)
2014-03-23 03:34:50 +00:00
})
2018-08-28 18:34:08 +01:00
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
2014-03-23 03:34:50 +00:00
})
2018-08-28 18:34:08 +01:00
wire2.on('extended', ext => {
2014-03-23 03:34:50 +00:00
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')
}
})
2018-08-28 18:34:08 +01:00
wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
2014-03-23 03:34:50 +00:00
})
2018-08-28 18:34:08 +01:00
test('fetch() from peer without metadata', t => {
2014-03-23 04:10:23 +00:00
t.plan(2)
2014-03-23 03:34:50 +00:00
2018-08-28 18:34:08 +01:00
const wire1 = new Protocol()
const wire2 = new Protocol()
2014-03-23 03:34:50 +00:00
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
2018-08-28 18:34:08 +01:00
wire2.ut_metadata.on('metadata', () => {
2014-03-23 03:34:50 +00:00
t.fail('No "metadata" event should fire')
})
2018-08-28 18:34:08 +01:00
wire1.ut_metadata.onMessage = () => {
2014-03-23 03:34:50 +00:00
t.fail('No messages should be sent to wire1')
2018-08-28 18:34:08 +01:00
// No messages should be sent because wire1 never sent metadata_size
// in the extended handshake, so he doesn't have metadata
2014-03-23 03:34:50 +00:00
}
2018-08-28 18:34:08 +01:00
wire2.ut_metadata.on('warning', () => {
2014-03-23 04:10:23 +00:00
t.pass('got warning about peer missing metadata')
})
2018-08-28 18:34:08 +01:00
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
2014-03-23 03:34:50 +00:00
})
2018-08-28 18:34:08 +01:00
wire2.on('extended', ext => {
2014-03-23 03:34:50 +00:00
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')
}
})
2018-08-28 18:34:08 +01:00
wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
2014-03-23 05:15:57 +00:00
})
2018-08-28 18:34:08 +01:00
test('fetch when peer gets metadata later (setMetadata)', t => {
2014-06-04 05:27:33 +01:00
t.plan(3)
2018-08-28 18:34:08 +01:00
const wire1 = new Protocol()
const wire2 = new Protocol()
2014-06-04 05:27:33 +01:00
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
2018-08-28 18:34:08 +01:00
process.nextTick(() => {
// wire1 gets metadata later
wire1.ut_metadata.setMetadata(leavesMetadata.torrent)
2014-06-04 05:27:33 +01:00
2018-08-28 18:34:08 +01:00
process.nextTick(() => {
// 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()
2018-08-28 18:34:08 +01:00
wire2.ut_metadata.on('metadata', _metadata => {
2014-06-04 05:27:33 +01:00
// got metadata!
2018-08-28 18:34:08 +01:00
t.equal(
_metadata.toString('hex'),
bencode.encode({
info: bencode.decode(leavesMetadata.torrent).info
}).toString('hex')
)
2014-06-04 05:27:33 +01:00
})
2018-08-28 18:34:08 +01:00
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
2014-06-04 05:27:33 +01:00
})
2018-08-28 18:34:08 +01:00
wire2.on('extended', ext => {
2014-06-04 05:27:33 +01:00
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')
}
})
2018-08-28 18:34:08 +01:00
wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
2014-06-04 05:27:33 +01:00
})
})
})
2018-08-28 18:34:08 +01:00
test('fetch() large torrent', t => {
t.plan(4)
2018-08-28 18:34:08 +01:00
const wire1 = new Protocol()
const wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1)
2018-08-28 18:34:08 +01:00
wire1.use(utMetadata(sintel.torrent)) // wire1 already has metadata
2016-04-24 09:32:47 +01:00
wire2.use(utMetadata()) // wire2 does not
wire2.ut_metadata.fetch()
2018-08-28 18:34:08 +01:00
wire2.ut_metadata.on('metadata', _metadata => {
// got metadata!
2018-08-28 18:34:08 +01:00
t.equal(
_metadata.toString('hex'),
bencode.encode({
info: bencode.decode(sintel.torrent).info
}).toString('hex')
)
})
2018-08-28 18:34:08 +01:00
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(sintel.parsedTorrent.infoHash, id2)
})
2018-08-28 18:34:08 +01:00
wire2.on('extended', ext => {
if (ext === 'handshake') {
t.pass('got extended handshake')
} else if (ext === 'ut_metadata') {
2018-08-28 18:34:08 +01: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')
}
})
2018-08-28 18:34:08 +01:00
wire1.handshake(sintel.parsedTorrent.infoHash, id1)
})
2018-08-28 18:34:08 +01:00
test('discard invalid metadata', t => {
2014-03-23 05:15:57 +00:00
t.plan(1)
2018-08-28 18:34:08 +01:00
const wire1 = new Protocol()
const wire2 = new Protocol()
2014-03-23 05:15:57 +00:00
wire1.pipe(wire2).pipe(wire1)
2018-08-28 18:34:08 +01:00
const invalidMetadata = 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
2018-08-28 18:34:08 +01:00
wire2.ut_metadata.on('metadata', () => {
2014-03-23 05:15:57 +00:00
t.fail('No "metadata" event should fire')
})
2018-08-28 18:34:08 +01:00
wire2.ut_metadata.on('warning', () => {
2014-03-23 05:15:57 +00:00
t.pass('got warning because peer sent reject too much')
})
2018-08-28 18:34:08 +01:00
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
2014-03-23 05:15:57 +00:00
})
2018-08-28 18:34:08 +01:00
wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
2014-03-23 05:15:57 +00:00
})
2019-05-28 15:02:03 +01:00
test('stop receiving data after cancel', t => {
t.plan(1)
const wire1 = new Protocol()
const wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1)
wire1.use(utMetadata(sintel.torrent))
wire2.use(utMetadata())
wire2.ut_metadata.once('metadata', () => {
t.fail('No "metadata" event should fire')
})
wire2.once('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(sintel.parsedTorrent.infoHash, id2)
wire2.ut_metadata.fetch()
})
wire2.on('extended', ext => {
if (ext === 'ut_metadata') {
wire2.ut_metadata.cancel()
}
})
wire1.handshake(sintel.parsedTorrent.infoHash, id1)
process.nextTick(() => t.pass('no metadata received'))
})