github.com/osrg/gobgp@v2.0.0+incompatible/test/scenario_test/bgp_malformed_msg_handling_test.py (about) 1 # Copyright (C) 2017 Nippon Telegraph and Telephone Corporation. 2 # 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 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 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 12 # implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 from __future__ import absolute_import 17 18 import sys 19 import time 20 import unittest 21 22 from fabric.api import local 23 import nose 24 25 from lib.noseplugin import OptionParser, parser_option 26 27 from lib import base 28 from lib.base import BGP_FSM_ESTABLISHED 29 from lib.gobgp import GoBGPContainer 30 from lib.exabgp import ExaBGPContainer 31 32 33 class GoBGPTestBase(unittest.TestCase): 34 35 @classmethod 36 def setUpClass(cls): 37 gobgp_ctn_image_name = parser_option.gobgp_image 38 base.TEST_PREFIX = parser_option.test_prefix 39 40 g1 = GoBGPContainer(name='g1', asn=65000, router_id='192.168.0.1', 41 ctn_image_name=gobgp_ctn_image_name, 42 log_level=parser_option.gobgp_log_level) 43 e1 = ExaBGPContainer(name='e1', asn=65001, router_id='192.168.0.2') 44 45 ctns = [g1, e1] 46 initial_wait_time = max(ctn.run() for ctn in ctns) 47 time.sleep(initial_wait_time) 48 49 g1.add_peer(e1, treat_as_withdraw=True) 50 e1.add_peer(g1) 51 52 cls.g1 = g1 53 cls.e1 = e1 54 55 # test each neighbor state is turned establish 56 def test_01_neighbor_established(self): 57 self.g1.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.e1) 58 59 def test_02_attribute_discard(self): 60 # Malformed attribute 'AGGREGATOR' should be discard, but the session should not be disconnected. 61 self.e1.add_route('10.0.0.0/24', attribute='0x07 0xc0 0x0000006400') 62 63 # Confirm the session is not disconnected 64 for _ in range(5): 65 state = self.g1.get_neighbor_state(self.e1) 66 self.assertTrue(BGP_FSM_ESTABLISHED, state) 67 time.sleep(1) 68 69 # Confirm the path is added 70 dests = self.g1.get_global_rib() 71 self.assertEqual(len(dests), 1) 72 routes = dests[0]['paths'] 73 self.assertEqual(len(routes), 1) 74 75 # Confirm the attribute 'AGGREGATOR(type=7)' is discarded 76 for d in routes[0]['attrs']: 77 self.assertFalse(d['type'] == 7) 78 79 self.e1.del_route('10.0.0.0/24') 80 81 def test_03_treat_as_withdraw(self): 82 # Malformed attribute 'MULTI_EXIT_DESC' should be treated as withdraw, 83 # but the session should not be disconnected. 84 self.e1.add_route('20.0.0.0/24', attribute='0x04 0x80 0x00000064') 85 self.e1.add_route('30.0.0.0/24', attribute='0x04 0x80 0x00000064') 86 # Malformed 87 self.e1.add_route('30.0.0.0/24', attribute='0x04 0x80 0x0000000064') 88 89 # Confirm the session is not disconnected 90 for _ in range(5): 91 state = self.g1.get_neighbor_state(self.e1) 92 self.assertTrue(BGP_FSM_ESTABLISHED, state) 93 time.sleep(1) 94 95 # Confirm the number of path in RIB is only one 96 dests = self.g1.get_global_rib() 97 self.assertEqual(len(dests), 1) 98 self.assertEqual(dests[0]['paths'][0]['nlri']['prefix'], '20.0.0.0/24') 99 100 101 if __name__ == '__main__': 102 output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True) 103 if int(output) is not 0: 104 print "docker not found" 105 sys.exit(1) 106 107 nose.main(argv=sys.argv, addplugins=[OptionParser()], 108 defaultTest=sys.argv[0])