bgp4r 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/README.rdoc +1 -10
  2. data/bgp/io.rb +4 -2
  3. data/bgp/messages/capability.rb +176 -0
  4. data/bgp/messages/keepalive.rb +30 -0
  5. data/bgp/messages/markers.rb +10 -0
  6. data/bgp/messages/message.rb +85 -0
  7. data/bgp/messages/messages.rb +17 -0
  8. data/bgp/messages/notification.rb +116 -0
  9. data/bgp/messages/open.rb +117 -0
  10. data/bgp/messages/route_refresh.rb +181 -0
  11. data/bgp/messages/update.rb +148 -0
  12. data/bgp/neighbor.rb +20 -11
  13. data/bgp/nlris/inet.rb +18 -0
  14. data/bgp/{label.rb → nlris/label.rb} +1 -1
  15. data/bgp/nlris/labeled.rb +39 -0
  16. data/bgp/nlris/nlri.rb +154 -0
  17. data/bgp/{attributes.rb → nlris/nlris.rb} +11 -13
  18. data/bgp/nlris/prefix.rb +33 -0
  19. data/bgp/{rd.rb → nlris/rd.rb} +1 -1
  20. data/bgp/nlris/vpn.rb +68 -0
  21. data/bgp/optional_parameters/as4.rb +59 -0
  22. data/bgp/optional_parameters/capabilities.rb +13 -0
  23. data/bgp/optional_parameters/capability.rb +145 -0
  24. data/bgp/optional_parameters/dynamic.rb +37 -0
  25. data/bgp/optional_parameters/graceful_restart.rb +116 -0
  26. data/bgp/optional_parameters/mbgp.rb +97 -0
  27. data/bgp/optional_parameters/optional_parameter.rb +76 -0
  28. data/bgp/optional_parameters/orf.rb +90 -0
  29. data/bgp/optional_parameters/route_refresh.rb +36 -0
  30. data/bgp/{prefix_orf.rb → orfs/prefix_orf.rb} +4 -10
  31. data/bgp/{aggregator.rb → path_attributes/aggregator.rb} +2 -2
  32. data/bgp/{as_path.rb → path_attributes/as_path.rb} +2 -2
  33. data/bgp/{atomic_aggregate.rb → path_attributes/atomic_aggregate.rb} +2 -2
  34. data/bgp/{attribute.rb → path_attributes/attribute.rb} +2 -2
  35. data/bgp/path_attributes/attributes.rb +21 -0
  36. data/bgp/{cluster_list.rb → path_attributes/cluster_list.rb} +2 -2
  37. data/bgp/{communities.rb → path_attributes/communities.rb} +2 -2
  38. data/bgp/{extended_communities.rb → path_attributes/extended_communities.rb} +3 -3
  39. data/bgp/{extended_community.rb → path_attributes/extended_community.rb} +1 -1
  40. data/bgp/{local_pref.rb → path_attributes/local_pref.rb} +2 -2
  41. data/bgp/{mp_reach.rb → path_attributes/mp_reach.rb} +3 -69
  42. data/bgp/path_attributes/mp_unreach.rb +97 -0
  43. data/bgp/{multi_exit_disc.rb → path_attributes/multi_exit_disc.rb} +2 -2
  44. data/bgp/{next_hop.rb → path_attributes/next_hop.rb} +2 -2
  45. data/bgp/{origin.rb → path_attributes/origin.rb} +2 -2
  46. data/bgp/{originator_id.rb → path_attributes/originator_id.rb} +2 -2
  47. data/bgp/{path_attribute.rb → path_attributes/path_attribute.rb} +6 -7
  48. data/bgp4r.gemspec +127 -67
  49. data/bgp4r.rb +5 -25
  50. data/examples/routegen +1 -1
  51. data/examples/routegen.yml +21 -5
  52. data/examples/simple.rb +4 -4
  53. data/examples/unit-testing/malformed_update.rb +1 -1
  54. data/examples/unit-testing/unknown_transitive_attr.rb +18 -10
  55. data/test/messages/capability_test.rb +113 -0
  56. data/test/messages/keepalive_test.rb +34 -0
  57. data/test/messages/markers_test.rb +33 -0
  58. data/test/messages/message_test.rb +43 -0
  59. data/test/messages/notification_test.rb +49 -0
  60. data/test/messages/open_test.rb +98 -0
  61. data/test/messages/route_refresh_test.rb +77 -0
  62. data/test/messages/update_test.rb +133 -0
  63. data/test/neighbor_test.rb +8 -9
  64. data/test/nlris/inet_test.rb +43 -0
  65. data/test/{label_test.rb → nlris/labeled_test.rb} +1 -1
  66. data/test/{nlri_test.rb → nlris/nlri_test.rb} +5 -1
  67. data/test/{rd_test.rb → nlris/rd_test.rb} +1 -2
  68. data/test/optional_parameters/as4_test.rb +34 -0
  69. data/test/optional_parameters/capability_test.rb +69 -0
  70. data/test/optional_parameters/dynamic_test.rb +34 -0
  71. data/test/optional_parameters/graceful_restart_test.rb +23 -0
  72. data/test/optional_parameters/mbgp_test.rb +35 -0
  73. data/test/optional_parameters/optional_parameter_test.rb +51 -0
  74. data/test/optional_parameters/orf_test.rb +42 -0
  75. data/test/optional_parameters/route_refresh_test.rb +32 -0
  76. data/test/{prefix_orf_test.rb → orfs/prefix_orf_test.rb} +2 -1
  77. data/test/{aggregator_test.rb → path_attributes/aggregator_test.rb} +1 -1
  78. data/test/{as_path_test.rb → path_attributes/as_path_test.rb} +1 -1
  79. data/test/{atomic_aggregate_test.rb → path_attributes/atomic_aggregate_test.rb} +1 -1
  80. data/test/{attribute_test.rb → path_attributes/attribute_test.rb} +2 -2
  81. data/test/{cluster_list_test.rb → path_attributes/cluster_list_test.rb} +1 -1
  82. data/test/{communities_test.rb → path_attributes/communities_test.rb} +1 -1
  83. data/test/{extended_communities_test.rb → path_attributes/extended_communities_test.rb} +1 -1
  84. data/test/{extended_community_test.rb → path_attributes/extended_community_test.rb} +1 -1
  85. data/test/{local_pref_test.rb → path_attributes/local_pref_test.rb} +1 -1
  86. data/test/{mp_reach_test.rb → path_attributes/mp_reach_test.rb} +1 -37
  87. data/test/path_attributes/mp_unreach_test.rb +67 -0
  88. data/test/{multi_exit_disc_test.rb → path_attributes/multi_exit_disc_test.rb} +1 -1
  89. data/test/{next_hop_test.rb → path_attributes/next_hop_test.rb} +1 -1
  90. data/test/{origin_test.rb → path_attributes/origin_test.rb} +2 -1
  91. data/test/{originator_id_test.rb → path_attributes/originator_id_test.rb} +1 -1
  92. data/test/{path_attribute_test.rb → path_attributes/path_attribute_test.rb} +2 -2
  93. metadata +122 -67
  94. data/bgp/message.rb +0 -921
  95. data/bgp/nlri.rb +0 -303
  96. data/bgp/version.rb +0 -3
  97. data/examples/unit-testing/keepalive_set_to_zeo.rb +0 -64
  98. data/test/message_test.rb +0 -347
  99. /data/bgp/{orf.rb → orfs/orf.rb} +0 -0
