netaddr 1.2.0 → 1.3.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.

data/tests/cidr_test.rb CHANGED
@@ -7,6 +7,14 @@ require 'test/unit'
7
7
 
8
8
  class TestCIDR < Test::Unit::TestCase
9
9
 
10
+
11
+ def test_new
12
+ cidr = NetAddr::CIDRv4.new(3232235777, netmask=4294967040, tag={}, wildcard_mask=4294967040)
13
+ assert_equal('192.168.1.0/24', cidr.desc)
14
+ assert_equal('192.168.1.1', cidr.ip)
15
+ assert_equal('255.255.255.0', cidr.wildcard_mask)
16
+ end
17
+
10
18
  def test_create
11
19
  assert_raise(ArgumentError) {NetAddr::CIDR.create('192.168.1.0/24', :test => true)}
12
20
  assert_nothing_raised(Exception){NetAddr::CIDR.create('0.0.0.0/0')}
@@ -20,51 +28,108 @@ class TestCIDR < Test::Unit::TestCase
20
28
  assert_nothing_raised(Exception){NetAddr::CIDR.create('::/0')}
21
29
  assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1/64')}
22
30
  assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1/64')}
23
- assert_nothing_raised(Exception){NetAddr::CIDR.create(0x0a0a0a0a, :PackedNetmask => 0xffffffff)}
31
+ assert_nothing_raised(Exception){NetAddr::CIDR.create(0x0a0a0a0a, :Mask => 0xffffffff)}
24
32
  assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1', :WildcardMask => ['0.0.7.255', true]) }
25
33
  assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1', :WildcardMask => [0x000007ff, true]) }
26
34
  assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1', :WildcardMask => ['0000:ffff::ffff', true])}
27
35
  assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1', :WildcardMask => [0xffff, true])}
28
-
36
+
29
37
  assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('192.168.1.1 255.255.0.0'))
30
38
  assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('192.168.1.1/24 255.255.0.0'))
31
39
  assert_kind_of(NetAddr::CIDRv6, NetAddr::CIDR.create('fec0::1/64'))
32
40
  assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('10.10.10.10/32 255.255.255.0'))
33
- assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('10.10.10.10/32', :PackedNetmask => 0xffffff00))
34
-
41
+ assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('10.10.10.10/32', :Mask => 0xffffff00))
42
+
35
43
  assert_raise(ArgumentError){ NetAddr::CIDR.create(:Version => 4) }
36
44
  assert_raise(NetAddr::ValidationError){ NetAddr::CIDR.create('192.168.1.1', :WildcardMask => ['0000:ffff::ffff', true]) }
37
45
  assert_raise(NetAddr::ValidationError){ NetAddr::CIDR.create('fec0::1', :WildcardMask => ['0.0.7.255', true]) }
38
-
46
+
39
47
  cidr = NetAddr::CIDRv4.create('192.168.1.1 255.255.0.0')
40
48
  assert_equal(16, cidr.bits )
41
49
 
42
-
43
50
  cidr = NetAddr::CIDRv4.create('192.168.1.1/24 255.255.0.0')
44
51
  assert_equal(24, cidr.bits )
45
52
 
46
-
47
53
  cidr = NetAddr::CIDRv4.create('10.10.10.10/32 255.255.255.0')
48
54
  assert_equal(32, cidr.bits )
49
55
 
50
-
51
- cidr = NetAddr::CIDRv4.create('10.10.10.10/32', :PackedNetmask => 0xffffff00)
56
+ cidr = NetAddr::CIDRv4.create('10.10.10.10/32', :Mask => 0xffffff00)
52
57
  assert_equal(24, cidr.bits )
53
-
58
+
54
59
  cidr = NetAddr::CIDR.create('fec0::1/64')
55
60
  assert_equal(64, cidr.bits )
56
61
 
57
62
  assert_raise(ArgumentError){ NetAddr::CIDRv4.create({}) }
58
63
  assert_raise(NetAddr::ValidationError){ NetAddr::CIDRv4.create('192.168.1.0 a') }
59
64
  end
