3 from framework import VppTestCase, VppTestRunner
5 from scapy.packet import Raw
6 from scapy.layers.inet import IP, UDP
7 from vpp_papi_provider import SYSLOG_SEVERITY
8 from syslog_rfc5424_parser import SyslogMessage, ParseError
9 from syslog_rfc5424_parser.constants import SyslogFacility, SyslogSeverity
12 class TestSyslog(VppTestCase):
13 """ Syslog Protocol Test Cases """
17 super(TestSyslog, cls).setUpClass()
20 cls.create_pg_interfaces(range(1))
26 super(TestSyslog, cls).tearDownClass()
29 def syslog_generate(self, facility, severity, appname, msgid, sd=None,
32 Generate syslog message
34 :param facility: facility value
35 :param severity: severity level
36 :param appname: application name that originate message
37 :param msgid: message indetifier
38 :param sd: structured data (optional)
39 :param msg: free-form message (optional)
41 facility_str = ['kernel', 'user-level', 'mail-system',
42 'system-daemons', 'security-authorization', 'syslogd',
43 'line-printer', 'network-news', 'uucp', 'clock-daemon',
44 '', 'ftp-daemon', 'ntp-subsystem', 'log-audit',
45 'log-alert', '', 'local0', 'local1', 'local2',
46 'local3', 'local4', 'local5', 'local6', 'local7']
48 severity_str = ['emergency', 'alert', 'critical', 'error', 'warning',
49 'notice', 'informational', 'debug']
51 cli_str = "test syslog %s %s %s %s" % (facility_str[facility],
52 severity_str[severity],
56 for sd_id, sd_params in sd.items():
57 cli_str += " sd-id %s" % (sd_id)
58 for name, value in sd_params.items():
59 cli_str += " sd-param %s %s" % (name, value)
61 cli_str += " %s" % (msg)
62 self.vapi.cli(cli_str)
64 def syslog_verify(self, data, facility, severity, appname, msgid, sd=None,
69 :param data: syslog message
70 :param facility: facility value
71 :param severity: severity level
72 :param appname: application name that originate message
73 :param msgid: message indetifier
74 :param sd: structured data (optional)
75 :param msg: free-form message (optional)
77 message = data.decode('utf-8')
81 message = SyslogMessage.parse(message)
82 self.assertEqual(message.facility, facility)
83 self.assertEqual(message.severity, severity)
84 self.assertEqual(message.appname, appname)
85 self.assertEqual(message.msgid, msgid)
86 self.assertEqual(message.msg, msg)
87 self.assertEqual(message.sd, sd)
88 self.assertEqual(message.version, 1)
89 self.assertEqual(message.hostname, self.pg0.local_ip4)
90 except ParseError as e:
93 def test_syslog(self):
94 """ Syslog Protocol test """
95 self.vapi.syslog_set_sender(self.pg0.remote_ip4n, self.pg0.local_ip4n)
96 config = self.vapi.syslog_get_sender()
97 self.assertEqual(config.collector_address.address,
99 self.assertEqual(config.collector_port, 514)
100 self.assertEqual(config.src_address.address, self.pg0.local_ip4n)
101 self.assertEqual(config.vrf_id, 0)
102 self.assertEqual(config.max_msg_size, 480)
106 msg = 'this is message'
107 sd1 = {'exampleSDID@32473': {'iut': '3',
108 'eventSource': 'App',
110 sd2 = {'exampleSDID@32473': {'iut': '3',
111 'eventSource': 'App',
113 'examplePriority@32473': {'class': 'high'}}
115 self.pg_enable_capture(self.pg_interfaces)
116 self.syslog_generate(SyslogFacility.local7,
122 capture = self.pg0.get_capture(1)
124 self.assertEqual(capture[0][IP].src, self.pg0.local_ip4)
125 self.assertEqual(capture[0][IP].dst, self.pg0.remote_ip4)
126 self.assertEqual(capture[0][UDP].dport, 514)
127 self.assert_packet_checksums_valid(capture[0], False)
129 self.logger.error(ppp("invalid packet:", capture[0]))
131 self.syslog_verify(capture[0][Raw].load,
132 SyslogFacility.local7,
139 self.pg_enable_capture(self.pg_interfaces)
140 self.vapi.syslog_set_filter(SYSLOG_SEVERITY.WARN)
141 filter = self.vapi.syslog_get_filter()
142 self.assertEqual(filter.severity, SYSLOG_SEVERITY.WARN)
143 self.syslog_generate(SyslogFacility.local7,
149 self.pg0.assert_nothing_captured()
151 self.pg_enable_capture(self.pg_interfaces)
152 self.syslog_generate(SyslogFacility.local6,
153 SyslogSeverity.warning,
158 capture = self.pg0.get_capture(1)
159 self.syslog_verify(capture[0][Raw].load,
160 SyslogFacility.local6,
161 SyslogSeverity.warning,
167 self.vapi.syslog_set_sender(self.pg0.remote_ip4n,
169 collector_port=12345)
170 config = self.vapi.syslog_get_sender()
171 self.assertEqual(config.collector_port, 12345)
173 self.pg_enable_capture(self.pg_interfaces)
174 self.syslog_generate(SyslogFacility.local5,
180 capture = self.pg0.get_capture(1)
182 self.assertEqual(capture[0][UDP].dport, 12345)
184 self.logger.error(ppp("invalid packet:", capture[0]))
186 self.syslog_verify(capture[0][Raw].load,
187 SyslogFacility.local5,
195 if __name__ == '__main__':
196 unittest.main(testRunner=VppTestRunner)