From 4728476f7c8e6cb2ac3e840932aa41543fd9b592 Mon Sep 17 00:00:00 2001 From: Julian Rachele Date: Thu, 29 Oct 2020 14:46:55 -0400 Subject: [PATCH] Update cisco.py to handle using raw protocol numbers in policy #170 --- capirca/lib/cisco.py | 4 ++-- tests/lib/cisco_test.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/capirca/lib/cisco.py b/capirca/lib/cisco.py index 6aba58b1..07862f10 100644 --- a/capirca/lib/cisco.py +++ b/capirca/lib/cisco.py @@ -522,7 +522,7 @@ def __str__(self): elif self.term.protocol == ['hopopt']: protocol = ['hbh'] elif self.proto_int: - protocol = [proto if proto in self.ALLOWED_PROTO_STRINGS + protocol = [proto if proto in self.ALLOWED_PROTO_STRINGS or proto.isnumeric() else self.PROTO_MAP.get(proto) for proto in self.term.protocol] else: @@ -835,7 +835,7 @@ def __str__(self): protocol = ['ip'] else: - protocol = [proto if proto in self.ALLOWED_PROTO_STRINGS + protocol = [proto if proto in self.ALLOWED_PROTO_STRINGS or proto.isnumeric() else self.PROTO_MAP.get(proto) for proto in self.term.protocol] diff --git a/tests/lib/cisco_test.py b/tests/lib/cisco_test.py index 61a18bfc..9dd3fa1b 100644 --- a/tests/lib/cisco_test.py +++ b/tests/lib/cisco_test.py @@ -338,6 +338,12 @@ action:: accept } """ +GOOD_TERM_23 = """ +term good_term_23 { + protocol:: 50 + action:: accept +} +""" LONG_COMMENT_TERM = """ term long-comment-term { comment:: "%s " @@ -878,7 +884,6 @@ def testLongHeader(self): LONG_VERSION_HEADER + GOOD_TERM_7, self.naming) acl = cisco.Cisco(pol, EXP_INFO) - print(acl) self.assertIn('remark This long header should be split even on a', str(acl)) self.assertIn(('remark looooooooooooooooooooooooooonnnnnnnnnnnnnnnnnn' 'gggggggggg string.'), str(acl)) @@ -889,6 +894,13 @@ def testLongHeader(self): self.assertIn(('remark 5:0x169ef02a512c5b28!8m2!3d37.4220579!4d-122.084' '0897'), str(acl)) + def testProtocolByNumber(self): + """Test policy term refering to protocol by number""" + pol = policy.ParsePolicy( + GOOD_HEADER + GOOD_TERM_23, self.naming + ) + acl = cisco.Cisco(pol, EXP_INFO) + self.assertIn('permit 50 any any', str(acl)) if __name__ == '__main__': unittest.main()