60
-
65
+
66
+ def test_comparasins
67
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
68
+
69
+ assert(cidr4 == '192.168.1.0/24')
70
+ assert(cidr4 > '192.168.0.0/24')
71
+ assert(cidr4 < '192.168.2.0/24')
72
+ assert_equal(0, cidr4 <=> '192.168.1.0/24')
73
+ assert_equal(1, cidr4 <=> '192.168.0.0/24')
74
+ assert_equal(-1, cidr4 <=> '192.168.2.0/24')
75
+ end
76
+
77
+ def test_index
78
+ cidr = NetAddr::CIDR.create('192.168.1.0/24')
79
+ assert_equal('192.168.1.1/32', cidr[1].desc)
80
+ assert_equal('192.168.1.255/32', cidr[255].desc)
81
+ assert_raise(NetAddr::BoundaryError){ cidr[256] }
82
+ assert_raise(NetAddr::BoundaryError){ cidr[-1] }
83
+ assert_raise(ArgumentError){ cidr['a'] }
84
+ end
85
+
86
+ def test_allocate_allocate_rfc3531
87
+ cidr = NetAddr::CIDR.create('192.168.0.0/16')
88
+ centermost = ["192.168.0.0/21", "192.168.32.0/21", "192.168.64.0/21", "192.168.96.0/21",
89
+ "192.168.16.0/21", "192.168.48.0/21", "192.168.80.0/21", "192.168.112.0/21",
90
+ "192.168.128.0/21", "192.168.144.0/21", "192.168.160.0/21", "192.168.176.0/21",
91
+ "192.168.192.0/21", "192.168.208.0/21", "192.168.224.0/21", "192.168.240.0/21",
92
+ "192.168.8.0/21", "192.168.24.0/21", "192.168.40.0/21", "192.168.56.0/21", "192.168.72.0/21",
93
+ "192.168.88.0/21", "192.168.104.0/21", "192.168.120.0/21", "192.168.136.0/21",
94
+ "192.168.152.0/21", "192.168.168.0/21", "192.168.184.0/21", "192.168.200.0/21",
95
+ "192.168.216.0/21", "192.168.232.0/21", "192.168.248.0/21"]
96
+ leftmost = ["192.168.0.0/21", "192.168.128.0/21", "192.168.64.0/21", "192.168.192.0/21",
97
+ "192.168.32.0/21", "192.168.160.0/21", "192.168.96.0/21", "192.168.224.0/21",
98
+ "192.168.16.0/21", "192.168.144.0/21", "192.168.80.0/21", "192.168.208.0/21",
99
+ "192.168.48.0/21", "192.168.176.0/21", "192.168.112.0/21", "192.168.240.0/21",
100
+ "192.168.8.0/21", "192.168.136.0/21", "192.168.72.0/21", "192.168.200.0/21",
101
+ "192.168.40.0/21", "192.168.168.0/21", "192.168.104.0/21", "192.168.232.0/21",
102
+ "192.168.24.0/21", "192.168.152.0/21", "192.168.88.0/21", "192.168.216.0/21",
103
+ "192.168.56.0/21", "192.168.184.0/21", "192.168.120.0/21", "192.168.248.0/21"]
104
+
105
+ assert_equal(centermost, cidr.allocate_rfc3531(21, :Strategy => :centermost) )
106
+ assert_equal(leftmost, cidr.allocate_rfc3531(21) )
107
+ assert_equal("192.168.192.0/21", cidr.allocate_rfc3531(21, :Objectify => true)[3].desc )
108
+ assert_equal("192.168.96.0/21", cidr.allocate_rfc3531(21, :Strategy => :centermost, :Objectify => true)[3].desc )
109
+ end
110
+
111
+ def test_arpa
112
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
113
+ cidr6 = NetAddr::CIDR.create('fec0::1/64')
114
+
115
+ assert_equal('1.168.192.in-addr.arpa.', cidr4.arpa() )
116
+ assert_equal('0.0.0.0.0.0.0.0.0.0.0.0.0.c.e.f.ip6.arpa.', cidr6.arpa() )
117
+ end
118
+
119
+ def test_bits
120
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
121
+ cidr6 = NetAddr::CIDR.create('fec0::1/64')
122
+ assert_equal(24,cidr4.bits() )
123
+ assert_equal(64,cidr6.bits() )
124
+ end
125
+
61
126
  def test_cmp
62
127
 
63
128
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
64
129
  cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
65
130
  cidr6 = NetAddr::CIDR.create('fec0::/64')
66
131
  cidr6_2 = NetAddr::CIDR.create('fec0::/96')
67
-
132
+
68
133
  assert_equal(1,cidr4.cmp('192.168.1.0/26') )
69
134
  assert_equal(-1,cidr4.cmp('192.168.0.0/23') )
70
135
  assert_equal(0,cidr4.cmp('192.168.1.0/24') )
@@ -75,132 +140,143 @@ class TestCIDR < Test::Unit::TestCase
75
140
  assert_equal(0,cidr6.cmp('fec0::/64') )
76
141
  assert_nil(cidr6.cmp('fe80::/64') )
77
142
  assert_equal(1,cidr6.cmp(cidr6_2) )
