bgp4r 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. data/bgp/messages/update.rb +8 -0
  2. data/bgp/nlris/label.rb +9 -1
  3. data/bgp/nlris/labeled.rb +11 -0
  4. data/bgp/nlris/nlri.rb +15 -4
  5. data/bgp/nlris/prefix.rb +11 -0
  6. data/bgp/nlris/rd.rb +13 -1
  7. data/bgp/nlris/vpn.rb +59 -10
  8. data/bgp/path_attributes/aggregator.rb +19 -0
  9. data/bgp/path_attributes/aigp.rb +63 -0
  10. data/bgp/path_attributes/as_path.rb +57 -0
  11. data/bgp/path_attributes/atomic_aggregate.rb +3 -0
  12. data/bgp/path_attributes/attribute.rb +10 -0
  13. data/bgp/path_attributes/attributes.rb +1 -0
  14. data/bgp/path_attributes/cluster_list.rb +24 -1
  15. data/bgp/path_attributes/communities.rb +32 -1
  16. data/bgp/path_attributes/extended_communities.rb +26 -0
  17. data/bgp/path_attributes/extended_community.rb +101 -16
  18. data/bgp/path_attributes/local_pref.rb +10 -0
  19. data/bgp/path_attributes/mp_reach.rb +21 -2
  20. data/bgp/path_attributes/mp_unreach.rb +5 -1
  21. data/bgp/path_attributes/multi_exit_disc.rb +10 -0
  22. data/bgp/path_attributes/next_hop.rb +20 -0
  23. data/bgp/path_attributes/origin.rb +14 -0
  24. data/bgp/path_attributes/originator_id.rb +17 -6
  25. data/bgp/path_attributes/path_attribute.rb +87 -2
  26. data/bgp/path_attributes/tlvs/tlv.rb +18 -0
  27. data/test/unit/messages/update_test.rb +79 -5
  28. data/test/unit/neighbor/neighbor_test.rb +0 -1
  29. data/test/unit/nlris/label_test.rb +6 -0
  30. data/test/unit/nlris/labeled_test.rb +12 -0
  31. data/test/unit/nlris/prefix_test.rb +5 -1
  32. data/test/unit/nlris/rd_test.rb +2 -0
  33. data/test/unit/nlris/vpn_test.rb +50 -0
  34. data/test/unit/path_attributes/aggregator_test.rb +6 -0
  35. data/test/unit/path_attributes/{aigp.rb → aigp_test.rb} +29 -0
  36. data/test/unit/path_attributes/as_path_test.rb +53 -0
  37. data/test/unit/path_attributes/atomic_aggregate_test.rb +3 -0
  38. data/test/unit/path_attributes/cluster_list_test.rb +15 -10
  39. data/test/unit/path_attributes/communities_test.rb +9 -4
  40. data/test/unit/path_attributes/extended_communities_test.rb +31 -3
  41. data/test/unit/path_attributes/extended_community_test.rb +52 -9
  42. data/test/unit/path_attributes/local_pref_test.rb +5 -0
  43. data/test/unit/path_attributes/mp_reach_test.rb +51 -4
  44. data/test/unit/path_attributes/mp_unreach_test.rb +9 -7
  45. data/test/unit/path_attributes/next_hop_test.rb +3 -0
  46. data/test/unit/path_attributes/origin_test.rb +1 -0
  47. data/test/unit/path_attributes/path_attribute_test.rb +53 -2
  48. data/test/unit/path_attributes/tlvs/tlv_test.rb +33 -0
  49. data/test/unit/path_attributes/tunnel_encapsulation_test (Jean-Michel's Laptop's conflicted copy 2011-11-02).rb +388 -0
  50. metadata +14 -4
@@ -46,6 +46,27 @@ class Update_Test < Test::Unit::TestCase
46
46
  assert_equal(Update, m.class)
47
47
  assert_equal(s, m.to_shex)
48
48
  assert_equal(9, m.path_attribute.size)
49
+
50
+ h = {
51
+ :path_attributes=> {
52
+ :origin=>:incomplete,
53
+ :as_path=>{},
54
+ :multi_exit_disc=>0,
55
+ :local_pref=>100,
56
+ :communities=>["10283:20103"],
57
+ :route_target=>[10283, 30000],
58
+ :cluster_ids=>["0.0.0.1"],
59
+ :origin_id=>"81.52.17.128",
60
+ :mp_reach=> {
61
+ :safi=>128,
62
+ :afi=>1,
63
+ :nlris=>{:label=>1851, :rd=>[3215, 1291308], :prefix=>"172.31.63.251/32"},
64
+ :nexthop=>["81.52.17.128"]
65
+ }
66
+ }
67
+ }
68
+
69
+ assert_equal(h, m.to_hash)
49
70
 
50
71
  s = 'ffffffffffffffffffffffffffffffff0050020000002f40010101400304c0a80105800404000000644005040000006440020402010064c0080c051f00010137003b0af50040200a0a0a0a2020202020'
51
72
  m = Message.factory([s].pack('H*'))
@@ -57,18 +78,27 @@ class Update_Test < Test::Unit::TestCase
57
78
  s = 'ffffffffffffffffffffffffffffffff0050020000002f40010101400304c0a80105800404000000644005040000006440020402010064c0080c051f00010137003b0af50040200a0a0a0a2020202020'
58
79
  m = Message.factory([s].pack('H*'))
59
80
  w = Update.withdrawn(m)
81
+ assert_equal({:withdrawns=>["10.10.10.10/32", "32.32.32.32/32"]}, w.to_hash)
60
82
  assert_equal(Update,w.class)
61
83
  assert_equal('200a0a0a0a2020202020', w.withdrawn.to_shex)
62
84
  end
63
85
 
64
- def __test_experiment
86
+ def test_experiment
65
87
  upd = Update.new do
66
88
  path_attribute << Local_pref.new
67
89
  path_attribute << Origin.new
68
90
  path_attribute << Next_hop.new('10.1.1.1')
