31 lines
955 B
TypeScript
31 lines
955 B
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
base32ToBytes,
|
|
base64UrlToBytes,
|
|
bytesToBase32,
|
|
bytesToBase64Url,
|
|
bytesToHex,
|
|
hexToBytes,
|
|
utf8ToBytes,
|
|
} from "../../src/crypto/encoding";
|
|
|
|
describe("encoding", () => {
|
|
it("round-trips RFC 4648-style encodings", () => {
|
|
const bytes = utf8ToBytes("Hello!\u{1f512}");
|
|
expect([...base32ToBytes(bytesToBase32(bytes))]).toEqual([...bytes]);
|
|
expect([...base64UrlToBytes(bytesToBase64Url(bytes))]).toEqual([...bytes]);
|
|
expect([...hexToBytes(bytesToHex(bytes))]).toEqual([...bytes]);
|
|
});
|
|
|
|
it("rejects non-zero trailing Base32 bits", () => {
|
|
expect(() => base32ToBytes("AB")).toThrow(/trailing bits/iu);
|
|
});
|
|
|
|
it("accepts deliberate manual separators only when enabled", () => {
|
|
expect(() => base32ToBytes("JBSW Y3DP")).toThrow();
|
|
expect([...base32ToBytes("JBSW-Y3DP", { allowSeparators: true })]).toEqual([
|
|
...utf8ToBytes("Hello"),
|
|
]);
|
|
});
|
|
});
|