@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 285 lines 9.43 KB
Raw
1 import { describe, expect, test } from "@alchemy.run/test";
2 import { canonicalCidr } from "@/Util/ip-address.ts";
3
4 const valid: [string, string][] = [
5 ["10.0.0.7/16", "10.0.0.0/16"],
6 ["10.0.0.7/016", "10.0.0.0/16"],
7 ["10.0.0.1/024", "10.0.0.0/24"],
8 ["::1/064", "::/64"],
9 ["10.0.0.1/00", "0.0.0.0/0"],
10 ["::1/000", "::/0"],
11 ["192.168.1.255/24", "192.168.1.0/24"],
12 ["172.31.255.255/12", "172.16.0.0/12"],
13 ["192.0.2.129/25", "192.0.2.128/25"],
14 ["192.0.2.131/31", "192.0.2.130/31"],
15 ["192.0.2.131/32", "192.0.2.131/32"],
16 ["255.255.255.255/0", "0.0.0.0/0"],
17 ["255.255.255.255/1", "128.0.0.0/1"],
18 ["255.255.255.255/32", "255.255.255.255/32"],
19 ["0.0.0.0/32", "0.0.0.0/32"],
20 ["::/0", "::/0"],
21 ["::/128", "::/128"],
22 ["::1/128", "::1/128"],
23 ["::1/127", "::/127"],
24 ["FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF/0", "::/0"],
25 ["FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF/1", "8000::/1"],
26 ["ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/127", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe/127"],
27 ["ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"],
28 ["2001:0DB8:0000:0000:0000:0000:0000:0001/128", "2001:db8::1/128"],
29 ["2001:db8:abcd:1234:5678:9abc:def0:1234/64", "2001:db8:abcd:1234::/64"],
30 ["2001:db8:abcd:1234:ffff:ffff:ffff:ffff/65", "2001:db8:abcd:1234:8000::/65"],
31 ["2001:db8:abcd:1234:ffff:ffff:ffff:ffff/63", "2001:db8:abcd:1234::/63"],
32 ["1:0:0:2:0:0:3:4/128", "1::2:0:0:3:4/128"],
33 ["1:0:0:2:0:0:0:4/128", "1:0:0:2::4/128"],
34 ["0:0:1:2:3:4:0:0/128", "::1:2:3:4:0:0/128"],
35 ["1:2:3:4:5:6:0:0/128", "1:2:3:4:5:6::/128"],
36 ["1:2:3:4:5:6:0:8/128", "1:2:3:4:5:6:0:8/128"],
37 ["1:2:3:4:5:6::8/128", "1:2:3:4:5:6:0:8/128"],
38 ["::ffff:192.0.2.129/128", "::ffff:c000:281/128"],
39 ["::ffff:192.0.2.129/120", "::ffff:c000:200/120"],
40 ["::ffff:192.0.2.129/96", "::ffff:0:0/96"],
41 ["::ffff:192.0.2.129/80", "::/80"],
42 ["::192.0.2.129/120", "::c000:200/120"],
43 ["0:0:0:0:0:FFFF:192.0.2.129/128", "::ffff:c000:281/128"],
44 ["1:2:3:4:5:6:192.0.2.1/128", "1:2:3:4:5:6:c000:201/128"],
45 ["1:2:3:4:5::192.0.2.1/128", "1:2:3:4:5:0:c000:201/128"],
46 ];
47
48 const invalid = [
49 "",
50 "10.0.0.1",
51 "::1",
52 "/24",
53 "/128",
54 "10.0.0.1/",
55 "::1/",
56 "10.0.0.1/33",
57 "::1/129",
58 "10.0.0.1/-1",
59 "::1/-1",
60 "10.0.0.1/+24",
61 "::1/+64",
62 "10.0.0.1/24.0",
63 "::1/6.4",
64 "10.0.0.1/2e1",
65 "::1/0x40",
66 "10.0.0.1/NaN",
67 "::1/Infinity",
68 "10.0.0.1/24/1",
69 "::1/64/1",
70 "10.0.0.1//24",
71 "::1//64",
72 "10.0.0/24",
73 "10.0.0.1.2/24",
74 "256.0.0.1/24",
75 "10.256.0.1/24",
76 "10.0.256.1/24",
77 "10.0.0.256/24",
78 "10..0.1/24",
79 ".10.0.1/24",
80 "10.0.1./24",
81 "-1.0.0.1/24",
82 "+1.0.0.1/24",
83 "1e1.0.0.1/24",
84 "0x0a.0.0.1/24",
85 "010.0.0.1/24",
86 "10.00.0.1/24",
87 "10.0.0.01/24",
88 "10.a.0.1/24",
89 "10.0.0.1/24",
90 "localhost/24",
91 "[::1]/128",
92 "fe80::1%eth0/64",
93 "fe80::1%1/64",
94 ":/64",
95 ":::/64",
96 "::::/64",
97 ":1:2:3:4:5:6:7/64",
98 "1:2:3:4:5:6:7:/64",
99 "1::2::3/64",
100 "1:2:3:4:5:6:7/64",
101 "1:2:3:4:5:6:7:8:9/64",
102 "1:2:3:4:5:6:7:8::/64",
103 "::1:2:3:4:5:6:7:8/64",
104 "1:2:3:4::5:6:7:8/64",
105 "1:2:3:4:5:6:12345:8/128",
106 "1:2:3:4:5:6:gggg:8/128",
107 "1:2:3:4:5:6:0x1:8/128",
108 "1:2:3:4:5:6:+1:8/128",
109 "1:2:3:4:5:6:-1:8/128",
110 "::ffff:256.0.0.1/128",
111 "::ffff:192.0.2/128",
112 "::ffff:192.0.2.1.2/128",
113 "::ffff:192.0.02.1/128",
114 "192.0.2.1::/128",
115 "::192.0.2.1:1/128",
116 "1:2:3:4:5:192.0.2.1/128",
117 "1:2:3:4:5:6:7:192.0.2.1/128",
118 "1:2:3:4:5:6::192.0.2.1/128",
119 "::1:2:3:4:5:6:192.0.2.1/128",
120 ];
121
122 const ipv4Bits = (address: string) =>
123 address
124 .split(".")
125 .map((octet) => Number(octet).toString(2).padStart(8, "0"))
126 .join("");
127
128 const ipv6Bits = (address: string) => {
129 const [left, right] = address.split("::");
130 const head = left ? left.split(":") : [];
131 const tail = right ? right.split(":") : [];
132 const words =
133 right === undefined
134 ? head
135 : [...head, ...Array<string>(8 - head.length - tail.length).fill("0"), ...tail];
136 return words.map((word) => parseInt(word, 16).toString(2).padStart(16, "0")).join("");
137 };
138
139 describe("canonicalCidr", { tags: ["unit", "local"] }, () => {
140 test("preserves undefined", () => {
141 expect(canonicalCidr(undefined)).toBeUndefined();
142 });
143
144 for (const [input, expected] of valid) {
145 test(`canonicalizes ${input}`, () => {
146 expect(canonicalCidr(input)).toBe(expected);
147 expect(canonicalCidr(expected)).toBe(expected);
148 });
149 }
150
151 for (const input of invalid) {
152 test(`preserves invalid input ${JSON.stringify(input)}`, () => {
153 expect(canonicalCidr(input)).toBe(input);
154 });
155 }
156
157 test("preserves whitespace rather than interpreting a different address", () => {
158 for (const whitespace of [" ", "\t", "\n", "\r", "\r\n", "\u00a0", "\ufeff"]) {
159 for (const base of ["10.0.0.1/24", "2001:db8::1/64"]) {
160 for (let index = 0; index <= base.length; index++) {
161 const input = base.slice(0, index) + whitespace + base.slice(index);
162 expect(canonicalCidr(input)).toBe(input);
163 }
164 }
165 }
166 });
167
168 test("preserves oversized inputs without throwing", () => {
169 for (const input of [
170 `10.0.0.1/${"9".repeat(1000)}`,
171 `::1/${"9".repeat(1000)}`,
172 `${"1:".repeat(1000)}1/64`,
173 `${"1.".repeat(1000)}1/24`,
174 `${"f".repeat(1000)}::/64`,
175 ])
176 expect(canonicalCidr(input)).toBe(input);
177 });
178
179 test("masks every IPv4 prefix and is idempotent", () => {
180 for (const address of [
181 "0.0.0.0",
182 "255.255.255.255",
183 "170.85.170.85",
184 "85.170.85.170",
185 "192.168.171.205",
186 ]) {
187 const bits = ipv4Bits(address);
188 for (let prefix = 0; prefix <= 32; prefix++) {
189 const result = canonicalCidr(`${address}/${prefix}`)!;
190 const [network, actualPrefix] = result.split("/");
191 expect(actualPrefix).toBe(String(prefix));
192 expect(ipv4Bits(network!)).toBe(bits.slice(0, prefix).padEnd(32, "0"));
193 expect(canonicalCidr(result)).toBe(result);
194 }
195 }
196 });
197
198 test("masks every IPv6 prefix and is idempotent", () => {
199 for (const address of [
200 "0:0:0:0:0:0:0:0",
201 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
202 "aaaa:5555:aaaa:5555:aaaa:5555:aaaa:5555",
203 "5555:aaaa:5555:aaaa:5555:aaaa:5555:aaaa",
204 "2001:db8:abcd:1234:5678:9abc:def0:1234",
205 ]) {
206 const bits = ipv6Bits(address);
207 for (let prefix = 0; prefix <= 128; prefix++) {
208 const result = canonicalCidr(`${address}/${prefix}`)!;
209 const [network, actualPrefix] = result.split("/");
210 expect(actualPrefix).toBe(String(prefix));
211 expect(ipv6Bits(network!)).toBe(bits.slice(0, prefix).padEnd(128, "0"));
212 expect(canonicalCidr(result)).toBe(result);
213 }
214 }
215 });
216
217 test("handles every IPv4 octet value in every position", () => {
218 for (let index = 0; index < 4; index++) {
219 for (let value = 0; value <= 255; value++) {
220 const octets = [1, 2, 3, 4];
221 octets[index] = value;
222 const input = `${octets.join(".")}/32`;
223 expect(canonicalCidr(input)).toBe(input);
224 }
225 }
226 });
227
228 test("handles every 16-bit IPv6 word in every position, including case and leading zeros", () => {
229 for (let index = 0; index < 8; index++) {
230 for (let value = 0; value <= 65535; value++) {
231 const words = ["1", "2", "3", "4", "5", "6", "7", "8"];
232 words[index] = value.toString(16);
233 const expected = `${words.join(":")}/128`;
234 words[index] = value.toString(16).toUpperCase().padStart(4, "0");
235 expect(canonicalCidr(`${words.join(":")}/128`)).toBe(expected);
236 }
237 }
238 });
239
240 test("normalizes every valid position and length of IPv6 zero compression", () => {
241 for (let start = 0; start < 8; start++) {
242 for (let length = 1; length <= 8 - start; length++) {
243 const words = Array.from({ length: 8 }, (_, index) =>
244 index >= start && index < start + length ? "0" : String(index + 1),
245 );
246 const expanded = words.join(":");
247 const compressed = `${words.slice(0, start).join(":")}::${words.slice(start + length).join(":")}`;
248 const expected = `${length === 1 ? expanded : compressed}/128`;
249 expect(canonicalCidr(`${expanded}/128`)).toBe(expected);
250 expect(canonicalCidr(`${compressed}/128`)).toBe(expected);
251 }
252 }
253 });
254
255 test("chooses the first longest zero run across all 256 IPv6 zero/nonzero word patterns", () => {
256 for (let pattern = 0; pattern < 256; pattern++) {
257 const expanded = Array.from({ length: 8 }, (_, index) =>
258 pattern & (1 << index) ? "0" : String(index + 1),
259 ).join(":");
260 const runs = Array.from(expanded.matchAll(/(?:^|:)0(?::0)+(?=:|$)/g))
261 .map((match) => ({
262 start: match.index + (match[0].startsWith(":") ? 1 : 0),
263 text: match[0].replace(/^:/, ""),
264 }))
265 .sort((a, b) => b.text.length - a.text.length);
266 const longest = runs[0];
267 const expected =
268 longest === undefined
269 ? expanded
270 : `${expanded.slice(0, longest.start).replace(/:$/, "")}::${expanded.slice(longest.start + longest.text.length).replace(/^:/, "")}`;
271 expect(canonicalCidr(`${expanded}/128`)).toBe(`${expected}/128`);
272 }
273 });
274
275 test("masks embedded IPv4 addresses identically to their hexadecimal form at every prefix", () => {
276 for (let prefix = 0; prefix <= 128; prefix++) {
277 expect(canonicalCidr(`::ffff:192.0.2.129/${prefix}`)).toBe(
278 canonicalCidr(`0:0:0:0:0:ffff:c000:281/${prefix}`),
279 );
280 expect(canonicalCidr(`1:2:3:4:5:6:192.0.2.129/${prefix}`)).toBe(
281 canonicalCidr(`1:2:3:4:5:6:c000:281/${prefix}`),
282 );
283 }
284 });
285 });