netaddr 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of netaddr might be problematic. Click here for more details.

@@ -0,0 +1,219 @@
1
+ module NetAddr
2
+ private
3
+
4
+ #==============================================================================#
5
+ # validate_args()
6
+ #==============================================================================#
7
+
8
+ # validate options hash
9
+ #
10
+ def validate_args(to_validate,known_args)
11
+ to_validate.each do |x|
12
+ raise ArgumentError, "Unrecognized argument #{x}. Valid arguments are " +
13
+ "#{known_args.join(',')}" if (!known_args.include?(x))
14
+ end
15
+ end
16
+ module_function :validate_args
17
+
18
+ #==============================================================================#
19
+ # validate_ip_int()
20
+ #==============================================================================#
21
+
22
+ def validate_ip_int(ip,version)
23
+ version = 4 if (!version && ip < 2**32)
24
+ if (version == 4)
25
+ raise ValidationError, "#{ip} is invalid for IPv4 (Integer is out of bounds)." if ( (ip < 0) || (ip > 2**32-1) )
26
+ else
27
+ raise ValidationError, "#{ip} is invalid for both IPv4 and IPv6 (Integer is out of bounds)." if ( (ip < 0) || (ip > 2**128-1) )
28
+ version = 6
29
+ end
30
+ return(version)
31
+ end
32
+ module_function :validate_ip_int
33
+
34
+ #==============================================================================#
35
+ # validate_ip_str()
36
+ #==============================================================================#
37
+
38
+ def validate_ip_str(ip,version)
39
+ # check validity of charaters
40
+ if (ip =~ /[^0-9a-fA-F\.:]/)
41
+ raise ValidationError, "#{ip} is invalid (contains invalid characters)."
42
+ end
43
+
44
+ if (version == 4)
45
+ octets = ip.split('.')
46
+ raise ValidationError, "#{ip} is invalid (IPv4 requires (4) octets)." if (octets.length != 4)
47
+
48
+ # are octets in range 0..255?
49
+ octets.each do |octet|
50
+ raise ValidationError, "#{ip} is invalid (IPv4 dotted-decimal format " +
51
+ "should not contain non-numeric characters)." if (octet =~ /[\D]/ || octet == '')
52
+ octet = octet.to_i()
53
+ if ( (octet < 0) || (octet >= 256) )
54
+ raise ValidationError, "#{ip} is invalid (IPv4 octets should be between 0 and 255)."
55
+ end
56
+ end
57
+
58
+ else
59
+ # make sure we only have at most (2) colons in a row, and then only
60
+ # (1) instance of that
61
+ if ( (ip =~ /:{3,}/) || (ip.split("::").length > 2) )
62
+ raise ValidationError, "#{ip} is invalid (IPv6 field separators (:) are bad)."
63
+ end
64
+
65
+ # set flags
66
+ shorthand = false
67
+ if (ip =~ /\./)
68
+ dotted_dec = true
69
+ else
70
+ dotted_dec = false
71
+ end
72
+
73
+ # split up by ':'
74
+ fields = []
75
+ if (ip =~ /::/)
76
+ shorthand = true
77
+ ip.split('::').each do |x|
78
+ fields.concat( x.split(':') )
79
+ end
80
+ else
81
+ fields.concat( ip.split(':') )
82
+ end
83
+
84
+ # make sure we have the correct number of fields
85
+ if (shorthand)
86
+ if ( (dotted_dec && fields.length > 6) || (!dotted_dec && fields.length > 7) )
87
+ raise ValidationError, "#{ip} is invalid (IPv6 shorthand notation has " +
88
+ "incorrect number of fields)."
89
+ end
90
+ else
91
+ if ( (dotted_dec && fields.length != 7 ) || (!dotted_dec && fields.length != 8) )
92
+ raise ValidationError, "#{ip} is invalid (IPv6 address has " +
93
+ "incorrect number of fields)."
94
+ end
95
+ end
96
+
97
+ # if dotted_dec then validate the last field
98
+ if (dotted_dec)
99
+ dotted = fields.pop()
100
+ octets = dotted.split('.')
101
+ raise ValidationError, "#{ip} is invalid (Legacy IPv4 portion of IPv6 " +
102
+ "address should contain (4) octets)." if (octets.length != 4)
103
+ octets.each do |x|
104
+ raise ValidationError, "#{ip} is invalid (egacy IPv4 portion of IPv6 " +
105
+ "address should not contain non-numeric characters)." if (x =~ /[^0-9]/ )
106
+ x = x.to_i
107
+ if ( (x < 0) || (x >= 256) )
108
+ raise ValidationError, "#{ip} is invalid (Octets of a legacy IPv4 portion of IPv6 " +
109
+ "address should be between 0 and 255)."
110
+ end
111
+ end
112
+ end
113
+
114
+ # validate hex fields
115
+ fields.each do |x|
116
+ if (x =~ /[^0-9a-fA-F]/)
117
+ raise ValidationError, "#{ip} is invalid (IPv6 address contains invalid hex characters)."
118
+ else
119
+ x = x.to_i(16)
120
+ if ( (x < 0) || (x >= 2**16) )
121
+ raise ValidationError, "#{ip} is invalid (Fields of an IPv6 address " +
122
+ "should be between 0x0 and 0xFFFF)."
123
+ end
124
+ end
125
+ end
126
+
127
+ end
128
+ return(true)
129
+ end
130
+ module_function :validate_ip_str
131
+
132
+ #==============================================================================#
133
+ # validate_netmask_int()
134
+ #==============================================================================#
135
+ def validate_netmask_int(netmask,version,packed=false)
136
+ address_len = 32
137
+ address_len = 128 if (version == 6)
138
+
139
+ if (!packed)
140
+ if (netmask > address_len || netmask < 0 )
141
+ raise ValidationError, "Netmask, #{netmask}, is out of bounds for IPv#{version}."
142
+ end
143
+ else
144
+ if (netmask >= 2**address_len || netmask < 0 )
145
+ raise ValidationError, "Packed netmask, #{netmask}, is out of bounds for IPv#{version}."
146
+ end
147
+ end
148
+ return(true)
149
+ end
150
+ module_function :validate_netmask_int
151
+
152
+ #==============================================================================#
153
+ # validate_netmask_str()
154
+ #==============================================================================#
155
+ def validate_netmask_str(netmask,version)
156
+ address_len = 32
157
+ address_len = 128 if (version == 6)
158
+
159
+ if(netmask =~ /\./) # extended netmask
160
+ all_f = 2**32-1
161
+ packed_netmask = 0
162
+
163
+ # validate & pack extended mask
164
+ begin
165
+ packed_netmask = pack_ip_addr(netmask, :Version => 4)
166
+ rescue Exception => error
167
+ raise ValidationError, "#{netmask} is improperly formed: #{error}"
168
+ end
169
+
170
+ # cycle through the bits of hostmask and compare
171
+ # with packed_mask. when we hit the firt '1' within
172
+ # packed_mask (our netmask boundary), xor hostmask and
173
+ # packed_mask. the result should be all 1's. this whole
174
+ # process is in place to make sure that we dont have
175
+ # and crazy masks such as 255.254.255.0
176
+ hostmask = 1
177
+ 32.times do
178
+ check = packed_netmask & hostmask
179
+ if ( check != 0)
180
+ hostmask = hostmask >> 1
181
+ unless ( (packed_netmask ^ hostmask) == all_f)
182
+ raise ValidationError, "#{netmask} contains '1' bits within the host portion of the netmask."
183
+ end
184
+ break
185
+ else
186
+ hostmask = hostmask << 1
187
+ hostmask = hostmask | 1
188
+ end
189
+ end
190
+
191
+ else # cidr format
192
+ # remove '/' if present
193
+ if (netmask =~ /^\// )
194
+ netmask[0] = " "
195
+ netmask.lstrip!
196
+ end
197
+
198
+ # check if we have any non numeric characters
199
+ if (netmask =~ /\D/)
200
+ raise ValidationError, "#{netmask} contains invalid characters."
201
+ end
202
+
203
+ netmask = netmask.to_i
204
+ if (netmask > address_len || netmask < 0 )
205
+ raise ValidationError, "Netmask, #{netmask}, is out of bounds for IPv#{version}."
206
+ end
207
+
208
+ end
209
+ return(true)
210
+ end
211
+ module_function :validate_netmask_str
212
+
213
+
214
+
215
+ end # module NetAddr
216
+
217
+ __END__
218
+
219
+
data/tests/cidr_test.rb CHANGED
@@ -9,6 +9,7 @@ class TestCIDR < Test::Unit::TestCase
9
9
 
10
10
  def test_create
11
11
  assert_raise(ArgumentError) {NetAddr::CIDR.create('192.168.1.0/24', :test => true)}
12
+ assert_nothing_raised(Exception){NetAddr::CIDR.create('0.0.0.0/0')}
12
13
  assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.0/24')}
13
14
  assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.0/24') }
14
15
  assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.0 255.255.255.0')}
@@ -16,13 +17,14 @@ class TestCIDR < Test::Unit::TestCase
16
17
  assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1 ') }
17
18
  assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::/64') }
18
19
  assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1/24 255.255.0.0')}
20
+ assert_nothing_raised(Exception){NetAddr::CIDR.create('::/0')}
19
21
  assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1/64')}
20
22
  assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1/64')}
21
23
  assert_nothing_raised(Exception){NetAddr::CIDR.create(0x0a0a0a0a, :PackedNetmask => 0xffffffff)}
22
- assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1', :WildcardMask => ['0.0.7.255', :inversed]) }
23
- assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1', :WildcardMask => [0x000007ff, :inversed]) }
24
- assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1', :WildcardMask => ['0000:ffff::ffff', :inversed])}
25
- assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1', :WildcardMask => [0xffff, :inversed])}
24
+ assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1', :WildcardMask => ['0.0.7.255', true]) }
25
+ assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1', :WildcardMask => [0x000007ff, true]) }
26
+ assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1', :WildcardMask => ['0000:ffff::ffff', true])}
27
+ assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1', :WildcardMask => [0xffff, true])}
26
28
 
27
29
  assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('192.168.1.1 255.255.0.0'))
