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