]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await,@typescript-eslint/no-floating-promises */ |
cc43831a | 2 | |
41fb13c3 C |
3 | import magnetUtil from 'magnet-uri' |
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 | ||
22 | const parsed = magnetUtil.decode(goodMagnet) | |
23 | parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9' | |
24 | ||
25 | badMagnet = magnetUtil.encode(parsed) | |
26 | } | |
27 | }) | |
28 | ||
31b6ddf8 | 29 | it('Should succeed with the correct infohash', function (done) { |
cc43831a C |
30 | this.timeout(10000) |
31 | const webtorrent = new WebTorrent() | |
32 | ||
33 | const torrent = webtorrent.add(goodMagnet) | |
34 | ||
35 | torrent.on('error', done) | |
36 | torrent.on('warning', warn => { | |
37 | const message = typeof warn === 'string' ? warn : warn.message | |
bdd428a6 | 38 | if (message.includes('Unknown infoHash ')) return done(new Error('Error on infohash')) |
cc43831a C |
39 | }) |
40 | ||
41 | torrent.on('done', done) | |
42 | }) | |
43 | ||
31b6ddf8 C |
44 | it('Should disable the tracker', function (done) { |
45 | this.timeout(20000) | |
46 | ||
cf5d7d9d C |
47 | const errCb = () => done(new Error('Tracker is enabled')) |
48 | ||
31b6ddf8 | 49 | killallServers([ server ]) |
254d3579 | 50 | .then(() => server.run({ tracker: { enabled: false } })) |
31b6ddf8 C |
51 | .then(() => { |
52 | const webtorrent = new WebTorrent() | |
53 | ||
54 | const torrent = webtorrent.add(goodMagnet) | |
55 | ||
56 | torrent.on('error', done) | |
57 | torrent.on('warning', warn => { | |
58 | const message = typeof warn === 'string' ? warn : warn.message | |
cf5d7d9d C |
59 | if (message.includes('disabled ')) { |
60 | torrent.off('done', errCb) | |
61 | ||
62 | return done() | |
63 | } | |
31b6ddf8 C |
64 | }) |
65 | ||
cf5d7d9d | 66 | torrent.on('done', errCb) |
31b6ddf8 C |
67 | }) |
68 | }) | |
69 | ||
db48de85 C |
70 | it('Should return an error when adding an incorrect infohash', function (done) { |
71 | this.timeout(20000) | |
72 | ||
73 | killallServers([ server ]) | |
254d3579 | 74 | .then(() => server.run()) |
db48de85 C |
75 | .then(() => { |
76 | const webtorrent = new WebTorrent() | |
77 | ||
78 | const torrent = webtorrent.add(badMagnet) | |
79 | ||
80 | torrent.on('error', done) | |
81 | torrent.on('warning', warn => { | |
82 | const message = typeof warn === 'string' ? warn : warn.message | |
83 | if (message.includes('Unknown infoHash ')) return done() | |
84 | }) | |
85 | ||
86 | torrent.on('done', () => done(new Error('No error on infohash'))) | |
87 | }) | |
88 | }) | |
89 | ||
90 | it('Should block the IP after the failed infohash', function (done) { | |
91 | const webtorrent = new WebTorrent() | |
92 | ||
93 | const torrent = webtorrent.add(goodMagnet) | |
94 | ||
95 | torrent.on('error', done) | |
96 | torrent.on('warning', warn => { | |
97 | const message = typeof warn === 'string' ? warn : warn.message | |
98 | if (message.includes('Unsupported tracker protocol')) return done() | |
99 | }) | |
100 | }) | |
101 | ||
7c3b7976 C |
102 | after(async function () { |
103 | await cleanupTests([ server ]) | |
cc43831a C |
104 | }) |
105 | }) |