28
30
  assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('192.168.1.1/24 255.255.0.0'))
@@ -31,67 +33,29 @@ class TestCIDR < Test::Unit::TestCase
31
33
  assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('10.10.10.10/32', :PackedNetmask => 0xffffff00))
32
34
 
33
35
  assert_raise(ArgumentError){ NetAddr::CIDR.create(:Version => 4) }
34
- assert_raise(NetAddr::ValidationError){ NetAddr::CIDR.create('192.168.1.1', :WildcardMask => ['0000:ffff::ffff', :inversed]) }
35
- assert_raise(NetAddr::ValidationError){ NetAddr::CIDR.create('fec0::1', :WildcardMask => ['0.0.7.255', :inversed]) }
36
- end
37
-
38
- def test_new
39
- assert_raise(ArgumentError) {NetAddr::CIDRv4.new('192.168.1.0/24', :test => true)}
40
- assert_raise(ArgumentError) {NetAddr::CIDRv6.new('fec0::1/64', :test => true)}
41
- assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.0/24')}
42
- assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.0/24') }
43
- assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.0 255.255.255.0')}
44
- assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.1') }
45
- assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.1 ') }
46
- assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.1/24 255.255.0.0')}
47
- assert_nothing_raised(Exception){NetAddr::CIDRv4.new(0x0a0a0a0a, :PackedNetmask => 0xffffffff)}
48
- assert_nothing_raised(Exception){NetAddr::CIDRv6.new('fec0::1/64')}
49
- assert_nothing_raised(Exception){NetAddr::CIDRv6.new('fec0::1/64')}
50
- assert_nothing_raised(Exception){NetAddr::CIDRv6.new('fec0::/64') }
51
-
52
- cidr = NetAddr::CIDRv4.new('192.168.1.1 255.255.0.0')
36
+ assert_raise(NetAddr::ValidationError){ NetAddr::CIDR.create('192.168.1.1', :WildcardMask => ['0000:ffff::ffff', true]) }
37
+ assert_raise(NetAddr::ValidationError){ NetAddr::CIDR.create('fec0::1', :WildcardMask => ['0.0.7.255', true]) }
38
+
39
+ cidr = NetAddr::CIDRv4.create('192.168.1.1 255.255.0.0')
53
40
  assert_equal(16, cidr.bits )
