|
2 | 2 |
|
3 | 3 | import OLDCID from 'cids' |
4 | 4 | import assert from 'assert' |
5 | | -import { toHex, equals } from '../src/bytes.js' |
| 5 | +import { fromHex, toHex, equals } from '../src/bytes.js' |
6 | 6 | import { varint, CID } from 'multiformats' |
7 | 7 | import { base58btc } from 'multiformats/bases/base58' |
8 | 8 | import { base32 } from 'multiformats/bases/base32' |
9 | 9 | import { base64 } from 'multiformats/bases/base64' |
10 | 10 | import { sha256, sha512 } from 'multiformats/hashes/sha2' |
11 | 11 | import util from 'util' |
12 | 12 | import { Buffer } from 'buffer' |
| 13 | +import invalidMultihash from './fixtures/invalid-multihash.js' |
| 14 | + |
13 | 15 | const test = it |
14 | 16 |
|
15 | | -const same = (x, y) => { |
16 | | - if (x instanceof Uint8Array && y instanceof Uint8Array) { |
17 | | - if (Buffer.compare(Buffer.from(x), Buffer.from(y)) === 0) return |
| 17 | +const same = (actual, expected) => { |
| 18 | + if (actual instanceof Uint8Array && expected instanceof Uint8Array) { |
| 19 | + if (Buffer.compare(Buffer.from(actual), Buffer.from(expected)) === 0) return |
18 | 20 | } |
19 | | - return assert.deepStrictEqual(x, y) |
| 21 | + return assert.deepStrictEqual(actual, expected) |
20 | 22 | } |
21 | 23 |
|
22 | 24 | // eslint-disable-next-line no-unused-vars |
@@ -119,6 +121,35 @@ describe('CID', () => { |
119 | 121 | const newCid = CID.asCID(oldCid) |
120 | 122 | same(newCid.toString(), cidStr) |
121 | 123 | }) |
| 124 | + |
| 125 | + test('inspect bytes', () => { |
| 126 | + const byts = fromHex('1220ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') |
| 127 | + const inspected = CID.inspectBytes(byts.subarray(0, 10)) // should only need the first few bytes |
| 128 | + same({ |
| 129 | + version: 0, |
| 130 | + codec: 0x70, |
| 131 | + multihashCode: 0x12, |
| 132 | + multihashSize: 34, |
| 133 | + digestSize: 32, |
| 134 | + size: 34 |
| 135 | + }, inspected) |
| 136 | + }) |
| 137 | + |
| 138 | + describe('decodeFirst', () => { |
| 139 | + test('no remainder', () => { |
| 140 | + const byts = fromHex('1220ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') |
| 141 | + const [cid, remainder] = CID.decodeFirst(byts) |
| 142 | + same(cid.toString(), 'QmatYkNGZnELf8cAGdyJpUca2PyY4szai3RHyyWofNY1pY') |
| 143 | + same(remainder.byteLength, 0) |
| 144 | + }) |
| 145 | + |
| 146 | + test('remainder', () => { |
| 147 | + const byts = fromHex('1220ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad0102030405') |
| 148 | + const [cid, remainder] = CID.decodeFirst(byts) |
| 149 | + same(cid.toString(), 'QmatYkNGZnELf8cAGdyJpUca2PyY4szai3RHyyWofNY1pY') |
| 150 | + same(toHex(remainder), '0102030405') |
| 151 | + }) |
| 152 | + }) |
122 | 153 | }) |
123 | 154 |
|
124 | 155 | describe('v1', () => { |
@@ -282,6 +313,13 @@ describe('CID', () => { |
282 | 313 | const name = `CID.create(${version}, ${code}, ${mh})` |
283 | 314 | test(name, async () => await testThrowAny(() => CID.create(version, code, hash))) |
284 | 315 | } |
| 316 | + |
| 317 | + test('invalid fixtures', async () => { |
| 318 | + for (const test of invalidMultihash) { |
| 319 | + const buff = fromHex(`0171${test.hex}`) |
| 320 | + assert.throws(() => CID.decode(buff), new RegExp(test.message)) |
| 321 | + } |
| 322 | + }) |
285 | 323 | }) |
286 | 324 |
|
287 | 325 | describe('idempotence', () => { |
@@ -482,6 +520,35 @@ describe('CID', () => { |
482 | 520 | }) |
483 | 521 | }) |
484 | 522 |
|
| 523 | + test('inspect bytes', () => { |
| 524 | + const byts = fromHex('01711220ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') |
| 525 | + const inspected = CID.inspectBytes(byts.subarray(0, 10)) // should only need the first few bytes |
| 526 | + same({ |
| 527 | + version: 1, |
| 528 | + codec: 0x71, |
| 529 | + multihashCode: 0x12, |
| 530 | + multihashSize: 34, |
| 531 | + digestSize: 32, |
| 532 | + size: 36 |
| 533 | + }, inspected) |
| 534 | + |
| 535 | + describe('decodeFirst', () => { |
| 536 | + test('no remainder', () => { |
| 537 | + const byts = fromHex('01711220ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') |
| 538 | + const [cid, remainder] = CID.decodeFirst(byts) |
| 539 | + same(cid.toString(), 'bafyreif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu') |
| 540 | + same(remainder.byteLength, 0) |
| 541 | + }) |
| 542 | + |
| 543 | + test('remainder', () => { |
| 544 | + const byts = fromHex('01711220ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad0102030405') |
| 545 | + const [cid, remainder] = CID.decodeFirst(byts) |
| 546 | + same(cid.toString(), 'bafyreif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu') |
| 547 | + same(toHex(remainder), '0102030405') |
| 548 | + }) |
| 549 | + }) |
| 550 | + }) |
| 551 | + |
485 | 552 | test('new CID from old CID', async () => { |
486 | 553 | const hash = await sha256.digest(Buffer.from('abc')) |
487 | 554 | const cid = CID.asCID(new OLDCID(1, 'raw', Buffer.from(hash.bytes))) |
@@ -527,6 +594,7 @@ describe('CID', () => { |
527 | 594 | const encoded = varint.encodeTo(2, new Uint8Array(32)) |
528 | 595 | await testThrow(() => CID.decode(encoded), 'Invalid CID version 2') |
529 | 596 | }) |
| 597 | + |
530 | 598 | test('buffer', async () => { |
531 | 599 | const hash = await sha256.digest(Buffer.from('abc')) |
532 | 600 | const cid = CID.create(1, 112, hash) |
|
0 commit comments