blob: 5c697ee1e44ebb1b9f331d41f893e0ecb93660eb [file] [log] [blame]
Matus Fabianb4515b42018-11-19 04:25:32 -08001#!/usr/bin/env python
2
Paul Vinciguerradd3c5d22019-01-13 16:09:10 -08003import unittest
Matus Fabianb4515b42018-11-19 04:25:32 -08004from framework import VppTestCase, VppTestRunner
5from util import ppp
6from scapy.packet import Raw
7from scapy.layers.inet import IP, UDP
8from vpp_papi_provider import SYSLOG_SEVERITY
9from syslog_rfc5424_parser import SyslogMessage, ParseError
10from syslog_rfc5424_parser.constants import SyslogFacility, SyslogSeverity
11
12
13class TestSyslog(VppTestCase):
14 """ Syslog Protocol Test Cases """
15
16 @classmethod
17 def setUpClass(cls):
18 super(TestSyslog, cls).setUpClass()
19
20 try:
Paul Vinciguerrad7532092019-01-14 08:51:17 -080021 cls.pg0, = cls.create_pg_interfaces(range(1))
Matus Fabianb4515b42018-11-19 04:25:32 -080022 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 Vinciguerrad7532092019-01-14 08:51:17 -080038 :param msgid: message identifier
Matus Fabianb4515b42018-11-19 04:25:32 -080039 :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 Vinciguerrad7532092019-01-14 08:51:17 -080074 :param msgid: message identifier
Matus Fabianb4515b42018-11-19 04:25:32 -080075 :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 Vinciguerrad7532092019-01-14 08:51:17 -080083 except ParseError as e:
84 self.logger.error(e)
85 raise
86 else:
Matus Fabianb4515b42018-11-19 04:25:32 -080087 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 Fabianb4515b42018-11-19 04:25:32 -080095
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 Troan0bcad322018-12-11 13:04:01 +0100100 self.assertEqual(str(config.collector_address),
101 self.pg0.remote_ip4)
Matus Fabianb4515b42018-11-19 04:25:32 -0800102 self.assertEqual(config.collector_port, 514)
Ole Troan0bcad322018-12-11 13:04:01 +0100103 self.assertEqual(str(config.src_address), self.pg0.local_ip4)
Matus Fabianb4515b42018-11-19 04:25:32 -0800104 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 Vinciguerra776e46d2019-01-29 12:27:16 -0800132 self.logger.error(ppp("invalid packet:", capture[0]))
133 raise
Matus Fabianb4515b42018-11-19 04:25:32 -0800134 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 Vinciguerra776e46d2019-01-29 12:27:16 -0800187 self.logger.error(ppp("invalid packet:", capture[0]))
188 raise
Matus Fabianb4515b42018-11-19 04:25:32 -0800189 self.syslog_verify(capture[0][Raw].load,
190 SyslogFacility.local5,
191 SyslogSeverity.err,
192 appname,
193 msgid,
194 sd2,
195 None)
196
197
198if __name__ == '__main__':
199 unittest.main(testRunner=VppTestRunner)