forked from libp2p/js-libp2p-kad-dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkad-utils.spec.js
125 lines (106 loc) · 3.31 KB
/
kad-utils.spec.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const base32 = require('base32.js')
const PeerId = require('peer-id')
const distance = require('xor-distance')
const waterfall = require('async/waterfall')
const utils = require('../src/utils')
const createPeerInfo = require('./utils/create-peer-info')
describe('kad utils', () => {
describe('bufferToKey', () => {
it('returns the base32 encoded key of the buffer', () => {
const buf = Buffer.from('hello world')
const key = utils.bufferToKey(buf)
const enc = new base32.Encoder()
expect(key.toString())
.to.equal('/' + enc.write(buf).finalize())
})
})
describe('convertBuffer', () => {
it('returns the sha2-256 hash of the buffer', (done) => {
const buf = Buffer.from('hello world')
utils.convertBuffer(buf, (err, digest) => {
expect(err).to.not.exist()
expect(digest)
.to.eql(Buffer.from('b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9', 'hex'))
done()
})
})
})
describe('sortClosestPeers', () => {
it('sorts a list of PeerInfos', (done) => {
const rawIds = [
'11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31',
'11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32',
'11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33',
'11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34'
]
const ids = rawIds.map((raw) => {
return new PeerId(Buffer.from(raw))
})
const input = [
ids[2],
ids[1],
ids[3],
ids[0]
]
waterfall([
(cb) => utils.convertPeerId(ids[0], cb),
(id, cb) => utils.sortClosestPeers(input, id, cb),
(out, cb) => {
expect(
out.map((m) => m.toB58String())
).to.eql([
ids[0],
ids[3],
ids[2],
ids[1]
].map((m) => m.toB58String()))
done()
}
], done)
})
})
describe('xorCompare', () => {
it('sorts two distances', () => {
const target = Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a90')
const a = {
distance: distance(Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a95'), target)
}
const b = {
distance: distance(Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a96'), target)
}
expect(utils.xorCompare(a, b)).to.eql(-1)
expect(utils.xorCompare(b, a)).to.eql(1)
expect(utils.xorCompare(a, a)).to.eql(0)
})
})
describe('keyForPublicKey', () => {
it('works', (done) => {
createPeerInfo(1, (err, peers) => {
expect(err).to.not.exist()
expect(utils.keyForPublicKey(peers[0].id))
.to.eql(Buffer.concat([Buffer.from('/pk/'), peers[0].id.id]))
done()
})
})
})
describe('fromPublicKeyKey', () => {
it('round trips', function (done) {
this.timeout(40 * 1000)
createPeerInfo(50, (err, peers) => {
expect(err).to.not.exist()
peers.forEach((p, i) => {
const id = p.id
expect(utils.isPublicKeyKey(utils.keyForPublicKey(id))).to.eql(true)
expect(utils.fromPublicKeyKey(utils.keyForPublicKey(id)).id)
.to.eql(id.id)
})
done()
})
})
})
})