ipaddress 0.8.0 → 0.8.2

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.
@@ -214,8 +214,8 @@ module IPAddress
214
214
  # #=> 64
215
215
  #
216
216
  def initialize(num=128)
217
- unless (1..128).include? num.to_i
218
- raise ArgumentError, "Prefix must be in range 1..128, got: #{num}"
217
+ unless (0..128).include? num.to_i
218
+ raise ArgumentError, "Prefix must be in range 0..128, got: #{num}"
219
219
  end
220
220
  super(num.to_i)
221
221
  end
@@ -0,0 +1,3 @@
1
+ module Ipaddress
2
+ VERSION = "0.8.2"
3
+ end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class IPv4Test < Test::Unit::TestCase
3
+ class IPv4Test < Minitest::Test
4
4
 
5
5
  def setup
6
6
  @klass = IPAddress::IPv4
@@ -26,7 +26,8 @@ class IPv4Test < Test::Unit::TestCase
26
26
  "10.0.0.0/8" => "255.0.0.0",
27
27
  "172.16.0.0/16" => "255.255.0.0",
28
28
  "192.168.0.0/24" => "255.255.255.0",
29
- "192.168.100.4/30" => "255.255.255.252"}
29
+ "192.168.100.4/30" => "255.255.255.252",
30
+ "192.168.12.4/32" => "255.255.255.255"}
30
31
 
31
32
  @decimal_values ={
32
33
  "0.0.0.0/0" => 0,
@@ -34,7 +35,13 @@ class IPv4Test < Test::Unit::TestCase
34
35
  "172.16.0.0/16" => 2886729728,
35
36
  "192.168.0.0/24" => 3232235520,
36
37
  "192.168.100.4/30" => 3232261124}
37
-
38
+
39
+ @hex_values = {
40
+ "10.0.0.0" => "0a000000",
41
+ "172.16.5.4" => "ac100504",
42
+ "192.168.100.4" => "c0a86404",
43
+ }
44
+
38
45
  @ip = @klass.new("172.16.10.1/24")
39
46
  @network = @klass.new("172.16.10.0/24")
40
47
 
@@ -42,13 +49,17 @@ class IPv4Test < Test::Unit::TestCase
42
49
  "10.0.0.0/8" => "10.255.255.255/8",
43
50
  "172.16.0.0/16" => "172.16.255.255/16",
44
51
  "192.168.0.0/24" => "192.168.0.255/24",
45
- "192.168.100.4/30" => "192.168.100.7/30"}
52
+ "192.168.100.4/30" => "192.168.100.7/30",
53
+ "192.168.12.3/31" => "255.255.255.255/31",
54
+ "10.0.0.1/32" => "10.0.0.1/32"}
46
55
 
47
56
  @networks = {
48
57
  "10.5.4.3/8" => "10.0.0.0/8",
49
58
  "172.16.5.4/16" => "172.16.0.0/16",
50
59
  "192.168.4.3/24" => "192.168.4.0/24",
51
- "192.168.100.5/30" => "192.168.100.4/30"}
60
+ "192.168.100.5/30" => "192.168.100.4/30",
61
+ "192.168.1.3/31" => "192.168.1.2/31",
62
+ "192.168.2.5/32" => "192.168.2.5/32"}
52
63
 
53
64
  @class_a = @klass.new("10.0.0.1/8")
54
65
  @class_b = @klass.new("172.16.0.1/16")
@@ -58,6 +69,11 @@ class IPv4Test < Test::Unit::TestCase
58
69
  "10.1.1.1" => 8,
59
70
  "150.1.1.1" => 16,
60
71
  "200.1.1.1" => 24 }
72
+
73
+ @in_range = {
74
+ "10.32.0.1" => ["10.32.0.253", 253],
75
+ "192.0.0.0" => ["192.1.255.255", 131072]
76
+ }
61
77
 
62
78
  end
63
79
 
@@ -67,25 +83,19 @@ class IPv4Test < Test::Unit::TestCase
67
83
  assert_instance_of @klass, ip
68
84
  end
69
85
  assert_instance_of IPAddress::Prefix32, @ip.prefix
70
- assert_raise (ArgumentError) do
86
+ assert_raises (ArgumentError) do
71
87
  @klass.new
72
88
  end
73
- assert_nothing_raised do
74
- @klass.new "10.0.0.0/8"
75
- end
76
89
  end
77
90
 
78
91
  def test_initialize_format_error
