|
2 | 2 |
|
3 | 3 | import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-compliance-tests/mocks'
|
4 | 4 | import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
5 |
| -import { multiaddr } from '@multiformats/multiaddr' |
| 5 | +import { multiaddr, resolvers } from '@multiformats/multiaddr' |
6 | 6 | import { expect } from 'aegir/chai'
|
7 | 7 | import delay from 'delay'
|
8 | 8 | import pDefer from 'p-defer'
|
@@ -280,4 +280,52 @@ describe('dial queue', () => {
|
280 | 280 | // Dial attempt finished without connection
|
281 | 281 | expect(signals['/ip4/127.0.0.1/tcp/1233']).to.have.property('aborted', true)
|
282 | 282 | })
|
| 283 | + |
| 284 | + it('should ignore DNS addresses for other peers', async () => { |
| 285 | + const remotePeer = await createEd25519PeerId() |
| 286 | + const otherRemotePeer = await createEd25519PeerId() |
| 287 | + const ma = multiaddr(`/dnsaddr/example.com/p2p/${remotePeer}`) |
| 288 | + const maStr = `/ip4/123.123.123.123/tcp/2348/p2p/${remotePeer}` |
| 289 | + const resolvedAddresses = [ |
| 290 | + `/ip4/234.234.234.234/tcp/4213/p2p/${otherRemotePeer}`, |
| 291 | + maStr |
| 292 | + ] |
| 293 | + |
| 294 | + let resolvedDNSAddrs = false |
| 295 | + let dialedBadAddress = false |
| 296 | + |
| 297 | + // simulate a DNSAddr that resolves to multiple different peers like |
| 298 | + // bootstrap.libp2p.io |
| 299 | + resolvers.set('dnsaddr', async (addr) => { |
| 300 | + if (addr.equals(ma)) { |
| 301 | + resolvedDNSAddrs = true |
| 302 | + return resolvedAddresses |
| 303 | + } |
| 304 | + |
| 305 | + return [] |
| 306 | + }) |
| 307 | + |
| 308 | + dialer = new DialQueue(components, { |
| 309 | + maxParallelDials: 50 |
| 310 | + }) |
| 311 | + components.transportManager.transportForMultiaddr.returns(stubInterface<Transport>()) |
| 312 | + |
| 313 | + const connection = mockConnection(mockMultiaddrConnection(mockDuplex(), remotePeer)) |
| 314 | + |
| 315 | + components.transportManager.dial.callsFake(async (ma, opts = {}) => { |
| 316 | + if (ma.toString() === maStr) { |
| 317 | + await delay(100) |
| 318 | + return connection |
| 319 | + } |
| 320 | + |
| 321 | + dialedBadAddress = true |
| 322 | + throw new Error('Could not dial address') |
| 323 | + }) |
| 324 | + |
| 325 | + await expect(dialer.dial(ma)).to.eventually.equal(connection) |
| 326 | + expect(resolvedDNSAddrs).to.be.true('Did not resolve DNSAddrs') |
| 327 | + expect(dialedBadAddress).to.be.false('Dialed address with wrong peer id') |
| 328 | + |
| 329 | + resolvers.delete('dnsaddr') |
| 330 | + }) |
283 | 331 | })
|
0 commit comments