Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 1 | from asfframework import VppTestRunner |
| 2 | from framework import VppTestCase |
adrianvillin | 4450aed | 2023-10-19 12:15:52 +0200 | [diff] [blame] | 3 | import unittest |
| 4 | from config import config |
| 5 | |
| 6 | |
| 7 | @unittest.skipIf("snort" in config.excluded_plugins, "Exclude snort plugin test") |
| 8 | class TestSnort(VppTestCase): |
| 9 | """Simple Snort plugin test""" |
| 10 | |
| 11 | @classmethod |
| 12 | def setUpClass(cls): |
| 13 | super(TestSnort, cls).setUpClass() |
| 14 | try: |
| 15 | cls.create_pg_interfaces(range(2)) |
| 16 | for i in cls.pg_interfaces: |
| 17 | i.config_ip4().resolve_arp() |
| 18 | i.admin_up() |
| 19 | except Exception: |
| 20 | cls.tearDownClass() |
| 21 | raise |
| 22 | |
| 23 | @classmethod |
| 24 | def tearDownClass(cls): |
| 25 | for i in cls.pg_interfaces: |
| 26 | i.unconfig_ip4() |
| 27 | i.admin_down() |
| 28 | super(TestSnort, cls).tearDownClass() |
| 29 | |
| 30 | def test_snort_cli(self): |
| 31 | # TODO: add a test with packets |
| 32 | # { cli command : part of the expected reply } |
Alexander Skorichenko | e3ad5aa | 2024-09-12 09:32:46 +0200 | [diff] [blame] | 33 | print("TEST SNORT CLI") |
adrianvillin | 4450aed | 2023-10-19 12:15:52 +0200 | [diff] [blame] | 34 | commands_replies = { |
| 35 | "snort create-instance name snortTest queue-size 16 on-disconnect drop": "", |
| 36 | "snort create-instance name snortTest2 queue-size 16 on-disconnect pass": "", |
| 37 | "snort attach instance snortTest interface pg0 output": "", |
| 38 | "snort attach instance snortTest2 interface pg1 input": "", |
| 39 | "show snort instances": "snortTest", |
| 40 | "show snort interfaces": "pg0", |
| 41 | "show snort clients": "number of clients", |
| 42 | "show snort mode": "input mode: interrupt", |
| 43 | "snort mode polling": "", |
| 44 | "snort mode interrupt": "", |
| 45 | "snort detach interface pg0": "", |
| 46 | "snort detach interface pg1": "", |
Alexander Skorichenko | e3ad5aa | 2024-09-12 09:32:46 +0200 | [diff] [blame] | 47 | "snort delete instance snortTest": "", |
adrianvillin | 4450aed | 2023-10-19 12:15:52 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | for command, reply in commands_replies.items(): |
| 51 | actual_reply = self.vapi.cli(command) |
| 52 | self.assertIn(reply, actual_reply) |
| 53 | |
| 54 | |
Alexander Skorichenko | e3ad5aa | 2024-09-12 09:32:46 +0200 | [diff] [blame] | 55 | @unittest.skipIf("snort" in config.excluded_plugins, "Exclude snort plugin test") |
| 56 | class TestSnortVapi(VppTestCase): |
| 57 | """Snort plugin test [VAPI]""" |
| 58 | |
| 59 | @classmethod |
| 60 | def setUpClass(cls): |
| 61 | super(TestSnortVapi, cls).setUpClass() |
| 62 | try: |
| 63 | cls.create_pg_interfaces(range(2)) |
| 64 | for i in cls.pg_interfaces: |
| 65 | i.config_ip4() |
| 66 | i.resolve_arp() |
| 67 | i.admin_up() |
| 68 | except Exception: |
| 69 | cls.tearDownClass() |
| 70 | raise |
| 71 | |
| 72 | @classmethod |
| 73 | def tearDownClass(cls): |
| 74 | for i in cls.pg_interfaces: |
| 75 | i.unconfig_ip4() |
| 76 | i.admin_down() |
| 77 | super(TestSnortVapi, cls).tearDownClass() |
| 78 | |
| 79 | def test_snort_01_modes_set_interrupt(self): |
| 80 | """Set mode to interrupt""" |
| 81 | self.vapi.snort_input_mode_set(input_mode=1) |
| 82 | reply = self.vapi.snort_input_mode_get() |
| 83 | self.assertEqual(reply.snort_mode, 1) |
| 84 | reply = self.vapi.cli("show snort mode") |
| 85 | self.assertIn("interrupt", reply) |
| 86 | |
| 87 | def test_snort_02_modes_set_polling(self): |
| 88 | """Set mode to polling""" |
| 89 | self.vapi.snort_input_mode_set(input_mode=0) |
| 90 | reply = self.vapi.snort_input_mode_get() |
| 91 | self.assertEqual(reply.snort_mode, 0) |
| 92 | |
| 93 | def test_snort_03_create(self): |
| 94 | """Create two snort instances""" |
| 95 | reply = self.vapi.snort_instance_create( |
| 96 | queue_size=8, drop_on_disconnect=0, name="snortTest0" |
| 97 | ) |
| 98 | self.assertEqual(reply.instance_index, 0) |
| 99 | reply = self.vapi.snort_instance_create( |
| 100 | queue_size=32, drop_on_disconnect=1, name="snortTest1" |
| 101 | ) |
| 102 | self.assertEqual(reply.instance_index, 1) |
| 103 | reply = self.vapi.cli("show snort instances") |
| 104 | self.assertIn("snortTest0", reply) |
| 105 | self.assertIn("snortTest1", reply) |
| 106 | |
| 107 | def test_snort_04_attach_if(self): |
| 108 | """Interfaces can be attached""" |
| 109 | reply = self.vapi.snort_interface_attach( |
| 110 | instance_index=0, sw_if_index=1, snort_dir=1 |
| 111 | ) |
| 112 | try: |
| 113 | reply = self.vapi.snort_interface_attach( |
| 114 | instance_index=1, sw_if_index=1, snort_dir=1 |
| 115 | ) |
| 116 | except: |
| 117 | pass |
| 118 | else: |
| 119 | self.assertNotEqual(reply.retval, 0) |
| 120 | |
| 121 | reply = self.vapi.snort_interface_attach( |
| 122 | instance_index=1, sw_if_index=2, snort_dir=3 |
| 123 | ) |
| 124 | reply = self.vapi.cli("show snort interfaces") |
| 125 | self.assertIn("snortTest0", reply) |
| 126 | self.assertIn("snortTest1", reply) |
| 127 | |
| 128 | def test_snort_05_delete_instance(self): |
| 129 | """Instances can be deleted""" |
| 130 | reply = self.vapi.snort_instance_delete(instance_index=0) |
| 131 | reply = self.vapi.cli("show snort interfaces") |
| 132 | self.assertNotIn("snortTest0", reply) |
| 133 | self.assertIn("snortTest1", reply) |
| 134 | reply = self.vapi.cli("show snort interfaces") |
| 135 | self.assertNotIn("pg0", reply) |
| 136 | self.assertIn("pg1", reply) |
| 137 | |
| 138 | def test_snort_06_detach_if(self): |
| 139 | """Interfaces can be detached""" |
| 140 | try: |
| 141 | reply = self.vapi.snort_interface_detach(sw_if_index=1) |
| 142 | except: |
| 143 | pass |
| 144 | else: |
| 145 | self.assertNotEqual(reply.retval, 0) |
| 146 | reply = self.vapi.snort_interface_detach(sw_if_index=2) |
| 147 | reply = self.vapi.cli("show snort interfaces") |
| 148 | self.assertNotIn("pg1", reply) |
| 149 | |
| 150 | |
adrianvillin | 4450aed | 2023-10-19 12:15:52 +0200 | [diff] [blame] | 151 | if __name__ == "__main__": |
| 152 | unittest.main(testRunner=VppTestRunner) |