79
92
  @invalid_ipv4.each do |i|
80
- assert_raise(ArgumentError) {@klass.new(i)}
93
+ assert_raises(ArgumentError) {@klass.new(i)}
81
94
  end
82
- assert_raise (ArgumentError) {@klass.new("10.0.0.0/asd")}
95
+ assert_raises (ArgumentError) {@klass.new("10.0.0.0/asd")}
83
96
  end
84
97
 
85
98
  def test_initialize_without_prefix
86
- assert_nothing_raised do
87
- @klass.new("10.10.0.0")
88
- end
89
99
  ip = @klass.new("10.10.0.0")
90
100
  assert_instance_of IPAddress::Prefix32, ip.prefix
91
101
  assert_equal 32, ip.prefix.to_i
@@ -105,11 +115,15 @@ class IPv4Test < Test::Unit::TestCase
105
115
  end
106
116
 
107
117
  def test_initialize_should_require_ip
108
- assert_raise(ArgumentError) { @klass.new }
118
+ assert_raises(ArgumentError) { @klass.new }
109
119
  end
110
120
 
111
121
  def test_method_data
112
- assert_equal "\254\020\n\001", @ip.data
122
+ if RUBY_VERSION < "2.0"
123
+ assert_equal "\254\020\n\001", @ip.data
124
+ else
125
+ assert_equal "\xAC\x10\n\x01".b, @ip.data
126
+ end
113
127
  end
114
128
 
115
129
  def test_method_to_string
@@ -140,10 +154,22 @@ class IPv4Test < Test::Unit::TestCase
140
154
  end
141
155
  end
142
156
 
157
+ def test_method_to_hex
158
+ @hex_values.each do |addr,hex|
159
+ ip = @klass.new(addr)
160
+ assert_equal hex, ip.to_hex
161
+ end
162
+ end
163
+
143
164
  def test_method_network?
144
165
  assert_equal true, @network.network?
145
166
  assert_equal false, @ip.network?
146
167
  end
168
+
169
+ def test_one_address_network
170
+ network = @klass.new("172.16.10.1/32")
171
+ assert_equal false, network.network?
172
+ end
147
173
 
148
174
  def test_method_broadcast
149
175
  @broadcast.each do |addr,bcast|
@@ -173,6 +199,12 @@ class IPv4Test < Test::Unit::TestCase
173
199
  ip = @klass.new("192.168.100.50/24")
174
200
  assert_instance_of @klass, ip.first
175
201
  assert_equal "192.168.100.1", ip.first.to_s
202
+ ip = @klass.new("192.168.100.50/32")
203
+ assert_instance_of @klass, ip.first
204
+ assert_equal "192.168.100.50", ip.first.to_s
205
+ ip = @klass.new("192.168.100.50/31")
206
+ assert_instance_of @klass, ip.first
207
+ assert_equal "192.168.100.50", ip.first.to_s
176
208
  end
177
209
 
178
210
  def test_method_last
@@ -182,6 +214,12 @@ class IPv4Test < Test::Unit::TestCase
182
214
  ip = @klass.new("192.168.100.50/24")
183
215
  assert_instance_of @klass, ip.last
184
216
  assert_equal "192.168.100.254", ip.last.to_s
217
+ ip = @klass.new("192.168.100.50/32")
218
+ assert_instance_of @klass, ip.last
219
+ assert_equal "192.168.100.50", ip.last.to_s
220
+ ip = @klass.new("192.168.100.50/31")
221
+ assert_instance_of @klass, ip.last
222
+ assert_equal "192.168.100.51", ip.last.to_s
185
223
  end
186
224
 
187
225
  def test_method_each_host
@@ -378,8 +416,8 @@ class IPv4Test < Test::Unit::TestCase
378
416
  end
379
417
 
380
418
  def test_method_split
381
- assert_raise(ArgumentError) {@ip.split(0)}
382
- assert_raise(ArgumentError) {@ip.split(257)}
419
+ assert_raises(ArgumentError) {@ip.split(0)}
420
+ assert_raises(ArgumentError) {@ip.split(257)}
383
421
 
384
422
  assert_equal @ip.network, @ip.split(1).first
385
423
 
@@ -409,9 +447,8 @@ class IPv4Test < Test::Unit::TestCase
409
447
  end
410
448
 
411
449
  def test_method_subnet
