|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +const dirtyChai = require('dirty-chai') |
| 6 | +const expect = chai.expect |
| 7 | +chai.use(dirtyChai) |
| 8 | +const PeerInfo = require('peer-info') |
| 9 | +const parallel = require('async/parallel') |
| 10 | +const map = require('async/map') |
| 11 | +const MDNS = require('multicast-dns') |
| 12 | +const OS = require('os') |
| 13 | + |
| 14 | +const Querier = require('../../src/compat/querier') |
| 15 | +const { SERVICE_TAG_LOCAL } = require('../../src/compat/constants') |
| 16 | + |
| 17 | +describe('Querier', () => { |
| 18 | + let querier, mdns |
| 19 | + const peerAddrs = [ |
| 20 | + '/ip4/127.0.0.1/tcp/20001', |
| 21 | + '/ip4/127.0.0.1/tcp/20002' |
| 22 | + ] |
| 23 | + let peerInfos |
| 24 | + |
| 25 | + before(done => { |
| 26 | + map(peerAddrs, (addr, cb) => { |
| 27 | + PeerInfo.create((err, info) => { |
| 28 | + expect(err).to.not.exist() |
| 29 | + info.multiaddrs.add(addr) |
| 30 | + cb(null, info) |
| 31 | + }) |
| 32 | + }, (err, infos) => { |
| 33 | + expect(err).to.not.exist() |
| 34 | + peerInfos = infos |
| 35 | + done() |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + afterEach(done => { |
| 40 | + parallel([ |
| 41 | + cb => querier ? querier.stop(cb) : cb(), |
| 42 | + cb => mdns ? mdns.destroy(cb) : cb() |
| 43 | + ], err => { |
| 44 | + querier = mdns = null |
| 45 | + done(err) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should start and stop', done => { |
| 50 | + const querier = new Querier(peerInfos[0].id) |
| 51 | + |
| 52 | + querier.start(err => { |
| 53 | + expect(err).to.not.exist() |
| 54 | + querier.stop(err => { |
| 55 | + expect(err).to.not.exist() |
| 56 | + done() |
| 57 | + }) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should query on interval', done => { |
| 62 | + querier = new Querier(peerInfos[0].id, { queryPeriod: 0, queryInterval: 10 }) |
| 63 | + mdns = MDNS() |
| 64 | + |
| 65 | + let queryCount = 0 |
| 66 | + |
| 67 | + mdns.on('query', event => { |
| 68 | + const questions = event.questions || [] |
| 69 | + if (!questions.some(q => q.name === SERVICE_TAG_LOCAL)) return |
| 70 | + queryCount++ |
| 71 | + }) |
| 72 | + |
| 73 | + querier.start(err => expect(err).to.not.exist()) |
| 74 | + |
| 75 | + setTimeout(() => { |
| 76 | + // Should have queried at least twice by now! |
| 77 | + expect(queryCount >= 2).to.be.true() |
| 78 | + done() |
| 79 | + }, 100) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should not emit peer for responses with non matching service tags', done => { |
| 83 | + ensureNoPeer(event => { |
| 84 | + const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` |
| 85 | + const bogusServiceTagLocal = '_ifps-discovery._udp' |
| 86 | + |
| 87 | + return [{ |
| 88 | + name: bogusServiceTagLocal, |
| 89 | + type: 'PTR', |
| 90 | + class: 'IN', |
| 91 | + ttl: 120, |
| 92 | + data: peerServiceTagLocal |
| 93 | + }] |
| 94 | + }, done) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should not emit peer for responses with missing TXT record', done => { |
| 98 | + ensureNoPeer(event => { |
| 99 | + const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` |
| 100 | + |
| 101 | + return [{ |
| 102 | + name: SERVICE_TAG_LOCAL, |
| 103 | + type: 'PTR', |
| 104 | + class: 'IN', |
| 105 | + ttl: 120, |
| 106 | + data: peerServiceTagLocal |
| 107 | + }] |
| 108 | + }, done) |
| 109 | + }) |
| 110 | + |
| 111 | + it('should not emit peer for responses with missing peer ID in TXT record', done => { |
| 112 | + ensureNoPeer(event => { |
| 113 | + const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` |
| 114 | + |
| 115 | + return [{ |
| 116 | + name: SERVICE_TAG_LOCAL, |
| 117 | + type: 'PTR', |
| 118 | + class: 'IN', |
| 119 | + ttl: 120, |
| 120 | + data: peerServiceTagLocal |
| 121 | + }, { |
| 122 | + name: peerServiceTagLocal, |
| 123 | + type: 'TXT', |
| 124 | + class: 'IN', |
| 125 | + ttl: 120, |
| 126 | + data: [] // undefined peer ID |
| 127 | + }] |
| 128 | + }, done) |
| 129 | + }) |
| 130 | + |
| 131 | + it('should not emit peer for responses to self', done => { |
| 132 | + ensureNoPeer(event => { |
| 133 | + const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` |
| 134 | + |
| 135 | + return [{ |
| 136 | + name: SERVICE_TAG_LOCAL, |
| 137 | + type: 'PTR', |
| 138 | + class: 'IN', |
| 139 | + ttl: 120, |
| 140 | + data: peerServiceTagLocal |
| 141 | + }, { |
| 142 | + name: peerServiceTagLocal, |
| 143 | + type: 'TXT', |
| 144 | + class: 'IN', |
| 145 | + ttl: 120, |
| 146 | + data: peerInfos[0].id.toB58String() |
| 147 | + }] |
| 148 | + }, done) |
| 149 | + }) |
| 150 | + |
| 151 | + // TODO: unskip when https://github.com/libp2p/js-peer-id/issues/83 is resolved |
| 152 | + it.skip('should not emit peer for responses with invalid peer ID in TXT record', done => { |
| 153 | + ensureNoPeer(event => { |
| 154 | + const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` |
| 155 | + |
| 156 | + return [{ |
| 157 | + name: SERVICE_TAG_LOCAL, |
| 158 | + type: 'PTR', |
| 159 | + class: 'IN', |
| 160 | + ttl: 120, |
| 161 | + data: peerServiceTagLocal |
| 162 | + }, { |
| 163 | + name: peerServiceTagLocal, |
| 164 | + type: 'TXT', |
| 165 | + class: 'IN', |
| 166 | + ttl: 120, |
| 167 | + data: '🤪' |
| 168 | + }] |
| 169 | + }, done) |
| 170 | + }) |
| 171 | + |
| 172 | + it('should not emit peer for responses with missing SRV record', done => { |
| 173 | + ensureNoPeer(event => { |
| 174 | + const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` |
| 175 | + |
| 176 | + return [{ |
| 177 | + name: SERVICE_TAG_LOCAL, |
| 178 | + type: 'PTR', |
| 179 | + class: 'IN', |
| 180 | + ttl: 120, |
| 181 | + data: peerServiceTagLocal |
| 182 | + }, { |
| 183 | + name: peerServiceTagLocal, |
| 184 | + type: 'TXT', |
| 185 | + class: 'IN', |
| 186 | + ttl: 120, |
| 187 | + data: peerInfos[1].id.toB58String() |
| 188 | + }] |
| 189 | + }, done) |
| 190 | + }) |
| 191 | + |
| 192 | + it('should emit peer for responses even if no multiaddrs', done => { |
| 193 | + ensurePeer(event => { |
| 194 | + const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` |
| 195 | + |
| 196 | + return [{ |
| 197 | + name: SERVICE_TAG_LOCAL, |
| 198 | + type: 'PTR', |
| 199 | + class: 'IN', |
| 200 | + ttl: 120, |
| 201 | + data: peerServiceTagLocal |
| 202 | + }, { |
| 203 | + name: peerServiceTagLocal, |
| 204 | + type: 'TXT', |
| 205 | + class: 'IN', |
| 206 | + ttl: 120, |
| 207 | + data: peerInfos[1].id.toB58String() |
| 208 | + }, { |
| 209 | + name: peerServiceTagLocal, |
| 210 | + type: 'SRV', |
| 211 | + class: 'IN', |
| 212 | + ttl: 120, |
| 213 | + data: { |
| 214 | + priority: 10, |
| 215 | + weight: 1, |
| 216 | + port: parseInt(peerAddrs[1].split().pop()), |
| 217 | + target: OS.hostname() |
| 218 | + } |
| 219 | + }] |
| 220 | + }, done) |
| 221 | + }) |
| 222 | + |
| 223 | + it('should emit peer for responses with valid multiaddrs', done => { |
| 224 | + ensurePeer(event => { |
| 225 | + const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` |
| 226 | + |
| 227 | + return [{ |
| 228 | + name: SERVICE_TAG_LOCAL, |
| 229 | + type: 'PTR', |
| 230 | + class: 'IN', |
| 231 | + ttl: 120, |
| 232 | + data: peerServiceTagLocal |
| 233 | + }, { |
| 234 | + name: peerServiceTagLocal, |
| 235 | + type: 'TXT', |
| 236 | + class: 'IN', |
| 237 | + ttl: 120, |
| 238 | + data: peerInfos[1].id.toB58String() |
| 239 | + }, { |
| 240 | + name: peerServiceTagLocal, |
| 241 | + type: 'SRV', |
| 242 | + class: 'IN', |
| 243 | + ttl: 120, |
| 244 | + data: { |
| 245 | + priority: 10, |
| 246 | + weight: 1, |
| 247 | + port: parseInt(peerAddrs[1].split().pop()), |
| 248 | + target: OS.hostname() |
| 249 | + } |
| 250 | + }, { |
| 251 | + name: OS.hostname(), |
| 252 | + type: peerAddrs[1].startsWith('/ip4') ? 'A' : 'AAAA', |
| 253 | + class: 'IN', |
| 254 | + ttl: 120, |
| 255 | + data: peerAddrs[1].split('/')[2] |
| 256 | + }] |
| 257 | + }, done) |
| 258 | + }) |
| 259 | + |
| 260 | + /** |
| 261 | + * Ensure peerInfos[1] are emitted from `querier` |
| 262 | + * @param {Function} getResponse Given a query, construct a response to test the querier |
| 263 | + * @param {Function} callback Callback called when test finishes |
| 264 | + */ |
| 265 | + function ensurePeer (getResponse, callback) { |
| 266 | + querier = new Querier(peerInfos[0].id) |
| 267 | + mdns = MDNS() |
| 268 | + |
| 269 | + mdns.on('query', (event, info) => { |
| 270 | + const questions = event.questions || [] |
| 271 | + if (!questions.some(q => q.name === SERVICE_TAG_LOCAL)) return |
| 272 | + mdns.respond(getResponse(event, info), info) |
| 273 | + }) |
| 274 | + |
| 275 | + let peerInfo |
| 276 | + |
| 277 | + querier.on('peer', info => { |
| 278 | + // Ignore non-test peers |
| 279 | + if (!info.id.isEqual(peerInfos[1].id)) return |
| 280 | + peerInfo = info |
| 281 | + }) |
| 282 | + |
| 283 | + querier.start(err => { |
| 284 | + if (err) return callback(err) |
| 285 | + setTimeout(() => { |
| 286 | + callback(peerInfo ? null : new Error('Missing peer')) |
| 287 | + }, 100) |
| 288 | + }) |
| 289 | + } |
| 290 | + |
| 291 | + /** |
| 292 | + * Ensure none of peerInfos are emitted from `querier` |
| 293 | + * @param {Function} getResponse Given a query, construct a response to test the querier |
| 294 | + * @param {Function} callback Callback called when test finishes |
| 295 | + */ |
| 296 | + function ensureNoPeer (getResponse, callback) { |
| 297 | + querier = new Querier(peerInfos[0].id) |
| 298 | + mdns = MDNS() |
| 299 | + |
| 300 | + mdns.on('query', (event, info) => { |
| 301 | + const questions = event.questions || [] |
| 302 | + if (!questions.some(q => q.name === SERVICE_TAG_LOCAL)) return |
| 303 | + mdns.respond(getResponse(event, info), info) |
| 304 | + }) |
| 305 | + |
| 306 | + let peerInfo |
| 307 | + |
| 308 | + querier.on('peer', info => { |
| 309 | + // Ignore non-test peers |
| 310 | + if (!info.id.isEqual(peerInfos[0].id) && !info.id.isEqual(peerInfos[1].id)) return |
| 311 | + peerInfo = info |
| 312 | + }) |
| 313 | + |
| 314 | + querier.start(err => { |
| 315 | + if (err) return callback(err) |
| 316 | + setTimeout(() => { |
| 317 | + if (!peerInfo) return callback() |
| 318 | + callback(Object.assign(new Error('Unexpected peer'), { peerInfo })) |
| 319 | + }, 100) |
| 320 | + }) |
| 321 | + } |
| 322 | +}) |
0 commit comments