]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */ |
cc43831a | 2 | |
83002a82 | 3 | import { decode as magnetUriDecode, encode as magnetUriEncode } from 'magnet-uri' |
41fb13c3 | 4 | import WebTorrent from 'webtorrent' |
bf54587a | 5 | import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands' |
cc43831a C |
6 | |
7 | describe('Test tracker', function () { | |
254d3579 | 8 | let server: PeerTubeServer |
cc43831a C |
9 | let badMagnet: string |
10 | let goodMagnet: string | |
11 | ||
12 | before(async function () { | |
13 | this.timeout(60000) | |
254d3579 | 14 | server = await createSingleServer(1) |
cc43831a C |
15 | await setAccessTokensToServers([ server ]) |
16 | ||
17 | { | |
89d241a7 C |
18 | const { uuid } = await server.videos.upload() |
19 | const video = await server.videos.get({ id: uuid }) | |
cc43831a C |
20 | goodMagnet = video.files[0].magnetUri |
21 | ||
83002a82 | 22 | const parsed = magnetUriDecode(goodMagnet) |
cc43831a C |
23 | parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9' |
24 | ||
83002a82 | 25 | badMagnet = magnetUriEncode(parsed) |
cc43831a C |
26 | } |
27 | }) | |
28 | ||
31b6ddf8 | 29 | it('Should succeed with the correct infohash', function (done) { |
cc43831a C |
30 | const webtorrent = new WebTorrent() |
31 | ||
32 | const torrent = webtorrent.add(goodMagnet) | |
33 | ||
34 | torrent.on('error', done) | |
35 | torrent.on('warning', warn => { | |
36 | const message = typeof warn === 'string' ? warn : warn.message | |
bdd428a6 | 37 | if (message.includes('Unknown infoHash ')) return done(new Error('Error on infohash')) |
cc43831a C |
38 | }) |
39 | ||
40 | torrent.on('done', done) | |
41 | }) | |
42 | ||
31b6ddf8 C |
43 | it('Should disable the tracker', function (done) { |
44 | this.timeout(20000) | |
45 | ||
cf5d7d9d C |
46 | const errCb = () => done(new Error('Tracker is enabled')) |
47 | ||
31b6ddf8 | 48 | killallServers([ server ]) |
254d3579 | 49 | .then(() => server.run({ tracker: { enabled: false } })) |
31b6ddf8 C |
50 | .then(() => { |
51 | const webtorrent = new WebTorrent() | |
52 | ||
53 | const torrent = webtorrent.add(goodMagnet) | |
54 | ||
55 | torrent.on('error', done) | |
56 | torrent.on('warning', warn => { | |
57 | const message = typeof warn === 'string' ? warn : warn.message | |
cf5d7d9d C |
58 | if (message.includes('disabled ')) { |
59 | torrent.off('done', errCb) | |
60 | ||
61 | return done() | |
62 | } | |
31b6ddf8 C |
63 | }) |
64 | ||
cf5d7d9d | 65 | torrent.on('done', errCb) |
31b6ddf8 C |
66 | }) |
67 | }) | |
68 | ||
db48de85 C |
69 | it('Should return an error when adding an incorrect infohash', function (done) { |
70 | this.timeout(20000) | |
71 | ||
72 | killallServers([ server ]) | |
254d3579 | 73 | .then(() => server.run()) |
db48de85 C |
74 | .then(() => { |
75 | const webtorrent = new WebTorrent() | |
76 | ||
77 | const torrent = webtorrent.add(badMagnet) | |
78 | ||
79 | torrent.on('error', done) | |
80 | torrent.on('warning', warn => { | |
81 | const message = typeof warn === 'string' ? warn : warn.message | |
82 | if (message.includes('Unknown infoHash ')) return done() | |
83 | }) | |
84 | ||
85 | torrent.on('done', () => done(new Error('No error on infohash'))) | |
86 | }) | |
87 | }) | |
88 | ||
89 | it('Should block the IP after the failed infohash', function (done) { | |
90 | const webtorrent = new WebTorrent() | |
91 | ||
92 | const torrent = webtorrent.add(goodMagnet) | |
93 | ||
94 | torrent.on('error', done) | |
95 | torrent.on('warning', warn => { | |
96 | const message = typeof warn === 'string' ? warn : warn.message | |
97 | if (message.includes('Unsupported tracker protocol')) return done() | |
98 | }) | |
99 | }) | |
100 | ||
7c3b7976 C |
101 | after(async function () { |
102 | await cleanupTests([ server ]) | |
cc43831a C |
103 | }) |
104 | }) |