bgp4r 0.0.3

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 (55) hide show
  1. data/COPYING +674 -0
  2. data/LICENSE.txt +53 -0
  3. data/README.rdoc +259 -0
  4. data/bgp/aggregator.rb +104 -0
  5. data/bgp/as_path.rb +223 -0
  6. data/bgp/atomic_aggregate.rb +42 -0
  7. data/bgp/attribute.rb +181 -0
  8. data/bgp/attributes.rb +34 -0
  9. data/bgp/cluster_list.rb +117 -0
  10. data/bgp/common.rb +204 -0
  11. data/bgp/communities.rb +139 -0
  12. data/bgp/extended_communities.rb +107 -0
  13. data/bgp/extended_community.rb +254 -0
  14. data/bgp/iana.rb +269 -0
  15. data/bgp/io.rb +116 -0
  16. data/bgp/label.rb +94 -0
  17. data/bgp/local_pref.rb +75 -0
  18. data/bgp/message.rb +894 -0
  19. data/bgp/mp_reach.rb +208 -0
  20. data/bgp/multi_exit_disc.rb +76 -0
  21. data/bgp/neighbor.rb +291 -0
  22. data/bgp/next_hop.rb +63 -0
  23. data/bgp/nlri.rb +303 -0
  24. data/bgp/orf.rb +88 -0
  25. data/bgp/origin.rb +88 -0
  26. data/bgp/originator_id.rb +73 -0
  27. data/bgp/path_attribute.rb +210 -0
  28. data/bgp/prefix_orf.rb +263 -0
  29. data/bgp/rd.rb +107 -0
  30. data/examples/bgp +65 -0
  31. data/examples/routegen +85 -0
  32. data/examples/routegen.yml +50 -0
  33. data/test/aggregator_test.rb +66 -0
  34. data/test/as_path_test.rb +149 -0
  35. data/test/atomic_aggregate_test.rb +35 -0
  36. data/test/attribute_test.rb +57 -0
  37. data/test/cluster_list_test.rb +39 -0
  38. data/test/common_test.rb +68 -0
  39. data/test/communities_test.rb +75 -0
  40. data/test/extended_communities_test.rb +111 -0
  41. data/test/extended_community_test.rb +93 -0
  42. data/test/label_test.rb +50 -0
  43. data/test/local_pref_test.rb +43 -0
  44. data/test/message_test.rb +294 -0
  45. data/test/mp_reach_test.rb +143 -0
  46. data/test/multi_exit_disc_test.rb +46 -0
  47. data/test/neighbor_test.rb +50 -0
  48. data/test/next_hop_test.rb +37 -0
  49. data/test/nlri_test.rb +189 -0
  50. data/test/origin_test.rb +57 -0
  51. data/test/originator_id_test.rb +38 -0
  52. data/test/path_attribute_test.rb +127 -0
  53. data/test/prefix_orf_test.rb +97 -0
  54. data/test/rd_test.rb +44 -0
  55. metadata +133 -0
