1 # Copyright (C) 2016 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 import mock # Python 2
21 from unittest import mock # Python 3
23 from nose.tools import eq_
25 from ryu.lib.packet import bgp
26 from ryu.services.protocols.bgp import peer
29 LOG = logging.getLogger(__name__)
32 class Test_Peer(unittest.TestCase):
34 Test case for peer.Peer
38 peer.Peer, '__init__', mock.MagicMock(return_value=None))
39 def _test_construct_as_path_attr(
40 self, input_as_path, input_as4_path, expected_as_path):
42 input_as_path_attr = bgp.BGPPathAttributeAsPath(input_as_path)
43 input_as4_path_attr = bgp.BGPPathAttributeAs4Path(input_as4_path)
44 _peer = peer.Peer(None, None, None, None, None)
47 output_as_path_attr = _peer._construct_as_path_attr(
48 input_as_path_attr, input_as4_path_attr)
50 eq_(bgp.BGP_ATTR_TYPE_AS_PATH, output_as_path_attr.type)
51 eq_(expected_as_path, output_as_path_attr.path_seg_list)
53 def test_construct_as_path_attr_sequence_only(self):
56 input_as_path = [[65000, 4000, 23456, 23456, 40001]]
57 input_as4_path = [[400000, 300000, 40001]]
59 expected_as_path = [[65000, 4000, 400000, 300000, 40001]]
61 self._test_construct_as_path_attr(
62 input_as_path, input_as4_path, expected_as_path)
64 def test_construct_as_path_attr_aggregated_as_path_1(self):
67 input_as_path = [[65000, 4000], {10, 20, 30}, [23456, 23456, 40001]]
68 input_as4_path = [[400000, 300000, 40001]]
70 expected_as_path = [[65000, 4000], {10, 20, 30}, [400000, 300000, 40001]]
72 self._test_construct_as_path_attr(
73 input_as_path, input_as4_path, expected_as_path)
75 def test_construct_as_path_attr_aggregated_as_path_2(self):
78 input_as_path = [[65000, 4000], {10, 20, 30}, [23456, 23456, 40001]]
79 input_as4_path = [[3000, 400000, 300000, 40001]]
81 expected_as_path = [[65000, 4000, 3000, 400000, 300000, 40001]]
83 self._test_construct_as_path_attr(
84 input_as_path, input_as4_path, expected_as_path)
86 def test_construct_as_path_attr_aggregated_path_3(self):
89 input_as_path = [[65000, 4000, 23456, 23456, 40001]]
90 input_as4_path = [[400000, 300000, 40001], {10, 20, 30}]
92 expected_as_path = [[65000, 400000, 300000, 40001], {10, 20, 30}]
94 self._test_construct_as_path_attr(
95 input_as_path, input_as4_path, expected_as_path)
97 def test_construct_as_path_attr_aggregated_as4_path(self):
100 input_as_path = [[65000, 4000, 23456, 23456, 40001]]
101 input_as4_path = [{10, 20, 30}, [400000, 300000, 40001]]
103 expected_as_path = [[65000], {10, 20, 30}, [400000, 300000, 40001]]
105 self._test_construct_as_path_attr(
106 input_as_path, input_as4_path, expected_as_path)
108 def test_construct_as_path_attr_too_short_as_path(self):
111 input_as_path = [[65000, 4000, 23456, 23456, 40001]]
112 input_as4_path = [[100000, 65000, 4000, 400000, 300000, 40001]]
114 expected_as_path = [[65000, 4000, 23456, 23456, 40001]]
116 self._test_construct_as_path_attr(
117 input_as_path, input_as4_path, expected_as_path)
119 def test_construct_as_path_attr_too_short_as4_path(self):
122 input_as_path = [[65000, 4000, 23456, 23456, 40001]]
123 input_as4_path = [[300000, 40001]]
125 expected_as_path = [[65000, 4000, 23456, 300000, 40001]]
127 self._test_construct_as_path_attr(
128 input_as_path, input_as4_path, expected_as_path)
130 def test_construct_as_path_attr_empty_as4_path(self):
133 input_as_path = [[65000, 4000, 23456, 23456, 40001]]
134 input_as4_path = [[]]
136 expected_as_path = [[65000, 4000, 23456, 23456, 40001]]
138 self._test_construct_as_path_attr(
139 input_as_path, input_as4_path, expected_as_path)
142 peer.Peer, '__init__', mock.MagicMock(return_value=None))
143 def test_construct_as_path_attr_as4_path_None(self):
146 input_as_path = [[65000, 4000, 23456, 23456, 40001]]
147 # input_as4_path = None
149 expected_as_path = [[65000, 4000, 23456, 23456, 40001]]
152 input_as_path_attr = bgp.BGPPathAttributeAsPath(input_as_path)
153 input_as4_path_attr = None
154 _peer = peer.Peer(None, None, None, None, None)
157 output_as_path_attr = _peer._construct_as_path_attr(
158 input_as_path_attr, input_as4_path_attr)
160 eq_(bgp.BGP_ATTR_TYPE_AS_PATH, output_as_path_attr.type)
161 eq_(expected_as_path, output_as_path_attr.path_seg_list)
164 peer.Peer, '__init__', mock.MagicMock(return_value=None))
165 def _test_trans_as_path(
166 self, input_as_path, expected_as_path, expected_as4_path):
168 _peer = peer.Peer(None, None, None, None, None)
171 output_as_path, output_as4_path = _peer._trans_as_path(input_as_path)
173 eq_(expected_as_path, output_as_path)
174 eq_(expected_as4_path, output_as4_path)
177 peer.Peer, 'is_four_octet_as_number_cap_valid',
178 mock.MagicMock(return_value=True))
179 def test_trans_as_path_as4_path_is_supported(self):
182 input_as_path = [[65000, 4000, 400000, 300000, 40001]]
184 expected_as_path = [[65000, 4000, 400000, 300000, 40001]]
185 expected_as4_path = None
187 self._test_trans_as_path(
188 input_as_path, expected_as_path, expected_as4_path)
191 peer.Peer, 'is_four_octet_as_number_cap_valid',
192 mock.MagicMock(return_value=False))
193 def test_trans_as_path_sequence_only(self):
196 input_as_path = [[65000, 4000, 400000, 300000, 40001]]
198 expected_as_path = [[65000, 4000, 23456, 23456, 40001]]
199 expected_as4_path = [[65000, 4000, 400000, 300000, 40001]]
201 self._test_trans_as_path(
202 input_as_path, expected_as_path, expected_as4_path)
205 peer.Peer, 'is_four_octet_as_number_cap_valid',
206 mock.MagicMock(return_value=False))
207 def test_trans_as_path_no_trans(self):
210 input_as_path = [[65000, 4000, 40000, 30000, 40001]]
212 expected_as_path = [[65000, 4000, 40000, 30000, 40001]]
213 expected_as4_path = None
215 self._test_trans_as_path(
216 input_as_path, expected_as_path, expected_as4_path)
219 peer.Peer, '__init__', mock.MagicMock(return_value=None))
220 def _test_extract_and_reconstruct_as_path(
221 self, path_attributes, ex_as_path_value,
222 ex_aggregator_as_number, ex_aggregator_addr):
224 update_msg = bgp.BGPUpdate(path_attributes=path_attributes)
225 _peer = peer.Peer(None, None, None, None, None)
228 _peer._extract_and_reconstruct_as_path(update_msg)
230 umsg_pattrs = update_msg.pathattr_map
231 as_path_attr = umsg_pattrs.get(
232 bgp.BGP_ATTR_TYPE_AS_PATH, None)
233 as4_path_attr = umsg_pattrs.get(
234 bgp.BGP_ATTR_TYPE_AS4_PATH, None)
235 aggregator_attr = umsg_pattrs.get(
236 bgp.BGP_ATTR_TYPE_AGGREGATOR, None)
237 as4_aggregator_attr = umsg_pattrs.get(
238 bgp.BGP_ATTR_TYPE_AS4_AGGREGATOR, None)
240 eq_(ex_as_path_value, as_path_attr.value)
241 eq_(None, as4_path_attr)
242 eq_(ex_aggregator_as_number, aggregator_attr.as_number)
243 eq_(ex_aggregator_addr, aggregator_attr.addr)
244 eq_(None, as4_aggregator_attr)
247 peer.Peer, '__init__', mock.MagicMock(return_value=None))
248 def test_extract_and_reconstruct_as_path_with_no_as4_attr(self):
250 in_as_path_value = [[1000, 2000, 3000]]
252 in_aggregator_as_number = 4000
253 in_aggregator_addr = '10.0.0.1'
254 # in_as4_aggregator_as_number
255 # in_as4_aggregator_addr
258 ex_as_path_value = [[1000, 2000, 3000]]
259 ex_aggregator_as_number = 4000
260 ex_aggregator_addr = '10.0.0.1'
264 bgp.BGPPathAttributeAsPath(
265 value=in_as_path_value),
266 bgp.BGPPathAttributeAggregator(
267 as_number=in_aggregator_as_number, addr=in_aggregator_addr),
271 self._test_extract_and_reconstruct_as_path(
272 path_attributes, ex_as_path_value,
273 ex_aggregator_as_number, ex_aggregator_addr)
276 peer.Peer, '__init__', mock.MagicMock(return_value=None))
277 def test_extract_and_reconstruct_as_path_with_as4_attr(self):
279 in_as_path_value = [[1000, 23456, 3000]]
280 in_as4_path_value = [[2000, 3000]]
281 in_aggregator_as_number = 23456
282 in_aggregator_addr = '10.0.0.1'
283 in_as4_aggregator_as_number = 4000
284 in_as4_aggregator_addr = '10.0.0.1'
287 ex_as_path_value = [[1000, 2000, 3000]]
288 ex_aggregator_as_number = 4000
289 ex_aggregator_addr = '10.0.0.1'
293 bgp.BGPPathAttributeAsPath(
294 value=in_as_path_value),
295 bgp.BGPPathAttributeAs4Path(
296 value=in_as4_path_value),
297 bgp.BGPPathAttributeAggregator(
298 as_number=in_aggregator_as_number,
299 addr=in_aggregator_addr),
300 bgp.BGPPathAttributeAs4Aggregator(
301 as_number=in_as4_aggregator_as_number,
302 addr=in_as4_aggregator_addr),
306 self._test_extract_and_reconstruct_as_path(
307 path_attributes, ex_as_path_value,
308 ex_aggregator_as_number, ex_aggregator_addr)
311 peer.Peer, '__init__', mock.MagicMock(return_value=None))
312 def test_extract_and_reconstruct_as_path_with_not_trans_as_aggr(self):
314 in_as_path_value = [[1000, 23456, 3000]]
315 in_as4_path_value = [[2000, 3000]]
316 in_aggregator_as_number = 4000 # not AS_TRANS
317 in_aggregator_addr = '10.0.0.1'
318 in_as4_aggregator_as_number = 4000
319 in_as4_aggregator_addr = '10.0.0.1'
322 ex_as_path_value = [[1000, 23456, 3000]]
323 ex_aggregator_as_number = 4000
324 ex_aggregator_addr = '10.0.0.1'
328 bgp.BGPPathAttributeAsPath(
329 value=in_as_path_value),
330 bgp.BGPPathAttributeAs4Path(
331 value=in_as4_path_value),
332 bgp.BGPPathAttributeAggregator(
333 as_number=in_aggregator_as_number,
334 addr=in_aggregator_addr),
335 bgp.BGPPathAttributeAs4Aggregator(
336 as_number=in_as4_aggregator_as_number,
337 addr=in_as4_aggregator_addr),
341 self._test_extract_and_reconstruct_as_path(
342 path_attributes, ex_as_path_value,
343 ex_aggregator_as_number, ex_aggregator_addr)
346 peer.Peer, '__init__', mock.MagicMock(return_value=None))
347 def test_extract_and_reconstruct_as_path_with_short_as_path(self):
349 in_as_path_value = [[1000, 23456, 3000]]
350 in_as4_path_value = [[2000, 3000, 4000, 5000]] # longer than AS_PATH
351 in_aggregator_as_number = 4000
352 in_aggregator_addr = '10.0.0.1'
353 # in_as4_aggregator_as_number
354 # in_as4_aggregator_addr
357 ex_as_path_value = [[1000, 23456, 3000]]
358 ex_aggregator_as_number = 4000
359 ex_aggregator_addr = '10.0.0.1'
363 bgp.BGPPathAttributeAsPath(
364 value=in_as_path_value),
365 bgp.BGPPathAttributeAs4Path(
366 value=in_as4_path_value),
367 bgp.BGPPathAttributeAggregator(
368 as_number=in_aggregator_as_number,
369 addr=in_aggregator_addr),
373 self._test_extract_and_reconstruct_as_path(
374 path_attributes, ex_as_path_value,
375 ex_aggregator_as_number, ex_aggregator_addr)