1
- /* globals before, describe, it */
1
+ /* globals describe, it */
2
2
import crypto from 'crypto'
3
3
import OLDCID from 'cids'
4
4
import assert from 'assert'
@@ -48,11 +48,6 @@ describe('CID', () => {
48
48
]
49
49
multihash . add ( hashes )
50
50
const b58 = multibase . get ( 'base58btc' )
51
- let hash
52
-
53
- before ( async ( ) => {
54
- hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
55
- } )
56
51
57
52
describe ( 'v0' , ( ) => {
58
53
test ( 'handles B58Str multihash' , ( ) => {
@@ -66,7 +61,8 @@ describe('CID', () => {
66
61
same ( cid . toString ( ) , mhStr )
67
62
} )
68
63
69
- test ( 'create by parts' , ( ) => {
64
+ test ( 'create by parts' , async ( ) => {
65
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
70
66
const cid = new CID ( 0 , 112 , hash )
71
67
72
68
same ( cid . code , 112 )
@@ -76,17 +72,30 @@ describe('CID', () => {
76
72
same ( cid . toString ( ) , b58 . encode ( hash ) )
77
73
} )
78
74
79
- test ( 'throws on invalid BS58Str multihash ' , ( ) => {
75
+ test ( 'create from multihash' , async ( ) => {
76
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
77
+ const cid = new CID ( hash )
78
+
79
+ same ( cid . code , 112 )
80
+ same ( cid . version , 0 )
81
+ same ( cid . multihash , hash )
82
+ cid . toString ( )
83
+ same ( cid . toString ( ) , b58 . encode ( hash ) )
84
+ } )
85
+
86
+ test ( 'throws on invalid BS58Str multihash ' , async ( ) => {
80
87
const msg = 'Non-base58 character'
81
88
testThrow ( ( ) => new CID ( 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII' ) , msg )
82
89
} )
83
90
84
- test ( 'throws on trying to create a CIDv0 with a codec other than dag-pb' , ( ) => {
91
+ test ( 'throws on trying to create a CIDv0 with a codec other than dag-pb' , async ( ) => {
92
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
85
93
const msg = 'Version 0 CID must be 112 codec (dag-cbor)'
86
94
testThrow ( ( ) => new CID ( 0 , 113 , hash ) , msg )
87
95
} )
88
96
89
- test ( 'throws on trying to pass specific base encoding [deprecated]' , ( ) => {
97
+ test ( 'throws on trying to pass specific base encoding [deprecated]' , async ( ) => {
98
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
90
99
const msg = 'No longer supported, cannot specify base encoding in instantiation'
91
100
testThrow ( ( ) => new CID ( 0 , 112 , hash , 'base32' ) , msg )
92
101
} )
@@ -98,7 +107,8 @@ describe('CID', () => {
98
107
testThrow ( ( ) => cid . toString ( 'base32' ) , msg )
99
108
} )
100
109
101
- test ( '.buffer' , ( ) => {
110
+ test ( '.buffer' , async ( ) => {
111
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
102
112
const codec = 112
103
113
const cid = new CID ( 0 , codec , hash )
104
114
const buffer = cid . buffer
@@ -134,14 +144,16 @@ describe('CID', () => {
134
144
same ( cid . toString ( ) , cidStr )
135
145
} )
136
146
137
- test ( 'create by parts' , ( ) => {
147
+ test ( 'create by parts' , async ( ) => {
148
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
138
149
const cid = new CID ( 1 , 0x71 , hash )
139
150
same ( cid . code , 0x71 )
140
151
same ( cid . version , 1 )
141
152
same ( cid . multihash , hash )
142
153
} )
143
154
144
- test ( 'can roundtrip through cid.toString()' , ( ) => {
155
+ test ( 'can roundtrip through cid.toString()' , async ( ) => {
156
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
145
157
const cid1 = new CID ( 1 , 0x71 , hash )
146
158
const cid2 = new CID ( cid1 . toString ( ) )
147
159
@@ -167,7 +179,8 @@ describe('CID', () => {
167
179
})
168
180
*/
169
181
170
- test ( '.buffer' , ( ) => {
182
+ test ( '.buffer' , async ( ) => {
183
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
171
184
const code = 0x71
172
185
const cid = new CID ( 1 , code , hash )
173
186
const buffer = cid . buffer
@@ -248,13 +261,6 @@ describe('CID', () => {
248
261
test ( name , ( ) => testThrowAny ( ( ) => new CID ( 1 , 112 , i ) ) )
249
262
}
250
263
invalid . forEach ( mapper )
251
-
252
- const invalidVersions = [ - 1 , 2 ]
253
- mapper = i => {
254
- const name = `new CID(${ i } , 112, buffer)`
255
- test ( name , ( ) => testThrowAny ( new CID ( i , 112 , hash ) ) )
256
- }
257
- invalidVersions . forEach ( mapper )
258
264
} )
259
265
260
266
describe ( 'idempotence' , ( ) => {
@@ -301,7 +307,8 @@ describe('CID', () => {
301
307
assert . ok ( cid . buffer )
302
308
same ( cid . buffer , cid . buffer )
303
309
} )
304
- test ( 'should cache string representation when it matches the multibaseName it was constructed with' , ( ) => {
310
+ test ( 'should cache string representation when it matches the multibaseName it was constructed with' , async ( ) => {
311
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
305
312
const cid = new CID ( 1 , 112 , hash )
306
313
same ( cid . _baseCache . size , 0 )
307
314
@@ -323,17 +330,20 @@ describe('CID', () => {
323
330
} )
324
331
} )
325
332
326
- test ( 'toJSON()' , ( ) => {
333
+ test ( 'toJSON()' , async ( ) => {
334
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
327
335
const cid = new CID ( 1 , 112 , hash )
328
336
same ( cid . toJSON ( ) , { code : 112 , version : 1 , hash } )
329
337
} )
330
338
331
- test ( 'isCID' , ( ) => {
339
+ test ( 'isCID' , async ( ) => {
340
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
332
341
const cid = new CID ( 1 , 112 , hash )
333
342
assert . ok ( OLDCID . isCID ( cid ) )
334
343
} )
335
344
336
- test ( 'asCID' , ( ) => {
345
+ test ( 'asCID' , async ( ) => {
346
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
337
347
class IncompatibleCID {
338
348
constructor ( version , code , multihash ) {
339
349
this . version = version
@@ -349,9 +359,9 @@ describe('CID', () => {
349
359
350
360
const version = 1
351
361
const code = 112
352
- const multihash = hash
362
+ const _multihash = hash
353
363
354
- const incompatibleCID = new IncompatibleCID ( version , code , multihash )
364
+ const incompatibleCID = new IncompatibleCID ( version , code , _multihash )
355
365
assert . ok ( CID . isCID ( incompatibleCID ) )
356
366
assert . strictEqual ( incompatibleCID . toString ( ) , '[object Object]' )
357
367
assert . strictEqual ( typeof incompatibleCID . toV0 , 'undefined' )
@@ -360,18 +370,18 @@ describe('CID', () => {
360
370
assert . ok ( cid1 instanceof CID )
361
371
assert . strictEqual ( cid1 . code , code )
362
372
assert . strictEqual ( cid1 . version , version )
363
- assert . strictEqual ( cid1 . multihash , multihash )
373
+ assert . strictEqual ( cid1 . multihash , _multihash )
364
374
365
- const cid2 = CID . asCID ( { version, code, multihash } )
375
+ const cid2 = CID . asCID ( { version, code, _multihash } )
366
376
assert . strictEqual ( cid2 , null )
367
377
368
- const duckCID = { version, code, multihash }
378
+ const duckCID = { version, code, multihash : _multihash }
369
379
duckCID . asCID = duckCID
370
380
const cid3 = CID . asCID ( duckCID )
371
381
assert . ok ( cid3 instanceof CID )
372
382
assert . strictEqual ( cid3 . code , code )
373
383
assert . strictEqual ( cid3 . version , version )
374
- assert . strictEqual ( cid3 . multihash , multihash )
384
+ assert . strictEqual ( cid3 . multihash , _multihash )
375
385
376
386
const cid4 = CID . asCID ( cid3 )
377
387
assert . strictEqual ( cid3 , cid4 )
@@ -383,42 +393,48 @@ describe('CID', () => {
383
393
assert . strictEqual ( cid5 . code , 85 )
384
394
} )
385
395
386
- test ( 'new CID from old CID' , ( ) => {
396
+ test ( 'new CID from old CID' , async ( ) => {
397
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
387
398
const cid = new CID ( new OLDCID ( 1 , 'raw' , Buffer . from ( hash ) ) )
388
399
same ( cid . version , 1 )
389
400
same ( cid . multihash , hash )
390
401
same ( cid . code , 85 )
391
402
} )
392
403
393
404
if ( ! process . browser ) {
394
- test ( 'util.inspect' , ( ) => {
405
+ test ( 'util.inspect' , async ( ) => {
406
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
395
407
const cid = new CID ( 1 , 112 , hash )
396
408
same ( util . inspect ( cid ) , 'CID(bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu)' )
397
409
} )
398
410
}
399
411
400
- describe ( 'deprecations' , ( ) => {
412
+ describe ( 'deprecations' , async ( ) => {
401
413
test ( 'codec' , async ( ) => {
414
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
402
415
const cid = new CID ( 1 , 112 , hash )
403
416
await testThrow ( ( ) => cid . codec , '"codec" property is deprecated, use integer "code" property instead' )
404
417
await testThrow ( ( ) => new CID ( 1 , 'dag-pb' , hash ) , 'String codecs are no longer supported' )
405
418
} )
406
419
test ( 'multibaseName' , async ( ) => {
420
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
407
421
const cid = new CID ( 1 , 112 , hash )
408
422
await testThrow ( ( ) => cid . multibaseName , '"multibaseName" property is deprecated' )
409
423
} )
410
424
test ( 'prefix' , async ( ) => {
425
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
411
426
const cid = new CID ( 1 , 112 , hash )
412
427
await testThrow ( ( ) => cid . prefix , '"prefix" property is deprecated' )
413
428
} )
414
429
test ( 'toBaseEncodedString()' , async ( ) => {
430
+ const hash = await multihash . hash ( Buffer . from ( 'abc' ) , 'sha2-256' )
415
431
const cid = new CID ( 1 , 112 , hash )
416
432
await testThrow ( ( ) => cid . toBaseEncodedString ( ) , 'Deprecated, use .toString()' )
417
433
} )
418
434
} )
419
435
420
436
test ( 'invalid CID version' , async ( ) => {
421
- const encoded = varint . encode ( 18 )
422
- await testThrow ( ( ) => new CID ( encoded ) , 'Invalid CID version 18 ' )
437
+ const encoded = varint . encode ( 2 )
438
+ await testThrow ( ( ) => new CID ( encoded ) , 'Invalid CID version 2 ' )
423
439
} )
424
440
} )
0 commit comments