Skip to content

Commit

Permalink
chore: add test for trailing null byte
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Aug 28, 2023
1 parent c35fa28 commit 42ff49e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
32 changes: 32 additions & 0 deletions test/test-reader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 42ff49e

Please sign in to comment.