54
41
 
55
42
 
56
- cidr = NetAddr::CIDRv4.new('192.168.1.1/24 255.255.0.0')
43
+ cidr = NetAddr::CIDRv4.create('192.168.1.1/24 255.255.0.0')
57
44
  assert_equal(24, cidr.bits )
58
45
 
59
46
 
60
- cidr = NetAddr::CIDRv4.new('10.10.10.10/32 255.255.255.0')
47
+ cidr = NetAddr::CIDRv4.create('10.10.10.10/32 255.255.255.0')
61
48
  assert_equal(32, cidr.bits )
62
49
 
63
50
 
64
- cidr = NetAddr::CIDRv4.new('10.10.10.10/32', :PackedNetmask => 0xffffff00)
51
+ cidr = NetAddr::CIDRv4.create('10.10.10.10/32', :PackedNetmask => 0xffffff00)
65
52
  assert_equal(24, cidr.bits )
66
53
 
67
54
  cidr = NetAddr::CIDR.create('fec0::1/64')
68
55
  assert_equal(64, cidr.bits )
69
56
 
70
- assert_raise(ArgumentError){ NetAddr::CIDRv4.new({}) }
71
- assert_raise(ArgumentError){ NetAddr::CIDRv6.new({}) }
72
- assert_raise(ArgumentError){ NetAddr::CIDRv6.new('fec0::1/64 255.255.255.0') }
73
- assert_raise(NetAddr::ValidationError){ NetAddr::CIDRv4.new('192.168.1.0 a') }
74
- end
75
-
76
- def test_matches?
77
- cidr = NetAddr::CIDR.create('10.0.0.0/24')
78
- assert(cidr.matches?('10.0.0.22'))
79
- assert(!cidr.matches?('10.1.1.1'))
80
-
81
- cidr = NetAddr::CIDR.create('10.0.248.0')
82
- cidr.wildcard_mask('255.248.255.0')
83
- assert(cidr.matches?('10.1.248.0'))
84
- assert(!cidr.matches?('10.8.248.0'))
85
-
86
- cidr = NetAddr::CIDR.create('10.0.248.0')
87
- cidr.wildcard_mask('0.7.0.255', :inversed)
88
- assert(cidr.matches?('10.1.248.0'))
89
- assert(!cidr.matches?('10.8.248.0'))
90
-
91
- cidr = NetAddr::CIDR.create('fec0::1')
92
- cidr.wildcard_mask('0000:ffff::ffff', :inversed)
93
- assert(cidr.matches?('fec0:1::1'))
94
- assert(!cidr.matches?('fec0:0:1::1'))
57
+ assert_raise(ArgumentError){ NetAddr::CIDRv4.create({}) }
58
+ assert_raise(NetAddr::ValidationError){ NetAddr::CIDRv4.create('192.168.1.0 a') }
95
59
  end