78
-
79
- assert_raise(NetAddr::VersionError) { cidr4.cmp(cidr6_2) }
143
+
144
+ assert_raise(NetAddr::VersionError) { cidr4.cmp(cidr6_2) }
80
145
  end
81
-
82
-
146
+
83
147
  def test_contains?
84
148
 
85
149
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
86
150
  cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
87
151
  cidr6 = NetAddr::CIDR.create('fec0::/64')
88
152
  cidr6_2 = NetAddr::CIDR.create('fec0::/96')
89
-
153
+
90
154
  assert_equal(true,cidr4.contains?('192.168.1.0/26') )
91
155
  assert_equal(true,cidr4.contains?(cidr4_2) )
92
156
  assert_equal(true,cidr6.contains?(cidr6_2) )
93
157
  assert_equal(false,cidr4.contains?('192.168.2.0/26') )
94
158
  assert_equal(false,cidr6.contains?('fe80::/96') )
95
-
96
- assert_raise(NetAddr::VersionError) { cidr4.contains?(cidr6_2) }
159
+
160
+ assert_raise(NetAddr::VersionError) { cidr4.contains?(cidr6_2) }
97
161
  end
98
-
99
-
162
+
163
+ def test_desc
164
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
165
+ cidr6 = NetAddr::CIDR.create('fec0::1/64')
166
+
167
+ assert_raise(ArgumentError) {cidr4.desc(:test => true)}
168
+ assert_equal('192.168.1.0/24',cidr4.desc() )
169
+ assert_equal('192.168.1.1/24',cidr4.desc(:IP => true) )
170
+ assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/64',cidr6.desc() )
171
+ assert_equal('fec0::/64',cidr6.desc(:Short => true) )
172
+ end
173
+
100
174
  def test_enumerate
101
175
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
102
176
  cidr6 = NetAddr::CIDR.create('fec0::/64')
103
-
177
+
104
178
  assert_raise(ArgumentError) {cidr4.enumerate(:test => true)}
105
179
  assert_equal(['192.168.1.0', '192.168.1.1'],cidr4.enumerate(:Limit => 2) )
106
180
  assert_equal(['fec0:0000:0000:0000:0000:0000:0000:0000'],cidr6.enumerate(:Limit => 1) )
107
181
  assert_equal(['fec0::'],cidr6.enumerate(:Limit => 1, :Short => true) )
108
-
109
-
182
+
110
183
  enums4 = cidr4.enumerate(:Limit => 2, :Bitstep => 5)
111
184
  enums6 = cidr6.enumerate(:Limit => 2, :Bitstep => 5)
112
185
  assert_equal('192.168.1.5', enums4[1] )
113
186
  assert_equal('fec0:0000:0000:0000:0000:0000:0000:0005', enums6[1] )
114
-
187
+
115
188
  enums4 = cidr4.enumerate(:Objectify => true,:Limit => 1)
116
189
  assert_kind_of(NetAddr::CIDR, enums4[0] )
117
-
118
190
  end
119
-
120
- def test_eql?
121
- cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
122
- cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
123
- cidr4_3 = NetAddr::CIDR.create('192.168.1.0/24')
124
- cidr6 = NetAddr::CIDR.create('fec0::/64')
125
- cidr6_2 = NetAddr::CIDR.create('fec0::/96')
126
- cidr6_3 = NetAddr::CIDR.create('fec0::/64')
127
-
128
- assert_equal(true,cidr4.eql?('192.168.1.0/24') )
129
- assert_equal(false,cidr4.eql?('192.168.1.0/26') )
130
- assert_equal(true,cidr4.eql?(cidr4_3) )
131
- assert_equal(false,cidr4.eql?(cidr4_2) )
132
- assert_equal(true,cidr6.eql?('fec0::/64') )
133
- assert_equal(false,cidr6.eql?('fec0::/96') )
134
- assert_equal(true,cidr6.eql?(cidr6_3) )
135
- assert_equal(false,cidr6.eql?(cidr6_2) )
136
-
137
- assert_raise(NetAddr::VersionError) {cidr4.eql?(cidr6) }
138
- assert_raise(NetAddr::VersionError){cidr6.eql?(cidr4) }
139
- assert_raise(ArgumentError) { cidr6.eql?('a') }
140
- end
141
-
191
+
142
192
  def test_fill_in
143
193
  cidr = NetAddr::CIDR.create('192.168.1.0/24')
144
194
  filled = cidr.fill_in(['192.168.1.0/27','192.168.1.44/30',
145
195
  '192.168.1.64/26','192.168.1.129'])
