ipaddress 0.2.3 → 0.2.6

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 (113) hide show
  1. package/{crunchy.d.ts → dist/src/crunchy.d.ts} +1 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +484 -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 -5
  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} +18 -16
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1228 -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 +176 -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 +35 -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 +25 -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.d.ts +2 -0
  44. package/dist/test/test_crunchy.d.ts.map +1 -0
  45. package/dist/test/test_crunchy.js +451 -0
  46. package/dist/test/test_ipaddress.d.ts +2 -0
  47. package/dist/test/test_ipaddress.d.ts.map +1 -0
  48. package/dist/test/test_ipaddress.js +182 -0
  49. package/dist/test/test_ipv4.d.ts +2 -0
  50. package/dist/test/test_ipv4.d.ts.map +1 -0
  51. package/dist/test/test_ipv4.js +561 -0
  52. package/dist/test/test_ipv6.d.ts +2 -0
  53. package/dist/test/test_ipv6.d.ts.map +1 -0
  54. package/dist/test/test_ipv6.js +313 -0
  55. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  56. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  57. package/dist/test/test_ipv6_loopback.js +40 -0
  58. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  59. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_mapped.js +89 -0
  61. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  62. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_unspec.js +39 -0
  64. package/dist/test/test_prefix128.d.ts +2 -0
  65. package/dist/test/test_prefix128.d.ts.map +1 -0
  66. package/dist/test/test_prefix128.js +35 -0
  67. package/dist/test/test_prefix32.d.ts +2 -0
  68. package/dist/test/test_prefix32.d.ts.map +1 -0
  69. package/dist/test/test_prefix32.js +126 -0
  70. package/dist/test/test_rle.d.ts +2 -0
  71. package/dist/test/test_rle.d.ts.map +1 -0
  72. package/dist/test/test_rle.js +141 -0
  73. package/dist/vitest.config.d.ts +3 -0
  74. package/dist/vitest.config.d.ts.map +1 -0
  75. package/dist/vitest.config.js +11 -0
  76. package/package.json +40 -37
  77. package/src/crunchy.ts +546 -0
  78. package/src/index.ts +23 -0
  79. package/src/ip_bits.ts +148 -0
  80. package/src/ip_version.ts +6 -0
  81. package/src/ipaddress.ts +1331 -0
  82. package/src/ipv4.ts +190 -0
  83. package/src/ipv6.ts +202 -0
  84. package/src/ipv6_loopback.ts +55 -0
  85. package/src/ipv6_mapped.ts +163 -0
  86. package/src/ipv6_unspec.ts +59 -0
  87. package/src/prefix.ts +170 -0
  88. package/src/prefix128.ts +38 -0
  89. package/src/prefix32.ts +28 -0
  90. package/src/rle.ts +85 -0
  91. package/test/test_crunchy.ts +614 -0
  92. package/test/test_ipaddress.ts +208 -0
  93. package/test/test_ipv4.ts +596 -0
  94. package/test/test_ipv6.ts +325 -0
  95. package/test/test_ipv6_loopback.ts +52 -0
  96. package/test/test_ipv6_mapped.ts +101 -0
  97. package/test/test_ipv6_unspec.ts +51 -0
  98. package/test/test_prefix128.ts +39 -0
  99. package/test/test_prefix32.ts +130 -0
  100. package/test/test_rle.ts +144 -0
  101. package/LICENSE +0 -20
  102. package/README.md +0 -956
  103. package/index.d.ts +0 -12
  104. package/ipaddress.js +0 -2854
  105. package/ipaddress.js.map +0 -1
  106. package/ipv4.d.ts +0 -14
  107. package/ipv6.d.ts +0 -12
  108. package/ipv6_loopback.d.ts +0 -5
  109. package/ipv6_mapped.d.ts +0 -5
  110. package/ipv6_unspec.d.ts +0 -5
  111. package/prefix128.d.ts +0 -6
  112. package/prefix32.d.ts +0 -6
  113. package/rle.d.ts +0 -14
