Skip to content

Commit e93d258

Browse files
authored
test: add test for inbound and outbound identify (#1482)
Ensure identify is run on both inbound and outbound connections.
1 parent 9e3ef64 commit e93d258

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

test/identify/service.node.ts

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/* eslint-env mocha */
2+
3+
import { multiaddr } from '@multiformats/multiaddr'
4+
import { expect } from 'aegir/chai'
5+
import { pEvent } from 'p-event'
6+
import sinon from 'sinon'
7+
import { identifyService } from '../../src/identify/index.js'
8+
import { createLibp2pNode } from '../../src/libp2p.js'
9+
import { createBaseOptions } from '../utils/base-options.js'
10+
import { createNode } from '../utils/creators/peer.js'
11+
import type { Libp2p } from '@libp2p/interface-libp2p'
12+
13+
const LOCAL_PORT = 47321
14+
const REMOTE_PORT = 47322
15+
16+
describe('identify', () => {
17+
let libp2p: Libp2p<{ identify: unknown }>
18+
let remoteLibp2p: Libp2p<{ identify: unknown }>
19+
20+
beforeEach(async () => {
21+
libp2p = await createLibp2pNode(createBaseOptions({
22+
addresses: {
23+
announce: [`/dns4/localhost/tcp/${LOCAL_PORT}`],
24+
listen: [`/ip4/0.0.0.0/tcp/${LOCAL_PORT}`]
25+
},
26+
services: {
27+
identify: identifyService()
28+
}
29+
}))
30+
remoteLibp2p = await createLibp2pNode(createBaseOptions({
31+
addresses: {
32+
announce: [`/dns4/localhost/tcp/${REMOTE_PORT}`],
33+
listen: [`/ip4/0.0.0.0/tcp/${REMOTE_PORT}`]
34+
},
35+
services: {
36+
identify: identifyService()
37+
}
38+
}))
39+
})
40+
41+
afterEach(async () => {
42+
sinon.restore()
43+
44+
if (libp2p != null) {
45+
await libp2p.stop()
46+
}
47+
48+
if (remoteLibp2p != null) {
49+
await remoteLibp2p.stop()
50+
}
51+
})
52+
53+
it('should run identify automatically for outbound connections', async () => {
54+
await libp2p.start()
55+
await remoteLibp2p.start()
56+
57+
if (libp2p.services.identify == null) {
58+
throw new Error('Identity service was not configured')
59+
}
60+
61+
const eventPromise = pEvent(libp2p, 'peer:identify')
62+
63+
// dial local -> remote via loopback in order to assert we receive the announce address via identify
64+
const connection = await libp2p.dial(multiaddr(`/ip4/127.0.0.1/tcp/${REMOTE_PORT}/p2p/${remoteLibp2p.peerId.toString()}`))
65+
expect(connection).to.exist()
66+
67+
// wait for identify to run on the new connection
68+
await eventPromise
69+
70+
// assert we have received certified announce addresses
71+
const peer = await libp2p.peerStore.get(remoteLibp2p.peerId)
72+
expect(peer.addresses).to.have.lengthOf(1)
73+
expect(peer.addresses[0].isCertified).to.be.true('did not receive certified address via identify')
74+
expect(peer.addresses[0].multiaddr.toString()).to.startWith('/dns4/localhost/', 'did not receive announce address via identify')
75+
})
76+
77+
it('should run identify automatically for inbound connections', async () => {
78+
await libp2p.start()
79+
await remoteLibp2p.start()
80+
81+
if (libp2p.services.identify == null) {
82+
throw new Error('Identity service was not configured')
83+
}
84+
85+
const eventPromise = pEvent(remoteLibp2p, 'peer:identify')
86+
87+
// dial remote -> local via loopback in order to assert we receive the announce address via identify
88+
const connection = await remoteLibp2p.dial(multiaddr(`/ip4/127.0.0.1/tcp/${LOCAL_PORT}/p2p/${libp2p.peerId.toString()}`))
89+
expect(connection).to.exist()
90+
91+
// wait for identify to run on the new connection
92+
await eventPromise
93+
94+
// assert we have received certified announce addresses
95+
const peer = await libp2p.peerStore.get(remoteLibp2p.peerId)
96+
expect(peer.addresses).to.have.lengthOf(1)
97+
expect(peer.addresses[0].isCertified).to.be.true('did not receive certified address via identify')
98+
expect(peer.addresses[0].multiaddr.toString()).to.startWith('/dns4/localhost/', 'did not receive announce address via identify')
99+
})
100+
101+
it('should identify connection on dial and get proper announce addresses', async () => {
102+
const announceAddrs = [
103+
'/dns4/peers1.com/tcp/433/wss',
104+
'/dns4/peers2.com/tcp/433/wss'
105+
]
106+
const port = 58322
107+
const protocol = '/ipfs/bitswap/1.2.0'
108+
109+
const receiver = await createNode({
110+
config: {
111+
addresses: {
112+
announce: announceAddrs,
113+
listen: [`/ip4/127.0.0.1/tcp/${port}/ws`]
114+
},
115+
services: {
116+
identify: identifyService()
117+
}
118+
}
119+
})
120+
await receiver.handle(protocol, () => {})
121+
122+
const sender = await createNode({
123+
config: {
124+
addresses: {
125+
listen: ['/ip4/127.0.0.1/tcp/0/ws']
126+
},
127+
services: {
128+
identify: identifyService()
129+
}
130+
}
131+
})
132+
133+
const eventPromise = pEvent(sender, 'peer:identify')
134+
135+
const connection = await sender.dial(multiaddr(`/ip4/127.0.0.1/tcp/${port}/ws/p2p/${receiver.peerId.toString()}`))
136+
137+
await eventPromise
138+
139+
const stream = await connection.newStream(protocol)
140+
const clientPeer = await sender.peerStore.get(receiver.peerId)
141+
142+
expect(clientPeer.addresses).to.have.length(2)
143+
expect(clientPeer.addresses[0].multiaddr.toString()).to.equal(announceAddrs[0].toString())
144+
expect(clientPeer.addresses[1].multiaddr.toString()).to.equal(announceAddrs[1].toString())
145+
146+
stream.close()
147+
await connection.close()
148+
await receiver.stop()
149+
await sender.stop()
150+
})
151+
})

0 commit comments

Comments
 (0)