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