From 42ff49e0edc3d1a71ab97f40d4a1d7adabf12739 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Mon, 28 Aug 2023 15:47:21 +1000 Subject: [PATCH] chore: add test for trailing null byte --- src/writer.js | 1 + test/test-reader.spec.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/writer.js b/src/writer.js index b2027bb..cee84b4 100644 --- a/src/writer.js +++ b/src/writer.js @@ -63,6 +63,7 @@ export class CarWriter extends BrowserCarWriter { bytes = new Uint8Array(chunkSize) // need a new chunk each time, can't reuse old const read = await readChunk() offset += read + /* eslint no-warning-comments: 0 */ // TODO: test header > 256 bytes return read < chunkSize ? bytes.subarray(0, read) : bytes }) diff --git a/test/test-reader.spec.js b/test/test-reader.spec.js index 928939e..d276804 100644 --- a/test/test-reader.spec.js +++ b/test/test-reader.spec.js @@ -87,6 +87,38 @@ describe('CarReader fromBytes()', () => { } }) + describe('decode error - trailing null bytes', () => { + const cases = [ + { + name: 'carBytes', + bytes: (() => { + const bytes = new Uint8Array(carBytes.length + 5) + bytes.set(carBytes) + return bytes + })() + }, + { + name: 'sample-v1-with-zero-len-section.car', + bytes: base64.baseDecode(fixtures['sample-v1-with-zero-len-section.car']) + }, + { + name: 'sample-v1-with-zero-len-section2.car', + bytes: base64.baseDecode(fixtures['sample-v1-with-zero-len-section2.car']) + } + ] + for (const { name, bytes } of cases) { + it(name, async () => { + try { + await CarReader.fromBytes(bytes) + } catch (/** @type {any} */ err) { + assert.strictEqual(err.message, 'Invalid CAR section (zero length)') + return + } + assert.fail('Did not throw') + }) + } + }) + it('decode error - trailing null bytes', async () => { const bytes = new Uint8Array(carBytes.length + 5) bytes.set(carBytes)