bittorrent-tracker/test/stats.js

195 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-10-29 04:57:47 +00:00
const Client = require('../')
const commonTest = require('./common')
const fixtures = require('webtorrent-fixtures')
const get = require('simple-get')
const test = require('tape')
2016-06-07 08:49:27 +00:00
2020-10-29 04:57:47 +00:00
const peerId = Buffer.from('-WW0091-4ea5886ce160')
const unknownPeerId = Buffer.from('01234567890123456789')
2016-06-07 08:49:27 +00:00
function parseHtml (html) {
2020-10-29 04:57:47 +00:00
const extractValue = /[^v^h](\d+)/
2021-06-15 01:54:41 +00:00
const array = html.replace('torrents', '\n').split('\n').filter(line => line && line.trim().length > 0).map(line => {
2020-10-29 04:57:47 +00:00
const a = extractValue.exec(line)
2016-06-07 12:34:38 +00:00
if (a) {
return parseInt(a[1])
}
2020-10-28 20:02:35 +00:00
return null
2016-06-07 08:49:27 +00:00
})
2020-10-29 04:57:47 +00:00
let i = 0
2016-06-07 08:49:27 +00:00
return {
torrents: array[i++],
activeTorrents: array[i++],
peersAll: array[i++],
peersSeederOnly: array[i++],
peersLeecherOnly: array[i++],
peersSeederAndLeecher: array[i++],
peersIPv4: array[i++],
peersIPv6: array[i]
}
}
2021-06-15 01:54:41 +00:00
test('server: get empty stats', t => {
2016-06-07 08:49:27 +00:00
t.plan(11)
2021-06-15 01:54:41 +00:00
commonTest.createServer(t, 'http', (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const url = announceUrl.replace('/announce', '/stats')
2016-06-07 08:49:27 +00:00
2021-06-15 01:54:41 +00:00
get.concat(url, (err, res, data) => {
2016-06-07 08:49:27 +00:00
t.error(err)
2020-10-29 04:57:47 +00:00
const stats = parseHtml(data.toString())
2016-06-07 08:49:27 +00:00
t.equal(res.statusCode, 200)
t.equal(stats.torrents, 0)
t.equal(stats.activeTorrents, 0)
t.equal(stats.peersAll, 0)
t.equal(stats.peersSeederOnly, 0)
t.equal(stats.peersLeecherOnly, 0)
t.equal(stats.peersSeederAndLeecher, 0)
t.equal(stats.peersIPv4, 0)
t.equal(stats.peersIPv6, 0)
2021-06-15 01:54:41 +00:00
server.close(() => { t.pass('server closed') })
2016-06-07 08:49:27 +00:00
})
})
})
2021-06-15 01:54:41 +00:00
test('server: get empty stats with json header', t => {
2016-06-07 08:49:27 +00:00
t.plan(11)
2021-06-15 01:54:41 +00:00
commonTest.createServer(t, 'http', (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const opts = {
2016-06-07 08:49:27 +00:00
url: announceUrl.replace('/announce', '/stats'),
headers: {
2017-04-04 18:46:43 +00:00
accept: 'application/json'
2016-06-07 08:49:27 +00:00
},
json: true
}
2021-06-15 01:54:41 +00:00
get.concat(opts, (err, res, stats) => {
2016-06-07 08:49:27 +00:00
t.error(err)
t.equal(res.statusCode, 200)
t.equal(stats.torrents, 0)
t.equal(stats.activeTorrents, 0)
t.equal(stats.peersAll, 0)
t.equal(stats.peersSeederOnly, 0)
t.equal(stats.peersLeecherOnly, 0)
t.equal(stats.peersSeederAndLeecher, 0)
t.equal(stats.peersIPv4, 0)
t.equal(stats.peersIPv6, 0)
2021-06-15 01:54:41 +00:00
server.close(() => { t.pass('server closed') })
2016-06-07 08:49:27 +00:00
})
})
})
2021-06-15 01:54:41 +00:00
test('server: get empty stats on stats.json', t => {
2016-06-07 08:49:27 +00:00
t.plan(11)
2021-06-15 01:54:41 +00:00
commonTest.createServer(t, 'http', (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const opts = {
2016-06-07 08:49:27 +00:00
url: announceUrl.replace('/announce', '/stats.json'),
json: true
}
2021-06-15 01:54:41 +00:00
get.concat(opts, (err, res, stats) => {
2016-06-07 08:49:27 +00:00
t.error(err)
t.equal(res.statusCode, 200)
t.equal(stats.torrents, 0)
t.equal(stats.activeTorrents, 0)
t.equal(stats.peersAll, 0)
t.equal(stats.peersSeederOnly, 0)
t.equal(stats.peersLeecherOnly, 0)
t.equal(stats.peersSeederAndLeecher, 0)
t.equal(stats.peersIPv4, 0)
t.equal(stats.peersIPv6, 0)
2021-06-15 01:54:41 +00:00
server.close(() => { t.pass('server closed') })
2016-06-07 08:49:27 +00:00
})
})
})
2021-06-15 01:54:41 +00:00
test('server: get leecher stats.json', t => {
2016-06-07 12:34:38 +00:00
t.plan(11)
2016-06-07 08:49:27 +00:00
2021-06-15 01:54:41 +00:00
commonTest.createServer(t, 'http', (server, announceUrl) => {
2016-06-07 08:49:27 +00:00
// announce a torrent to the tracker
2020-10-29 04:57:47 +00:00
const client = new Client({
2016-06-07 08:49:27 +00:00
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
2020-04-25 09:34:40 +00:00
peerId,
2016-06-07 08:49:27 +00:00
port: 6881
})
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2016-06-07 08:49:27 +00:00
client.start()
2021-06-15 01:54:41 +00:00
server.once('start', () => {
2020-10-29 04:57:47 +00:00
const opts = {
2016-06-07 08:49:27 +00:00
url: announceUrl.replace('/announce', '/stats.json'),
json: true
}
2021-06-15 01:54:41 +00:00
get.concat(opts, (err, res, stats) => {
2016-06-07 08:49:27 +00:00
t.error(err)
t.equal(res.statusCode, 200)
t.equal(stats.torrents, 1)
t.equal(stats.activeTorrents, 1)
t.equal(stats.peersAll, 1)
t.equal(stats.peersSeederOnly, 0)
t.equal(stats.peersLeecherOnly, 1)
t.equal(stats.peersSeederAndLeecher, 0)
2019-07-28 03:35:35 +00:00
t.equal(stats.clients.WebTorrent['0.91'], 1)
2016-06-07 12:34:38 +00:00
2021-06-15 01:54:41 +00:00
client.destroy(() => { t.pass('client destroyed') })
server.close(() => { t.pass('server closed') })
2016-06-07 12:34:38 +00:00
})
})
})
})
2021-06-15 01:54:41 +00:00
test('server: get leecher stats.json (unknown peerId)', t => {
2016-06-07 12:34:38 +00:00
t.plan(11)
2021-06-15 01:54:41 +00:00
commonTest.createServer(t, 'http', (server, announceUrl) => {
2016-06-07 12:34:38 +00:00
// announce a torrent to the tracker
2020-10-29 04:57:47 +00:00
const client = new Client({
2016-06-07 12:34:38 +00:00
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: unknownPeerId,
port: 6881
})
2021-06-15 01:54:41 +00:00
client.on('error', err => { t.error(err) })
client.on('warning', err => { t.error(err) })
2016-06-07 12:34:38 +00:00
client.start()
2021-06-15 01:54:41 +00:00
server.once('start', () => {
2020-10-29 04:57:47 +00:00
const opts = {
2016-06-07 12:34:38 +00:00
url: announceUrl.replace('/announce', '/stats.json'),
json: true
}
2021-06-15 01:54:41 +00:00
get.concat(opts, (err, res, stats) => {
2016-06-07 12:34:38 +00:00
t.error(err)
t.equal(res.statusCode, 200)
t.equal(stats.torrents, 1)
t.equal(stats.activeTorrents, 1)
t.equal(stats.peersAll, 1)
t.equal(stats.peersSeederOnly, 0)
t.equal(stats.peersLeecherOnly, 1)
t.equal(stats.peersSeederAndLeecher, 0)
2019-07-28 03:35:35 +00:00
t.equal(stats.clients.unknown['01234567'], 1)
2016-06-07 08:49:27 +00:00
2021-06-15 01:54:41 +00:00
client.destroy(() => { t.pass('client destroyed') })
server.close(() => { t.pass('server closed') })
2016-06-07 08:49:27 +00:00
})
})
})
})