96
60
 
97
61
  def test_cmp
@@ -203,6 +167,39 @@ class TestCIDR < Test::Unit::TestCase
203
167
 
204
168
  assert_raise(NetAddr::VersionError) { cidr4.is_contained?(cidr6_2) }
205
169
  end
170
+
171
+ def test_matches?
172
+ cidr = NetAddr::CIDR.create('10.0.0.0/24')
173
+ assert(cidr.matches?('10.0.0.22'))
174
+ assert(!cidr.matches?('10.1.1.1'))
175
+
176
+ cidr = NetAddr::CIDR.create('10.0.248.0', :WildcardMask => ['255.248.255.0'])
177
+ assert(cidr.matches?('10.1.248.0'))
178
+ assert(!cidr.matches?('10.8.248.0'))
179
+
180
+ cidr = NetAddr::CIDR.create('10.0.248.0')
181
+ cidr.set_wildcard_mask('0.7.0.255', true)
182
+ assert(cidr.matches?('10.1.248.0'))
183
+ assert(!cidr.matches?('10.8.248.0'))
184
+
185
+ cidr = NetAddr::CIDR.create('127.0.0.0')
186
+ cidr.set_wildcard_mask('0.255.255.255', true)
187
+ assert(cidr.matches?('127.0.0.1'))
188
+ assert(!cidr.matches?('128.0.0.0'))
189
+
190
+ cidr = NetAddr::CIDR.create('127.0.0.0', :WildcardMask => ['0.255.255.255', true])
191
+ assert(cidr.matches?('127.0.0.1'))
192
+ assert(!cidr.matches?('128.0.0.0'))
193
+
194
+ cidr = NetAddr::CIDR.create('fec0::1')
195
+ cidr.set_wildcard_mask('0000:ffff::ffff', true)
196
+ assert(cidr.matches?('fec0:1::1'))
197
+ assert(!cidr.matches?('fec0:0:1::1'))
198
+
199
+ cidr = NetAddr::CIDR.create('fec0::1', :WildcardMask => ['0000:ffff::ffff', true])
200
+ assert(cidr.matches?('fec0:1::1'))
201
+ assert(!cidr.matches?('fec0:0:1::1'))
202
+ end
206
203
 
