/ test / filter.js
filter.js
  1  import Client from '../index.js'
  2  import common from './common.js'
  3  import fixtures from 'webtorrent-fixtures'
  4  import test from 'tape'
  5  
  6  const peerId = Buffer.from('01234567890123456789')
  7  
  8  function testFilterOption (t, serverType) {
  9    t.plan(8)
 10  
 11    const opts = { serverType } // this is test-suite-only option
 12    opts.filter = (infoHash, params, cb) => {
 13      process.nextTick(() => {
 14        if (infoHash === fixtures.alice.parsedTorrent.infoHash) {
 15          cb(new Error('disallowed info_hash (Alice)'))
 16        } else {
 17          cb(null)
 18        }
 19      })
 20    }
 21  
 22    common.createServer(t, opts, (server, announceUrl) => {
 23      const client1 = new Client({
 24        infoHash: fixtures.alice.parsedTorrent.infoHash,
 25        announce: announceUrl,
 26        peerId,
 27        port: 6881,
 28        wrtc: {}
 29      })
 30  
 31      client1.on('error', err => { t.error(err) })
 32      if (serverType === 'ws') common.mockWebsocketTracker(client1)
 33  
 34      client1.once('warning', err => {
 35        t.ok(err.message.includes('disallowed info_hash (Alice)'), 'got client warning')
 36  
 37        client1.destroy(() => {
 38          t.pass('client1 destroyed')
 39  
 40          const client2 = new Client({
 41            infoHash: fixtures.leaves.parsedTorrent.infoHash,
 42            announce: announceUrl,
 43            peerId,
 44            port: 6881,
 45            wrtc: {}
 46          })
 47          if (serverType === 'ws') common.mockWebsocketTracker(client2)
 48  
 49          client2.on('error', err => { t.error(err) })
 50          client2.on('warning', err => { t.error(err) })
 51  
 52          client2.on('update', () => {
 53            t.pass('got announce')
 54            client2.destroy(() => { t.pass('client2 destroyed') })
 55            server.close(() => { t.pass('server closed') })
 56          })
 57  
 58          server.on('start', () => {
 59            t.equal(Object.keys(server.torrents).length, 1)
 60          })
 61  
 62          client2.start()
 63        })
 64      })
 65  
 66      server.removeAllListeners('warning')
 67      server.once('warning', err => {
 68        t.ok(err.message.includes('disallowed info_hash (Alice)'), 'got server warning')
 69        t.equal(Object.keys(server.torrents).length, 0)
 70      })
 71  
 72      client1.start()
 73    })
 74  }
 75  
 76  test('http: filter option blocks tracker from tracking torrent', t => {
 77    testFilterOption(t, 'http')
 78  })
 79  
 80  test('udp: filter option blocks tracker from tracking torrent', t => {
 81    testFilterOption(t, 'udp')
 82  })
 83  
 84  test('ws: filter option blocks tracker from tracking torrent', t => {
 85    testFilterOption(t, 'ws')
 86  })
 87  
 88  function testFilterCustomError (t, serverType) {
 89    t.plan(8)
 90  
 91    const opts = { serverType } // this is test-suite-only option
 92    opts.filter = (infoHash, params, cb) => {
 93      process.nextTick(() => {
 94        if (infoHash === fixtures.alice.parsedTorrent.infoHash) {
 95          cb(new Error('alice blocked'))
 96        } else {
 97          cb(null)
 98        }
 99      })
100    }
101  
102    common.createServer(t, opts, (server, announceUrl) => {
103      const client1 = new Client({
104        infoHash: fixtures.alice.parsedTorrent.infoHash,
105        announce: announceUrl,
106        peerId,
107        port: 6881,
108        wrtc: {}
109      })
110  
111      client1.on('error', err => { t.error(err) })
112      if (serverType === 'ws') common.mockWebsocketTracker(client1)
113  
114      client1.once('warning', err => {
115        t.ok(/alice blocked/.test(err.message), 'got client warning')
116  
117        client1.destroy(() => {
118          t.pass('client1 destroyed')
119          const client2 = new Client({
120            infoHash: fixtures.leaves.parsedTorrent.infoHash,
121            announce: announceUrl,
122            peerId,
123            port: 6881,
124            wrtc: {}
125          })
126          if (serverType === 'ws') common.mockWebsocketTracker(client2)
127  
128          client2.on('error', err => { t.error(err) })
129          client2.on('warning', err => { t.error(err) })
130  
131          client2.on('update', () => {
132            t.pass('got announce')
133            client2.destroy(() => { t.pass('client2 destroyed') })
134            server.close(() => { t.pass('server closed') })
135          })
136  
137          server.on('start', () => {
138            t.equal(Object.keys(server.torrents).length, 1)
139          })
140  
141          client2.start()
142        })
143      })
144  
145      server.removeAllListeners('warning')
146      server.once('warning', err => {
147        t.ok(/alice blocked/.test(err.message), 'got server warning')
148        t.equal(Object.keys(server.torrents).length, 0)
149      })
150  
151      client1.start()
152    })
153  }
154  
155  test('http: filter option with custom error', t => {
156    testFilterCustomError(t, 'http')
157  })
158  
159  test('udp: filter option filter option with custom error', t => {
160    testFilterCustomError(t, 'udp')
161  })
162  
163  test('ws: filter option filter option with custom error', t => {
164    testFilterCustomError(t, 'ws')
165  })