69
- 10.times { |i| nlri << "10.0.#{i}.0/24"}
91
+ 5.times { |i| nlri << "10.0.#{i}.0/24"}
70
92
  end
71
- puts upd
93
+ h = {
94
+ :path_attributes=>{
95
+ :local_pref=>100,
96
+ :next_hop=>"10.1.1.1",
97
+ :origin=>:igp
98
+ },
99
+ :nlris=>["10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24", "10.0.4.0/24"]
100
+ }
101
+ assert_equal(h, upd.to_hash)
72
102
  end
73
103
 
74
104
  def test_verify_as4_byte_encoding
@@ -158,6 +188,7 @@ class Update_Test < Test::Unit::TestCase
158
188
  m = Update.new([s].pack('H*'))
159
189
  assert m.withdrawn, "Should contain withdrawn routes."
160
190
  assert_equal 3,m.withdrawn.nlris.size
191
+ assert_equal({:withdrawns=>["212.3.116.0/22", "85.198.136.0/23", "212.8.48.0/22"]}, m.to_hash)
161
192
  end
162
193
 
163
194
  def test_factory_to_create_an_update_message_witth_ext_inet_unicast_nlris
@@ -174,6 +205,7 @@ class Update_Test < Test::Unit::TestCase
174
205
  # Need to tell the factory we are dealing with a ext nlri.
175
206
  upd2 = Update.factory(upd1.encode(o), o)
176
207
  assert_equal(upd1.to_shex(o), upd2.to_shex(o))
208
+ assert_equal({:path_attributes=>{:as_path=>{:sequence=>[100]}}, :nlris=>["ID=100, 77.0.0.0/17"]}, upd1.to_hash)
177
209
  end
178
210
 
179
211
  def test_path_id_and_as4byte
@@ -202,7 +234,25 @@ class Update_Test < Test::Unit::TestCase
202
234
  upd3 = Update.factory upd1.encode(o), :as4byte=> true, :path_id=>true
203
235
  assert_equal upd1.to_shex, upd2.to_shex
204
236
  assert_equal upd2.to_shex, upd3.to_shex
205
-
237
+ h = {
238
+ :path_attributes=>{
239
+ :as_path=>{:sequence=>[100]},
240
+ :multi_exit_disc=>20,
241
+ :mp_reach=>{
242
+ :nexthop=>["10.0.0.1"],
243
+ :safi=>128,
244
+ :afi=>1,
245
+ :nlris=>[
246
+ {:label=>101,:rd=>[100, 100],:prefix=>"192.168.0.0/24",:path_id=>100},
247
+ {:label=>102,:rd=>[100, 100],:prefix=>"192.168.1.0/24",:path_id=>100},
248
+ {:label=>103,:rd=>[100, 100],:prefix=>"192.168.2.0/24",:path_id=>100}
249
+ ]
250
+ },
251
+ :local_pref=>10,
252
+ :origin=>:igp
253
+ }
254
+ }
255
+ assert_equal(h, upd3.to_hash)
206
256
  end
207
257
 
208
258
  # updates with path_id
@@ -254,7 +304,22 @@ class Update_Test < Test::Unit::TestCase
254
304
  assert upd.path_attribute.has_a_mp_reach_attr?
255
305
  assert_equal s2.split.join, upd.to_shex
256
306
  assert_equal s2.split.join, ext_update(upd.to_shex).to_shex
257
-
307
+ h = {
308
+ :path_attributes=>{
309
+ :as_path=>{},
310
+ :multi_exit_disc=>0,
311
+ :mp_reach=>{
312
+ :nexthop=>["1.1.1.1"],
313
+ :safi=>128,
314
+ :afi=>1,
315
+ :nlris=>{:label=>16000, :rd=>[19, 17], :prefix=>"10.1.1.0/24", :path_id=>1}
316
+ },
317
+ :local_pref=>1,
318
+ :origin=>:egp,
319
+ :route_target=>[4, 1]
320
+ }
321
+ }
322
+ assert_equal(h, upd.to_hash)
258
323
  end
259
324
 
260
325
  def test_factory_to_build_a_withdrawn_update_for_inet_mpls_vpn_unicast_with_path_id
@@ -268,6 +333,15 @@ class Update_Test < Test::Unit::TestCase
268
333
  mp_unreach = upd.path_attribute[:mp_unreach]
269
334
  assert_equal 1, mp_unreach.nlris[0].path_id
270
335
  assert_equal 'Label Stack=524288 (bottom) RD=1:1, ID=1, IPv4=10.1.1.0/24', mp_unreach.nlris[0].to_s
336
+ h = {
337
+ :path_attributes=>{
338
+ :mp_unreach=>{
339
+ :safi=>128, :afi=>1,
340
+ :nlris=>{:label=>524288, :rd=>[1, 1], :prefix=>"10.1.1.0/24", :path_id=>1}
341
+ }
342
+ }
343
+ }
344
+ assert_equal(h, upd.to_hash)
271
345
  end
272
346
 
273
347
  end
@@ -24,7 +24,6 @@ require "test/unit"
24
24
  require 'bgp4r'
25
25
  require 'test/helpers/server'
26
26
  include BGP
27
- include BGP
28
27
  include BGP::OPT_PARM::CAP
29
28
  include BGP::TestHelpers
30
29
 
@@ -34,6 +34,8 @@ class Label_Test < Test::Unit::TestCase
34
34
  assert_equal('000641', Label.new(100).to_shex)
35
35
  assert_equal('000643', Label.new(100,1).to_shex)
36
36
  assert_equal({:label=>100, :exp=>1},Label.new(100,1).to_hash)
37
+ assert_equal({:label=>100, :exp=>1}, Label.new(:label=>100, :exp=>1).to_hash)
38
+ assert_equal({:label=>100, :exp=>0}, Label.new(:label=>100).to_hash)
37
39
  end
38
40
  end
39
41
 