412
- assert_raise(ArgumentError) {@network.subnet(23)}
413
- assert_raise(ArgumentError) {@network.subnet(33)}
414
- assert_nothing_raised {@ip.subnet(30)}
450
+ assert_raises(ArgumentError) {@network.subnet(23)}
451
+ assert_raises(ArgumentError) {@network.subnet(33)}
415
452
  arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26",
416
453
  "172.16.10.192/26"]
417
454
  assert_equal arr, @network.subnet(26).map {|s| s.to_string}
@@ -422,7 +459,7 @@ class IPv4Test < Test::Unit::TestCase
422
459
  end
423
460
 
424
461
  def test_method_supernet
425
- assert_raise(ArgumentError) {@ip.supernet(24)}
462
+ assert_raises(ArgumentError) {@ip.supernet(24)}
426
463
  assert_equal "0.0.0.0/0", @ip.supernet(0).to_string
427
464
  assert_equal "0.0.0.0/0", @ip.supernet(-2).to_string
428
465
  assert_equal "172.16.10.0/23", @ip.supernet(23).to_string
@@ -524,9 +561,40 @@ class IPv4Test < Test::Unit::TestCase
524
561
  assert_equal prefix, res.prefix
525
562
  assert_equal "#{ip}/#{prefix}", res.to_string
526
563
  end
527
- assert_raise(ArgumentError){ @klass.parse_classful("192.168.256.257") }
564
+ assert_raises(ArgumentError){ @klass.parse_classful("192.168.256.257") }
528
565
  end
529
566
 
567
+ def test_network_split
568
+ @classful.each do |ip,net|
569
+ x = @klass.new("#{ip}/#{net}")
570
+ assert_equal x.split(1).length, 1
571
+ assert_equal x.split(2).length, 2
572
+ assert_equal x.split(32).length, 32
573
+ assert_equal x.split(256).length, 256
574
+ end
575
+ end
576
+
577
+ def test_in_range
578
+ @in_range.each do |s,d|
579
+ ip = @klass.new(s)
580
+ assert_equal ip.to(d[0]).length, d[1]
581
+ end
582
+ end
583
+
584
+ def test_octect_updates
585
+ ip = @klass.new("10.0.1.15/32")
586
+ ip[1] = 15
587
+ assert_equal "10.15.1.15/32", ip.to_string
588
+
589
+ ip = @klass.new("172.16.100.1")
590
+ ip[3] = 200
591
+ assert_equal "172.16.100.200/32", ip.to_string
592
+
593
+ ip = @klass.new("192.168.199.0/24")
594
+ ip[2] = 200
595
+ assert_equal "192.168.200.0/24", ip.to_string
596
+ end
597
+
530
598
  end # class IPv4Test
531
599
 
532
600
 
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class IPv6Test < Test::Unit::TestCase
3
+ class IPv6Test < Minitest::Test
4
4
 
5
5
  def setup
6
6
  @klass = IPAddress::IPv6
@@ -31,7 +31,9 @@ class IPv6Test < Test::Unit::TestCase
31
31
  "1080::8:800:200C:417A" => 21932261930451111902915077091070067066}
32
32
 
33
33
  @invalid_ipv6 = [":1:2:3:4:5:6:7",
34
- ":1:2:3:4:5:6:7"]
34
+ ":1:2:3:4:5:6:7",
35
+ "2002:516:2:200",
36
+ "dd"]
35
37
 
