@@ -13,15 +13,15 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
13
13
import swarmKey from '../fixtures/swarm.key.js'
14
14
import { DefaultUpgrader } from '../../src/upgrader.js'
15
15
import { codes } from '../../src/errors.js'
16
- import { mockConnectionGater , mockConnectionManager , mockMultiaddrConnPair , mockRegistrar , mockStream } from '@libp2p/interface-mocks'
16
+ import { mockConnectionGater , mockConnectionManager , mockMultiaddrConnPair , mockRegistrar , mockStream , mockMuxer } from '@libp2p/interface-mocks'
17
17
import Peers from '../fixtures/peers.js'
18
18
import type { Upgrader } from '@libp2p/interface-transport'
19
19
import type { PeerId } from '@libp2p/interface-peer-id'
20
20
import { createFromJSON } from '@libp2p/peer-id-factory'
21
21
import { plaintext } from '../../src/insecure/index.js'
22
22
import type { ConnectionEncrypter , SecuredConnection } from '@libp2p/interface-connection-encrypter'
23
23
import type { StreamMuxer , StreamMuxerFactory , StreamMuxerInit } from '@libp2p/interface-stream-muxer'
24
- import type { Stream } from '@libp2p/interface-connection'
24
+ import type { ConnectionProtector , Stream } from '@libp2p/interface-connection'
25
25
import pDefer from 'p-defer'
26
26
import { createLibp2pNode , Libp2pNode } from '../../src/libp2p.js'
27
27
import { pEvent } from 'p-event'
@@ -32,6 +32,7 @@ import { Uint8ArrayList } from 'uint8arraylist'
32
32
import { PersistentPeerStore } from '@libp2p/peer-store'
33
33
import { MemoryDatastore } from 'datastore-core'
34
34
import { DefaultComponents } from '../../src/components.js'
35
+ import { StubbedInstance , stubInterface } from 'sinon-ts'
35
36
36
37
const addrs = [
37
38
multiaddr ( '/ip4/127.0.0.1/tcp/0' ) ,
@@ -41,7 +42,12 @@ const addrs = [
41
42
describe ( 'Upgrader' , ( ) => {
42
43
let localUpgrader : Upgrader
43
44
let localMuxerFactory : StreamMuxerFactory
45
+ let localConnectionEncrypter : ConnectionEncrypter
46
+ let localConnectionProtector : StubbedInstance < ConnectionProtector >
44
47
let remoteUpgrader : Upgrader
48
+ let remoteMuxerFactory : StreamMuxerFactory
49
+ let remoteConnectionEncrypter : ConnectionEncrypter
50
+ let remoteConnectionProtector : StubbedInstance < ConnectionProtector >
45
51
let localPeer : PeerId
46
52
let remotePeer : PeerId
47
53
let localComponents : DefaultComponents
@@ -56,39 +62,50 @@ describe('Upgrader', () => {
56
62
createFromJSON ( Peers [ 1 ] )
57
63
] ) )
58
64
65
+ localConnectionProtector = stubInterface < ConnectionProtector > ( )
66
+ localConnectionProtector . protect . resolvesArg ( 0 )
67
+
59
68
localComponents = new DefaultComponents ( {
60
69
peerId : localPeer ,
61
70
connectionGater : mockConnectionGater ( ) ,
62
71
registrar : mockRegistrar ( ) ,
63
- datastore : new MemoryDatastore ( )
72
+ datastore : new MemoryDatastore ( ) ,
73
+ connectionProtector : localConnectionProtector
64
74
} )
65
75
localComponents . peerStore = new PersistentPeerStore ( localComponents )
66
76
localComponents . connectionManager = mockConnectionManager ( localComponents )
67
77
localMuxerFactory = mplex ( ) ( )
78
+ localConnectionEncrypter = plaintext ( ) ( )
68
79
localUpgrader = new DefaultUpgrader ( localComponents , {
69
80
connectionEncryption : [
70
- plaintext ( ) ( )
81
+ localConnectionEncrypter
71
82
] ,
72
83
muxers : [
73
84
localMuxerFactory
74
85
] ,
75
86
inboundUpgradeTimeout : 1000
76
87
} )
77
88
89
+ remoteConnectionProtector = stubInterface < ConnectionProtector > ( )
90
+ remoteConnectionProtector . protect . resolvesArg ( 0 )
91
+
78
92
remoteComponents = new DefaultComponents ( {
79
93
peerId : remotePeer ,
80
94
connectionGater : mockConnectionGater ( ) ,
81
95
registrar : mockRegistrar ( ) ,
82
- datastore : new MemoryDatastore ( )
96
+ datastore : new MemoryDatastore ( ) ,
97
+ connectionProtector : remoteConnectionProtector
83
98
} )
84
99
remoteComponents . peerStore = new PersistentPeerStore ( remoteComponents )
85
100
remoteComponents . connectionManager = mockConnectionManager ( remoteComponents )
101
+ remoteMuxerFactory = mplex ( ) ( )
102
+ remoteConnectionEncrypter = plaintext ( ) ( )
86
103
remoteUpgrader = new DefaultUpgrader ( remoteComponents , {
87
104
connectionEncryption : [
88
- plaintext ( ) ( )
105
+ remoteConnectionEncrypter
89
106
] ,
90
107
muxers : [
91
- mplex ( ) ( )
108
+ remoteMuxerFactory
92
109
] ,
93
110
inboundUpgradeTimeout : 1000
94
111
} )
@@ -451,6 +468,70 @@ describe('Upgrader', () => {
451
468
expect ( connections [ 0 ] . streams ) . to . have . lengthOf ( 0 )
452
469
expect ( connections [ 1 ] . streams ) . to . have . lengthOf ( 0 )
453
470
} )
471
+
472
+ it ( 'should allow skipping encryption, protection and muxing' , async ( ) => {
473
+ const localStreamMuxerFactorySpy = sinon . spy ( localMuxerFactory , 'createStreamMuxer' )
474
+ const localMuxerFactoryOverride = mockMuxer ( )
475
+ const localStreamMuxerFactoryOverrideSpy = sinon . spy ( localMuxerFactoryOverride , 'createStreamMuxer' )
476
+ const localConnectionEncrypterSpy = sinon . spy ( localConnectionEncrypter , 'secureOutbound' )
477
+
478
+ const remoteStreamMuxerFactorySpy = sinon . spy ( remoteMuxerFactory , 'createStreamMuxer' )
479
+ const remoteMuxerFactoryOverride = mockMuxer ( )
480
+ const remoteStreamMuxerFactoryOverrideSpy = sinon . spy ( remoteMuxerFactoryOverride , 'createStreamMuxer' )
481
+ const remoteConnectionEncrypterSpy = sinon . spy ( remoteConnectionEncrypter , 'secureInbound' )
482
+
483
+ const { inbound, outbound } = mockMultiaddrConnPair ( {
484
+ addrs : [
485
+ multiaddr ( '/ip4/127.0.0.1/tcp/0' ) . encapsulate ( `/p2p/${ remotePeer . toString ( ) } ` ) ,
486
+ multiaddr ( '/ip4/127.0.0.1/tcp/0' )
487
+ ] ,
488
+ remotePeer
489
+ } )
490
+
491
+ const connections = await Promise . all ( [
492
+ localUpgrader . upgradeOutbound ( outbound , {
493
+ skipEncryption : true ,
494
+ skipProtection : true ,
495
+ muxerFactory : localMuxerFactoryOverride
496
+ } ) ,
497
+ remoteUpgrader . upgradeInbound ( inbound , {
498
+ skipEncryption : true ,
499
+ skipProtection : true ,
500
+ muxerFactory : remoteMuxerFactoryOverride
501
+ } )
502
+ ] )
503
+
504
+ expect ( connections ) . to . have . length ( 2 )
505
+
506
+ const stream = await connections [ 0 ] . newStream ( '/echo/1.0.0' )
507
+ expect ( stream ) . to . have . nested . property ( 'stat.protocol' , '/echo/1.0.0' )
508
+
509
+ const hello = uint8ArrayFromString ( 'hello there!' )
510
+ const result = await pipe (
511
+ [ hello ] ,
512
+ stream ,
513
+ function toBuffer ( source ) {
514
+ return ( async function * ( ) {
515
+ for await ( const val of source ) yield val . slice ( )
516
+ } ) ( )
517
+ } ,
518
+ async ( source ) => await all ( source )
519
+ )
520
+
521
+ expect ( result ) . to . eql ( [ hello ] )
522
+
523
+ expect ( localStreamMuxerFactorySpy . callCount ) . to . equal ( 0 , 'did not use passed stream muxer factory' )
524
+ expect ( localStreamMuxerFactoryOverrideSpy . callCount ) . to . equal ( 1 , 'did not use passed stream muxer factory' )
525
+
526
+ expect ( remoteStreamMuxerFactorySpy . callCount ) . to . equal ( 0 , 'did not use passed stream muxer factory' )
527
+ expect ( remoteStreamMuxerFactoryOverrideSpy . callCount ) . to . equal ( 1 , 'did not use passed stream muxer factory' )
528
+
529
+ expect ( localConnectionEncrypterSpy . callCount ) . to . equal ( 0 , 'used local connection encrypter' )
530
+ expect ( remoteConnectionEncrypterSpy . callCount ) . to . equal ( 0 , 'used remote connection encrypter' )
531
+
532
+ expect ( localConnectionProtector . protect . callCount ) . to . equal ( 0 , 'used local connection protector' )
533
+ expect ( remoteConnectionProtector . protect . callCount ) . to . equal ( 0 , 'used remote connection protector' )
534
+ } )
454
535
} )
455
536
456
537
describe ( 'libp2p.upgrader' , ( ) => {
0 commit comments