@@ -46,5 +48,9 @@ class Label_stack_Test < Test::Unit::TestCase
46
48
  assert_equal('Label stack:(empty)', Label_stack.new.to_s)
47
49
  assert_equal(ls.encode, Label_stack.new(['000640000650000661'].pack('H*')).encode)
48
50
  assert_equal("Label Stack=100,101 (bottom)", Label_stack.new(['000640000651000661'].pack('H*')).to_s)
51
+ assert_equal({:labels=>[100,101]}, Label_stack.new(['000640000651000661'].pack('H*')).to_hash)
52
+ assert_equal({:labels=>[100,101,102]}, ls.to_hash)
53
+ ls = Label_stack.new :labels=>[100,101,102]
54
+ assert_equal('000640000650000661', ls.to_shex)
49
55
  end
50
56
  end
@@ -2,6 +2,8 @@ require "test/unit"
2
2
 
3
3
  require "bgp/nlris/labeled"
4
4
  require 'bgp/nlris/prefix'
5
+ require 'bgp/nlris/vpn'
6
+ require 'bgp/nlris/rd'
5
7
 
6
8
  class TestBgpNlrisLabeled < Test::Unit::TestCase
7
9
  include BGP
@@ -12,6 +14,10 @@ class TestBgpNlrisLabeled < Test::Unit::TestCase
12
14
  assert_equal('200006410a', Labeled.new(Prefix.new('10.0.0.1/8'), 100).to_shex)
13
15
  assert_equal('380006400006510a', Labeled.new(Prefix.new('10.0.0.1/8'), 100,101).to_shex)
14
16
  assert_equal('500006400006500006610a', Labeled.new(Prefix.new('10.0.0.1/8'), 100,101,102).to_shex)
17
+ assert_equal({:prefix=>"10.0.0.0/8", :labels=>[100, 101, 102]}, Labeled.new(Prefix.new('10.0.0.1/8'), 100,101,102).to_hash)
18
+ labeled = Labeled.new(Prefix.new('10.0.0.1/8'), 100,101,102)
19
+ assert_equal(labeled.to_shex, Labeled.new(labeled.to_hash).to_shex)
20
+ assert_equal({:prefix=>"10.0.0.0/8", :labels=>[100, 101, 102]}, Labeled.new(Prefix.new('10.0.0.1/8'), 100,101,102).to_hash)
15
21
  #FIXME: add labeled and labeled vpn tests
16
22
  end
17
23
  def test_new_ntop
@@ -23,6 +29,12 @@ class TestBgpNlrisLabeled < Test::Unit::TestCase
23
29
  assert_equal('500006400006500006610a', Labeled.new_ntop(['500006400006500006610a'].pack('H*'), 1, 1).to_shex)
24
30
  #FIXME: add labeled and labeled vpn tests
25
31
  end
32
+ def test_new_afi_1_safi_128_rd_0_0
33
+ lb = Labeled.new_ntop(['580064b10000006405591311'].pack('H*'), 1, 128)
34
+ assert_equal('580064b10000006405591311', lb.to_shex)
35
+ assert_equal('Label Stack=1611 (bottom) RD=100:89723665, IPv4=0.0.0.0/0', lb.to_s)
36
+ assert_equal({:rd=>[100, 89723665], :label=>1611}, lb.to_hash)
37
+ end
26
38
  def test_new_ntop_path_id
27
39
  #FIXME: add labeled and labeled vpn tests
28
40
  end
@@ -42,6 +42,8 @@ class Prefix_Test < Test::Unit::TestCase
42
42
  assert_equal(1, pfx.afi)
43
43
  assert ! pfx.extended?
44
44
  assert_equal('IPv4=10.255.128.0/17', pfx.to_s_with_afi)
45
+ assert_equal({:prefix=>"10.255.128.0/17"}, pfx.to_hash)
46
+ assert_equal(pfx.to_hash, Prefix.new(pfx.to_hash).to_hash)
45
47
  end
46
48
  def test_extended
47
49
  pfx = Prefix.new(100, '10.0.0.0')
@@ -55,7 +57,8 @@ class Prefix_Test < Test::Unit::TestCase
55
57
  assert_equal('ID=100, 2011:13:11::/64', pfx.to_s)
56
58
  assert_equal('00000064402011001300110000', pfx.to_shex)
57
59
  assert_equal('ID=100, IPv6=2011:13:11::/64', pfx.to_s_with_afi)
58
-
60
+ assert_equal({:path_id=>100, :prefix=>"2011:13:11::/64"}, pfx.to_hash)
61
+ assert_equal(pfx.to_hash, Prefix.new(pfx.to_hash).to_hash)
59
62
  end
60
63
  def test_inet6
61
64
  pfx = Prefix.new('2011:13:11::0/64')
@@ -97,6 +100,7 @@ class Prefix_Test < Test::Unit::TestCase
97
100
  assert pfx.iso?
98
101
  assert_equal(3, pfx.afi)
99
102
  assert_equal('NSAP=49.0001.0002.0003.0000.0000.0000.0000.0000.0000.00/56', pfx.to_s_with_afi)
103
+ assert_equal(pfx.to_hash, Prefix.new(pfx.to_hash).to_hash)
100
104
  end
101
105
  def test_new_ntop_inet
102
106
  s = '110aff80'
@@ -34,6 +34,8 @@ class Rd_Test < Test::Unit::TestCase
34
34
  assert_equal('0002000000010101', Rd.new(1,'1.1.1.1').to_shex)
35
35
  assert_equal('RD=1:1 (0x01 0x0001)',Rd.new(1,1,0).to_s)
36
36
  assert_equal('RD=1:1 (0x0001 0x01)',Rd.new(1,1,2).to_s)
37
+ assert_equal('RD=1:1 (0x01 0x0001)',Rd.new(:rd=>[1,1]).to_s)
38
+ assert_equal({:rd=>[1,1]},Rd.new(:rd=>[1,1]).to_hash)
37
39
  end
38
40
  def test_2
