@libp2p/interface-compliance-tests 0.0.0 → 1.0.2

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.
Files changed (177) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +25 -0
  3. package/dist/src/connection/connection.d.ts +5 -0
  4. package/dist/src/connection/connection.d.ts.map +1 -0
  5. package/dist/src/connection/connection.js +148 -0
  6. package/dist/src/connection/connection.js.map +1 -0
  7. package/dist/src/connection/index.d.ts +5 -0
  8. package/dist/src/connection/index.d.ts.map +1 -0
  9. package/dist/src/connection/index.js +5 -0
  10. package/dist/src/connection/index.js.map +1 -0
  11. package/dist/src/crypto/index.d.ts +5 -0
  12. package/dist/src/crypto/index.d.ts.map +1 -0
  13. package/dist/src/crypto/index.js +78 -0
  14. package/dist/src/crypto/index.js.map +1 -0
  15. package/dist/src/index.d.ts +5 -0
  16. package/dist/src/index.d.ts.map +1 -0
  17. package/dist/src/index.js +2 -0
  18. package/dist/src/index.js.map +1 -0
  19. package/dist/src/peer-discovery/index.d.ts +6 -0
  20. package/dist/src/peer-discovery/index.d.ts.map +1 -0
  21. package/dist/src/peer-discovery/index.js +63 -0
  22. package/dist/src/peer-discovery/index.js.map +1 -0
  23. package/dist/src/pubsub/api.d.ts +6 -0
  24. package/dist/src/pubsub/api.d.ts.map +1 -0
  25. package/dist/src/pubsub/api.js +65 -0
  26. package/dist/src/pubsub/api.js.map +1 -0
  27. package/dist/src/pubsub/connection-handlers.d.ts +6 -0
  28. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -0
  29. package/dist/src/pubsub/connection-handlers.js +279 -0
  30. package/dist/src/pubsub/connection-handlers.js.map +1 -0
  31. package/dist/src/pubsub/emit-self.d.ts +6 -0
  32. package/dist/src/pubsub/emit-self.d.ts.map +1 -0
  33. package/dist/src/pubsub/emit-self.js +51 -0
  34. package/dist/src/pubsub/emit-self.js.map +1 -0
  35. package/dist/src/pubsub/index.d.ts +6 -0
  36. package/dist/src/pubsub/index.d.ts.map +1 -0
  37. package/dist/src/pubsub/index.js +17 -0
  38. package/dist/src/pubsub/index.js.map +1 -0
  39. package/dist/src/pubsub/messages.d.ts +6 -0
  40. package/dist/src/pubsub/messages.d.ts.map +1 -0
  41. package/dist/src/pubsub/messages.js +93 -0
  42. package/dist/src/pubsub/messages.js.map +1 -0
  43. package/dist/src/pubsub/multiple-nodes.d.ts +6 -0
  44. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -0
  45. package/dist/src/pubsub/multiple-nodes.js +283 -0
  46. package/dist/src/pubsub/multiple-nodes.js.map +1 -0
  47. package/dist/src/pubsub/two-nodes.d.ts +6 -0
  48. package/dist/src/pubsub/two-nodes.d.ts.map +1 -0
  49. package/dist/src/pubsub/two-nodes.js +127 -0
  50. package/dist/src/pubsub/two-nodes.js.map +1 -0
  51. package/dist/src/pubsub/utils.d.ts +3 -0
  52. package/dist/src/pubsub/utils.d.ts.map +1 -0
  53. package/dist/src/pubsub/utils.js +11 -0
  54. package/dist/src/pubsub/utils.js.map +1 -0
  55. package/dist/src/record/index.d.ts +5 -0
  56. package/dist/src/record/index.d.ts.map +1 -0
  57. package/dist/src/record/index.js +25 -0
  58. package/dist/src/record/index.js.map +1 -0
  59. package/dist/src/stream-muxer/base-test.d.ts +5 -0
  60. package/dist/src/stream-muxer/base-test.d.ts.map +1 -0
  61. package/dist/src/stream-muxer/base-test.js +123 -0
  62. package/dist/src/stream-muxer/base-test.js.map +1 -0
  63. package/dist/src/stream-muxer/close-test.d.ts +5 -0
  64. package/dist/src/stream-muxer/close-test.d.ts.map +1 -0
  65. package/dist/src/stream-muxer/close-test.js +103 -0
  66. package/dist/src/stream-muxer/close-test.js.map +1 -0
  67. package/dist/src/stream-muxer/index.d.ts +5 -0
  68. package/dist/src/stream-muxer/index.d.ts.map +1 -0
  69. package/dist/src/stream-muxer/index.js +13 -0
  70. package/dist/src/stream-muxer/index.js.map +1 -0
  71. package/dist/src/stream-muxer/mega-stress-test.d.ts +5 -0
  72. package/dist/src/stream-muxer/mega-stress-test.d.ts.map +1 -0
  73. package/dist/src/stream-muxer/mega-stress-test.js +8 -0
  74. package/dist/src/stream-muxer/mega-stress-test.js.map +1 -0
  75. package/dist/src/stream-muxer/spawner.d.ts +4 -0
  76. package/dist/src/stream-muxer/spawner.d.ts.map +1 -0
  77. package/dist/src/stream-muxer/spawner.js +32 -0
  78. package/dist/src/stream-muxer/spawner.js.map +1 -0
  79. package/dist/src/stream-muxer/stress-test.d.ts +5 -0
  80. package/dist/src/stream-muxer/stress-test.d.ts.map +1 -0
  81. package/dist/src/stream-muxer/stress-test.js +21 -0
  82. package/dist/src/stream-muxer/stress-test.js.map +1 -0
  83. package/dist/src/topology/multicodec-topology.d.ts +5 -0
  84. package/dist/src/topology/multicodec-topology.d.ts.map +1 -0
  85. package/dist/src/topology/multicodec-topology.js +109 -0
  86. package/dist/src/topology/multicodec-topology.js.map +1 -0
  87. package/dist/src/topology/topology.d.ts +5 -0
  88. package/dist/src/topology/topology.d.ts.map +1 -0
  89. package/dist/src/topology/topology.js +29 -0
  90. package/dist/src/topology/topology.js.map +1 -0
  91. package/dist/src/transport/dial-test.d.ts +5 -0
  92. package/dist/src/transport/dial-test.d.ts.map +1 -0
  93. package/dist/src/transport/dial-test.js +78 -0
  94. package/dist/src/transport/dial-test.js.map +1 -0
  95. package/dist/src/transport/filter-test.d.ts +5 -0
  96. package/dist/src/transport/filter-test.d.ts.map +1 -0
  97. package/dist/src/transport/filter-test.js +19 -0
  98. package/dist/src/transport/filter-test.js.map +1 -0
  99. package/dist/src/transport/index.d.ts +18 -0
  100. package/dist/src/transport/index.d.ts.map +1 -0
  101. package/dist/src/transport/index.js +11 -0
  102. package/dist/src/transport/index.js.map +1 -0
  103. package/dist/src/transport/listen-test.d.ts +5 -0
  104. package/dist/src/transport/listen-test.d.ts.map +1 -0
  105. package/dist/src/transport/listen-test.js +116 -0
  106. package/dist/src/transport/listen-test.js.map +1 -0
  107. package/dist/src/transport/utils/index.d.ts +8 -0
  108. package/dist/src/transport/utils/index.d.ts.map +1 -0
  109. package/dist/src/transport/utils/index.js +104 -0
  110. package/dist/src/transport/utils/index.js.map +1 -0
  111. package/dist/src/utils/peers.d.ts +7 -0
  112. package/dist/src/utils/peers.d.ts.map +1 -0
  113. package/dist/src/utils/peers.js +26 -0
  114. package/dist/src/utils/peers.js.map +1 -0
  115. package/dist/test/connection/index.spec.d.ts +2 -0
  116. package/dist/test/connection/index.spec.d.ts.map +1 -0
  117. package/dist/test/connection/index.spec.js +71 -0
  118. package/dist/test/connection/index.spec.js.map +1 -0
  119. package/dist/test/crypto/index.spec.d.ts +2 -0
  120. package/dist/test/crypto/index.spec.d.ts.map +1 -0
  121. package/dist/test/crypto/index.spec.js +11 -0
  122. package/dist/test/crypto/index.spec.js.map +1 -0
  123. package/dist/test/crypto/mock-crypto.d.ts +4 -0
  124. package/dist/test/crypto/mock-crypto.d.ts.map +1 -0
  125. package/dist/test/crypto/mock-crypto.js +93 -0
  126. package/dist/test/crypto/mock-crypto.js.map +1 -0
  127. package/dist/test/peer-discovery/index.spec.d.ts +2 -0
  128. package/dist/test/peer-discovery/index.spec.d.ts.map +1 -0
  129. package/dist/test/peer-discovery/index.spec.js +18 -0
  130. package/dist/test/peer-discovery/index.spec.js.map +1 -0
  131. package/dist/test/peer-discovery/mock-discovery.d.ts +20 -0
  132. package/dist/test/peer-discovery/mock-discovery.d.ts.map +1 -0
  133. package/dist/test/peer-discovery/mock-discovery.js +39 -0
  134. package/dist/test/peer-discovery/mock-discovery.js.map +1 -0
  135. package/dist/test/topology/mock-peer-store.d.ts +12 -0
  136. package/dist/test/topology/mock-peer-store.d.ts.map +1 -0
  137. package/dist/test/topology/mock-peer-store.js +18 -0
  138. package/dist/test/topology/mock-peer-store.js.map +1 -0
  139. package/dist/test/topology/multicodec-topology.spec.d.ts +2 -0
  140. package/dist/test/topology/multicodec-topology.spec.d.ts.map +1 -0
  141. package/dist/test/topology/multicodec-topology.spec.js +45 -0
  142. package/dist/test/topology/multicodec-topology.spec.js.map +1 -0
  143. package/dist/test/topology/topology.spec.d.ts +2 -0
  144. package/dist/test/topology/topology.spec.d.ts.map +1 -0
  145. package/dist/test/topology/topology.spec.js +21 -0
  146. package/dist/test/topology/topology.spec.js.map +1 -0
  147. package/dist/tsconfig.tsbuildinfo +1 -0
  148. package/package.json +203 -4
  149. package/src/connection/README.md +256 -0
  150. package/src/connection/connection.ts +178 -0
  151. package/src/connection/index.ts +7 -0
  152. package/src/crypto/index.ts +104 -0
  153. package/src/index.ts +5 -0
  154. package/src/peer-discovery/index.ts +87 -0
  155. package/src/pubsub/api.ts +89 -0
  156. package/src/pubsub/connection-handlers.ts +356 -0
  157. package/src/pubsub/emit-self.ts +67 -0
  158. package/src/pubsub/index.ts +20 -0
  159. package/src/pubsub/messages.ts +111 -0
  160. package/src/pubsub/multiple-nodes.ts +353 -0
  161. package/src/pubsub/two-nodes.ts +175 -0
  162. package/src/pubsub/utils.ts +13 -0
  163. package/src/record/index.ts +32 -0
  164. package/src/stream-muxer/base-test.ts +155 -0
  165. package/src/stream-muxer/close-test.ts +124 -0
  166. package/src/stream-muxer/index.ts +15 -0
  167. package/src/stream-muxer/mega-stress-test.ts +11 -0
  168. package/src/stream-muxer/spawner.ts +52 -0
  169. package/src/stream-muxer/stress-test.ts +24 -0
  170. package/src/topology/multicodec-topology.ts +136 -0
  171. package/src/topology/topology.ts +38 -0
  172. package/src/transport/dial-test.ts +98 -0
  173. package/src/transport/filter-test.ts +26 -0
  174. package/src/transport/index.ts +29 -0
  175. package/src/transport/listen-test.ts +152 -0
  176. package/src/transport/utils/index.ts +123 -0
  177. package/src/utils/peers.ts +25 -0
