ipaddress 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/dist/src/crunchy.d.ts +130 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +616 -0
  4. package/dist/src/index.d.ts +13 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +21 -0
  7. package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -7
  8. package/dist/src/ip_bits.d.ts.map +1 -0
  9. package/dist/src/ip_bits.js +133 -0
  10. package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
  11. package/dist/src/ip_version.d.ts.map +1 -0
  12. package/dist/src/ip_version.js +6 -0
  13. package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +38 -35
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1243 -0
  16. package/dist/src/ipv4.d.ts +15 -0
  17. package/dist/src/ipv4.d.ts.map +1 -0
  18. package/dist/src/ipv4.js +187 -0
  19. package/dist/src/ipv6.d.ts +13 -0
  20. package/dist/src/ipv6.d.ts.map +1 -0
  21. package/dist/src/ipv6.js +190 -0
  22. package/dist/src/ipv6_loopback.d.ts +6 -0
  23. package/dist/src/ipv6_loopback.d.ts.map +1 -0
  24. package/dist/src/ipv6_loopback.js +51 -0
  25. package/dist/src/ipv6_mapped.d.ts +6 -0
  26. package/dist/src/ipv6_mapped.d.ts.map +1 -0
  27. package/dist/src/ipv6_mapped.js +159 -0
  28. package/dist/src/ipv6_unspec.d.ts +6 -0
  29. package/dist/src/ipv6_unspec.d.ts.map +1 -0
  30. package/dist/src/ipv6_unspec.js +57 -0
  31. package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
  32. package/dist/src/prefix.d.ts.map +1 -0
  33. package/dist/src/prefix.js +142 -0
  34. package/dist/src/prefix128.d.ts +7 -0
  35. package/dist/src/prefix128.d.ts.map +1 -0
  36. package/dist/src/prefix128.js +39 -0
  37. package/dist/src/prefix32.d.ts +7 -0
  38. package/dist/src/prefix32.d.ts.map +1 -0
  39. package/dist/src/prefix32.js +29 -0
  40. package/dist/src/rle.d.ts +19 -0
  41. package/dist/src/rle.d.ts.map +1 -0
  42. package/dist/src/rle.js +73 -0
  43. package/dist/test/test_crunchy_impls.d.ts +2 -0
  44. package/dist/test/test_crunchy_impls.d.ts.map +1 -0
  45. package/dist/test/test_crunchy_impls.js +150 -0
  46. package/dist/test/test_crunchy_with32bit.d.ts +2 -0
  47. package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
  48. package/dist/test/test_crunchy_with32bit.js +382 -0
  49. package/dist/test/test_ipaddress.d.ts +2 -0
  50. package/dist/test/test_ipaddress.d.ts.map +1 -0
  51. package/dist/test/test_ipaddress.js +182 -0
  52. package/dist/test/test_ipv4.d.ts +2 -0
  53. package/dist/test/test_ipv4.d.ts.map +1 -0
  54. package/dist/test/test_ipv4.js +561 -0
  55. package/dist/test/test_ipv6.d.ts +2 -0
  56. package/dist/test/test_ipv6.d.ts.map +1 -0
  57. package/dist/test/test_ipv6.js +313 -0
  58. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  59. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_loopback.js +40 -0
  61. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  62. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_mapped.js +89 -0
  64. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  65. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  66. package/dist/test/test_ipv6_unspec.js +39 -0
  67. package/dist/test/test_prefix128.d.ts +2 -0
  68. package/dist/test/test_prefix128.d.ts.map +1 -0
  69. package/dist/test/test_prefix128.js +35 -0
  70. package/dist/test/test_prefix32.d.ts +2 -0
  71. package/dist/test/test_prefix32.d.ts.map +1 -0
  72. package/dist/test/test_prefix32.js +126 -0
  73. package/dist/test/test_rle.d.ts +2 -0
  74. package/dist/test/test_rle.d.ts.map +1 -0
  75. package/dist/test/test_rle.js +141 -0
  76. package/dist/vitest.config.d.ts +3 -0
  77. package/dist/vitest.config.d.ts.map +1 -0
  78. package/dist/vitest.config.js +11 -0
  79. package/package.json +41 -37
  80. package/src/crunchy.ts +731 -0
  81. package/src/index.ts +23 -0
  82. package/src/ip_bits.ts +148 -0
  83. package/src/ip_version.ts +6 -0
  84. package/src/ipaddress.ts +1347 -0
  85. package/src/ipv4.ts +203 -0
  86. package/src/ipv6.ts +202 -0
  87. package/src/ipv6_loopback.ts +55 -0
  88. package/src/ipv6_mapped.ts +163 -0
  89. package/src/ipv6_unspec.ts +59 -0
  90. package/src/prefix.ts +170 -0
  91. package/src/prefix128.ts +42 -0
  92. package/src/prefix32.ts +32 -0
  93. package/src/rle.ts +85 -0
  94. package/test/test_crunchy_impls.ts +179 -0
  95. package/test/test_crunchy_with32bit.ts +510 -0
  96. package/test/test_ipaddress.ts +208 -0
  97. package/test/test_ipv4.ts +596 -0
  98. package/test/test_ipv6.ts +325 -0
  99. package/test/test_ipv6_loopback.ts +52 -0
  100. package/test/test_ipv6_mapped.ts +101 -0
  101. package/test/test_ipv6_unspec.ts +51 -0
  102. package/test/test_prefix128.ts +39 -0
  103. package/test/test_prefix32.ts +130 -0
  104. package/test/test_rle.ts +144 -0
  105. package/LICENSE +0 -20
  106. package/README.md +0 -960
  107. package/crunchy.d.ts +0 -55
  108. package/index.d.ts +0 -12
  109. package/ipaddress.js +0 -2869
  110. package/ipaddress.js.map +0 -1
  111. package/ipv4.d.ts +0 -14
  112. package/ipv6.d.ts +0 -12
  113. package/ipv6_loopback.d.ts +0 -5
  114. package/ipv6_mapped.d.ts +0 -5
  115. package/ipv6_unspec.d.ts +0 -5
  116. package/prefix128.d.ts +0 -6
  117. package/prefix32.d.ts +0 -6
  118. package/rle.d.ts +0 -14
