Skip to content

Commit a19a735

Browse files
authored
feat!: test tls encryption (#145)
Adds tests for tls encryption but as a major to control when it is pulled in to the js-libp2p monorepo. BREAKING CHANGE: tls support is required
1 parent ba8a627 commit a19a735

File tree

6 files changed

+45
-42
lines changed

6 files changed

+45
-42
lines changed

src/connect.ts

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
import { expect } from 'aegir/chai'
2-
import { keys } from './resources/keys/index.js'
3-
import type { Daemon, NodeType, SpawnOptions, DaemonFactory, PeerIdType } from './index.js'
2+
import { runTests } from './utils/test-matrix.js'
3+
import type { Daemon, SpawnOptions, DaemonFactory } from './index.js'
44

55
export function connectTests (factory: DaemonFactory): void {
6-
const keyTypes: PeerIdType[] = ['ed25519', 'rsa', 'secp256k1']
7-
const impls: NodeType[] = ['js', 'go']
8-
9-
for (const keyType of keyTypes) {
10-
for (const implA of impls) {
11-
for (const implB of impls) {
12-
runConnectTests(
13-
`noise/${keyType}`,
14-
factory,
15-
{ type: implA, noise: true, key: keys.go[keyType] },
16-
{ type: implB, noise: true, key: keys.js[keyType] }
17-
)
18-
}
19-
}
20-
}
6+
runTests('connect', runConnectTests, factory)
217
}
228

239
function runConnectTests (name: string, factory: DaemonFactory, optionsA: SpawnOptions, optionsB: SpawnOptions): void {
24-
describe(`connect using ${name}`, () => {
10+
describe(name, () => {
2511
let daemonA: Daemon
2612
let daemonB: Daemon
2713

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ export type NodeType = 'js' | 'go'
5858
export type PeerIdType = 'rsa' | 'ed25519' | 'secp256k1'
5959
export type PubSubRouter = 'gossipsub' | 'floodsub'
6060
export type Muxer = 'mplex' | 'yamux'
61+
export type Encryption = 'noise' | 'tls'
6162

6263
export interface SpawnOptions {
6364
type: NodeType
6465
key?: string
65-
noise?: true
66+
encryption?: Encryption
6667
dht?: boolean
6768
pubsub?: boolean
6869
pubsubRouter?: PubSubRouter

src/relay/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export function relayTests (factory: DaemonFactory): void {
1515
function relayTest (factory: DaemonFactory, aType: NodeType, bType: NodeType, relayType: NodeType): void {
1616
describe(`${aType} to ${bType} over relay ${relayType}`, () => {
1717
const opts: SpawnOptions[] = [
18-
{ type: aType, noise: true, noListen: true },
19-
{ type: bType, noise: true, noListen: true },
20-
{ type: relayType, noise: true, relay: true }
18+
{ type: aType, encryption: 'noise', noListen: true },
19+
{ type: bType, encryption: 'noise', noListen: true },
20+
{ type: relayType, encryption: 'noise', relay: true }
2121
]
2222

2323
let aNode: Daemon

src/streams/echo.ts

+3-18
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,10 @@ import all from 'it-all'
55
import { pipe } from 'it-pipe'
66
import defer from 'p-defer'
77
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8-
import type { Daemon, DaemonFactory, Muxer, NodeType, SpawnOptions } from '../index.js'
8+
import type { Daemon, DaemonFactory, SpawnOptions } from '../index.js'
99

10-
export function echoStreamTests (factory: DaemonFactory, muxer: Muxer): void {
11-
const nodeTypes: NodeType[] = ['js', 'go']
12-
13-
for (const typeA of nodeTypes) {
14-
for (const typeB of nodeTypes) {
15-
runEchoStreamTests(
16-
factory,
17-
muxer,
18-
{ type: typeA, muxer },
19-
{ type: typeB, muxer }
20-
)
21-
}
22-
}
23-
}
24-
25-
function runEchoStreamTests (factory: DaemonFactory, muxer: Muxer, optionsA: SpawnOptions, optionsB: SpawnOptions): void {
26-
describe(`echo streams - ${muxer}`, () => {
10+
export function echoStreamTests (name: string, factory: DaemonFactory, optionsA: SpawnOptions, optionsB: SpawnOptions): void {
11+
describe(name, () => {
2712
let daemonA: Daemon
2813
let daemonB: Daemon
2914

src/streams/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { runTests } from '../utils/test-matrix.js'
12
import { echoStreamTests } from './echo.js'
23
import type { DaemonFactory } from '../index.js'
34

45
export async function streamTests (factory: DaemonFactory): Promise<void> {
5-
echoStreamTests(factory, 'mplex')
6-
echoStreamTests(factory, 'yamux')
6+
runTests('echo', echoStreamTests, factory)
77
}

src/utils/test-matrix.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { keys } from '../resources/keys/index.js'
2+
import type { DaemonFactory, Encryption, Muxer, NodeType, PeerIdType, SpawnOptions } from '../index.js'
3+
4+
export interface TestFunction {
5+
(name: string, factory: DaemonFactory, optionsA: SpawnOptions, optionsB: SpawnOptions): void
6+
}
7+
8+
export function runTests (name: string, fn: TestFunction, factory: DaemonFactory): void {
9+
const keyTypes: PeerIdType[] = ['ed25519', 'rsa', 'secp256k1']
10+
const impls: NodeType[] = ['js', 'go']
11+
const encrypters: Encryption[] = ['noise', 'tls']
12+
const muxers: Muxer[] = ['mplex', 'yamux']
13+
14+
for (const keyType of keyTypes) {
15+
for (const implA of impls) {
16+
for (const implB of impls) {
17+
for (const encrypter of encrypters) {
18+
// eslint-disable-next-line max-depth
19+
for (const muxer of muxers) {
20+
fn(
21+
`${keyType}/${encrypter}/${muxer} ${name}`,
22+
factory,
23+
{ type: implA, encryption: encrypter, key: keys.go[keyType] },
24+
{ type: implB, encryption: encrypter, key: keys.js[keyType] }
25+
)
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)