Debian Codegraph @

src/journal/test-journald-syslog.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "journald-syslog.h"
5 #include "syslog-util.h"
6 #include "tests.h"
7
8 static void test_syslog_parse_identifier_one(
9 const char *str,
10 const char *ident,
11 pid_t pid,
12 const char *rest,
13 int ret) {
14
15 const char *buf = str;
16 _cleanup_free_ char *ident2 = NULL;
17 pid_t pid2;
18 int ret2;
19
20 ret2 = syslog_parse_identifier(&buf, &ident2, &pid2);
21
22 ASSERT_EQ(ret, ret2);
23 ASSERT_STREQ(ident, ident2);
24 ASSERT_EQ(pid, pid2);
25 ASSERT_STREQ(buf, rest);
26 }
27
28 static void test_syslog_parse_priority_one(const char *str, bool with_facility, int priority, int ret) {
29 int priority2 = 0, ret2;
30
31 ret2 = syslog_parse_priority(&str, &priority2, with_facility);
32
33 assert_se(ret == ret2);
34 if (ret2 == 1)
35 assert_se(priority == priority2);
36 }
37
38 TEST(syslog_parse_identifier) {
39 test_syslog_parse_identifier_one("pidu[111]: xxx", "pidu", 111, "xxx", 11);
40 test_syslog_parse_identifier_one("pidu: xxx", "pidu", 0, "xxx", 6);
41 test_syslog_parse_identifier_one("pidu: xxx", "pidu", 0, " xxx", 6);
42 test_syslog_parse_identifier_one("pidu xxx", NULL, 0, "pidu xxx", 0);
43 test_syslog_parse_identifier_one(" pidu xxx", NULL, 0, " pidu xxx", 0);
44 test_syslog_parse_identifier_one("", NULL, 0, "", 0);
45 test_syslog_parse_identifier_one(" ", NULL, 0, " ", 0);
46 test_syslog_parse_identifier_one(":", "", 0, "", 1);
47 test_syslog_parse_identifier_one(": ", "", 0, " ", 2);
48 test_syslog_parse_identifier_one(" :", "", 0, "", 2);
49 test_syslog_parse_identifier_one(" pidu:", "pidu", 0, "", 8);
50 test_syslog_parse_identifier_one("pidu:", "pidu", 0, "", 5);
51 test_syslog_parse_identifier_one("pidu: ", "pidu", 0, "", 6);
52 test_syslog_parse_identifier_one("pidu : ", NULL, 0, "pidu : ", 0);
53 }
54
55 TEST(syslog_parse_priority) {
56 test_syslog_parse_priority_one("", false, 0, 0);
57 test_syslog_parse_priority_one("<>", false, 0, 0);
58 test_syslog_parse_priority_one("<>aaa", false, 0, 0);
59 test_syslog_parse_priority_one("<aaaa>", false, 0, 0);
60 test_syslog_parse_priority_one("<aaaa>aaa", false, 0, 0);
61 test_syslog_parse_priority_one(" <aaaa>", false, 0, 0);
62 test_syslog_parse_priority_one(" <aaaa>aaa", false, 0, 0);
63 test_syslog_parse_priority_one(" <aaaa>aaa", false, 0, 0);
64 test_syslog_parse_priority_one(" <1>", false, 0, 0);
65 test_syslog_parse_priority_one("<1>", false, 1, 1);
66 test_syslog_parse_priority_one("<7>", false, 7, 1);
67 test_syslog_parse_priority_one("<8>", false, 0, 0);
68 test_syslog_parse_priority_one("<9>", true, 9, 1);
69 test_syslog_parse_priority_one("<22>", true, 22, 1);
70 test_syslog_parse_priority_one("<111>", false, 0, 0);
71 test_syslog_parse_priority_one("<111>", true, 111, 1);
72 }
73
74 DEFINE_TEST_MAIN(LOG_INFO);