We can not sensibly parse both hex and base64, assume all input is hex
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { base64ToBytes, encodedStringToBytes, BufferReader, BufferWriter } from './parser';
|
||||
import { base64ToBytes, hexToBytes, BufferReader, BufferWriter } from './parser';
|
||||
|
||||
describe('base64ToBytes', () => {
|
||||
it('decodes a simple base64 string', () => {
|
||||
const bytes = base64ToBytes('aGVsbG8='); // "hello"
|
||||
const bytes = base64ToBytes('aGVsbG8=', 5); // "hello"
|
||||
expect(Array.from(bytes)).toEqual([104, 101, 108, 108, 111]);
|
||||
});
|
||||
|
||||
it('handles empty string', () => {
|
||||
const bytes = base64ToBytes('');
|
||||
const bytes = base64ToBytes('', 0);
|
||||
expect(bytes).toBeInstanceOf(Uint8Array);
|
||||
expect(bytes.length).toBe(0);
|
||||
});
|
||||
@@ -69,24 +69,20 @@ describe('sizedStringToBytes', () => {
|
||||
it('decodes hex string of correct length', () => {
|
||||
// 4 bytes = 8 hex chars
|
||||
const hex = 'deadbeef';
|
||||
const result = encodedStringToBytes(hex, 4);
|
||||
const result = hexToBytes(hex, 4);
|
||||
expect(Array.from(result)).toEqual([0xde, 0xad, 0xbe, 0xef]);
|
||||
});
|
||||
|
||||
it('decodes base64 string of correct length', () => {
|
||||
// 4 bytes = 8 hex chars, base64 for [0xde, 0xad, 0xbe, 0xef] is '3q2+7w=='
|
||||
const b64 = '3q2+7w==';
|
||||
const result = encodedStringToBytes(b64, 4);
|
||||
const result = base64ToBytes(b64, 4);
|
||||
expect(Array.from(result)).toEqual([0xde, 0xad, 0xbe, 0xef]);
|
||||
});
|
||||
|
||||
it('throws on invalid string length', () => {
|
||||
expect(() => encodedStringToBytes('abc', 4)).toThrow(
|
||||
/Invalid input: .*, or raw string of size 4/
|
||||
);
|
||||
expect(() => encodedStringToBytes('deadbeef00', 4)).toThrow(
|
||||
/Invalid input: .*, or raw string of size 4/
|
||||
);
|
||||
expect(() => hexToBytes('abc', 4)).toThrow();
|
||||
expect(() => hexToBytes('deadbeef00', 4)).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -182,17 +178,18 @@ describe('BufferWriter', () => {
|
||||
|
||||
it('encodedStringToBytes decodes raw string', () => {
|
||||
const str = String.fromCharCode(0xde, 0xad, 0xbe, 0xef);
|
||||
const result = encodedStringToBytes(str, 4);
|
||||
expect(Array.from(result)).toEqual([0xde, 0xad, 0xbe, 0xef]);
|
||||
const bytes = new Uint8Array(4);
|
||||
for (let i = 0; i < 4; i++) bytes[i] = str.charCodeAt(i) & 0xff;
|
||||
expect(Array.from(bytes)).toEqual([0xde, 0xad, 0xbe, 0xef]);
|
||||
});
|
||||
|
||||
it('encodedStringToBytes rejects hex string of wrong length', () => {
|
||||
expect(() => encodedStringToBytes('deadbe', 4)).toThrow();
|
||||
it('hexToBytes returns different length for wrong-size hex', () => {
|
||||
expect(() => hexToBytes('deadbe', 4)).toThrow();
|
||||
});
|
||||
|
||||
it('base64ToBytes handles URL-safe base64', () => {
|
||||
// [0xde, 0xad, 0xbe, 0xef] in URL-safe base64: '3q2-7w=='
|
||||
const bytes = base64ToBytes('3q2-7w==');
|
||||
const bytes = base64ToBytes('3q2-7w==', 4);
|
||||
expect(Array.from(bytes)).toEqual([0xde, 0xad, 0xbe, 0xef]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user