39
41
  assert_equal('0000000100000001', Rd.new(['0000000100000001'].pack('H*')).to_shex)
@@ -0,0 +1,50 @@
1
+ #--
2
+ # Copyright 2011 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of BGP4R.
8
+ #
9
+
10
+ require "test/unit"
11
+ require 'bgp/nlris/vpn'
12
+ require 'bgp/nlris/rd'
13
+ require 'bgp/nlris/prefix'
14
+
15
+ class TestNlrisVpn < Test::Unit::TestCase
16
+ include BGP
17
+ def test_1
18
+ assert_equal('RD=1.1.1.1:1, IPv4=10.0.0.0/8', Vpn.new("10.0.0.1/8", Rd.new('1.1.1.1',1)).to_s)
19
+ assert_equal('RD=1.1.1.1:1, IPv6=2011:1::/48', Vpn.new("2011:1::1/48", Rd.new('1.1.1.1',1)).to_s)
20
+ assert_equal('4800000001000000010a', Vpn.new("10.1.2.3/8", Rd.new(1,1)).to_shex)
21
+ assert_equal('RD=1:1, IPv4=10.0.0.0/8', Vpn.new(["4800000001000000010a"].pack('H*')).to_s)
22
+ assert_equal('4900010101010100010a00', Vpn.new("10.1.2.3/9", Rd.new('1.1.1.1',1)).to_shex)
23
+ assert_equal('RD=1.1.1.1:1, IPv4=10.0.0.0/9', Vpn.new(["4900010101010100010a00"].pack('H*')).to_s)
24
+ assert_equal('4a00010101010100010a00', Vpn.new("10.1.2.3/10", Rd.new('1.1.1.1',1)).to_shex)
25
+ assert_equal('5000010101010100010a01', Vpn.new("10.1.2.3/16", Rd.new('1.1.1.1',1)).to_shex)
26
+ assert_equal('5100010101010100010a0100', Vpn.new("10.1.2.3/17", Rd.new('1.1.1.1',1)).to_shex)
27
+ assert_equal('5800010101010100010a0102', Vpn.new("10.1.2.3/24", Rd.new('1.1.1.1',1)).to_shex)
28
+ assert_equal('5900010101010100010a010200', Vpn.new("10.1.2.3/25", Rd.new('1.1.1.1',1)).to_shex)
29
+ assert_equal('6000010101010100010a010203', Vpn.new("10.1.2.3/32", Rd.new('1.1.1.1',1)).to_shex)
30
+ assert_equal('4a00010101010100010a00', Vpn.new(["4a00010101010100010a00"].pack('H*')).to_shex)
31
+ assert_equal('5000010101010100010a01', Vpn.new(["5000010101010100010a01"].pack('H*')).to_shex)
32
+ assert_equal('5100010101010100010a0100', Vpn.new(["5100010101010100010a0100"].pack('H*')).to_shex)
33
+ assert_equal('5800010101010100010a0102', Vpn.new(["5800010101010100010a0102"].pack('H*')).to_shex)
34
+ assert_equal('5900010101010100010a010200', Vpn.new(["5900010101010100010a010200"].pack('H*')).to_shex)
35
+ assert_equal('6000010101010100010a010203', Vpn.new(["6000010101010100010a010203"].pack('H*')).to_shex)
36
+ assert_equal('400000000100000001', Vpn.new(Rd.new(1,1)).to_shex)
37
+ assert_equal('400000000100000001', Vpn.new(["400000000100000001"].pack('H*')).to_shex)
38
+ assert_equal('RD=1:1, IPv4=0.0.0.0/0', Vpn.new(Rd.new(1,1)).to_s)
39
+ vpn = Vpn.new('1.2.3.4/32', Rd.new(1,1))
40
+ assert_equal('000000010000000101020304', vpn.encode_without_len_without_path_id.unpack('H*')[0])
41
+ end
42
+ def test_2
43
+ vpn = Vpn.new("10.0.0.1/8", Rd.new('1.1.1.1',1))
44
+ assert_equal("RD=1.1.1.1:1, IPv4=10.0.0.0/8", vpn.to_s)
45
+ assert_equal({:prefix=>"10.0.0.0/8", :rd=>["1.1.1.1", 1]}, vpn.to_hash)
46
+ vpn = Vpn.new("10.0.0.1/8", Rd.new(1,1))
47
+ assert_equal("RD=1:1, IPv4=10.0.0.0/8", vpn.to_s)
48
+ assert_equal({:prefix=>"10.0.0.0/8", :rd=>[1, 1]}, vpn.to_hash)
49
+ end
50
+ end
@@ -63,4 +63,10 @@ class Aggregator_Test < Test::Unit::TestCase
63
63
  assert_raise(ArgumentError) {As4_aggregator.new(ag2)}
64
64
  assert_equal(ag4.encode, ag.encode)
65
65
  end
66
+ def test5
67
+ ag1 = Aggregator.new('10.0.0.1', 100)
68
+ ag2 = Aggregator.new :asn=> 100, :address=> '10.0.0.1'
69
+ assert_equal(ag1.encode,ag2.encode)
70
+ assert_equal({:asn=>100, :address=>"10.0.0.1"}, ag2.to_hash)
71
+ end
66
72
  end
