1 |
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const chai = require('chai')
|
6 | chai.use(require('dirty-chai'))
|
7 | const expect = chai.expect
|
8 | const waterfall = require('async/waterfall')
|
9 | const each = require('async/each')
|
10 | const crypto = require('libp2p-crypto')
|
11 | const PeerId = require('peer-id')
|
12 |
|
13 | const libp2pRecord = require('../src')
|
14 | const validator = libp2pRecord.validator
|
15 | const Record = libp2pRecord.Record
|
16 |
|
17 | const fixture = require('./fixtures/go-key-records.js')
|
18 |
|
19 | const generateCases = (hash) => {
|
20 | return {
|
21 | valid: {
|
22 | publicKey: [
|
23 | Buffer.concat([
|
24 | Buffer.from('/pk/'),
|
25 | hash
|
26 | ])
|
27 | ]
|
28 | },
|
29 | invalid: {
|
30 | publicKey: [
|
31 |
|
32 | Buffer.from('/pk/'),
|
33 |
|
34 | Buffer.concat([
|
35 | Buffer.from('/pk/'),
|
36 | Buffer.from('random')
|
37 | ]),
|
38 |
|
39 | hash
|
40 | ]
|
41 | }
|
42 | }
|
43 | }
|
44 |
|
45 | describe('validator', () => {
|
46 | let key
|
47 | let hash
|
48 | let cases
|
49 |
|
50 | before((done) => {
|
51 | waterfall([
|
52 | (cb) => crypto.keys.generateKeyPair('rsa', 1024, cb),
|
53 | (pair, cb) => {
|
54 | key = pair
|
55 | pair.public.hash(cb)
|
56 | },
|
57 | (_hash, cb) => {
|
58 | hash = _hash
|
59 | cases = generateCases(hash)
|
60 | cb()
|
61 | }
|
62 | ], done)
|
63 | })
|
64 |
|
65 | describe('verifyRecord', () => {
|
66 | it('calls matching validator', (done) => {
|
67 | const k = Buffer.from('/hello/you')
|
68 | const rec = new Record(k, Buffer.from('world'), new PeerId(hash))
|
69 |
|
70 | const validators = {
|
71 | hello: {
|
72 | func (key, value, cb) {
|
73 | expect(key).to.eql(k)
|
74 | expect(value).to.eql(Buffer.from('world'))
|
75 | cb()
|
76 | },
|
77 | sign: false
|
78 | }
|
79 | }
|
80 | validator.verifyRecord(validators, rec, done)
|
81 | })
|
82 |
|
83 | it('calls not matching any validator', (done) => {
|
84 | const k = Buffer.from('/hallo/you')
|
85 | const rec = new Record(k, Buffer.from('world'), new PeerId(hash))
|
86 |
|
87 | const validators = {
|
88 | hello: {
|
89 | func (key, value, cb) {
|
90 | expect(key).to.eql(k)
|
91 | expect(value).to.eql(Buffer.from('world'))
|
92 | cb()
|
93 | },
|
94 | sign: false
|
95 | }
|
96 | }
|
97 | validator.verifyRecord(validators, rec, (err) => {
|
98 | expect(err).to.exist()
|
99 | done()
|
100 | })
|
101 | })
|
102 | })
|
103 |
|
104 | describe('validators', () => {
|
105 | it('exports pk', () => {
|
106 | expect(validator.validators).to.have.keys(['pk'])
|
107 | })
|
108 |
|
109 | describe('public key', () => {
|
110 | it('exports func and sing', () => {
|
111 | const pk = validator.validators.pk
|
112 |
|
113 | expect(pk).to.have.property('func')
|
114 | expect(pk).to.have.property('sign', false)
|
115 | })
|
116 |
|
117 | it('does not error on valid record', (done) => {
|
118 | each(cases.valid.publicKey, (k, cb) => {
|
119 | validator.validators.pk.func(k, key.public.bytes, cb)
|
120 | }, done)
|
121 | })
|
122 |
|
123 | it('throws on invalid records', (done) => {
|
124 | each(cases.invalid.publicKey, (k, cb) => {
|
125 | validator.validators.pk.func(k, key.public.bytes, (err) => {
|
126 | expect(err).to.exist()
|
127 | cb()
|
128 | })
|
129 | }, done)
|
130 | })
|
131 | })
|
132 | })
|
133 |
|
134 | describe('go interop', () => {
|
135 | it('record with key from from go', (done) => {
|
136 | const pubKey = crypto.keys.unmarshalPublicKey(fixture.publicKey)
|
137 |
|
138 | pubKey.hash((err, hash) => {
|
139 | expect(err).to.not.exist()
|
140 | const k = Buffer.concat([Buffer.from('/pk/'), hash])
|
141 |
|
142 | validator.validators.pk.func(k, pubKey.bytes, done)
|
143 | })
|
144 | })
|
145 | })
|
146 | })
|