ipaddress_2 0.11.0

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.
@@ -0,0 +1,707 @@
1
+ require 'test_helper'
2
+
3
+ class IPv6Test < Minitest::Test
4
+
5
+ def setup
6
+ @klass = IPAddress::IPv6
7
+
8
+ @compress_addr = {
9
+ "2001:db8:0000:0000:0008:0800:200c:417a" => "2001:db8::8:800:200c:417a",
10
+ "2001:db8:0:0:8:800:200c:417a" => "2001:db8::8:800:200c:417a",
11
+ "ff01:0:0:0:0:0:0:101" => "ff01::101",
12
+ "0:0:0:0:0:0:0:1" => "::1",
13
+ "0:0:0:0:0:0:0:0" => "::"}
14
+
15
+ @valid_ipv6 = { # Kindly taken from the python IPy library
16
+ "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210" => 338770000845734292534325025077361652240,
17
+ "1080:0000:0000:0000:0008:0800:200C:417A" => 21932261930451111902915077091070067066,
18
+ "1080:0:0:0:8:800:200C:417A" => 21932261930451111902915077091070067066,
19
+ "1080:0::8:800:200C:417A" => 21932261930451111902915077091070067066,
20
+ "1080::8:800:200C:417A" => 21932261930451111902915077091070067066,
21
+ "FF01:0:0:0:0:0:0:43" => 338958331222012082418099330867817087043,
22
+ "FF01:0:0::0:0:43" => 338958331222012082418099330867817087043,
23
+ "FF01::43" => 338958331222012082418099330867817087043,
24
+ "0:0:0:0:0:0:0:1" => 1,
25
+ "0:0:0::0:0:1" => 1,
26
+ "::1" => 1,
27
+ "0:0:0:0:0:0:0:0" => 0,
28
+ "0:0:0::0:0:0" => 0,
29
+ "::" => 0}
30
+
31
+ @invalid_ipv6 = [":1:2:3:4:5:6:7",
32
+ ":1:2:3:4:5:6:7",
33
+ "2002:516:2:200",
34
+ "dd"]
35
+
36
+ @networks = {
37
+ "2001:db8:1:1:1:1:1:1/32" => "2001:db8::/32",
38
+ "2001:db8:1:1:1:1:1::/32" => "2001:db8::/32",
39
+ "2001:db8::1/64" => "2001:db8::/64"}
40
+
41
+ @ip = @klass.new "2001:db8::8:800:200c:417a/64"
42
+ @network = @klass.new "2001:db8:8:800::/64"
43
+ @network2 = @klass.new "2001:db8:8::/48"
44
+ @arr = [8193,3512,0,0,8,2048,8204,16762]
45
+ @hex = "20010db80000000000080800200c417a"
46
+
47
+ @link_local = [
48
+ "fe80::",
49
+ "fe80::1",
50
+ "fe80::208:74ff:feda:625c",
51
+ "fe80:0000:0000:0000:0000:0000:0000:0000",
52
+ "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
53
+ "fe80::/10",
54
+ "fe80::/11",
55
+ "fe80::/20",
56
+ "fe80::/30",
57
+ "fe80::/30",
58
+ "fe80::/63",
59
+ "fe80::/64",
60
+ "fe80::/65"]
61
+
62
+ @not_link_local = [
63
+ "::",
64
+ "::1",
65
+ "ff80:03:02:01::",
66
+ "2001:db8::8:800:200c:417a"]
67
+
68
+ @unique_local = [
69
+ "fc00::/7",
70
+ "fc00::/8",
71
+ "fd00::/8",
72
+ "fd12:3456:789a:1::1",
73
+ "fd12:3456:789a:1::/64",
74
+ "fc00::1"]
75
+
76
+ @not_unique_local = [
77
+ "fc00::/6",
78
+ "::",
79
+ "::1",
80
+ "fe80::",
81
+ "fe80::1",
82
+ "fe80::/64"]
83
+
84
+ end
85
+
86
+ def test_attribute_address
87
+ addr = "2001:0db8:0000:0000:0008:0800:200c:417a"
88
+ assert_equal addr, @ip.address
89
+ end
90
+
91
+ def test_initialize
92
+ assert_instance_of @klass, @ip
93
+ @invalid_ipv6.each do |ip|
94
+ assert_raises(ArgumentError) {@klass.new ip}
95
+ end
96
+ assert_equal 64, @ip.prefix
97
+
98
+ assert_raises(ArgumentError) {@klass.new nil }
99
+ assert_raises(ArgumentError) {
100
+ @klass.new "::10.1.1.1"
101
+ }
102
+ end
103
+
104
+ def test_attribute_groups
105
+ assert_equal @arr, @ip.groups
106
+ end
107
+
108
+ def test_method_as_json
109
+ ip = @klass.new("2001:db8::8:800:200c:417a/64")
110
+ assert_equal "2001:db8::8:800:200c:417a/64", ip.as_json
111
+ end
112
+
113
+ def test_method_hexs
114
+ arr = "2001:0db8:0000:0000:0008:0800:200c:417a".split(":")
115
+ assert_equal arr, @ip.hexs
116
+ end
117
+
118
+ def test_method_to_i
119
+ @valid_ipv6.each do |ip,num|
120
+ assert_equal num, @klass.new(ip).to_i
121
+ end
122
+ end
123
+
124
+ def test_method_bits
125
+ bits = "0010000000000001000011011011100000000000000000000" +
126
+ "000000000000000000000000000100000001000000000000010000" +
127
+ "0000011000100000101111010"
128
+ assert_equal bits, @ip.bits
129
+ end
130
+
131
+ def test_method_prefix=()
132
+ ip = @klass.new "2001:db8::8:800:200c:417a"
133
+ assert_equal 128, ip.prefix
134
+ ip.prefix = 64
135
+ assert_equal 64, ip.prefix
136
+ assert_equal "2001:db8::8:800:200c:417a/64", ip.to_string
137
+ end
138
+
139
+ def test_method_mapped?
140
+ assert_equal false, @ip.mapped?
141
+ ip6 = @klass.new "::ffff:1234:5678"
142
+ assert_equal true, ip6.mapped?
143
+ end
144
+
145
+ def test_classmethod_summarize
146
+ # Should return self if only one network given
147
+ assert_equal [@ip.network], @klass.summarize(@ip)
148
+
149
+ # Summarize homogeneous networks
150
+ ip1 = @klass.new("2001:db8:8:800::1/64")
151
+ ip2 = @klass.new("2001:0db8:8:801::2/64")
152
+ assert_equal ["2001:db8:8:800::/63"], @klass.summarize(ip1,ip2).map{|i| i.to_string}
153
+
154
+ ip1 = @klass.new("2001:db8:8:800::1/64")
155
+ ip2 = @klass.new("2001:db8:8:801::1/64")
156
+ ip3 = @klass.new("2001:db8:8:802::1/64")
157
+ ip4 = @klass.new("2001:db8:8:803::1/64")
158
+ assert_equal ["2001:db8:8:800::/62"], @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
159
+ assert_equal ["2001:db8:8:800::/62"], @klass.summarize(ip4,ip3,ip2,ip1).map{|i| i.to_string}
160
+
161
+ # Summarize non homogeneous networks
162
+ ip1 = @klass.new("2001:db8:8:800::/63")
163
+ ip2 = @klass.new("2001:db8:9:800::/64")
164
+ assert_equal ["2001:db8:8:800::/63","2001:db8:9:800::/64"], @klass.summarize(ip1,ip2).map{|i| i.to_string}
165
+
166
+ ip1 = @klass.new("2001:db8::/32")
167
+ ip2 = @klass.new("2001:db8:8:800::/64")
168
+ assert_equal ["2001:db8::/32"], @klass.summarize(ip1,ip2).map{|i| i.to_string}
169
+
170
+ ip1 = @klass.new("2001:db8:8:800::/63")
171
+ ip2 = @klass.new("2002:db8:8:800::/64")
172
+ assert_equal ["2001:db8:8:800::/63","2002:db8:8:800::/64"], @klass.summarize(ip1,ip2).map{|i| i.to_string}
173
+
174
+ ip1 = @klass.new("2001:db8:8:800::/63")
175
+ ip2 = @klass.new("2001:db8:8:802::/63")
176
+ ip3 = @klass.new("2001:db8:9:800::/64")
177
+ ip4 = @klass.new("2001:db8:6:800::/64")
178
+ assert_equal ["2001:db8:6:800::/64","2001:db8:8:800::/62","2001:db8:9:800::/64"],
179
+ @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
180
+
181
+ ip1 = @klass.new("2001:db8:8:801::1/64")
182
+ ip2 = @klass.new("2001:db8:8:802::1/64")
183
+ ip3 = @klass.new("2001:db8:8:803::1/64")
184
+ ip4 = @klass.new("2001:db8:8:804::1/64")
185
+ result = ["2001:db8:8:801::/64","2001:db8:8:802::/63","2001:db8:8:804::/64"]
186
+ assert_equal result, @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
187
+ assert_equal result, @klass.summarize(ip4,ip3,ip2,ip1).map{|i| i.to_string}
188
+
189
+ ip1 = @klass.new("2001:db8:8:800::1/64")
190
+ ip2 = @klass.new("2002:db8:9:800::1/64")
191
+ ip3 = @klass.new("2004:db8:8:800::1/64")
192
+ ip4 = @klass.new("2004:db8:8:801::2/64")
193
+ result = ["2001:db8:8:800::/64","2002:db8:9:800::/64","2004:db8:8:800::/63"]
194
+ assert_equal result, @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
195
+
196
+ ips = [@klass.new("2001:db8:8:800::1/128"),
197
+ @klass.new("2001:db8:9:800::1/64")]
198
+ result = ["2001:db8:8:800::1/128", "2001:db8:9:800::/64"]
199
+ assert_equal result, @klass.summarize(*ips).map{|i| i.to_string}
200
+
201
+ ips = [@klass.new("2091:db8:8:800::1/128"),
202
+ @klass.new("2001:db8:9:900::1/128")]
203
+ result = ["2001:db8:9:900::1/128", "2091:db8:8:800::1/128"]
204
+ assert_equal result, @klass.summarize(*ips).map{|i| i.to_string}
205
+
206
+ ips = [@klass.new("2004:db8:8:800::1/128"),
207
+ @klass.new("2001:db8:8:800::1/128")]
208
+ result = ["2001:db8:8:800::1/128", "2004:db8:8:800::1/128"]
209
+ assert_equal result, @klass.summarize(*ips).map{|i| i.to_string}
210
+ end
211
+
212
+ def test_method_plus
213
+ ip1 = @klass.new("2001:db8:8:800::1/64")
214
+ ip2 = @klass.new("2001:0db8:8:801::2/64")
215
+ assert_equal ["2001:db8:8:800::/63"], (ip1+ip2).map{|i| i.to_string}
216
+
217
+ ip2 = @klass.new("2001:db8:8:802::1/64")
218
+ assert_equal [ip1.network.to_string, ip2.network.to_string],
219
+ (ip1 + ip2).map{|i| i.to_string}
220
+
221
+ ip1 = @klass.new("2001:db8:8:800::/63")
222
+ ip2 = @klass.new("2001:db8:8:900::/64")
223
+ assert_equal ["2001:db8:8:800::/63","2001:db8:8:900::/64"], (ip1+ip2).map{|i| i.to_string}
224
+
225
+ ip1 = @klass.new("2001:db8:8:800::/64")
226
+ ip2 = @klass.new("2001:db8:8:800:c000::/66")
227
+ assert_equal ["2001:db8:8:800::/64"], (ip1+ip2).map{|i| i.to_string}
228
+
229
+ ip1 = @klass.new("2001:db8:8:800::/63")
230
+ ip2 = @klass.new("2001:db9:8:800::/64")
231
+ assert_equal ["2001:db8:8:800::/63","2001:db9:8:800::/64"], (ip1+ip2).map{|i| i.to_string}
232
+ end
233
+
234
+ def test_method_split
235
+ assert_raises(ArgumentError) {@ip.split(0)}
236
+ # assert_raises(ArgumentError) {@ip.split(257)}
237
+
238
+ assert_equal @ip.network, @ip.split(1).first
239
+
240
+ arr = ["2001:db8:8::/51", "2001:db8:8:2000::/51", "2001:db8:8:4000::/51",
241
+ "2001:db8:8:6000::/51", "2001:db8:8:8000::/51", "2001:db8:8:a000::/51",
242
+ "2001:db8:8:c000::/51", "2001:db8:8:e000::/51"]
243
+ assert_equal arr, @network2.split(8).map {|s| s.to_string}
244
+ arr = ["2001:db8:8::/51", "2001:db8:8:2000::/51", "2001:db8:8:4000::/51",
245
+ "2001:db8:8:6000::/51", "2001:db8:8:8000::/51", "2001:db8:8:a000::/51",
246
+ "2001:db8:8:c000::/50"]
247
+ assert_equal arr, @network2.split(7).map {|s| s.to_string}
248
+ arr = ["2001:db8:8::/51", "2001:db8:8:2000::/51", "2001:db8:8:4000::/51",
249
+ "2001:db8:8:6000::/51", "2001:db8:8:8000::/50", "2001:db8:8:c000::/50"]
250
+ assert_equal arr, @network2.split(6).map {|s| s.to_string}
251
+ arr = ["2001:db8:8::/51", "2001:db8:8:2000::/51", "2001:db8:8:4000::/51",
252
+ "2001:db8:8:6000::/51", "2001:db8:8:8000::/49"]
253
+ assert_equal arr, @network2.split(5).map {|s| s.to_string}
254
+ arr = ["2001:db8:8::/50", "2001:db8:8:4000::/50", "2001:db8:8:8000::/50",
255
+ "2001:db8:8:c000::/50"]
256
+ assert_equal arr, @network2.split(4).map {|s| s.to_string}
257
+ arr = ["2001:db8:8::/50", "2001:db8:8:4000::/50", "2001:db8:8:8000::/49"]
258
+ assert_equal arr, @network2.split(3).map {|s| s.to_string}
259
+ arr = ["2001:db8:8::/49", "2001:db8:8:8000::/49"]
260
+ assert_equal arr, @network2.split(2).map {|s| s.to_string}
261
+ arr = ["2001:db8:8::/48"]
262
+ assert_equal arr, @network2.split(1).map {|s| s.to_string}
263
+ end
264
+
265
+ def test_method_literal
266
+ str = "2001-0db8-0000-0000-0008-0800-200c-417a.ipv6-literal.net"
267
+ assert_equal str, @ip.literal
268
+ end
269
+
270
+ def test_method_group
271
+ @arr.each_with_index do |val,index|
272
+ assert_equal val, @ip[index]
273
+ end
274
+ end
275
+
276
+ def test_method_ipv4?
277
+ assert_equal false, @ip.ipv4?
278
+ end
279
+
280
+ def test_method_ipv6?
281
+ assert_equal true, @ip.ipv6?
282
+ end
283
+
284
+ def test_method_network?
285
+ assert_equal true, @network.network?
286
+ assert_equal false, @ip.network?
287
+ end
288
+
289
+ def test_method_network_u128
290
+ assert_equal 42540766411282592856903984951653826560, @ip.network_u128
291
+ end
292
+
293
+ def test_method_broadcast_u128
294
+ assert_equal 42540766411282592875350729025363378175, @ip.broadcast_u128
295
+ end
296
+
297
+ def test_method_size
298
+ ip = @klass.new("2001:db8::8:800:200c:417a/64")
299
+ assert_equal 2**64, ip.size
300
+ ip = @klass.new("2001:db8::8:800:200c:417a/32")
301
+ assert_equal 2**96, ip.size
302
+ ip = @klass.new("2001:db8::8:800:200c:417a/120")
303
+ assert_equal 2**8, ip.size
304
+ ip = @klass.new("2001:db8::8:800:200c:417a/124")
305
+ assert_equal 2**4, ip.size
306
+ end
307
+
308
+ def test_method_include?
309
+ assert_equal true, @ip.include?(@ip)
310
+ # test prefix on same address
311
+ included = @klass.new "2001:db8::8:800:200c:417a/128"
312
+ not_included = @klass.new "2001:db8::8:800:200c:417a/46"
313
+ assert_equal true, @ip.include?(included)
314
+ assert_equal false, @ip.include?(not_included)
315
+ # test address on same prefix
316
+ included = @klass.new "2001:db8::8:800:200c:0/64"
317
+ not_included = @klass.new "2001:db8:1::8:800:200c:417a/64"
318
+ assert_equal true, @ip.include?(included)
319
+ assert_equal false, @ip.include?(not_included)
320
+ # general test
321
+ included = @klass.new "2001:db8::8:800:200c:1/128"
322
+ not_included = @klass.new "2001:db8:1::8:800:200c:417a/76"
323
+ assert_equal true, @ip.include?(included)
324
+ assert_equal false, @ip.include?(not_included)
325
+ end
326
+
327
+ def test_method_include_all?
328
+ ip = @klass.new("2001:db8:8:800::1/64")
329
+ addr1 = @klass.new("2001:db8:8:800::2/64")
330
+ addr2 = @klass.new("2001:db8:8:800::8/64")
331
+ assert_equal true, ip.include_all?(addr1,addr2)
332
+ assert_equal false, ip.include_all?(addr1, @klass.new("2002:db8:8:800::2/64"))
333
+ end
334
+
335
+ def test_method_to_hex
336
+ assert_equal @hex, @ip.to_hex
337
+ end
338
+
339
+ def test_method_to_s
340
+ assert_equal "2001:db8::8:800:200c:417a", @ip.to_s
341
+ end
342
+
343
+ def test_method_to_string
344
+ assert_equal "2001:db8::8:800:200c:417a/64", @ip.to_string
345
+ end
346
+
347
+ def test_method_to_string_uncompressed
348
+ str = "2001:0db8:0000:0000:0008:0800:200c:417a/64"
349
+ assert_equal str, @ip.to_string_uncompressed
350
+ end
351
+
352
+ def test_method_data
353
+ if RUBY_VERSION < "2.0"
354
+ str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
355
+ else
356
+ str = " \x01\r\xB8\x00\x00\x00\x00\x00\b\b\x00 \fAz".b
357
+ end
358
+ assert_equal str, @ip.data
359
+ end
360
+
361
+ def test_method_reverse
362
+ 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"
363
+ assert_equal str, @klass.new("3ffe:505:2::f").reverse
364
+ end
365
+
366
+ def test_method_compressed
367
+ assert_equal "1:1:1::1", @klass.new("1:1:1:0:0:0:0:1").compressed
368
+ assert_equal "1:0:1::1", @klass.new("1:0:1:0:0:0:0:1").compressed
369
+ assert_equal "1:0:0:1::1", @klass.new("1:0:0:1:0:0:0:1").compressed
370
+ assert_equal "1::1:0:0:1", @klass.new("1:0:0:0:1:0:0:1").compressed
371
+ assert_equal "1::1", @klass.new("1:0:0:0:0:0:0:1").compressed
372
+ end
373
+
374
+ def test_method_unspecified?
375
+ assert_equal true, @klass.new("::").unspecified?
376
+ assert_equal false, @ip.unspecified?
377
+ end
378
+
379
+ def test_method_loopback?
380
+ assert_equal true, @klass.new("::1").loopback?
381
+ assert_equal false, @ip.loopback?
382
+ end
383
+
384
+ def test_method_link_local?
385
+ @link_local.each do |addr|
386
+ assert_equal true, @klass.new(addr).link_local?, addr
387
+ end
388
+ @not_link_local.each do |addr|
389
+ assert_equal false, @klass.new(addr).link_local?, addr
390
+ end
391
+ end
392
+
393
+ def test_method_unique_local?
394
+ @unique_local.each do |addr|
395
+ assert_equal true, @klass.new(addr).unique_local?
396
+ end
397
+ @not_unique_local.each do |addr|
398
+ assert_equal false, @klass.new(addr).unique_local?
399
+ end
400
+ end
401
+
402
+ def test_method_network
403
+ @networks.each do |addr,net|
404
+ ip = @klass.new addr
405
+ assert_instance_of @klass, ip.network
406
+ assert_equal net, ip.network.to_string
407
+ end
408
+ end
409
+
410
+ def test_method_each
411
+ ip = @klass.new("2001:db8::4/125")
412
+ arr = []
413
+ ip.each {|i| arr << i.compressed}
414
+ expected = ["2001:db8::","2001:db8::1","2001:db8::2",
415
+ "2001:db8::3","2001:db8::4","2001:db8::5",
416
+ "2001:db8::6","2001:db8::7"]
417
+ assert_equal expected, arr
418
+ end
419
+
420
+ def test_method_succ
421
+ ip = @klass.new("2001:db8:0:cd30::/64")
422
+ assert_instance_of @klass, ip.succ
423
+ assert_equal "2001:db8:0:cd30::1/64", ip.succ.to_string
424
+ ip = @klass.new("::")
425
+ assert_instance_of @klass, ip.succ
426
+ assert_equal "::1/128", ip.succ.to_string
427
+ ip = @klass.new("::1")
428
+ assert_instance_of @klass, ip.succ
429
+ assert_equal "::2/128", ip.succ.to_string
430
+ ip = @klass.new("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64")
431
+ assert_instance_of @klass, ip.succ
432
+ assert_equal "::/64", ip.succ.to_string
433
+ end
434
+
435
+ def test_method_pred
436
+ ip = @klass.new("2001:db8:0:cd30::/64")
437
+ assert_instance_of @klass, ip.pred
438
+ assert_equal "2001:db8:0:cd2f:ffff:ffff:ffff:ffff/64", ip.pred.to_string
439
+ ip = @klass.new("::")
440
+ assert_instance_of @klass, ip.pred
441
+ assert_equal "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128", ip.pred.to_string
442
+ ip = @klass.new("::1")
443
+ assert_instance_of @klass, ip.pred
444
+ assert_equal "::/128", ip.pred.to_string
445
+ ip = @klass.new("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64")
446
+ assert_instance_of @klass, ip.pred
447
+ assert_equal "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe/64", ip.pred.to_string
448
+ end
449
+
450
+ def test_allocate_addresses
451
+ ip = @klass.new("2001:db8::4/125")
452
+ ip1 = ip.allocate
453
+ ip2 = ip.allocate
454
+ ip3 = ip.allocate
455
+ assert_equal "2001:db8::1", ip1.compressed
456
+ assert_equal "2001:db8::2", ip2.compressed
457
+ assert_equal "2001:db8::3", ip3.compressed
458
+ end
459
+
460
+ def test_allocate_can_skip_addresses
461
+ ip = @klass.new("2001:db8::4/125")
462
+ ip1 = ip.allocate(2)
463
+ assert_equal "2001:db8::3", ip1.compressed
464
+ end
465
+
466
+ def test_allocate_will_raise_stopiteration
467
+ ip = @klass.new("2001:db8::4/125")
468
+ ip.allocate(6)
469
+ assert_raises (StopIteration) do
470
+ ip.allocate
471
+ end
472
+ end
473
+
474
+ def test_method_compare
475
+ ip1 = @klass.new("2001:db8:1::1/64")
476
+ ip2 = @klass.new("2001:db8:2::1/64")
477
+ ip3 = @klass.new("2001:db8:1::2/64")
478
+ ip4 = @klass.new("2001:db8:1::1/65")
479
+
480
+ # ip2 should be greater than ip1
481
+ assert_equal true, ip2 > ip1
482
+ assert_equal false, ip1 > ip2
483
+ assert_equal false, ip2 < ip1
484
+ # ip3 should be less than ip2
485
+ assert_equal true, ip2 > ip3
486
+ assert_equal false, ip2 < ip3
487
+ # ip1 should be less than ip3
488
+ assert_equal true, ip1 < ip3
489
+ assert_equal false, ip1 > ip3
490
+ assert_equal false, ip3 < ip1
491
+ # ip1 should be equal to itself
492
+ assert_equal true, ip1 == ip1
493
+ # ip4 should be greater than ip1
494
+ assert_equal true, ip1 < ip4
495
+ assert_equal false, ip1 > ip4
496
+ # test sorting
497
+ arr = ["2001:db8:1::1/64","2001:db8:1::1/65",
498
+ "2001:db8:1::2/64","2001:db8:2::1/64"]
499
+ assert_equal arr, [ip1,ip2,ip3,ip4].sort.map{|s| s.to_string}
500
+ # compare with alien thing
501
+ ip1 = @klass.new('::1')
502
+ ip2 = IPAddress::IPv4.new('127.0.0.1')
503
+ not_ip = String
504
+ assert_nil ip1 <=> ip2
505
+ assert_nil ip1 <=> not_ip
506
+ end
507
+
508
+ def test_method_subnet
509
+ assert_raises(ArgumentError) {@network.subnet(63)}
510
+ assert_raises(ArgumentError) {@network.subnet(33)}
511
+ assert_equal %w(2001:db8:8:800::/66 2001:db8:8:800:4000::/66
512
+ 2001:db8:8:800:8000::/66 2001:db8:8:800:c000::/66),
513
+ @network.subnet(66).map {|s| s.to_string}
514
+ assert_equal %w(2001:db8:8:800::/65 2001:db8:8:800:8000::/65),
515
+ @network.subnet(65).map {|s| s.to_string}
516
+ assert_equal ["2001:db8:8:800::/64"], @network.subnet(64).map {|s| s.to_string}
517
+ end
518
+
519
+ def test_method_supernet
520
+ assert_raises(ArgumentError) {@ip.supernet(67)}
521
+ assert_equal "::/0", @ip.supernet(0).to_string
522
+ assert_equal "::/0", @ip.supernet(-2).to_string
523
+ assert_equal "2001:db8::/63", @ip.supernet(63).to_string
524
+ assert_equal "2001:db8::/32", @ip.supernet(32).to_string
525
+ assert_equal "2001::/20", @ip.supernet(20).to_string
526
+ assert_equal "2000::/8", @ip.supernet(8).to_string
527
+ assert_equal "2001:db8::/32", @klass.new("2001:db8:8:800::1/64").supernet(32).to_string
528
+ assert_equal "2001:c00::/22", @klass.new("2001:db8:8:800::1/64").supernet(22).to_string
529
+ end
530
+
531
+ def test_classmethod_expand
532
+ compressed = "2001:db8:0:cd30::"
533
+ expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000"
534
+ assert_equal expanded, @klass.expand(compressed)
535
+ refute_equal expanded, @klass.expand("2001:0db8:0::cd3")
536
+ refute_equal expanded, @klass.expand("2001:0db8::cd30")
537
+ refute_equal expanded, @klass.expand("2001:0db8::cd3")
538
+ end
539
+
540
+ def test_classmethod_compress
541
+ compressed = "2001:db8:0:cd30::"
542
+ expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000"
543
+ assert_equal compressed, @klass.compress(expanded)
544
+ refute_equal compressed, @klass.compress("2001:0db8:0::cd3")
545
+ refute_equal compressed, @klass.compress("2001:0db8::cd30")
546
+ refute_equal compressed, @klass.compress("2001:0db8::cd3")
547
+ end
548
+
549
+ def test_classmethod_parse_data
550
+ str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
551
+ ip = @klass.parse_data str
552
+ assert_instance_of @klass, ip
553
+ assert_equal "2001:0db8:0000:0000:0008:0800:200c:417a", ip.address
554
+ assert_equal "2001:db8::8:800:200c:417a/128", ip.to_string
555
+ end
556
+
557
+ def test_classhmethod_parse_u128
558
+ @valid_ipv6.each do |ip,num|
559
+ assert_equal @klass.new(ip).to_s, @klass.parse_u128(num).to_s
560
+ end
561
+ end
562
+
563
+ def test_classmethod_parse_hex
564
+ assert_equal @ip.to_s, @klass.parse_hex(@hex,64).to_s
565
+ end
566
+
567
+ def test_group_updates
568
+ ip = @klass.new("2001:db8::8:800:200c:417a/64")
569
+ ip[2] = '1234'
570
+ assert_equal "2001:db8:4d2:0:8:800:200c:417a/64", ip.to_string
571
+ end
572
+
573
+ def test_finds_adjacent_subnet
574
+ refute @klass.new("::/0").find_adjacent_subnet
575
+ assert_equal "2001:db9::/32", @klass.new("2001:db8::/32").find_adjacent_subnet
576
+ assert_equal "2001:db8:0:1::/64", @klass.new("2001:db8::/64").find_adjacent_subnet
577
+ assert_equal "2001:db8:8:2000::/51", @klass.new("2001:db8:8::/51").find_adjacent_subnet
578
+ # assert_equal "", @klass.new("").find_adjacent_subnet
579
+ # assert_equal "", @klass.new("").find_adjacent_subnet
580
+ end
581
+
582
+ end # class IPv6Test
583
+
584
+ class IPv6UnspecifiedTest < Minitest::Test
585
+
586
+ def setup
587
+ @klass = IPAddress::IPv6::Unspecified
588
+ @ip = @klass.new
589
+ @s = "::"
590
+ @str = "::/128"
591
+ @string = "0000:0000:0000:0000:0000:0000:0000:0000/128"
592
+ @u128 = 0
593
+ @address = "::"
594
+ end
595
+
596
+ def test_initialize
597
+ assert_instance_of @klass, @ip
598
+ end
599
+
600
+ def test_attributes
601
+ assert_equal @address, @ip.compressed
602
+ assert_equal 128, @ip.prefix
603
+ assert_equal true, @ip.unspecified?
604
+ assert_equal @s, @ip.to_s
605
+ assert_equal @str, @ip.to_string
606
+ assert_equal @string, @ip.to_string_uncompressed
607
+ assert_equal @u128, @ip.to_u128
608
+ end
609
+
610
+ def test_method_ipv6?
611
+ assert_equal true, @ip.ipv6?
612
+ end
613
+
614
+ end # class IPv6UnspecifiedTest
615
+
616
+
617
+ class IPv6LoopbackTest < Minitest::Test
618
+
619
+ def setup
620
+ @klass = IPAddress::IPv6::Loopback
621
+ @ip = @klass.new
622
+ @s = "::1"
623
+ @str = "::1/128"
624
+ @string = "0000:0000:0000:0000:0000:0000:0000:0001/128"
625
+ @u128 = 1
626
+ @address = "::1"
627
+ end
628
+
629
+ def test_initialize
630
+ assert_instance_of @klass, @ip
631
+ end
632
+
633
+ def test_attributes
634
+ assert_equal @address, @ip.compressed
635
+ assert_equal 128, @ip.prefix
636
+ assert_equal true, @ip.loopback?
637
+ assert_equal @s, @ip.to_s
638
+ assert_equal @str, @ip.to_string
639
+ assert_equal @string, @ip.to_string_uncompressed
640
+ assert_equal @u128, @ip.to_u128
641
+ end
642
+
643
+ def test_method_ipv6?
644
+ assert_equal true, @ip.ipv6?
645
+ end
646
+
647
+ end # class IPv6LoopbackTest
648
+
649
+ class IPv6MappedTest < Minitest::Test
650
+
651
+ def setup
652
+ @klass = IPAddress::IPv6::Mapped
653
+ @ip = @klass.new("::172.16.10.1")
654
+ @s = "::ffff:172.16.10.1"
655
+ @str = "::ffff:172.16.10.1/128"
656
+ @string = "0000:0000:0000:0000:0000:ffff:ac10:0a01/128"
657
+ @u128 = 281473568475649
658
+ @address = "::ffff:ac10:a01"
659
+
660
+ @valid_mapped = {'::13.1.68.3' => 281470899930115,
661
+ '0:0:0:0:0:ffff:129.144.52.38' => 281472855454758,
662
+ '::ffff:129.144.52.38' => 281472855454758}
663
+
664
+ @valid_mapped_ipv6 = {'::0d01:4403' => 281470899930115,
665
+ '0:0:0:0:0:ffff:8190:3426' => 281472855454758,
666
+ '::ffff:8190:3426' => 281472855454758}
667
+
668
+ @valid_mapped_ipv6_conversion = {'::0d01:4403' => "13.1.68.3",
669
+ '0:0:0:0:0:ffff:8190:3426' => "129.144.52.38",
670
+ '::ffff:8190:3426' => "129.144.52.38"}
671
+
672
+ end
673
+
674
+ def test_initialize
675
+ assert_instance_of @klass, @ip
676
+ @valid_mapped.each do |ip, u128|
677
+ assert_equal u128, @klass.new(ip).to_u128
678
+ end
679
+ @valid_mapped_ipv6.each do |ip, u128|
680
+ assert_equal u128, @klass.new(ip).to_u128
681
+ end
682
+ end
683
+
684
+ def test_mapped_from_ipv6_conversion
685
+ @valid_mapped_ipv6_conversion.each do |ip6,ip4|
686
+ assert_equal ip4, @klass.new(ip6).ipv4.to_s
687
+ end
688
+ end
689
+
690
+ def test_attributes
691
+ assert_equal @address, @ip.compressed
692
+ assert_equal 128, @ip.prefix
693
+ assert_equal @s, @ip.to_s
694
+ assert_equal @str, @ip.to_string
695
+ assert_equal @string, @ip.to_string_uncompressed
696
+ assert_equal @u128, @ip.to_u128
697
+ end
698
+
699
+ def test_method_ipv6?
700
+ assert_equal true, @ip.ipv6?
701
+ end
702
+
703
+ def test_mapped?
704
+ assert_equal true, @ip.mapped?
705
+ end
706
+
707
+ end # class IPv6MappedTest