@@ -1,3 +1,32 @@
1
+ require "test/unit"
2
+ require "bgp/path_attributes/aigp"
3
+
4
+ class TestAigp < Test::Unit::TestCase
5
+ include BGP
6
+ def test_1
7
+ assert Aigp.new
8
+ assert_equal('801a0b01000b0000000000000000', Aigp.new.to_shex)
9
+ assert_equal(0, Aigp.new.to_i)
10
+ assert_equal(4294967295, Aigp.new(0xffffffff).to_i)
11
+ assert_equal('801a0b01000b00000000ffffffff', Aigp.new(0xffffffff).to_shex)
12
+ assert_equal('(0xffffffff) 4294967295', Aigp.new(0xffffffff).metric)
13
+ assert_equal(72057594037927935, Aigp.new(0xffffffffffffff).to_i)
14
+ assert_equal('801a0b01000b00ffffffffffffff', Aigp.new(0xffffffffffffff).to_shex)
15
+ assert_equal('(0xffffffffffffff) 72057594037927935', Aigp.new(0xffffffffffffff).metric)
16
+ assert_equal('801a0b01000b0001020304050607', Aigp.new(Aigp.new(0x01020304050607).encode).to_shex)
17
+ assert ! Aigp.new.is_transitive?
18
+ assert Aigp.new.is_optional?
19
+ end
20
+ def test_2
21
+ assert Aigp.new_hash
22
+ assert Aigp.new_hash :metric=> 0x1f2f3f4f5f6f7f8f
23
+ m1 = Aigp.new 0x1f2f3f4f5f6f7f8f
24
+ m2 = Aigp.new_hash :metric=> 0x1f2f3f4f5f6f7f8f
25
+ assert_equal(m1.encode, m2.encode)
26
+ assert_equal({:metric=>2247084349217275791}, m2.to_hash)
27
+ end
28
+ end
29
+
1
30
  __END__
2
31
 
3
32
  3. AIGP Attribute
@@ -63,6 +63,12 @@ class Segment_Test < Test::Unit::TestCase
63
63
  assert_equal('0203000000010000000200000003', seg.to_shex(true))
64
64
  assert_equal('0203000100020003', seg.to_shex(false))
65
65
  end
66
+ def test_5
67
+ assert_equal({:set=>[1, 2]},As_path::Set.new(1,2).to_hash)
68
+ assert_equal({:sequence=>[3,4]},As_path::Sequence.new(3,4).to_hash)
69
+ assert_equal({:confed_sequence=>[5,6]},As_path::Confed_sequence.new(5,6).to_hash)
70
+ assert_equal({:confed_set=>[7,8]},As_path::Confed_set.new(7,8).to_hash)
71
+ end
66
72
  end
67
73
 
68
74
  class As_path_Test < Test::Unit::TestCase
@@ -124,6 +130,53 @@ class As_path_Test < Test::Unit::TestCase
124
130
  asp2 = As_path.new(asp)
125
131
  assert_equal(asp.encode, asp2.encode)
126
132
  end
133
+ def test_6
134
+ set1 = As_path.new_set 1,2,3,4
135
+ set2 = As_path.new As_path.new(As_path::Set.new(1,2,3,4))
136
+ assert_equal( set1.encode, set2.encode)
137
+ set1 = As_path.new_sequence 1,2,3,4
138
+ set2 = As_path.new As_path.new(As_path::Sequence.new(1,2,3,4))
139
+ assert_equal( set1.encode, set2.encode)
140
+ set1 = As_path.new_confed_set 1,2,3,4
141
+ set2 = As_path.new As_path.new(As_path::Confed_set.new(1,2,3,4))
142
+ assert_equal( set1.encode, set2.encode)
143
+ set1 = As_path.new_confed_sequence 1,2,3,4
144
+ set2 = As_path.new As_path.new(As_path::Confed_sequence.new(1,2,3,4))
145
+ assert_equal( set1.encode, set2.encode)
146
+ end
147
+ def test_7
148
+ set1 = As_path.new_hash :set=> [1,2,3,4]
149
+ set2 = As_path.new As_path.new(As_path::Set.new(1,2,3,4))
150
+ assert_equal( set1.encode, set2.encode)
151
+ set1 = As_path.new_hash :sequence=> [1,2,3,4]
152
+ set2 = As_path.new As_path.new(As_path::Sequence.new(1,2,3,4))
153
+ assert_equal( set1.encode, set2.encode)
154
+ set1 = As_path.new_hash :confed_set=> [1,2,3,4]
155
+ set2 = As_path.new As_path.new(As_path::Confed_set.new(1,2,3,4))
156
+ assert_equal( set1.encode, set2.encode)
157
+ set1 = As_path.new_hash :confed_sequence=> [1,2,3,4]
158
+ set2 = As_path.new As_path.new(As_path::Confed_sequence.new(1,2,3,4))
159
+ assert_equal( set1.encode, set2.encode)
160
+ end
161
+ def test_8
162
+ set = As_path.new_hash :set=> [1,2], :sequence=>[3,4], :confed_sequence=>[5,6], :confed_set=>[7,8]
163
+ assert_equal('400218010200010002020200030004030200050006040200070008', set.to_shex)
164
+ assert_equal [1,2], set.to_hash[:as_path][:set]
165
+ assert_equal [3,4], set.to_hash[:as_path][:sequence]
166
+ assert_equal [5,6], set.to_hash[:as_path][:confed_sequence]
167
+ assert_equal [7,8], set.to_hash[:as_path][:confed_set]
168
+ set1 = As4_path.new
169
+ set1 << As_path::Set.new(1,2)
170
+ set1 << As_path::Sequence.new(3,4)
171
+ set1 << As_path::Confed_sequence.new(5,6)
172
+ set1 << As_path::Confed_set.new(7,8)
173
+ set2 = As4_path.new_hash :set=> [1,2], :sequence=>[3,4], :confed_sequence=>[5,6], :confed_set=>[7,8]
174
+ assert_equal(set1.to_shex, set2.to_shex)
175
+ assert_equal [1,2], set2.to_hash[:as_path][:set]
176
+ assert_equal [3,4], set2.to_hash[:as_path][:sequence]
177
+ assert_equal [5,6], set2.to_hash[:as_path][:confed_sequence]
178
+ assert_equal [7,8], set2.to_hash[:as_path][:confed_set]
179
+ end
127
180
  end
128
181
 
129
182
  class As4_path_Test < Test::Unit::TestCase
@@ -31,5 +31,8 @@ class Atomic_aggregate_Test < Test::Unit::TestCase
31
31
  obj = Atomic_aggregate.new
32
32
  assert_equal("800600", Atomic_aggregate.new(obj.encode).to_shex)
