Dave Barach | ac0326f | 2020-07-14 18:30:05 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
| 4 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 5 | from asfframework import VppAsfTestCase, VppTestRunner |
Dave Barach | ac0326f | 2020-07-14 18:30:05 -0400 | [diff] [blame] | 6 | |
| 7 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 8 | class TestAdl(VppAsfTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 9 | """Allow/Deny Plugin Unit Test Cases""" |
Dave Barach | ac0326f | 2020-07-14 18:30:05 -0400 | [diff] [blame] | 10 | |
| 11 | @classmethod |
| 12 | def setUpClass(cls): |
| 13 | super(TestAdl, cls).setUpClass() |
| 14 | |
| 15 | @classmethod |
| 16 | def tearDownClass(cls): |
| 17 | super(TestAdl, cls).tearDownClass() |
| 18 | |
| 19 | def setUp(self): |
| 20 | super(TestAdl, self).setUp() |
| 21 | |
| 22 | def tearDown(self): |
| 23 | super(TestAdl, self).tearDown() |
| 24 | |
| 25 | def test_adl1_unittest(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 26 | """Plugin API Test""" |
| 27 | cmds = [ |
| 28 | "loop create\n", |
| 29 | "set int ip address loop0 192.168.1.1/24\n", |
| 30 | "set int ip6 table loop0 0\n", |
| 31 | "set int ip address loop0 2001:db01::1/64\n", |
| 32 | "set int state loop0 up\n", |
| 33 | "packet-generator new {\n" |
| 34 | " name ip4\n" |
| 35 | " limit 100\n" |
| 36 | " rate 0\n" |
| 37 | " size 128-128\n" |
| 38 | " interface loop0\n" |
| 39 | " node adl-input\n" |
| 40 | " data { IP4: 1.2.40 -> 3cfd.fed0.b6c8\n" |
| 41 | " UDP: 192.168.1.2-192.168.1.10 -> 192.168.2.1\n" |
| 42 | " UDP: 1234 -> 2345\n" |
| 43 | " incrementing 114\n" |
| 44 | " }\n" |
| 45 | " }\n", |
| 46 | "packet-generator new {\n" |
| 47 | " name ip6-allow\n" |
| 48 | " limit 50\n" |
| 49 | " rate 0\n" |
| 50 | " size 128-128\n" |
| 51 | " interface loop0\n" |
| 52 | " node adl-input\n" |
| 53 | " data { IP6: 1.2.40 -> 3cfd.fed0.b6c8\n" |
| 54 | " UDP: 2001:db01::2 -> 2001:db01::1\n" |
| 55 | " UDP: 1234 -> 2345\n" |
| 56 | " incrementing 80\n" |
| 57 | " }\n" |
| 58 | " }\n", |
| 59 | "packet-generator new {\n" |
| 60 | " name ip6-drop\n" |
| 61 | " limit 50\n" |
| 62 | " rate 0\n" |
| 63 | " size 128-128\n" |
| 64 | " interface loop0\n" |
| 65 | " node adl-input\n" |
| 66 | " data { IP6: 1.2.40 -> 3cfd.fed0.b6c8\n" |
| 67 | " UDP: 2001:db01::3 -> 2001:db01::1\n" |
| 68 | " UDP: 1234 -> 2345\n" |
| 69 | " incrementing 80\n" |
| 70 | " }\n" |
| 71 | " }\n", |
| 72 | "ip table 1\n", |
| 73 | "ip route add 192.168.2.1/32 via drop\n", |
| 74 | "ip route add table 1 192.168.1.2/32 via local\n", |
| 75 | "ip6 table 1\n", |
| 76 | "ip route add 2001:db01::1/128 via drop\n", |
| 77 | "ip route add table 1 2001:db01::2/128 via local\n", |
| 78 | "bin adl_interface_enable_disable loop0\n", |
| 79 | "bin adl_allowlist_enable_disable loop0 fib-id 1 ip4 ip6\n", |
| 80 | "pa en\n", |
| 81 | ] |
Dave Barach | ac0326f | 2020-07-14 18:30:05 -0400 | [diff] [blame] | 82 | |
| 83 | for cmd in cmds: |
| 84 | r = self.vapi.cli_return_response(cmd) |
| 85 | if r.retval != 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 86 | if hasattr(r, "reply"): |
Dave Barach | ac0326f | 2020-07-14 18:30:05 -0400 | [diff] [blame] | 87 | self.logger.info(cmd + " FAIL reply " + r.reply) |
| 88 | else: |
| 89 | self.logger.info(cmd + " FAIL retval " + str(r.retval)) |
| 90 | |
| 91 | total_pkts = self.statistics.get_err_counter( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 92 | "/err/adl-input/Allow/Deny packets processed" |
| 93 | ) |
Dave Barach | ac0326f | 2020-07-14 18:30:05 -0400 | [diff] [blame] | 94 | |
| 95 | self.assertEqual(total_pkts, 200) |
| 96 | |
| 97 | ip4_allow = self.statistics.get_err_counter( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 98 | "/err/ip4-adl-allowlist/ip4 allowlist allowed" |
| 99 | ) |
Dave Barach | ac0326f | 2020-07-14 18:30:05 -0400 | [diff] [blame] | 100 | self.assertEqual(ip4_allow, 12) |
| 101 | ip6_allow = self.statistics.get_err_counter( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 102 | "/err/ip6-adl-allowlist/ip6 allowlist allowed" |
| 103 | ) |
Dave Barach | ac0326f | 2020-07-14 18:30:05 -0400 | [diff] [blame] | 104 | self.assertEqual(ip6_allow, 50) |
| 105 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 106 | |
| 107 | if __name__ == "__main__": |
Dave Barach | ac0326f | 2020-07-14 18:30:05 -0400 | [diff] [blame] | 108 | unittest.main(testRunner=VppTestRunner) |