-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
116a887
commit aacd106
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { generateKeyPair } from '@libp2p/crypto/keys' | ||
import { peerIdFromString, peerIdFromPrivateKey } from '@libp2p/peer-id' | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { expect } from 'aegir/chai' | ||
import Sinon from 'sinon' | ||
import { matchMultiaddr, matchPeerId } from '../src/matchers.js' | ||
|
||
describe('peer id matcher', () => { | ||
it('should match the same object', async () => { | ||
const privateKey = await generateKeyPair('Ed25519') | ||
const peerId = peerIdFromPrivateKey(privateKey) | ||
|
||
const stub = Sinon.stub() | ||
stub(peerId) | ||
|
||
expect(stub.calledWith(matchPeerId(peerId))).to.be.true() | ||
}) | ||
|
||
it('should match the same value', async () => { | ||
const privateKey = await generateKeyPair('Ed25519') | ||
const peerId = peerIdFromPrivateKey(privateKey) | ||
const peerId2 = peerIdFromString(peerId.toString()) | ||
|
||
const stub = Sinon.stub() | ||
stub(peerId) | ||
|
||
// this does not match because peerId2 does not contain the private key so | ||
// the values are not deeply equal | ||
expect(stub.calledWith(peerId2)).to.be.false() | ||
expect(stub.calledWith(matchPeerId(peerId2))).to.be.true() | ||
}) | ||
}) | ||
|
||
describe('multiaddr matcher', () => { | ||
it('should match the same object', async () => { | ||
const ma = multiaddr('/ip4/127.0.0.1/tcp/4001') | ||
|
||
const stub = Sinon.stub() | ||
stub(ma) | ||
|
||
expect(stub.calledWith(matchMultiaddr(ma))).to.be.true() | ||
}) | ||
|
||
it('should match the same value', async () => { | ||
const ma = multiaddr('/ip4/127.0.0.1/tcp/4001') | ||
const ma2 = multiaddr('/ip4/127.0.0.1/tcp/4001') | ||
|
||
const stub = Sinon.stub() | ||
stub(ma) | ||
|
||
// this would match because no properties are changed after creation since | ||
// https://github.com/multiformats/js-multiaddr/pull/330 | ||
// expect(stub.calledWith(ma2)).to.be.false() | ||
expect(stub.calledWith(matchMultiaddr(ma2))).to.be.true() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { generateKeyPair } from '@libp2p/crypto/keys' | ||
import { peerIdFromPrivateKey } from '@libp2p/peer-id' | ||
import { pipe } from 'it-pipe' | ||
import tests from '../../src/connection/index.js' | ||
import { connectionPair } from '../../src/mocks/connection.js' | ||
import { mockRegistrar } from '../../src/mocks/registrar.js' | ||
import type { Connection } from '@libp2p/interface' | ||
|
||
describe('mock connection compliance tests', () => { | ||
let connections: Connection[] = [] | ||
|
||
tests({ | ||
async setup () { | ||
const privateKeyA = await generateKeyPair('Ed25519') | ||
const componentsA = { | ||
peerId: peerIdFromPrivateKey(privateKeyA), | ||
registrar: mockRegistrar() | ||
} | ||
const privateKeyB = await generateKeyPair('Ed25519') | ||
const componentsB = { | ||
peerId: peerIdFromPrivateKey(privateKeyB), | ||
registrar: mockRegistrar() | ||
} | ||
connections = connectionPair(componentsA, componentsB) | ||
|
||
await componentsB.registrar.handle('/echo/0.0.1', (data) => { | ||
void pipe( | ||
data.stream, | ||
data.stream | ||
) | ||
}) | ||
|
||
return connections[0] | ||
}, | ||
async teardown () { | ||
await Promise.all(connections.map(async conn => { | ||
await conn.close() | ||
})) | ||
} | ||
}) | ||
}) |