33
33
  end
34
+ def test_2
35
+ assert_equal({},Atomic_aggregate.new.to_hash)
36
+ end
34
37
  end
35
38
 
@@ -25,15 +25,20 @@ require 'test/unit'
25
25
  class Cluster_list_Test < Test::Unit::TestCase
26
26
  include BGP
27
27
  def test_1
28
- list1 = Cluster_list.new('192.168.0.1','10.0.0.1')
29
- list2 = Cluster_list.new('192.168.0.1 10.0.0.1')
30
- assert_equal('800a08c0a800010a000001', list1.to_shex)
31
- assert_equal(list1.to_shex, list2.to_shex)
32
- assert_equal('192.168.0.1 10.0.0.1',list1.cluster_list)
33
- assert_equal("[Oncr] (10) Cluster List: [800a08c0a800010a00000...] '192.168.0.1 10.0.0.1'", list1.to_s)
34
- list3 = Cluster_list.new(['800a08c0a800010a000001'].pack('H*'))
35
- assert_equal(list1.encode, list3.encode)
36
- list4 = Cluster_list.new(list3)
37
- assert_equal(list3.encode, list4.encode)
28
+ clist1 = Cluster_list.new('192.168.0.1','10.0.0.1')
29
+ clist2 = Cluster_list.new('192.168.0.1 10.0.0.1')
30
+ assert_equal('800a08c0a800010a000001', clist1.to_shex)
31
+ assert_equal(clist1.to_shex, clist2.to_shex)
32
+ assert_equal('192.168.0.1 10.0.0.1',clist1.cluster_list)
33
+ assert_equal("[Oncr] (10) Cluster List: [800a08c0a800010a00000...] '192.168.0.1 10.0.0.1'", clist1.to_s)
34
+ clist3 = Cluster_list.new(['800a08c0a800010a000001'].pack('H*'))
35
+ assert_equal(clist1.encode, clist3.encode)
36
+ clist4 = Cluster_list.new(clist3)
37
+ assert_equal(clist3.encode, clist4.encode)
38
+ end
39
+ def test_2
40
+ cids = ['1.1.1.1', '2.2.2.2', '3.3.3.3']
41
+ clist1 = Cluster_list.new_hash :cluster_ids=> cids
42
+ assert_equal( cids, clist1.to_hash[:cluster_ids])
38
43
  end
39
44
  end
@@ -26,10 +26,10 @@ require 'test/unit'
26
26
  class Community_Test < Test::Unit::TestCase
27
27
  include BGP
28
28
  def test_1
29
- assert_equal(0xFFFFFF01,Communities::Community.new(:no_export).to_i)
30
- assert_equal(0xFFFFFF02,Communities::Community.new(:no_advertise).to_i)
31
- assert_equal(0xFFFFFF03,Communities::Community.new(:no_export_sub_confed).to_i)
32
- assert_equal(0xFFFFFF04,Communities::Community.new(:no_peer).to_i)
29
+ assert_equal(0xFFFFFF01,Communities::Community.no_export.to_i)
30
+ assert_equal(0xFFFFFF02,Communities::Community.no_advertise.to_i)
31
+ assert_equal(0xFFFFFF03,Communities::Community.no_export_sub_confed.to_i)
32
+ assert_equal(0xFFFFFF04,Communities::Community.no_peer.to_i)
33
33
  assert_equal(0xdeadbeef,Communities::Community.new(0xdeadbeef).to_i)
34
34
  assert_equal '5:1234', Communities::Community.new(Communities::Community.new('5:1234')).to_s
35
35
  end
@@ -58,6 +58,11 @@ class Communities_Test < Test::Unit::TestCase
58
58
  assert_equal('145:30 145:40 145:50 145:60', com3.communities)
59
59
  assert_equal("[OTcr] (8) Communities: [c008100091001e0091002...] '145:30 145:40 145:50 145:60'", com3.to_s)
60
60
  assert_equal(com3.to_shex, Communities.new(com3.encode).to_shex)
61
+ assert_equal({:communities=>["145:30", "145:40", "145:50", "145:60"]}, com1.to_hash)
62
+ com4 = Communities.new_hash :communities=> ["145:30", "145:40", "145:50", "145:60", :no_export, :no_advertise]
63
+ assert_equal('c008180091001e00910028009100320091003cffffff01ffffff02', com4.to_shex)
64
+ com5 = Communities.new_hash(com4.to_hash)
65
+ assert_equal(com4.to_shex, com5.to_shex)
61
66
  end
62
67
  def test_2
63
68
  com1 = Communities.new("145:60", "145:10", "145:30", "145:20")
@@ -47,14 +47,38 @@ class Extended_communitiesTest < Test::Unit::TestCase
47
47
  exc = Extended_communities.new(['c010080002282b00007530'].pack('H*'))
48
48
  assert_equal(Route_target, exc.communities[0].class)
49
49
  end
50
+ def test_4
51
+ ec = Extended_communities.new_hash :color=> 100
52
+ ec2 = Extended_communities.new_hash(ec.to_hash)
53
+ assert_equal(ec.to_hash, Extended_communities.new_hash(ec.to_hash).to_hash)
54
+ ec = Extended_communities.new_hash :link_bandwidth=> 999_999_999
55
+ ec2 = Extended_communities.new_hash(ec.to_hash)
56
+ assert_equal(ec.to_hash, Extended_communities.new_hash(ec.to_hash).to_hash)
57
+ ec = Extended_communities.new_hash :route_target=> ['10.0.1.2', 10]
58
+ ec2 = Extended_communities.new_hash(ec.to_hash)
59
+ assert_equal(ec.to_hash, Extended_communities.new_hash(ec.to_hash).to_hash)
60
+ ec = Extended_communities.new_hash :ospf_domain_id=> '9.1.0.1'
61
+ ec2 = Extended_communities.new_hash(ec.to_hash)
62
+ assert_equal(ec.to_hash, Extended_communities.new_hash(ec.to_hash).to_hash)
63
+ ec = Extended_communities.new_hash :encapsulation=> :l2tpv3
64
+ ec2 = Extended_communities.new_hash(ec.to_hash)
65
+ assert_equal(ec.to_hash, Extended_communities.new_hash(ec.to_hash).to_hash)
66
+ ec = Extended_communities.new_hash :route_origin=> ['10.0.1.2', 10]
67
+ ec2 = Extended_communities.new_hash(ec.to_hash)
68
+ assert_equal(ec.to_hash, Extended_communities.new_hash(ec.to_hash).to_hash)
69
+ ec = Extended_communities.new_hash :color => 100, :link_bandwidth => 999_999_999, :route_origin => ['10.0.1.2', 10], :encapsulation => :ipip
70
+ assert_equal(100, ec.to_hash[:color])
71
+ end
50
72
  def test_sort
