@libp2p/interface-compliance-tests 2.0.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -11
- package/dist/src/{utils/is-valid-tick.d.ts → is-valid-tick.d.ts} +0 -0
- package/dist/src/is-valid-tick.d.ts.map +1 -0
- package/dist/src/{utils/is-valid-tick.js → is-valid-tick.js} +0 -0
- package/dist/src/is-valid-tick.js.map +1 -0
- package/dist/src/{utils/peers.d.ts → peers.d.ts} +0 -0
- package/dist/src/peers.d.ts.map +1 -0
- package/dist/src/{utils/peers.js → peers.js} +0 -0
- package/dist/src/peers.js.map +1 -0
- package/package.json +11 -89
- package/src/{utils/is-valid-tick.ts → is-valid-tick.ts} +0 -0
- package/src/{utils/peers.ts → peers.ts} +0 -0
- package/dist/src/connection/connection.d.ts +0 -5
- package/dist/src/connection/connection.d.ts.map +0 -1
- package/dist/src/connection/connection.js +0 -146
- package/dist/src/connection/connection.js.map +0 -1
- package/dist/src/connection/index.d.ts +0 -5
- package/dist/src/connection/index.d.ts.map +0 -1
- package/dist/src/connection/index.js +0 -5
- package/dist/src/connection/index.js.map +0 -1
- package/dist/src/connection-encrypter/index.d.ts +0 -5
- package/dist/src/connection-encrypter/index.d.ts.map +0 -1
- package/dist/src/connection-encrypter/index.js +0 -77
- package/dist/src/connection-encrypter/index.js.map +0 -1
- package/dist/src/connection-encrypter/utils/index.d.ts +0 -3
- package/dist/src/connection-encrypter/utils/index.d.ts.map +0 -1
- package/dist/src/connection-encrypter/utils/index.js +0 -18
- package/dist/src/connection-encrypter/utils/index.js.map +0 -1
- package/dist/src/mocks/connection-encrypter.d.ts +0 -3
- package/dist/src/mocks/connection-encrypter.d.ts.map +0 -1
- package/dist/src/mocks/connection-encrypter.js +0 -93
- package/dist/src/mocks/connection-encrypter.js.map +0 -1
- package/dist/src/mocks/connection-gater.d.ts +0 -12
- package/dist/src/mocks/connection-gater.d.ts.map +0 -1
- package/dist/src/mocks/connection-gater.js +0 -14
- package/dist/src/mocks/connection-gater.js.map +0 -1
- package/dist/src/mocks/connection-manager.d.ts +0 -28
- package/dist/src/mocks/connection-manager.d.ts.map +0 -1
- package/dist/src/mocks/connection-manager.js +0 -107
- package/dist/src/mocks/connection-manager.js.map +0 -1
- package/dist/src/mocks/connection.d.ts +0 -18
- package/dist/src/mocks/connection.d.ts.map +0 -1
- package/dist/src/mocks/connection.js +0 -138
- package/dist/src/mocks/connection.js.map +0 -1
- package/dist/src/mocks/duplex.d.ts +0 -3
- package/dist/src/mocks/duplex.d.ts.map +0 -1
- package/dist/src/mocks/duplex.js +0 -7
- package/dist/src/mocks/duplex.js.map +0 -1
- package/dist/src/mocks/index.d.ts +0 -10
- package/dist/src/mocks/index.d.ts.map +0 -1
- package/dist/src/mocks/index.js +0 -9
- package/dist/src/mocks/index.js.map +0 -1
- package/dist/src/mocks/multiaddr-connection.d.ts +0 -17
- package/dist/src/mocks/multiaddr-connection.d.ts.map +0 -1
- package/dist/src/mocks/multiaddr-connection.js +0 -51
- package/dist/src/mocks/multiaddr-connection.js.map +0 -1
- package/dist/src/mocks/muxer.d.ts +0 -9
- package/dist/src/mocks/muxer.d.ts.map +0 -1
- package/dist/src/mocks/muxer.js +0 -302
- package/dist/src/mocks/muxer.js.map +0 -1
- package/dist/src/mocks/registrar.d.ts +0 -18
- package/dist/src/mocks/registrar.d.ts.map +0 -1
- package/dist/src/mocks/registrar.js +0 -73
- package/dist/src/mocks/registrar.js.map +0 -1
- package/dist/src/mocks/upgrader.d.ts +0 -15
- package/dist/src/mocks/upgrader.d.ts.map +0 -1
- package/dist/src/mocks/upgrader.js +0 -33
- package/dist/src/mocks/upgrader.js.map +0 -1
- package/dist/src/peer-discovery/index.d.ts +0 -6
- package/dist/src/peer-discovery/index.d.ts.map +0 -1
- package/dist/src/peer-discovery/index.js +0 -64
- package/dist/src/peer-discovery/index.js.map +0 -1
- package/dist/src/pubsub/api.d.ts +0 -6
- package/dist/src/pubsub/api.d.ts.map +0 -1
- package/dist/src/pubsub/api.js +0 -87
- package/dist/src/pubsub/api.js.map +0 -1
- package/dist/src/pubsub/connection-handlers.d.ts +0 -6
- package/dist/src/pubsub/connection-handlers.d.ts.map +0 -1
- package/dist/src/pubsub/connection-handlers.js +0 -329
- package/dist/src/pubsub/connection-handlers.js.map +0 -1
- package/dist/src/pubsub/emit-self.d.ts +0 -6
- package/dist/src/pubsub/emit-self.d.ts.map +0 -1
- package/dist/src/pubsub/emit-self.js +0 -86
- package/dist/src/pubsub/emit-self.js.map +0 -1
- package/dist/src/pubsub/index.d.ts +0 -10
- package/dist/src/pubsub/index.d.ts.map +0 -1
- package/dist/src/pubsub/index.js +0 -17
- package/dist/src/pubsub/index.js.map +0 -1
- package/dist/src/pubsub/messages.d.ts +0 -6
- package/dist/src/pubsub/messages.d.ts.map +0 -1
- package/dist/src/pubsub/messages.js +0 -46
- package/dist/src/pubsub/messages.js.map +0 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +0 -6
- package/dist/src/pubsub/multiple-nodes.d.ts.map +0 -1
- package/dist/src/pubsub/multiple-nodes.js +0 -350
- package/dist/src/pubsub/multiple-nodes.js.map +0 -1
- package/dist/src/pubsub/two-nodes.d.ts +0 -6
- package/dist/src/pubsub/two-nodes.d.ts.map +0 -1
- package/dist/src/pubsub/two-nodes.js +0 -185
- package/dist/src/pubsub/two-nodes.js.map +0 -1
- package/dist/src/pubsub/utils.d.ts +0 -6
- package/dist/src/pubsub/utils.d.ts.map +0 -1
- package/dist/src/pubsub/utils.js +0 -22
- package/dist/src/pubsub/utils.js.map +0 -1
- package/dist/src/record/index.d.ts +0 -5
- package/dist/src/record/index.d.ts.map +0 -1
- package/dist/src/record/index.js +0 -25
- package/dist/src/record/index.js.map +0 -1
- package/dist/src/stream-muxer/base-test.d.ts +0 -5
- package/dist/src/stream-muxer/base-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/base-test.js +0 -147
- package/dist/src/stream-muxer/base-test.js.map +0 -1
- package/dist/src/stream-muxer/close-test.d.ts +0 -5
- package/dist/src/stream-muxer/close-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/close-test.js +0 -180
- package/dist/src/stream-muxer/close-test.js.map +0 -1
- package/dist/src/stream-muxer/index.d.ts +0 -5
- package/dist/src/stream-muxer/index.d.ts.map +0 -1
- package/dist/src/stream-muxer/index.js +0 -13
- package/dist/src/stream-muxer/index.js.map +0 -1
- package/dist/src/stream-muxer/mega-stress-test.d.ts +0 -5
- package/dist/src/stream-muxer/mega-stress-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/mega-stress-test.js +0 -12
- package/dist/src/stream-muxer/mega-stress-test.js.map +0 -1
- package/dist/src/stream-muxer/spawner.d.ts +0 -4
- package/dist/src/stream-muxer/spawner.d.ts.map +0 -1
- package/dist/src/stream-muxer/spawner.js +0 -34
- package/dist/src/stream-muxer/spawner.js.map +0 -1
- package/dist/src/stream-muxer/stress-test.d.ts +0 -5
- package/dist/src/stream-muxer/stress-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/stress-test.js +0 -26
- package/dist/src/stream-muxer/stress-test.js.map +0 -1
- package/dist/src/topology/topology.d.ts +0 -5
- package/dist/src/topology/topology.d.ts.map +0 -1
- package/dist/src/topology/topology.js +0 -20
- package/dist/src/topology/topology.js.map +0 -1
- package/dist/src/transport/dial-test.d.ts +0 -5
- package/dist/src/transport/dial-test.d.ts.map +0 -1
- package/dist/src/transport/dial-test.js +0 -97
- package/dist/src/transport/dial-test.js.map +0 -1
- package/dist/src/transport/filter-test.d.ts +0 -5
- package/dist/src/transport/filter-test.d.ts.map +0 -1
- package/dist/src/transport/filter-test.js +0 -18
- package/dist/src/transport/filter-test.js.map +0 -1
- package/dist/src/transport/index.d.ts +0 -15
- package/dist/src/transport/index.d.ts.map +0 -1
- package/dist/src/transport/index.js +0 -11
- package/dist/src/transport/index.js.map +0 -1
- package/dist/src/transport/listen-test.d.ts +0 -5
- package/dist/src/transport/listen-test.d.ts.map +0 -1
- package/dist/src/transport/listen-test.js +0 -152
- package/dist/src/transport/listen-test.js.map +0 -1
- package/dist/src/utils/is-valid-tick.d.ts.map +0 -1
- package/dist/src/utils/is-valid-tick.js.map +0 -1
- package/dist/src/utils/peers.d.ts.map +0 -1
- package/dist/src/utils/peers.js.map +0 -1
- package/src/connection/README.md +0 -256
- package/src/connection/connection.ts +0 -176
- package/src/connection/index.ts +0 -7
- package/src/connection-encrypter/index.ts +0 -104
- package/src/connection-encrypter/utils/index.ts +0 -23
- package/src/mocks/connection-encrypter.ts +0 -110
- package/src/mocks/connection-gater.ts +0 -14
- package/src/mocks/connection-manager.ts +0 -141
- package/src/mocks/connection.ts +0 -205
- package/src/mocks/duplex.ts +0 -8
- package/src/mocks/index.ts +0 -10
- package/src/mocks/multiaddr-connection.ts +0 -66
- package/src/mocks/muxer.ts +0 -424
- package/src/mocks/registrar.ts +0 -96
- package/src/mocks/upgrader.ts +0 -46
- package/src/peer-discovery/index.ts +0 -89
- package/src/pubsub/api.ts +0 -115
- package/src/pubsub/connection-handlers.ts +0 -413
- package/src/pubsub/emit-self.ts +0 -105
- package/src/pubsub/index.ts +0 -25
- package/src/pubsub/messages.ts +0 -58
- package/src/pubsub/multiple-nodes.ts +0 -429
- package/src/pubsub/two-nodes.ts +0 -235
- package/src/pubsub/utils.ts +0 -28
- package/src/record/index.ts +0 -32
- package/src/stream-muxer/base-test.ts +0 -190
- package/src/stream-muxer/close-test.ts +0 -227
- package/src/stream-muxer/index.ts +0 -15
- package/src/stream-muxer/mega-stress-test.ts +0 -15
- package/src/stream-muxer/spawner.ts +0 -52
- package/src/stream-muxer/stress-test.ts +0 -29
- package/src/topology/topology.ts +0 -25
- package/src/transport/dial-test.ts +0 -123
- package/src/transport/filter-test.ts +0 -25
- package/src/transport/index.ts +0 -25
- package/src/transport/listen-test.ts +0 -191
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import { expect } from 'aegir/chai'
|
|
2
|
-
import sinon from 'sinon'
|
|
3
|
-
import type { TestSetup } from '../index.js'
|
|
4
|
-
import type { Connection } from '@libp2p/interfaces/connection'
|
|
5
|
-
|
|
6
|
-
export default (test: TestSetup<Connection>) => {
|
|
7
|
-
describe('connection', () => {
|
|
8
|
-
describe('open connection', () => {
|
|
9
|
-
let connection: Connection
|
|
10
|
-
|
|
11
|
-
beforeEach(async () => {
|
|
12
|
-
connection = await test.setup()
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
afterEach(async () => {
|
|
16
|
-
await connection.close()
|
|
17
|
-
await test.teardown()
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
it('should have properties set', () => {
|
|
21
|
-
expect(connection.id).to.exist()
|
|
22
|
-
expect(connection.remotePeer).to.exist()
|
|
23
|
-
expect(connection.remoteAddr).to.exist()
|
|
24
|
-
expect(connection.stat.status).to.equal('OPEN')
|
|
25
|
-
expect(connection.stat.timeline.open).to.exist()
|
|
26
|
-
expect(connection.stat.timeline.upgraded).to.exist()
|
|
27
|
-
expect(connection.stat.timeline.close).to.not.exist()
|
|
28
|
-
expect(connection.stat.direction).to.exist()
|
|
29
|
-
expect(connection.streams).to.eql([])
|
|
30
|
-
expect(connection.tags).to.eql([])
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('should get the metadata of an open connection', () => {
|
|
34
|
-
const stat = connection.stat
|
|
35
|
-
|
|
36
|
-
expect(stat.status).to.equal('OPEN')
|
|
37
|
-
expect(stat.direction).to.exist()
|
|
38
|
-
expect(stat.timeline.open).to.exist()
|
|
39
|
-
expect(stat.timeline.upgraded).to.exist()
|
|
40
|
-
expect(stat.timeline.close).to.not.exist()
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('should return an empty array of streams', () => {
|
|
44
|
-
const streams = connection.streams
|
|
45
|
-
|
|
46
|
-
expect(streams).to.eql([])
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('should be able to create a new stream', async () => {
|
|
50
|
-
const protocolToUse = '/echo/0.0.1'
|
|
51
|
-
const { stream, protocol } = await connection.newStream([protocolToUse])
|
|
52
|
-
|
|
53
|
-
expect(protocol).to.equal(protocolToUse)
|
|
54
|
-
|
|
55
|
-
const connStreams = await connection.streams
|
|
56
|
-
|
|
57
|
-
expect(stream).to.exist()
|
|
58
|
-
expect(connStreams).to.exist()
|
|
59
|
-
expect(connStreams).to.have.lengthOf(1)
|
|
60
|
-
expect(connStreams[0]).to.equal(stream)
|
|
61
|
-
})
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
describe('close connection', () => {
|
|
65
|
-
let connection: Connection
|
|
66
|
-
let timelineProxy
|
|
67
|
-
const proxyHandler = {
|
|
68
|
-
set () {
|
|
69
|
-
// @ts-expect-error - TS fails to infer here
|
|
70
|
-
return Reflect.set(...arguments)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
beforeEach(async () => {
|
|
75
|
-
timelineProxy = new Proxy({
|
|
76
|
-
open: Date.now() - 10,
|
|
77
|
-
upgraded: Date.now()
|
|
78
|
-
}, proxyHandler)
|
|
79
|
-
|
|
80
|
-
connection = await test.setup({
|
|
81
|
-
stat: {
|
|
82
|
-
timeline: timelineProxy,
|
|
83
|
-
direction: 'outbound',
|
|
84
|
-
encryption: '/crypto/1.0.0',
|
|
85
|
-
multiplexer: '/muxer/1.0.0'
|
|
86
|
-
}
|
|
87
|
-
})
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
afterEach(async () => {
|
|
91
|
-
await test.teardown()
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
it('should be able to close the connection after being created', async () => {
|
|
95
|
-
expect(connection.stat.timeline.close).to.not.exist()
|
|
96
|
-
await connection.close()
|
|
97
|
-
|
|
98
|
-
expect(connection.stat.timeline.close).to.exist()
|
|
99
|
-
expect(connection.stat.status).to.equal('CLOSED')
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
it('should be able to close the connection after opening a stream', async () => {
|
|
103
|
-
// Open stream
|
|
104
|
-
const protocol = '/echo/0.0.1'
|
|
105
|
-
await connection.newStream([protocol])
|
|
106
|
-
|
|
107
|
-
// Close connection
|
|
108
|
-
expect(connection.stat.timeline.close).to.not.exist()
|
|
109
|
-
await connection.close()
|
|
110
|
-
|
|
111
|
-
expect(connection.stat.timeline.close).to.exist()
|
|
112
|
-
expect(connection.stat.status).to.equal('CLOSED')
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
it('should properly track streams', async () => {
|
|
116
|
-
// Open stream
|
|
117
|
-
const protocol = '/echo/0.0.1'
|
|
118
|
-
const { stream } = await connection.newStream([protocol])
|
|
119
|
-
const trackedStream = connection.registry.get(stream.id)
|
|
120
|
-
expect(trackedStream).to.have.property('protocol', protocol)
|
|
121
|
-
|
|
122
|
-
// Close stream
|
|
123
|
-
await stream.close()
|
|
124
|
-
|
|
125
|
-
expect(connection.registry.get(stream.id)).to.not.exist()
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
it('should support a proxy on the timeline', async () => {
|
|
129
|
-
sinon.spy(proxyHandler, 'set')
|
|
130
|
-
expect(connection.stat.timeline.close).to.not.exist()
|
|
131
|
-
|
|
132
|
-
await connection.close()
|
|
133
|
-
// @ts-expect-error - fails to infer callCount
|
|
134
|
-
expect(proxyHandler.set.callCount).to.equal(1)
|
|
135
|
-
// @ts-expect-error - fails to infer getCall
|
|
136
|
-
const [obj, key, value] = proxyHandler.set.getCall(0).args
|
|
137
|
-
expect(obj).to.eql(connection.stat.timeline)
|
|
138
|
-
expect(key).to.equal('close')
|
|
139
|
-
expect(value).to.be.a('number').that.equals(connection.stat.timeline.close)
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
it('should fail to create a new stream if the connection is closing', async () => {
|
|
143
|
-
expect(connection.stat.timeline.close).to.not.exist()
|
|
144
|
-
const p = connection.close()
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
const protocol = '/echo/0.0.1'
|
|
148
|
-
await connection.newStream([protocol])
|
|
149
|
-
} catch (err: any) {
|
|
150
|
-
expect(err).to.exist()
|
|
151
|
-
return
|
|
152
|
-
} finally {
|
|
153
|
-
await p
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
throw new Error('should fail to create a new stream if the connection is closing')
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
it('should fail to create a new stream if the connection is closed', async () => {
|
|
160
|
-
expect(connection.stat.timeline.close).to.not.exist()
|
|
161
|
-
await connection.close()
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
const protocol = '/echo/0.0.1'
|
|
165
|
-
await connection.newStream([protocol])
|
|
166
|
-
} catch (err: any) {
|
|
167
|
-
expect(err).to.exist()
|
|
168
|
-
expect(err.code).to.equal('ERR_CONNECTION_CLOSED')
|
|
169
|
-
return
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
throw new Error('should fail to create a new stream if the connection is closing')
|
|
173
|
-
})
|
|
174
|
-
})
|
|
175
|
-
})
|
|
176
|
-
}
|
package/src/connection/index.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { expect } from 'aegir/chai'
|
|
2
|
-
import { pipe } from 'it-pipe'
|
|
3
|
-
import * as PeerIdFactory from '@libp2p/peer-id-factory'
|
|
4
|
-
import all from 'it-all'
|
|
5
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
6
|
-
import peers from '../utils/peers.js'
|
|
7
|
-
import { UnexpectedPeerError } from '@libp2p/interfaces/connection-encrypter/errors'
|
|
8
|
-
import { createMaConnPair } from './utils/index.js'
|
|
9
|
-
import type { TestSetup } from '../index.js'
|
|
10
|
-
import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'
|
|
11
|
-
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
12
|
-
import type { Source } from 'it-stream-types'
|
|
13
|
-
|
|
14
|
-
export default (common: TestSetup<ConnectionEncrypter>) => {
|
|
15
|
-
describe('interface-connection-encrypter compliance tests', () => {
|
|
16
|
-
let crypto: ConnectionEncrypter
|
|
17
|
-
let localPeer: PeerId
|
|
18
|
-
let remotePeer: PeerId
|
|
19
|
-
let mitmPeer: PeerId
|
|
20
|
-
|
|
21
|
-
before(async () => {
|
|
22
|
-
[
|
|
23
|
-
crypto,
|
|
24
|
-
localPeer,
|
|
25
|
-
remotePeer,
|
|
26
|
-
mitmPeer
|
|
27
|
-
] = await Promise.all([
|
|
28
|
-
common.setup(),
|
|
29
|
-
PeerIdFactory.createFromJSON(peers[0]),
|
|
30
|
-
PeerIdFactory.createFromJSON(peers[1]),
|
|
31
|
-
PeerIdFactory.createFromJSON(peers[2])
|
|
32
|
-
])
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
after(async () => {
|
|
36
|
-
await common.teardown()
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
it('has a protocol string', () => {
|
|
40
|
-
expect(crypto.protocol).to.exist()
|
|
41
|
-
expect(crypto.protocol).to.be.a('string')
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('it wraps the provided duplex connection', async () => {
|
|
45
|
-
const [localConn, remoteConn] = createMaConnPair()
|
|
46
|
-
|
|
47
|
-
const [
|
|
48
|
-
inboundResult,
|
|
49
|
-
outboundResult
|
|
50
|
-
] = await Promise.all([
|
|
51
|
-
crypto.secureInbound(remotePeer, localConn),
|
|
52
|
-
crypto.secureOutbound(localPeer, remoteConn, remotePeer)
|
|
53
|
-
])
|
|
54
|
-
|
|
55
|
-
// Echo server
|
|
56
|
-
void pipe(inboundResult.conn, inboundResult.conn)
|
|
57
|
-
|
|
58
|
-
// Send some data and collect the result
|
|
59
|
-
const input = uint8ArrayFromString('data to encrypt')
|
|
60
|
-
const result = await pipe(
|
|
61
|
-
[input],
|
|
62
|
-
outboundResult.conn,
|
|
63
|
-
// Convert BufferList to Buffer via slice
|
|
64
|
-
(source: Source<Uint8Array>) => (async function * toBuffer () {
|
|
65
|
-
for await (const chunk of source) {
|
|
66
|
-
yield chunk.slice()
|
|
67
|
-
}
|
|
68
|
-
})(),
|
|
69
|
-
async (source) => await all(source)
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
expect(result).to.eql([input])
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
it('should return the remote peer id', async () => {
|
|
76
|
-
const [localConn, remoteConn] = createMaConnPair()
|
|
77
|
-
|
|
78
|
-
const [
|
|
79
|
-
inboundResult,
|
|
80
|
-
outboundResult
|
|
81
|
-
] = await Promise.all([
|
|
82
|
-
crypto.secureInbound(remotePeer, localConn),
|
|
83
|
-
crypto.secureOutbound(localPeer, remoteConn, remotePeer)
|
|
84
|
-
])
|
|
85
|
-
|
|
86
|
-
// Inbound should return the initiator (local) peer
|
|
87
|
-
expect(inboundResult.remotePeer.toBytes()).to.equalBytes(localPeer.toBytes())
|
|
88
|
-
// Outbound should return the receiver (remote) peer
|
|
89
|
-
expect(outboundResult.remotePeer.toBytes()).to.equalBytes(remotePeer.toBytes())
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
it('inbound connections should verify peer integrity if known', async () => {
|
|
93
|
-
const [localConn, remoteConn] = createMaConnPair()
|
|
94
|
-
|
|
95
|
-
await Promise.all([
|
|
96
|
-
crypto.secureInbound(remotePeer, localConn, mitmPeer),
|
|
97
|
-
crypto.secureOutbound(localPeer, remoteConn, remotePeer)
|
|
98
|
-
]).then(() => expect.fail(), (err) => {
|
|
99
|
-
expect(err).to.exist()
|
|
100
|
-
expect(err).to.have.property('code', UnexpectedPeerError.code)
|
|
101
|
-
})
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { duplexPair } from 'it-pair/duplex'
|
|
2
|
-
import { Multiaddr } from '@multiformats/multiaddr'
|
|
3
|
-
import type { MultiaddrConnection } from '@libp2p/interfaces/transport'
|
|
4
|
-
import type { Duplex } from 'it-stream-types'
|
|
5
|
-
|
|
6
|
-
export function createMaConnPair (): [MultiaddrConnection, MultiaddrConnection] {
|
|
7
|
-
const [local, remote] = duplexPair<Uint8Array>()
|
|
8
|
-
|
|
9
|
-
function duplexToMaConn (duplex: Duplex<Uint8Array>): MultiaddrConnection {
|
|
10
|
-
const output: MultiaddrConnection = {
|
|
11
|
-
...duplex,
|
|
12
|
-
close: async () => {},
|
|
13
|
-
remoteAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4001'),
|
|
14
|
-
timeline: {
|
|
15
|
-
open: Date.now()
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return output
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return [duplexToMaConn(local), duplexToMaConn(remote)]
|
|
23
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
2
|
-
import { handshake } from 'it-handshake'
|
|
3
|
-
import { duplexPair } from 'it-pair/duplex'
|
|
4
|
-
import { pipe } from 'it-pipe'
|
|
5
|
-
import { UnexpectedPeerError } from '@libp2p/interfaces/connection-encrypter/errors'
|
|
6
|
-
import { Multiaddr } from '@multiformats/multiaddr'
|
|
7
|
-
import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'
|
|
8
|
-
import type { Transform, Source } from 'it-stream-types'
|
|
9
|
-
|
|
10
|
-
// A basic transform that does nothing to the data
|
|
11
|
-
const transform = (): Transform<Uint8Array, Uint8Array> => {
|
|
12
|
-
return (source: Source<Uint8Array>) => (async function * () {
|
|
13
|
-
for await (const chunk of source) {
|
|
14
|
-
yield chunk
|
|
15
|
-
}
|
|
16
|
-
})()
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function mockConnectionEncrypter () {
|
|
20
|
-
const encrypter: ConnectionEncrypter = {
|
|
21
|
-
protocol: 'insecure',
|
|
22
|
-
secureInbound: async (localPeer, duplex, expectedPeer) => {
|
|
23
|
-
// 1. Perform a basic handshake.
|
|
24
|
-
const shake = handshake(duplex)
|
|
25
|
-
shake.write(localPeer.toBytes())
|
|
26
|
-
const remoteId = await shake.read()
|
|
27
|
-
|
|
28
|
-
if (remoteId == null) {
|
|
29
|
-
throw new Error('Could not read remote ID')
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const remotePeer = peerIdFromBytes(remoteId.slice())
|
|
33
|
-
shake.rest()
|
|
34
|
-
|
|
35
|
-
if (expectedPeer != null && !expectedPeer.equals(remotePeer)) {
|
|
36
|
-
throw new UnexpectedPeerError()
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// 2. Create your encryption box/unbox wrapper
|
|
40
|
-
const wrapper = duplexPair<Uint8Array>()
|
|
41
|
-
const encrypt = transform() // Use transform iterables to modify data
|
|
42
|
-
const decrypt = transform()
|
|
43
|
-
|
|
44
|
-
void pipe(
|
|
45
|
-
wrapper[0], // We write to wrapper
|
|
46
|
-
encrypt, // The data is encrypted
|
|
47
|
-
shake.stream, // It goes to the remote peer
|
|
48
|
-
decrypt, // Decrypt the incoming data
|
|
49
|
-
wrapper[0] // Pipe to the wrapper
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
conn: {
|
|
54
|
-
...wrapper[1],
|
|
55
|
-
close: async () => {},
|
|
56
|
-
localAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4001'),
|
|
57
|
-
remoteAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4002'),
|
|
58
|
-
timeline: {
|
|
59
|
-
open: Date.now()
|
|
60
|
-
},
|
|
61
|
-
conn: true
|
|
62
|
-
},
|
|
63
|
-
remotePeer,
|
|
64
|
-
remoteEarlyData: new Uint8Array(0)
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
secureOutbound: async (localPeer, duplex, remotePeer) => {
|
|
68
|
-
// 1. Perform a basic handshake.
|
|
69
|
-
const shake = handshake(duplex)
|
|
70
|
-
shake.write(localPeer.toBytes())
|
|
71
|
-
const remoteId = await shake.read()
|
|
72
|
-
|
|
73
|
-
if (remoteId == null) {
|
|
74
|
-
throw new Error('Could not read remote ID')
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
shake.rest()
|
|
78
|
-
|
|
79
|
-
// 2. Create your encryption box/unbox wrapper
|
|
80
|
-
const wrapper = duplexPair<Uint8Array>()
|
|
81
|
-
const encrypt = transform()
|
|
82
|
-
const decrypt = transform()
|
|
83
|
-
|
|
84
|
-
void pipe(
|
|
85
|
-
wrapper[0], // We write to wrapper
|
|
86
|
-
encrypt, // The data is encrypted
|
|
87
|
-
shake.stream, // It goes to the remote peer
|
|
88
|
-
decrypt, // Decrypt the incoming data
|
|
89
|
-
wrapper[0] // Pipe to the wrapper
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
return {
|
|
93
|
-
conn: {
|
|
94
|
-
...wrapper[1],
|
|
95
|
-
close: async () => {},
|
|
96
|
-
localAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4001'),
|
|
97
|
-
remoteAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4002'),
|
|
98
|
-
timeline: {
|
|
99
|
-
open: Date.now()
|
|
100
|
-
},
|
|
101
|
-
conn: true
|
|
102
|
-
},
|
|
103
|
-
remotePeer: peerIdFromBytes(remoteId.slice()),
|
|
104
|
-
remoteEarlyData: new Uint8Array(0)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return encrypter
|
|
110
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export function mockConnectionGater () {
|
|
3
|
-
return {
|
|
4
|
-
denyDialPeer: async () => await Promise.resolve(false),
|
|
5
|
-
denyDialMultiaddr: async () => await Promise.resolve(false),
|
|
6
|
-
denyInboundConnection: async () => await Promise.resolve(false),
|
|
7
|
-
denyOutboundConnection: async () => await Promise.resolve(false),
|
|
8
|
-
denyInboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
9
|
-
denyOutboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
10
|
-
denyInboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
11
|
-
denyOutboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
12
|
-
filterMultiaddrForPeer: async () => await Promise.resolve(true)
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
2
|
-
import type { Startable } from '@libp2p/interfaces/startable'
|
|
3
|
-
import type { Connection } from '@libp2p/interfaces/connection'
|
|
4
|
-
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
5
|
-
import type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interfaces/connection-manager'
|
|
6
|
-
import type { Components, Initializable } from '@libp2p/interfaces/src/components'
|
|
7
|
-
import { connectionPair } from './connection.js'
|
|
8
|
-
import errCode from 'err-code'
|
|
9
|
-
|
|
10
|
-
class MockNetwork {
|
|
11
|
-
private components: Components[] = []
|
|
12
|
-
|
|
13
|
-
addNode (components: Components): void {
|
|
14
|
-
this.components.push(components)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
getNode (peerId: PeerId): Components {
|
|
18
|
-
for (const components of this.components) {
|
|
19
|
-
if (peerId.equals(components.getPeerId())) {
|
|
20
|
-
return components
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
throw errCode(new Error('Peer not found'), 'ERR_PEER_NOT_FOUND')
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
reset () {
|
|
28
|
-
this.components = []
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const mockNetwork = new MockNetwork()
|
|
33
|
-
|
|
34
|
-
class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Initializable, Startable {
|
|
35
|
-
private connections: Connection[] = []
|
|
36
|
-
private components?: Components
|
|
37
|
-
private started = false
|
|
38
|
-
|
|
39
|
-
init (components: Components) {
|
|
40
|
-
this.components = components
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
isStarted () {
|
|
44
|
-
return this.started
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async start () {
|
|
48
|
-
this.started = true
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async stop () {
|
|
52
|
-
for (const connection of this.connections) {
|
|
53
|
-
await this.closeConnections(connection.remotePeer)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
this.started = false
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
getConnections (peerId?: PeerId): Connection[] {
|
|
60
|
-
if (peerId != null) {
|
|
61
|
-
return this.connections
|
|
62
|
-
.filter(c => c.remotePeer.toString() === peerId.toString())
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return this.connections
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async openConnection (peerId: PeerId) {
|
|
69
|
-
if (this.components == null) {
|
|
70
|
-
throw errCode(new Error('Not initialized'), 'ERR_NOT_INITIALIZED')
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const existingConnections = this.getConnections(peerId)
|
|
74
|
-
|
|
75
|
-
if (existingConnections.length > 0) {
|
|
76
|
-
return existingConnections[0]
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const componentsB = mockNetwork.getNode(peerId)
|
|
80
|
-
|
|
81
|
-
const [aToB, bToA] = connectionPair(this.components, componentsB)
|
|
82
|
-
|
|
83
|
-
// track connections
|
|
84
|
-
this.connections.push(aToB)
|
|
85
|
-
;(componentsB.getConnectionManager() as MockConnectionManager).connections.push(bToA)
|
|
86
|
-
|
|
87
|
-
this.components.getConnectionManager().dispatchEvent(new CustomEvent<Connection>('peer:connect', {
|
|
88
|
-
detail: aToB
|
|
89
|
-
}))
|
|
90
|
-
|
|
91
|
-
for (const protocol of this.components.getRegistrar().getProtocols()) {
|
|
92
|
-
for (const topology of this.components.getRegistrar().getTopologies(protocol)) {
|
|
93
|
-
topology.onConnect(componentsB.getPeerId(), aToB)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
componentsB.getConnectionManager().dispatchEvent(new CustomEvent<Connection>('peer:connect', {
|
|
98
|
-
detail: bToA
|
|
99
|
-
}))
|
|
100
|
-
|
|
101
|
-
for (const protocol of componentsB.getRegistrar().getProtocols()) {
|
|
102
|
-
for (const topology of componentsB.getRegistrar().getTopologies(protocol)) {
|
|
103
|
-
topology.onConnect(this.components.getPeerId(), bToA)
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return aToB
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async closeConnections (peerId: PeerId) {
|
|
111
|
-
if (this.components == null) {
|
|
112
|
-
throw errCode(new Error('Not initialized'), 'ERR_NOT_INITIALIZED')
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const connections = this.getConnections(peerId)
|
|
116
|
-
|
|
117
|
-
if (connections.length === 0) {
|
|
118
|
-
return
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const componentsB = mockNetwork.getNode(peerId)
|
|
122
|
-
|
|
123
|
-
for (const protocol of this.components.getRegistrar().getProtocols()) {
|
|
124
|
-
this.components.getRegistrar().getTopologies(protocol).forEach(topology => {
|
|
125
|
-
topology.onDisconnect(componentsB.getPeerId())
|
|
126
|
-
})
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
for (const conn of connections) {
|
|
130
|
-
await conn.close()
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
this.connections = this.connections.filter(c => !c.remotePeer.equals(peerId))
|
|
134
|
-
|
|
135
|
-
await componentsB.getConnectionManager().closeConnections(peerId)
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export function mockConnectionManager () {
|
|
140
|
-
return new MockConnectionManager()
|
|
141
|
-
}
|