@@ -0,0 +1,325 @@
1
+ import IPAddress from "../src/ipaddress.js";
2
+ // import Prefix128 from '../src/prefix128';
3
+ import Ipv6 from "../src/ipv6.js";
4
+ import Crunchy from "../src/crunchy.js";
5
+
6
+ class IPv6Test {
7
+ compress_addr: [string, string][] = [];
8
+ valid_ipv6: [string, Crunchy][] = [];
9
+ invalid_ipv6: string[] = [":1:2:3:4:5:6:7", ":1:2:3:4:5:6:7", "2002:516:2:200", "dd"];
10
+ networks: [string, string][] = [];
11
+ ip: IPAddress = IPAddress.parse("2001:db8::8:800:200c:417a/64")!;
12
+ network: IPAddress = IPAddress.parse("2001:db8:8:800::/64")!;
13
+ arr: number[] = [8193, 3512, 0, 0, 8, 2048, 8204, 16762];
14
+ hex = "20010db80000000000080800200c417a";
15
+ }
16
+
17
+ describe("ipv6", () => {
18
+ function assertArrayEqual<T>(a: T[], b: T[]): void {
19
+ assert.equal(a.length, b.length, "length missmatch");
20
+ for (let i = 0; i < a.length; ++i) {
21
+ assert.equal(a[i], b[i]);
22
+ }
23
+ }
24
+
25
+ function setup(): IPv6Test {
26
+ const ip6t = new IPv6Test();
27
+ ip6t.compress_addr.push(["2001:db8:0000:0000:0008:0800:200c:417a", "2001:db8::8:800:200c:417a"]);
28
+ ip6t.compress_addr.push(["2001:db8:0:0:8:800:200c:417a", "2001:db8.8:800:200c:417a"]);
29
+ ip6t.compress_addr.push(["ff01:0:0:0:0:0:0:101", "ff01::101"]);
30
+ ip6t.compress_addr.push(["0:0:0:0:0:0:0:1", ".1"]);
31
+ ip6t.compress_addr.push(["0:0:0:0:0:0:0:0", "."]);
32
+
33
+ ip6t.valid_ipv6.push(["FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", Crunchy.parse("338770000845734292534325025077361652240")]);
34
+ ip6t.valid_ipv6.push(["1080:0000:0000:0000:0008:0800:200C:417A", Crunchy.parse("21932261930451111902915077091070067066")]);
35
+ ip6t.valid_ipv6.push(["1080:0:0:0:8:800:200C:417A", Crunchy.parse("21932261930451111902915077091070067066")]);
36
+ ip6t.valid_ipv6.push(["1080:0::8:800:200C:417A", Crunchy.parse("21932261930451111902915077091070067066")]);
37
+ ip6t.valid_ipv6.push(["1080::8:800:200C:417A", Crunchy.parse("21932261930451111902915077091070067066")]);
38
+ ip6t.valid_ipv6.push(["FF01:0:0:0:0:0:0:43", Crunchy.parse("338958331222012082418099330867817087043")]);
39
+ ip6t.valid_ipv6.push(["FF01:0::0:0:43", Crunchy.parse("338958331222012082418099330867817087043")]);
40
+ ip6t.valid_ipv6.push(["FF01::43", Crunchy.parse("338958331222012082418099330867817087043")]);
41
+ ip6t.valid_ipv6.push(["0:0:0:0:0:0:0:1", Crunchy.parse("1")]);
42
+ ip6t.valid_ipv6.push(["0:0:0::0:0:1", Crunchy.parse("1")]);
43
+ ip6t.valid_ipv6.push(["::1", Crunchy.parse("1")]);
44
+ ip6t.valid_ipv6.push(["0:0:0:0:0:0:0:0", Crunchy.parse("0")]);
45
+ ip6t.valid_ipv6.push(["0:0:0::0:0:0", Crunchy.parse("0")]);
46
+ ip6t.valid_ipv6.push(["::", Crunchy.parse("0")]);
47
+ ip6t.valid_ipv6.push(["::/0", Crunchy.parse("0")]);
48
+ ip6t.valid_ipv6.push(["1080:0:0:0:8:800:200C:417A", Crunchy.parse("21932261930451111902915077091070067066")]);
49
+ ip6t.valid_ipv6.push(["1080::8:800:200C:417A", Crunchy.parse("21932261930451111902915077091070067066")]);
50
+
51
+ ip6t.networks.push(["2001:db8:1:1:1:1:1:1/32", "2001:db8::/32"]);
52
+ ip6t.networks.push(["2001:db8:1:1:1:1:1::/32", "2001:db8::/32"]);
53
+ ip6t.networks.push(["2001:db8::1/64", "2001:db8::/64"]);
54
+ return ip6t;
55
+ }
56
+
57
+ it("test_attribute_address", () => {
58
+ const addr = "2001:0db8:0000:0000:0008:0800:200c:417a";
59
+ assert.equal(addr, setup().ip.to_s_uncompressed());
60
+ });
61
+ it("test_initialize", () => {
62
+ assert.equal(false, setup().ip.is_ipv4());
63
+
64
+ for (const ip of setup().valid_ipv6) {
65
+ assert.isOk(IPAddress.parse(ip[0]));
66
+ }
67
+ for (const ip of setup().invalid_ipv6) {
68
+ assert.isNotOk(IPAddress.parse(ip));
69
+ }
70
+ assert.equal(64, setup().ip.prefix.num);
71
+
72
+ assert.isOk(IPAddress.parse("::10.1.1.1"));
73
+ });
74
+ it("test_attribute_groups", () => {
75
+ assertArrayEqual(setup().arr, setup().ip.parts());
76
+ });
77
+ it("test_method_hexs", () => {
78
+ assertArrayEqual(setup().ip.parts_hex_str(), ["2001", "0db8", "0000", "0000", "0008", "0800", "200c", "417a"]);
79
+ });
80
+
81
+ it("test_method_to_i", () => {
82
+ for (const i of setup().valid_ipv6) {
83
+ const ip = i[0];
84
+ const num = i[1];
85
+ assert.isOk(num.eq(IPAddress.parse(ip)!.host_address));
86
+ }
87
+ });
88
+ it("test_method_set_prefix", () => {
89
+ const ip = IPAddress.parse("2001:db8::8:800:200c:417a");
90
+ assert.equal(128, ip!.prefix.num);
91
+ assert.equal("2001:db8::8:800:200c:417a/128", ip!.to_string());
92
+ const nip = ip!.change_prefix(64);
93
+ assert.equal(64, nip!.prefix.num);
94
+ assert.equal("2001:db8::8:800:200c:417a/64", nip!.to_string());
95
+ });
96
+ it("test_method_mapped", () => {
97
+ assert.equal(false, setup().ip.is_mapped());
98
+ const ip6 = IPAddress.parse("::ffff:1234:5678");
99
+ assert.equal(true, ip6!.is_mapped());
100
+ });
101
+ it("test_method_group", () => {
102
+ const s = setup();
103
+ assertArrayEqual(s.ip.parts(), s.arr);
104
+ });
105
+ it("test_method_ipv4()", () => {
106
+ assert.equal(false, setup().ip.is_ipv4());
107
+ });
108
+ it("test_method_ipv6", () => {
109
+ assert.equal(true, setup().ip.is_ipv6());
110
+ });
111
+ it("test_method_network_known", () => {
112
+ assert.equal(true, setup().network.is_network());
113
+ assert.equal(false, setup().ip.is_network());
114
+ });
115
+ it("test_method_network_u128", () => {
116
+ assert.isOk(Ipv6.from_int(Crunchy.parse("42540766411282592856903984951653826560"), 64)!.eq(setup().ip.network()));
117
+ });
118
+ it("test_method_broadcast_u128", () => {
119
+ assert.isOk(Ipv6.from_int(Crunchy.parse("42540766411282592875350729025363378175"), 64)!.eq(setup().ip.broadcast()));
120
+ });
121
+ it("test_method_size", () => {
122
+ let ip = IPAddress.parse("2001:db8::8:800:200c:417a/64");
123
+ assert.isOk(Crunchy.one().shl(64).eq(ip!.size()));
124
+ ip = IPAddress.parse("2001:db8::8:800:200c:417a/32");
125
+ assert.isOk(Crunchy.one().shl(96).eq(ip!.size()));
126
+ ip = IPAddress.parse("2001:db8::8:800:200c:417a/120");
127
+ assert.isOk(Crunchy.one().shl(8).eq(ip!.size()));
128
+ ip = IPAddress.parse("2001:db8::8:800:200c:417a/124");
129
+ assert.isOk(Crunchy.one().shl(4).eq(ip!.size()));
130
+ });
131
+ it("test_method_includes", () => {
132
+ const ip = setup().ip;
133
+ assert.equal(true, ip.includes(ip));
134
+ // test prefix on same address
135
+ let included = IPAddress.parse("2001:db8::8:800:200c:417a/128");
136
+ let not_included = IPAddress.parse("2001:db8::8:800:200c:417a/46");
137
+ assert.equal(true, ip.includes(included!));
138
+ assert.equal(false, ip.includes(not_included!));
139
+ // test address on same prefix
140
+ included = IPAddress.parse("2001:db8::8:800:200c:0/64");
141
+ not_included = IPAddress.parse("2001:db8:1::8:800:200c:417a/64");
142
+ assert.equal(true, ip.includes(included!));
143
+ assert.equal(false, ip.includes(not_included!));
144
+ // general test
145
+ included = IPAddress.parse("2001:db8::8:800:200c:1/128");
146
+ not_included = IPAddress.parse("2001:db8:1::8:800:200c:417a/76");
147
+ assert.equal(true, ip.includes(included!));
148
+ assert.equal(false, ip.includes(not_included!));
149
+ });
150
+ it("test_method_to_hex", () => {
151
+ assert(setup().hex, setup().ip.to_hex());
152
+ });
153
+ it("test_method_to_s", () => {
154
+ assert.equal("2001:db8::8:800:200c:417a", setup().ip.to_s());
155
+ });
156
+ it("test_method_to_string", () => {
157
+ assert.equal("2001:db8::8:800:200c:417a/64", setup().ip.to_string());
158
+ });
159
+ it("test_method_to_string_uncompressed", () => {
160
+ const str = "2001:0db8:0000:0000:0008:0800:200c:417a/64";
161
+ assert.equal(str, setup().ip.to_string_uncompressed());
162
+ });
163
+ it("test_method_reverse", () => {
164
+ const str = "f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa";
165
+ assert.equal(str, IPAddress.parse("3ffe:505:2::f")!.dns_reverse());
166
+ });
167
+ it("test_method_dns_rev_domains", () => {
168
+ assertArrayEqual(IPAddress.parse("f000:f100::/3")!.dns_rev_domains(), ["e.ip6.arpa", "f.ip6.arpa"]);
169
+ assertArrayEqual(IPAddress.parse("fea3:f120::/15")!.dns_rev_domains(), ["2.a.e.f.ip6.arpa", "3.a.e.f.ip6.arpa"]);
170
+ assertArrayEqual(IPAddress.parse("3a03:2f80:f::/48")!.dns_rev_domains(), ["f.0.0.0.0.8.f.2.3.0.a.3.ip6.arpa"]);
171
+
172
+ assertArrayEqual(IPAddress.parse("f000:f100::1234/125")!.dns_rev_domains(), [
173
+ "0.3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.f.0.0.0.f.ip6.arpa",
174
+ "1.3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.f.0.0.0.f.ip6.arpa",
175
+ "2.3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.f.0.0.0.f.ip6.arpa",
176
+ "3.3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.f.0.0.0.f.ip6.arpa",
177
+ "4.3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.f.0.0.0.f.ip6.arpa",
178
+ "5.3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.f.0.0.0.f.ip6.arpa",
179
+ "6.3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.f.0.0.0.f.ip6.arpa",
180
+ "7.3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.f.0.0.0.f.ip6.arpa",
181
+ ]);
182
+ });
183
+ it("test_method_compressed", () => {
184
+ assert.equal("1:1:1::1", IPAddress.parse("1:1:1:0:0:0:0:1")!.to_s());
185
+ assert.equal("1:0:1::1", IPAddress.parse("1:0:1:0:0:0:0:1")!.to_s());
186
+ assert.equal("1::1:1:1:2:3:1", IPAddress.parse("1:0:1:1:1:2:3:1")!.to_s());
187
+ assert.equal("1::1:1:0:2:3:1", IPAddress.parse("1:0:1:1::2:3:1")!.to_s());
188
+ assert.equal("1:0:0:1::1", IPAddress.parse("1:0:0:1:0:0:0:1")!.to_s());
189
+ assert.equal("1::1:0:0:1", IPAddress.parse("1:0:0:0:1:0:0:1")!.to_s());
190
+ assert.equal("1::1", IPAddress.parse("1:0:0:0:0:0:0:1")!.to_s());
191
+ // assert.equal("1:1.1:2:0:0:1", IPAddress.parse("1:1:0:1:2.1").to_s
192
+ });
193
+ it("test_method_unspecified", () => {
194
+ assert.equal(true, IPAddress.parse("::")!.is_unspecified());
195
+ assert.equal(false, setup().ip.is_unspecified());
196
+ });
197
+ it("test_method_loopback", () => {
198
+ assert.equal(true, IPAddress.parse("::1")!.is_loopback());
199
+ assert.equal(false, setup().ip.is_loopback());
200
+ });
201
+ it("test_method_network", () => {
202
+ for (const i of setup().networks) {
203
+ const addr = i[0];
204
+ const net = i[1];
205
+ const ip = IPAddress.parse(addr);
206
+ assert.equal(net, ip!.network().to_string());
207
+ }
208
+ });
209
+ it("test_method_each", () => {
210
+ const ip = IPAddress.parse("2001:db8::4/125");
211
+ const arr: string[] = [];
212
+ ip!.each((i) => arr.push(i.to_s()));
213
+ assertArrayEqual(arr, [
214
+ "2001:db8::",
215
+ "2001:db8::1",
216
+ "2001:db8::2",
217
+ "2001:db8::3",
218
+ "2001:db8::4",
219
+ "2001:db8::5",
220
+ "2001:db8::6",
221
+ "2001:db8::7",
222
+ ]);
223
+ });
224
+ it("test_method_each_net", function () {
225
+ // this.timeout(30000);
226
+ const test_addrs = [
227
+ "0000:0000:0000:0000:0000:0000:0000:0000",
228
+ "1111:1111:1111:1111:1111:1111:1111:1111",
229
+ "2222:2222:2222:2222:2222:2222:2222:2222",
230
+ "3333:3333:3333:3333:3333:3333:3333:3333",
231
+ "4444:4444:4444:4444:4444:4444:4444:4444",
232
+ "5555:5555:5555:5555:5555:5555:5555:5555",
233
+ "6666:6666:6666:6666:6666:6666:6666:6666",
234
+ "7777:7777:7777:7777:7777:7777:7777:7777",
235
+ "8888:8888:8888:8888:8888:8888:8888:8888",
236
+ "9999:9999:9999:9999:9999:9999:9999:9999",
237
+ "aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa",
238
+ "bbbb:bbbb:bbbb:bbbb:bbbb:bbbb:bbbb:bbbb",
239
+ "cccc:cccc:cccc:cccc:cccc:cccc:cccc:cccc",
240
+ "dddd:dddd:dddd:dddd:dddd:dddd:dddd:dddd",
241
+ "eeee:eeee:eeee:eeee:eeee:eeee:eeee:eeee",
242
+ "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
243
+ ];
244
+ for (let prefix = 0; prefix < 128; ++prefix) {
245
+ const nr_networks = 1 << ((128 - prefix) % 4);
246
+ for (const adr of test_addrs) {
247
+ const net_adr = IPAddress.parse(`${adr}/${prefix}`);
248
+ const ret = net_adr!.dns_networks();
249
+ assert.equal(ret[0].prefix.num % 4, 0);
250
+ assert.equal(ret.length, nr_networks);
251
+ assert.equal(net_adr!.network().to_s(), ret[0].network().to_s());
252
+ assert.equal(net_adr!.broadcast().to_s(), ret[ret.length - 1].broadcast().to_s());
253
+ // puts "//{adr}///{prefix} //{nr_networks} //{ret}"
254
+ }
255
+ }
256
+ const ret0: string[] = [];
257
+ for (const i of IPAddress.parse("fd01:db8::4/3")!.dns_networks()) {
258
+ ret0.push(i.to_string());
259
+ }
260
+ assertArrayEqual(ret0, ["e000::/4", "f000::/4"]);
261
+ const ret1: string[] = [];
262
+ for (const i of IPAddress.parse("3a03:2f80:f::/48")!.dns_networks()) {
263
+ ret1.push(i.to_string());
264
+ }
265
+ assertArrayEqual(ret1, ["3a03:2f80:f::/48"]);
266
+ }, 10000);
267
+ it("test_method_compare", () => {
268
+ const ip1 = IPAddress.parse("2001:db8:1::1/64");
269
+ const ip2 = IPAddress.parse("2001:db8:2::1/64");
270
+ const ip3 = IPAddress.parse("2001:db8:1::2/64");
271
+ const ip4 = IPAddress.parse("2001:db8:1::1/65");
272
+
273
+ // ip2 should be greater than ip1
274
+ assert.equal(true, ip2!.gt(ip1!));
275
+ assert.equal(false, ip1!.gt(ip2!));
276
+ assert.equal(false, ip2!.lt(ip1!));
277
+ // ip3 should be less than ip2
278
+ assert.equal(true, ip2!.gt(ip3!));
279
+ assert.equal(false, ip2!.lt(ip3!));
280
+ // ip1 should be less than ip3
281
+ assert.equal(true, ip1!.lt(ip3!));
282
+ assert.equal(false, ip1!.gt(ip3!));
283
+ assert.equal(false, ip3!.lt(ip1!));
284
+ // ip1 should be equal to itself
285
+ assert.equal(true, ip1!.eq(ip1!));
286
+ // ip4 should be greater than ip1
287
+ assert.equal(true, ip1!.lt(ip4!));
288
+ assert.equal(false, ip1!.gt(ip4!));
289
+ // test sorting
290
+ const r = [ip1, ip2, ip3, ip4].sort((a, b) => a!.cmp(b!));
291
+ const ret: string[] = [];
292
+ for (const i of r) {
293
+ ret.push(i!.to_string());
294
+ }
295
+ assertArrayEqual(ret, ["2001:db8:1::1/64", "2001:db8:1::1/65", "2001:db8:1::2/64", "2001:db8:2::1/64"]);
296
+ });
297
+
298
+ // it("test_classmethod_expand", () => {
299
+ // let compressed = "2001:db8:0:cd30.";
300
+ // let expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000";
301
+ // assert.equal(expanded, @klass.expand(compressed));
302
+ // assert.equal(expanded, @klass.expand("2001:0db8:0.cd3"));
303
+ // assert.equal(expanded, @klass.expand("2001:0db8.cd30"));
304
+ // assert.equal(expanded, @klass.expand("2001:0db8.cd3"));
305
+ // }
306
+ it("test_classmethod_compress", () => {
307
+ const compressed = "2001:db8:0:cd30::";
308
+ const expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000";
309
+ assert.equal(compressed, IPAddress.parse(expanded)!.to_s());
310
+ assert.equal("2001:db8::cd3", IPAddress.parse("2001:0db8:0::cd3")!.to_s());
311
+ assert.equal("2001:db8::cd30", IPAddress.parse("2001:0db8::cd30")!.to_s());
312
+ assert.equal("2001:db8::cd3", IPAddress.parse("2001:0db8::cd3")!.to_s());
313
+ });
314
+ it("test_classhmethod_parse_u128", () => {
315
+ for (const i of setup().valid_ipv6) {
316
+ const ip = i[0];
317
+ const num = i[1];
318
+ // console.log(">>>>>>>>", i);
319
+ assert.equal(IPAddress.parse(ip)!.to_s(), Ipv6.from_int(num, 128)!.to_s());
320
+ }
321
+ });
322
+ it("test_classmethod_parse_hex", () => {
323
+ assert.equal(setup().ip.to_string(), Ipv6.from_str(setup().hex, 16, 64)!.to_string());
324
+ });
325
+ });
@@ -0,0 +1,52 @@
1
+ import IPAddress from "../src/ipaddress.js";
2
+ // import Prefix128 from '../src/prefix128';
3
+ import Ipv6Loopback from "../src/ipv6_loopback.js";
4
+ import Crunchy from "../src/crunchy.js";
5
+
6
+ interface IPv6LoopbackTestParams {
7
+ ip: IPAddress;
8
+ s: string;
9
+ n: string;
10
+ string: string;
11
+ one: Crunchy;
12
+ }
13
+
14
+ class IPv6LoopbackTest {
15
+ ip: IPAddress;
16
+ s: string;
17
+ n: string;
18
+ string: string;
19
+ one: Crunchy;
20
+ constructor(obj: IPv6LoopbackTestParams) {
21
+ this.ip = obj.ip;
22
+ this.s = obj.s;
23
+ this.n = obj.n;
24
+ this.string = obj.string;
25
+ this.one = obj.one;
26
+ }
27
+ }
28
+
29
+ describe("ipv6_loopback", () => {
30
+ function setup(): IPv6LoopbackTest {
31
+ return new IPv6LoopbackTest({
32
+ ip: Ipv6Loopback.create()!,
33
+ s: "::1",
34
+ n: "::1/128",
35
+ string: "0000:0000:0000:0000:0000:0000:0000:0001/128",
36
+ one: Crunchy.one(),
37
+ });
38
+ }
39
+
40
+ it("test_attributes", () => {
41
+ const s = setup();
42
+ assert.equal(128, s.ip.prefix.num);
43
+ assert.equal(true, s.ip.is_loopback());
44
+ assert.equal(s.s, s.ip.to_s());
45
+ assert.equal(s.n, s.ip.to_string());
46
+ assert.equal(s.string, s.ip.to_string_uncompressed());
47
+ assert.equal(s.one.toString(), s.ip.host_address.toString());
48
+ });
49
+ it("test_method_ipv6", () => {
50
+ assert.equal(true, setup().ip.is_ipv6());
51
+ });
52
+ });
@@ -0,0 +1,101 @@
1
+ import IPAddress from "../src/ipaddress.js";
2
+ // import Prefix128 from '../src/prefix128';
3
+ // import Ipv6Unspec from '../src/ipv6_unspec';
4
+ import Ipv6Mapped from "../src/ipv6_mapped.js";
5
+ import Crunchy from "../src/crunchy.js";
6
+
7
+ interface IPv6MappedTestParams {
8
+ ip: IPAddress;
9
+ s: string;
10
+ sstr: string;
11
+ string: string;
12
+ u128: Crunchy;
13
+ address: string;
14
+ }
15
+
16
+ class IPv6MappedTest {
17
+ ip: IPAddress;
18
+ s: string;
19
+ sstr: string;
20
+ string: string;
21
+ u128: Crunchy;
22
+ address: string;
23
+ valid_mapped: [string, Crunchy][] = [];
24
+ valid_mapped_ipv6: [string, Crunchy][] = [];
25
+ valid_mapped_ipv6_conversion: [string, string][] = [];
26
+ constructor(obj: IPv6MappedTestParams) {
27
+ this.ip = obj.ip;
28
+ this.s = obj.s;
29
+ this.sstr = obj.sstr;
30
+ this.string = obj.string;
31
+ this.u128 = obj.u128;
32
+ this.address = obj.address;
33
+ }
34
+ }
35
+
36
+ describe("ipv6_mapped", () => {
37
+ function setup(): IPv6MappedTest {
38
+ const ipv6 = new IPv6MappedTest({
39
+ ip: Ipv6Mapped.create("::172.16.10.1")!,
40
+ s: "::ffff:172.16.10.1",
41
+ sstr: "::ffff:172.16.10.1/32",
42
+ string: "0000:0000:0000:0000:0000:ffff:ac10:0a01/128",
43
+ u128: Crunchy.parse("281473568475649"),
44
+ address: "::ffff:ac10:a01/128",
45
+ });
46
+ ipv6.valid_mapped.push(["::13.1.68.3", Crunchy.parse("281470899930115")]);
47
+ ipv6.valid_mapped.push(["0:0:0:0:0:ffff:129.144.52.38", Crunchy.parse("281472855454758")]);
48
+ ipv6.valid_mapped.push(["::ffff:129.144.52.38", Crunchy.parse("281472855454758")]);
49
+ ipv6.valid_mapped_ipv6.push(["::ffff:13.1.68.3", Crunchy.parse("281470899930115")]);
50
+ ipv6.valid_mapped_ipv6.push(["0:0:0:0:0:ffff:8190:3426", Crunchy.parse("281472855454758")]);
51
+ ipv6.valid_mapped_ipv6.push(["::ffff:8190:3426", Crunchy.parse("281472855454758")]);
52
+ ipv6.valid_mapped_ipv6_conversion.push(["::ffff:13.1.68.3", "13.1.68.3"]);
53
+ ipv6.valid_mapped_ipv6_conversion.push(["0:0:0:0:0:ffff:8190:3426", "129.144.52.38"]);
54
+ ipv6.valid_mapped_ipv6_conversion.push(["::ffff:8190:3426", "129.144.52.38"]);
55
+ return ipv6;
56
+ }
57
+
58
+ it("test_initialize", () => {
59
+ const s = setup();
60
+ assert.isOk(IPAddress.parse("::172.16.10.1"));
61
+ for (const i of s.valid_mapped) {
62
+ const ip = i[0];
63
+ const u128 = i[1];
64
+ // println("-{}--{}", ip, u128);
65
+ if (!IPAddress.parse(ip)) {
66
+ // console.log(IPAddress.parse(ip));
67
+ }
68
+ assert.isOk(IPAddress.parse(ip));
69
+ assert.equal(u128.toString(), IPAddress.parse(ip)!.host_address.toString());
70
+ }
71
+ for (const i of s.valid_mapped_ipv6) {
72
+ const ip = i[0];
73
+ const u128 = i[1];
74
+ // println("===={}=={:x}", ip, u128);
75
+ assert.isOk(IPAddress.parse(ip));
76
+ assert.equal(u128.toString(), IPAddress.parse(ip)!.host_address.toString());
77
+ }
78
+ });
79
+ it("test_mapped_from_ipv6_conversion", () => {
80
+ for (const i of setup().valid_mapped_ipv6_conversion) {
81
+ const ip6 = i[0];
82
+ const ip4 = i[1];
83
+ assert.equal(ip4, IPAddress.parse(ip6)!.mapped!.to_s());
84
+ }
85
+ });
86
+ it("test_attributes", () => {
87
+ const s = setup();
88
+ assert.equal(s.address, s.ip.to_string());
89
+ assert.equal(128, s.ip.prefix.num);
90
+ assert.equal(s.s, s.ip.to_s_mapped());
91
+ assert.equal(s.sstr, s.ip.to_string_mapped());
92
+ assert.equal(s.string, s.ip.to_string_uncompressed());
93
+ assert.equal(s.u128.toString(), s.ip.host_address.toString());
94
+ });
95
+ it("test_method_ipv6", () => {
96
+ assert.isOk(setup().ip.is_ipv6());
97
+ });
98
+ it("test_mapped", () => {
99
+ assert.isOk(setup().ip.is_mapped());
100
+ });
101
+ });
@@ -0,0 +1,51 @@
1
+ import IPAddress from "../src/ipaddress.js";
2
+ // import Prefix128 from '../src/prefix128.js';
3
+ import Ipv6Unspec from "../src/ipv6_unspec.js";
4
+ import Crunchy from "../src/crunchy.js";
5
+
6
+ interface IPv6UnspecifiedTestParams {
7
+ ip: IPAddress;
8
+ to_s: string;
9
+ to_string: string;
10
+ to_string_uncompressed: string;
11
+ num: Crunchy;
12
+ }
13
+
14
+ class IPv6UnspecifiedTest {
15
+ ip: IPAddress;
16
+ to_s: string;
17
+ to_string: string;
18
+ to_string_uncompressed: string;
19
+ num: Crunchy;
20
+ constructor(obj: IPv6UnspecifiedTestParams) {
21
+ this.ip = obj.ip;
22
+ this.to_s = obj.to_s;
23
+ this.to_string = obj.to_string;
24
+ this.to_string_uncompressed = obj.to_string_uncompressed;
25
+ this.num = obj.num;
26
+ }
27
+ }
28
+
29
+ describe("ipv6_unspec", () => {
30
+ function setup(): IPv6UnspecifiedTest {
31
+ return new IPv6UnspecifiedTest({
32
+ ip: Ipv6Unspec.create()!,
33
+ to_s: "::",
34
+ to_string: "::/128",
35
+ to_string_uncompressed: "0000:0000:0000:0000:0000:0000:0000:0000/128",
36
+ num: Crunchy.zero(),
37
+ });
38
+ }
39
+
40
+ it("test_attributes", () => {
41
+ assert.isOk(setup().ip.host_address.eq(setup().num));
42
+ assert.equal(128, setup().ip.prefix.num);
43
+ assert.equal(true, setup().ip.is_unspecified());
44
+ assert.equal(setup().to_s, setup().ip.to_s());
45
+ assert.equal(setup().to_string, setup().ip.to_string());
46
+ assert.equal(setup().to_string_uncompressed, setup().ip.to_string_uncompressed());
47
+ });
48
+ it("test_method_ipv6", () => {
49
+ assert.equal(true, setup().ip.is_ipv6());
50
+ });
51
+ });
@@ -0,0 +1,39 @@
1
+ import Prefix128 from "../src/prefix128.js";
2
+ import Crunchy from "../src/crunchy.js";
3
+
4
+ class Prefix128Test {
5
+ public u128_hash: [number, Crunchy][] = [];
6
+ }
7
+
8
+ describe("prefix128", () => {
9
+ function setup(): Prefix128Test {
10
+ const p128t = new Prefix128Test();
11
+ p128t.u128_hash.push([32, Crunchy.parse("340282366841710300949110269838224261120")]);
12
+ p128t.u128_hash.push([64, Crunchy.parse("340282366920938463444927863358058659840")]);
13
+ p128t.u128_hash.push([96, Crunchy.parse("340282366920938463463374607427473244160")]);
14
+ p128t.u128_hash.push([126, Crunchy.parse("340282366920938463463374607431768211452")]);
15
+ return p128t;
16
+ }
17
+
18
+ it("test_initialize", () => {
19
+ assert.isNotOk(Prefix128.create(129));
20
+ assert.isOk(Prefix128.create(64));
21
+ });
22
+
23
+ it("test_method_bits", () => {
24
+ const prefix = Prefix128.create(64);
25
+ let str = "";
26
+ for (let i = 0; i < 64; ++i) {
27
+ str += "1";
28
+ }
29
+ for (let i = 0; i < 64; ++i) {
30
+ str += "0";
31
+ }
32
+ assert.equal(str, prefix!.bits());
33
+ });
34
+ it("test_method_to_u32", () => {
35
+ for (const hash of setup().u128_hash) {
36
+ assert.isOk(hash[1].eq(Prefix128.create(hash[0])!.netmask()));
37
+ }
38
+ });
39
+ });