55const chai = require ( 'chai' )
66chai . use ( require ( 'dirty-chai' ) )
77const expect = chai . expect
8- const waterfall = require ( 'async/waterfall' )
9- const each = require ( 'async/each' )
108const crypto = require ( 'libp2p-crypto' )
119const PeerId = require ( 'peer-id' )
1210
@@ -29,14 +27,16 @@ const generateCases = (hash) => {
2927 invalid : {
3028 publicKey : [
3129 // missing hashkey
32- Buffer . from ( '/pk/' ) ,
30+ [ Buffer . from ( '/pk/' ) , 'ERR_INVALID_RECORD_KEY_TOO_SHORT' ] ,
3331 // not the hash of a key
34- Buffer . concat ( [
32+ [ Buffer . concat ( [
3533 Buffer . from ( '/pk/' ) ,
3634 Buffer . from ( 'random' )
37- ] ) ,
35+ ] ) , 'ERR_INVALID_RECORD_HASH_MISMATCH' ] ,
3836 // missing prefix
39- hash
37+ [ hash , 'ERR_INVALID_RECORD_KEY_BAD_PREFIX' ] ,
38+ // not a buffer
39+ [ 'not a buffer' , 'ERR_INVALID_RECORD_KEY_NOT_BUFFER' ]
4040 ]
4141 }
4242 }
@@ -47,57 +47,47 @@ describe('validator', () => {
4747 let hash
4848 let cases
4949
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 )
50+ before ( async ( ) => {
51+ key = await crypto . keys . generateKeyPair ( 'rsa' , 1024 )
52+ hash = await key . public . hash ( )
53+ cases = generateCases ( hash )
6354 } )
6455
6556 describe ( 'verifyRecord' , ( ) => {
66- it ( 'calls matching validator' , ( done ) => {
57+ it ( 'calls matching validator' , ( ) => {
6758 const k = Buffer . from ( '/hello/you' )
6859 const rec = new Record ( k , Buffer . from ( 'world' ) , new PeerId ( hash ) )
6960
7061 const validators = {
7162 hello : {
72- func ( key , value , cb ) {
63+ func ( key , value ) {
7364 expect ( key ) . to . eql ( k )
7465 expect ( value ) . to . eql ( Buffer . from ( 'world' ) )
75- cb ( )
7666 } ,
7767 sign : false
7868 }
7969 }
80- validator . verifyRecord ( validators , rec , done )
70+ return validator . verifyRecord ( validators , rec )
8171 } )
8272
83- it ( 'calls not matching any validator' , ( done ) => {
73+ it ( 'calls not matching any validator' , ( ) => {
8474 const k = Buffer . from ( '/hallo/you' )
8575 const rec = new Record ( k , Buffer . from ( 'world' ) , new PeerId ( hash ) )
8676
8777 const validators = {
8878 hello : {
89- func ( key , value , cb ) {
79+ func ( key , value ) {
9080 expect ( key ) . to . eql ( k )
9181 expect ( value ) . to . eql ( Buffer . from ( 'world' ) )
92- cb ( )
9382 } ,
9483 sign : false
9584 }
9685 }
97- validator . verifyRecord ( validators , rec , ( err ) => {
98- expect ( err ) . to . exist ( )
99- done ( )
100- } )
86+ return expect (
87+ ( ) => validator . verifyRecord ( validators , rec )
88+ ) . to . throw (
89+ / I n v a l i d r e c o r d k e y t y p e /
90+ )
10191 } )
10292 } )
10393
@@ -107,40 +97,40 @@ describe('validator', () => {
10797 } )
10898
10999 describe ( 'public key' , ( ) => {
110- it ( 'exports func and sing ' , ( ) => {
100+ it ( 'exports func and sign ' , ( ) => {
111101 const pk = validator . validators . pk
112102
113103 expect ( pk ) . to . have . property ( 'func' )
114104 expect ( pk ) . to . have . property ( 'sign' , false )
115105 } )
116106
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 )
107+ it ( 'does not error on valid record' , ( ) => {
108+ return Promise . all ( cases . valid . publicKey . map ( ( k ) => {
109+ return validator . validators . pk . func ( k , key . public . bytes )
110+ } ) )
121111 } )
122112
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 )
113+ it ( 'throws on invalid records' , ( ) => {
114+ return Promise . all ( cases . invalid . publicKey . map ( async ( [ k , errCode ] ) => {
115+ try {
116+ await validator . validators . pk . func ( k , key . public . bytes )
117+ } catch ( err ) {
118+ expect ( err . code ) . to . eql ( errCode )
119+ return
120+ }
121+ expect . fail ( 'did not throw an error with code ' + errCode )
122+ } ) )
130123 } )
131124 } )
132125 } )
133126
134127 describe ( 'go interop' , ( ) => {
135- it ( 'record with key from from go' , ( done ) => {
128+ it ( 'record with key from from go' , async ( ) => {
136129 const pubKey = crypto . keys . unmarshalPublicKey ( fixture . publicKey )
137130
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- } )
131+ const hash = await pubKey . hash ( )
132+ const k = Buffer . concat ( [ Buffer . from ( '/pk/' ) , hash ] )
133+ return validator . validators . pk . func ( k , pubKey . bytes )
144134 } )
145135 } )
146136} )
0 commit comments