bittorrent-tracker/test/client.js

641 lines
15 KiB
JavaScript
Raw Normal View History

import Client from '../index.js'
import common from './common.js'
import http from 'http'
import fixtures from 'webtorrent-fixtures'
import net from 'net'
import test from 'tape'
import undici from 'undici'
2014-03-26 08:17:59 +00:00
2020-10-29 04:57:47 +00:00
const peerId1 = Buffer.from('01234567890123456789')
const peerId2 = Buffer.from('12345678901234567890')
const peerId3 = Buffer.from('23456789012345678901')
const port = 6881
2014-03-26 08:17:59 +00:00
2014-07-11 10:50:35 +00:00
function testClientStart (t, serverType) {
2016-02-29 22:48:23 +00:00
t.plan(4)
2016-03-16 19:22:33 +00:00
2021-06-15 01:54:41 +00:00
common.createServer(t, serverType, (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
wrtc: {}
})
2014-03-26 08:17:59 +00:00
if (serverType === 'ws') common.mockWebsocketTracker(client)
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2014-07-11 04:58:19 +00:00
2021-06-15 01:54:41 +00:00
client.once('update', data => {
t.equal(data.announce, announceUrl)
2014-03-26 08:17:59 +00:00
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
2014-05-24 04:14:19 +00:00
client.stop()
2021-06-15 01:54:41 +00:00
client.once('update', () => {
t.pass('got response to stop')
server.close()
client.destroy()
2014-05-24 04:14:19 +00:00
})
})
2014-03-26 08:17:59 +00:00
2014-05-24 04:14:19 +00:00
client.start()
})
2014-07-11 10:50:35 +00:00
}
2021-06-15 01:54:41 +00:00
test('http: client.start()', t => {
2014-07-11 10:50:35 +00:00
testClientStart(t, 'http')
2014-05-24 04:14:19 +00:00
})
2014-03-26 08:17:59 +00:00
2021-06-15 01:54:41 +00:00
test('udp: client.start()', t => {
2014-07-11 10:50:35 +00:00
testClientStart(t, 'udp')
})
2014-03-26 08:17:59 +00:00
2021-06-15 01:54:41 +00:00
test('ws: client.start()', t => {
2016-02-29 22:48:23 +00:00
testClientStart(t, 'ws')
})
2014-07-11 10:50:35 +00:00
function testClientStop (t, serverType) {
t.plan(4)
2016-03-16 19:22:33 +00:00
2021-06-15 01:54:41 +00:00
common.createServer(t, serverType, (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
wrtc: {}
})
2014-03-26 08:17:59 +00:00
if (serverType === 'ws') common.mockWebsocketTracker(client)
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2014-07-11 04:58:19 +00:00
2014-05-24 04:14:19 +00:00
client.start()
2014-03-26 08:17:59 +00:00
2021-06-15 01:54:41 +00:00
client.once('update', () => {
t.pass('client received response to "start" message')
2014-03-26 08:17:59 +00:00
client.stop()
2014-05-24 04:14:19 +00:00
2021-06-15 01:54:41 +00:00
client.once('update', data => {
2014-05-24 04:14:19 +00:00
// receive one final update after calling stop
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
server.close()
client.destroy()
2014-05-24 04:14:19 +00:00
})
})
2014-05-24 04:14:19 +00:00
})
2014-07-11 10:50:35 +00:00
}
2021-06-15 01:54:41 +00:00
test('http: client.stop()', t => {
2014-07-11 10:50:35 +00:00
testClientStop(t, 'http')
2014-05-24 04:14:19 +00:00
})
2021-06-15 01:54:41 +00:00
test('udp: client.stop()', t => {
2014-07-11 10:50:35 +00:00
testClientStop(t, 'udp')
})
2014-05-24 04:14:19 +00:00
2021-06-15 01:54:41 +00:00
test('ws: client.stop()', t => {
2016-02-29 22:48:23 +00:00
testClientStop(t, 'ws')
})
function testClientStopDestroy (t, serverType) {
t.plan(2)
2021-06-15 01:54:41 +00:00
common.createServer(t, serverType, (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
wrtc: {}
})
if (serverType === 'ws') common.mockWebsocketTracker(client)
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
client.start()
2021-06-15 01:54:41 +00:00
client.once('update', () => {
t.pass('client received response to "start" message')
client.stop()
2021-06-15 01:54:41 +00:00
client.on('update', () => { t.fail('client should not receive update after destroy is called') })
// Call destroy() in the same tick as stop(), but the message should still
// be received by the server, though obviously the client won't receive the
// response.
client.destroy()
2021-06-15 01:54:41 +00:00
server.once('stop', (peer, params) => {
t.pass('server received "stop" message')
2021-06-15 01:54:41 +00:00
setTimeout(() => {
// give the websocket server time to finish in progress (stream) messages
// to peers
server.close()
}, 100)
})
})
})
}
2021-06-15 01:54:41 +00:00
test('http: client.stop(); client.destroy()', t => {
testClientStopDestroy(t, 'http')
})
2021-06-15 01:54:41 +00:00
test('udp: client.stop(); client.destroy()', t => {
testClientStopDestroy(t, 'udp')
})
2021-06-15 01:54:41 +00:00
test('ws: client.stop(); client.destroy()', t => {
testClientStopDestroy(t, 'ws')
})
2014-07-11 10:50:35 +00:00
function testClientUpdate (t, serverType) {
t.plan(4)
2016-03-16 19:22:33 +00:00
2021-06-15 01:54:41 +00:00
common.createServer(t, serverType, (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
wrtc: {}
})
2014-05-24 04:14:19 +00:00
if (serverType === 'ws') common.mockWebsocketTracker(client)
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2014-03-26 08:17:59 +00:00
2017-01-21 02:33:30 +00:00
client.setInterval(500)
2014-07-11 04:58:19 +00:00
2014-05-24 04:14:19 +00:00
client.start()
2021-06-15 01:54:41 +00:00
client.once('update', () => {
2017-01-21 02:33:30 +00:00
client.setInterval(500)
2015-12-05 08:06:23 +00:00
2017-01-21 02:33:30 +00:00
// after interval, we should get another update
2021-06-15 01:54:41 +00:00
client.once('update', data => {
2014-05-24 04:14:19 +00:00
// received an update!
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
client.stop()
2021-06-15 01:54:41 +00:00
client.once('update', () => {
t.pass('got response to stop')
server.close()
client.destroy()
2014-05-24 04:14:19 +00:00
})
})
})
2014-03-26 08:17:59 +00:00
})
2014-07-11 10:50:35 +00:00
}
2021-06-15 01:54:41 +00:00
test('http: client.update()', t => {
2014-07-11 10:50:35 +00:00
testClientUpdate(t, 'http')
2014-03-26 08:17:59 +00:00
})
2021-06-15 01:54:41 +00:00
test('udp: client.update()', t => {
2014-07-11 10:50:35 +00:00
testClientUpdate(t, 'udp')
})
2021-06-15 01:54:41 +00:00
test('ws: client.update()', t => {
2016-02-29 22:48:23 +00:00
testClientUpdate(t, 'ws')
})
2014-07-11 10:50:35 +00:00
function testClientScrape (t, serverType) {
t.plan(4)
2016-03-16 19:22:33 +00:00
2021-06-15 01:54:41 +00:00
common.createServer(t, serverType, (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
wrtc: {}
})
if (serverType === 'ws') common.mockWebsocketTracker(client)
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2014-07-11 04:58:19 +00:00
2021-06-15 01:54:41 +00:00
client.once('scrape', data => {
2014-05-24 04:14:19 +00:00
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
t.equal(typeof data.downloaded, 'number')
2014-05-12 00:32:57 +00:00
server.close()
client.destroy()
2014-05-24 04:14:19 +00:00
})
client.scrape()
})
2014-07-11 10:50:35 +00:00
}
2021-06-15 01:54:41 +00:00
test('http: client.scrape()', t => {
2014-07-11 10:50:35 +00:00
testClientScrape(t, 'http')
})
2021-06-15 01:54:41 +00:00
test('udp: client.scrape()', t => {
2014-07-11 10:50:35 +00:00
testClientScrape(t, 'udp')
})
2021-06-15 01:54:41 +00:00
test('ws: client.scrape()', t => {
testClientScrape(t, 'ws')
})
2016-02-29 22:48:23 +00:00
function testClientAnnounceWithParams (t, serverType) {
t.plan(5)
2016-03-16 19:22:33 +00:00
2021-06-15 01:54:41 +00:00
common.createServer(t, serverType, (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
wrtc: {}
})
2021-06-15 01:54:41 +00:00
server.on('start', (peer, params) => {
t.equal(params.testParam, 'this is a test')
})
if (serverType === 'ws') common.mockWebsocketTracker(client)
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2021-06-15 01:54:41 +00:00
client.once('update', data => {
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
client.stop()
2021-06-15 01:54:41 +00:00
client.once('update', () => {
t.pass('got response to stop')
server.close()
client.destroy()
})
})
client.start({
testParam: 'this is a test'
})
})
}
2021-06-15 01:54:41 +00:00
test('http: client.announce() with params', t => {
testClientAnnounceWithParams(t, 'http')
})
2021-06-15 01:54:41 +00:00
test('ws: client.announce() with params', t => {
testClientAnnounceWithParams(t, 'ws')
})
function testClientGetAnnounceOpts (t, serverType) {
t.plan(5)
2016-03-16 19:22:33 +00:00
2021-06-15 01:54:41 +00:00
common.createServer(t, serverType, (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
2021-06-15 01:54:41 +00:00
getAnnounceOpts () {
return {
testParam: 'this is a test'
}
},
wrtc: {}
})
2021-06-15 01:54:41 +00:00
server.on('start', (peer, params) => {
t.equal(params.testParam, 'this is a test')
})
if (serverType === 'ws') common.mockWebsocketTracker(client)
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2021-06-15 01:54:41 +00:00
client.once('update', data => {
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
client.stop()
2021-06-15 01:54:41 +00:00
client.once('update', () => {
t.pass('got response to stop')
server.close()
client.destroy()
})
})
client.start()
})
}
2021-06-15 01:54:41 +00:00
test('http: client `opts.getAnnounceOpts`', t => {
testClientGetAnnounceOpts(t, 'http')
})
2021-06-15 01:54:41 +00:00
test('ws: client `opts.getAnnounceOpts`', t => {
testClientGetAnnounceOpts(t, 'ws')
})
2014-07-11 10:50:48 +00:00
function testClientAnnounceWithNumWant (t, serverType) {
t.plan(4)
2016-03-16 19:22:33 +00:00
2021-06-15 01:54:41 +00:00
common.createServer(t, serverType, (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client1 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
2019-07-05 21:36:14 +00:00
announce: [announceUrl],
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
wrtc: {}
})
2016-02-29 22:48:23 +00:00
if (serverType === 'ws') common.mockWebsocketTracker(client1)
2021-06-15 01:54:41 +00:00
client1.on('error', err => { t.error(err) })
client1.on('warning', err => { t.error(err) })
2014-07-11 10:50:48 +00:00
client1.start()
2021-06-15 01:54:41 +00:00
client1.once('update', () => {
2020-10-29 04:57:47 +00:00
const client2 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId2,
port: port + 1,
wrtc: {}
})
2016-02-29 22:48:23 +00:00
if (serverType === 'ws') common.mockWebsocketTracker(client2)
2021-06-15 01:54:41 +00:00
client2.on('error', err => { t.error(err) })
client2.on('warning', err => { t.error(err) })
2016-02-29 22:48:23 +00:00
2014-07-11 10:50:48 +00:00
client2.start()
2021-06-15 01:54:41 +00:00
client2.once('update', () => {
2020-10-29 04:57:47 +00:00
const client3 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId3,
port: port + 2,
wrtc: {}
})
2016-02-29 22:48:23 +00:00
if (serverType === 'ws') common.mockWebsocketTracker(client3)
2021-06-15 01:54:41 +00:00
client3.on('error', err => { t.error(err) })
client3.on('warning', err => { t.error(err) })
2016-02-29 22:48:23 +00:00
2015-07-29 08:47:09 +00:00
client3.start({ numwant: 1 })
2021-06-15 01:54:41 +00:00
client3.on('peer', () => {
2014-07-11 10:50:48 +00:00
t.pass('got one peer (this should only fire once)')
2020-10-29 04:57:47 +00:00
let num = 3
function tryCloseServer () {
num -= 1
if (num === 0) server.close()
}
2014-07-11 10:50:48 +00:00
client1.stop()
2021-06-15 01:54:41 +00:00
client1.once('update', () => {
t.pass('got response to stop (client1)')
client1.destroy()
tryCloseServer()
})
2014-07-11 10:50:48 +00:00
client2.stop()
2021-06-15 01:54:41 +00:00
client2.once('update', () => {
t.pass('got response to stop (client2)')
client2.destroy()
tryCloseServer()
})
2014-07-11 10:50:48 +00:00
client3.stop()
2021-06-15 01:54:41 +00:00
client3.once('update', () => {
t.pass('got response to stop (client3)')
client3.destroy()
tryCloseServer()
})
2014-07-11 10:50:48 +00:00
})
})
})
})
}
2021-06-15 01:54:41 +00:00
test('http: client announce with numwant', t => {
2014-07-11 10:50:48 +00:00
testClientAnnounceWithNumWant(t, 'http')
})
2021-06-15 01:54:41 +00:00
test('udp: client announce with numwant', t => {
2014-07-11 10:50:48 +00:00
testClientAnnounceWithNumWant(t, 'udp')
})
2017-01-16 23:43:47 +00:00
2021-06-15 01:54:41 +00:00
test('http: userAgent', t => {
2017-01-16 23:43:47 +00:00
t.plan(2)
2021-06-15 01:54:41 +00:00
common.createServer(t, 'http', (server, announceUrl) => {
2017-01-16 23:43:47 +00:00
// Confirm that user-agent header is set
2021-06-15 01:54:41 +00:00
server.http.on('request', (req, res) => {
t.ok(req.headers['user-agent'].includes('WebTorrent'))
2017-01-16 23:43:47 +00:00
})
2020-10-29 04:57:47 +00:00
const client = new Client({
2017-01-16 23:43:47 +00:00
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
2017-01-16 23:43:47 +00:00
userAgent: 'WebTorrent/0.98.0 (https://webtorrent.io)',
wrtc: {}
})
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2017-01-16 23:43:47 +00:00
2021-06-15 01:54:41 +00:00
client.once('update', data => {
2017-01-16 23:43:47 +00:00
t.equal(data.announce, announceUrl)
server.close()
client.destroy()
})
client.start()
})
})
2019-04-08 18:43:50 +00:00
function testSupportedTracker (t, serverType) {
t.plan(1)
2021-06-15 01:54:41 +00:00
common.createServer(t, serverType, (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client = new Client({
2019-04-08 18:43:50 +00:00
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
2019-04-08 18:43:50 +00:00
wrtc: {}
})
if (serverType === 'ws') common.mockWebsocketTracker(client)
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2019-04-08 18:43:50 +00:00
client.start()
2021-06-15 01:54:41 +00:00
client.once('update', data => {
2019-04-08 18:43:50 +00:00
t.pass('tracker is valid')
server.close()
client.destroy()
})
})
}
2021-06-15 01:54:41 +00:00
test('http: valid tracker port', t => {
2019-04-08 18:43:50 +00:00
testSupportedTracker(t, 'http')
})
2021-06-15 01:54:41 +00:00
test('udp: valid tracker port', t => {
2019-04-08 18:43:50 +00:00
testSupportedTracker(t, 'udp')
})
2021-06-15 01:54:41 +00:00
test('ws: valid tracker port', t => {
2019-04-08 18:43:50 +00:00
testSupportedTracker(t, 'ws')
})
function testUnsupportedTracker (t, announceUrl) {
t.plan(1)
2020-10-29 04:57:47 +00:00
const client = new Client({
2019-04-08 18:43:50 +00:00
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
2020-04-25 09:34:40 +00:00
port,
2019-04-08 18:43:50 +00:00
wrtc: {}
})
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => {
2019-04-08 18:43:50 +00:00
t.ok(err.message.includes('tracker'), 'got warning')
client.destroy()
})
}
2021-06-15 01:54:41 +00:00
test('unsupported tracker protocol', t => {
2019-04-08 18:43:50 +00:00
testUnsupportedTracker(t, 'badprotocol://127.0.0.1:8080/announce')
})
2021-06-15 01:54:41 +00:00
test('http: invalid tracker port', t => {
2019-04-08 18:43:50 +00:00
testUnsupportedTracker(t, 'http://127.0.0.1:69691337/announce')
})
2021-06-15 01:54:41 +00:00
test('http: invalid tracker url', t => {
2021-05-20 19:38:51 +00:00
testUnsupportedTracker(t, 'http:')
})
2021-06-15 01:54:41 +00:00
test('http: invalid tracker url with slash', t => {
2021-05-20 19:38:51 +00:00
testUnsupportedTracker(t, 'http://')
})
2021-06-15 01:54:41 +00:00
test('udp: invalid tracker port', t => {
2019-04-08 18:43:50 +00:00
testUnsupportedTracker(t, 'udp://127.0.0.1:69691337')
})
2021-06-15 01:54:41 +00:00
test('udp: invalid tracker url', t => {
2021-05-20 19:38:51 +00:00
testUnsupportedTracker(t, 'udp:')
})
2021-06-15 01:54:41 +00:00
test('udp: invalid tracker url with slash', t => {
2021-05-20 19:38:51 +00:00
testUnsupportedTracker(t, 'udp://')
})
2021-06-15 01:54:41 +00:00
test('ws: invalid tracker port', t => {
2019-04-08 18:43:50 +00:00
testUnsupportedTracker(t, 'ws://127.0.0.1:69691337')
})
2021-05-20 19:38:51 +00:00
2021-06-15 01:54:41 +00:00
test('ws: invalid tracker url', t => {
2021-05-20 19:38:51 +00:00
testUnsupportedTracker(t, 'ws:')
})
2021-06-15 01:54:41 +00:00
test('ws: invalid tracker url with slash', t => {
2021-05-20 19:38:51 +00:00
testUnsupportedTracker(t, 'ws://')
})
function testClientStartHttpAgent (t, serverType) {
t.plan(5)
common.createServer(t, serverType, function (server, announceUrl) {
let agent
if (global.fetch && serverType !== 'ws') {
const connector = undici.buildConnector({ rejectUnauthorized: false })
agent = new undici.Agent({
connect (opts, cb) {
agentUsed = true
connector(opts, (err, socket) => {
if (err) {
cb(err, null)
} else {
cb(null, socket)
}
})
}
})
} else {
agent = new http.Agent()
agent.createConnection = function (opts, fn) {
agentUsed = true
return net.createConnection(opts, fn)
}
}
let agentUsed = false
const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
port,
wrtc: {},
proxyOpts: {
httpAgent: agent
}
})
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
client.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
t.ok(agentUsed)
client.stop()
client.once('update', function () {
t.pass('got response to stop')
server.close()
client.destroy()
})
})
client.start()
})
}
test('http: client.start(httpAgent)', function (t) {
testClientStartHttpAgent(t, 'http')
})
test('ws: client.start(httpAgent)', function (t) {
testClientStartHttpAgent(t, 'ws')
})