207
204
  def test_mcast
208
205
  cidr4 = NetAddr::CIDR.create('224.0.0.1')
@@ -355,6 +352,13 @@ class TestCIDR < Test::Unit::TestCase
355
352
  cidr6 = NetAddr::CIDR.create('fec0::1/64')
356
353
  cidr6_2 = NetAddr::CIDR.create('fec0::/96')
357
354
 
355
+ assert(cidr4 == '192.168.1.0/24')
356
+ assert(cidr4 > '192.168.0.0/24')
357
+ assert(cidr4 < '192.168.2.0/24')
358
+ assert_equal(0, cidr4 <=> '192.168.1.0/24')
359
+ assert_equal(1, cidr4 <=> '192.168.0.0/24')
360
+ assert_equal(-1, cidr4 <=> '192.168.2.0/24')
361
+
358
362
  assert_equal('1.168.192.in-addr.arpa.', cidr4.arpa() )
359
363
  assert_equal('0.0.0.0.0.0.0.0.0.0.0.0.0.c.e.f.ip6.arpa.', cidr6.arpa() )
360
364
 
@@ -425,24 +429,24 @@ class TestCIDR < Test::Unit::TestCase
425
429
 
426
430
  def test_wildcard_mask
427
431
  cidr = NetAddr::CIDR.create('10.1.0.0/24')
428
- assert_equal('0.0.0.255', cidr.wildcard_mask(nil,:inversed))
432
+ assert_equal('0.0.0.255', cidr.wildcard_mask(true))
429
433
  assert_equal('255.255.255.0', cidr.wildcard_mask)
430
434
 
431
- cidr.wildcard_mask('0.7.0.255', :inversed)
432
- assert_equal('0.7.0.255', cidr.wildcard_mask(nil,:inversed))
435
+ cidr.set_wildcard_mask('0.7.0.255', true)
436
+ assert_equal('0.7.0.255', cidr.wildcard_mask(true))
433
437
  assert_equal('255.248.255.0', cidr.wildcard_mask())
434
- cidr.wildcard_mask('255.248.255.0')
435
- assert_equal('0.7.0.255', cidr.wildcard_mask(nil,:inversed))
438
+ cidr.set_wildcard_mask('255.248.255.0')
439
+ assert_equal('0.7.0.255', cidr.wildcard_mask(true))
436
440
  assert_equal('255.248.255.0', cidr.wildcard_mask())
437
- cidr.wildcard_mask('0.0.0.0')
441
+ cidr.set_wildcard_mask('0.0.0.0')
438
442
  assert_equal('0.0.0.0', cidr.wildcard_mask)
439
- assert_raise(NetAddr::ValidationError){ cidr.wildcard_mask('0000:ffff::ffff') }
443
+ assert_raise(NetAddr::ValidationError){ cidr.set_wildcard_mask('0000:ffff::ffff') }
440
444
 
441
445
  cidr = NetAddr::CIDR.create('fec0::1/64')
442
- assert_equal('0000:0000:0000:0000:ffff:ffff:ffff:ffff', cidr.wildcard_mask(nil,:inversed))
443
- cidr.wildcard_mask('0000:ffff::ffff', :inversed)
444
- assert_equal('0000:ffff:0000:0000:0000:0000:0000:ffff', cidr.wildcard_mask(nil,:inversed))
445
- assert_raise(NetAddr::ValidationError){ cidr.wildcard_mask('0.7.0.255', :inversed) }
446
+ assert_equal('0000:0000:0000:0000:ffff:ffff:ffff:ffff', cidr.wildcard_mask(true))
447
+ cidr.set_wildcard_mask('0000:ffff::ffff', true)
448
+ assert_equal('0000:ffff:0000:0000:0000:0000:0000:ffff', cidr.wildcard_mask(true))
449
+ assert_raise(NetAddr::ValidationError){ cidr.set_wildcard_mask('0.7.0.255', true) }
446
450
 