51
73
  ec = Extended_communities.new
74
+ ec.add(Color.new(100))
52
75
  ec.add(Link_bandwidth.new(999_999_999))
53
76
  ec.add(Route_target.new('10.0.1.2',10))
54
77
  ec.add(Ospf_domain_id.new('9.1.0.1'))
55
78
  ec.add(Route_target.new('11.0.1.1',10))
56
79
  ec.add(Route_target.new('8.0.1.1',10))
57
80
  ec.add(Route_target.new('7.0.1.1',8))
81
+ ec.add(Encapsulation.new(:l2tpv3))
58
82
  ec.add(Ospf_domain_id.new('20.0.0.1'))
59
83
  ec.add(Route_origin.new('10.0.3.2',9))
60
84
  ec.add(Route_target.new('10.0.3.2',7))
@@ -68,13 +92,16 @@ class Extended_communitiesTest < Test::Unit::TestCase
68
92
  assert_equal("Ospf domain id: 9.1.0.1:0",ec.sort.communities[6].to_s)
69
93
  assert_equal("Ospf domain id: 20.0.0.1:0",ec.sort.communities[7].to_s)
70
94
  assert_equal("Ospf router id: 10.0.0.1:0",ec.sort.communities[8].to_s)
71
- assert_equal("Link bandwidth: 1000000000.0",ec.sort.communities[9].to_s)
95
+ assert_equal("Color: 100",ec.sort.communities[9].to_s)
96
+ assert_equal("Encapsulation: 1",ec.sort.communities[10].to_s)
97
+ assert_equal("Link bandwidth: 1000000000.0",ec.sort.communities[11].to_s)
72
98
  end
73
99
  def test_sort!
74
100
  ec = Extended_communities.new
75
101
  ec.add(Link_bandwidth.new(999_999_999))
76
102
  ec.add(Route_target.new('10.0.1.2',10))
77
103
  ec.add(Ospf_domain_id.new('9.1.0.1'))
104
+ ec.add(Color.new(100))
78
105
  ec.add(Route_target.new('11.0.1.1',10))
79
106
  ec.add(Route_target.new('8.0.1.1',10))
80
107
  ec.add(Route_target.new('7.0.1.1',8))
@@ -92,11 +119,11 @@ class Extended_communitiesTest < Test::Unit::TestCase
92
119
  assert_equal("Ospf domain id: 9.1.0.1:0",ec.communities[6].to_s)
93
120
  assert_equal("Ospf domain id: 20.0.0.1:0",ec.communities[7].to_s)
94
121
  assert_equal("Ospf router id: 10.0.0.1:0",ec.communities[8].to_s)
95
- assert_equal("Link bandwidth: 1000000000.0",ec.communities[9].to_s)
122
+ assert_equal("Link bandwidth: 1000000000.0",ec.communities[10].to_s)
123
+ assert_equal("Color: 100",ec.communities[9].to_s)
96
124
  ec1 = Extended_communities.new(ec)
97
125
  assert_equal(ec.encode, ec1.encode)
98
126
  end
99
-
100
127
  def test_compare_eql?
101
128
  ec = Extended_communities.new
102
129
  ec.add(Route_target.new('10.0.1.2',100))
@@ -110,6 +137,7 @@ class Extended_communitiesTest < Test::Unit::TestCase
110
137
  assert_equal(0, ec <=> ec2)
111
138
  assert(! ec.eql?(ec2))
112
139
  end
140
+
113
141
  end
114
142
 
115
143
  #MiniTest::Unit.autorun
@@ -35,20 +35,20 @@ class Extended_community_Test < Test::Unit::TestCase
35
35
  def test_to_i
36
36
  assert_equal("000200ff00deadbe",Route_target.new(0xff,0xdeadbe).to_shex)
37
37
  assert_equal(0x200ff00deadbe,Route_target.new(0xff,0xdeadbe).to_i)
38
- assert_equal('030a0a0000010064', Opaque.new(10,"0a0000010064").to_shex)
39
- assert_equal(0x030a0a0000010064, Opaque.new(10,"0a0000010064").to_i)
38
+ assert_equal('030a0a0000010064', Opaque.new(0,10,"0a0000010064").to_shex)
39
+ assert_equal(0x030a0a0000010064, Opaque.new(0,10,"0a0000010064").to_i)
40
40
  end
41
41
 
42
42
  def test_compare
43
43
  rt = Route_target.new(0xff,0xdeadbe)
44
- op = Opaque.new(10,"0a0000010064")
44
+ op = Opaque.new(ATTR::XTENDED_COMMUNITY::NON_TRANSITIVE, 10,"0a0000010064")
45
45
  assert( rt < op )
46
46
  end
47
47
 
48
48
  def test_opaque
49
- ec = Opaque.new(10,"0a0000010064")
49
+ ec = Opaque.new(ATTR::XTENDED_COMMUNITY::NON_TRANSITIVE, 10,"0a0000010064")
50
50
  ec.instance_of?(Extended_community)
