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