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
data/examples/routegen ADDED
@@ -0,0 +1,85 @@
1
+ #--
2
+ #
3
+ # Copyright 2008, 2009 Jean-Michel Esnault.
4
+ # All rights reserved.
5
+ # See LICENSE.txt for permissions.
6
+ #
7
+ #++
8
+
9
+ require 'rubygems'
10
+ require 'bgp4r'
11
+ require 'yaml'
12
+ include BGP
13
+
14
+ require 'pp'
15
+ config_file = ARGV[0] ||= File.basename($0) + ".yml"
16
+
17
+ Log.create
18
+ Log.level=Logger::DEBUG
19
+ config = YAML::load_file(config_file)
20
+
21
+ p config
22
+
23
+ config.keys.each do |k|
24
+ case k
25
+ when 'neighbor'
26
+ neighbor = config['neighbor']
27
+ neighbor.keys.each do |k|
28
+ case k
29
+ when /router\s+id/ ; @router_id = neighbor[k]
30
+ when /my\s+as/ ; @my_as = neighbor[k]
31
+ when 'holdtime' ; @holdtime = neighbor[k]
32
+ when /neighbor\s+address/ ; @neighbor_addr = neighbor[k]
33
+ when /local\s+address/ ; @local_addr = neighbor[k]
34
+ when 'capabilities'
35
+ end
36
+ end
37
+ when 'routes'
38
+ routes = config['routes']
39
+ routes.keys.each do |k|
40
+ case k
41
+ when /nlri(|s)/
42
+ _pfx, _npfx, _pack = routes[k].split(',')
43
+ @nlri = IPAddr.new _pfx
44
+ @times = _npfx.to_i ||=1
45
+ @pack = _pack.to_i ||=1
46
+ when /next\s*hop/ ; @nexthop= routes[k]
47
+ when /local\s*pref/ ; @local_pref= routes[k]
48
+ when /med/ ; @med = routes[k]
49
+ when /origin/ ; @origin = routes[k]
50
+ when /communities/ ; @communities = routes[k]
51
+ when /as\s*path/ ; @as_path= routes[k]
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ neighbor = Neighbor.new \
58
+ :version => 4,
59
+ :my_as=> @my_as,
60
+ :remote_addr => @neighbor_addr,
61
+ :local_addr => @local_addr,
62
+ :id=> @router_id,
63
+ :holdtime=> @holdtime
64
+
65
+ # neighbor.capability :as4_byte
66
+
67
+ pa = Path_attribute.new(
68
+ Origin.new(@origin),
69
+ Next_hop.new(@nexthop),
70
+ Local_pref.new(@local_pref),
71
+ Communities.new(*(@communities.split.map { |com| com.to_i})),
72
+ As_path.new(*(@as_path.split.map { |as| as.to_i}))
73
+ )
74
+
75
+ neighbor.start
76
+
77
+ nlris = Nlri.new
78
+ (1..@times.to_i).each do |n|
79
+ nlris << (@nlri ^ n)
80
+ next unless (n % @pack) == 0 or (n == @times)
81
+ neighbor.send_message Update.new(pa, nlris)
82
+ nlris = Nlri.new
83
+ end
84
+
85
+ Thread.stop
@@ -0,0 +1,50 @@
1
+ # YAML config file for routegen demo.
2
+ # $ ruby routegen routegen.yaml
3
+ ---
4
+ neighbor:
5
+ router id: 1.1.1.1
6
+ my as: 100
7
+ holdtime: 180
8
+ local address: 192.168.1.6
9
+ neighbor address: 192.168.1.199
10
+ capabilities:
11
+ - four byte as
12
+ routes:
13
+ nlris: 11.0.0.0/28, 1999, 127
14
+ next hop: 192.168.1.6
15
+ local pref: 100
16
+ med: 100
17
+ origin: 1
18
+ communities: 100:1 200:1 300:1
19
+ as path: 200 300 400 500
20
+
21
+ ...
22
+
23
+ produces:
24
+
25
+ ios#show ip route summary | inc bgp
26
+ bgp 100 0 1999 107946 287856
27
+ ios#
28
+
29
+ ios#show ip bgp ipv4 unicast
30
+ BGP table version is 13995, local router ID is 172.21.2.1
31
+ Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
32
+ r RIB-failure
33
+ Origin codes: i - IGP, e - EGP, ? - incomplete
34
+
35
+ Network Next Hop Metric LocPrf Weight Path
36
+ *>i11.0.0.16/28 192.168.1.6 100 0 200 300 400 500 e
37
+ *>i11.0.0.32/28 192.168.1.6 100 0 200 300 400 500 e
38
+ *>i11.0.0.48/28 192.168.1.6 100 0 200 300 400 500 e
39
+ *>i11.0.0.64/28 192.168.1.6 100 0 200 300 400 500 e
40
+ *>i11.0.0.80/28 192.168.1.6 100 0 200 300 400 500 e
41
+ *>i11.0.0.96/28 192.168.1.6 100 0 200 300 400 500 e
42
+ *>i11.0.0.112/28 192.168.1.6 100 0 200 300 400 500 e
43
+ *>i11.0.0.128/28 192.168.1.6 100 0 200 300 400 500 e
44
+ *>i11.0.0.144/28 192.168.1.6 100 0 200 300 400 500 e
45
+ *>i11.0.0.160/28 192.168.1.6 100 0 200 300 400 500 e
46
+ *>i11.0.0.176/28 192.168.1.6 100 0 200 300 400 500 e
47
+
48
+ ...
49
+
50
+
@@ -0,0 +1,66 @@
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
+
24
+ require 'bgp/aggregator'
25
+ require 'test/unit'
26
+ class Aggregator_Test < Test::Unit::TestCase
27
+ include BGP
28
+ def test_1
29
+ aggregator = Aggregator.new('10.0.0.1',100)
30
+ aggregator.encode4
31
+ assert_equal(aggregator.encode4, aggregator.encode)
32
+ assert_equal('10.0.0.1, 100', aggregator.aggregator)
33
+ assert_equal('c0070600640a000001', aggregator.to_shex)
34
+ assert_equal('c00708000000640a000001', aggregator.to_shex(true))
35
+ assert_equal(aggregator.to_shex, Aggregator.new(aggregator.encode).to_shex)
36
+ assert_equal("[OTcr] (7) Aggregator: [c0070600640a000001] '10.0.0.1, 100'", aggregator.to_s)
37
+ end
38
+ def test_2
39
+ aggregator = Aggregator.new('10.0.0.1',100)
40
+ assert_equal('10.0.0.1, 0.100', aggregator.aggregator(true))
41
+ aggregator = Aggregator.new('10.0.0.1',0xdeadbeef)
42
+ assert_equal('10.0.0.1, 57005.48879', aggregator.aggregator(true))
43
+ assert_equal('c00708deadbeef0a000001', aggregator.to_shex(true))
44
+ aggregator = Aggregator.new(aggregator.encode(true),true)
45
+ assert_equal('10.0.0.1, 3735928559', aggregator.aggregator)
46
+ assert_equal(0xdeadbeef, aggregator.as)
47
+ end
48
+ def test3
49
+ ag2 = Aggregator.new('10.0.0.1',100)
50
+ ag4 = As4_aggregator.new('10.0.0.1',100)
51
+ assert_equal('c01208000000640a000001', ag4.to_shex)
52
+ assert_not_equal(ag2.to_shex(true), ag4.to_shex)
53
+ assert_equal('10.0.0.1, 0.100',ag4.aggregator)
54
+ assert_equal("[OTcr] (18) As4 Aggregator: [c01208000000640a00000...] '10.0.0.1, 0.100'",ag4.to_s)
55
+ assert_equal(ag4.to_shex,As4_aggregator.new(ag4.encode).to_shex)
56
+ end
57
+ def test4
58
+ ag2 = Aggregator.new('10.0.0.1',100)
59
+ ag = Aggregator.new(ag2)
60
+ assert_equal(ag2.encode, ag.encode)
61
+ ag4 = As4_aggregator.new('10.0.0.1',100)
62
+ ag = As4_aggregator.new(ag4)
63
+ assert_raise(ArgumentError) {As4_aggregator.new(ag2)}
64
+ assert_equal(ag4.encode, ag.encode)
65
+ end
66
+ end
@@ -0,0 +1,149 @@
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/as_path'
24
+ require 'test/unit'
25
+
26
+ class Segment_Test < Test::Unit::TestCase
27
+ include BGP
28
+ def test_1
29
+ assert_equal(BGP::ATTR::SEQUENCE, As_path::Segment.new(:sequence, 1).seg_type)
30
+ assert_equal([1], As_path::Segment.new(:sequence, 1).as)
31
+ assert_equal([1,2,3], As_path::Segment.new(:sequence, 1,2,3).as)
32
+ assert_equal('1 2 3', As_path::Segment.new(:sequence, 1,2,3).to_s)
33
+ end
34
+ def test_2
35
+ assert_equal('02010001', As_path::Segment.new(:sequence, 1).to_shex)
36
+ assert_equal('020100000001', As_path::Segment.new(:sequence, 1).to_shex(true))
37
+ assert_equal('02010001', As_path::Segment.new(['02010001'].pack('H*')).to_shex)
38
+ assert_equal('0203000100020003', As_path::Segment.new(:sequence, 1,2,3).to_shex)
39
+ assert_equal('0203000100020003', As_path::Sequence.new(1,2,3).to_shex)
40
+ end
41
+ def test_3
42
+ assert_equal('01010001', As_path::Segment.new(:set, 1).to_shex)
43
+ assert_equal('030100000001', As_path::Segment.new(:confed_sequence, 1).to_shex(true))
44
+ assert_equal('030100000001', As_path::Segment.new(:confed_sequence, 1).to_shex(true))
45
+ assert_equal('030100000001', As_path::Confed_sequence.new(1).to_shex(true))
46
+ assert_equal('040100000001', As_path::Confed_set.new(1).to_shex(true))
47
+ assert_equal('02010001', As_path::Segment.new(['02010001'].pack('H*')).to_shex)
48
+ end
49
+ def test_4
50
+ seg = As_path::Segment.factory(['0203000100020003'].pack('H*'))
51
+ assert_equal(As_path::Sequence,seg.class)
52
+ seg = As_path::Segment.factory(['0103000100020003'].pack('H*'))
53
+ assert_equal(As_path::Set,seg.class)
54
+ seg = As_path::Segment.factory(['0303000100020003'].pack('H*'))
55
+ assert_equal(As_path::Confed_sequence,seg.class)
56
+ seg = As_path::Segment.factory(['0403000100020003'].pack('H*'))
57
+ assert_equal(As_path::Confed_set,seg.class)
58
+ seg = As_path::Segment.factory(['0203000100020003'].pack('H*'))
59
+ assert_equal(As_path::Sequence,seg.class)
60
+ assert_equal('0203000100020003', seg.to_shex)
61
+ assert_equal('0203000000010000000200000003', seg.to_shex(true))
62
+ seg = As_path::Segment.factory(['0203000000010000000200000003'].pack('H*'), true)
63
+ assert_equal('0203000000010000000200000003', seg.to_shex(true))
64
+ assert_equal('0203000100020003', seg.to_shex(false))
65
+ end
66
+ end
67
+
68
+ class As_path_Test < Test::Unit::TestCase
69
+ include BGP
70
+ def test_1
71
+ assert_equal('400200', As_path.new.to_shex)
72
+ path = As_path.new(1)
73
+ assert_equal("40020402010001", As_path.new(1).to_shex)
74
+ assert_equal("40020402010001", As_path.new(As_path::Sequence.new(1)).to_shex)
75
+ assert_equal("40020402010001", As_path.new(As_path::Segment.new(:sequence,1)).to_shex)
76
+ assert_equal("4002080203000100020003", As_path.new(1,2,3).to_shex)
77
+ assert_equal("4002080203000100020003", As_path.new(As_path::Sequence.new(1,2,3)).to_shex)
78
+ assert_equal("4002080203000100020003", As_path.new(As_path::Segment.new(:sequence,1,2,3)).to_shex)
79
+ end
80
+ def test_2
81
+ path = As_path.new(['4002080203000100020003'].pack('H*').is_packed)
82
+ assert_equal('4002080203000100020003', path.to_shex)
83
+ path << As_path::Set.new(1,2,3)
84
+ path << As_path::Confed_sequence.new(1,2,3)
85
+ assert_equal('400218020300010002000301030001000200030303000100020003', path.to_shex)
86
+ assert_equal('40021802030...', path.to_shex_len(10))
87
+ assert_equal("[wTcr] (2) As Path: [400218020300010002000...] '1 2 3 {1, 2, 3} (1 2 3)'", path.to_s)
88
+ end
89
+ def test_3
90
+ path = As_path.new(1,2,3)
91
+ path.as4byte=true
92
+ assert_equal('40020e0203000000010000000200000003', path.to_shex)
93
+ assert_equal('4002080203000100020003', path.to_shex(false))
94
+ path.as4byte=false
95
+ assert_equal('4002080203000100020003', path.to_shex)
96
+ assert_equal('40020e0203000000010000000200000003', path.to_shex(true))
97
+ end
98
+ def test_4
99
+ assert_equal('400200', As_path.new.to_shex)
100
+ path = As_path.new(1)
101
+ assert_equal("1", As_path.new(1).as_path)
102
+ assert_equal("1", As_path.new(As_path::Sequence.new(1)).as_path)
103
+ assert_equal("1", As_path.new(As_path::Segment.new(:sequence,1)).as_path)
104
+ assert_equal("1 2 3 4 5 6 7", As_path.new(1,2,3,4,5,6,7).as_path)
105
+ assert_equal("{1, 2, 3}", As_path.new(As_path::Set.new(1,2,3)).as_path)
106
+ assert_equal("(1 2 3)", As_path.new(As_path::Segment.new(:confed_sequence,1,2,3)).as_path)
107
+ assert_equal("[1, 2, 3]", As_path.new(As_path::Segment.new(:confed_set,1,2,3)).as_path)
108
+ assert_equal("[wTcr] (2) As Path: [400208040300010002000...] '[1, 2, 3]'", As_path.new(As_path::Segment.new(:confed_set,1,2,3)).to_s)
109
+ end
110
+ def test_5
111
+ two_byte_as = '4002080203000100020003'
112
+ four_byte_as = '40020e0203000000010000000200000003'
113
+ path = As_path.new([four_byte_as].pack('H*'),true)
114
+ assert_equal(two_byte_as, path.to_shex)
115
+ assert_equal(four_byte_as, path.to_shex(true))
116
+ path = As_path.new([two_byte_as].pack('H*'))
117
+ assert_equal(two_byte_as, path.to_shex)
118
+ assert_equal(four_byte_as, path.to_shex(true))
119
+ asp = As_path.new(['40020c020500010002000300040005c0080c051f00010137003b0af50040'].pack('H*'))
120
+ asp2 = As_path.new(asp)
121
+ assert_equal(asp.encode, asp2.encode)
122
+ end
123
+ end
124
+
125
+ class As4_path_Test < Test::Unit::TestCase
126
+ include BGP
127
+ def test_1
128
+ assert_equal('c01100', As4_path.new.to_shex)
129
+ path = As4_path.new(1)
130
+ assert_equal("c01106020100000001", As4_path.new(1).to_shex)
131
+ assert_equal("c01106020100000001", As4_path.new(As_path::Sequence.new(1)).to_shex)
132
+ assert_equal("c01106020100000001", As4_path.new(As_path::Segment.new(:sequence,1)).to_shex)
133
+ assert_equal("c0110e0203000000010000000200000003", As4_path.new(1,2,3).to_shex)
134
+ assert_equal("c0110e0203000000010000000200000003", As4_path.new(As_path::Sequence.new(1,2,3)).to_shex)
135
+ assert_equal("c0110e0203000000010000000200000003", As4_path.new(As_path::Segment.new(:sequence,1,2,3)).to_shex)
136
+ end
137
+ def test_2
138
+ path = As4_path.new(['c0110e0203000000010000000200000003'].pack('H*'))
139
+ assert_equal('c0110e0203000000010000000200000003', path.to_shex)
140
+ path << As_path::Set.new(1,2,3)
141
+ path << As_path::Confed_sequence.new(1,2,3)
142
+ assert_equal('c0112a020300000001000000020000000301030000000100000002000000030303000000010000000200000003', path.to_shex)
143
+ assert_equal('c0112a02030...', path.to_shex_len(10))
144
+ assert_equal('1 2 3 {1, 2, 3} (1 2 3)',path.as_path)
145
+ assert_equal("[OTcr] (17) As4 Path: [c0112a020300000001000...] '1 2 3 {1, 2, 3} (1 2 3)'",path.to_s)
146
+ end
147
+ end
148
+
149
+ #MiniTest::Unit.autorun
@@ -0,0 +1,35 @@
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/atomic_aggregate"
24
+
25
+ require 'test/unit'
26
+ class Atomic_aggregate_Test < Test::Unit::TestCase
27
+ include BGP
28
+ def test_1
29
+ assert_equal("800600", Atomic_aggregate.new.to_shex)
30
+ assert_equal("Atomic Aggregate (6), length: 0, Flags [O]: ", Atomic_aggregate.new.to_s)
31
+ obj = Atomic_aggregate.new
32
+ assert_equal("800600", Atomic_aggregate.new(obj.encode).to_shex)
33
+ end
34
+ end
35
+
@@ -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/common'
24
+ require 'bgp/path_attribute'
25
+ require 'test/unit'
26
+
27
+ class ATTR_Test < Test::Unit::TestCase
28
+ include BGP
29
+ class Attr_test < Attr
30
+ def initialize
31
+ @type, @flags = 0, 0
32
+ end
33
+ def encode
34
+ @type=0xee
35
+ @flags=0xf
36
+ super()
37
+ end
38
+ end
39
+ def test_1
40
+ assert_equal('f0ee00',Attr_test.new.to_shex)
41
+ attr = Attr_test.new
42
+ assert_equal(attr.encode4, attr.encode)
43
+ end
44
+ end
45
+ class Attr_factory_Test < Test::Unit::TestCase
46
+ include BGP
47
+ def test_1
48
+ s = ['400101014003040a000001800404000000644005040000006440020c020500010002000300040005c0080c051f00010137003b0af50040'].pack('H*')
49
+ assert_equal(Origin, BGP::Attr.factory(s).class)
50
+ assert_equal(Next_hop, BGP::Attr.factory(s).class)
51
+ assert_equal(Multi_exit_disc, BGP::Attr.factory(s).class)
52
+ assert_equal(Local_pref, BGP::Attr.factory(s).class)
53
+ assert_equal(As_path, BGP::Attr.factory(s).class)
54
+ assert_equal(Communities, BGP::Attr.factory(s).class)
55
+ assert_equal('',s)
56
+ end
57
+ end
@@ -0,0 +1,39 @@
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/cluster_list'
24
+ require 'test/unit'
25
+ class Cluster_list_Test < Test::Unit::TestCase
26
+ include BGP
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)
38
+ end
39
+ end
@@ -0,0 +1,68 @@
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/common"
24
+
25
+ require 'test/unit'
26
+ class Common_Test < Test::Unit::TestCase
27
+ def test_ipaddr_1
28
+ s = [1,2,3].pack('C*')
29
+ assert(s.is_packed?)
30
+ assert( ! "1234".is_packed?)
31
+ end
32
+ def test_ipaddr_2
33
+ ip = IPAddr.new('10.0.0.1')
34
+ assert_equal('0a000001', ip.to_shex)
35
+ assert_equal('0a000001', IPAddr.create(ip.encode).to_shex)
36
+ assert_equal('0a000001', IPAddr.create(ip.to_i).to_shex)
37
+ end
38
+ def test_ipaddr_3
39
+ assert_equal(32, IPAddr.new('10.0.0.1').mlen)
40
+ assert_equal(16, IPAddr.new('10.0.0.1/16').mlen)
41
+ assert_equal(7, IPAddr.new('10.0.0.0/7').mlen)
42
+ assert_equal(128, IPAddr.new('0::0').mlen)
43
+ assert_equal(96, IPAddr.new('2004::0001:3/96').mlen)
44
+ assert_equal('255.255.255.255', IPAddr.new('1.1.1.1').netmask)
45
+ assert_equal('255.255.255.255', IPAddr.new('1.1.1.1/32').netmask)
46
+ assert_equal('255.255.255.254', IPAddr.new('1.1.1.1/31').netmask)
47
+ assert_equal('255.255.255.252', IPAddr.new('1.1.1.1/30').netmask)
48
+ assert_equal('255.255.255.248', IPAddr.new('1.1.1.1/29').netmask)
49
+ end
50
+ def test_ipaddr_4
51
+ ip1 = IPAddr.new('10.0.0.1/28')
52
+ ip2 = IPAddr.new('10.0.0.1/24')
53
+ ip3 = IPAddr.new('10.0.0.1/12')
54
+ assert_equal('10.0.0.16/28', ip1 ^ 1)
55
+ assert_equal('10.0.0.32/28', ip1 ^ 2)
56
+ assert_equal('10.0.1.0/24', ip2 ^ 1)
57
+ assert_equal('10.0.2.0/24', ip2 ^ 2)
58
+ assert_equal('10.16.0.0/12', ip3 ^ 1)
59
+ assert_equal('10.32.0.0/12', ip3 ^ 2)
60
+ end
61
+ def test_string_1
62
+ sbin = ['0a000001'].pack('H*')
63
+ assert_equal('0x0000: 0a00 0001',sbin.hexlify.join)
64
+ end
65
+ def test_nlri
66
+ assert_equal('14000000', IPAddr.new_nlri4(['101400'].pack('H*')).to_shex)
67
+ end
68
+ end
@@ -0,0 +1,75 @@
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/communities'
24
+
25
+ require 'test/unit'
26
+ class Community_Test < Test::Unit::TestCase
27
+ include BGP
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)
33
+ assert_equal(0xdeadbeef,Communities::Community.new(0xdeadbeef).to_i)
34
+ end
35
+ end
36
+ class Communities_Test < Test::Unit::TestCase
37
+ include BGP
38
+ def test_1
39
+ assert_equal('113:10 113:20 113:30 113:40', Communities.new("113:10", "113:20", "113:30", "113:40").communities)
40
+ assert_equal('113:10 113:20 113:30 113:40', Communities.new("113:10 113:20 113:30 113:40").communities)
41
+ assert_equal('113:10 113:20 113:30 113:40', Communities.new("113:10, 113:20, 113:30, 113:40").communities)
42
+ assert_equal('113:20', Communities.new("113:20").communities)
43
+ com = Communities.new
44
+ com << '113:10'
45
+ assert_equal('113:10', com.communities)
46
+ com << '113:20'
47
+ assert_equal('113:10 113:20', com.communities)
48
+ com << '113:30'
49
+ assert_equal('113:10 113:20 113:30', com.communities)
50
+ assert_equal('c0080c0071000a007100140071001e', com.to_shex)
51
+ com1 = Communities.new("145:30", "145:40", "145:50", "145:60")
52
+ com2 = Communities.new("145:30,145:40,145:50,145:60")
53
+ assert_equal(com1.to_shex, com2.to_shex)
54
+ assert_equal(["0x91001e", "0x910028", "0x910032", "0x91003c"], com2.to_ary.collect { |c| "0x#{c.to_s(16)}" })
55
+ assert_equal('c008100091001e00910028009100320091003c',com1.to_shex)
56
+ com3 = Communities.new(0x91001E, 0x910028, 0x910032, 0x91003c)
57
+ assert_equal('c008100091001e00910028009100320091003c',com3.to_shex)
58
+ assert_equal('145:30 145:40 145:50 145:60', com3.communities)
59
+ assert_equal("[OTcr] (8) Communities: [c008100091001e0091002...] '145:30 145:40 145:50 145:60'", com3.to_s)
60
+ assert_equal(com3.to_shex, Communities.new(com3.encode).to_shex)
61
+ end
62
+ def test_2
63
+ com1 = Communities.new("145:60", "145:10", "145:30", "145:20")
64
+ com2 = Communities.new("145:30,145:20,145:10,145:60")
65
+ assert(com1 == com2)
66
+ com1.sort
67
+ assert_equal('145:60 145:10 145:30 145:20', com1.communities)
68
+ com1.sort!
69
+ assert_equal('145:10 145:20 145:30 145:60', com1.communities)
70
+ com3 = Communities.new(com1)
71
+ assert_equal(com1.encode, com3.encode)
72
+ end
73
+ end
74
+
75
+ #MiniTest::Unit.autorun