PeerTube/server/tests/api/server/tracker.ts

105 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-01-31 16:56:52 +01:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */
2018-08-14 11:00:03 +02:00
2023-05-22 17:04:39 +02:00
import { decode as magnetUriDecode, encode as magnetUriEncode } from 'magnet-uri'
2021-08-27 14:32:44 +02:00
import WebTorrent from 'webtorrent'
import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
2018-08-14 11:00:03 +02:00
describe('Test tracker', function () {
2021-07-16 09:47:51 +02:00
let server: PeerTubeServer
2018-08-14 11:00:03 +02:00
let badMagnet: string
let goodMagnet: string
before(async function () {
this.timeout(60000)
2021-07-16 09:47:51 +02:00
server = await createSingleServer(1)
2018-08-14 11:00:03 +02:00
await setAccessTokensToServers([ server ])
{
2021-07-16 09:04:35 +02:00
const { uuid } = await server.videos.upload()
const video = await server.videos.get({ id: uuid })
2018-08-14 11:00:03 +02:00
goodMagnet = video.files[0].magnetUri
2023-05-22 17:04:39 +02:00
const parsed = magnetUriDecode(goodMagnet)
2018-08-14 11:00:03 +02:00
parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9'
2023-05-22 17:04:39 +02:00
badMagnet = magnetUriEncode(parsed)
2018-08-14 11:00:03 +02:00
}
})
2019-04-10 09:23:18 +02:00
it('Should succeed with the correct infohash', function (done) {
2018-08-14 11:00:03 +02:00
const webtorrent = new WebTorrent()
const torrent = webtorrent.add(goodMagnet)
torrent.on('error', done)
torrent.on('warning', warn => {
const message = typeof warn === 'string' ? warn : warn.message
2020-02-28 16:03:39 +01:00
if (message.includes('Unknown infoHash ')) return done(new Error('Error on infohash'))
2018-08-14 11:00:03 +02:00
})
torrent.on('done', done)
})
2019-04-10 09:23:18 +02:00
it('Should disable the tracker', function (done) {
this.timeout(20000)
2020-03-20 16:17:14 +01:00
const errCb = () => done(new Error('Tracker is enabled'))
2019-04-10 09:23:18 +02:00
killallServers([ server ])
2021-07-16 09:47:51 +02:00
.then(() => server.run({ tracker: { enabled: false } }))
2019-04-10 09:23:18 +02:00
.then(() => {
const webtorrent = new WebTorrent()
const torrent = webtorrent.add(goodMagnet)
torrent.on('error', done)
torrent.on('warning', warn => {
const message = typeof warn === 'string' ? warn : warn.message
2020-03-20 16:17:14 +01:00
if (message.includes('disabled ')) {
torrent.off('done', errCb)
return done()
}
2019-04-10 09:23:18 +02:00
})
2020-03-20 16:17:14 +01:00
torrent.on('done', errCb)
2019-04-10 09:23:18 +02:00
})
})
2020-06-25 16:27:35 +02:00
it('Should return an error when adding an incorrect infohash', function (done) {
this.timeout(20000)
killallServers([ server ])
2021-07-16 09:47:51 +02:00
.then(() => server.run())
2020-06-25 16:27:35 +02:00
.then(() => {
const webtorrent = new WebTorrent()
const torrent = webtorrent.add(badMagnet)
torrent.on('error', done)
torrent.on('warning', warn => {
const message = typeof warn === 'string' ? warn : warn.message
if (message.includes('Unknown infoHash ')) return done()
})
torrent.on('done', () => done(new Error('No error on infohash')))
})
})
it('Should block the IP after the failed infohash', function (done) {
const webtorrent = new WebTorrent()
const torrent = webtorrent.add(goodMagnet)
torrent.on('error', done)
torrent.on('warning', warn => {
const message = typeof warn === 'string' ? warn : warn.message
if (message.includes('Unsupported tracker protocol')) return done()
})
})
2019-04-24 15:10:37 +02:00
after(async function () {
await cleanupTests([ server ])
2018-08-14 11:00:03 +02:00
})
})