Release Helper Tools 0.1.0
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
adler32,
|
||||
cidrContains,
|
||||
digestHex,
|
||||
encodeText,
|
||||
fnv1a32,
|
||||
formatChecksum,
|
||||
formatIpv4,
|
||||
formatIpv6,
|
||||
parseCidr,
|
||||
parseIpAddress,
|
||||
parseIpv4,
|
||||
parseIpv6,
|
||||
crc32,
|
||||
} from "../../src/helpers";
|
||||
|
||||
describe("checksums and cryptographic digests", () => {
|
||||
it("matches published checksum vectors", async () => {
|
||||
const input = encodeText("123456789");
|
||||
expect(formatChecksum(crc32(input))).toBe("cbf43926");
|
||||
expect(formatChecksum(adler32(input))).toBe("091e01de");
|
||||
expect(formatChecksum(fnv1a32(encodeText("hello")))).toBe("4f9f2cab");
|
||||
await expect(digestHex(encodeText("abc"), "SHA-256")).resolves.toBe(
|
||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
|
||||
);
|
||||
});
|
||||
|
||||
it("applies byte ceilings before digest work", async () => {
|
||||
expect(() => crc32(new Uint8Array(2), 1)).toThrow(/limit/u);
|
||||
await expect(digestHex(new Uint8Array(2), "SHA-256", 1)).rejects.toThrow(
|
||||
/limit/u,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("IPv4, IPv6, and CIDR", () => {
|
||||
it("parses strict IPv4 and canonical IPv6", () => {
|
||||
expect(parseIpv4("192.0.2.1")).toMatchObject({
|
||||
version: 4,
|
||||
canonical: "192.0.2.1",
|
||||
value: 3221225985n,
|
||||
});
|
||||
expect(formatIpv4(parseIpv4("203.0.113.4").bytes)).toBe("203.0.113.4");
|
||||
expect(parseIpv6("2001:0db8:0:0:0:ff00:0042:8329").canonical).toBe(
|
||||
"2001:db8::ff00:42:8329",
|
||||
);
|
||||
expect(parseIpv6("::ffff:192.0.2.128").canonical).toBe("::ffff:c000:280");
|
||||
expect(formatIpv6(1n)).toBe("::1");
|
||||
expect(parseIpAddress("::1").version).toBe(6);
|
||||
});
|
||||
|
||||
it("computes exact network ranges and containment", () => {
|
||||
const ipv4 = parseCidr("192.0.2.129/25");
|
||||
expect(ipv4).toMatchObject({
|
||||
canonical: "192.0.2.128/25",
|
||||
size: 128n,
|
||||
});
|
||||
expect(ipv4.first.canonical).toBe("192.0.2.128");
|
||||
expect(ipv4.last.canonical).toBe("192.0.2.255");
|
||||
expect(ipv4.broadcast?.canonical).toBe("192.0.2.255");
|
||||
expect(cidrContains(ipv4, "192.0.2.200")).toBe(true);
|
||||
expect(cidrContains(ipv4, "192.0.3.1")).toBe(false);
|
||||
const ipv6 = parseCidr("2001:db8::1/126");
|
||||
expect(ipv6.canonical).toBe("2001:db8::/126");
|
||||
expect(ipv6.last.canonical).toBe("2001:db8::3");
|
||||
expect(cidrContains(ipv6, "2001:db8::2")).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects ambiguous and malformed addresses", () => {
|
||||
expect(() => parseIpv4("127.00.0.1")).toThrow(/octet/u);
|
||||
expect(() => parseIpv4("256.0.0.1")).toThrow(/255/u);
|
||||
expect(() => parseIpv6("1::2::3")).toThrow(/IPv6/u);
|
||||
expect(() => parseIpv6("fe80::1%eth0")).toThrow(/scoped/u);
|
||||
expect(() => parseIpv6("::192.0.2.1:192.0.2.2")).toThrow(/final/u);
|
||||
expect(() => parseCidr("192.0.2.1/33")).toThrow(/32/u);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user