146
-
196
+
147
197
  assert_equal(['192.168.1.0/27','192.168.1.32/29','192.168.1.40/30',
148
198
  '192.168.1.44/30','192.168.1.48/28','192.168.1.64/26',
149
199
  '192.168.1.128/32','192.168.1.129/32','192.168.1.130/31',
150
200
  '192.168.1.132/30','192.168.1.136/29','192.168.1.144/28',
151
201
  '192.168.1.160/27','192.168.1.192/26'],filled)
152
202
  end
153
-
203
+
204
+ def test_hostmask_ext
205
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
206
+ assert_equal('0.0.0.255',cidr4.hostmask_ext() )
207
+ assert_equal('255.255.255.0',cidr4.netmask_ext() )
208
+ end
209
+
210
+ def test_ip
211
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
212
+ cidr6 = NetAddr::CIDR.create('fec0::1/64')
213
+
214
+ assert_raise(ArgumentError) {cidr4.ip(:test => true)}
215
+ assert_equal('192.168.1.1',cidr4.ip() )
216
+ assert_equal('fec0:0000:0000:0000:0000:0000:0000:0001',cidr6.ip() )
217
+ assert_equal('fec0::1',cidr6.ip(:Short => true) )
218
+ end
219
+
154
220
  def test_is_contained?
155
221
 
156
222
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
157
223
  cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
158
224
  cidr6 = NetAddr::CIDR.create('fec0::/64')
159
225
  cidr6_2 = NetAddr::CIDR.create('fec0::/96')
160
-
226
+
161
227
  assert_equal(true,cidr4_2.is_contained?('192.168.1.0/24') )
162
228
  assert_equal(true,cidr4_2.is_contained?(cidr4) )
163
229
  assert_equal(true,cidr6_2.is_contained?('fec0::/64') )
164
230
  assert_equal(true,cidr6_2.is_contained?(cidr6) )
165
231
  assert_equal(false,cidr4.is_contained?('192.168.2.0/26') )
166
232
  assert_equal(false,cidr6.is_contained?('fe80::/96') )
167
-
168
- assert_raise(NetAddr::VersionError) { cidr4.is_contained?(cidr6_2) }
233
+
234
+ assert_raise(NetAddr::VersionError) { cidr4.is_contained?(cidr6_2) }
235
+ end
236
+
237
+ def test_last
238
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
239
+ cidr6 = NetAddr::CIDR.create('fec0::1/64')
240
+
241
+ assert_raise(ArgumentError) {cidr4.last(:test => true)}
242
+ assert_equal('192.168.1.255',cidr4.last() )
243
+ assert_equal('fec0:0000:0000:0000:ffff:ffff:ffff:ffff',cidr6.last() )
244
+ assert_equal('fec0::ffff:ffff:ffff:ffff',cidr6.last(:Short => true) )
169
245
  end
170
246
 
171
247
  def test_matches?
172
248
  cidr = NetAddr::CIDR.create('10.0.0.0/24')
173
249
  assert(cidr.matches?('10.0.0.22'))
174
250
  assert(!cidr.matches?('10.1.1.1'))
175
-
251
+
176
252
  cidr = NetAddr::CIDR.create('10.0.248.0', :WildcardMask => ['255.248.255.0'])
177
253
  assert(cidr.matches?('10.1.248.0'))
178
254
  assert(!cidr.matches?('10.8.248.0'))
179
-
255
+
180
256
  cidr = NetAddr::CIDR.create('10.0.248.0')
181
257
  cidr.set_wildcard_mask('0.7.0.255', true)
182
258
  assert(cidr.matches?('10.1.248.0'))
183
259
  assert(!cidr.matches?('10.8.248.0'))
184
-
260
+
185
261
  cidr = NetAddr::CIDR.create('127.0.0.0')
186
262
  cidr.set_wildcard_mask('0.255.255.255', true)
187
263
  assert(cidr.matches?('127.0.0.1'))
188
264
  assert(!cidr.matches?('128.0.0.0'))
189
-
265
+
190
266
  cidr = NetAddr::CIDR.create('127.0.0.0', :WildcardMask => ['0.255.255.255', true])
191
267
  assert(cidr.matches?('127.0.0.1'))
192
268
  assert(!cidr.matches?('128.0.0.0'))
193
-
269
+
194
270
  cidr = NetAddr::CIDR.create('fec0::1')
195
271
  cidr.set_wildcard_mask('0000:ffff::ffff', true)
196
272
  assert(cidr.matches?('fec0:1::1'))
197
273
  assert(!cidr.matches?('fec0:0:1::1'))
