Setting the file. One moment.
IP Write Test · Clickhouse Js Node Rowbinary · ClickHouse/agent-skills · Skills Docs
ContentsBack to the top of the page (opens in a new tab)
tests/ IP.write.test.ts
TypeScript · 75 lines · 3 KB
"../src/writers/ip.js"
;
11
12 describe ( "writeIPv4" , () => {
13 it ( "encodes 0.0.0.0" , async () =>
14 expect ( encode (writeIPv4, parseIPv4 ( "0.0.0.0" ))). toEqual (
15 await query ( "SELECT toIPv4('0.0.0.0') FORMAT RowBinary" ),
16 ));
17 it ( "encodes 1.2.3.4" , async () =>
18 expect ( encode (writeIPv4, parseIPv4 ( "1.2.3.4" ))). toEqual (
19 await query ( "SELECT toIPv4('1.2.3.4') FORMAT RowBinary" ),
20 ));
21 it ( "encodes 192.168.0.1" , async () =>
22 expect ( encode (writeIPv4, parseIPv4 ( "192.168.0.1" ))). toEqual (
23 await query ( "SELECT toIPv4('192.168.0.1') FORMAT RowBinary" ),
24 ));
25 it ( "encodes 255.255.255.255" , async () =>
26 expect ( encode (writeIPv4, parseIPv4 ( "255.255.255.255" ))). toEqual (
27 await query ( "SELECT toIPv4('255.255.255.255') FORMAT RowBinary" ),
28 ));
29 });
30
31 describe ( "parseIPv4" , () => {
32 it ( "packs the dotted quad big-endian" , () =>
33 expect ( parseIPv4 ( "1.2.3.4" )). toBe ( 0x01020304 ));
34 it ( "rejects an out-of-range octet" , () =>
35 expect (() => parseIPv4 ( "1.2.3.256" )). toThrow (RangeError));
36 });
37
38 describe ( "writeIPv6" , () => {
39 it ( "encodes ::" , async () =>
40 expect ( encode (writeIPv6, parseIPv6 ( "::" ))). toEqual (
41 await query ( "SELECT toIPv6('::') FORMAT RowBinary" ),
42 ));
43 it ( "encodes ::1" , async () =>
44 expect ( encode (writeIPv6, parseIPv6 ( "::1" ))). toEqual (
45 await query ( "SELECT toIPv6('::1') FORMAT RowBinary" ),
46 ));
47 it ( "encodes 2001:db8::1" , async () =>
48 expect ( encode (writeIPv6, parseIPv6 ( "2001:db8::1" ))). toEqual (
49 await query ( "SELECT toIPv6('2001:db8::1') FORMAT RowBinary" ),
50 ));
51 it ( "encodes fe80::a6:6ad3:dba0:1" , async () =>
52 expect ( encode (writeIPv6, parseIPv6 ( "fe80::a6:6ad3:dba0:1" ))). toEqual (
53 await query ( "SELECT toIPv6('fe80::a6:6ad3:dba0:1') FORMAT RowBinary" ),
54 ));
55 it ( "encodes the IPv4-mapped ::ffff:1.2.3.4" , async () =>
56 expect ( encode (writeIPv6, parseIPv6 ( "::ffff:1.2.3.4" ))). toEqual (
57 await query ( "SELECT toIPv6('::ffff:1.2.3.4') FORMAT RowBinary" ),
58 ));
59
60 it ( "rejects non-16-byte input" , () =>
61 expect (() =>
62 writeIPv6 ( new Sink (Buffer. allocUnsafe ( 16 )), Buffer. alloc ( 4 )),
63 ). toThrow (RangeError));
64 });
65
66 describe ( "parseIPv6 validation" , () => {
67 it ( "rejects a non-hex group instead of silently encoding 0" , () =>
68 expect (() => parseIPv6 ( "2001:db8::gggg" )). toThrow (RangeError));
69 it ( "rejects a negative group instead of wrapping to 0xffff" , () =>
70 expect (() => parseIPv6 ( "2001:db8::-1" )). toThrow (RangeError));
71 it ( "rejects an over-long (5-digit) group" , () =>
72 expect (() => parseIPv6 ( "2001:db8::12345" )). toThrow (RangeError));
73 it ( "rejects an empty group" , () =>
74 expect (() => parseIPv6 ( "2001:db8:::1" )). toThrow (RangeError));
75 });