bgp4r 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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,143 @@
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/mp_reach'
24
+ require 'test/unit'
25
+
26
+ class Mp_reach_Test < Test::Unit::TestCase
27
+ include BGP
28
+ def test_1
29
+ s = '800e30000202102009000900190000000000000000000100402009000100000000402009000200000000402009000300000000'
30
+ sbin = [s].pack('H*')
31
+ mpr = Mp_reach.new(sbin)
32
+ assert_equal("\n AFI IPv6 (2), SAFI Multicast (2)\n nexthop: 2009:9:19::1\n 2009:1::/64\n 2009:2::/64\n 2009:3::/64", mpr.mp_reach)
33
+ assert_equal(2, mpr.afi)
34
+ assert_equal(2, mpr.safi)
35
+ assert_equal(s, mpr.to_shex)
36
+ end
37
+ def test_2
38
+ s = "800e2500020120200900090019000000000000000000012009000900190000000000000000000200"
39
+ sbin = [s].pack('H*')
40
+ mpr = Mp_reach.new(sbin)
41
+ assert_equal("\n AFI IPv6 (2), SAFI Unicast (1)\n nexthop: 2009:9:19::1, 2009:9:19::2", mpr.mp_reach)
42
+ assert_equal(2, mpr.afi)
43
+ assert_equal(1, mpr.safi)
44
+ assert_equal(s, mpr.to_shex)
45
+ end
46
+ def test_3
47
+ s = "800e0d000102080a0000010a00000200"
48
+ sbin = [s].pack('H*')
49
+ mpr = Mp_reach.new(sbin)
50
+ assert_equal("\n AFI IPv4 (1), SAFI Multicast (2)\n nexthop: 10.0.0.1, 10.0.0.2", mpr.mp_reach)
51
+ assert_equal(1, mpr.afi)
52
+ assert_equal(2, mpr.safi)
53
+ assert_equal(s, mpr.to_shex)
54
+ end
55
+ def test_4
56
+ s = "800e11000102040a0000010018c0a80118c0a802"
57
+ sbin = [s].pack('H*')
58
+ mpr = Mp_reach.new(sbin)
59
+ assert_equal("\n AFI IPv4 (1), SAFI Multicast (2)\n nexthop: 10.0.0.1\n 192.168.1.0/24\n 192.168.2.0/24", mpr.mp_reach)
60
+ assert_equal(1, mpr.afi)
61
+ assert_equal(2, mpr.safi)
62
+ assert_equal(s, mpr.to_shex)
63
+ end
64
+ def test_5
65
+ s = "800e39000204102009000900190000000000000000000100580006512009000100000000580006612009000200000000580006712009000300000000"
66
+ sbin = [s].pack('H*')
67
+ mpr = Mp_reach.new(sbin)
68
+ assert_equal("\n AFI IPv6 (2), SAFI Labeled NLRI (4)\n nexthop: 2009:9:19::1\n Label Stack=101 (bottom) 2009:1::/64\n Label Stack=102 (bottom) 2009:2::/64\n Label Stack=103 (bottom) 2009:3::/64", mpr.mp_reach)
69
+ assert_equal(2, mpr.afi)
70
+ assert_equal(4, mpr.safi)
71
+ assert_equal(s, mpr.to_shex)
72
+ #puts mpr
73
+ end
74
+ def test_6
75
+ mpr = Mp_reach.new(:safi=>2, :nexthop=> ['2009:9:19::1/128'], :prefix=> ['2009:1::/64', '2009:2::/64', '2009:3::/64'])
76
+ assert_equal("\n AFI IPv6 (2), SAFI Multicast (2)\n nexthop: 2009:9:19::1\n 2009:1::/64\n 2009:2::/64\n 2009:3::/64", mpr.mp_reach)
77
+ assert_equal(mpr.to_shex, Mp_reach.new(mpr.encode).to_shex)
78
+ mpr = Mp_reach.new(:safi=>1, :nexthop=> ['10.0.0.1','10.0.0.2'], :prefix=> '192.168.0.0/24')
79
+ assert_equal("\n AFI IPv4 (1), SAFI Unicast (1)\n nexthop: 10.0.0.1, 10.0.0.2\n 192.168.0.0/24", mpr.mp_reach)
80
+ assert_equal(mpr.to_shex, Mp_reach.new(mpr.encode).to_shex)
81
+ mpr = Mp_reach.new(:safi=>4, :nexthop=> ['10.0.0.1','10.0.0.2'], :nlri=> {:prefix=> '192.168.0.0/24', :label=>101} )
82
+ assert_equal("\n AFI IPv4 (1), SAFI Labeled NLRI (4)\n nexthop: 10.0.0.1, 10.0.0.2\n Label Stack=101 (bottom) 192.168.0.0/24", mpr.mp_reach)
83
+ assert_equal(mpr.to_shex, Mp_reach.new(mpr.encode).to_shex)
84
+ mpr = Mp_reach.new(:safi=>128, :nexthop=> ['10.0.0.1','10.0.0.2'], :nlri=> {:rd=> [100,100], :prefix=> '192.168.0.0/24', :label=>101})
85
+ assert_equal("\n AFI IPv4 (1), SAFI Labeled VPN Unicast (128)\n nexthop: 10.0.0.1, 10.0.0.2\n Label Stack=101 (bottom) RD=100:100, IPv4=192.168.0.0/24", mpr.mp_reach)
86
+ assert_equal(mpr.to_shex, Mp_reach.new(mpr.encode).to_shex)
87
+ mpr = Mp_reach.new(:safi=>128, :nexthop=> ['10.0.0.1','10.0.0.2'], :nlri=> {:rd=> Rd.new(100,100), :prefix=> Prefix.new('192.168.0.0/24'), :label=>101})
88
+ assert_equal("\n AFI IPv4 (1), SAFI Labeled VPN Unicast (128)\n nexthop: 10.0.0.1, 10.0.0.2\n Label Stack=101 (bottom) RD=100:100, IPv4=192.168.0.0/24", mpr.mp_reach)
89
+ assert_equal(mpr.to_shex, Mp_reach.new(mpr.encode).to_shex)
90
+ end
91
+ def test_7
92
+ mpr = Mp_reach.new(:safi=>2, :nexthop=> '2009:9:19::1/128', :prefix=> [
93
+ '2009:1::/64', '2009:2::/64', '2009:3::/64', '2009:4::/64', '2009:5::/64', '2009:6::/64'])
94
+ assert_equal("\n AFI IPv6 (2), SAFI Multicast (2)\n nexthop: 2009:9:19::1\n 2009:1::/64\n 2009:2::/64\n 2009:3::/64\n 2009:4::/64\n 2009:5::/64\n 2009:6::/64", mpr.mp_reach)
95
+ mpr = Mp_reach.new(:safi=>1, :nexthop=> '10.0.0.1', :prefix=> [
96
+ '192.168.0.0/24', '192.168.1.0/24', '192.168.2.0/24', '192.168.3.0/24', '192.168.4.0/24', '192.168.5.0/24'])
97
+ assert_equal("\n AFI IPv4 (1), SAFI Unicast (1)\n nexthop: 10.0.0.1\n 192.168.0.0/24\n 192.168.1.0/24\n 192.168.2.0/24\n 192.168.3.0/24\n 192.168.4.0/24\n 192.168.5.0/24", mpr.mp_reach)
98
+ end
99
+ def test_8
100
+ mpr = Mp_reach.new(:safi=>1, :nexthop=> '10.0.0.1', :prefix=> [
101
+ '192.168.0.0/24', '192.168.1.0/24', '192.168.2.0/24', '192.168.3.0/24', '192.168.4.0/24', '192.168.5.0/24'])
102
+ assert_equal(Mp_unreach,mpr.new_unreach.class)
103
+ mpr2 = Mp_reach.new(mpr)
104
+ assert_equal(mpr.encode, mpr2.encode)
105
+ assert_equal(Mp_unreach, Mp_unreach.new(mpr.new_unreach).class)
106
+ end
107
+ end
108
+
109
+ class Mp_unreach_Test < Test::Unit::TestCase
110
+ include BGP
111
+ def test_1
112
+ mpur = Mp_unreach.new(:safi=>1, :prefix=>['10.0.0.0/16', '10.1.0.0/16'])
113
+ assert_raise(ArgumentError) { Mp_unreach.new }
114
+ end
115
+
116
+ def test_2
117
+ mpur = Mp_unreach.new(:safi=>2, :prefix=>['192.168.1.0/24', '192.168.2.0/24'])
118
+ assert_equal('800f0b00010218c0a80118c0a802', mpur.to_shex)
119
+
120
+ mpur = Mp_unreach.new(:safi=>2, :prefix=>['2007:1::/64', '2007:2::/64','2007:3::/64'])
121
+ assert_equal('800f1e000202402007000100000000402007000200000000402007000300000000', mpur.to_shex)
122
+
123
+ #mpur = Mp_unreach.new(:safi=>2, :prefix=>['2007:1::/64, 101', '2007:2::/64,102','2007:3::/64, 103'])
124
+ mpur = Mp_unreach.new(:safi=>4, :nlri=> [
125
+ {:prefix=>'2007:1::/64', :label=> 101},
126
+ {:prefix=>'2007:2::/64', :label=> 102},
127
+ {:prefix=>'2007:3::/64', :label=> 103},])
128
+ assert_equal('800f27000204580006512007000100000000580006612007000200000000580006712007000300000000', mpur.to_shex)
129
+ assert_match(/^800f..000204/,mpur.to_shex)
130
+ assert_match(/58000651200700010000000058/,mpur.to_shex)
131
+ assert_match(/58000661200700020000000058/,mpur.to_shex)
132
+ assert_match(/580006712007000300000000$/,mpur.to_shex)
133
+
134
+ mpur = Mp_unreach.new(:safi=>128, :nlri=> [
135
+ {:rd=> Rd.new(100,100), :prefix=> Prefix.new('192.168.1.0/24'), :label=>101},
136
+ {:rd=> Rd.new(100,100), :prefix=> Prefix.new('192.168.2.0/24'), :label=>102},
137
+ {:rd=> Rd.new(100,100), :prefix=> Prefix.new('192.168.3.0/24'), :label=>103},])
138
+ assert_match(/^800f..000180/,mpur.to_shex)
139
+ assert_equal("700006510000006400000064c0a801",mpur.nlris[0].to_shex)
140
+ assert_equal("700006610000006400000064c0a802",mpur.nlris[1].to_shex)
141
+
142
+ end
143
+ end
@@ -0,0 +1,46 @@
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/multi_exit_disc"
24
+ require 'test/unit'
25
+ class Multi_exit_disc_Test < Test::Unit::TestCase
26
+ include BGP
27
+ def test_1
28
+ assert_equal("80040400000000", Multi_exit_disc.new.to_shex)
29
+ assert_equal("80040400000064", Multi_exit_disc.new(100).to_shex)
30
+ assert_equal("800404000000c8", Multi_exit_disc.new(200).to_shex)
31
+ assert_equal("800404000000c8", Multi_exit_disc.new(:med=>200).to_shex)
32
+ assert_equal("800404000000c8", Multi_exit_disc.new(:multi_exit_disc=>200).to_shex)
33
+ assert_raise(ArgumentError) { Multi_exit_disc.new(:local_pref=>200) }
34
+ assert_equal("(0x00c8) 200", Multi_exit_disc.new(200).multi_exit_disc)
35
+ assert_equal("[Oncr] (4) Multi Exit Disc: [800404000000c8] '(0x00c8) 200'", Multi_exit_disc.new(200).to_s)
36
+ assert_raise(ArgumentError) { Multi_exit_disc.new({})}
37
+ end
38
+ def test_2
39
+ mp = Multi_exit_disc.new(200)
40
+ assert_equal("800404000000c8", Multi_exit_disc.new(mp.encode).to_shex)
41
+ assert_equal("(0x00c8) 200", Multi_exit_disc.new(200).multi_exit_disc)
42
+ assert_equal(200, Multi_exit_disc.new(200).to_i)
43
+ mp1 = Multi_exit_disc.new(mp)
44
+ assert_equal(mp.encode, mp.encode)
45
+ end
46
+ 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 "test/unit"
24
+
25
+ require "bgp/neighbor"
26
+ require 'bgp/message'
27
+
28
+ class TestBgpNeighbor < Test::Unit::TestCase
29
+ include BGP
30
+ def test_open_msg
31
+ neighbor = Neighbor.new \
32
+ :version=> 4,
33
+ :my_as=> 100,
34
+ :remote_addr => '192.168.1.200',
35
+ :local_addr => '192.168.1.5',
36
+ :id=> '1.1.1.1',
37
+ :holdtime=> 20
38
+ neighbor.capability Mbgp_cap.ipv4_unicast
39
+ neighbor.capability Mbgp_cap.ipv4_multicast
40
+ neighbor.capability Route_refresh_cap.new
41
+ neighbor.capability Route_refresh_cap.new 128
42
+ neighbor.capability As4_cap.new(100)
43
+ open_msg = neighbor.instance_eval { _open_msg_ }
44
+ assert_equal(5,open_msg.opt_parms.size)
45
+ assert_equal(4,open_msg.version)
46
+ assert_equal(20,open_msg.holdtime)
47
+ assert_equal(100,open_msg.local_as)
48
+ # puts neighbor
49
+ end
50
+ end
@@ -0,0 +1,37 @@
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/next_hop'
24
+ require 'test/unit'
25
+ class Next_hop_Test < Test::Unit::TestCase
26
+ include BGP
27
+ def test_1
28
+ next_hop = Next_hop.new('10.0.0.1')
29
+ assert_equal('10.0.0.1', next_hop.next_hop)
30
+ assert_equal("[wTcr] (3) Next Hop: [4003040a000001] '10.0.0.1'", next_hop.to_s)
31
+ assert_equal(167772161, next_hop.to_i)
32
+ assert_equal('4003040a000001', next_hop.to_shex)
33
+ assert_equal(next_hop.to_shex, Next_hop.new(next_hop.encode).to_shex)
34
+ next_hop1 = Next_hop.new(next_hop)
35
+ assert_equal(next_hop.encode, next_hop1.encode)
36
+ end
37
+ end
data/test/nlri_test.rb ADDED
@@ -0,0 +1,189 @@
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/nlri'
24
+ require 'test/unit'
25
+ class NLRI_Ip4_Test < Test::Unit::TestCase
26
+ include BGP
27
+ def test_1
28
+ el = Nlri::Ip4.new('20.0.0.0/16')
29
+ el2 = Nlri::Ip4.new(el)
30
+ assert_equal(el2.to_shex, el.to_shex)
31
+ assert_equal(Nlri::Ip4, el2.class)
32
+ assert_equal('0f1400', Nlri::Ip4.new('20.0.0.0/15').to_shex)
33
+ assert_equal('101400', Nlri::Ip4.new('20.0.0.0/16').to_shex)
34
+ assert_equal('11140000', Nlri::Ip4.new('20.0.0.0/17').to_shex)
35
+ assert_equal('17140000', Nlri::Ip4.new('20.0.0.0/23').to_shex)
36
+ assert_equal('18140000', Nlri::Ip4.new('20.0.0.0/24').to_shex)
37
+ assert_equal('1914000000', Nlri::Ip4.new('20.0.0.0/25').to_shex)
38
+ assert_equal('20.0.0.0/25', Nlri::Ip4.new('20.0.0.0/25').to_s)
39
+ end
40
+
41
+ def test_2
42
+ assert_equal('0f1400',Nlri::Ip4.new(['0f1400'].pack('H*')).to_shex)
43
+ assert_equal('101400',Nlri::Ip4.new(['101400'].pack('H*')).to_shex)
44
+ assert_equal('11140000',Nlri::Ip4.new(['11140000'].pack('H*')).to_shex)
45
+ assert_equal('18140000',Nlri::Ip4.new(['18140000'].pack('H*')).to_shex)
46
+ assert_equal('1914000000',Nlri::Ip4.new(['1914000000'].pack('H*')).to_shex)
47
+ s = ['0f140010140011140000'].pack('H*')
48
+ ip4 = Nlri::Ip4.new(s)
49
+ assert_equal('10140011140000', s.unpack('H*')[0])
50
+ ip4 = Nlri::Ip4.new(s)
51
+ assert_equal('11140000', s.unpack('H*')[0])
52
+ ip4 = Nlri::Ip4.new(s)
53
+ assert_equal('', s.unpack('H*')[0])
54
+
55
+ end
56
+ end
57
+ class NLRI_Ip6_Test < Test::Unit::TestCase
58
+ include BGP
59
+ def test_1
60
+ assert_equal('0410',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/4').to_shex)
61
+ assert_equal('0510',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/5').to_shex)
62
+ assert_equal('0710',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/7').to_shex)
63
+ assert_equal('0811',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/8').to_shex)
64
+ assert_equal('091100',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/9').to_shex)
65
+ assert_equal('30111122223333',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/48').to_shex)
66
+ assert_equal('3111112222333300',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/49').to_shex)
67
+ assert_equal('401111222233334444',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/64').to_shex)
68
+ assert_equal('41111122223333444400',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/65').to_shex)
69
+ assert_equal('7f11112222333344445555666677778888',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/127').to_shex)
70
+ assert_equal('8011112222333344445555666677778888',Nlri::Ip6.new('1111:2222:3333:4444:5555:6666:7777:8888/128').to_shex)
71
+ end
72
+ end
73
+
74
+ class Withdrawn_Test < Test::Unit::TestCase
75
+ include BGP
76
+ def test_1
77
+ nlri1 = Withdrawn.new
78
+ assert_equal('0000', nlri1.to_shex)
79
+ nlri1 << Withdrawn::Ip4.new('20.0.0.0/15')
80
+ nlri1 << '20.0.0.0/17'
81
+ nlri1 << '20.0.0.0/24'
82
+ s = '0f140010140011140000'
83
+ nlri2 = Withdrawn.new([s].pack('H*'))
84
+ assert_equal('000a0f140010140011140000', nlri2.to_shex)
85
+ assert_equal('000a0f140010140011140000', nlri2.to_shex(true))
86
+ assert_equal('0f140010140011140000', nlri2.to_shex(false))
87
+ assert_equal(3,nlri2.nlris.size)
88
+ end
89
+ end
90
+
91
+ class Nlri_Test < Test::Unit::TestCase
92
+ include BGP
93
+ def test_1
94
+ nlri1 = Nlri.new
95
+ nlri1 << Nlri::Ip4.new('20.0.0.0/15')
96
+ nlri1 << '20.0.0.0/17'
97
+ nlri1 << '20.0.0.0/24'
98
+ s = '0f140010140011140000'
99
+ nlri2 = Nlri.new([s].pack('H*'))
100
+ assert_equal('0f140010140011140000', nlri2.to_shex)
101
+ assert_raise(ArgumentError) { nlri2.to_shex(true) }
102
+ assert_equal(3,nlri2.nlris.size)
103
+
104
+ end
105
+
106
+ def test_2
107
+ nlri1 = Nlri.new
108
+ nlri1 << Nlri::Ip4.new('20.0.0.0/15')
109
+ nlri1 << '20.0.0.0/17'
110
+ nlri1 << '20.0.0.0/24'
111
+ s = '0f140010140011140000'
112
+ nlri2 = Nlri.new([s].pack('H*'))
113
+ assert_equal('0f140010140011140000', nlri2.to_shex)
114
+ assert_raise(ArgumentError) { nlri2.to_shex(true) }
115
+ assert_equal(3,nlri2.nlris.size)
116
+ end
117
+ end
118
+
119
+ class Prefix_Test < Test::Unit::TestCase
120
+ include BGP
121
+ def test_1
122
+ assert_equal('::/128',Prefix.new.to_s)
123
+ assert_equal(2,Prefix.new.afi)
124
+ assert_equal('192.168.0.0/16',Prefix.new('192.168.0.0/16').to_s)
125
+ assert_equal(1,Prefix.new('192.168.0.0/16').afi)
126
+ assert(Prefix.new('192.168.0.0/16').ipv4?)
127
+ assert_equal('10c0a8',Prefix.new('192.168.0.0/16').to_shex)
128
+ assert_equal('402009000400040000',Prefix.new('2009:4:4::/64').to_shex)
129
+ assert('402009000400040000',Prefix.new('2009:4:4::/64').ipv6?)
130
+ assert_equal('2009:4:4::/64',Prefix.new('2009:4:4::/64').to_s)
131
+ end
132
+ end
133
+ class Inet_unicast_Test < Test::Unit::TestCase
134
+ include BGP
135
+ def test_1
136
+ assert_equal(1,Inet_unicast.new('192.168.0.0/16').afi)
137
+ assert_equal(1,Inet_unicast.new('192.168.0.0/16').safi)
138
+ assert_equal(2,Inet_unicast.new('2009:4:4::/64').afi)
139
+ assert_equal(1,Inet_unicast.new('2009:4:4::/64').safi)
140
+ assert_equal(1,Inet_multicast.new('192.168.0.0/16').afi)
141
+ assert_equal(2,Inet_multicast.new('192.168.0.0/16').safi)
142
+ assert_equal(2,Inet_multicast.new('2009:4:4::/64').afi)
143
+ assert_equal(2,Inet_multicast.new('2009:4:4::/64').safi)
144
+ end
145
+ end
146
+
147
+ class Vpn_Test < Test::Unit::TestCase
148
+ include BGP
149
+ def test_1
150
+ assert_equal('5800000001000000010a0000', Vpn.new('10.0.0.1/24', Rd.new(1,1)).to_shex)
151
+ assert_equal('00000001000000010a0000', Vpn.new('10.0.0.1/24', Rd.new(1,1)).to_shex(false))
152
+ assert_equal('RD=1:1, IPv4=10.0.0.0/24', Vpn.new('10.0.0.1/24', Rd.new(1,1)).to_s)
153
+ assert_equal(88, Vpn.new('10.0.0.1/24', Rd.new(1,1)).bit_length)
154
+ assert_equal(1,Vpn.new('10.0.0.1/24', Rd.new(1,1)).afi)
155
+ assert_equal(true,Vpn.new('10.0.0.1/24', Rd.new(1,1)).ipv4?)
156
+ assert_equal(false,Vpn.new('10.0.0.1/24', Rd.new(1,1)).ipv6?)
157
+ assert_equal(2,Vpn.new('2009:4:5::/105', Rd.new(1,1)).afi)
158
+ assert_equal(false,Vpn.new('2009:4:5::/105', Rd.new(1,1)).ipv4?)
159
+ assert_equal(true,Vpn.new('2009:4:5::/105', Rd.new(1,1)).ipv6?)
160
+ assert_equal('RD=1:1, IPv6=2009:4:5::/105',Vpn.new('2009:4:5::/105', Rd.new(1,1)).to_s)
161
+ assert_equal('700000000100000001200900040005',Vpn.new('2009:4:5::/48', Rd.new(1,1)).to_shex)
162
+ assert_equal(169,Vpn.new('2009:4:5::/105', Rd.new(1,1)).bit_length)
163
+ end
164
+ def test_2
165
+ vpn = Vpn.new(['5800000001000000010a0000'].pack('H*'))
166
+ assert_equal( '5800000001000000010a0000', vpn.to_shex)
167
+ assert_equal( '00000001000000010a0000', vpn.to_shex(false))
168
+ vpn = Vpn.new(['700000000100000001200900040005'].pack('H*'), 2)
169
+ assert_equal( '700000000100000001200900040005', vpn.to_shex)
170
+ vpn = Vpn.new(['700000000100000001200900040005'].pack('H*'), 2)
171
+ assert_equal( '700000000100000001200900040005', vpn.to_shex)
172
+ end
173
+ end
174
+
175
+ class Labeled_Test < Test::Unit::TestCase
176
+ include BGP
177
+ def test_1
178
+ assert_equal('Label Stack=1,2,3 (bottom) 10.0.0.0/24', Labeled.new(Prefix.new('10.0.0.1/24'),1,2,3).to_s)
179
+ assert_equal(3*24+24, Labeled.new(Prefix.new('10.0.0.1/24'),1,2,3).bit_length)
180
+ assert_equal('600000100000200000310a0000', Labeled.new(Prefix.new('10.0.0.1/24'),1,2,3).to_shex)
181
+ assert_equal('6b0006510000006400000064c0a800',Labeled.new(Vpn.new('192.168.0.0/19', Rd.new(100,100)),101).to_shex)
182
+ assert_equal('d8000651000000640000006420090004000500000000000000000001',Labeled.new(Vpn.new('2009:4:5::1', Rd.new(100,100)),101).to_shex)
183
+ assert_equal(24+64+128,Labeled.new(Vpn.new('2009:4:5::1', Rd.new(100,100)),101).bit_length)
184
+ assert_equal(24+64+64,Labeled.new(Vpn.new('2009:4:5::1/64', Rd.new(100,100)),101).bit_length)
185
+ assert_equal(24*3+64+48,Labeled.new(Vpn.new('2009:4:5::1/48', Rd.new(100,100)),101,102,103).bit_length)
186
+ assert_equal('9800065100000064000000642009000400050000',Labeled.new(Vpn.new('2009:4:5::1/64', Rd.new(100,100)),101).to_shex)
187
+ assert_equal('9800065100000064000000642009000400050000',Labeled.new(['9800065100000064000000642009000400050000'].pack('H*'),2).to_shex)
188
+ end
189
+ end
@@ -0,0 +1,57 @@
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/origin'
24
+ require 'test/unit'
25
+ class Origin_Test < Test::Unit::TestCase
26
+ include BGP
27
+ def test_1
28
+ assert_equal("40010100", Origin.new.to_shex)
29
+ assert_equal("40010100", Origin.new(:igp).to_shex)
30
+ assert_equal("40010101", Origin.new(:egp).to_shex)
31
+ assert_equal("40010102", Origin.new(:incomplete).to_shex)
32
+ end
33
+ def test_2
34
+ assert_equal("40010100", Origin.new(0).to_shex)
35
+ assert_equal("40010101", Origin.new(1).to_shex)
36
+ assert_equal("40010102", Origin.new(2).to_shex)
37
+ end
38
+ def test_3
39
+ assert_equal("40010100", Origin.new(:origin => 0 ).to_shex)
40
+ assert_equal("40010101", Origin.new(:origin => 1 ).to_shex)
41
+ assert_equal("40010102", Origin.new(:origin => 2 ).to_shex)
42
+ end
43
+ def test_4
44
+ assert_equal("40010100", Origin.new( Origin.new.encode).to_shex)
45
+ end
46
+ def test_5
47
+ assert_equal("igp", Origin.new.origin)
48
+ assert_equal("igp", Origin.new(:igp).origin)
49
+ assert_equal("egp", Origin.new(:egp).origin)
50
+ assert_equal("incomplete", Origin.new(:incomplete).origin)
51
+ end
52
+ def test_6
53
+ assert_equal("[wTcr] (1) Origin: [40010100] 'igp'", Origin.new.to_s)
54
+ assert_equal("[wTcr] (1) Origin: [40010100] 'igp'", Origin.new(:igp).to_s)
55
+ assert_equal("Origin (1), length: 1, Flags [T]: igp\n 0x0000: ", Origin.new.to_s(:tcpdump))
56
+ end
57
+ end
@@ -0,0 +1,38 @@
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/originator_id'
24
+ require 'test/unit'
25
+ class Originator_id_Test < Test::Unit::TestCase
26
+ include BGP
27
+ def test_1
28
+ orig_id = Originator_id.new('10.0.0.1')
29
+ assert_equal('10.0.0.1', orig_id.originator_id)
30
+ assert_equal("[Oncr] (9) Originator Id: [8009040a000001] '10.0.0.1'", orig_id.to_s)
31
+ assert_equal(167772161, orig_id.to_i)
32
+ assert_equal('8009040a000001', orig_id.to_shex)
33
+ assert_equal('80090401010101', Originator_id.new('1.1.1.1').to_shex)
34
+ assert_equal(orig_id.to_shex, Originator_id.new(orig_id.encode).to_shex)
35
+ orig_id1 = Originator_id.new(orig_id)
36
+ assert_equal(orig_id.encode, orig_id1.encode)
37
+ end
38
+ end