198
-
274
+
199
275
  cidr = NetAddr::CIDR.create('fec0::1', :WildcardMask => ['0000:ffff::ffff', true])
200
276
  assert(cidr.matches?('fec0:1::1'))
201
277
  assert(!cidr.matches?('fec0:0:1::1'))
202
278
  end
203
-
279
+
204
280
  def test_mcast
205
281
  cidr4 = NetAddr::CIDR.create('224.0.0.1')
206
282
  cidr4_2 = NetAddr::CIDR.create('239.255.255.255')
@@ -212,100 +288,122 @@ class TestCIDR < Test::Unit::TestCase
212
288
  cidr6_3 = NetAddr::CIDR.create('ff00::ffff:ffff')
213
289
  cidr6_4 = NetAddr::CIDR.create('ff00::fec0:1234:')
214
290
  cidr6_5 = NetAddr::CIDR.create('2001:4800::1')
215
-
291
+
216
292
  assert_raise(ArgumentError) {cidr4.multicast_mac(:test => true)}
217
293
  assert_equal('01-00-5e-00-00-01',cidr4.multicast_mac(:Objectify => true).address )
218
294
  assert_equal('01-00-5e-7f-ff-ff',cidr4_2.multicast_mac )
219
295
  assert_equal('01-00-5e-02-03-05',cidr4_3.multicast_mac )
220
296
  assert_equal('01-00-5e-13-12-17',cidr4_4.multicast_mac )
221
-
297
+
222
298
  assert_equal('33-33-00-00-00-01',cidr6.multicast_mac(:Objectify => true).address )
223
299
  assert_equal('33-33-00-00-00-01',cidr6_2.multicast_mac )
224
300
  assert_equal('33-33-ff-ff-ff-ff',cidr6_3.multicast_mac )
225
301
  assert_equal('33-33-fe-c0-12-34',cidr6_4.multicast_mac )
226
-
302
+
227
303
  assert_raise(NetAddr::ValidationError){ cidr4_5.multicast_mac }
228
- assert_raise(NetAddr::ValidationError){ cidr6_5.multicast_mac }
229
- end
230
-
304
+ assert_raise(NetAddr::ValidationError){ cidr6_5.multicast_mac }
305
+ end
306
+
307
+ def test_netmask
308
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
309
+ cidr6 = NetAddr::CIDR.create('fec0::1/64')
310
+
311
+ assert_equal('/24',cidr4.netmask() )
312
+ assert_equal('/64',cidr6.netmask() )
313
+ end
314
+
315
+ def test_netmask_ext
316
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
317
+ assert_equal('255.255.255.0',cidr4.netmask_ext() )
318
+ end
319
+
320
+ def test_network
321
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
322
+ cidr6 = NetAddr::CIDR.create('fec0::1/64')
323
+
324
+ assert_raise(ArgumentError) {cidr4.network(:test => true)}
325
+ assert_equal('192.168.1.0',cidr4.network() )
326
+ assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000',cidr6.network() )
327
+ assert_equal('fec0::',cidr6.network(:Short => true) )
328
+ end
329
+
231
330
  def test_next_ip
232
331
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
233
332
  cidr6 = NetAddr::CIDR.create('fec0::/64')
234
-
333
+
235
334
  assert_raise(ArgumentError) {cidr4.next_ip(:test => true)}
236
335
  next4 = cidr4.next_ip()
237
336
  next6 = cidr6.next_ip()
238
337
  assert_equal('192.168.2.0',next4 )
239
338
  assert_equal('fec0:0000:0000:0001:0000:0000:0000:0000',next6 )
240
-
339
+
241
340
  next6 = cidr6.next_ip(:Short => true)
242
341
  assert_equal('fec0:0:0:1::',next6 )
243
-
342
+
244
343
  next4 = cidr4.next_ip(:Bitstep => 2)
245
344
  next6 = cidr6.next_ip(:Bitstep => 2)
246
345
  assert_equal('192.168.2.1',next4 )
247
346
  assert_equal('fec0:0000:0000:0001:0000:0000:0000:0001',next6 )
248
-
347
+
249
348
  next4 = cidr4.next_ip(:Objectify => true)
250
349
  next6 = cidr6.next_ip(:Objectify => true)
251
350
  assert_equal('192.168.2.0/32',next4.desc )
252
351
  assert_equal('fec0:0000:0000:0001:0000:0000:0000:0000/128',next6.desc )
253
-
352
+
254
353
  end
255
-
354
+
256
355
  def test_next_subnet
257
356
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
258
357
  cidr6 = NetAddr::CIDR.create('fec0::/64')
259
-
358
+
260
359
  assert_raise(ArgumentError) {cidr4.next_subnet(:test => true)}
