Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 2 | |
Paul Vinciguerra | dd3c5d2 | 2019-01-13 16:09:10 -0800 | [diff] [blame] | 3 | import unittest |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 4 | from framework import VppTestCase, VppTestRunner |
| 5 | from util import ppp |
| 6 | from scapy.packet import Raw |
| 7 | from scapy.layers.inet import IP, UDP |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 8 | from syslog_rfc5424_parser import SyslogMessage, ParseError |
| 9 | from syslog_rfc5424_parser.constants import SyslogFacility, SyslogSeverity |
Paul Vinciguerra | b765820 | 2019-05-17 09:48:15 -0400 | [diff] [blame] | 10 | from vpp_papi import VppEnum |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 11 | |
| 12 | |
| 13 | class TestSyslog(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 14 | """Syslog Protocol Test Cases""" |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 15 | |
Paul Vinciguerra | b765820 | 2019-05-17 09:48:15 -0400 | [diff] [blame] | 16 | @property |
| 17 | def SYSLOG_SEVERITY(self): |
| 18 | return VppEnum.vl_api_syslog_severity_t |
| 19 | |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 20 | @classmethod |
| 21 | def setUpClass(cls): |
| 22 | super(TestSyslog, cls).setUpClass() |
| 23 | |
| 24 | try: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 25 | (cls.pg0,) = cls.create_pg_interfaces(range(1)) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 26 | cls.pg0.admin_up() |
| 27 | cls.pg0.config_ip4() |
| 28 | cls.pg0.resolve_arp() |
| 29 | |
| 30 | except Exception: |
| 31 | super(TestSyslog, cls).tearDownClass() |
| 32 | raise |
| 33 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 34 | @classmethod |
| 35 | def tearDownClass(cls): |
| 36 | super(TestSyslog, cls).tearDownClass() |
| 37 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 38 | def syslog_generate(self, facility, severity, appname, msgid, sd=None, msg=None): |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 39 | """ |
| 40 | Generate syslog message |
| 41 | |
| 42 | :param facility: facility value |
| 43 | :param severity: severity level |
| 44 | :param appname: application name that originate message |
Paul Vinciguerra | d753209 | 2019-01-14 08:51:17 -0800 | [diff] [blame] | 45 | :param msgid: message identifier |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 46 | :param sd: structured data (optional) |
| 47 | :param msg: free-form message (optional) |
| 48 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 49 | facility_str = [ |
| 50 | "kernel", |
| 51 | "user-level", |
| 52 | "mail-system", |
| 53 | "system-daemons", |
| 54 | "security-authorization", |
| 55 | "syslogd", |
| 56 | "line-printer", |
| 57 | "network-news", |
| 58 | "uucp", |
| 59 | "clock-daemon", |
| 60 | "", |
| 61 | "ftp-daemon", |
| 62 | "ntp-subsystem", |
| 63 | "log-audit", |
| 64 | "log-alert", |
| 65 | "", |
| 66 | "local0", |
| 67 | "local1", |
| 68 | "local2", |
| 69 | "local3", |
| 70 | "local4", |
| 71 | "local5", |
| 72 | "local6", |
| 73 | "local7", |
| 74 | ] |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 75 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 76 | severity_str = [ |
| 77 | "emergency", |
| 78 | "alert", |
| 79 | "critical", |
| 80 | "error", |
| 81 | "warning", |
| 82 | "notice", |
| 83 | "informational", |
| 84 | "debug", |
| 85 | ] |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 86 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 87 | cli_str = "test syslog %s %s %s %s" % ( |
| 88 | facility_str[facility], |
| 89 | severity_str[severity], |
| 90 | appname, |
| 91 | msgid, |
| 92 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 93 | if sd is not None: |
| 94 | for sd_id, sd_params in sd.items(): |
| 95 | cli_str += " sd-id %s" % (sd_id) |
| 96 | for name, value in sd_params.items(): |
| 97 | cli_str += " sd-param %s %s" % (name, value) |
| 98 | if msg is not None: |
| 99 | cli_str += " %s" % (msg) |
| 100 | self.vapi.cli(cli_str) |
| 101 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 102 | def syslog_verify( |
| 103 | self, data, facility, severity, appname, msgid, sd=None, msg=None |
| 104 | ): |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 105 | """ |
| 106 | Verify syslog message |
| 107 | |
| 108 | :param data: syslog message |
| 109 | :param facility: facility value |
| 110 | :param severity: severity level |
| 111 | :param appname: application name that originate message |
Paul Vinciguerra | d753209 | 2019-01-14 08:51:17 -0800 | [diff] [blame] | 112 | :param msgid: message identifier |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 113 | :param sd: structured data (optional) |
| 114 | :param msg: free-form message (optional) |
| 115 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 116 | message = data.decode("utf-8") |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 117 | if sd is None: |
| 118 | sd = {} |
| 119 | try: |
| 120 | message = SyslogMessage.parse(message) |
Paul Vinciguerra | d753209 | 2019-01-14 08:51:17 -0800 | [diff] [blame] | 121 | except ParseError as e: |
| 122 | self.logger.error(e) |
| 123 | raise |
| 124 | else: |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 125 | self.assertEqual(message.facility, facility) |
| 126 | self.assertEqual(message.severity, severity) |
| 127 | self.assertEqual(message.appname, appname) |
| 128 | self.assertEqual(message.msgid, msgid) |
| 129 | self.assertEqual(message.msg, msg) |
| 130 | self.assertEqual(message.sd, sd) |
| 131 | self.assertEqual(message.version, 1) |
| 132 | self.assertEqual(message.hostname, self.pg0.local_ip4) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 133 | |
| 134 | def test_syslog(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 135 | """Syslog Protocol test""" |
| 136 | self.vapi.syslog_set_sender( |
| 137 | src_address=self.pg0.local_ip4, collector_address=self.pg0.remote_ip4 |
| 138 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 139 | config = self.vapi.syslog_get_sender() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 140 | self.assertEqual(str(config.collector_address), self.pg0.remote_ip4) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 141 | self.assertEqual(config.collector_port, 514) |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 142 | self.assertEqual(str(config.src_address), self.pg0.local_ip4) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 143 | self.assertEqual(config.vrf_id, 0) |
| 144 | self.assertEqual(config.max_msg_size, 480) |
| 145 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 146 | appname = "test" |
| 147 | msgid = "testMsg" |
| 148 | msg = "this is message" |
| 149 | sd1 = { |
| 150 | "exampleSDID@32473": {"iut": "3", "eventSource": "App", "eventID": "1011"} |
| 151 | } |
| 152 | sd2 = { |
| 153 | "exampleSDID@32473": {"iut": "3", "eventSource": "App", "eventID": "1011"}, |
| 154 | "examplePriority@32473": {"class": "high"}, |
| 155 | } |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 156 | |
| 157 | self.pg_enable_capture(self.pg_interfaces) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 158 | self.syslog_generate( |
| 159 | SyslogFacility.local7, SyslogSeverity.info, appname, msgid, None, msg |
| 160 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 161 | capture = self.pg0.get_capture(1) |
| 162 | try: |
| 163 | self.assertEqual(capture[0][IP].src, self.pg0.local_ip4) |
| 164 | self.assertEqual(capture[0][IP].dst, self.pg0.remote_ip4) |
| 165 | self.assertEqual(capture[0][UDP].dport, 514) |
| 166 | self.assert_packet_checksums_valid(capture[0], False) |
| 167 | except: |
Paul Vinciguerra | 776e46d | 2019-01-29 12:27:16 -0800 | [diff] [blame] | 168 | self.logger.error(ppp("invalid packet:", capture[0])) |
| 169 | raise |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 170 | self.syslog_verify( |
| 171 | capture[0][Raw].load, |
| 172 | SyslogFacility.local7, |
| 173 | SyslogSeverity.info, |
| 174 | appname, |
| 175 | msgid, |
| 176 | None, |
| 177 | msg, |
| 178 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 179 | |
| 180 | self.pg_enable_capture(self.pg_interfaces) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 181 | self.vapi.syslog_set_filter(self.SYSLOG_SEVERITY.SYSLOG_API_SEVERITY_WARN) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 182 | filter = self.vapi.syslog_get_filter() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 183 | self.assertEqual(filter.severity, self.SYSLOG_SEVERITY.SYSLOG_API_SEVERITY_WARN) |
| 184 | self.syslog_generate( |
| 185 | SyslogFacility.local7, SyslogSeverity.info, appname, msgid, None, msg |
| 186 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 187 | self.pg0.assert_nothing_captured() |
| 188 | |
| 189 | self.pg_enable_capture(self.pg_interfaces) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 190 | self.syslog_generate( |
| 191 | SyslogFacility.local6, SyslogSeverity.warning, appname, msgid, sd1, msg |
| 192 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 193 | capture = self.pg0.get_capture(1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 194 | self.syslog_verify( |
| 195 | capture[0][Raw].load, |
| 196 | SyslogFacility.local6, |
| 197 | SyslogSeverity.warning, |
| 198 | appname, |
| 199 | msgid, |
| 200 | sd1, |
| 201 | msg, |
| 202 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 203 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 204 | self.vapi.syslog_set_sender( |
| 205 | self.pg0.local_ip4, self.pg0.remote_ip4, collector_port=12345 |
| 206 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 207 | config = self.vapi.syslog_get_sender() |
| 208 | self.assertEqual(config.collector_port, 12345) |
| 209 | |
| 210 | self.pg_enable_capture(self.pg_interfaces) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 211 | self.syslog_generate( |
| 212 | SyslogFacility.local5, SyslogSeverity.err, appname, msgid, sd2, None |
| 213 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 214 | capture = self.pg0.get_capture(1) |
| 215 | try: |
| 216 | self.assertEqual(capture[0][UDP].dport, 12345) |
| 217 | except: |
Paul Vinciguerra | 776e46d | 2019-01-29 12:27:16 -0800 | [diff] [blame] | 218 | self.logger.error(ppp("invalid packet:", capture[0])) |
| 219 | raise |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 220 | self.syslog_verify( |
| 221 | capture[0][Raw].load, |
| 222 | SyslogFacility.local5, |
| 223 | SyslogSeverity.err, |
| 224 | appname, |
| 225 | msgid, |
| 226 | sd2, |
| 227 | None, |
| 228 | ) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 229 | |
| 230 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 231 | if __name__ == "__main__": |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 232 | unittest.main(testRunner=VppTestRunner) |