forked from multiformats/js-multiaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec.spec.ts
78 lines (70 loc) · 2.44 KB
/
codec.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import varint from 'varint'
import * as codec from '../src/codec.js'
import { convertToBytes } from '../src/convert.js'
describe('codec', () => {
describe('.stringToStringTuples', () => {
it('throws on invalid addresses', () => {
expect(
() => codec.stringToStringTuples('/ip4/0.0.0.0/ip4')
).to.throw(
/invalid address/
)
})
})
describe('.stringTuplesToTuples', () => {
const testCases: Array<{ name: string, stringTuples: Array<string[] | string>, tuples: Array<[number, Uint8Array?]>, path: string | null }> = [
{ name: 'handles non array tuples', stringTuples: [['ip4', '0.0.0.0'], 'utp'], tuples: [[4, Uint8Array.from([0, 0, 0, 0])], [302]], path: null },
{ name: 'handle not null path', stringTuples: [['unix', '/tmp/p2p.sock']], tuples: [[400, convertToBytes(400, '/tmp/p2p.sock')]], path: '/tmp/p2p.sock' },
{ name: 'should return the 1st path', stringTuples: [['unix', '/tmp/p2p.sock'], ['unix', '/tmp2/p2p.sock']], tuples: [[400, convertToBytes(400, '/tmp/p2p.sock')], [400, convertToBytes(400, '/tmp2/p2p.sock')]], path: '/tmp/p2p.sock' }
]
for (const { name, stringTuples, tuples, path } of testCases) {
it(name, () => {
expect(
codec.stringTuplesToTuples(stringTuples)
).to.eql(
{ tuples, path }
)
})
}
})
describe('.tuplesToStringTuples', () => {
it('single element tuples', () => {
expect(
codec.tuplesToStringTuples([[302]])
).to.eql(
[[302]]
)
})
})
describe('.bytesToTuples', () => {
it('throws on invalid address', () => {
expect(
() => codec.bytesToTuples(codec.tuplesToBytes([[4, uint8ArrayFromString('192')]]))
).to.throw(
/Invalid address/
)
})
})
describe('.fromBytes', () => {
it('throws on invalid buffer', () => {
expect(
() => codec.fromBytes(uint8ArrayFromString('hello/world'))
).to.throw()
})
})
describe('.isValidBytes', () => {
it('returns true for valid buffers', () => {
expect(
codec.isValidBytes(Uint8Array.from(varint.encode(302)))
).to.equal(true)
})
it('returns false for invalid buffers', () => {
expect(
codec.isValidBytes(Uint8Array.from(varint.encode(1234)))
).to.equal(false)
})
})
})