netaddr 1.0.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/Errors +13 -0
- data/README +202 -0
- data/lib/cidr.rb +1925 -0
- data/lib/eui.rb +390 -0
- data/lib/methods.rb +1273 -0
- data/lib/net_addr.rb +26 -0
- data/lib/tree.rb +880 -0
- data/tests/cidr_test.rb +405 -0
- data/tests/eui_test.rb +70 -0
- data/tests/methods_test.rb +303 -0
- data/tests/tree_test.rb +304 -0
- metadata +55 -0
data/tests/cidr_test.rb
ADDED
@@ -0,0 +1,405 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require '../lib/net_addr.rb'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
class TestCIDR < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def test_create
|
11
|
+
assert_raise(ArgumentError) {NetAddr::CIDR.create('192.168.1.0/24', :test => true)}
|
12
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.0/24')}
|
13
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.0/24') }
|
14
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.0 255.255.255.0')}
|
15
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1') }
|
16
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1 ') }
|
17
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::/64') }
|
18
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create('192.168.1.1/24 255.255.0.0')}
|
19
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1/64')}
|
20
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create('fec0::1/64')}
|
21
|
+
assert_nothing_raised(Exception){NetAddr::CIDR.create(0x0a0a0a0a, :PackedNetmask => 0xffffffff)}
|
22
|
+
|
23
|
+
assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('192.168.1.1 255.255.0.0'))
|
24
|
+
assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('192.168.1.1/24 255.255.0.0'))
|
25
|
+
assert_kind_of(NetAddr::CIDRv6, NetAddr::CIDR.create('fec0::1/64'))
|
26
|
+
assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('10.10.10.10/32 255.255.255.0'))
|
27
|
+
assert_kind_of(NetAddr::CIDRv4, NetAddr::CIDR.create('10.10.10.10/32', :PackedNetmask => 0xffffff00))
|
28
|
+
|
29
|
+
assert_raise(ArgumentError){ NetAddr::CIDR.create(:Version => 4) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_new
|
33
|
+
assert_raise(ArgumentError) {NetAddr::CIDRv4.new('192.168.1.0/24', :test => true)}
|
34
|
+
assert_raise(ArgumentError) {NetAddr::CIDRv6.new('fec0::1/64', :test => true)}
|
35
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.0/24')}
|
36
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.0/24') }
|
37
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.0 255.255.255.0')}
|
38
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.1') }
|
39
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.1 ') }
|
40
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv4.new('192.168.1.1/24 255.255.0.0')}
|
41
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv4.new(0x0a0a0a0a, :PackedNetmask => 0xffffffff)}
|
42
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv6.new('fec0::1/64')}
|
43
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv6.new('fec0::1/64')}
|
44
|
+
assert_nothing_raised(Exception){NetAddr::CIDRv6.new('fec0::/64') }
|
45
|
+
|
46
|
+
cidr = NetAddr::CIDRv4.new('192.168.1.1 255.255.0.0')
|
47
|
+
assert_equal(16, cidr.bits )
|
48
|
+
|
49
|
+
|
50
|
+
cidr = NetAddr::CIDRv4.new('192.168.1.1/24 255.255.0.0')
|
51
|
+
assert_equal(24, cidr.bits )
|
52
|
+
|
53
|
+
|
54
|
+
cidr = NetAddr::CIDRv4.new('10.10.10.10/32 255.255.255.0')
|
55
|
+
assert_equal(32, cidr.bits )
|
56
|
+
|
57
|
+
|
58
|
+
cidr = NetAddr::CIDRv4.new('10.10.10.10/32', :PackedNetmask => 0xffffff00)
|
59
|
+
assert_equal(24, cidr.bits )
|
60
|
+
|
61
|
+
cidr = NetAddr::CIDR.create('fec0::1/64')
|
62
|
+
assert_equal(64, cidr.bits )
|
63
|
+
|
64
|
+
assert_raise(ArgumentError){ NetAddr::CIDRv4.new({}) }
|
65
|
+
assert_raise(ArgumentError){ NetAddr::CIDRv6.new({}) }
|
66
|
+
assert_raise(ArgumentError){ NetAddr::CIDRv6.new('fec0::1/64 255.255.255.0') }
|
67
|
+
assert_raise(NetAddr::ValidationError){ NetAddr::CIDRv4.new('192.168.1.0 a') }
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
def test_cmp
|
72
|
+
|
73
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
74
|
+
cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
|
75
|
+
cidr6 = NetAddr::CIDR.create('fec0::/64')
|
76
|
+
cidr6_2 = NetAddr::CIDR.create('fec0::/96')
|
77
|
+
|
78
|
+
assert_equal(1,cidr4.cmp('192.168.1.0/26') )
|
79
|
+
assert_equal(-1,cidr4.cmp('192.168.0.0/23') )
|
80
|
+
assert_equal(0,cidr4.cmp('192.168.1.0/24') )
|
81
|
+
assert_nil(cidr4.cmp('192.168.2.0/26') )
|
82
|
+
assert_equal(1,cidr4.cmp(cidr4_2) )
|
83
|
+
assert_equal(1,cidr6.cmp('fec0::/96') )
|
84
|
+
assert_equal(-1,cidr6.cmp('fec0::/63') )
|
85
|
+
assert_equal(0,cidr6.cmp('fec0::/64') )
|
86
|
+
assert_nil(cidr6.cmp('fe80::/64') )
|
87
|
+
assert_equal(1,cidr6.cmp(cidr6_2) )
|
88
|
+
|
89
|
+
assert_raise(NetAddr::VersionError) { cidr4.cmp(cidr6_2) }
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
def test_contains?
|
94
|
+
|
95
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
96
|
+
cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
|
97
|
+
cidr6 = NetAddr::CIDR.create('fec0::/64')
|
98
|
+
cidr6_2 = NetAddr::CIDR.create('fec0::/96')
|
99
|
+
|
100
|
+
assert_equal(true,cidr4.contains?('192.168.1.0/26') )
|
101
|
+
assert_equal(true,cidr4.contains?(cidr4_2) )
|
102
|
+
assert_equal(true,cidr6.contains?(cidr6_2) )
|
103
|
+
assert_equal(false,cidr4.contains?('192.168.2.0/26') )
|
104
|
+
assert_equal(false,cidr6.contains?('fe80::/96') )
|
105
|
+
|
106
|
+
assert_raise(NetAddr::VersionError) { cidr4.contains?(cidr6_2) }
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
def test_enumerate
|
111
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
112
|
+
cidr6 = NetAddr::CIDR.create('fec0::/64')
|
113
|
+
|
114
|
+
assert_raise(ArgumentError) {cidr4.enumerate(:test => true)}
|
115
|
+
assert_equal(['192.168.1.0', '192.168.1.1'],cidr4.enumerate(:Limit => 2) )
|
116
|
+
assert_equal(['fec0:0000:0000:0000:0000:0000:0000:0000'],cidr6.enumerate(:Limit => 1) )
|
117
|
+
assert_equal(['fec0::'],cidr6.enumerate(:Limit => 1, :Short => true) )
|
118
|
+
|
119
|
+
|
120
|
+
enums4 = cidr4.enumerate(:Limit => 2, :Bitstep => 5)
|
121
|
+
enums6 = cidr6.enumerate(:Limit => 2, :Bitstep => 5)
|
122
|
+
assert_equal('192.168.1.5', enums4[1] )
|
123
|
+
assert_equal('fec0:0000:0000:0000:0000:0000:0000:0005', enums6[1] )
|
124
|
+
|
125
|
+
enums4 = cidr4.enumerate(:Objectify => true,:Limit => 1)
|
126
|
+
assert_kind_of(NetAddr::CIDR, enums4[0] )
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_eql?
|
131
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
132
|
+
cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
|
133
|
+
cidr4_3 = NetAddr::CIDR.create('192.168.1.0/24')
|
134
|
+
cidr6 = NetAddr::CIDR.create('fec0::/64')
|
135
|
+
cidr6_2 = NetAddr::CIDR.create('fec0::/96')
|
136
|
+
cidr6_3 = NetAddr::CIDR.create('fec0::/64')
|
137
|
+
|
138
|
+
assert_equal(true,cidr4.eql?('192.168.1.0/24') )
|
139
|
+
assert_equal(false,cidr4.eql?('192.168.1.0/26') )
|
140
|
+
assert_equal(true,cidr4.eql?(cidr4_3) )
|
141
|
+
assert_equal(false,cidr4.eql?(cidr4_2) )
|
142
|
+
assert_equal(true,cidr6.eql?('fec0::/64') )
|
143
|
+
assert_equal(false,cidr6.eql?('fec0::/96') )
|
144
|
+
assert_equal(true,cidr6.eql?(cidr6_3) )
|
145
|
+
assert_equal(false,cidr6.eql?(cidr6_2) )
|
146
|
+
|
147
|
+
assert_raise(NetAddr::VersionError) {cidr4.eql?(cidr6) }
|
148
|
+
assert_raise(NetAddr::VersionError){cidr6.eql?(cidr4) }
|
149
|
+
assert_raise(ArgumentError) { cidr6.eql?('a') }
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_fill_in
|
153
|
+
cidr = NetAddr::CIDR.create('192.168.1.0/24')
|
154
|
+
filled = cidr.fill_in(['192.168.1.0/27','192.168.1.44/30',
|
155
|
+
'192.168.1.64/26','192.168.1.129'])
|
156
|
+
|
157
|
+
assert_equal(['192.168.1.0/27','192.168.1.32/29','192.168.1.40/30',
|
158
|
+
'192.168.1.44/30','192.168.1.48/28','192.168.1.64/26',
|
159
|
+
'192.168.1.128/32','192.168.1.129/32','192.168.1.130/31',
|
160
|
+
'192.168.1.132/30','192.168.1.136/29','192.168.1.144/28',
|
161
|
+
'192.168.1.160/27','192.168.1.192/26'],filled)
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_is_contained?
|
165
|
+
|
166
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
167
|
+
cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
|
168
|
+
cidr6 = NetAddr::CIDR.create('fec0::/64')
|
169
|
+
cidr6_2 = NetAddr::CIDR.create('fec0::/96')
|
170
|
+
|
171
|
+
assert_equal(true,cidr4_2.is_contained?('192.168.1.0/24') )
|
172
|
+
assert_equal(true,cidr4_2.is_contained?(cidr4) )
|
173
|
+
assert_equal(true,cidr6_2.is_contained?('fec0::/64') )
|
174
|
+
assert_equal(true,cidr6_2.is_contained?(cidr6) )
|
175
|
+
assert_equal(false,cidr4.is_contained?('192.168.2.0/26') )
|
176
|
+
assert_equal(false,cidr6.is_contained?('fe80::/96') )
|
177
|
+
|
178
|
+
assert_raise(NetAddr::VersionError) { cidr4.is_contained?(cidr6_2) }
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_mcast
|
182
|
+
cidr4 = NetAddr::CIDR.create('224.0.0.1')
|
183
|
+
cidr4_2 = NetAddr::CIDR.create('239.255.255.255')
|
184
|
+
cidr4_3 = NetAddr::CIDR.create('230.2.3.5')
|
185
|
+
cidr4_4 = NetAddr::CIDR.create('235.147.18.23')
|
186
|
+
cidr4_5 = NetAddr::CIDR.create('192.168.1.1')
|
187
|
+
cidr6 = NetAddr::CIDR.create('ff00::1')
|
188
|
+
cidr6_2 = NetAddr::CIDR.create('ffff::1')
|
189
|
+
cidr6_3 = NetAddr::CIDR.create('ff00::ffff:ffff')
|
190
|
+
cidr6_4 = NetAddr::CIDR.create('ff00::fec0:1234:')
|
191
|
+
cidr6_5 = NetAddr::CIDR.create('2001:4800::1')
|
192
|
+
|
193
|
+
assert_raise(ArgumentError) {cidr4.multicast_mac(:test => true)}
|
194
|
+
assert_equal('01-00-5e-00-00-01',cidr4.multicast_mac(:Objectify => true).address )
|
195
|
+
assert_equal('01-00-5e-7f-ff-ff',cidr4_2.multicast_mac )
|
196
|
+
assert_equal('01-00-5e-02-03-05',cidr4_3.multicast_mac )
|
197
|
+
assert_equal('01-00-5e-13-12-17',cidr4_4.multicast_mac )
|
198
|
+
|
199
|
+
assert_equal('33-33-00-00-00-01',cidr6.multicast_mac(:Objectify => true).address )
|
200
|
+
assert_equal('33-33-00-00-00-01',cidr6_2.multicast_mac )
|
201
|
+
assert_equal('33-33-ff-ff-ff-ff',cidr6_3.multicast_mac )
|
202
|
+
assert_equal('33-33-fe-c0-12-34',cidr6_4.multicast_mac )
|
203
|
+
|
204
|
+
assert_raise(NetAddr::ValidationError){ cidr4_5.multicast_mac }
|
205
|
+
assert_raise(NetAddr::ValidationError){ cidr6_5.multicast_mac }
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_next_ip
|
209
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
210
|
+
cidr6 = NetAddr::CIDR.create('fec0::/64')
|
211
|
+
|
212
|
+
assert_raise(ArgumentError) {cidr4.next_ip(:test => true)}
|
213
|
+
next4 = cidr4.next_ip()
|
214
|
+
next6 = cidr6.next_ip()
|
215
|
+
assert_equal('192.168.2.0',next4 )
|
216
|
+
assert_equal('fec0:0000:0000:0001:0000:0000:0000:0000',next6 )
|
217
|
+
|
218
|
+
next6 = cidr6.next_ip(:Short => true)
|
219
|
+
assert_equal('fec0:0:0:1::',next6 )
|
220
|
+
|
221
|
+
next4 = cidr4.next_ip(:Bitstep => 2)
|
222
|
+
next6 = cidr6.next_ip(:Bitstep => 2)
|
223
|
+
assert_equal('192.168.2.1',next4 )
|
224
|
+
assert_equal('fec0:0000:0000:0001:0000:0000:0000:0001',next6 )
|
225
|
+
|
226
|
+
next4 = cidr4.next_ip(:Objectify => true)
|
227
|
+
next6 = cidr6.next_ip(:Objectify => true)
|
228
|
+
assert_equal('192.168.2.0/32',next4.desc )
|
229
|
+
assert_equal('fec0:0000:0000:0001:0000:0000:0000:0000/128',next6.desc )
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_next_subnet
|
234
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
235
|
+
cidr6 = NetAddr::CIDR.create('fec0::/64')
|
236
|
+
|
237
|
+
assert_raise(ArgumentError) {cidr4.next_subnet(:test => true)}
|
238
|
+
next4 = cidr4.next_subnet()
|
239
|
+
next6 = cidr6.next_subnet()
|
240
|
+
assert_equal('192.168.2.0/24',next4 )
|
241
|
+
assert_equal('fec0:0000:0000:0001:0000:0000:0000:0000/64',next6 )
|
242
|
+
|
243
|
+
next6 = cidr6.next_subnet(:Short => true)
|
244
|
+
assert_equal('fec0:0:0:1::/64',next6 )
|
245
|
+
|
246
|
+
next4 = cidr4.next_subnet(:Bitstep => 2)
|
247
|
+
next6 = cidr6.next_subnet(:Bitstep => 2)
|
248
|
+
assert_equal('192.168.3.0/24',next4 )
|
249
|
+
assert_equal('fec0:0000:0000:0002:0000:0000:0000:0000/64',next6 )
|
250
|
+
|
251
|
+
next4 = cidr4.next_subnet(:Objectify => true)
|
252
|
+
next6 = cidr6.next_subnet(:Objectify => true)
|
253
|
+
assert_equal('192.168.2.0/24',next4.desc )
|
254
|
+
assert_equal('fec0:0000:0000:0001:0000:0000:0000:0000/64',next6.desc )
|
255
|
+
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_nth
|
259
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
260
|
+
cidr6 = NetAddr::CIDR.create('fec0::/126')
|
261
|
+
|
262
|
+
assert_raise(ArgumentError) {cidr4.nth(1, :test => true)}
|
263
|
+
assert_equal('192.168.1.1',cidr4.nth(1) )
|
264
|
+
assert_equal('192.168.1.50',cidr4.nth(50) )
|
265
|
+
assert_kind_of(NetAddr::CIDR,cidr4.nth(1, :Objectify => true) )
|
266
|
+
assert_raise(NetAddr::BoundaryError){ cidr4.nth(256) }
|
267
|
+
assert_raise(ArgumentError){ cidr4.nth() }
|
268
|
+
|
269
|
+
assert_equal('fec0:0000:0000:0000:0000:0000:0000:0001',cidr6.nth(1) )
|
270
|
+
assert_equal('fec0:0000:0000:0000:0000:0000:0000:0003',cidr6.nth(3) )
|
271
|
+
assert_equal('fec0::1',cidr6.nth(1, :Short => true) )
|
272
|
+
assert_raise(NetAddr::BoundaryError){ cidr6.nth(10) }
|
273
|
+
|
274
|
+
assert_raise(ArgumentError) { cidr4.nth({}) }
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_range
|
278
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
279
|
+
cidr6 = NetAddr::CIDR.create('fec0::/64')
|
280
|
+
|
281
|
+
assert_raise(ArgumentError) {cidr4.range(25,0, :test => true)}
|
282
|
+
range4 = cidr4.range(25,0, :Bitstep => 5)
|
283
|
+
range4_2 = cidr4.range(250)
|
284
|
+
range6 = cidr6.range(25,0, :Bitstep => 5, :Short => true)
|
285
|
+
|
286
|
+
assert_equal(6,range4.length)
|
287
|
+
assert_equal(6,range4_2.length)
|
288
|
+
assert_equal(6,range6.length)
|
289
|
+
assert_equal('192.168.1.0',range4[0])
|
290
|
+
assert_equal('192.168.1.25',range4[5])
|
291
|
+
assert_equal('fec0::',range6[0])
|
292
|
+
assert_equal('fec0::19',range6[5])
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_remainder
|
296
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
297
|
+
cidr4_2 = NetAddr::CIDR.create('192.168.1.64/26')
|
298
|
+
|
299
|
+
assert_raise(ArgumentError) {cidr4.remainder(cidr4_2, :test => true)}
|
300
|
+
remainder = cidr4.remainder(cidr4_2)
|
301
|
+
|
302
|
+
assert_equal(2,remainder.length)
|
303
|
+
assert_equal('192.168.1.0/26',remainder[0])
|
304
|
+
|
305
|
+
remainder = cidr4.remainder('192.168.1.64/26', :Objectify => true)
|
306
|
+
assert_equal('192.168.1.128/25',remainder[1].desc)
|
307
|
+
end
|
308
|
+
|
309
|
+
def test_resize
|
310
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.129/24')
|
311
|
+
cidr6 = NetAddr::CIDR.create('fec0::1/64')
|
312
|
+
|
313
|
+
assert_raise(ArgumentError) {cidr4.resize(23, :test => true)}
|
314
|
+
new4 = cidr4.resize(23)
|
315
|
+
new6 = cidr6.resize(63)
|
316
|
+
assert_equal('192.168.0.0/23',new4.desc )
|
317
|
+
assert_equal('fec0::/63',new6.desc(:Short => true) )
|
318
|
+
|
319
|
+
cidr4.resize!(25)
|
320
|
+
cidr6.resize!(67)
|
321
|
+
assert_equal('192.168.1.0/25',cidr4.desc )
|
322
|
+
assert_equal('192.168.1.0',cidr4.ip )
|
323
|
+
assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/67',cidr6.desc )
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_simple_methods
|
327
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.1/24')
|
328
|
+
cidr4_2 = NetAddr::CIDR.create('192.168.1.0/26')
|
329
|
+
cidr6 = NetAddr::CIDR.create('fec0::1/64')
|
330
|
+
cidr6_2 = NetAddr::CIDR.create('fec0::/96')
|
331
|
+
|
332
|
+
assert_equal('1.168.192.in-addr.arpa.', cidr4.arpa() )
|
333
|
+
assert_equal('0.0.0.0.0.0.0.0.0.0.0.0.0.c.e.f.ip6.arpa.', cidr6.arpa() )
|
334
|
+
|
335
|
+
assert_raise(ArgumentError) {cidr4.desc(:test => true)}
|
336
|
+
assert_equal('192.168.1.0/24',cidr4.desc() )
|
337
|
+
assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/64',cidr6.desc() )
|
338
|
+
|
339
|
+
assert_equal('fec0::/64',cidr6.desc(:Short => true) )
|
340
|
+
|
341
|
+
assert_equal('0.0.0.255',cidr4.hostmask_ext() )
|
342
|
+
|
343
|
+
assert_equal(24,cidr4.bits() )
|
344
|
+
assert_equal(64,cidr6.bits() )
|
345
|
+
|
346
|
+
assert_raise(ArgumentError) {cidr4.ip(:test => true)}
|
347
|
+
assert_equal('192.168.1.1',cidr4.ip() )
|
348
|
+
assert_equal('fec0:0000:0000:0000:0000:0000:0000:0001',cidr6.ip() )
|
349
|
+
assert_equal('fec0::1',cidr6.ip(:Short => true) )
|
350
|
+
|
351
|
+
assert_raise(ArgumentError) {cidr4.last(:test => true)}
|
352
|
+
assert_equal('192.168.1.255',cidr4.last() )
|
353
|
+
assert_equal('fec0:0000:0000:0000:ffff:ffff:ffff:ffff',cidr6.last() )
|
354
|
+
assert_equal('fec0::ffff:ffff:ffff:ffff',cidr6.last(:Short => true) )
|
355
|
+
|
356
|
+
assert_equal('/24',cidr4.netmask() )
|
357
|
+
assert_equal('/64',cidr6.netmask() )
|
358
|
+
|
359
|
+
assert_equal('255.255.255.0',cidr4.netmask_ext() )
|
360
|
+
|
361
|
+
assert_raise(ArgumentError) {cidr4.network(:test => true)}
|
362
|
+
assert_equal('192.168.1.0',cidr4.network() )
|
363
|
+
assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000',cidr6.network() )
|
364
|
+
assert_equal('fec0::',cidr6.network(:Short => true) )
|
365
|
+
|
366
|
+
assert_equal(256,cidr4.size() )
|
367
|
+
assert_equal(2**64,cidr6.size() )
|
368
|
+
end
|
369
|
+
|
370
|
+
def test_subnet
|
371
|
+
cidr4 = NetAddr::CIDR.create('192.168.1.0/24')
|
372
|
+
cidr6 = NetAddr::CIDR.create('fec0::/64')
|
373
|
+
|
374
|
+
assert_raise(ArgumentError) {cidr4.subnet(:test => true)}
|
375
|
+
subnet4 = cidr4.subnet(:Bits => 26, :NumSubnets => 4)
|
376
|
+
subnet6 = cidr6.subnet(:Bits => 66, :NumSubnets => 4)
|
377
|
+
assert_equal('192.168.1.0/26', subnet4[0])
|
378
|
+
assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/66', subnet6[0])
|
379
|
+
|
380
|
+
subnet4 = cidr4.subnet(:Bits => 26, :NumSubnets => 1)
|
381
|
+
assert_equal('192.168.1.0/26', subnet4[0])
|
382
|
+
assert_equal('192.168.1.64/26', subnet4[1])
|
383
|
+
assert_equal('192.168.1.128/25', subnet4[2])
|
384
|
+
|
385
|
+
subnet4 = cidr4.subnet(:Bits => 28, :NumSubnets => 3, :Objectify => true)
|
386
|
+
assert_equal('192.168.1.0/28', subnet4[0].desc)
|
387
|
+
assert_equal('192.168.1.16/28', subnet4[1].desc)
|
388
|
+
assert_equal('192.168.1.32/28', subnet4[2].desc)
|
389
|
+
assert_equal('192.168.1.48/28', subnet4[3].desc)
|
390
|
+
assert_equal('192.168.1.64/26', subnet4[4].desc)
|
391
|
+
assert_equal('192.168.1.128/25', subnet4[5].desc)
|
392
|
+
|
393
|
+
subnet4 = cidr4.subnet(:IPCount => 112)
|
394
|
+
assert_equal('192.168.1.0/25', subnet4[0])
|
395
|
+
|
396
|
+
subnet4 = cidr4.subnet(:IPCount => 31)
|
397
|
+
assert_equal('192.168.1.0/27', subnet4[0])
|
398
|
+
end
|
399
|
+
|
400
|
+
|
401
|
+
end
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
|
data/tests/eui_test.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require '../lib/net_addr.rb'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
class TestEUI < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def test_new
|
11
|
+
assert_not_nil(NetAddr::EUI48.new('aa-bb-cc-dd-ee-ff') )
|
12
|
+
assert_not_nil(NetAddr::EUI48.new('aa-bb-cc-dd-ee-ff') )
|
13
|
+
assert_not_nil(NetAddr::EUI48.new('aa:bb:cc:dd:ee:ff') )
|
14
|
+
assert_not_nil(NetAddr::EUI48.new('aabb.ccdd.eeff') )
|
15
|
+
assert_not_nil(NetAddr::EUI64.new('aa-bb-cc-dd-ee-ff-00-01') )
|
16
|
+
assert_not_nil(NetAddr::EUI48.new(0x000000000001) )
|
17
|
+
assert_not_nil(NetAddr::EUI64.new(0x0000000000000001) )
|
18
|
+
|
19
|
+
assert_raise(ArgumentError){ NetAddr::EUI48.new() }
|
20
|
+
assert_raise(ArgumentError){ NetAddr::EUI48.new({}) }
|
21
|
+
assert_raise(ArgumentError){ NetAddr::EUI64.new() }
|
22
|
+
assert_raise(ArgumentError){ NetAddr::EUI64.new({}) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_create
|
26
|
+
assert_not_nil(NetAddr::EUI.create('aa-bb-cc-dd-ee-ff') )
|
27
|
+
assert_not_nil(NetAddr::EUI.create('aa:bb:cc:dd:ee:ff') )
|
28
|
+
assert_not_nil(NetAddr::EUI.create('aabb.ccdd.eeff') )
|
29
|
+
assert_not_nil(NetAddr::EUI.create('aa-bb-cc-dd-ee-ff-00-01') )
|
30
|
+
|
31
|
+
assert_raise(ArgumentError){ NetAddr::EUI.create() }
|
32
|
+
assert_raise(ArgumentError){ NetAddr::EUI.create(0x0000000000000001) }
|
33
|
+
|
34
|
+
assert_kind_of(NetAddr::EUI48, NetAddr::EUI.create('aa-bb-cc-dd-ee-ff'))
|
35
|
+
assert_kind_of(NetAddr::EUI64, NetAddr::EUI.create('aa-bb-cc-dd-ee-ff-00-01'))
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_simple
|
39
|
+
mac = NetAddr::EUI48.new('aa-bb-cc-dd-ee-ff')
|
40
|
+
assert_raise(ArgumentError) {mac.oui(:test => true)}
|
41
|
+
assert_raise(ArgumentError) {mac.ei(:test => true)}
|
42
|
+
|
43
|
+
assert_equal('aa-bb-cc', mac.oui )
|
44
|
+
assert_equal('dd-ee-ff', mac.ei )
|
45
|
+
assert_equal(NetAddr::EUI48, mac.class )
|
46
|
+
|
47
|
+
|
48
|
+
mac = NetAddr::EUI64.new('aa-bb-cc-dd-ee-ff-01-02')
|
49
|
+
assert_raise(ArgumentError) {mac.oui(:test => true)}
|
50
|
+
assert_raise(ArgumentError) {mac.ei(:test => true)}
|
51
|
+
assert_equal('aa-bb-cc', mac.oui )
|
52
|
+
assert_equal('dd-ee-ff-01-02', mac.ei )
|
53
|
+
assert_equal('aa-bb-cc-dd-ee-ff-01-02', mac.address )
|
54
|
+
assert_equal(NetAddr::EUI64, mac.class )
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_link_local
|
59
|
+
mac = NetAddr::EUI48.new('aa-bb-cc-dd-ee-ff')
|
60
|
+
assert_equal('fe80:0000:0000:0000:aabb:ccff:fedd:eeff', mac.link_local )
|
61
|
+
|
62
|
+
mac = NetAddr::EUI48.new('1234.5678.9abc')
|
63
|
+
assert_equal('fe80:0000:0000:0000:1234:56ff:fe78:9abc', mac.link_local )
|
64
|
+
|
65
|
+
mac = NetAddr::EUI64.new('1234.5678.9abc.def0')
|
66
|
+
assert_equal('fe80:0000:0000:0000:1234:5678:9abc:def0', mac.link_local(:Objectify => true).ip )
|
67
|
+
assert_raise(ArgumentError) {mac.link_local(:test => true)}
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|