51
- assert_equal('030a0a0000010064', ec.to_shex)
51
+ assert_equal('430a0a0000010064', ec.to_shex)
52
52
  assert_equal('Opaque: 0a0000010064', ec.to_s)
53
53
  end
54
54
 
@@ -68,6 +68,7 @@ class Extended_community_Test < Test::Unit::TestCase
68
68
  assert_equal("Route origin: 2.2.2.2:333",Route_origin.new('2.2.2.2', 333).to_s)
69
69
  assert_equal("Route origin: 10:20",Route_origin.new(10, 20).to_s)
70
70
  assert_equal("000300ff00deadbe",Route_origin.new(0xff,0xdeadbe).to_shex)
71
+
71
72
  end
72
73
 
73
74
  def test_ospf_domain_id
@@ -78,8 +79,8 @@ class Extended_community_Test < Test::Unit::TestCase
78
79
  end
79
80
 
80
81
  def test_ospf_router_id
81
- did = Ospf_router_id.new('1.1.1.1')
82
- assert_equal('Ospf router id: 1.1.1.1:0', did.to_s)
82
+ rid = Ospf_router_id.new('1.1.1.1')
83
+ assert_equal('Ospf router id: 1.1.1.1:0', rid.to_s)
83
84
  assert_equal("0107010203040000",Ospf_router_id.new('1.2.3.4').to_shex)
84
85
  assert_equal("Ospf router id: 1.2.3.4:0", Ospf_router_id.new(['0105010203040000'].pack('H*')).to_s)
85
86
  end
@@ -91,12 +92,54 @@ class Extended_community_Test < Test::Unit::TestCase
91
92
  end
92
93
 
93
94
  def test_link_bandwidth
94
- assert_equal('04040000461c4000', Link_bandwidth.new(10_000).to_shex)
95
+ assert_equal('40040000461c4000', Link_bandwidth.new(10_000).to_shex)
95
96
  assert_equal('Link bandwidth: 10000.0', Link_bandwidth.new(10_000).to_s)
96
97
  assert_equal('Link bandwidth: 10000.0', Link_bandwidth.new(['04040000461c4000'].pack('H*')).to_s)
97
- assert_equal('040400004cbebc20', Link_bandwidth.new(99_999_999).to_shex)
98
+ assert_equal('400400004cbebc20', Link_bandwidth.new(99_999_999).to_shex)
98
99
  assert_equal('Link bandwidth: 100000000.0', Link_bandwidth.new(99_999_999).to_s)
99
100
  assert_equal('Link bandwidth: 100000000.0', Link_bandwidth.new(['040400004cbebc20'].pack('H*')).to_s)
100
101
  end
101
102
 
103
+ def test_color
104
+ assert_equal('030b000000000000', Color.new.to_shex)
105
+ assert_equal('030b000000000000', Color.new(0).to_shex)
106
+ assert_equal('030b000000000064', Color.new(100).to_shex)
107
+ assert_equal('Color: 100', Color.new(100).to_s)
108
+ assert_equal('Color: 10000', Color.new(Color.new(10_000).encode).to_s)
109
+ end
110
+
111
+ def test_encapsulation
112
+ assert_equal('030c000000000000', Encapsulation.new.to_shex)
113
+ assert_equal('030c000000000001', Encapsulation.new(1).to_shex)
114
+ assert_equal('030c000000000002', Encapsulation.new(2).to_shex)
115
+ assert_equal('030c000000000007', Encapsulation.new(7).to_shex)
116
+ assert_equal('030c000000000001', Encapsulation.new(:l2tpv3).to_shex)
117
+ assert_equal('030c000000000002', Encapsulation.new(:gre).to_shex)
118
+ assert_equal('030c000000000007', Encapsulation.new(:ipip).to_shex)
119
+ assert_equal('Encapsulation: 1', Encapsulation.new(:l2tpv3).to_s)
120
+ assert_equal('Encapsulation: 7', Encapsulation.new(:ipip).to_s)
121
+ assert_equal('Encapsulation: 2', Encapsulation.new(:gre).to_s)
122
+ assert_equal('Encapsulation: 7', Encapsulation.new(Encapsulation.new(7).encode).to_s)
123
+ end
124
+
125
+ def test_factory
126
+ s = '0102070001010008 010208000101000a 01020a000102000a 01020a0003020007 01020b000101000a
127
+ 01030a0003020009 0105090100010000 0105140000010000 01070a0000010000 400400004e6e6b28
128
+ 430b000000000064 030c000000000007'.split.join
129
+ comms = []
130
+ communities = s.scan(/[0-9a-f]{16}/) { |comm| comms << Extended_community.factory([comm].pack('H*')) }
131
+ assert_equal BGP::Route_target, comms[0].class
132
+ assert_equal BGP::Route_target, comms[1].class
133
+ assert_equal BGP::Route_target, comms[2].class
134
+ assert_equal BGP::Route_target, comms[3].class
135
+ assert_equal BGP::Route_target, comms[4].class
136
+ assert_equal BGP::Route_origin, comms[5].class
137
+ assert_equal BGP::Ospf_domain_id, comms[6].class
138
+ assert_equal BGP::Ospf_domain_id, comms[7].class
139
+ assert_equal BGP::Ospf_router_id, comms[8].class
140
+ assert_equal BGP::Link_bandwidth, comms[9].class
141
+ assert_equal BGP::Color, comms[10].class
142
+ assert_equal BGP::Encapsulation, comms[11].class
143
+ end
144
+
102
145
  end
@@ -40,4 +40,9 @@ class Local_pref_Test < Test::Unit::TestCase
40
40
  lp1 = Local_pref.new(lp)
41
41
  assert_equal(lp.encode, lp1.encode)
42
42
  end
43
+ def test_3
44
+ lp = Local_pref.new_hash :local_pref=>100
45
+ assert_equal({:local_pref=>100}, lp.to_hash)
46
+ assert_equal(lp.to_hash, Local_pref.new_hash(lp.to_hash).to_hash)
47
+ end
43
48
  end