261
360
  next4 = cidr4.next_subnet()
262
361
  next6 = cidr6.next_subnet()
263
362
  assert_equal('192.168.2.0/24',next4 )
264
363
  assert_equal('fec0:0000:0000:0001:0000:0000:0000:0000/64',next6 )
265
-
364
+
266
365
  next6 = cidr6.next_subnet(:Short => true)
267
366
  assert_equal('fec0:0:0:1::/64',next6 )
268
-
367
+
269
368
  next4 = cidr4.next_subnet(:Bitstep => 2)
270
369
  next6 = cidr6.next_subnet(:Bitstep => 2)
271
370
  assert_equal('192.168.3.0/24',next4 )
272
371
  assert_equal('fec0:0000:0000:0002:0000:0000:0000:0000/64',next6 )
273
-
372
+
274
373
  next4 = cidr4.next_subnet(:Objectify => true)
275
374
  next6 = cidr6.next_subnet(:Objectify => true)
276
375
  assert_equal('192.168.2.0/24',next4.desc )
277
376
  assert_equal('fec0:0000:0000:0001:0000:0000:0000:0000/64',next6.desc )
278
-
279
377
  end
280
-
378
+
281
379
  def test_nth
282
380
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
283
381
  cidr6 = NetAddr::CIDR.create('fec0::/126')
284
-
382
+
285
383
  assert_raise(ArgumentError) {cidr4.nth(1, :test => true)}
286
384
  assert_equal('192.168.1.1',cidr4.nth(1) )
287
385
  assert_equal('192.168.1.50',cidr4.nth(50) )
288
386
  assert_kind_of(NetAddr::CIDR,cidr4.nth(1, :Objectify => true) )
289
387
  assert_raise(NetAddr::BoundaryError){ cidr4.nth(256) }
290
388
  assert_raise(ArgumentError){ cidr4.nth() }
291
-
389
+
292
390
  assert_equal('fec0:0000:0000:0000:0000:0000:0000:0001',cidr6.nth(1) )
293
391
  assert_equal('fec0:0000:0000:0000:0000:0000:0000:0003',cidr6.nth(3) )
294
392
  assert_equal('fec0::1',cidr6.nth(1, :Short => true) )
295
393
  assert_raise(NetAddr::BoundaryError){ cidr6.nth(10) }
296
-
394
+
297
395
  assert_raise(ArgumentError) { cidr4.nth({}) }
298
396
  end
299
-
397
+
300
398
  def test_range
301
399
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
302
400
  cidr6 = NetAddr::CIDR.create('fec0::/64')
303
-
401
+
304
402
  assert_raise(ArgumentError) {cidr4.range(25,0, :test => true)}
305
403
  range4 = cidr4.range(25,0, :Bitstep => 5)
306
404
  range4_2 = cidr4.range(250)
307
405
  range6 = cidr6.range(25,0, :Bitstep => 5, :Short => true)
308
-
406
+
309
407
  assert_equal(6,range4.length)
310
408
  assert_equal(6,range4_2.length)
311
409
  assert_equal(6,range6.length)
@@ -314,104 +412,83 @@ class TestCIDR < Test::Unit::TestCase
314
412
  assert_equal('fec0::',range6[0])
315
413
  assert_equal('fec0::19',range6[5])
316
414
  end
317
-
415
+
318
416
  def test_remainder
319
417
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
320
418
  cidr4_2 = NetAddr::CIDR.create('192.168.1.64/26')
321
-
419
+
322
420
  assert_raise(ArgumentError) {cidr4.remainder(cidr4_2, :test => true)}
323
421
  remainder = cidr4.remainder(cidr4_2)
324
-
422
+
325
423
  assert_equal(2,remainder.length)
326
424
  assert_equal('192.168.1.0/26',remainder[0])
327
-
425
+
328
426
  remainder = cidr4.remainder('192.168.1.64/26', :Objectify => true)
329
427
  assert_equal('192.168.1.128/25',remainder[1].desc)
330
428
  end
331
-
429
+
332
430
  def test_resize
333
431
  cidr4 = NetAddr::CIDR.create('192.168.1.129/24')
334
432
  cidr6 = NetAddr::CIDR.create('fec0::1/64')
335
-
433
+
336
434
  assert_raise(ArgumentError) {cidr4.resize(23, :test => true)}
337
435
  new4 = cidr4.resize(23)
338
436
  new6 = cidr6.resize(63)
339
437
  assert_equal('192.168.0.0/23',new4.desc )
340
438
  assert_equal('fec0::/63',new6.desc(:Short => true) )