447
451
  end
448
452
 
@@ -6,58 +6,35 @@ require 'test/unit'
6
6
 
7
7
 
8
8
  class TestMethods < Test::Unit::TestCase
9
-
9
+
10
10
  def test_merge
11
- cidr4 = [NetAddr::CIDR.create('192.168.1.0/24'),
12
- NetAddr::CIDR.create( '192.168.1.0/26'),
13
- NetAddr::CIDR.create('192.168.1.4/30'),
14
- NetAddr::CIDR.create('192.168.1.0/30'),
15
- NetAddr::CIDR.create('192.168.1.64/27'),
16
- NetAddr::CIDR.create('192.168.1.96/27'),
17
- NetAddr::CIDR.create('192.168.1.128/25'),
18
- NetAddr::CIDR.create('10.1.0.0/31'),
19
- NetAddr::CIDR.create('10.1.0.0/24'),
20
- NetAddr::CIDR.create('10.1.0.2/31'),
21
- NetAddr::CIDR.create('10.1.0.4/30'),
22
- NetAddr::CIDR.create('10.1.0.16/28'),
23
- NetAddr::CIDR.create('10.1.0.32/28'),
24
- NetAddr::CIDR.create('10.1.0.48/28'),
25
- NetAddr::CIDR.create('10.1.0.0/27'),
26
- NetAddr::CIDR.create('10.1.0.64/26'),
27
- NetAddr::CIDR.create('10.1.0.128/25'),
28
- NetAddr::CIDR.create('10.1.0.0/27'),
29
- NetAddr::CIDR.create('10.1.0.0/24')]
30
-
31
- cidr4_2 = ['10.0.0.0/9','10.1.0.0/9','10.1.0.0/24',
32
- '10.1.0.64/26','10.1.0.128/26','128.0.0.0/2',
33
- '129.0.0.0/2','1.1.1.0','1.1.1.1',
34
- '1.1.1.3','1.1.1.4','1.1.1.5',
35
- '1.1.1.6','1.1.1.7','192.168.1.0/27',
36
- '192.168.1.32/27','192.168.1.96/27','192.168.1.128/26',
37
- '192.168.1.192/26']
38
-
39
- cidr4_3 = ['192.168.1.0/26', '192.168.1.64/26', '192.168.1.128/26', '192.168.1.192/26']
40
-
41
- cidr6 = [NetAddr::CIDR.create('fec0::/16'),
42
- NetAddr::CIDR.create('fec0::/17'),
43
- NetAddr::CIDR.create('fec0:8000::/18'),
44
- NetAddr::CIDR.create('fec0:c000::/18'),
45
- NetAddr::CIDR.create('fec0::/64'),
46
- NetAddr::CIDR.create('fec0::/66'),
47
- NetAddr::CIDR.create('fec0::8000:0:0:0/66'),
48
- NetAddr::CIDR.create('fec0::c000:0:0:0/66'),
49
- NetAddr::CIDR.create('fec0::/127'),
50
- NetAddr::CIDR.create('fec0::2/127')]
51
-
52
- assert_raise(ArgumentError) {NetAddr.merge(cidr4, :test => true)}
53
- assert_equal(['10.1.0.0/24','10.1.0.0/29','10.1.0.16/28','192.168.1.0/24','192.168.1.0/29'], NetAddr.merge(cidr4))
54
- assert_equal(['1.1.1.0/31','1.1.1.3/32','1.1.1.4/30','10.0.0.0/9','10.1.0.0/24','10.1.0.64/26',
55
- '10.1.0.128/26','128.0.0.0/2','192.168.1.0/26','192.168.1.96/27','192.168.1.128/25'], NetAddr.merge(cidr4_2))
56
- assert_equal(['fec0::/16','fec0::/64','fec0::/66','fec0::/126','fec0::8000:0:0:0/65'], NetAddr.merge(cidr6, :Short => true))
57
- assert_kind_of(NetAddr::CIDRv4, NetAddr.merge(cidr4_3, :Objectify => true)[0])
58
-
59
11
  assert_raise(ArgumentError){ NetAddr.merge(1) }
