2019-10-31 13:31:07 -05:00
|
|
|
#!/usr/bin/env python3
|
2018-11-19 04:25:32 -08:00
|
|
|
|
2019-01-13 16:09:10 -08:00
|
|
|
import unittest
|
2023-08-31 00:47:44 -04:00
|
|
|
from framework import VppTestCase
|
|
|
|
from asfframework import VppTestRunner
|
2018-11-19 04:25:32 -08:00
|
|
|
from util import ppp
|
|
|
|
from scapy.packet import Raw
|
|
|
|
from scapy.layers.inet import IP, UDP
|
|
|
|
from syslog_rfc5424_parser import SyslogMessage, ParseError
|
|
|
|
from syslog_rfc5424_parser.constants import SyslogFacility, SyslogSeverity
|
2019-05-17 09:48:15 -04:00
|
|
|
from vpp_papi import VppEnum
|
2018-11-19 04:25:32 -08:00
|
|
|
|
|
|
|
|
|
|
|
class TestSyslog(VppTestCase):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Syslog Protocol Test Cases"""
|
2018-11-19 04:25:32 -08:00
|
|
|
|
2019-05-17 09:48:15 -04:00
|
|
|
@property
|
|
|
|
def SYSLOG_SEVERITY(self):
|
|
|
|
return VppEnum.vl_api_syslog_severity_t
|
|
|
|
|
2018-11-19 04:25:32 -08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(TestSyslog, cls).setUpClass()
|
|
|
|
|
|
|
|
try:
|
2022-04-26 19:02:15 +02:00
|
|
|
(cls.pg0,) = cls.create_pg_interfaces(range(1))
|
2018-11-19 04:25:32 -08:00
|
|
|
cls.pg0.admin_up()
|
|
|
|
cls.pg0.config_ip4()
|
|
|
|
cls.pg0.resolve_arp()
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
super(TestSyslog, cls).tearDownClass()
|
|
|
|
raise
|
|
|
|
|
2019-03-12 19:23:27 -07:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
super(TestSyslog, cls).tearDownClass()
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
def syslog_generate(self, facility, severity, appname, msgid, sd=None, msg=None):
|
2018-11-19 04:25:32 -08:00
|
|
|
"""
|
|
|
|
Generate syslog message
|
|
|
|
|
|
|
|
:param facility: facility value
|
|
|
|
:param severity: severity level
|
|
|
|
:param appname: application name that originate message
|
2019-01-14 08:51:17 -08:00
|
|
|
:param msgid: message identifier
|
2018-11-19 04:25:32 -08:00
|
|
|
:param sd: structured data (optional)
|
|
|
|
:param msg: free-form message (optional)
|
|
|
|
"""
|
2022-04-26 19:02:15 +02:00
|
|
|
facility_str = [
|
|
|
|
"kernel",
|
|
|
|
"user-level",
|
|
|
|
"mail-system",
|
|
|
|
"system-daemons",
|
|
|
|
"security-authorization",
|
|
|
|
"syslogd",
|
|
|
|
"line-printer",
|
|
|
|
"network-news",
|
|
|
|
"uucp",
|
|
|
|
"clock-daemon",
|
|
|
|
"",
|
|
|
|
"ftp-daemon",
|
|
|
|
"ntp-subsystem",
|
|
|
|
"log-audit",
|
|
|
|
"log-alert",
|
|
|
|
"",
|
|
|
|
"local0",
|
|
|
|
"local1",
|
|
|
|
"local2",
|
|
|
|
"local3",
|
|
|
|
"local4",
|
|
|
|
"local5",
|
|
|
|
"local6",
|
|
|
|
"local7",
|
|
|
|
]
|
|
|
|
|
|
|
|
severity_str = [
|
|
|
|
"emergency",
|
|
|
|
"alert",
|
|
|
|
"critical",
|
|
|
|
"error",
|
|
|
|
"warning",
|
|
|
|
"notice",
|
|
|
|
"informational",
|
|
|
|
"debug",
|
|
|
|
]
|
|
|
|
|
|
|
|
cli_str = "test syslog %s %s %s %s" % (
|
|
|
|
facility_str[facility],
|
|
|
|
severity_str[severity],
|
|
|
|
appname,
|
|
|
|
msgid,
|
|
|
|
)
|
2018-11-19 04:25:32 -08:00
|
|
|
if sd is not None:
|
|
|
|
for sd_id, sd_params in sd.items():
|
|
|
|
cli_str += " sd-id %s" % (sd_id)
|
|
|
|
for name, value in sd_params.items():
|
|
|
|
cli_str += " sd-param %s %s" % (name, value)
|
|
|
|
if msg is not None:
|
|
|
|
cli_str += " %s" % (msg)
|
|
|
|
self.vapi.cli(cli_str)
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
def syslog_verify(
|
|
|
|
self, data, facility, severity, appname, msgid, sd=None, msg=None
|
|
|
|
):
|
2018-11-19 04:25:32 -08:00
|
|
|
"""
|
|
|
|
Verify syslog message
|
|
|
|
|
|
|
|
:param data: syslog message
|
|
|
|
:param facility: facility value
|
|
|
|
:param severity: severity level
|
|
|
|
:param appname: application name that originate message
|
2019-01-14 08:51:17 -08:00
|
|
|
:param msgid: message identifier
|
2018-11-19 04:25:32 -08:00
|
|
|
:param sd: structured data (optional)
|
|
|
|
:param msg: free-form message (optional)
|
|
|
|
"""
|
2022-04-26 19:02:15 +02:00
|
|
|
message = data.decode("utf-8")
|
2018-11-19 04:25:32 -08:00
|
|
|
if sd is None:
|
|
|
|
sd = {}
|
|
|
|
try:
|
|
|
|
message = SyslogMessage.parse(message)
|
2019-01-14 08:51:17 -08:00
|
|
|
except ParseError as e:
|
|
|
|
self.logger.error(e)
|
|
|
|
raise
|
|
|
|
else:
|
2018-11-19 04:25:32 -08:00
|
|
|
self.assertEqual(message.facility, facility)
|
|
|
|
self.assertEqual(message.severity, severity)
|
|
|
|
self.assertEqual(message.appname, appname)
|
|
|
|
self.assertEqual(message.msgid, msgid)
|
|
|
|
self.assertEqual(message.msg, msg)
|
|
|
|
self.assertEqual(message.sd, sd)
|
|
|
|
self.assertEqual(message.version, 1)
|
|
|
|
self.assertEqual(message.hostname, self.pg0.local_ip4)
|
|
|
|
|
|
|
|
def test_syslog(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Syslog Protocol test"""
|
|
|
|
self.vapi.syslog_set_sender(
|
|
|
|
src_address=self.pg0.local_ip4, collector_address=self.pg0.remote_ip4
|
|
|
|
)
|
2018-11-19 04:25:32 -08:00
|
|
|
config = self.vapi.syslog_get_sender()
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assertEqual(str(config.collector_address), self.pg0.remote_ip4)
|
2018-11-19 04:25:32 -08:00
|
|
|
self.assertEqual(config.collector_port, 514)
|
2018-12-11 13:04:01 +01:00
|
|
|
self.assertEqual(str(config.src_address), self.pg0.local_ip4)
|
2018-11-19 04:25:32 -08:00
|
|
|
self.assertEqual(config.vrf_id, 0)
|
|
|
|
self.assertEqual(config.max_msg_size, 480)
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
appname = "test"
|
|
|
|
msgid = "testMsg"
|
|
|
|
msg = "this is message"
|
|
|
|
sd1 = {
|
|
|
|
"exampleSDID@32473": {"iut": "3", "eventSource": "App", "eventID": "1011"}
|
|
|
|
}
|
|
|
|
sd2 = {
|
|
|
|
"exampleSDID@32473": {"iut": "3", "eventSource": "App", "eventID": "1011"},
|
|
|
|
"examplePriority@32473": {"class": "high"},
|
|
|
|
}
|
2018-11-19 04:25:32 -08:00
|
|
|
|
|
|
|
self.pg_enable_capture(self.pg_interfaces)
|
2022-04-26 19:02:15 +02:00
|
|
|
self.syslog_generate(
|
|
|
|
SyslogFacility.local7, SyslogSeverity.info, appname, msgid, None, msg
|
|
|
|
)
|
2018-11-19 04:25:32 -08:00
|
|
|
capture = self.pg0.get_capture(1)
|
|
|
|
try:
|
|
|
|
self.assertEqual(capture[0][IP].src, self.pg0.local_ip4)
|
|
|
|
self.assertEqual(capture[0][IP].dst, self.pg0.remote_ip4)
|
|
|
|
self.assertEqual(capture[0][UDP].dport, 514)
|
|
|
|
self.assert_packet_checksums_valid(capture[0], False)
|
|
|
|
except:
|
2019-01-29 12:27:16 -08:00
|
|
|
self.logger.error(ppp("invalid packet:", capture[0]))
|
|
|
|
raise
|
2022-04-26 19:02:15 +02:00
|
|
|
self.syslog_verify(
|
|
|
|
capture[0][Raw].load,
|
|
|
|
SyslogFacility.local7,
|
|
|
|
SyslogSeverity.info,
|
|
|
|
appname,
|
|
|
|
msgid,
|
|
|
|
None,
|
|
|
|
msg,
|
|
|
|
)
|
2018-11-19 04:25:32 -08:00
|
|
|
|
|
|
|
self.pg_enable_capture(self.pg_interfaces)
|
2022-04-26 19:02:15 +02:00
|
|
|
self.vapi.syslog_set_filter(self.SYSLOG_SEVERITY.SYSLOG_API_SEVERITY_WARN)
|
2018-11-19 04:25:32 -08:00
|
|
|
filter = self.vapi.syslog_get_filter()
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assertEqual(filter.severity, self.SYSLOG_SEVERITY.SYSLOG_API_SEVERITY_WARN)
|
|
|
|
self.syslog_generate(
|
|
|
|
SyslogFacility.local7, SyslogSeverity.info, appname, msgid, None, msg
|
|
|
|
)
|
2018-11-19 04:25:32 -08:00
|
|
|
self.pg0.assert_nothing_captured()
|
|
|
|
|
|
|
|
self.pg_enable_capture(self.pg_interfaces)
|
2022-04-26 19:02:15 +02:00
|
|
|
self.syslog_generate(
|
|
|
|
SyslogFacility.local6, SyslogSeverity.warning, appname, msgid, sd1, msg
|
|
|
|
)
|
2018-11-19 04:25:32 -08:00
|
|
|
capture = self.pg0.get_capture(1)
|
2022-04-26 19:02:15 +02:00
|
|
|
self.syslog_verify(
|
|
|
|
capture[0][Raw].load,
|
|
|
|
SyslogFacility.local6,
|
|
|
|
SyslogSeverity.warning,
|
|
|
|
appname,
|
|
|
|
msgid,
|
|
|
|
sd1,
|
|
|
|
msg,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.vapi.syslog_set_sender(
|
|
|
|
self.pg0.local_ip4, self.pg0.remote_ip4, collector_port=12345
|
|
|
|
)
|
2018-11-19 04:25:32 -08:00
|
|
|
config = self.vapi.syslog_get_sender()
|
|
|
|
self.assertEqual(config.collector_port, 12345)
|
|
|
|
|
|
|
|
self.pg_enable_capture(self.pg_interfaces)
|
2022-04-26 19:02:15 +02:00
|
|
|
self.syslog_generate(
|
|
|
|
SyslogFacility.local5, SyslogSeverity.err, appname, msgid, sd2, None
|
|
|
|
)
|
2018-11-19 04:25:32 -08:00
|
|
|
capture = self.pg0.get_capture(1)
|
|
|
|
try:
|
|
|
|
self.assertEqual(capture[0][UDP].dport, 12345)
|
|
|
|
except:
|
2019-01-29 12:27:16 -08:00
|
|
|
self.logger.error(ppp("invalid packet:", capture[0]))
|
|
|
|
raise
|
2022-04-26 19:02:15 +02:00
|
|
|
self.syslog_verify(
|
|
|
|
capture[0][Raw].load,
|
|
|
|
SyslogFacility.local5,
|
|
|
|
SyslogSeverity.err,
|
|
|
|
appname,
|
|
|
|
msgid,
|
|
|
|
sd2,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-11-19 04:25:32 -08:00
|
|
|
unittest.main(testRunner=VppTestRunner)
|