@@ -0,0 +1,256 @@
1
+ interface-connection
2
+ ==================
3
+
4
+ This is a test suite and interface you can use to implement a connection. The connection interface contains all the metadata associated with it, as well as an array of the streams opened through this connection. In the same way as the connection, a stream contains properties with its metadata, plus an iterable duplex object that offers a mechanism for writing and reading data, with back pressure. This module and test suite were heavily inspired by abstract-blob-store and interface-stream-muxer.
5
+
6
+ The primary goal of this module is to enable developers to pick, swap or upgrade their connection without losing the same API expectations and mechanisms such as back pressure and the ability to half close a connection.
7
+
8
+ Publishing a test suite as a module lets multiple modules ensure compatibility since they use the same test suite.
9
+
10
+ ## Usage
11
+
12
+ ### Connection
13
+
14
+ Before creating a connection from a transport compatible with `libp2p` it is important to understand some concepts:
15
+
16
+ - **socket**: the underlying raw duplex connection between two nodes. It is created by the transports during a dial/listen.
17
+ - **[multiaddr connection](https://github.com/libp2p/interface-transport#multiaddrconnection)**: an abstraction over the socket to allow it to work with multiaddr addresses. It is a duplex connection that transports create to wrap the socket before passing to an upgrader that turns it into a standard connection (see below).
18
+ - **connection**: a connection between two _peers_ that has built in multiplexing and info about the connected peer. It is created from a [multiaddr connection](https://github.com/libp2p/interface-transport#multiaddrconnection) by an upgrader. The upgrader uses multistream-select to add secio and multiplexing and returns this object.
19
+ - **stream**: a muxed duplex channel of the `connection`. Each connection may have many streams.
20
+
21
+ A connection stands for the libp2p communication duplex layer between two nodes. It is **not** the underlying raw transport duplex layer (socket), such as a TCP socket, but an abstracted layer that sits on top of the raw socket.
22
+
23
+ This helps ensuring that the transport is responsible for socket management, while also allowing the application layer to handle the connection management.
24
+
25
+ ### Test suite
26
+
27
+ ```js
28
+ const tests = require('libp2p-interfaces-compliance-tests/connection')
29
+ describe('your connection', () => {
30
+ tests({
31
+ // Options should be passed to your connection
32
+ async setup (options) {
33
+ return YourConnection
34
+ },
35
+ async teardown () {
36
+ // cleanup resources created by setup()
37
+ }
38
+ })
39
+ })
40
+ ```
41
+
42
+ ## API
43
+
44
+ ### Connection
45
+
46
+ A valid connection (one that follows this abstraction), must implement the following API:
47
+
48
+ - type: `Connection`
49
+ ```js
50
+ new Connection({
51
+ localAddr,
52
+ remoteAddr,
53
+ localPeer,
54
+ remotePeer,
55
+ newStream,
56
+ close,
57
+ getStreams,
58
+ stat: {
59
+ direction,
60
+ timeline: {
61
+ open,
62
+ upgraded
63
+ },
64
+ multiplexer,
65
+ encryption
66
+ }
67
+ })
68
+ ```
69
+ - `<Multiaddr> conn.localAddr`
70
+ - `<Multiaddr> conn.remoteAddr`
71
+ - `<PeerId> conn.localPeer`
72
+ - `<PeerId> conn.remotePeer`
73
+ - `<Object> conn.stat`
74
+ - `<Map> conn.registry`
75
+ - `Array<Stream> conn.streams`
76
+ - `Promise<object> conn.newStream(Array<protocols>)`
77
+ - `<void> conn.removeStream(id)`
78
+ - `<Stream> conn.addStream(stream, protocol, metadata)`
79
+ - `Promise<> conn.close()`
80
+
81
+ It can be obtained as follows:
82
+
83
+ ```js
84
+ const { Connection } = require('interface-connection')
85
+
86
+ const conn = new Connection({
87
+ localAddr: maConn.localAddr,
88
+ remoteAddr: maConn.remoteAddr,
89
+ localPeer: this._peerId,
90
+ remotePeer,
91
+ newStream,
92
+ close: err => maConn.close(err),
93
+ getStreams,
94
+ stats: {
95
+ direction: 'outbound',
96
+ timeline: {
97
+ open: maConn.timeline.open,
98
+ upgraded: Date.now()
99
+ },
100
+ multiplexer,
101
+ encryption
102
+ }
103
+ })
104
+ ```
105
+
106
+ #### Creating a connection instance
107
+
108
+ - `JavaScript` - `const conn = new Connection({localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, direction, multiplexer, encryption})`
109
+
110
+ Creates a new Connection instance.
111
+
112
+ `localAddr` is the optional [multiaddr](https://github.com/multiformats/multiaddr) address used by the local peer to reach the remote.
113
+ `remoteAddr` is the optional [multiaddr](https://github.com/multiformats/multiaddr) address used to communicate with the remote peer.
114
+ `localPeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the local peer.
115
+ `remotePeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the remote peer.
116
+ `newStream` is the `function` responsible for getting a new muxed+multistream-selected stream.
117
+ `close` is the `function` responsible for closing the raw connection.
118
+ `getStreams` is the `function` responsible for getting the streams muxed within the connection.
119
+ `stats` is an `object` with the metadata of the connection. It contains:
120
+ - `direction` is a `string` indicating whether the connection is `inbound` or `outbound`.
121
+ - `timeline` is an `object` with the relevant events timestamps of the connection (`open`, `upgraded` and `closed`; the `closed` will be added when the connection is closed).
122
+ - `multiplexer` is a `string` with the connection multiplexing codec (optional).
123
+ - `encryption` is a `string` with the connection encryption method identifier (optional).
124
+ - `status` is a `string` indicating the overall status of the connection. It is one of [`'open'`, `'closing'`, `'closed'`]
125
+
126
+ #### Create a new stream
127
+
128
+ - `JavaScript` - `conn.newStream(protocols)`
129
+
130
+ Create a new stream within the connection.
131
+
132
+ `protocols` is an array of the intended protocol to use (by order of preference). Example: `[/echo/1.0.0]`
133
+
134
+ It returns a `Promise` with an object with the following properties:
135
+
136
+ ```js
137
+ {
138
+ stream,
139
+ protocol
140
+ }
141
+ ```
142
+
143
+ The stream property contains the muxed stream, while the protocol contains the protocol codec used by the stream.
144
+
145
+ #### Add stream metadata
146
+
147
+ - `JavaScript` - `conn.addStream(stream, { protocol, ...metadata })`
148
+
149
+ Add a new stream to the connection registry.
150
+
151
+ `stream` is a muxed stream.
152
+ `protocol` is the string codec for the protocol used by the stream. Example: `/echo/1.0.0`
153
+ `metadata` is an object containing any additional, optional, stream metadata that you wish to track (such as its `tags`).
154
+
155
+ #### Remove a from the registry
156
+
157
+ - `JavaScript` - `conn.removeStream(id)`
158
+
159
+ Removes the stream with the given id from the connection registry.
160
+
161
+ `id` is the unique id of the stream for this connection.
162
+
163
+
164
+ #### Close connection
165
+
166
+ - `JavaScript` - `conn.close()`
167
+
168
+ This method closes the connection to the remote peer, as well as all the streams muxed within the connection.
169
+
170
+ It returns a `Promise`.
171
+
172
+ #### Connection identifier
173
+
174
+ - `JavaScript` - `conn.id`
175
+
176
+ This property contains the identifier of the connection.
177
+
178
+ #### Connection streams registry
179
+
180
+ - `JavaScript` - `conn.registry`
181
+
182
+ This property contains a map with the muxed streams indexed by their id. This registry contains the protocol used by the stream, as well as its metadata.
183
+
184
+ #### Remote peer
185
+
186
+ - `JavaScript` - `conn.remotePeer`
187
+
188
+ This property contains the remote `peer-id` of this connection.
189
+
190
+ #### Local peer
191
+
192
+ - `JavaScript` - `conn.localPeer`
193
+
194
+ This property contains the local `peer-id` of this connection.
195
+
196
+ #### Get the connection Streams
197
+
198
+ - `JavaScript` - `conn.streams`
199
+
200
+ This getter returns all the muxed streams within the connection.
201
+
202
+ It returns an `Array`.
203
+
204
+ #### Remote address
205
+
206
+ - `JavaScript` - `conn.remoteAddr`
207
+
208
+ This getter returns the `remote` [multiaddr](https://github.com/multiformats/multiaddr) address.
209
+
210
+ #### Local address
211
+
212
+ - `JavaScript` - `conn.localAddr`
213
+
214
+ This getter returns the `local` [multiaddr](https://github.com/multiformats/multiaddr) address.
215
+
216
+ #### Stat
217
+
218
+ - `JavaScript` - `conn.stat`
219
+
220
+ This getter returns an `Object` with the metadata of the connection, as follows:
221
+
222
+ - `status`:
223
+
224
+ This property contains the status of the connection. It can be either `open`, `closing` or `closed`. Once the connection is created it is in an `open` status. When a `conn.close()` happens, the status will change to `closing` and finally, after all the connection streams are properly closed, the status will be `closed`. These values can also be directly referenced by importing the `status` file:
225
+
226
+ ```js
227
+ const {
228
+ OPEN, CLOSING, CLOSED
229
+ } = require('libp2p-interfaces/src/connection/status')
230
+
231
+ if (connection.stat.status === OPEN) {
232
+ // ...
233
+ }
234
+ ```
235
+
236
+ - `timeline`:
237
+
238
+ This property contains an object with the `open`, `upgraded` and `close` timestamps of the connection. Note that, the `close` timestamp is `undefined` until the connection is closed.
239
+
240
+ - `direction`:
241
+
242
+ This property contains the direction of the peer in the connection. It can be `inbound` or `outbound`.
243
+
244
+ - `multiplexer`:
245
+
246
+ This property contains the `multiplexing` codec being used in the connection.
247
+
248
+ - `encryption`:
249
+
250
+ This property contains the encryption method being used in the connection. It is `undefined` if the connection is not encrypted.
251
+
252
+ #### Tags
253
+
254
+ - `JavaScript` - `conn.tags`
255
+
256
+ This property contains an array of tags associated with the connection. New tags can be pushed to this array during the connection's lifetime.
@@ -0,0 +1,178 @@
1
+ import { expect } from 'aegir/utils/chai.js'
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.localPeer).to.exist()
23
+ expect(connection.remotePeer).to.exist()
24
+ expect(connection.localAddr).to.exist()
25
+ expect(connection.remoteAddr).to.exist()
26
+ expect(connection.stat.status).to.equal('OPEN')
27
+ expect(connection.stat.timeline.open).to.exist()
28
+ expect(connection.stat.timeline.upgraded).to.exist()
29
+ expect(connection.stat.timeline.close).to.not.exist()
30
+ expect(connection.stat.direction).to.exist()
31
+ expect(connection.streams).to.eql([])
32
+ expect(connection.tags).to.eql([])
33
+ })
34
+
35
+ it('should get the metadata of an open connection', () => {
36
+ const stat = connection.stat
37
+
38
+ expect(stat.status).to.equal('OPEN')
39
+ expect(stat.direction).to.exist()
40
+ expect(stat.timeline.open).to.exist()
41
+ expect(stat.timeline.upgraded).to.exist()
42
+ expect(stat.timeline.close).to.not.exist()
43
+ })
44
+
45
+ it('should return an empty array of streams', () => {
46
+ const streams = connection.streams
47
+
48
+ expect(streams).to.eql([])
49
+ })
50
+
51
+ it('should be able to create a new stream', async () => {
52
+ const protocolToUse = '/echo/0.0.1'
53
+ const { stream, protocol } = await connection.newStream([protocolToUse])
54
+
55
+ expect(protocol).to.equal(protocolToUse)
56
+
57
+ const connStreams = await connection.streams
58
+
59
+ expect(stream).to.exist()
60
+ expect(connStreams).to.exist()
61
+ expect(connStreams).to.have.lengthOf(1)
62
+ expect(connStreams[0]).to.equal(stream)
63
+ })
64
+ })
65
+
66
+ describe('close connection', () => {
67
+ let connection: Connection
68
+ let timelineProxy
69
+ const proxyHandler = {
70
+ set () {
71
+ // @ts-expect-error - TS fails to infer here
72
+ return Reflect.set(...arguments)
73
+ }
74
+ }
75
+
76
+ beforeEach(async () => {
77
+ timelineProxy = new Proxy({
78
+ open: Date.now() - 10,
79
+ upgraded: Date.now()
80
+ }, proxyHandler)
81
+
82
+ connection = await test.setup({
83
+ stat: {
84
+ timeline: timelineProxy,
85
+ direction: 'outbound',
86
+ encryption: '/crypto/1.0.0',
87
+ multiplexer: '/muxer/1.0.0'
88
+ }
89
+ })
90
+ })
91
+
92
+ afterEach(async () => {
93
+ await test.teardown()
94
+ })
95
+
96
+ it('should be able to close the connection after being created', async () => {
97
+ expect(connection.stat.timeline.close).to.not.exist()
98
+ await connection.close()
99
+
100
+ expect(connection.stat.timeline.close).to.exist()
101
+ expect(connection.stat.status).to.equal('CLOSED')
102
+ })
103
+
104
+ it('should be able to close the connection after opening a stream', async () => {
105
+ // Open stream
106
+ const protocol = '/echo/0.0.1'
107
+ await connection.newStream([protocol])
108
+
109
+ // Close connection
110
+ expect(connection.stat.timeline.close).to.not.exist()
111
+ await connection.close()
112
+
113
+ expect(connection.stat.timeline.close).to.exist()
114
+ expect(connection.stat.status).to.equal('CLOSED')
115
+ })
116
+
117
+ it('should properly track streams', async () => {
118
+ // Open stream
119
+ const protocol = '/echo/0.0.1'
120
+ const { stream } = await connection.newStream([protocol])
121
+ const trackedStream = connection.registry.get(stream.id)
122
+ expect(trackedStream).to.have.property('protocol', protocol)
123
+
124
+ // Close stream
125
+ await stream.close()
126
+
127
+ expect(connection.registry.get(stream.id)).to.not.exist()
128
+ })
129
+
130
+ it('should support a proxy on the timeline', async () => {
131
+ sinon.spy(proxyHandler, 'set')
132
+ expect(connection.stat.timeline.close).to.not.exist()
133
+
134
+ await connection.close()
135
+ // @ts-expect-error - fails to infer callCount
136
+ expect(proxyHandler.set.callCount).to.equal(1)
137
+ // @ts-expect-error - fails to infer getCall
138
+ const [obj, key, value] = proxyHandler.set.getCall(0).args
139
+ expect(obj).to.eql(connection.stat.timeline)
140
+ expect(key).to.equal('close')
141
+ expect(value).to.be.a('number').that.equals(connection.stat.timeline.close)
142
+ })
143
+
144
+ it('should fail to create a new stream if the connection is closing', async () => {
145
+ expect(connection.stat.timeline.close).to.not.exist()
146
+ const p = connection.close()
147
+
148
+ try {
149
+ const protocol = '/echo/0.0.1'
150
+ await connection.newStream([protocol])
151
+ } catch (err: any) {
152
+ expect(err).to.exist()
153
+ return
154
+ } finally {
155
+ await p
156
+ }
157
+
158
+ throw new Error('should fail to create a new stream if the connection is closing')
159
+ })
160
+
161
+ it('should fail to create a new stream if the connection is closed', async () => {
162
+ expect(connection.stat.timeline.close).to.not.exist()
163
+ await connection.close()
164
+
165
+ try {
166
+ const protocol = '/echo/0.0.1'
167
+ await connection.newStream([protocol])
168
+ } catch (err: any) {
169
+ expect(err).to.exist()
170
+ expect(err.code).to.equal('ERR_CONNECTION_CLOSED')
171
+ return
172
+ }
173
+
174
+ throw new Error('should fail to create a new stream if the connection is closing')
175
+ })
176
+ })
177
+ })
178
+ }
@@ -0,0 +1,7 @@
1
+ import connectionSuite from './connection.js'
2
+ import type { TestSetup } from '../index.js'
3
+ import type { Connection } from '@libp2p/interfaces/connection'
4
+
5
+ export default (test: TestSetup<Connection>) => {
6
+ connectionSuite(test)
7
+ }
@@ -0,0 +1,104 @@
1
+ import { expect } from 'aegir/utils/chai.js'
2
+ // @ts-expect-error no types
3
+ import duplexPair from 'it-pair/duplex.js'
4
+ import { pipe } from 'it-pipe'
5
+ import * as PeerIdFactory from '@libp2p/peer-id-factory'
6
+ import { collect } from 'streaming-iterables'
7
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
+ import peers from '../utils/peers.js'
9
+ import { UnexpectedPeerError } from '@libp2p/interfaces/crypto/errors'
10
+ import type { TestSetup } from '../index.js'
11
+ import type { Crypto } from '@libp2p/interfaces/crypto'
12
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
13
+
14
+ export default (common: TestSetup<Crypto>) => {
15
+ describe('interface-crypto compliance tests', () => {
16
+ let crypto: Crypto
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] = duplexPair()
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
+ 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: AsyncIterable<Uint8Array>) => (async function * toBuffer () {
65
+ for await (const chunk of source) {
66
+ yield chunk.slice()
67
+ }
68
+ })(),
69
+ collect
70
+ )
71
+
72
+ expect(result).to.eql([input])
73
+ })
74
+
75
+ it('should return the remote peer id', async () => {
76
+ const [localConn, remoteConn] = duplexPair()
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] = duplexPair()
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
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+
2
+ export interface TestSetup<T, SetupArgs = {}> {
3
+ setup: (args?: SetupArgs) => Promise<T>
4
+ teardown: () => Promise<void>
5
+ }
@@ -0,0 +1,87 @@
1
+ import { expect } from 'aegir/utils/chai.js'
2
+ import { Multiaddr } from '@multiformats/multiaddr'
3
+ import delay from 'delay'
4
+ import pDefer from 'p-defer'
5
+ import type { TestSetup } from '../index.js'
6
+ import type { PeerDiscovery } from '@libp2p/interfaces/peer-discovery'
7
+ import type { Startable } from '@libp2p/interfaces'
8
+
9
+ export default (common: TestSetup<PeerDiscovery & Startable>) => {
10
+ describe('interface-peer-discovery compliance tests', () => {
11
+ let discovery: PeerDiscovery & Startable
12
+
13
+ beforeEach(async () => {
14
+ discovery = await common.setup()
15
+ })
16
+
17
+ afterEach('ensure discovery was stopped', async () => {
18
+ await discovery.stop()
19
+
20
+ discovery.removeAllListeners()
21
+
22
+ await common.teardown()
23
+ })
24
+
25
+ it('can start the service', async () => {
26
+ await discovery.start()
27
+ })
28
+
29
+ it('can start and stop the service', async () => {
30
+ await discovery.start()
31
+ await discovery.stop()
32
+ })
33
+
34
+ it('should not fail to stop the service if it was not started', async () => {
35
+ await discovery.stop()
36
+ })
37
+
38
+ it('should not fail to start the service if it is already started', async () => {
39
+ await discovery.start()
40
+ await discovery.start()
41
+ })
42
+
43
+ it('should emit a peer event after start', async () => {
44
+ const defer = pDefer()
45
+ await discovery.start()
46
+
47
+ discovery.on('peer', ({ id, multiaddrs }) => {
48
+ expect(id).to.exist()
49
+ expect(id).to.have.property('type').that.is.oneOf(['RSA', 'Ed25519', 'secp256k1'])
50
+ expect(multiaddrs).to.exist()
51
+
52
+ multiaddrs.forEach((m) => expect(Multiaddr.isMultiaddr(m)).to.eql(true))
53
+
54
+ defer.resolve()
55
+ })
56
+
57
+ await defer.promise
58
+ })
59
+
60
+ it('should not receive a peer event before start', async () => {
61
+ discovery.on('peer', () => {
62
+ throw new Error('should not receive a peer event before start')
63
+ })
64
+
65
+ await delay(2000)
66
+ })
67
+
68
+ it('should not receive a peer event after stop', async () => {
69
+ const deferStart = pDefer()
70
+
71
+ await discovery.start()
72
+
73
+ discovery.on('peer', () => {
74
+ deferStart.resolve()
75
+ })
76
+
77
+ await deferStart.promise
78
+ await discovery.stop()
79
+
80
+ discovery.on('peer', () => {
81
+ throw new Error('should not receive a peer event after stop')
82
+ })
83
+
84
+ await delay(2000)
85
+ })
86
+ })
87
+ }