import * as l from "../mod.ts"; import { assert, throws } from "./assert.ts"; Deno.test("byte values use standard base64 with optional, correctly formed padding", () => { const check = l.compile(l.bytes); for (const value of ["", "AA", "AA==", "AQI", "AQI=", "AQID", "+/8="]) { assert(check({ $bytes: value }).success, `Rejected base64: ${value}`); } for (const value of ["A", "A=", "AA=", "AA===", "A===", "AQI==", "A A", "-_8=", "AB", "AQJ="]) { assert(!check({ $bytes: value }).success, `Accepted malformed base64: ${value}`); } assert(!check({ $bytes: "AA", extra: true }).success); assert(!check({ $bytes: true }).success); assert(!check(new Uint8Array([1])).success); assert(l.compile(l.bytesWith({ maxLength: 1 }))({ $bytes: "AA==" }).success); assert(!l.compile(l.bytesWith({ minLength: 1 }))({ $bytes: "" }).success); }); Deno.test("objects cannot themselves be compound values", () => { const check = l.compile(l.object({})); assert(!check({ $bytes: "AQI=" }).success); assert(!check({ $type: "blob" }).success); throws(() => l.object({ $bytes: l.string }), "reserved"); }); Deno.test("malformed CID headers are rejected even if their base32 encoding is valid", () => { const check = l.compile(l.cid); // Independently encode binary CID headers to exercise version/codec/hash validation. for ( const header of [[2, 0x71, 0x12, 32], [1, 0x70, 0x12, 32], [1, 0x71, 0x13, 32], [ 1, 0x71, 0x12, 31, ]] ) { const bits = [...header, ...new Array(32).fill(0)].map((n) => n.toString(2).padStart(8, "0") ).join(""); let encoded = "b"; for (let i = 0; i < bits.length; i += 5) { encoded += "abcdefghijklmnopqrstuvwxyz234567"[parseInt(bits.slice(i, i + 5).padEnd(5, "0"), 2)]; } assert(!check(encoded).success); } });