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.

@@ -0,0 +1,303 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require '../lib/net_addr.rb'
4
+ require 'test/unit'
5
+
6
+
7
+
8
+ class TestMethods < Test::Unit::TestCase
9
+
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
+ assert_raise(ArgumentError){ NetAddr.merge(1) }
60
+ assert_raise(ArgumentError){ NetAddr.merge({}) }
61
+ end
62
+
63
+
64
+ def test_minimum_size
65
+ assert_raise(ArgumentError) {NetAddr.minimum_size(200, :test => true)}
66
+ assert_equal(24, NetAddr.minimum_size(200))
67
+ assert_equal(96, NetAddr.minimum_size(2**32-1, :Version => 6))
68
+
69
+ assert_raise(ArgumentError){ NetAddr.minimum_size({}) }
70
+ end
71
+
72
+
73
+ def test_pack_ip_addr
74
+ assert_raise(ArgumentError) {NetAddr.pack_ip_addr('255.255.255.255', :test => true)}
75
+ assert_equal(2**128-1, NetAddr.pack_ip_addr('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff') )
76
+ assert_equal(1, NetAddr.pack_ip_addr('::1') )
77
+ assert_equal(2**32-1, NetAddr.pack_ip_addr('255.255.255.255') )
78
+ assert_equal(2**128-1, NetAddr.pack_ip_addr('ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255') )
79
+ assert_equal(0, NetAddr.pack_ip_addr('::') )
80
+ assert_equal(2**32-1, NetAddr.pack_ip_addr('::255.255.255.255') )
81
+ assert_equal(0x0a0a0a0a, NetAddr.pack_ip_addr('10.10.10.10') )
82
+ assert_equal(2**127+1, NetAddr.pack_ip_addr('8000::0.0.0.1') )
83
+ assert_equal(0x8080000000000000000080800a0a0a0a, NetAddr.pack_ip_addr('8080::8080:10.10.10.10') )
84
+ assert_equal(0xffff0a010001, NetAddr.pack_ip_addr('::ffff:10.1.0.1') )
85
+ assert_equal(2**127+1, NetAddr.pack_ip_addr('8000::1') )
86
+ assert_equal(1, NetAddr.pack_ip_addr('::1') )
87
+ assert_equal(2**127, NetAddr.pack_ip_addr('8000::') )
88
+
89
+ assert_raise(ArgumentError){ NetAddr.pack_ip_addr({}) }
90
+ assert_raise(NetAddr::VersionError){ NetAddr.pack_ip_addr('192.168.1.1',:Version => 5) }
91
+ assert_raise(ArgumentError){ NetAddr.pack_ip_addr(0xffffffff,:Version => 4) }
92
+ end
93
+
94
+
95
+ def test_pack_ip_netmask
96
+ assert_raise(ArgumentError) {NetAddr.pack_ip_netmask('32', :test => true)}
97
+ assert_equal(2**32-1, NetAddr.pack_ip_netmask('255.255.255.255') )
98
+ assert_equal(2**32-1, NetAddr.pack_ip_netmask('32') )
99
+ assert_equal(2**32-1, NetAddr.pack_ip_netmask('/32') )
100
+ assert_equal(2**32-1, NetAddr.pack_ip_netmask(32) )
101
+ assert_equal(2**128-1, NetAddr.pack_ip_netmask('128', :Version => 6) )
102
+ assert_equal(2**128-1, NetAddr.pack_ip_netmask('/128', :Version => 6) )
103
+ assert_equal(2**128-1, NetAddr.pack_ip_netmask(128, :Version => 6) )
104
+
105
+ assert_raise(ArgumentError){ NetAddr.pack_ip_netmask({}) }
106
+ assert_raise(NetAddr::VersionError){ NetAddr.pack_ip_netmask('/24',:Version => 5) }
107
+ assert_raise(ArgumentError){ NetAddr.pack_ip_netmask([], :Version => 4) }
108
+ end
109
+
110
+
111
+ def test_range
112
+ cidr4_1 = NetAddr::CIDR.create('192.168.1.0/24')
113
+ cidr4_2 = NetAddr::CIDR.create('192.168.1.50/24')
114
+ cidr4_3 = NetAddr::CIDR.create('192.168.1.2/24')
115
+ cidr6_1 = NetAddr::CIDR.create('fec0::/64')
116
+ cidr6_2 = NetAddr::CIDR.create('fec0::32/64')
117
+
118
+ assert_raise(ArgumentError) {NetAddr.range(cidr4_1,cidr4_2, :test => true)}
119
+ assert_equal(['192.168.1.1'], NetAddr.range(cidr4_1,cidr4_2, :Limit => 1) )
120
+ assert_equal(['fec0:0000:0000:0000:0000:0000:0000:0001'], NetAddr.range(cidr6_1,cidr6_2, :Limit => 1) )
121
+
122
+ list = NetAddr.range('192.168.1.0/24','192.168.1.50/24', :Bitstep => 2)
123
+ assert_equal(25, list.length)
124
+ assert_equal('192.168.1.49', list[24])
125
+
126
+ list = NetAddr.range(cidr4_1,cidr4_3, :Objectify => true)
127
+ assert_kind_of(NetAddr::CIDR, list[0])
128
+ assert_equal('192.168.1.1/32', (list[0]).desc)
129
+
130
+ assert_raise(ArgumentError){ NetAddr.range(:Limit => 1) }
131
+ assert_raise(NetAddr::VersionError){ NetAddr.range(cidr4_1,cidr6_2) }
132
+ end
133
+
134
+
135
+ def test_shorten
136
+ assert_equal('fec0::', NetAddr.shorten('fec0:0000:0000:0000:0000:0000:0000:0000') )
137
+ assert_equal('fec0::2:0:0:1', NetAddr.shorten('fec0:0000:0000:0000:0002:0000:0000:0001') )
138
+ assert_equal('fec0::2:0:0:1', NetAddr.shorten('fec0:00:0000:0:02:0000:0:1') )
139
+ assert_equal('fec0::2:2:0:0:1', NetAddr.shorten('fec0:0000:0000:0002:0002:0000:0000:0001') )
140
+ assert_equal('fec0:0:0:1::', NetAddr.shorten('fec0:0000:0000:0001:0000:0000:0000:0000') )
141
+ assert_equal('fec0:1:1:1:1:1:1:1', NetAddr.shorten('fec0:0001:0001:0001:0001:0001:0001:0001') )
142
+ assert_equal('fec0:ffff:ffff:0:ffff:ffff:ffff:ffff', NetAddr.shorten('fec0:ffff:ffff:0000:ffff:ffff:ffff:ffff') )
143
+ assert_equal('fec0:ffff:ffff:ffff:ffff:ffff:ffff:ffff', NetAddr.shorten('fec0:ffff:ffff:ffff:ffff:ffff:ffff:ffff') )
144
+ assert_equal('fec0::', NetAddr.shorten('fec0::') )
145
+ assert_equal('fec0::192.168.1.1', NetAddr.shorten('fec0:0:0:0:0:0:192.168.1.1') )
146
+
147
+ assert_raise(ArgumentError){ NetAddr.shorten(1) }
148
+ end
149
+
150
+
151
+ def test_sort
152
+ cidr4_1 = NetAddr::CIDR.create('192.168.1.0/24')
153
+ cidr4_2 = NetAddr::CIDR.create('192.168.1.128/25')
154
+ cidr4_3 = NetAddr::CIDR.create('192.168.1.64/26')
155
+ cidr4_4 = NetAddr::CIDR.create('192.168.1.0/30')
156
+ cidr4_5 = '192.168.2.0/24'
157
+
158
+ cidr6_1 = NetAddr::CIDR.create('fec0::0/64')
159
+ cidr6_2 = NetAddr::CIDR.create('fec0::0/10')
160
+ cidr6_3 = NetAddr::CIDR.create('fe80::0/10')
161
+ cidr6_4 = 'fe80::0'
162
+
163
+ sort1 = NetAddr.sort([cidr4_1,cidr4_2,cidr4_3,cidr4_4,cidr4_5])
164
+ sort2 = NetAddr.sort([cidr6_1,cidr6_2,cidr6_3,cidr6_4])
165
+
166
+ assert_equal([cidr4_1,cidr4_4,cidr4_3,cidr4_2,cidr4_5], sort1)
167
+ assert_equal([cidr6_3,cidr6_4,cidr6_2,cidr6_1], sort2)
168
+
169
+ end
170
+
171
+
172
+ def test_unshorten
173
+ assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000', NetAddr.unshorten('fec0::') )
174
+ assert_equal('fec0:0000:0000:0000:0002:0000:0000:0001', NetAddr.unshorten('fec0::2:0:0:1') )
175
+ assert_equal('fec0:0000:0000:0000:0002:0000:0000:0001', NetAddr.unshorten('fec0:0:0:0:2:0:0:1') )
176
+ assert_equal('0000:0000:0000:0000:0000:ffff:10.1.0.1', NetAddr.unshorten('::ffff:10.1.0.1') )
177
+
178
+ assert_raise(ArgumentError){ NetAddr.unshorten(1) }
179
+ end
180
+
181
+
182
+ def test_unpack_ip_addr
183
+ assert_raise(ArgumentError) {NetAddr.unpack_ip_addr(2**32-1, :test => true)}
184
+ assert_equal('255.255.255.255', NetAddr.unpack_ip_addr(2**32-1) )
185
+ assert_equal('0.0.0.0', NetAddr.unpack_ip_addr(0, :Version => 4) )
186
+ assert_equal('0000:0000:0000:0000:0000:0000:0000:0000', NetAddr.unpack_ip_addr(0, :Version => 6) )
187
+ assert_equal('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', NetAddr.unpack_ip_addr(2**128-1) )
188
+ assert_equal('0000:0000:0000:0000:0000:0000:ffff:ffff', NetAddr.unpack_ip_addr(2**32-1, :Version => 6) )
189
+ assert_equal('0000:0000:0000:0000:0000:ffff:10.1.0.1', NetAddr.unpack_ip_addr(0xffff0a010001,
190
+ :IPv4Mapped => true,
191
+ :Version => 6) )
192
+
193
+ assert_raise(ArgumentError){ NetAddr.unpack_ip_addr('1') }
194
+ assert_raise(ArgumentError){ NetAddr.unpack_ip_addr({}) }
195
+ assert_raise(NetAddr::VersionError){ NetAddr.unpack_ip_addr(0xffffffff,:Version => 5) }
196
+ assert_raise(ArgumentError){ NetAddr.unpack_ip_addr('1', :Version => 4) }
197
+ end
198
+
199
+
200
+ def test_unpack_ip_netmask
201
+ assert_equal(32, NetAddr.unpack_ip_netmask(2**32-1) )
202
+ assert_equal(24, NetAddr.unpack_ip_netmask((2**32 - 2**8 ) ) )
203
+ assert_equal(128, NetAddr.unpack_ip_netmask(2**128-1) )
204
+ assert_equal(96, NetAddr.unpack_ip_netmask((2**128 - 2**32)) )
205
+
206
+ assert_raise(ArgumentError){ NetAddr.unpack_ip_netmask('1') }
207
+ assert_raise(ArgumentError){ NetAddr.unpack_ip_netmask({}) }
208
+ assert_raise(ArgumentError){ NetAddr.unpack_ip_netmask('1')}
209
+ end
210
+
211
+
212
+ def test_validate_eui
213
+ assert_nothing_raised(NetAddr::ValidationError) {NetAddr.validate_eui('aa-bb-cc-dd-ee-ff')}
214
+ assert_nothing_raised(NetAddr::ValidationError) {NetAddr.validate_eui('aabb.ccdd.eeff') }
215
+ assert_nothing_raised(NetAddr::ValidationError) {NetAddr.validate_eui('aa:bb:cc:dd:ee:ff') }
216
+ assert_nothing_raised(NetAddr::ValidationError) {NetAddr.validate_eui('aabb.ccdd.eeff.1234') }
217
+ assert_raise(NetAddr::ValidationError){NetAddr.validate_eui('aabb.ccdd.eeff.123') }
218
+ assert_raise(NetAddr::ValidationError){NetAddr.validate_eui('aabb.ccdd.eeff.12312') }
219
+ assert_raise(NetAddr::ValidationError){NetAddr.validate_eui('aa-bb-c-dd-ee-ff') }
220
+ assert_raise(NetAddr::ValidationError){NetAddr.validate_eui('aa:bb:cc:dd:e:ff') }
221
+
222
+ assert_raise(ArgumentError){ NetAddr.validate_eui(0xaabbccddeeff) }
223
+ assert_raise(ArgumentError){ NetAddr.validate_eui() }
224
+ end
225
+
226
+
227
+ def test_validate_ip_addr
228
+ assert_raise(ArgumentError) {NetAddr.validate_ip_addr('192.168.1.0', :test => true)}
229
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('192.168.1.0')}
230
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('255.255.255.255')}
231
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('224.0.0.1')}
232
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('0.192.0.1')}
233
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('0.0.0.0')}
234
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr(0xff0000)}
235
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr(2**32-1)}
236
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr(0)}
237
+
238
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')}
239
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('::')}
240
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('ffff::1')}
241
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('1234:5678:9abc:def0:1234:5678:9abc:def0')}
242
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('::1')}
243
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('ffff::')}
244
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('0001::1')}
245
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('2001:4800::64.39.2.1')}
246
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('::1.1.1.1')}
247
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('::192.168.255.0')}
248
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr(2**128-1)}
249
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('fec0:0:0:0:0:0:192.168.1.1')}
250
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_addr('8080::8080:10.10.10.10')}
251
+
252
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('') }
253
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('10.0') }
254
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('192.168.1.256') }
255
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('192..168.1.1') }
256
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('192.168.1a.255') }
257
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('192.168.1.1.1') }
258
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('ff.ff.ff.ff') }
259
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr(2**128-1, :Version => 4) }
260
+
261
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('ffff::1111::1111') }
262
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('abcd:efgh::1') }
263
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('fffff::1') }
264
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('fffg::1') }
265
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('ffff:::0::1') }
266
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('1:0:0:0:0:0:0:0:1') }
267
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('::192.168.256.0') }
268
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('::192.168.a3.0') }
269
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_addr('::192.168.1.1.0') }
270
+
271
+ assert_raise(ArgumentError){ NetAddr.validate_ip_addr({}) }
272
+ assert_raise(ArgumentError){ NetAddr.validate_ip_addr([])}
273
+ assert_raise(ArgumentError){ NetAddr.validate_ip_addr('192.168.1.0', :Version => 5)}
274
+
275
+ end
276
+
277
+
278
+ def test_validate_ip_netmask
279
+ assert_raise(ArgumentError) {NetAddr.validate_ip_netmask('255.255.255.255', :test => true)}
280
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_netmask('255.255.255.255')}
281
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_netmask('32')}
282
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_netmask('/32')}
283
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_netmask(32)}
284
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_netmask(0xffffffff, :Packed => true)}
285
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_netmask('128', :Version => 6)}
286
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_netmask('/128', :Version => 6)}
287
+ assert_nothing_raised(Exception) {NetAddr.validate_ip_netmask(128, :Version => 6)}
288
+
289
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_netmask('255.192.255.0') }
290
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_netmask(33) }
291
+ assert_raise(NetAddr::ValidationError){ NetAddr.validate_ip_netmask(129, :Version => 6) }
292
+
293
+ assert_raise(ArgumentError){ NetAddr.validate_ip_netmask({}) }
294
+ assert_raise(ArgumentError){ NetAddr.validate_ip_netmask([])}
295
+ assert_raise(ArgumentError){ NetAddr.validate_ip_netmask('/24', :Version => 5)}
296
+ end
297
+
298
+
299
+ end
300
+
301
+
302
+
303
+
@@ -0,0 +1,304 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require '../lib/net_addr.rb'
4
+ require 'test/unit'
5
+
6
+
7
+
8
+ class TestTree < Test::Unit::TestCase
9
+
10
+ def test_add
11
+ tree = NetAddr::Tree.new()
12
+
13
+ assert_nothing_raised(RuntimeError){tree.add!('192.168.1.0 255.255.255.0')}
14
+ assert_nothing_raised(RuntimeError){tree.add!('10.1.0.0/24')}
15
+ assert_nothing_raised(RuntimeError){tree.add!('10.1.0.0')}
16
+ assert_nothing_raised(RuntimeError){tree.add!('192.168.1.0/26')}
17
+ assert_nothing_raised(RuntimeError){tree.add!('fec0::/10')}
18
+ assert_nothing_raised(RuntimeError){tree.add!('fec0::/64')}
19
+
20
+ end
21
+
22
+ def test_ancestors
23
+ tree = NetAddr::Tree.new()
24
+
25
+ tree.add!('192.168.1.0/24')
26
+ tree.add!('192.168.1.64/26')
27
+ tree.add!('192.168.1.64/27')
28
+ tree.add!('192.168.1.64/28')
29
+ tree.add!('192.168.2.0/24')
30
+
31
+ ancestors = tree.ancestors('192.168.1.64/28')
32
+ assert_equal('192.168.1.64/27', ancestors[0].desc)
33
+ assert_equal('192.168.1.64/26', ancestors[1].desc)
34
+ assert_equal('192.168.1.0/24', ancestors[2].desc)
35
+
36
+ end
37
+
38
+ def test_children
39
+ tree = NetAddr::Tree.new()
40
+
41
+ tree.add!('192.168.1.0/24')
42
+ tree.add!('192.168.1.64/26')
43
+ tree.add!('192.168.1.64/27')
44
+ tree.add!('192.168.1.96/27')
45
+ tree.add!('192.168.1.64/28')
46
+ tree.add!('192.168.2.0/24')
47
+
48
+ children = tree.children('192.168.1.64/26')
49
+ assert_equal('192.168.1.64/27', children[0].desc)
50
+ assert_equal('192.168.1.96/27', children[1].desc)
51
+ end
52
+
53
+ def test_delete
54
+ tree = NetAddr::Tree.new()
55
+
56
+ tree.add!('192.168.1.0/24')
57
+ tree.add!('192.168.1.64/26')
58
+ tree.add!('192.168.1.64/27')
59
+ tree.add!('192.168.1.64/28')
60
+ tree.add!('192.168.2.0/24')
61
+
62
+ tree.delete!('192.168.1.64/27')
63
+ assert_equal('192.168.1.64/28', tree.children('192.168.1.64/26')[0].desc)
64
+ end
65
+
66
+ def test_descendants
67
+ tree = NetAddr::Tree.new()
68
+
69
+ tree.add!('192.168.1.0/24')
70
+ tree.add!('192.168.1.64/26')
71
+ tree.add!('192.168.1.64/27')
72
+ tree.add!('192.168.1.96/27')
73
+ tree.add!('192.168.1.64/28')
74
+ tree.add!('192.168.2.0/24')
75
+
76
+ descendants = tree.descendants('192.168.1.64/26')
77
+ assert_equal('192.168.1.64/27', descendants[0].desc)
78
+ assert_equal('192.168.1.64/28', descendants[1].desc)
79
+ assert_equal('192.168.1.96/27', descendants[2].desc)
80
+ end
81
+
82
+ def test_dump
83
+ tree = NetAddr::Tree.new()
84
+
85
+ tree.add!('192.168.1.0/24')
86
+ tree.add!('10.1.0.0/24')
87
+ tree.add!('192.168.1.0/26')
88
+ tree.add!('192.168.1.0/30')
89
+ tree.add!('fec0::/10')
90
+ tree.add!('fe80::/10')
91
+ tree.add!('fec0::/64')
92
+ tree.add!('fec0::/126')
93
+
94
+ dump = tree.dump()
95
+
96
+ obj0 = dump[0][:CIDR]
97
+ obj1 = dump[1][:CIDR]
98
+ obj3 = dump[3][:CIDR]
99
+ assert_equal('10.1.0.0/24', obj0.desc)
100
+ assert_equal('192.168.1.0/24', obj1.desc)
101
+ assert_equal('192.168.1.0/30', obj3.desc)
102
+
103
+ obj4 = dump[4][:CIDR]
104
+ obj5 = dump[5][:CIDR]
105
+ obj7 = dump[7][:CIDR]
106
+ assert_equal('fe80:0000:0000:0000:0000:0000:0000:0000/10', obj4.desc)
107
+ assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/10', obj5.desc)
108
+ assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/126', obj7.desc)
109
+
110
+ end
111
+
112
+ def test_exists
113
+
114
+ tree = NetAddr::Tree.new()
115
+
116
+ tree.add!('192.168.1.0/24')
117
+ tree.add!('10.1.0.0/24')
118
+ tree.add!('192.168.1.64/26')
119
+ tree.add!('10.1.0.44/30')
120
+
121
+ assert_equal(true, tree.exists?('192.168.1.0/24'))
122
+ assert_equal(true, tree.exists?('10.1.0.44/30'))
123
+ assert_equal(false, tree.exists?('10.2.0.0/24'))
124
+
125
+ end
126
+
127
+ def test_fill_in
128
+ tree = NetAddr::Tree.new()
129
+
130
+ tree.add!('192.168.1.0/24')
131
+ tree.add!('192.168.1.64/26')
132
+
133
+ tree.fill_in!('192.168.1.0/24')
134
+ children = tree.children('192.168.1.0/24')
135
+
136
+ assert_equal('192.168.1.0/26', children[0].desc)
137
+ assert_equal('192.168.1.64/26', children[1].desc)
138
+ assert_equal('192.168.1.128/25', children[2].desc)
139
+ end
140
+
141
+ def test_find
142
+ tree = NetAddr::Tree.new()
143
+
144
+ tree.add!('192.168.1.0/24')
145
+ tree.add!('10.1.0.0/24')
146
+ tree.add!('192.168.1.64/26')
147
+ tree.add!('10.1.0.44/30')
148
+ assert_equal('192.168.1.64/26', tree.longest_match('192.168.1.64/26').desc)
149
+ assert_equal('10.1.0.44/30', tree.longest_match('10.1.0.44/30').desc)
150
+ end
151
+
152
+ def test_find_space
153
+ tree = NetAddr::Tree.new()
154
+
155
+ cidr = ['192.168.1.0/24','192.168.1.0/26','192.168.1.64/26',
156
+ '192.168.1.128/26','192.168.1.192/26','192.168.1.0/27',
157
+ '192.168.1.0/28','192.168.1.16/30','192.168.1.16/29',
158
+ '192.168.1.32/27','192.168.1.24/30','192.168.1.28/30',
159
+ '192.168.1.64/27','192.168.1.25',
160
+ 'fec0::/60','fec0::/66','fec0::4000:0:0:0/66',
161
+ 'fec0::8000:0:0:0/66','fec0::c000:0:0:0/66','fec0::c000:0:0:0/67',
162
+ 'fec0::/67','fec0::2000:0:0:0/67','fec0::8000:0:0:0/67','fec0::4000:0:0:0/69']
163
+
164
+ assert_raise(ArgumentError) {tree.find_space(:test => true)}
165
+ cidr.each {|x| tree.add!(x)}
166
+ assert_equal(10, tree.find_space(:IPCount => 16).length)
167
+ end
168
+
169
+ def test_longest_match
170
+
171
+ tree = NetAddr::Tree.new()
172
+
173
+ tree.add!('192.168.1.0/24')
174
+ tree.add!('10.1.0.0/24')
175
+ tree.add!('192.168.1.64/26')
176
+ tree.add!('10.1.0.44/30')
177
+
178
+ assert_equal('192.168.1.64/26', tree.longest_match('192.168.1.65').desc)
179
+ assert_equal('10.1.0.44/30', tree.longest_match('10.1.0.46').desc)
180
+ assert_equal(nil, tree.longest_match('192.168.2.0') )
181
+
182
+ end
183
+
184
+ def test_merge_subnets
185
+ tree = NetAddr::Tree.new()
186
+
187
+ tree.add!('192.168.1.0/24')
188
+ tree.add!('192.168.1.0/26')
189
+ tree.add!('192.168.1.64/26')
190
+ tree.add!('192.168.1.192/26')
191
+
192
+ tree.merge_subnets!('192.168.1.0/24')
193
+ children = tree.children('192.168.1.0/24')
194
+
195
+ assert_equal('192.168.1.0/25', children[0].desc)
196
+ assert_equal('192.168.1.192/26', children[1].desc)
197
+ end
198
+
199
+ def test_prune
200
+ cidr4_1 = NetAddr::CIDR.create('192.168.1.0/24')
201
+ cidr4_2 = NetAddr::CIDR.create('10.1.0.0/24')
202
+ cidr4_3 = NetAddr::CIDR.create('192.168.1.0/26')
203
+ cidr4_4 = NetAddr::CIDR.create('192.168.1.0/30')
204
+ cidr4_5 = NetAddr::CIDR.create('192.168.1.64/26')
205
+ cidr4_6 = NetAddr::CIDR.create('192.168.1.128/26')
206
+ cidr4_7 = NetAddr::CIDR.create('192.168.1.192/26')
207
+
208
+ tree4 = NetAddr::Tree.new()
209
+
210
+ tree4.add!(cidr4_1)
211
+ tree4.add!(cidr4_2)
212
+ tree4.add!(cidr4_3)
213
+ tree4.add!(cidr4_4)
214
+ tree4.add!(cidr4_5)
215
+ tree4.add!(cidr4_6)
216
+ tree4.add!(cidr4_7)
217
+
218
+ tree4.prune!(cidr4_5)
219
+ dump = tree4.dump
220
+ assert_equal(7, dump.length)
221
+
222
+ tree4.prune!(cidr4_1)
223
+ dump = tree4.dump
224
+ assert_equal(2, dump.length)
225
+ end
226
+
227
+ def test_remove
228
+ cidr4_1 = NetAddr::CIDR.create('192.168.1.0/24')
229
+ cidr4_2 = NetAddr::CIDR.create('10.1.0.0/24')
230
+ cidr4_3 = NetAddr::CIDR.create('192.168.1.0/26')
231
+ cidr4_4 = NetAddr::CIDR.create('192.168.1.0/30')
232
+ cidr4_5 = NetAddr::CIDR.create('192.168.1.64/26')
233
+ cidr4_6 = NetAddr::CIDR.create('192.168.1.128/26')
234
+ cidr4_7 = NetAddr::CIDR.create('192.168.1.192/26')
235
+
236
+ tree4 = NetAddr::Tree.new()
237
+
238
+ tree4.add!(cidr4_1)
239
+ tree4.add!(cidr4_2)
240
+ tree4.add!(cidr4_3)
241
+ tree4.add!(cidr4_4)
242
+ tree4.add!(cidr4_5)
243
+ tree4.add!(cidr4_6)
244
+ tree4.add!(cidr4_7)
245
+
246
+ tree4.remove!(cidr4_5)
247
+ dump = tree4.dump
248
+ assert_equal(6, dump.length)
249
+
250
+ tree4.remove!(cidr4_1)
251
+ dump = tree4.dump
252
+ assert_equal(1, dump.length)
253
+ end
254
+
255
+ def test_root
256
+ tree = NetAddr::Tree.new()
257
+
258
+ tree.add!('192.168.1.0/24')
259
+ tree.add!('192.168.1.64/26')
260
+ tree.add!('192.168.1.64/27')
261
+ tree.add!('192.168.2.0/24')
262
+
263
+ assert_equal('192.168.1.0/24', tree.root('192.168.1.64/27').desc)
264
+ end
265
+
266
+ def test_show
267
+ cidr4_1 = NetAddr::CIDR.create('192.168.1.0/24')
268
+ cidr4_2 = NetAddr::CIDR.create('10.1.0.0/24')
269
+ cidr4_3 = NetAddr::CIDR.create('192.168.1.0/26')
270
+ cidr4_4 =NetAddr::CIDR.create('192.168.1.0/30')
271
+ cidr4_5 = NetAddr::CIDR.create('192.168.1.64/26')
272
+ cidr4_6 = NetAddr::CIDR.create('192.168.1.128/26')
273
+ cidr4_7 = NetAddr::CIDR.create('192.168.1.192/26')
274
+
275
+ tree4 = NetAddr::Tree.new()
276
+
277
+ tree4.add!(cidr4_1)
278
+ tree4.add!(cidr4_2)
279
+ tree4.add!(cidr4_3)
280
+ tree4.add!(cidr4_4)
281
+ tree4.add!(cidr4_5)
282
+ tree4.add!(cidr4_6)
283
+ tree4.add!(cidr4_7)
284
+
285
+ assert_not_nil(tree4.show())
286
+ end
287
+
288
+ def test_siblings
289
+ tree = NetAddr::Tree.new()
290
+
291
+ tree.add!('192.168.1.0/24')
292
+ tree.add!('192.168.1.0/26')
293
+ tree.add!('192.168.1.64/26')
294
+ tree.add!('192.168.1.128/26')
295
+ tree.add!('192.168.1.192/26')
296
+
297
+ assert_equal(3, tree.siblings('192.168.1.0/26').length)
298
+ end
299
+
300
+ end
301
+
302
+
303
+
304
+