|
| 1 | +import { memory } from '@libp2p/memory' |
| 2 | +import { mplex } from '@libp2p/mplex' |
| 3 | +import { plaintext } from '@libp2p/plaintext' |
| 4 | +import { multiaddr } from '@multiformats/multiaddr' |
| 5 | +import { expect } from 'aegir/chai' |
| 6 | +import delay from 'delay' |
| 7 | +import { createLibp2p } from 'libp2p' |
| 8 | +import Sinon from 'sinon' |
| 9 | +import { stubInterface } from 'sinon-ts' |
| 10 | +import type { ConnectionGater, Libp2p } from '@libp2p/interface' |
| 11 | + |
| 12 | +async function createLocalNode (connectionGater: ConnectionGater): Promise<Libp2p> { |
| 13 | + return createLibp2p({ |
| 14 | + connectionGater, |
| 15 | + addresses: { |
| 16 | + listen: [ |
| 17 | + '/memory/0' |
| 18 | + ] |
| 19 | + }, |
| 20 | + transports: [ |
| 21 | + memory() |
| 22 | + ], |
| 23 | + connectionEncrypters: [ |
| 24 | + plaintext() |
| 25 | + ], |
| 26 | + streamMuxers: [ |
| 27 | + mplex() |
| 28 | + ] |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +describe('connection-gater', () => { |
| 33 | + let localNode: Libp2p |
| 34 | + let remoteNode: Libp2p |
| 35 | + |
| 36 | + beforeEach(async () => { |
| 37 | + remoteNode = await createLibp2p({ |
| 38 | + addresses: { |
| 39 | + listen: [ |
| 40 | + '/memory/1' |
| 41 | + ] |
| 42 | + }, |
| 43 | + transports: [ |
| 44 | + memory() |
| 45 | + ], |
| 46 | + connectionEncrypters: [ |
| 47 | + plaintext() |
| 48 | + ], |
| 49 | + streamMuxers: [ |
| 50 | + mplex() |
| 51 | + ] |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + afterEach(async () => { |
| 56 | + await localNode?.stop() |
| 57 | + await remoteNode?.stop() |
| 58 | + }) |
| 59 | + |
| 60 | + it('should deny dialling a peer', async () => { |
| 61 | + const connectionGater = stubInterface<ConnectionGater>({ |
| 62 | + denyDialPeer: Sinon.stub<any>().returns(true) |
| 63 | + }) |
| 64 | + |
| 65 | + localNode = await createLocalNode(connectionGater) |
| 66 | + |
| 67 | + const ma = multiaddr(`/memory/1/p2p/${remoteNode.peerId}`) |
| 68 | + |
| 69 | + await expect(localNode.dial(ma)).to.eventually.be.rejected |
| 70 | + .with.property('name', 'DialDeniedError') |
| 71 | + |
| 72 | + expect(connectionGater.denyDialPeer?.called).to.be.true() |
| 73 | + expect(connectionGater.denyDialPeer?.getCall(0).args[0]).to.deep.equal(remoteNode.peerId) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should deny dialling a multiaddr', async () => { |
| 77 | + const connectionGater = stubInterface<ConnectionGater>({ |
| 78 | + denyDialMultiaddr: Sinon.stub<any>().returns(true) |
| 79 | + }) |
| 80 | + |
| 81 | + localNode = await createLocalNode(connectionGater) |
| 82 | + |
| 83 | + await expect(localNode.dial(remoteNode.getMultiaddrs())).to.eventually.be.rejected |
| 84 | + .with.property('name', 'DialDeniedError') |
| 85 | + |
| 86 | + expect(connectionGater.denyDialMultiaddr?.called).to.be.true() |
| 87 | + }) |
| 88 | + |
| 89 | + it('should deny an inbound connection', async () => { |
| 90 | + const connectionGater = stubInterface<ConnectionGater>({ |
| 91 | + denyInboundConnection: Sinon.stub<any>().returns(true) |
| 92 | + }) |
| 93 | + |
| 94 | + localNode = await createLocalNode(connectionGater) |
| 95 | + |
| 96 | + await expect(remoteNode.dial(localNode.getMultiaddrs())).to.eventually.be.rejected |
| 97 | + .with.property('name', 'EncryptionFailedError') |
| 98 | + |
| 99 | + expect(connectionGater.denyInboundConnection?.called).to.be.true() |
| 100 | + }) |
| 101 | + |
| 102 | + it('should deny an outbound connection', async () => { |
| 103 | + const connectionGater = stubInterface<ConnectionGater>({ |
| 104 | + denyOutboundConnection: Sinon.stub<any>().returns(true) |
| 105 | + }) |
| 106 | + |
| 107 | + localNode = await createLocalNode(connectionGater) |
| 108 | + |
| 109 | + await expect(localNode.dial(remoteNode.getMultiaddrs(), { |
| 110 | + signal: AbortSignal.timeout(10_000) |
| 111 | + })).to.eventually.be.rejected |
| 112 | + .with.property('name', 'ConnectionInterceptedError') |
| 113 | + |
| 114 | + expect(connectionGater.denyOutboundConnection?.called).to.be.true() |
| 115 | + }) |
| 116 | + |
| 117 | + it('should deny an inbound encrypted connection', async () => { |
| 118 | + const connectionGater = stubInterface<ConnectionGater>({ |
| 119 | + denyInboundEncryptedConnection: Sinon.stub<any>().returns(true) |
| 120 | + }) |
| 121 | + |
| 122 | + localNode = await createLocalNode(connectionGater) |
| 123 | + |
| 124 | + await expect(remoteNode.dial(localNode.getMultiaddrs())).to.eventually.be.rejected |
| 125 | + .with.property('name', 'MuxerUnavailableError') |
| 126 | + |
| 127 | + expect(connectionGater.denyInboundEncryptedConnection?.called).to.be.true() |
| 128 | + }) |
| 129 | + |
| 130 | + it('should deny an outbound encrypted connection', async () => { |
| 131 | + const connectionGater = stubInterface<ConnectionGater>({ |
| 132 | + denyOutboundEncryptedConnection: Sinon.stub<any>().returns(true) |
| 133 | + }) |
| 134 | + |
| 135 | + localNode = await createLocalNode(connectionGater) |
| 136 | + |
| 137 | + await expect(localNode.dial(remoteNode.getMultiaddrs())).to.eventually.be.rejected |
| 138 | + .with.property('name', 'ConnectionInterceptedError') |
| 139 | + |
| 140 | + expect(connectionGater.denyOutboundEncryptedConnection?.called).to.be.true() |
| 141 | + }) |
| 142 | + |
| 143 | + it('should deny an inbound upgraded connection', async () => { |
| 144 | + const connectionGater = stubInterface<ConnectionGater>({ |
| 145 | + denyInboundUpgradedConnection: Sinon.stub<any>().returns(true) |
| 146 | + }) |
| 147 | + |
| 148 | + localNode = await createLocalNode(connectionGater) |
| 149 | + |
| 150 | + await remoteNode.dial(localNode.getMultiaddrs()) |
| 151 | + |
| 152 | + await delay(100) |
| 153 | + |
| 154 | + expect(localNode.getConnections()).to.be.empty() |
| 155 | + expect(connectionGater.denyInboundUpgradedConnection?.called).to.be.true() |
| 156 | + }) |
| 157 | + |
| 158 | + it('should deny an outbound upgraded connection', async () => { |
| 159 | + const connectionGater = stubInterface<ConnectionGater>({ |
| 160 | + denyOutboundUpgradedConnection: Sinon.stub<any>().returns(true) |
| 161 | + }) |
| 162 | + |
| 163 | + localNode = await createLocalNode(connectionGater) |
| 164 | + |
| 165 | + await expect(localNode.dial(remoteNode.getMultiaddrs())).to.eventually.be.rejected |
| 166 | + .with.property('name', 'ConnectionInterceptedError') |
| 167 | + |
| 168 | + expect(connectionGater.denyOutboundUpgradedConnection?.called).to.be.true() |
| 169 | + }) |
| 170 | +}) |
0 commit comments