341
-
439
+
342
440
  cidr4.resize!(25)
343
441
  cidr6.resize!(67)
344
442
  assert_equal('192.168.1.0/25',cidr4.desc )
345
443
  assert_equal('192.168.1.0',cidr4.ip )
346
444
  assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/67',cidr6.desc )
347
445
  end
348
-
349
- def test_simple_methods
446
+
447
+ def test_set_wildcard_mask
448
+ cidr = NetAddr::CIDR.create('10.1.0.0/24')
449
+ assert_equal('0.0.0.255', cidr.wildcard_mask(true))
450
+ assert_equal('255.255.255.0', cidr.wildcard_mask)
451
+
452
+ cidr.set_wildcard_mask('0.7.0.255', true)
453
+ assert_equal('0.7.0.255', cidr.wildcard_mask(true))
454
+ assert_equal('255.248.255.0', cidr.wildcard_mask())
455
+ cidr.set_wildcard_mask('255.248.255.0')
456
+ assert_equal('0.7.0.255', cidr.wildcard_mask(true))
457
+ assert_equal('255.248.255.0', cidr.wildcard_mask())
458
+ cidr.set_wildcard_mask('0.0.0.0')
459
+ assert_equal('0.0.0.0', cidr.wildcard_mask)
460
+ assert_raise(NetAddr::ValidationError){ cidr.set_wildcard_mask('0000:ffff::ffff') }
461
+
462
+ cidr = NetAddr::CIDR.create('fec0::1/64')
463
+ assert_equal('0000:0000:0000:0000:ffff:ffff:ffff:ffff', cidr.wildcard_mask(true))
464
+ cidr.set_wildcard_mask('0000:ffff::ffff', true)
465
+ assert_equal('0000:ffff:0000:0000:0000:0000:0000:ffff', cidr.wildcard_mask(true))
466
+ assert_raise(NetAddr::ValidationError){ cidr.set_wildcard_mask('0.7.0.255', true) }
467
+ end
468
+
469
+ def test_size
350
470
  cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
351
- cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
352
471
  cidr6 = NetAddr::CIDR.create('fec0::1/64')
353
- cidr6_2 = NetAddr::CIDR.create('fec0::/96')
354
472
 
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
-
362
- assert_equal('1.168.192.in-addr.arpa.', cidr4.arpa() )
363
- assert_equal('0.0.0.0.0.0.0.0.0.0.0.0.0.c.e.f.ip6.arpa.', cidr6.arpa() )
364
-
365
- assert_raise(ArgumentError) {cidr4.desc(:test => true)}
366
- assert_equal('192.168.1.0/24',cidr4.desc() )
367
- assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/64',cidr6.desc() )
368
-
369
- assert_equal('fec0::/64',cidr6.desc(:Short => true) )
370
-
371
- assert_equal('0.0.0.255',cidr4.hostmask_ext() )
372
-
373
- assert_equal(24,cidr4.bits() )
374
- assert_equal(64,cidr6.bits() )
375
-
376
- assert_raise(ArgumentError) {cidr4.ip(:test => true)}
377
- assert_equal('192.168.1.1',cidr4.ip() )
378
- assert_equal('fec0:0000:0000:0000:0000:0000:0000:0001',cidr6.ip() )
379
- assert_equal('fec0::1',cidr6.ip(:Short => true) )
380
-
381
- assert_raise(ArgumentError) {cidr4.last(:test => true)}
382
- assert_equal('192.168.1.255',cidr4.last() )
383
- assert_equal('fec0:0000:0000:0000:ffff:ffff:ffff:ffff',cidr6.last() )
384
- assert_equal('fec0::ffff:ffff:ffff:ffff',cidr6.last(:Short => true) )
385
-
386
- assert_equal('/24',cidr4.netmask() )
387
- assert_equal('/64',cidr6.netmask() )
388
-
389
- assert_equal('255.255.255.0',cidr4.netmask_ext() )
390
-
391
- assert_raise(ArgumentError) {cidr4.network(:test => true)}
392
- assert_equal('192.168.1.0',cidr4.network() )
393
- assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000',cidr6.network() )
394
- assert_equal('fec0::',cidr6.network(:Short => true) )
395
-
396
473
  assert_equal(256,cidr4.size() )
397
- assert_equal(2**64,cidr6.size() )
474
+ assert_equal(2**64,cidr6.size() )
398
475
  end
399
-
476
+
400
477
  def test_subnet
401
478
  cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
402
479
  cidr6 = NetAddr::CIDR.create('fec0::/64')
403
-
480
+
404
481
  assert_raise(ArgumentError) {cidr4.subnet(:test => true)}
