forked from multiformats/js-multiaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.spec.ts
104 lines (93 loc) · 3.01 KB
/
convert.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-env mocha */
import * as convert from '../src/convert.js'
import { expect } from 'aegir/chai'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
describe('convert', () => {
it('handles ip4 buffers', () => {
expect(
convert.convertToString('ip4', uint8ArrayFromString('c0a80001', 'base16'))
).to.eql(
'192.168.0.1'
)
})
it('handles ip6 buffers', () => {
expect(
convert.convertToString('ip6', uint8ArrayFromString('abcd0000000100020003000400050006', 'base16'))
).to.eql(
'abcd:0:1:2:3:4:5:6'
)
})
it('handles ipv6 strings', () => {
expect(
convert.convertToBytes('ip6', 'ABCD::1:2:3:4:5:6')
).to.eql(
uint8ArrayFromString('ABCD0000000100020003000400050006', 'base16upper')
)
})
it('handles ip4 strings', () => {
expect(
convert.convertToBytes('ip4', '192.168.0.1')
).to.eql(
uint8ArrayFromString('c0a80001', 'base16')
)
})
it('throws on invalid ip4 conversion', () => {
expect(
() => convert.convertToBytes('ip4', '555.168.0.1')
).to.throw(
/invalid ip address/
)
})
it('throws on invalid ip6 conversion', () => {
expect(
() => convert.convertToBytes('ip6', 'FFFF::GGGG')
).to.throw(
/invalid ip address/
)
})
describe('.toBytes', () => {
it('defaults to hex conversion', () => {
expect(
convert.convertToBytes('ws', 'c0a80001')
).to.eql(
Uint8Array.from([192, 168, 0, 1])
)
})
})
describe('.toString', () => {
it('throws on inconsistent ipfs links', () => {
const valid = uint8ArrayFromString('03221220d52ebb89d85b02a284948203a62ff28389c57c9f42beec4ec20db76a68911c0b', 'base16')
expect(
() => convert.convertToString('ipfs', valid.slice(0, valid.length - 8))
).to.throw(
/inconsistent length/
)
})
it('defaults to hex conversion', () => {
expect(
convert.convertToString('ws', Uint8Array.from([192, 168, 0, 1]))
).to.eql(
'c0a80001'
)
})
it('respects byteoffset during conversion', () => {
const bytes = convert.convertToBytes('sctp', '1234')
const buffer = new Uint8Array(bytes.byteLength + 5)
buffer.set(bytes, 5)
expect(convert.convertToString('sctp', buffer.subarray(5))).to.equal('1234')
})
})
it('can round-trip certhash, though encoding base may change', ()=>{
let myCertFingerprint = {
"algorithm": "sha-256",
"value": "f4:32:a0:45:34:62:85:e0:d8:d7:75:36:84:72:8e:b2:aa:9e:71:64:e4:eb:fe:06:51:64:42:64:fe:04:a8:d0"
};
let mb = 'f' + myCertFingerprint.value.replaceAll(':','');
let bytes = convert.convertToBytes('certhash',mb);
let outcome = convert.convertToString(466, bytes);
//Although I sent hex encoding in, base58btc always comes out
expect(outcome).to.equal('u9DKgRTRiheDY13U2hHKOsqqecWTk6_4GUWRCZP4EqNA');
let bytesOut = convert.convertToBytes(466,outcome);
expect(bytesOut.toString()).to.equal(bytes.toString());
})
})