]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal-syslog.c
journal: do not remove multiple spaces after identifier in syslog message
[thirdparty/systemd.git] / src / journal / test-journal-syslog.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
9 static void test_syslog_parse_identifier(const char *str,
10 const char *ident, const char *pid, const char *rest, int ret) {
11 const char *buf = str;
12 _cleanup_free_ char *ident2 = NULL, *pid2 = NULL;
13 int ret2;
14
15 ret2 = syslog_parse_identifier(&buf, &ident2, &pid2);
16
17 assert_se(ret == ret2);
18 assert_se(ident == ident2 || streq_ptr(ident, ident2));
19 assert_se(pid == pid2 || streq_ptr(pid, pid2));
20 assert_se(streq(buf, rest));
21 }
22
23 static void test_syslog_parse_priority(const char *str, int priority, int ret) {
24 const char *buf = str;
25 int priority2, ret2;
26
27 ret2 = syslog_parse_priority(&buf, &priority2, false);
28
29 assert_se(ret == ret2);
30 if (ret2 == 1)
31 assert_se(priority == priority2);
32 }
33
34 int main(void) {
35 test_syslog_parse_identifier("pidu[111]: xxx", "pidu", "111", "xxx", 11);
36 test_syslog_parse_identifier("pidu: xxx", "pidu", NULL, "xxx", 6);
37 test_syslog_parse_identifier("pidu: xxx", "pidu", NULL, " xxx", 6);
38 test_syslog_parse_identifier("pidu xxx", NULL, NULL, "pidu xxx", 0);
39 test_syslog_parse_identifier(" pidu xxx", NULL, NULL, " pidu xxx", 0);
40 test_syslog_parse_identifier("", NULL, NULL, "", 0);
41 test_syslog_parse_identifier(" ", NULL, NULL, " ", 0);
42 test_syslog_parse_identifier(":", "", NULL, "", 1);
43 test_syslog_parse_identifier(": ", "", NULL, " ", 2);
44 test_syslog_parse_identifier("pidu:", "pidu", NULL, "", 5);
45 test_syslog_parse_identifier("pidu: ", "pidu", NULL, "", 6);
46 test_syslog_parse_identifier("pidu : ", NULL, NULL, "pidu : ", 0);
47
48 test_syslog_parse_priority("<>", 0, 0);
49 test_syslog_parse_priority("<>aaa", 0, 0);
50 test_syslog_parse_priority("<aaaa>", 0, 0);
51 test_syslog_parse_priority("<aaaa>aaa", 0, 0);
52 test_syslog_parse_priority(" <aaaa>", 0, 0);
53 test_syslog_parse_priority(" <aaaa>aaa", 0, 0);
54 /* TODO: add test cases of valid priorities */
55
56 return 0;
57 }