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