|
3 | 3 |
|
4 | 4 | import { TypedEventEmitter } from '@libp2p/interface/events' |
5 | 5 | import { start, stop } from '@libp2p/interface/startable' |
6 | | -import { mockConnectionGater, mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks' |
| 6 | +import { mockConnectionGater, mockRegistrar, mockUpgrader, connectionPair, mockStream } from '@libp2p/interface-compliance-tests/mocks' |
7 | 7 | import { createEd25519PeerId } from '@libp2p/peer-id-factory' |
8 | 8 | import { PeerRecord, RecordEnvelope } from '@libp2p/peer-record' |
9 | 9 | import { PersistentPeerStore } from '@libp2p/peer-store' |
@@ -33,6 +33,9 @@ import { Identify } from '../../src/identify/pb/message.js' |
33 | 33 | import { DefaultTransportManager } from '../../src/transport-manager.js' |
34 | 34 | import type { IncomingStreamData } from '@libp2p/interface-internal/registrar' |
35 | 35 | import type { TransportManager } from '@libp2p/interface-internal/transport-manager' |
| 36 | +import { pushable } from 'it-pushable' |
| 37 | +import type { Duplex, Source } from 'it-stream-types' |
| 38 | +import type { Connection } from '@libp2p/interface/src/connection/index.js' |
36 | 39 |
|
37 | 40 | const listenMaddrs = [multiaddr('/ip4/127.0.0.1/tcp/15002/ws')] |
38 | 41 |
|
@@ -504,4 +507,60 @@ describe('identify', () => { |
504 | 507 | }]) |
505 | 508 | expect(peer.id.publicKey).to.equalBytes(remoteComponents.peerId.publicKey) |
506 | 509 | }) |
| 510 | + |
| 511 | + it('should not overwrite addresses when none are sent', async () => { |
| 512 | + const localIdentify = new DefaultIdentifyService(localComponents, defaultInit) |
| 513 | + await start(localIdentify) |
| 514 | + |
| 515 | + const duplex: Duplex<any, Source<any>, any> = { |
| 516 | + source: pushable(), |
| 517 | + sink: async (source) => { |
| 518 | + await drain(source) |
| 519 | + } |
| 520 | + } |
| 521 | + |
| 522 | + duplex.source.push(lp.encode.single(Identify.encode({ |
| 523 | + listenAddrs: [ |
| 524 | + multiaddr('/ip4/127.0.0.1/tcp/4001').bytes |
| 525 | + ], |
| 526 | + protocols: [ |
| 527 | + '/proto/1' |
| 528 | + ] |
| 529 | + }))) |
| 530 | + |
| 531 | + await localIdentify._handlePush({ |
| 532 | + stream: mockStream(duplex), |
| 533 | + connection: stubInterface<Connection>({ |
| 534 | + remotePeer: remoteComponents.peerId |
| 535 | + }) |
| 536 | + }) |
| 537 | + |
| 538 | + const peerData = await localComponents.peerStore.get(remoteComponents.peerId) |
| 539 | + expect(peerData.addresses[0].multiaddr.toString()).to.equal('/ip4/127.0.0.1/tcp/4001') |
| 540 | + expect(peerData.protocols).to.deep.equal(['/proto/1']) |
| 541 | + |
| 542 | + const updateDuplex: Duplex<any, Source<any>, any> = { |
| 543 | + source: pushable(), |
| 544 | + sink: async (source) => { |
| 545 | + await drain(source) |
| 546 | + } |
| 547 | + } |
| 548 | + |
| 549 | + updateDuplex.source.push(lp.encode.single(Identify.encode({ |
| 550 | + protocols: [ |
| 551 | + '/proto/2' |
| 552 | + ] |
| 553 | + }))) |
| 554 | + |
| 555 | + await localIdentify._handlePush({ |
| 556 | + stream: mockStream(updateDuplex), |
| 557 | + connection: stubInterface<Connection>({ |
| 558 | + remotePeer: remoteComponents.peerId |
| 559 | + }) |
| 560 | + }) |
| 561 | + |
| 562 | + const updatedPeerData = await localComponents.peerStore.get(remoteComponents.peerId) |
| 563 | + expect(updatedPeerData.addresses[0].multiaddr.toString()).to.equal('/ip4/127.0.0.1/tcp/4001') |
| 564 | + expect(updatedPeerData.protocols).to.deep.equal(['/proto/2']) |
| 565 | + }) |
507 | 566 | }) |
0 commit comments