405
482
  subnet4 = cidr4.subnet(:Bits => 26, :NumSubnets => 4)
406
483
  subnet6 = cidr6.subnet(:Bits => 66, :NumSubnets => 4)
407
484
  assert_equal('192.168.1.0/26', subnet4[0])
408
- assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/66', subnet6[0])
409
-
485
+ assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/66', subnet6[0])
486
+
410
487
  subnet4 = cidr4.subnet(:Bits => 26, :NumSubnets => 1)
411
488
  assert_equal('192.168.1.0/26', subnet4[0])
412
489
  assert_equal('192.168.1.64/26', subnet4[1])
413
490
  assert_equal('192.168.1.128/25', subnet4[2])
414
-
491
+
415
492
  subnet4 = cidr4.subnet(:Bits => 28, :NumSubnets => 3, :Objectify => true)
416
493
  assert_equal('192.168.1.0/28', subnet4[0].desc)
417
494
  assert_equal('192.168.1.16/28', subnet4[1].desc)
@@ -419,38 +496,47 @@ class TestCIDR < Test::Unit::TestCase
419
496
  assert_equal('192.168.1.48/28', subnet4[3].desc)
420
497
  assert_equal('192.168.1.64/26', subnet4[4].desc)
421
498
  assert_equal('192.168.1.128/25', subnet4[5].desc)
422
-
499
+
423
500
  subnet4 = cidr4.subnet(:IPCount => 112)
424
501
  assert_equal('192.168.1.0/25', subnet4[0])
425
-
502
+
426
503
  subnet4 = cidr4.subnet(:IPCount => 31)
427
- assert_equal('192.168.1.0/27', subnet4[0])
504
+ assert_equal('192.168.1.0/27', subnet4[0])
428
505
  end
429
-
506
+
507
+ def test_succ
508
+ cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
509
+ cidr6 = NetAddr::CIDR.create('fec0::/64')
510
+ cidr4_2 = NetAddr::CIDR.create('255.255.255.0/24')
511
+
512
+ assert_equal('192.168.2.0/24',cidr4.succ.desc)
513
+ assert_equal('fec0:0000:0000:0001:0000:0000:0000:0000/64',cidr6.succ.desc )
514
+ assert_raise(NetAddr::BoundaryError) {cidr4_2.succ}
515
+ end
516
+
517
+ def test_to_i
518
+ cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
519
+
520
+ assert_equal(3232235776,cidr4.to_i )
521
+ assert_equal(4294967040,cidr4.to_i(:netmask) )
522
+ assert_equal(3232235777,cidr4.to_i(:ip) )
523
+ assert_equal(255,cidr4.to_i(:hostmask) )
524
+ assert_equal(4294967040,cidr4.to_i(:wildcard_mask) )
525
+ end
526
+
527
+ def test_unique_local
528
+ eui = NetAddr::EUI48.new('abcdef010203')
529
+ cidr = NetAddr::CIDR.create('FC00::/7')
530
+ assert_kind_of(NetAddr::CIDRv6, NetAddr::CIDRv6.unique_local(eui))
531
+ assert(cidr.contains?(NetAddr::CIDRv6.unique_local(eui)) )
532
+ end
533
+
430
534
  def test_wildcard_mask
431
- cidr = NetAddr::CIDR.create('10.1.0.0/24')
432
- assert_equal('0.0.0.255', cidr.wildcard_mask(true))
433
- assert_equal('255.255.255.0', cidr.wildcard_mask)
434
-
435
- cidr.set_wildcard_mask('0.7.0.255', true)
535
+ cidr = NetAddr::CIDR.create('10.1.0.0/24', :WildcardMask => ['0.7.0.255', true])
436
536
  assert_equal('0.7.0.255', cidr.wildcard_mask(true))
437
- assert_equal('255.248.255.0', cidr.wildcard_mask())
438
- cidr.set_wildcard_mask('255.248.255.0')
439
- assert_equal('0.7.0.255', cidr.wildcard_mask(true))
440
- assert_equal('255.248.255.0', cidr.wildcard_mask())
441
- cidr.set_wildcard_mask('0.0.0.0')
442
- assert_equal('0.0.0.0', cidr.wildcard_mask)
443
- assert_raise(NetAddr::ValidationError){ cidr.set_wildcard_mask('0000:ffff::ffff') }
444
-
445
- cidr = NetAddr::CIDR.create('fec0::1/64')
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) }
450
-
537
+ assert_equal('255.248.255.0', cidr.wildcard_mask)
451
538
  end
452
539
 
453
-
454
540
  end
455
541
 
456
542