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