Skip to content

Commit 2d22ffa

Browse files
committed
fix(tests): add decoder tests to handle decoding strings at large offsets
1 parent 04f2615 commit 2d22ffa

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/decoder.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,65 @@ describe('lib/decoder', () => {
352352
assert.deepStrictEqual(decoder.decode(0).value, tc.expected);
353353
});
354354
}
355+
356+
function createBufferWithStringAtOffset(
357+
offset: number,
358+
content: string
359+
): Buffer {
360+
const contentBuf = Buffer.from(content);
361+
const size = contentBuf.length;
362+
const padding = Buffer.alloc(offset);
363+
364+
let header: Buffer;
365+
if (size <= 0x1f) {
366+
header = Buffer.from([0x40 | size]);
367+
} else if (size <= 0xffff) {
368+
header = Buffer.from([0x40 | 0x1f, (size >> 8) & 0xff, size & 0xff]);
369+
} else {
370+
header = Buffer.from([
371+
0x5f,
372+
(size >> 24) & 0xff,
373+
(size >> 16) & 0xff,
374+
(size >> 8) & 0xff,
375+
size & 0xff,
376+
]);
377+
}
378+
379+
return Buffer.concat([padding, header, contentBuf]);
380+
}
381+
382+
it('should handle string just below 2^31 boundary', function () {
383+
this.timeout(15000);
384+
const offset = 0x7ffffff0; // 2^31 - 16
385+
const content = 'boundary test';
386+
387+
const buffer = createBufferWithStringAtOffset(offset, content);
388+
389+
const decoder = new Decoder(buffer);
390+
391+
const result = decoder.decode(offset);
392+
assert.strictEqual(result.value, content);
393+
assert(
394+
result.offset > offset,
395+
`Offset ${result.offset} should be > ${offset}`
396+
);
397+
});
398+
399+
it('should handle when string at offset >= 2^31', function () {
400+
this.timeout(15000);
401+
const offset = 0x80000000; // Exactly 2^31
402+
const content = 'test';
403+
404+
const buffer = createBufferWithStringAtOffset(offset, content);
405+
const decoder = new Decoder(buffer);
406+
407+
const result = decoder.decode(offset);
408+
assert.strictEqual(result.value, content);
409+
assert(
410+
result.offset > offset,
411+
`Offset ${result.offset} should be > ${offset}`
412+
);
413+
});
355414
});
356415

357416
describe('decodeUint() - uint16', () => {

0 commit comments

Comments
 (0)