36
38
  @networks = {
37
39
  "2001:db8:1:1:1:1:1:1/32" => "2001:db8::/32",
@@ -51,15 +53,12 @@ class IPv6Test < Test::Unit::TestCase
51
53
 
52
54
  def test_initialize
53
55
  assert_instance_of @klass, @ip
54
- @valid_ipv6.keys.each do |ip|
55
- assert_nothing_raised {@klass.new ip}
56
- end
57
56
  @invalid_ipv6.each do |ip|
58
- assert_raise(ArgumentError) {@klass.new ip}
57
+ assert_raises(ArgumentError) {@klass.new ip}
59
58
  end
60
59
  assert_equal 64, @ip.prefix
61
60
 
62
- assert_raise(ArgumentError) {
61
+ assert_raises(ArgumentError) {
63
62
  @klass.new "::10.1.1.1"
64
63
  }
65
64
  end
@@ -180,7 +179,11 @@ class IPv6Test < Test::Unit::TestCase
180
179
  end
181
180
 
182
181
  def test_method_data
183
- str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
182
+ if RUBY_VERSION < "2.0"
183
+ str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
184
+ else
185
+ str = " \x01\r\xB8\x00\x00\x00\x00\x00\b\b\x00 \fAz".b
186
+ end
184
187
  assert_equal str, @ip.data
185
188
  end
186
189
 
@@ -257,18 +260,18 @@ class IPv6Test < Test::Unit::TestCase
257
260
  compressed = "2001:db8:0:cd30::"
258
261
  expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000"
259
262
  assert_equal expanded, @klass.expand(compressed)
260
- assert_not_equal expanded, @klass.expand("2001:0db8:0:cd3")
261
- assert_not_equal expanded, @klass.expand("2001:0db8::cd30")
262
- assert_not_equal expanded, @klass.expand("2001:0db8::cd3")
263
+ refute_equal expanded, @klass.expand("2001:0db8:0::cd3")
264
+ refute_equal expanded, @klass.expand("2001:0db8::cd30")
265
+ refute_equal expanded, @klass.expand("2001:0db8::cd3")
263
266
  end
264
267
 
265
268
  def test_classmethod_compress
266
269
  compressed = "2001:db8:0:cd30::"
267
270
  expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000"
268
271
  assert_equal compressed, @klass.compress(expanded)
269
- assert_not_equal compressed, @klass.compress("2001:0db8:0:cd3")
270
- assert_not_equal compressed, @klass.compress("2001:0db8::cd30")
271
- assert_not_equal compressed, @klass.compress("2001:0db8::cd3")
272
+ refute_equal compressed, @klass.compress("2001:0db8:0::cd3")
273
+ refute_equal compressed, @klass.compress("2001:0db8::cd30")
274
+ refute_equal compressed, @klass.compress("2001:0db8::cd3")
272
275
  end
273
276
 
274
277
  def test_classmethod_parse_data
@@ -289,9 +292,15 @@ class IPv6Test < Test::Unit::TestCase
289
292
  assert_equal @ip.to_s, @klass.parse_hex(@hex,64).to_s
290
293
  end
291
294
 
295
+ def test_group_updates
296
+ ip = @klass.new("2001:db8::8:800:200c:417a/64")
297
+ ip[2] = '1234'
298
+ assert_equal "2001:db8:4d2:0:8:800:200c:417a/64", ip.to_string
299
+ end
300
+
292
301
  end # class IPv6Test
293
302
 
294
- class IPv6UnspecifiedTest < Test::Unit::TestCase
303
+ class IPv6UnspecifiedTest < Minitest::Test
295
304
 
296
305
  def setup
297
306
  @klass = IPAddress::IPv6::Unspecified
@@ -304,7 +313,6 @@ class IPv6UnspecifiedTest < Test::Unit::TestCase
304
313
  end
305
314
 
306
315
  def test_initialize
307
- assert_nothing_raised {@klass.new}
308
316
  assert_instance_of @klass, @ip
309
317
  end
310
318
 
@@ -325,7 +333,7 @@ class IPv6UnspecifiedTest < Test::Unit::TestCase
325
333
  end # class IPv6UnspecifiedTest
326
334
 
327
335
 
328
- class IPv6LoopbackTest < Test::Unit::TestCase
336
+ class IPv6LoopbackTest < Minitest::Test
329
337
 
330
338
  def setup
331
339
  @klass = IPAddress::IPv6::Loopback
@@ -338,7 +346,6 @@ class IPv6LoopbackTest < Test::Unit::TestCase
338
346
  end
339
347
 
340
348
  def test_initialize
341
- assert_nothing_raised {@klass.new}
342
349
  assert_instance_of @klass, @ip
343
350
  end
344
351
 
@@ -358,7 +365,7 @@ class IPv6LoopbackTest < Test::Unit::TestCase
358
365
 
359
366
  end # class IPv6LoopbackTest
360
367
 
361
- class IPv6MappedTest < Test::Unit::TestCase
368
+ class IPv6MappedTest < Minitest::Test
362
369
 
363
370
  def setup
364
371
  @klass = IPAddress::IPv6::Mapped
@@ -384,14 +391,11 @@ class IPv6MappedTest < Test::Unit::TestCase
384
391
  end
385
392
 
386
393
  def test_initialize
387
- assert_nothing_raised {@klass.new("::172.16.10.1")}
388
394
  assert_instance_of @klass, @ip
389
395
  @valid_mapped.each do |ip, u128|
390
- assert_nothing_raised {@klass.new ip}
391
396
  assert_equal u128, @klass.new(ip).to_u128
392
397
  end
393
398
  @valid_mapped_ipv6.each do |ip, u128|
394
- assert_nothing_raised {@klass.new ip}
395
399
  assert_equal u128, @klass.new(ip).to_u128
396
400
  end
397
401
  end
@@ -418,5 +422,5 @@ class IPv6MappedTest < Test::Unit::TestCase
418
422
  def test_mapped?
419
423
  assert_equal true, @ip.mapped?
420
424
  end
421
-
425
+
422
426
  end # class IPv6MappedTest
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+ require 'ipaddress/mongoid'
3
+
4
+ class MongoidTest < Minitest::Test
5
+
6
+ def setup
7
+ @valid_host4 = "172.16.10.1"
8
+ @valid_host6 = "2001:0db8:0000:0000:0008:0800:200c:417a"
9
+ @valid_host6_compressed = IPAddress::IPv6.compress(@valid_host6)
10
+ @valid_network4 = "#{@valid_host4}/24"
11
+ @valid_network6 = "#{@valid_host6}/96"
12
+ @valid_network6_compressed = "#{@valid_host6_compressed}/96"
13
+ @host4 = IPAddress.parse(@valid_host4)
14
+ @host6 = IPAddress.parse(@valid_host6)
15
+ @network4 = IPAddress.parse(@valid_network4)
16
+ @network6 = IPAddress.parse(@valid_network6)
17
+ @invalid_values = [nil, "", "invalid"]
18
+ end
19
+
20
+ def test_mongoize
21
+ # Instance method should be delegated to class method
22
+ assert_equal @host4.mongoize, IPAddress.mongoize(@host4)
23
+ assert_equal @network4.mongoize, IPAddress.mongoize(@network4)
24
+
25
+ # Hosts addresses should be stored without prefix
26
+ assert_equal @valid_host4, IPAddress.mongoize(@host4)
27
+ assert_equal @valid_host6, IPAddress.mongoize(@host6)
28
+ assert_equal @valid_host4, IPAddress.mongoize("#{@host4}/32")
29
+ assert_equal @valid_host6, IPAddress.mongoize("#{@host6}/128")
30
+
31
+ # Network addresses should be stored with their prefix
32
+ assert_equal @valid_network4, IPAddress.mongoize(@network4)
33
+ assert_equal @valid_network6, IPAddress.mongoize(@network6)
34
+
35
+ # IPv6 addresses should always be stored uncompressed
36
+ assert_equal @valid_host6, IPAddress.mongoize(@valid_host6_compressed)
37
+ assert_equal @valid_network6, IPAddress.mongoize(@valid_network6_compressed)
38
+
39
+ @invalid_values.each do |invalid_value|
40
+ # Invalid addresses should serialize to nil
41
+ assert_equal nil, IPAddress.mongoize(invalid_value)
42
+ end
43
+ end
44
+
45
+ def test_demongoize
46
+ # Valid stored values should be loaded with expected IPAddress type
47
+ assert_instance_of IPAddress::IPv4, IPAddress.demongoize(@valid_host4)
48
+ assert_instance_of IPAddress::IPv6, IPAddress.demongoize(@valid_host6)
49
+ assert_instance_of IPAddress::IPv4, IPAddress.demongoize(@valid_network4)
50
+ assert_instance_of IPAddress::IPv6, IPAddress.demongoize(@valid_network6)
51
+
52
+ # Valid stored values should be loaded as the original IPAddress object
53
+ assert_equal @host4, IPAddress.demongoize(@valid_host4)
54
+ assert_equal @host6, IPAddress.demongoize(@valid_host6)
55
+ assert_equal @network4, IPAddress.demongoize(@valid_network4)
56
+ assert_equal @network6, IPAddress.demongoize(@valid_network6)
57
+
58
+ @invalid_values.each do |invalid_value|
59
+ # Invalid stored value should be loaded as nil
60
+ assert_equal nil, IPAddress.demongoize(invalid_value)
61
+ end
62
+ end
63
+
64
+ def test_evolve
65
+ # evolve should delegate to mongoize
66
+ assert_equal IPAddress.mongoize(@valid_host4), IPAddress.evolve(@valid_host4)
67
+ assert_equal IPAddress.mongoize(@valid_network4), IPAddress.evolve(@valid_network4)
68
+ end
69
+
70
+ end