60
12
  assert_raise(ArgumentError){ NetAddr.merge({}) }
13
+
14
+ subs = NetAddr::CIDR.create('10.0.0.0/24').subnet(:Bits => 26, :Objectify => true)
15
+ subs.concat( NetAddr::CIDR.create('10.1.0.0/24').subnet(:Bits => 29, :NumSubnets => 4, :Objectify => true) )
16
+ subs.delete_at(2)
17
+ subs.delete_at(7)
18
+ assert_equal(['10.0.0.0/25', '10.0.0.192/26', '10.1.0.0/27', '10.1.0.64/26', '10.1.0.128/25'], NetAddr.merge(subs) )
19
+
20
+ cidr = NetAddr::CIDR.create('fec0::/64')
21
+ subs = cidr.range(1, 8, :Objectify => true)
22
+ subs.concat([NetAddr::CIDR.create('192.168.0.0/27'), NetAddr::CIDR.create('192.168.0.32/27')])
23
+ assert_equal(['192.168.0.0/26', 'fec0::1/128', 'fec0::2/127', 'fec0::4/126', 'fec0::8/128',], NetAddr.merge(subs, :Short => true) )
24
+
25
+ subs = []
26
+ NetAddr.range('192.168.35.0','192.168.39.255',
27
+ :Inclusive => true, :Bitstep => 32).each {|x| subs.push(NetAddr::CIDR.create("#{x}/27")) }
28
+ assert_equal(['192.168.35.0/24', '192.168.36.0/22'], NetAddr.merge(subs) )
29
+
30
+ subs = NetAddr::CIDR.create('10.0.0.0/24').subnet(:Bits => 26, :Objectify => true)
31
+ subs.concat( subs.pop.subnet(:Bits => 27, :Objectify => true) )
32
+ subs.concat( subs.pop.subnet(:Bits => 28, :Objectify => true) )
33
+ subs[5].tag[:test] = true
34
+ merged = NetAddr.merge(subs, :Objectify => true)
35
+ assert_equal('10.0.0.0/24', merged[0].desc)
36
+ assert_equal('10.0.0.240/28', merged[0].tag[:Subnets][5].desc)
37
+ assert(merged[0].tag[:Subnets][5].tag[:test])
61
38
  end
62
39
 
63
40
 
@@ -133,6 +110,8 @@ class TestMethods < Test::Unit::TestCase
133
110
 
134
111
  assert_raise(ArgumentError){ NetAddr.range(:Limit => 1) }
135
112
  assert_raise(NetAddr::VersionError){ NetAddr.range(cidr4_1,cidr6_2) }
113
+
114
+ assert_equal(256, NetAddr.range('192.168.0.0', '192.168.0.255', :Size => true, :Inclusive => true) )
136
115
  end
137
116
 
138
117
 
@@ -222,7 +201,8 @@ class TestMethods < Test::Unit::TestCase
222
201
  assert_raise(NetAddr::ValidationError){NetAddr.validate_eui('aabb.ccdd.eeff.12312') }
223
202
  assert_raise(NetAddr::ValidationError){NetAddr.validate_eui('aa-bb-c-dd-ee-ff') }
224
203
  assert_raise(NetAddr::ValidationError){NetAddr.validate_eui('aa:bb:cc:dd:e:ff') }
225
-
204
+ assert_raise(NetAddr::ValidationError){NetAddr.validate_eui('aa:bb:cc:dd:ee:^^') }
205
+ assert_raise(NetAddr::ValidationError){NetAddr.validate_eui('aa:bb:cc:dd:ee:ZZ') }
226
206
  assert_raise(ArgumentError){ NetAddr.validate_eui(0xaabbccddeeff) }
227
207
  assert_raise(ArgumentError){ NetAddr.validate_eui() }
228
208
  end
@@ -255,6 +235,7 @@ class TestMethods < Test::Unit::TestCase
255
235
 
256
236
  assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('') }
257
237
  assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('10.0') }
238
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('10.0..0') }
258
239
  assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('192.168.1.256') }
259
240
  assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('192..168.1.1') }
260
241
  assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('192.168.1a.255') }