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