@@ -0,0 +1,111 @@
1
+ #--
2
+ # Copyright 2008, 2009 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of BGP4R.
8
+ #
9
+ # BGP4R is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # BGP4R is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with BGP4R. If not, see <http://www.gnu.org/licenses/>.
21
+ #++
22
+
23
+ require 'bgp/extended_communities'
24
+ require 'test/unit'
25
+ class Extended_communitiesTest < Test::Unit::TestCase
26
+ include BGP
27
+
28
+ def test1
29
+ ec = Extended_communities.new
30
+ ec.add(Route_target.new('10.0.0.1',100))
31
+ assert_equal("c0100801020a0000010064", ec.to_shex)
32
+ ec.add(Route_origin.new('10.0.0.1',200))
33
+ assert_equal("c0101001020a000001006401030a00000100c8", ec.to_shex)
34
+ ec.add(Ospf_domain_id.new('10.0.0.1'))
35
+ assert_match(/Route target: 10.0.0.1:100/m, ec.to_s)
36
+ assert_match(/Route origin: 10.0.0.1:200/m, ec.to_s)
37
+ assert_match(/Ospf domain id: 10.0.0.1:0/m, ec.to_s)
38
+ ec1 = Extended_communities.new(ec)
39
+ assert_equal(ec.encode, ec1.encode)
40
+ end
41
+ def test2
42
+ ec = Extended_communities.new
43
+ (100..140).each { |g| ec.add(Route_target.new(g,100)) }
44
+ assert_equal("d", ec.to_shex.slice(0,1))
45
+ end
46
+ def test_3
47
+ exc = Extended_communities.new(['c010080002282b00007530'].pack('H*'))
48
+ assert_equal(Route_target, exc.communities[0].class)
49
+ end
50
+ def test_sort
51
+ ec = Extended_communities.new
52
+ ec.add(Route_target.new('10.0.1.2',10))
53
+ ec.add(Ospf_domain_id.new('9.1.0.1'))
54
+ ec.add(Route_target.new('11.0.1.1',10))
55
+ ec.add(Route_target.new('8.0.1.1',10))
56
+ ec.add(Route_target.new('7.0.1.1',8))
57
+ ec.add(Ospf_domain_id.new('20.0.0.1'))
58
+ ec.add(Route_origin.new('10.0.3.2',9))
59
+ ec.add(Route_target.new('10.0.3.2',7))
60
+ ec.add(Ospf_router_id.new('10.0.0.1'))
61
+ assert_equal("Route target: 7.0.1.1:8",ec.sort.communities[0].to_s)
62
+ assert_equal("Route target: 8.0.1.1:10",ec.sort.communities[1].to_s)
63
+ assert_equal("Route target: 10.0.1.2:10",ec.sort.communities[2].to_s)
64
+ assert_equal("Route target: 10.0.3.2:7",ec.sort.communities[3].to_s)
65
+ assert_equal("Route target: 11.0.1.1:10",ec.sort.communities[4].to_s)
66
+ assert_equal("Route origin: 10.0.3.2:9",ec.sort.communities[5].to_s)
67
+ assert_equal("Ospf domain id: 9.1.0.1:0",ec.sort.communities[6].to_s)
68
+ assert_equal("Ospf domain id: 20.0.0.1:0",ec.sort.communities[7].to_s)
69
+ assert_equal("Ospf router id: 10.0.0.1:0",ec.sort.communities[8].to_s)
70
+ end
71
+ def test_sort!
72
+ ec = Extended_communities.new
73
+ ec.add(Route_target.new('10.0.1.2',10))
74
+ ec.add(Ospf_domain_id.new('9.1.0.1'))
75
+ ec.add(Route_target.new('11.0.1.1',10))
76
+ ec.add(Route_target.new('8.0.1.1',10))
77
+ ec.add(Route_target.new('7.0.1.1',8))
78
+ ec.add(Ospf_domain_id.new('20.0.0.1'))
79
+ ec.add(Route_origin.new('10.0.3.2',9))
80
+ ec.add(Route_target.new('10.0.3.2',7))
81
+ ec.add(Ospf_router_id.new('10.0.0.1'))
82
+ ec.sort!
83
+ assert_equal("Route target: 7.0.1.1:8",ec.communities[0].to_s)
84
+ assert_equal("Route target: 8.0.1.1:10",ec.communities[1].to_s)
85
+ assert_equal("Route target: 10.0.1.2:10",ec.communities[2].to_s)
86
+ assert_equal("Route target: 10.0.3.2:7",ec.communities[3].to_s)
87
+ assert_equal("Route target: 11.0.1.1:10",ec.communities[4].to_s)
88
+ assert_equal("Route origin: 10.0.3.2:9",ec.communities[5].to_s)
89
+ assert_equal("Ospf domain id: 9.1.0.1:0",ec.communities[6].to_s)
90
+ assert_equal("Ospf domain id: 20.0.0.1:0",ec.communities[7].to_s)
91
+ assert_equal("Ospf router id: 10.0.0.1:0",ec.communities[8].to_s)
92
+ ec1 = Extended_communities.new(ec)
93
+ assert_equal(ec.encode, ec1.encode)
94
+ end
95
+
96
+ def test_compare_eql?
97
+ ec = Extended_communities.new
98
+ ec.add(Route_target.new('10.0.1.2',100))
99
+ ec.add(Ospf_domain_id.new('10.0.0.1'))
100
+ ec.add(Route_target.new('10.0.1.1',100))
101
+ ec.add(Ospf_domain_id.new('10.0.0.1'))
102
+ ec.add(Route_origin.new('10.0.1.1',99))
103
+ ec.add(Route_target.new('10.0.1.1',99))
104
+ ec.add(Ospf_router_id.new('10.0.0.1'))
105
+ ec2 = ec.sort
106
+ assert_equal(0, ec <=> ec2)
107
+ assert(! ec.eql?(ec2))
108
+ end
109
+ end
110
+
111
+ #MiniTest::Unit.autorun
@@ -0,0 +1,93 @@
1
+ #--
2
+ # Copyright 2008, 2009 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of BGP4R.
8
+ #
9
+ # BGP4R is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # BGP4R is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with BGP4R. If not, see <http://www.gnu.org/licenses/>.
21
+ #++
22
+
23
+ require 'bgp/extended_community'
24
+ require 'test/unit'
25
+ class Extended_community_Test < Test::Unit::TestCase
26
+ include BGP
27
+ def test_community
28
+ ec = Extended_community.new
29
+ assert_equal('Extended community: 0:0', ec.to_s)
30
+ assert_equal('0000000000000000',ec.to_shex)
31
+ assert_equal('Extended community: 0:0', ec.to_s)
32
+ assert_raise(ArgumentError) { Extended_community.new(0,2, '192.168.0.1',1) }
33
+ end
34
+
35
+ def test_to_i
36
+ assert_equal("000200ff00deadbe",Route_target.new(0xff,0xdeadbe).to_shex)
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)
40
+ end
41
+
42
+ def test_compare
43
+ rt = Route_target.new(0xff,0xdeadbe)
44
+ op = Opaque.new(10,"0a0000010064")
45
+ assert( rt < op )
46
+ end
47
+
48
+ def test_opaque
49
+ ec = Opaque.new(10,"0a0000010064")
50
+ ec.instance_of?(Extended_community)
51
+ assert_equal('030a0a0000010064', ec.to_shex)
52
+ assert_equal('Opaque: 0a0000010064', ec.to_s)
53
+ end
54
+
55
+ def test_route_target
56
+ assert_raise(ArgumentError) { Route_target.new }
57
+ assert_raise(ArgumentError) { Route_target.new('172.21.0.0') }
58
+ assert_equal("0102010101010064",Route_target.new('1.1.1.1', 100).to_shex)
59
+ assert_equal("Route target: 2.2.2.2:333",Route_target.new('2.2.2.2', 333).to_s)
60
+ assert_equal("Route target: 10:20",Route_target.new(10, 20).to_s)
61
+ assert_equal("000200ff00deadbe",Route_target.new(0xff,0xdeadbe).to_shex)
62
+ end
63
+
64
+ def test_route_origin
65
+ assert_raise(ArgumentError) { Route_origin.new }
66
+ assert_raise(ArgumentError) { Route_origin.new('172.21.0.0') }
67
+ assert_equal("0103010101010064",Route_origin.new('1.1.1.1', 100).to_shex)
68
+ assert_equal("Route origin: 2.2.2.2:333",Route_origin.new('2.2.2.2', 333).to_s)
69
+ assert_equal("Route origin: 10:20",Route_origin.new(10, 20).to_s)
70
+ assert_equal("000300ff00deadbe",Route_origin.new(0xff,0xdeadbe).to_shex)
71
+ end
72
+
73
+ def test_ospf_domain_id
74
+ did = Ospf_domain_id.new('1.1.1.1')
75
+ assert_equal('Ospf domain id: 1.1.1.1:0', did.to_s)
76
+ assert_equal("0105010203040000",Ospf_domain_id.new('1.2.3.4').to_shex)
77
+ assert_equal("Ospf domain id: 1.2.3.4:0", Ospf_domain_id.new(['0105010203040000'].pack('H*')).to_s)
78
+ end
79
+
80
+ 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)
83
+ assert_equal("0107010203040000",Ospf_router_id.new('1.2.3.4').to_shex)
84
+ assert_equal("Ospf router id: 1.2.3.4:0", Ospf_router_id.new(['0105010203040000'].pack('H*')).to_s)
85
+ end
86
+
87
+ def test_bgp_data_collect
88
+ assert_equal("0008006400000064",Bgp_data_collect.new(100,100).to_shex)
89
+ assert_equal("Bgp data collect: 100:100",Bgp_data_collect.new(100,100).to_s)
90
+ assert_equal("Bgp data collect: 100:100", Bgp_data_collect.new(['0008006400000064'].pack('H*')).to_s)
91
+ end
92
+
93
+ end
@@ -0,0 +1,50 @@
1
+ #--
2
+ # Copyright 2008, 2009 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of BGP4R.
8
+ #
9
+ # BGP4R is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # BGP4R is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with BGP4R. If not, see <http://www.gnu.org/licenses/>.
21
+ #++
22
+
23
+ require 'bgp/label'
24
+ require 'test/unit'
25
+
26
+ class Label_Test < Test::Unit::TestCase
27
+ include BGP
28
+ def test_1
29
+ l1 = Label.new(:label=>100, :exp=>1)
30
+ l2 = Label.new(100,1)
31
+ l3 = Label.new(l2.encode)
32
+ assert_equal(l1.to_shex, l2.to_shex)
33
+ assert_equal(l1.to_shex, l3.to_shex)
34
+ assert_equal('000641', Label.new(100).to_shex)
35
+ assert_equal('000643', Label.new(100,1).to_shex)
36
+ assert_equal({:label=>100, :exp=>1},Label.new(100,1).to_hash)
37
+ end
38
+ end
39
+
40
+ class Label_stack_Test < Test::Unit::TestCase
41
+ include BGP
42
+ def test_1
43
+ ls = Label_stack.new(100,101,102)
44
+ assert_equal('000640000650000661', ls.to_shex)
45
+ assert_equal('Label Stack=100,101,102 (bottom)', ls.to_s)
46
+ assert_equal('Label stack:(empty)', Label_stack.new.to_s)
47
+ assert_equal(ls.encode, Label_stack.new(['000640000650000661'].pack('H*')).encode)
48
+ assert_equal("Label Stack=100,101 (bottom)", Label_stack.new(['000640000651000661'].pack('H*')).to_s)
49
+ end
50
+ end
@@ -0,0 +1,43 @@
1
+ #--
2
+ # Copyright 2008, 2009 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of BGP4R.
8
+ #
9
+ # BGP4R is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # BGP4R is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with BGP4R. If not, see <http://www.gnu.org/licenses/>.
21
+ #++
22
+
23
+ require 'bgp/local_pref'
24
+ require 'test/unit'
25
+ class Local_pref_Test < Test::Unit::TestCase
26
+ include BGP
27
+ def test_1
28
+ assert_equal("40050400000064", Local_pref.new.to_shex)
29
+ assert_equal("40050400000064", Local_pref.new(100).to_shex)
30
+ assert_equal("400504000000c8", Local_pref.new(200).to_shex)
31
+ assert_equal('40050400000064', Local_pref.new(:local_pref=>100).to_shex)
32
+ assert_raise(ArgumentError) { Local_pref.new({})}
33
+ end
34
+ def test_2
35
+ lp = Local_pref.new(200)
36
+ assert_equal("400504000000c8", Local_pref.new(lp.encode).to_shex)
37
+ assert_equal("(0x00c8) 200", Local_pref.new(200).local_pref)
38
+ assert_equal("[wTcr] (5) Local Pref: [400504000000c8] '(0x00c8) 200'", Local_pref.new(200).to_s)
39
+ assert_equal(200, Local_pref.new(200).to_i)
40
+ lp1 = Local_pref.new(lp)
41
+ assert_equal(lp.encode, lp1.encode)
42
+ end
43
+ end
@@ -0,0 +1,294 @@
1
+ #--
2
+ # Copyright 2008, 2009 Jean-Michel Esnault.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #
6
+ #
7
+ # This file is part of BGP4R.
8
+ #
9
+ # BGP4R is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # BGP4R is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with BGP4R. If not, see <http://www.gnu.org/licenses/>.
21
+ #++
22
+
23
+ require 'test/unit'
24
+ require 'bgp/common'
25
+ require 'bgp/message'
26
+ require 'bgp/prefix_orf'
27
+
28
+ class Message_Test < Test::Unit::TestCase
29
+ include BGP
30
+ class MyMessage < Message
31
+ attr_reader :data
32
+ def initialize(s=nil)
33
+ @data= parse(s) if s
34
+ end
35
+ def encode
36
+ @msg_type=0xee
37
+ super('abcdefghihjklmopqrstuvwxyz')
38
+ end
39
+ end
40
+ def test_1
41
+ msg1 = MyMessage.new
42
+ msg2 = MyMessage.new(msg1.encode)
43
+ assert_equal('abcdefghihjklmopqrstuvwxyz',msg2.data)
44
+ end
45
+ end
46
+
47
+ class Keepalive_Test < Test::Unit::TestCase
48
+ include BGP
49
+ def test_1
50
+ Keepalive.new
51
+ assert_equal('ffffffffffffffffffffffffffffffff001304',Keepalive.new.to_shex)
52
+ assert_equal('ffffffffffffffffffffffffffffffff001304',Message.keepalive.unpack('H*')[0])
53
+ assert_equal(Keepalive, Message.factory(Keepalive.new.encode).class)
54
+ end
55
+ end
56
+
57
+ class Update_Test < Test::Unit::TestCase
58
+ include BGP
59
+ def test_1
60
+ s = "ffffffffffffffffffffffffffffffff006b0200000054400101024002009004040000000040050400000064c0100800020c8f04127ee8800a0800000001000000048009045134134d800e1f0001800c00000000000000005134134d00680114d100000c8f041291480a39"
61
+ sbin = [s].pack('H*')
62
+ u = Update.new(sbin)
63
+ assert_equal(Update, Message.factory(sbin).class)
64
+ #TODO: more tests with nrli, with withdrawns....
65
+ end
66
+ def test_2
67
+ s = "
68
+ ffff ffff ffff ffff
69
+ ffff ffff ffff ffff 0070 0200 0000 5940
70
+ 0101 0240 0200 8004 0400 0000 0040 0504
71
+ 0000 0064 c008 0428 2b4e 87c0 1008 0002
72
+ 282b 0000 7530 800a 0400 0000 0180 0904
73
+ 5134 1180 800e 2100 0180 0c00 0000 0000
74
+ 0000 0051 3411 8000 7800 73b1 0000 0c8f
75
+ 0013 b42c ac1f 3ffb
76
+ ".split.join
77
+ m = Message.factory([s].pack('H*'))
78
+ assert_equal(Update, m.class)
79
+ assert_equal(s, m.to_shex)
80
+ assert_equal(9, m.path_attribute.size)
81
+
82
+ s = 'ffffffffffffffffffffffffffffffff0050020000002f40010101400304c0a80105800404000000644005040000006440020402010064c0080c051f00010137003b0af50040200a0a0a0a2020202020'
83
+ m = Message.factory([s].pack('H*'))
84
+ assert_equal(Update, m.class)
85
+ assert_equal(2,m.nlri.size)
86
+ assert_equal("10.10.10.10/32\n32.32.32.32/32", m.nlri.to_s)
87
+ end
88
+ def test_3
89
+ s = 'ffffffffffffffffffffffffffffffff0050020000002f40010101400304c0a80105800404000000644005040000006440020402010064c0080c051f00010137003b0af50040200a0a0a0a2020202020'
90
+ m = Message.factory([s].pack('H*'))
91
+ w = Update.withdrawn(m)
92
+ assert_equal(Update,w.class)
93
+ assert_equal('000a200a0a0a0a2020202020', w.withdrawn.to_shex)
94
+ end
95
+ def test_4
96
+ an_update = Update.new(
97
+ Path_attribute.new(
98
+ Origin.new(1),
99
+ Next_hop.new('192.168.1.5'),
100
+ Multi_exit_disc.new(100),
101
+ Local_pref.new(100),
102
+ As_path.new(100,200,300),
103
+ Communities.new('1311:1 311:59 2805:64')
104
+ ),
105
+ Nlri.new('77.0.0.0/17', '78.0.0.0/18', '79.0.0.0/19')
106
+ )
107
+ # Ship it!
108
+ assert_equal(3*2, an_update.encode4.size - an_update.encode.size)
109
+ end
110
+
111
+ end
112
+
113
+ class Open_Test < Test::Unit::TestCase
114
+ include BGP
115
+ include BGP::OPT_PARM
116
+ def test_1
117
+ s = "ffffffffffffffffffffffffffffffff001d0104006400c80a00000100"
118
+ sbin = [s].pack('H*')
119
+ assert_equal(Open, Message.factory(sbin).class)
120
+ open = Message.factory(sbin)
121
+ assert_equal(s, open.to_shex)
122
+ assert_equal(s, Open.new(4,100, 200, '10.0.0.1').to_shex)
123
+ assert_equal(s, Open.new(4,100, 200, '10.0.0.1', []).to_shex)
124
+ assert_equal('00290104006400c80a0000010c020641040000006402020200', Open.new(4,100, 200, '10.0.0.1', As4_cap.new(100), Route_refresh_cap.new).to_shex[32..-1])
125
+ open1 = Open.new(4,100, 200, '10.0.0.1', As4_cap.new(100), Route_refresh_cap.new)
126
+ open2 = Open.new(open1.encode)
127
+ assert_equal('00290104006400c80a0000010c020641040000006402020200', open2.to_shex[32..-1])
128
+ open = Open.new(4,100, 200, '10.0.0.1')
129
+ open << As4_cap.new(100)
130
+ open << Route_refresh_cap.new
131
+ assert_equal('00290104006400c80a0000010c020641040000006402020200', open.to_shex[32..-1])
132
+ s = 'ffffffffffffffffffffffffffffffff002d0104626200b4513411091002060104000100800202800002020200'
133
+ open = BGP::Message.factory([s].pack('H*'))
134
+ assert_equal(s, open.to_shex)
135
+
136
+ s = "ffff ffff ffff ffff ffff
137
+ ffff ffff ffff 0038 0104 0023 00b4 1919
138
+ 1901 1b02 0601 0400 0100 0102 0280 0002
139
+ 0202 0002 0982 0700 0100 0101 8002".split.join
140
+ open = BGP::Message.factory([s].pack('H*'))
141
+ assert_equal(s, open.to_shex)
142
+
143
+ s = "ffffffffffffffffffffffffffffffff003d0104006400b402020202200206010400010001020601040001000202028000020202000206410400000064"
144
+ open = BGP::Message.factory([s].pack('H*'))
145
+ assert_equal(4, open.version)
146
+ assert_equal(100, open.local_as)
147
+ assert_equal('2.2.2.2', open.bgp_id)
148
+ assert_equal(180, open.holdtime)
149
+ assert_equal(5, open.opt_parms.size)
150
+ assert_equal(Mbgp_cap, open.opt_parms[0].class)
151
+ assert_equal(Mbgp_cap, open.opt_parms[1].class)
152
+ assert_equal(Route_refresh_cap, open.opt_parms[2].class)
153
+ assert_equal(Route_refresh_cap, open.opt_parms[3].class)
154
+ assert_equal(As4_cap, open.opt_parms[4].class)
155
+
156
+ end
157
+ def test_2
158
+ s = "ffffffffffffffffffffffffffffffff003d0104006400b402020202200206010400010001020601040001000202028000020202000206410400000064"
159
+ open = BGP::Message.factory([s].pack('H*'))
160
+ assert_equal(5, open.to_hash[:capabilities].size )
161
+ assert_equal(1, open.to_hash[:capabilities][0][:code])
162
+ assert_equal({:safi=>1, :afi=>1}, open.to_hash[:capabilities][0][:capability])
163
+ assert_equal(1, open.to_hash[:capabilities][1][:code])
164
+ assert_equal({:safi=>2, :afi=>1}, open.to_hash[:capabilities][1][:capability])
165
+ assert_equal(128, open.to_hash[:capabilities][2][:code])
166
+ assert_nil(open.to_hash[:capabilities][2][:capability])
167
+ assert_equal(2, open.to_hash[:capabilities][3][:code])
168
+ assert_nil(open.to_hash[:capabilities][3][:capability])
169
+ assert_equal(65, open.to_hash[:capabilities][4][:code])
170
+ assert_equal(100,open.to_hash[:capabilities][4 ][:capability][:as])
171
+ end
172
+ end
173
+
174
+ class Route_refresh_Test < Test::Unit::TestCase
175
+ include BGP
176
+ def test_1
177
+ s = "ffffffffffffffffffffffffffffffff00170500010001"
178
+ sbin = [s].pack('H*')
179
+ assert_equal(Route_refresh, Message.factory(sbin).class)
180
+ rr = Message.factory(sbin)
181
+ assert_equal(s, rr.to_shex)
182
+ rr.afi, rr.safi=2,2
183
+ assert_equal(2, rr.afi)
184
+ assert_equal(2, rr.safi)
185
+ assert_equal('ffffffffffffffffffffffffffffffff00170500020002', rr.to_shex)
186
+ assert_raise(ArgumentError) { rr.safi=0x100 }
187
+ assert_raise(ArgumentError) { rr.afi=-1 }
188
+ assert_equal(s, Route_refresh.new(1,1).to_shex)
189
+ assert_equal(s, Route_refresh.new(:afi=>1, :safi=>1).to_shex)
190
+ assert_equal({:afi=>1, :safi=>1}, Route_refresh.new(:afi=>1, :safi=>1).to_hash)
191
+ assert_equal(s, Message.route_refresh(1,1).unpack('H*')[0])
192
+ end
193
+ end
194
+
195
+
196
+ class Orf_route_refresh_Test < Test::Unit::TestCase
197
+ include BGP
198
+
199
+ def test_1
200
+ rr = Orf_route_refresh.new
201
+ orf = Prefix_orf.new([
202
+ Prefix_entry.add_and_permit(10,'10.0.0.0/8'),
203
+ Prefix_entry.add_and_permit(20,'20.0.0.0/8'),
204
+ Prefix_entry.add_and_permit(30,'30.0.0.0/8'),
205
+ Prefix_entry.add_and_permit(40,'40.0.0.0/8'),
206
+ Prefix_entry.add_and_permit(50,'50.0.0.0/8'),
207
+ ])
208
+ rr << orf
209
+ rr.afi=1
210
+ rr.safi=1
211
+ assert_equal('0047050001000140002d000000000a0000080a000000001400000814000000001e0000081e000000002800000828000000003200000832',rr.to_shex[32..-1])
212
+ assert_equal('IPv4', rr.afi)
213
+ assert_equal('Unicast', rr.safi)
214
+ assert_equal(1, rr.orfs.size)
215
+ assert_match(/RF Route Refresh \(5\), length: 71\nAFI IPv4 \(1\), SAFI Unicast \(1\)/, rr.to_s)
216
+ end
217
+
218
+ def test_2
219
+ s = "ffffffffffffffffffffffffffffffff0047050001000140002d000000000a0000080a000000001400000814000000001e0000081e000000002800000828000000003200000832"
220
+ sbin = [s].pack('H*')
221
+ assert_equal(Orf_route_refresh, Message.factory(sbin).class)
222
+ rr = Message.factory(sbin)
223
+ assert_equal(s, rr.to_shex)
224
+ end
225
+ end
226
+
227
+ class Notification_Test < Test::Unit::TestCase
228
+ include BGP
229
+ def test_1
230
+ assert_equal('Undefined', Notification.code_to_s(0))
231
+ assert_equal('Header Error', Notification.code_to_s(1))
232
+ assert_equal('OPEN msg error', Notification.code_to_s(2))
233
+ assert_equal('UPDATE msg error', Notification.code_to_s(3))
234
+ assert_equal('Connection Not Synchronized', Notification.code_to_s(1,1))
235
+ assert_equal('Unrecognized Well-known Attribute', Notification.code_to_s(3,2))
236
+ notif = BGP::Notification.new(1,1)
237
+ assert_equal('ffffffffffffffffffffffffffffffff0015030101', notif.to_shex)
238
+ assert_equal(notif.encode, Notification.new(notif).encode)
239
+ notif = BGP::Notification.new(2,2,'some data')
240
+ assert_equal('ffffffffffffffffffffffffffffffff001e030202736f6d652064617461', notif.to_shex)
241
+ assert_equal(notif.encode, Notification.new(notif).encode)
242
+ s = 'ffffffffffffffffffffffffffffffff001e030202736f6d652064617461'
243
+ m = Message.factory([s].pack('H*'))
244
+ assert_equal(Notification, m.class)
245
+ assert_equal(m.encode, Notification.new(m).encode)
246
+ end
247
+ end
248
+
249
+ class As4_cap_Test < Test::Unit::TestCase
250
+ include BGP
251
+ def test_1
252
+ cap1 = As4_cap.new(100)
253
+ cap2 = As4_cap.new(['0206410400000064'].pack('H*'))
254
+ cap3 = As4_cap.new(cap1.encode)
255
+ assert_equal(cap2.encode, cap3.encode)
256
+ end
257
+ end
258
+
259
+ class Mbgp_cap_Test < Test::Unit::TestCase
260
+ include BGP
261
+ def test_1
262
+ mbgp1 = Mbgp_cap.new(1,1)
263
+ mbgp2 = Mbgp_cap.new(['0206010400010001'].pack('H*'))
264
+ mbgp3 = Mbgp_cap.new(mbgp1.encode)
265
+ assert_equal(mbgp2.encode, mbgp3.encode)
266
+ end
267
+ end
268
+
269
+ class Route_refresh_cap_Test < Test::Unit::TestCase
270
+ include BGP
271
+ def test_1
272
+ assert_equal('02020200',Route_refresh_cap.new.to_shex)
273
+ assert_equal('02028000',Route_refresh_cap.new(128).to_shex)
274
+ end
275
+ end
276
+
277
+ class Orf_cap_Test < Test::Unit::TestCase
278
+ include BGP
279
+ def test_1
280
+ ent1 = Orf_cap::Entry.new(1,1,[1,1],[2,1],[3,1])
281
+ assert_equal('0001000103010102010301', ent1.to_shex)
282
+ ent2 = Orf_cap::Entry.new(1,2,[1,1],[2,1],[3,1])
283
+ assert_equal('0001000203010102010301', ent2.to_shex)
284
+ ent3 = Orf_cap::Entry.new(ent1.encode)
285
+ assert_equal(ent1.encode, ent3.encode)
286
+ orf = Orf_cap.new
287
+ orf.add(ent1)
288
+ orf.add(ent2)
289
+ assert_equal('0218031600010001030101020103010001000203010102010301', orf.to_shex)
290
+ orf2 = Orf_cap.new(orf.encode)
291
+ assert_equal(orf.encode, orf2.encode)
292
+ end
293
+
294
+ end