data/bgp/nlri.rb DELETED
@@ -1,303 +0,0 @@
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/common'
25
- require 'bgp/iana'
26
-
27
- module BGP
28
-
29
- class Base_nlri
30
-
31
- class Nlri_element < IPAddr
32
- def to_s
33
- [super, mlen].join('/')
34
- end
35
- def encode_next_hop
36
- hton
37
- end
38
- def nbyte
39
- (mlen+7)/8
40
- end
41
- def encode(len_included=true)
42
- nbyte = (mlen+7)/8
43
- if len_included
44
- [mlen, hton].pack("Ca#{nbyte}")
45
- else
46
- [hton].pack("a#{nbyte}")
47
- end
48
- end
49
- def parse4(arg)
50
- s = arg.dup
51
- s +=([0]*3).pack('C*')
52
- plen, *nlri = s.unpack('CC4')
53
- arg.slice!(0,1+(plen+7)/8) # trim arg accordingly
54
- ipaddr = nlri.collect { |n| n.to_s }.join('.') + "/" + plen .to_s
55
- end
56
- def parse6(arg)
57
- s = arg.dup
58
- s +=([0]*16).pack('C*')
59
- plen, *nlri = s.unpack('Cn8')
60
- arg.slice!(0,1+(plen+7)/8) # trim arg accordingly
61
- ipaddr = nlri.collect { |n| n.to_s(16) }.join(':') + "/" + plen .to_s
62
- end
63
- end
64
-
65
- class Ip4 < Nlri_element
66
- def initialize(arg)
67
- if arg.is_a?(String) and arg.packed?
68
- super(parse4(arg))
69
- elsif arg.is_a?(Ip4)
70
- super(arg.to_s)
71
- else
72
- super(arg)
73
- end
74
- end
75
- end
76
-
77
- class Ip6 < Nlri_element
78
- def initialize(arg)
79
- if arg.is_a?(String) and arg.packed?
80
- super(parse6(arg))
81
- elsif arg.is_a?(Ip6)
82
- super(arg.to_s)
83
- else
84
- super(arg)
85
- end
86
- end
87
- end
88
-
89
- attr_reader :nlris
90
-
91
- def initialize(*args)
92
- if args[0].is_a?(String) and args[0].is_packed?
93
- parse(args[0])
94
- else
95
- add(*args)
96
- end
97
- end
98
- def add(*args)
99
- @nlris ||=[]
100
- args.flatten.each { |arg| @nlris << Ip4.new(arg) }
101
- end
102
- alias << add
103
-
104
- def parse(s)
105
- @nlris ||=[]
106
- while s.size>0
107
- add(s)
108
- end
109
- end
110
-
111
- def encode(len_included=false)
112
- enc = @nlris.collect { |x| x.encode }.join
113
- if len_included
114
- [enc.size].pack('n') + enc
115
- else
116
- enc
117
- end
118
- end
119
-
120
- def to_s
121
- @nlris.join("\n")
122
- end
123
-
124
- def size
125
- @nlris.size
126
- end
127
-
128
- end
129
-
130
- class Nlri < Base_nlri
131
- def encode
132
- super
133
- end
134
- end
135
- class Withdrawn < Base_nlri
136
- def encode(len_included=true)
137
- super(len_included)
138
- end
139
- end
140
-
141
- class Nlri
142
- def self.factory(s, afi, safi)
143
- case safi
144
- when 1,2
145
- Prefix.new(s.is_packed, afi)
146
- when 4,128,129
147
- Labeled.new(s.is_packed, afi, safi)
148
- end
149
- end
150
- end
151
-
152
- class Prefix < Nlri::Nlri_element
153
- def initialize(*args)
154
- if args[0].is_a?(String) and args[0].packed?
155
- afi = args[1] ||=1
156
- case afi
157
- when :ip4,1 ; super(parse4(args[0]))
158
- when :ip6,2 ; super(parse6(args[0]))
159
- end
160
- elsif args[0].is_a?(Nlri::Ip4) or args[0].is_a?(Nlri::Ip6) or args[0].is_a?(Prefix)
161
- super(args[0].to_s)
162
- else
163
- super(*args)
164
- end
165
- end
166
- def afi
167
- if ipv4?
168
- IANA::AFI::IP
169
- elsif ipv6?
170
- IANA::AFI::IP6
171
- end
172
- end
173
- alias bit_length mlen
174
-
175
- def nexthop
176
- to_s.split('/')[0]
177
- end
178
-
179
- end
180
-
181
- class Inet_unicast < Prefix
182
- def safi
183
- IANA::SAFI::UNICAST_NLRI
184
- end
185
- end
186
-
187
- class Inet_multicast < Prefix
188
- def safi
189
- IANA::SAFI::MULTICAST_NLRI
190
- end
191
- end
192
-
193
- end
194
-
195
- require 'bgp/rd'
196
-
197
- module BGP
198
- class Vpn
199
- attr_reader :prefix, :rd
200
- def initialize(*args)
201
- if args.size>0 and args[0].is_a?(String) and args[0].is_packed?
202
- parse(*args)
203
- else
204
- prefix, *rd = args
205
- self.prefix=(prefix)
206
- self.rd=rd
207
- end
208
- end
209
- def prefix=(arg)
210
- if arg.is_a?(Prefix)
211
- @prefix=arg
212
- else
213
- @prefix=Prefix.new(arg)
214
- end
215
- end
216
- def rd=(args)
217
- args.flatten!
218
- if args.empty?
219
- @rd=Rd.new
220
- elsif args.size==1 and args[0].is_a?(BGP::Rd)
221
- @rd=args[0]
222
- else
223
- @rd=Rd.new(*args)
224
- end
225
- end
226
- # len_included arg is used by labeled encode()
227
- def encode_next_hop
228
- Rd.new.encode + @prefix.encode(false)
229
- end
230
- def encode(len_included=true)
231
- if len_included
232
- [bit_length, @rd.encode, @prefix.encode(false)].pack('Ca*a*')
233
- else
234
- @rd.encode + @prefix.encode(false)
235
- end
236
- end
237
- def bit_length
238
- @rd.bit_length + @prefix.mlen
239
- end
240
- def ipv4?
241
- @prefix.ipv4?
242
- end
243
- def afi
244
- @prefix.afi
245
- end
246
- def ipv6?
247
- @prefix.ipv6?
248
- end
249
- def parse(s, afi=1)
250
- nbits = s.slice!(0,1).unpack('C')[0]
251
- rd,vpn = s.slice!(0,(7+nbits)/8).unpack("a8a*")
252
- @rd = Rd.new(rd.is_packed)
253
- @prefix= Prefix.new([nbits-64,vpn].pack('Ca*'), afi)
254
- end
255
- def nexthop
256
- @prefix.nexthop
257
- end
258
- def to_s
259
- #Label Stack=5806 (bottom) RD=3215:317720610, IPv4=10.45.142.64/32
260
- "#{@rd.to_s(false)}, #{@prefix.ipv4? ? 'IPv4' : 'IPv6'}=#{@prefix.to_s}"
261
- end
262
- end
263
- end
264
-
265
- require 'bgp/label'
266
- module BGP
267
- class Labeled
268
- def initialize(*args)
269
- if args.size>0 and args[0].is_a?(String) and args[0].is_packed?
270
- parse(*args)
271
- else
272
- @prefix, *labels = args
273
- @labels = Label_stack.new(*labels)
274
- end
275
- end
276
- def bit_length
277
- @labels.bit_length+@prefix.bit_length
278
- end
279
- def encode
280
- [bit_length, @labels.encode, @prefix.encode(false)].pack('Ca*a*')
281
- end
282
- def to_s
283
- "#{@labels} #{@prefix}"
284
- end
285
- def afi
286
- @prefix.afi
287
- end
288
- def parse(s, afi=1, safi=1)
289
- bitlen = s.slice!(0,1).unpack('C')[0]
290
- @labels = Label_stack.new(s)
291
- mlen = bitlen - (24*@labels.size)
292
- prefix = [mlen,s.slice!(0,(mlen+7)/8)].pack("Ca*")
293
- case safi
294
- when 128,129
295
- @prefix = Vpn.new(prefix)
296
- else
297
- @prefix = Prefix.new(prefix, afi)
298
- end
299
- end
300
- end
301
- end
302
-
303
- load "../test/#{ File.basename($0.gsub(/.rb/,'_test.rb'))}" if __FILE__ == $0
data/bgp/version.rb DELETED
@@ -1,3 +0,0 @@
1
- module BGP
2
- VERSION = '0.0.6'
3
- end
@@ -1,64 +0,0 @@
1
- require "test/unit"
2
- require 'bgp4r'
3
- require 'timeout'
4
-
5
- Thread.abort_on_exception=true
6
-
7
- class TestBgp < Test::Unit::TestCase
8
-
9
- include BGP
10
-
11
- Log.create
12
- Log.level=Logger::DEBUG
13
-
14
- N100 = Class.new(BGP::Neighbor)
15
-
16
- class RecvMsgHandler
17
- def initialize(q)
18
- @q = q
19
- end
20
- def update(bgp_msg)
21
- @q.enq bgp_msg
22
- end
23
- end
24
-
25
- def setup
26
- @n100 = N100.new(:my_as=> 100, :remote_addr => '40.0.0.2', :local_addr => '40.0.0.1', :id=> '13.11.19.59', :holdtime=>0)
27
- start_peering
28
- end
29
-
30
- def test_verify_that_200_is_prepended_to_aspath
31
- assert @n100.is_established?, "Expected to be in Established state. <> #{@n100.state}"
32
- queue = Queue.new
33
- @n300.add_observer RecvMsgHandler.new(queue)
34
- end
35
-
36
- def teardown
37
- [@n100].each { |n| n.stop }
38
- sleep(0.5)
39
- end
40
-
41
- private
42
-
43
- def start_peering
44
- [@n100].each { |n|
45
- n.capability :as4_byte
46
- n.start
47
- }
48
- end
49
-
50
- def recv(q, timeout=5)
51
- begin
52
- Timeout::timeout(timeout) do |t|
53
- msg = q.deq
54
- end
55
- rescue Timeout::Error => e
56
- nil
57
- end
58
- end
59
-
60
- def send_update_to(n)
61
- n.send_message update1
62
- end
63
-
64
- end
data/test/message_test.rb DELETED
@@ -1,347 +0,0 @@
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
- def test_5
112
- an_update = Update.new( Path_attribute.new( Origin.new(1),
113
- Next_hop.new('10.0.0.1'),
114
- Multi_exit_disc.new(100)
115
- ))
116
- assert ! an_update.path_attribute.has?(Local_pref), "Should not contain a Local Pref attr."
117
- an_update << Local_pref.new(113)
118
- assert an_update.path_attribute.has?(Local_pref), "Should contain a Local Pref attr."
119
- assert_nil an_update.nlri
120
- an_update << '77.0.0.0/17'
121
- assert_equal Nlri, an_update.nlri.class
122
- assert_equal 1, an_update.nlri.size
123
- an_update << '88.0.0.0/18'
124
- assert_equal 2, an_update.nlri.size
125
- an_update << Nlri.new('21.0.0.0/11', '22.0.0.0/22')
126
- assert_equal 4, an_update.nlri.size
127
- end
128
-
129
- def test_6
130
- s = 'ffffffffffffffffffffffffffffffff004a02000000274001010040020a0202000000c80000006440030428000101c0080c0137003b051f00010af50040114d0000124e0000134f0000'
131
- m = Message.factory([s].pack('H*'), true)
132
- assert_not_nil m
133
- assert_instance_of(Update, m)
134
- assert m.as4byte?
135
- end
136
-
137
- def test_5
138
- # build an update from a yaml description
139
- # advertised 172.18.179.192/27
140
- # origin igp
141
- # nexthop 193.251.127.210
142
- # metric 0
143
- # locpref 310000
144
- # community 3215:102 3215:210 3215:522 3215:588 3215:903 3215:7553 3215:8000 3215:8003
145
- # originatorid 193.252.102.210
146
- # cluster 0.0.17.49 0.0.29.76 0.0.29.79
147
- require 'yaml'
148
- require 'pp'
149
- s = "
150
- ---
151
- prefixes:202.44.2.0/24 202.44.3.0/24 202.44.5.0/24 202.44.6.0/24 202.44.7.0/24
152
- origin: incomplete
153
- nexthop: 10.0.0.1
154
- metric: 0
155
- localpref: 13000
156
- community: 3230:10 3230:110 3230:411 3230:912 3230:1010 3230:5911
157
- originator-id: 10.0.0.2
158
- cluster: 0.0.0.1
159
- "
160
-
161
-
162
- end
163
-
164
- end
165
-
166
- class Open_Test < Test::Unit::TestCase
167
- include BGP
168
- include BGP::OPT_PARM
169
- def test_1
170
- s = "ffffffffffffffffffffffffffffffff001d0104006400c80a00000100"
171
- sbin = [s].pack('H*')
172
- assert_equal(Open, Message.factory(sbin).class)
173
- open = Message.factory(sbin)
174
- assert_equal(s, open.to_shex)
175
- assert_equal(s, Open.new(4,100, 200, '10.0.0.1').to_shex)
176
- assert_equal(s, Open.new(4,100, 200, '10.0.0.1', []).to_shex)
177
- assert_equal('00290104006400c80a0000010c020641040000006402020200', Open.new(4,100, 200, '10.0.0.1', As4_cap.new(100), Route_refresh_cap.new).to_shex[32..-1])
178
- open1 = Open.new(4,100, 200, '10.0.0.1', As4_cap.new(100), Route_refresh_cap.new)
179
- open2 = Open.new(open1.encode)
180
- assert_equal('00290104006400c80a0000010c020641040000006402020200', open2.to_shex[32..-1])
181
- open = Open.new(4,100, 200, '10.0.0.1')
182
- open << As4_cap.new(100)
183
- open << Route_refresh_cap.new
184
- assert_equal('00290104006400c80a0000010c020641040000006402020200', open.to_shex[32..-1])
185
- s = 'ffffffffffffffffffffffffffffffff002d0104626200b4513411091002060104000100800202800002020200'
186
- open = BGP::Message.factory([s].pack('H*'))
187
- assert_equal(s, open.to_shex)
188
-
189
- s = "ffff ffff ffff ffff ffff
190
- ffff ffff ffff 0038 0104 0023 00b4 1919
191
- 1901 1b02 0601 0400 0100 0102 0280 0002
192
- 0202 0002 0982 0700 0100 0101 8002".split.join
193
- open = BGP::Message.factory([s].pack('H*'))
194
- assert_equal(s, open.to_shex)
195
-
196
- s = "ffffffffffffffffffffffffffffffff003d0104006400b402020202200206010400010001020601040001000202028000020202000206410400000064"
197
- open = BGP::Message.factory([s].pack('H*'))
198
- assert_equal(4, open.version)
199
- assert_equal(100, open.local_as)
200
- assert_equal('2.2.2.2', open.bgp_id)
201
- assert_equal(180, open.holdtime)
202
- assert_equal(5, open.opt_parms.size)
203
- assert_equal(Mbgp_cap, open.opt_parms[0].class)
204
- assert_equal(Mbgp_cap, open.opt_parms[1].class)
205
- assert_equal(Route_refresh_cap, open.opt_parms[2].class)
206
- assert_equal(Route_refresh_cap, open.opt_parms[3].class)
207
- assert_equal(As4_cap, open.opt_parms[4].class)
208
-
209
- end
210
- def test_2
211
- s = "ffffffffffffffffffffffffffffffff003d0104006400b402020202200206010400010001020601040001000202028000020202000206410400000064"
212
- open = BGP::Message.factory([s].pack('H*'))
213
- assert_equal(5, open.to_hash[:capabilities].size )
214
- assert_equal(1, open.to_hash[:capabilities][0][:code])
215
- assert_equal({:safi=>1, :afi=>1}, open.to_hash[:capabilities][0][:capability])
216
- assert_equal(1, open.to_hash[:capabilities][1][:code])
217
- assert_equal({:safi=>2, :afi=>1}, open.to_hash[:capabilities][1][:capability])
218
- assert_equal(128, open.to_hash[:capabilities][2][:code])
219
- assert_nil(open.to_hash[:capabilities][2][:capability])
220
- assert_equal(2, open.to_hash[:capabilities][3][:code])
221
- assert_nil(open.to_hash[:capabilities][3][:capability])
222
- assert_equal(65, open.to_hash[:capabilities][4][:code])
223
- assert_equal(100,open.to_hash[:capabilities][4 ][:capability][:as])
224
- end
225
- end
226
-
227
- class Route_refresh_Test < Test::Unit::TestCase
228
- include BGP
229
- def test_1
230
- s = "ffffffffffffffffffffffffffffffff00170500010001"
231
- sbin = [s].pack('H*')
232
- assert_equal(Route_refresh, Message.factory(sbin).class)
233
- rr = Message.factory(sbin)
234
- assert_equal(s, rr.to_shex)
235
- rr.afi, rr.safi=2,2
236
- assert_equal(2, rr.afi)
237
- assert_equal(2, rr.safi)
238
- assert_equal('ffffffffffffffffffffffffffffffff00170500020002', rr.to_shex)
239
- assert_raise(ArgumentError) { rr.safi=0x100 }
240
- assert_raise(ArgumentError) { rr.afi=-1 }
241
- assert_equal(s, Route_refresh.new(1,1).to_shex)
242
- assert_equal(s, Route_refresh.new(:afi=>1, :safi=>1).to_shex)
243
- assert_equal({:afi=>1, :safi=>1}, Route_refresh.new(:afi=>1, :safi=>1).to_hash)
244
- assert_equal(s, Message.route_refresh(1,1).unpack('H*')[0])
245
- end
246
- end
247
-
248
-
249
- class Orf_route_refresh_Test < Test::Unit::TestCase
250
- include BGP
251
-
252
- def test_1
253
- rr = Orf_route_refresh.new
254
- orf = Prefix_orf.new([
255
- Prefix_entry.add_and_permit(10,'10.0.0.0/8'),
256
- Prefix_entry.add_and_permit(20,'20.0.0.0/8'),
257
- Prefix_entry.add_and_permit(30,'30.0.0.0/8'),
258
- Prefix_entry.add_and_permit(40,'40.0.0.0/8'),
259
- Prefix_entry.add_and_permit(50,'50.0.0.0/8'),
260
- ])
261
- rr << orf
262
- rr.afi=1
263
- rr.safi=1
264
- assert_equal('0047050001000140002d000000000a0000080a000000001400000814000000001e0000081e000000002800000828000000003200000832',rr.to_shex[32..-1])
265
- assert_equal('IPv4', rr.afi)
266
- assert_equal('Unicast', rr.safi)
267
- assert_equal(1, rr.orfs.size)
268
- assert_match(/RF Route Refresh \(5\), length: 71\nAFI IPv4 \(1\), SAFI Unicast \(1\)/, rr.to_s)
269
- end
270
-
271
- def test_2
272
- s = "ffffffffffffffffffffffffffffffff0047050001000140002d000000000a0000080a000000001400000814000000001e0000081e000000002800000828000000003200000832"
273
- sbin = [s].pack('H*')
274
- assert_equal(Orf_route_refresh, Message.factory(sbin).class)
275
- rr = Message.factory(sbin)
276
- assert_equal(s, rr.to_shex)
277
- end
278
- end
279
-
280
- class Notification_Test < Test::Unit::TestCase
281
- include BGP
282
- def test_1
283
- assert_equal('Undefined', Notification.code_to_s(0))
284
- assert_equal('Header Error', Notification.code_to_s(1))
285
- assert_equal('OPEN msg error', Notification.code_to_s(2))
286
- assert_equal('UPDATE msg error', Notification.code_to_s(3))
287
- assert_equal('Connection Not Synchronized', Notification.code_to_s(1,1))
288
- assert_equal('Unrecognized Well-known Attribute', Notification.code_to_s(3,2))
289
- notif = BGP::Notification.new(1,1)
290
- assert_equal('ffffffffffffffffffffffffffffffff0015030101', notif.to_shex)
291
- assert_equal(notif.encode, Notification.new(notif).encode)
292
- notif = BGP::Notification.new(2,2,'some data')
293
- assert_equal('ffffffffffffffffffffffffffffffff001e030202736f6d652064617461', notif.to_shex)
294
- assert_equal(notif.encode, Notification.new(notif).encode)
295
- s = 'ffffffffffffffffffffffffffffffff001e030202736f6d652064617461'
296
- m = Message.factory([s].pack('H*'))
297
- assert_equal(Notification, m.class)
298
- assert_equal(m.encode, Notification.new(m).encode)
299
- end
300
- end
301
-
302
- class As4_cap_Test < Test::Unit::TestCase
303
- include BGP
304
- def test_1
305
- cap1 = As4_cap.new(100)
306
- cap2 = As4_cap.new(['0206410400000064'].pack('H*'))
307
- cap3 = As4_cap.new(cap1.encode)
308
- assert_equal(cap2.encode, cap3.encode)
309
- end
310
- end
311
-
312
- class Mbgp_cap_Test < Test::Unit::TestCase
313
- include BGP
314
- def test_1
315
- mbgp1 = Mbgp_cap.new(1,1)
316
- mbgp2 = Mbgp_cap.new(['0206010400010001'].pack('H*'))
317
- mbgp3 = Mbgp_cap.new(mbgp1.encode)
318
- assert_equal(mbgp2.encode, mbgp3.encode)
319
- end
320
- end
321
-
322
- class Route_refresh_cap_Test < Test::Unit::TestCase
323
- include BGP
324
- def test_1
325
- assert_equal('02020200',Route_refresh_cap.new.to_shex)
326
- assert_equal('02028000',Route_refresh_cap.new(128).to_shex)
327
- end
328
- end
329
-
330
- class Orf_cap_Test < Test::Unit::TestCase
331
- include BGP
332
- def test_1
333
- ent1 = Orf_cap::Entry.new(1,1,[1,1],[2,1],[3,1])
334
- assert_equal('0001000103010102010301', ent1.to_shex)
335
- ent2 = Orf_cap::Entry.new(1,2,[1,1],[2,1],[3,1])
336
- assert_equal('0001000203010102010301', ent2.to_shex)
337
- ent3 = Orf_cap::Entry.new(ent1.encode)
338
- assert_equal(ent1.encode, ent3.encode)
339
- orf = Orf_cap.new
340
- orf.add(ent1)
341
- orf.add(ent2)
342
- assert_equal('0218031600010001030101020103010001000203010102010301', orf.to_shex)
343
- orf2 = Orf_cap.new(orf.encode)
344
- assert_equal(orf.encode, orf2.encode)
345
- end
346
-
347
- end