@@ -0,0 +1,596 @@
1
+ import IPAddress from "../src/ipaddress.js";
2
+ // import Prefix32 from '../src/prefix32';
3
+ import Ipv4 from "../src/ipv4.js";
4
+ import Crunchy from "../src/crunchy.js";
5
+
6
+ interface Ipv4PrefixParams {
7
+ ip: string;
8
+ prefix: number;
9
+ }
10
+
11
+ class IPv4Prefix {
12
+ ip: string;
13
+ prefix: number;
14
+ constructor(obj: Ipv4PrefixParams) {
15
+ this.ip = obj.ip;
16
+ this.prefix = obj.prefix;
17
+ }
18
+ }
19
+
20
+ class IPv4Test {
21
+ valid_ipv4: [string, IPv4Prefix][] = [];
22
+ invalid_ipv4: string[] = ["10.0.0.256", "10.0.0.0.0"];
23
+ valid_ipv4_range: string[] = ["10.0.0.1-254", "10.0.1-254.0", "10.1-254.0.0"];
24
+ netmask_values: [string, string][] = [];
25
+ decimal_values: [string, Crunchy][] = [];
26
+ ip: IPAddress = IPAddress.parse("172.16.10.1/24");
27
+ network: IPAddress = IPAddress.parse("172.16.10.0/24");
28
+ networks: [string, string][] = [];
29
+ broadcast: [string, string][] = [];
30
+ class_a: IPAddress = IPAddress.parse("10.0.0.1/8");
31
+ class_b: IPAddress = IPAddress.parse("172.16.0.1/16");
32
+ class_c: IPAddress = IPAddress.parse("192.168.0.1/24");
33
+ classful: [string, number][] = [];
34
+ }
35
+
36
+ describe("Ipv4Test", () => {
37
+ function assertArrayEqual<T>(a: T[], b: T[]): void {
38
+ assert.equal(a.length, b.length, "length missmatch");
39
+ for (let i = 0; i < a.length; ++i) {
40
+ assert.equal(a[i], b[i]);
41
+ }
42
+ }
43
+
44
+ function setup(): IPv4Test {
45
+ const ipv4t = new IPv4Test();
46
+ ipv4t.valid_ipv4.push([
47
+ "9.9/17",
48
+ new IPv4Prefix({
49
+ ip: "9.0.0.9",
50
+ prefix: 17,
51
+ }),
52
+ ]);
53
+ ipv4t.valid_ipv4.push([
54
+ "100.1.100",
55
+ new IPv4Prefix({
56
+ ip: "100.1.0.100",
57
+ prefix: 32,
58
+ }),
59
+ ]);
60
+ ipv4t.valid_ipv4.push([
61
+ "0.0.0.0/0",
62
+ new IPv4Prefix({
63
+ ip: "0.0.0.0",
64
+ prefix: 0,
65
+ }),
66
+ ]);
67
+ ipv4t.valid_ipv4.push([
68
+ "10.0.0.0",
69
+ new IPv4Prefix({
70
+ ip: "10.0.0.0",
71
+ prefix: 32,
72
+ }),
73
+ ]);
74
+ ipv4t.valid_ipv4.push([
75
+ "10.0.0.1",
76
+ new IPv4Prefix({
77
+ ip: "10.0.0.1",
78
+ prefix: 32,
79
+ }),
80
+ ]);
81
+ ipv4t.valid_ipv4.push([
82
+ "10.0.0.1/24",
83
+ new IPv4Prefix({
84
+ ip: "10.0.0.1",
85
+ prefix: 24,
86
+ }),
87
+ ]);
88
+ ipv4t.valid_ipv4.push([
89
+ "10.0.0.9/255.255.255.0",
90
+ new IPv4Prefix({
91
+ ip: "10.0.0.9",
92
+ prefix: 24,
93
+ }),
94
+ ]);
95
+
96
+ ipv4t.netmask_values.push(["0.0.0.0/0", "0.0.0.0"]);
97
+ ipv4t.netmask_values.push(["10.0.0.0/8", "255.0.0.0"]);
98
+ ipv4t.netmask_values.push(["172.16.0.0/16", "255.255.0.0"]);
99
+ ipv4t.netmask_values.push(["192.168.0.0/24", "255.255.255.0"]);
100
+ ipv4t.netmask_values.push(["192.168.100.4/30", "255.255.255.252"]);
101
+
102
+ ipv4t.decimal_values.push(["0.0.0.0/0", Crunchy.parse("0")]);
103
+ ipv4t.decimal_values.push(["10.0.0.0/8", Crunchy.parse("167772160")]);
104
+ ipv4t.decimal_values.push(["172.16.0.0/16", Crunchy.parse("2886729728")]);
105
+ ipv4t.decimal_values.push(["192.168.0.0/24", Crunchy.parse("3232235520")]);
106
+ ipv4t.decimal_values.push(["192.168.100.4/30", Crunchy.parse("3232261124")]);
107
+
108
+ ipv4t.broadcast.push(["10.0.0.0/8", "10.255.255.255/8"]);
109
+ ipv4t.broadcast.push(["172.16.0.0/16", "172.16.255.255/16"]);
110
+ ipv4t.broadcast.push(["192.168.0.0/24", "192.168.0.255/24"]);
111
+ ipv4t.broadcast.push(["192.168.100.4/30", "192.168.100.7/30"]);
112
+
113
+ ipv4t.networks.push(["10.5.4.3/8", "10.0.0.0/8"]);
114
+ ipv4t.networks.push(["172.16.5.4/16", "172.16.0.0/16"]);
115
+ ipv4t.networks.push(["192.168.4.3/24", "192.168.4.0/24"]);
116
+ ipv4t.networks.push(["192.168.100.5/30", "192.168.100.4/30"]);
117
+
118
+ ipv4t.classful.push(["10.1.1.1", 8]);
119
+ ipv4t.classful.push(["150.1.1.1", 16]);
120
+ ipv4t.classful.push(["200.1.1.1", 24]);
121
+ return ipv4t;
122
+ }
123
+
124
+ it("test_initialize", () => {
125
+ for (const i of setup().valid_ipv4) {
126
+ // console.log(i[0]);
127
+ const ip = IPAddress.parse(i[0]);
128
+ assert.isOk(ip.is_ipv4() && !ip.is_ipv6());
129
+ }
130
+ assert.equal(32, setup().ip.prefix.ip_bits.bits);
131
+ assert.isNull(IPAddress.parse("1.f.13.1/-3"));
132
+ assert.isOk(IPAddress.parse("10.0.0.0/8"));
133
+ });
134
+ it("test_initialize_format_error", () => {
135
+ for (const i of setup().invalid_ipv4) {
136
+ assert.isNull(IPAddress.parse(i[0]));
137
+ }
138
+ assert.isNull(IPAddress.parse("10.0.0.0/asd"));
139
+ });
140
+ it("test_initialize_without_prefix", () => {
141
+ assert.isOk(IPAddress.parse("10.10.0.0"));
142
+ const ip = IPAddress.parse("10.10.0.0");
143
+ assert.isOk(!ip.is_ipv6() && ip.is_ipv4());
144
+ assert.equal(32, ip.prefix.num);
145
+ });
146
+ it("test_attributes", () => {
147
+ for (const i of setup().valid_ipv4) {
148
+ const arg = i[0];
149
+ const attr = i[1];
150
+ const ip = IPAddress.parse(arg);
151
+ // println!("test_attributes:{}:{:?}", arg, attr);
152
+ assert.equal(attr.ip, ip.to_s());
153
+ assert.equal(attr.prefix, ip.prefix.num);
154
+ }
155
+ });
156
+ it("test_octets", () => {
157
+ const ip = IPAddress.parse("10.1.2.3/8");
158
+ assertArrayEqual(ip.parts(), [10, 1, 2, 3]);
159
+ });
160
+ it("test_method_to_string", () => {
161
+ for (const i of setup().valid_ipv4) {
162
+ const arg = i[0];
163
+ const attr = i[1];
164
+ const ip = IPAddress.parse(arg);
165
+ assert.equal(`${attr.ip}/${attr.prefix}`, ip.to_string());
166
+ }
167
+ });
168
+ it("test_method_to_s", () => {
169
+ for (const i of setup().valid_ipv4) {
170
+ const arg = i[0];
171
+ const attr = i[1];
172
+ const ip = IPAddress.parse(arg);
173
+ assert.equal(attr.ip, ip.to_s());
174
+ // let ip_c = IPAddress.parse(arg);
175
+ // assert.equal(attr.ip, ip.to_s());
176
+ }
177
+ });
178
+ it("test_netmask", () => {
179
+ for (const i of setup().netmask_values) {
180
+ const addr = i[0];
181
+ const mask = i[1];
182
+ const ip = IPAddress.parse(addr);
183
+ assert.equal(ip.netmask().to_s(), mask);
184
+ }
185
+ });
186
+ it("test_method_to_u32", () => {
187
+ for (const i of setup().decimal_values) {
188
+ const addr = i[0];
189
+ const int = i[1];
190
+ const ip = IPAddress.parse(addr);
191
+ assert.isOk(ip.host_address.eq(int));
192
+ }
193
+ });
194
+ it("test_method_is_network", () => {
195
+ assert.equal(true, setup().network.is_network());
196
+ assert.equal(false, setup().ip.is_network());
197
+ });
198
+ it("test_one_address_network", () => {
199
+ const network = IPAddress.parse("172.16.10.1/32");
200
+ assert.equal(false, network.is_network());
201
+ });
202
+ it("test_method_broadcast", () => {
203
+ for (const i of setup().broadcast) {
204
+ const addr = i[0];
205
+ const bcast = i[1];
206
+ const ip = IPAddress.parse(addr);
207
+ assert.equal(bcast, ip.broadcast().to_string());
208
+ }
209
+ });
210
+ it("test_method_network", () => {
211
+ for (const i of setup().networks) {
212
+ const addr = i[0];
213
+ const net = i[1];
214
+ const ip = IPAddress.parse(addr);
215
+ assert.equal(net, ip.network().to_string());
216
+ }
217
+ });
218
+ it("test_method_bits", () => {
219
+ const ip = IPAddress.parse("127.0.0.1");
220
+ assert.equal("01111111000000000000000000000001", ip.bits());
221
+ });
222
+ it("test_method_first", () => {
223
+ let ip = IPAddress.parse("192.168.100.0/24");
224
+ assert.equal("192.168.100.1", ip.first().to_s());
225
+ ip = IPAddress.parse("192.168.100.50/24");
226
+ assert.equal("192.168.100.1", ip.first().to_s());
227
+ });
228
+ it("test_method_last", () => {
229
+ let ip = IPAddress.parse("192.168.100.0/24");
230
+ assert.equal("192.168.100.254", ip.last().to_s());
231
+ ip = IPAddress.parse("192.168.100.50/24");
232
+ assert.equal("192.168.100.254", ip.last().to_s());
233
+ });
234
+ it("test_method_each_host", () => {
235
+ const ip = IPAddress.parse("10.0.0.1/29");
236
+ const arr: string[] = [];
237
+ ip.each_host((i) => arr.push(i.to_s()));
238
+ assertArrayEqual(arr, ["10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4", "10.0.0.5", "10.0.0.6"]);
239
+ });
240
+ it("test_method_each", () => {
241
+ const ip = IPAddress.parse("10.0.0.1/29");
242
+ const arr: string[] = [];
243
+ ip.each((i) => arr.push(i.to_s()));
244
+ assertArrayEqual(arr, ["10.0.0.0", "10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4", "10.0.0.5", "10.0.0.6", "10.0.0.7"]);
245
+ });
246
+ it("test_method_size", () => {
247
+ const ip = IPAddress.parse("10.0.0.1/29");
248
+ assertArrayEqual(ip.size().num, Crunchy.parse("8").num);
249
+ });
250
+ it("test_method_network_u32", () => {
251
+ assert.equal("2886732288", setup().ip.network().host_address.toString());
252
+ });
253
+ it("test_method_broadcast_u32", () => {
254
+ assert.equal("2886732543", setup().ip.broadcast().host_address.toString());
255
+ });
256
+ it("test_method_include", () => {
257
+ let ip = IPAddress.parse("192.168.10.100/24");
258
+ const addr = IPAddress.parse("192.168.10.102/24");
259
+ assert.equal(true, ip.includes(addr));
260
+ assert.equal(false, ip.includes(IPAddress.parse("172.16.0.48")));
261
+ ip = IPAddress.parse("10.0.0.0/8");
262
+ assert.equal(true, ip.includes(IPAddress.parse("10.0.0.0/9")));
263
+ assert.equal(true, ip.includes(IPAddress.parse("10.1.1.1/32")));
264
+ assert.equal(true, ip.includes(IPAddress.parse("10.1.1.1/9")));
265
+ assert.equal(false, ip.includes(IPAddress.parse("172.16.0.0/16")));
266
+ assert.equal(false, ip.includes(IPAddress.parse("10.0.0.0/7")));
267
+ assert.equal(false, ip.includes(IPAddress.parse("5.5.5.5/32")));
268
+ assert.equal(false, ip.includes(IPAddress.parse("11.0.0.0/8")));
269
+ ip = IPAddress.parse("13.13.0.0/13");
270
+ assert.equal(false, ip.includes(IPAddress.parse("13.16.0.0/32")));
271
+ });
272
+ it("test_method_include_all", () => {
273
+ const ip = IPAddress.parse("192.168.10.100/24");
274
+ const addr1 = IPAddress.parse("192.168.10.102/24");
275
+ const addr2 = IPAddress.parse("192.168.10.103/24");
276
+ assert.equal(true, ip.includes_all([addr1, addr2]));
277
+ assert.equal(false, ip.includes_all([addr1, IPAddress.parse("13.16.0.0/32")]));
278
+ });
279
+ it("test_method_ipv4", () => {
280
+ assert.equal(true, setup().ip.is_ipv4());
281
+ });
282
+ it("test_method_ipv6", () => {
283
+ assert.equal(false, setup().ip.is_ipv6());
284
+ });
285
+ it("test_method_private", () => {
286
+ assert.equal(true, IPAddress.parse("169.254.99.4/24").is_private());
287
+ assert.equal(true, IPAddress.parse("192.168.10.50/24").is_private());
288
+ assert.equal(true, IPAddress.parse("192.168.10.50/16").is_private());
289
+ assert.equal(true, IPAddress.parse("172.16.77.40/24").is_private());
290
+ assert.equal(true, IPAddress.parse("172.16.10.50/14").is_private());
291
+ assert.equal(true, IPAddress.parse("10.10.10.10/10").is_private());
292
+ assert.equal(true, IPAddress.parse("10.0.0.0/8").is_private());
293
+ assert.equal(false, IPAddress.parse("192.168.10.50/12").is_private());
294
+ assert.equal(false, IPAddress.parse("3.3.3.3").is_private());
295
+ assert.equal(false, IPAddress.parse("10.0.0.0/7").is_private());
296
+ assert.equal(false, IPAddress.parse("172.32.0.0/12").is_private());
297
+ assert.equal(false, IPAddress.parse("172.16.0.0/11").is_private());
298
+ assert.equal(false, IPAddress.parse("192.0.0.2/24").is_private());
299
+ });
300
+ it("test_method_octet", () => {
301
+ assert.equal(setup().ip.parts()[0], 172);
302
+ assert.equal(setup().ip.parts()[1], 16);
303
+ assert.equal(setup().ip.parts()[2], 10);
304
+ assert.equal(setup().ip.parts()[3], 1);
305
+ });
306
+ it("test_method_a", () => {
307
+ assert.equal(true, Ipv4.is_class_a(setup().class_a));
308
+ assert.equal(false, Ipv4.is_class_a(setup().class_b));
309
+ assert.equal(false, Ipv4.is_class_a(setup().class_c));
310
+ });
311
+ it("test_method_b", () => {
312
+ assert.equal(true, Ipv4.is_class_b(setup().class_b));
313
+ assert.equal(false, Ipv4.is_class_b(setup().class_a));
314
+ assert.equal(false, Ipv4.is_class_b(setup().class_c));
315
+ });
316
+ it("test_method_c", () => {
317
+ assert.equal(true, Ipv4.is_class_c(setup().class_c));
318
+ assert.equal(false, Ipv4.is_class_c(setup().class_a));
319
+ assert.equal(false, Ipv4.is_class_c(setup().class_b));
320
+ });
321
+ it("test_method_to_ipv6", () => {
322
+ assert.equal("::ac10:a01", setup().ip.to_ipv6().to_s());
323
+ });
324
+ it("test_method_reverse", () => {
325
+ assert.equal(setup().ip.dns_reverse(), "10.16.172.in-addr.arpa");
326
+ });
327
+ it("test_method_dns_rev_domains", () => {
328
+ assertArrayEqual(IPAddress.parse("173.17.5.1/23").dns_rev_domains(), ["4.17.173.in-addr.arpa", "5.17.173.in-addr.arpa"]);
329
+ assertArrayEqual(IPAddress.parse("173.17.1.1/15").dns_rev_domains(), ["16.173.in-addr.arpa", "17.173.in-addr.arpa"]);
330
+ assertArrayEqual(IPAddress.parse("173.17.1.1/7").dns_rev_domains(), ["172.in-addr.arpa", "173.in-addr.arpa"]);
331
+ assertArrayEqual(IPAddress.parse("173.17.1.1/29").dns_rev_domains(), [
332
+ "0.1.17.173.in-addr.arpa",
333
+ "1.1.17.173.in-addr.arpa",
334
+ "2.1.17.173.in-addr.arpa",
335
+ "3.1.17.173.in-addr.arpa",
336
+ "4.1.17.173.in-addr.arpa",
337
+ "5.1.17.173.in-addr.arpa",
338
+ "6.1.17.173.in-addr.arpa",
339
+ "7.1.17.173.in-addr.arpa",
340
+ ]);
341
+ assertArrayEqual(IPAddress.parse("174.17.1.1/24").dns_rev_domains(), ["1.17.174.in-addr.arpa"]);
342
+ assertArrayEqual(IPAddress.parse("175.17.1.1/16").dns_rev_domains(), ["17.175.in-addr.arpa"]);
343
+ assertArrayEqual(IPAddress.parse("176.17.1.1/8").dns_rev_domains(), ["176.in-addr.arpa"]);
344
+ assertArrayEqual(IPAddress.parse("177.17.1.1/0").dns_rev_domains(), ["in-addr.arpa"]);
345
+ assertArrayEqual(IPAddress.parse("178.17.1.1/32").dns_rev_domains(), ["1.1.17.178.in-addr.arpa"]);
346
+ });
347
+ it("test_method_compare", () => {
348
+ let ip1 = IPAddress.parse("10.1.1.1/8");
349
+ let ip2 = IPAddress.parse("10.1.1.1/16");
350
+ let ip3 = IPAddress.parse("172.16.1.1/14");
351
+ const ip4 = IPAddress.parse("10.1.1.1/8");
352
+
353
+ // ip2 should be greater than ip1
354
+ assert.equal(true, ip1.lt(ip2));
355
+ assert.equal(false, ip1.gt(ip2));
356
+ assert.equal(false, ip2.lt(ip1));
357
+ // ip2 should be less than ip3
358
+ assert.equal(true, ip2.lt(ip3));
359
+ assert.equal(false, ip2.gt(ip3));
360
+ // ip1 should be less than ip3
361
+ assert.equal(true, ip1.lt(ip3));
362
+ assert.equal(false, ip1.gt(ip3));
363
+ assert.equal(false, ip3.lt(ip1));
364
+ // ip1 should be equal to itself
365
+ assert.equal(true, ip1.eq(ip1));
366
+ // ip1 should be equal to ip4
367
+ assert.equal(true, ip1.eq(ip4));
368
+ // test sorting
369
+ const res = [ip1, ip2, ip3].sort((a, b) => a.cmp(b));
370
+ assertArrayEqual(IPAddress.to_string_vec(res), ["10.1.1.1/8", "10.1.1.1/16", "172.16.1.1/14"]);
371
+ // test same prefix
372
+ ip1 = IPAddress.parse("10.0.0.0/24");
373
+ ip2 = IPAddress.parse("10.0.0.0/16");
374
+ ip3 = IPAddress.parse("10.0.0.0/8");
375
+ {
376
+ const res = [ip1, ip2, ip3].sort((a, b) => a.cmp(b));
377
+ assertArrayEqual(IPAddress.to_string_vec(res), ["10.0.0.0/8", "10.0.0.0/16", "10.0.0.0/24"]);
378
+ }
379
+ });
380
+ it("test_method_minus", () => {
381
+ const ip1 = IPAddress.parse("10.1.1.1/8");
382
+ const ip2 = IPAddress.parse("10.1.1.10/8");
383
+ assert.equal("9", ip2.sub(ip1).toString());
384
+ assert.equal("9", ip1.sub(ip2).toString());
385
+ });
386
+ it("test_method_plus", () => {
387
+ let ip1 = IPAddress.parse("172.16.10.1/24");
388
+ let ip2 = IPAddress.parse("172.16.11.2/24");
389
+ assertArrayEqual(IPAddress.to_string_vec(ip1.add(ip2)), ["172.16.10.0/23"]);
390
+
391
+ ip2 = IPAddress.parse("172.16.12.2/24");
392
+ assertArrayEqual(IPAddress.to_string_vec(ip1.add(ip2)), [ip1.network().to_string(), ip2.network().to_string()]);
393
+
394
+ ip1 = IPAddress.parse("10.0.0.0/23");
395
+ ip2 = IPAddress.parse("10.0.2.0/24");
396
+ assertArrayEqual(IPAddress.to_string_vec(ip1.add(ip2)), ["10.0.0.0/23", "10.0.2.0/24"]);
397
+
398
+ ip1 = IPAddress.parse("10.0.0.0/23");
399
+ ip2 = IPAddress.parse("10.0.2.0/24");
400
+ assertArrayEqual(IPAddress.to_string_vec(ip1.add(ip2)), ["10.0.0.0/23", "10.0.2.0/24"]);
401
+
402
+ ip1 = IPAddress.parse("10.0.0.0/16");
403
+ ip2 = IPAddress.parse("10.0.2.0/24");
404
+ assertArrayEqual(IPAddress.to_string_vec(ip1.add(ip2)), ["10.0.0.0/16"]);
405
+
406
+ ip1 = IPAddress.parse("10.0.0.0/23");
407
+ ip2 = IPAddress.parse("10.1.0.0/24");
408
+ assertArrayEqual(IPAddress.to_string_vec(ip1.add(ip2)), ["10.0.0.0/23", "10.1.0.0/24"]);
409
+ });
410
+ it("test_method_netmask_equal", () => {
411
+ const ip = IPAddress.parse("10.1.1.1/16");
412
+ assert.equal(16, ip.prefix.num);
413
+ const ip2 = ip.change_netmask("255.255.255.0");
414
+ assert.equal(24, ip2.prefix.num);
415
+ });
416
+ it("test_method_split", () => {
417
+ assert.isNull(setup().ip.split(0));
418
+ assert.isNull(setup().ip.split(257));
419
+
420
+ assertArrayEqual(IPAddress.to_string_vec(setup().ip.split(1)), [setup().ip.network().to_string()]);
421
+
422
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.split(8)), [
423
+ "172.16.10.0/27",
424
+ "172.16.10.32/27",
425
+ "172.16.10.64/27",
426
+ "172.16.10.96/27",
427
+ "172.16.10.128/27",
428
+ "172.16.10.160/27",
429
+ "172.16.10.192/27",
430
+ "172.16.10.224/27",
431
+ ]);
432
+
433
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.split(7)), [
434
+ "172.16.10.0/27",
435
+ "172.16.10.32/27",
436
+ "172.16.10.64/27",
437
+ "172.16.10.96/27",
438
+ "172.16.10.128/27",
439
+ "172.16.10.160/27",
440
+ "172.16.10.192/26",
441
+ ]);
442
+
443
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.split(6)), [
444
+ "172.16.10.0/27",
445
+ "172.16.10.32/27",
446
+ "172.16.10.64/27",
447
+ "172.16.10.96/27",
448
+ "172.16.10.128/26",
449
+ "172.16.10.192/26",
450
+ ]);
451
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.split(5)), [
452
+ "172.16.10.0/27",
453
+ "172.16.10.32/27",
454
+ "172.16.10.64/27",
455
+ "172.16.10.96/27",
456
+ "172.16.10.128/25",
457
+ ]);
458
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.split(4)), [
459
+ "172.16.10.0/26",
460
+ "172.16.10.64/26",
461
+ "172.16.10.128/26",
462
+ "172.16.10.192/26",
463
+ ]);
464
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.split(3)), ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/25"]);
465
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.split(2)), ["172.16.10.0/25", "172.16.10.128/25"]);
466
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.split(1)), ["172.16.10.0/24"]);
467
+ });
468
+ it("test_method_subnet", () => {
469
+ assert.isNull(setup().network.subnet(23));
470
+ assert.isNull(setup().network.subnet(33));
471
+ assert.isOk(setup().ip.subnet(30));
472
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.subnet(26)), [
473
+ "172.16.10.0/26",
474
+ "172.16.10.64/26",
475
+ "172.16.10.128/26",
476
+ "172.16.10.192/26",
477
+ ]);
478
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.subnet(25)), ["172.16.10.0/25", "172.16.10.128/25"]);
479
+ assertArrayEqual(IPAddress.to_string_vec(setup().network.subnet(24)), ["172.16.10.0/24"]);
480
+ });
481
+ it("test_method_supernet", () => {
482
+ assert.isNull(setup().ip.supernet(24));
483
+ assert.equal("0.0.0.0/0", setup().ip.supernet(0).to_string());
484
+ // assert.equal("0.0.0.0/0", setup().ip.supernet(-2).to_string());
485
+ assert.equal("172.16.10.0/23", setup().ip.supernet(23).to_string());
486
+ assert.equal("172.16.8.0/22", setup().ip.supernet(22).to_string());
487
+ });
488
+ it("test_classmethod_parse_u32", () => {
489
+ for (const i of setup().decimal_values) {
490
+ const addr = i[0];
491
+ const int = i[1];
492
+ const ip = Ipv4.from_number(int, 32);
493
+ const splitted = addr.split("/");
494
+ const ip2 = ip.change_prefix(parseInt(splitted[1], 10));
495
+ assert.equal(ip2.to_string(), addr);
496
+ }
497
+ });
498
+ it("test_classmethod_summarize", () => {
499
+ // Should return self if only one network given
500
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([setup().ip])), [setup().ip.network().to_string()]);
501
+
502
+ // Summarize homogeneous networks
503
+ let ip1 = IPAddress.parse("172.16.10.1/24");
504
+ let ip2 = IPAddress.parse("172.16.11.2/24");
505
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip1, ip2])), ["172.16.10.0/23"]);
506
+
507
+ {
508
+ const ip1 = IPAddress.parse("10.0.0.1/24");
509
+ const ip2 = IPAddress.parse("10.0.1.1/24");
510
+ const ip3 = IPAddress.parse("10.0.2.1/24");
511
+ const ip4 = IPAddress.parse("10.0.3.1/24");
512
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip1, ip2, ip3, ip4])), ["10.0.0.0/22"]);
513
+ }
514
+ {
515
+ const ip1 = IPAddress.parse("10.0.0.1/24");
516
+ const ip2 = IPAddress.parse("10.0.1.1/24");
517
+ const ip3 = IPAddress.parse("10.0.2.1/24");
518
+ const ip4 = IPAddress.parse("10.0.3.1/24");
519
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip4, ip3, ip2, ip1])), ["10.0.0.0/22"]);
520
+ }
521
+
522
+ // Summarize non homogeneous networks
523
+ ip1 = IPAddress.parse("10.0.0.0/23");
524
+ ip2 = IPAddress.parse("10.0.2.0/24");
525
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip1, ip2])), ["10.0.0.0/23", "10.0.2.0/24"]);
526
+
527
+ ip1 = IPAddress.parse("10.0.0.0/16");
528
+ ip2 = IPAddress.parse("10.0.2.0/24");
529
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip1, ip2])), ["10.0.0.0/16"]);
530
+
531
+ ip1 = IPAddress.parse("10.0.0.0/23");
532
+ ip2 = IPAddress.parse("10.1.0.0/24");
533
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip1, ip2])), ["10.0.0.0/23", "10.1.0.0/24"]);
534
+
535
+ ip1 = IPAddress.parse("10.0.0.0/23");
536
+ ip2 = IPAddress.parse("10.0.2.0/23");
537
+ let ip3 = IPAddress.parse("10.0.4.0/24");
538
+ let ip4 = IPAddress.parse("10.0.6.0/24");
539
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip1, ip2, ip3, ip4])), [
540
+ "10.0.0.0/22",
541
+ "10.0.4.0/24",
542
+ "10.0.6.0/24",
543
+ ]);
544
+ {
545
+ const ip1 = IPAddress.parse("10.0.1.1/24");
546
+ const ip2 = IPAddress.parse("10.0.2.1/24");
547
+ const ip3 = IPAddress.parse("10.0.3.1/24");
548
+ const ip4 = IPAddress.parse("10.0.4.1/24");
549
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip1, ip2, ip3, ip4])), [
550
+ "10.0.1.0/24",
551
+ "10.0.2.0/23",
552
+ "10.0.4.0/24",
553
+ ]);
554
+ }
555
+ {
556
+ const ip1 = IPAddress.parse("10.0.1.1/24");
557
+ const ip2 = IPAddress.parse("10.0.2.1/24");
558
+ const ip3 = IPAddress.parse("10.0.3.1/24");
559
+ const ip4 = IPAddress.parse("10.0.4.1/24");
560
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip4, ip3, ip2, ip1])), [
561
+ "10.0.1.0/24",
562
+ "10.0.2.0/23",
563
+ "10.0.4.0/24",
564
+ ]);
565
+ }
566
+
567
+ ip1 = IPAddress.parse("10.0.1.1/24");
568
+ ip2 = IPAddress.parse("10.10.2.1/24");
569
+ ip3 = IPAddress.parse("172.16.0.1/24");
570
+ ip4 = IPAddress.parse("172.16.1.1/24");
571
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([ip1, ip2, ip3, ip4])), [
572
+ "10.0.1.0/24",
573
+ "10.10.2.0/24",
574
+ "172.16.0.0/23",
575
+ ]);
576
+
577
+ let ips = [IPAddress.parse("10.0.0.12/30"), IPAddress.parse("10.0.100.0/24")];
578
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize(ips)), ["10.0.0.12/30", "10.0.100.0/24"]);
579
+
580
+ ips = [IPAddress.parse("172.16.0.0/31"), IPAddress.parse("10.10.2.1/32")];
581
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize(ips)), ["10.10.2.1/32", "172.16.0.0/31"]);
582
+
583
+ ips = [IPAddress.parse("172.16.0.0/32"), IPAddress.parse("10.10.2.1/32")];
584
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize(ips)), ["10.10.2.1/32", "172.16.0.0/32"]);
585
+ });
586
+ it("test_classmethod_parse_classful", () => {
587
+ for (const i of setup().classful) {
588
+ const ip = i[0];
589
+ const prefix = i[1];
590
+ const res = Ipv4.parse_classful(ip);
591
+ assert.equal(prefix, res.prefix.num);
592
+ assert.equal(`${ip}/${prefix}`, res.to_string());
593
+ }
594
+ assert.isNull(Ipv4.parse_classful("192.168.256.257"));
595
+ });
596
+ });