Debian Codegraph @

src/libsystemd/sd-journal/test-journal-send.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "sd-journal.h"
7
8 #include "fileio.h"
9 #include "journal-send.h"
10 #include "tests.h"
11
12 TEST(journal_print) {
13 ASSERT_OK_ZERO(sd_journal_print(LOG_INFO, "XXX"));
14 ASSERT_OK_ZERO(sd_journal_print(LOG_INFO, "%s", "YYY"));
15 ASSERT_OK_ZERO(sd_journal_print(LOG_INFO, "X%4094sY", "ZZZ"));
16 ASSERT_OK_ZERO(sd_journal_print(LOG_INFO, "X%*sY", (int) LONG_LINE_MAX - 8 - 3, "ZZZ"));
17 ASSERT_ERROR(sd_journal_print(LOG_INFO, "X%*sY", (int) LONG_LINE_MAX - 8 - 2, "ZZZ"), ENOBUFS);
18 }
19
20 TEST(journal_send) {
21 _cleanup_free_ char *huge = NULL;
22
23 #define HUGE_SIZE (4096*1024)
24 ASSERT_NOT_NULL(huge = malloc(HUGE_SIZE));
25
26 /* utf-8 and non-utf-8, message-less and message-ful iovecs */
27 struct iovec graph1[] = {
28 {(char*) "GRAPH=graph", STRLEN("GRAPH=graph")}
29 };
30 struct iovec graph2[] = {
31 {(char*) "GRAPH=graph\n", STRLEN("GRAPH=graph\n")}
32 };
33 struct iovec message1[] = {
34 {(char*) "MESSAGE=graph", STRLEN("MESSAGE=graph")}
35 };
36 struct iovec message2[] = {
37 {(char*) "MESSAGE=graph\n", STRLEN("MESSAGE=graph\n")}
38 };
39
40 ASSERT_OK_ZERO(sd_journal_print(LOG_INFO, "piepapo"));
41
42 ASSERT_OK_ZERO(sd_journal_send("MESSAGE=foobar",
43 "VALUE=%i", 7,
44 NULL));
45
46 errno = ENOENT;
47 ASSERT_OK_ZERO(sd_journal_perror("Foobar"));
48
49 ASSERT_OK_ZERO(sd_journal_perror(""));
50
51 memcpy(huge, "HUGE=", STRLEN("HUGE="));
52 memset(&huge[STRLEN("HUGE=")], 'x', HUGE_SIZE - STRLEN("HUGE=") - 1);
53 huge[HUGE_SIZE - 1] = '\0';
54
55 ASSERT_OK_ZERO(sd_journal_send("MESSAGE=Huge field attached",
56 huge,
57 NULL));
58
59 ASSERT_OK_ZERO(sd_journal_send("MESSAGE=uiui",
60 "VALUE=A",
61 "VALUE=B",
62 "VALUE=C",
63 "SINGLETON=1",
64 "OTHERVALUE=X",
65 "OTHERVALUE=Y",
66 "WITH_BINARY=this is a binary value \a",
67 NULL));
68
69 syslog(LOG_NOTICE, "Hello World!");
70
71 ASSERT_OK_ZERO(sd_journal_print(LOG_NOTICE, "Hello World"));
72
73 ASSERT_OK_ZERO(sd_journal_send("MESSAGE=Hello World!",
74 "MESSAGE_ID=52fb62f99e2c49d89cfbf9d6de5e3555",
75 "PRIORITY=5",
76 "HOME=%s", getenv("HOME"),
77 "TERM=%s", getenv("TERM"),
78 "PAGE_SIZE=%li", sysconf(_SC_PAGESIZE),
79 "N_CPUS=%li", sysconf(_SC_NPROCESSORS_ONLN),
80 NULL));
81
82 ASSERT_OK_ZERO(sd_journal_sendv(graph1, 1));
83 ASSERT_OK_ZERO(sd_journal_sendv(graph2, 1));
84 ASSERT_OK_ZERO(sd_journal_sendv(message1, 1));
85 ASSERT_OK_ZERO(sd_journal_sendv(message2, 1));
86
87 /* test without location fields */
88 #undef sd_journal_sendv
89 ASSERT_OK_ZERO(sd_journal_sendv(graph1, 1));
90 ASSERT_OK_ZERO(sd_journal_sendv(graph2, 1));
91 ASSERT_OK_ZERO(sd_journal_sendv(message1, 1));
92 ASSERT_OK_ZERO(sd_journal_sendv(message2, 1));
93
94 /* The above syslog() opens a fd which is stored in libc, and the valgrind reports the fd is
95 * leaked when we do not call closelog(). */
96 closelog();
97 }
98
99 static int outro(void) {
100 /* Sleep a bit to make it easy for journald to collect metadata. */
101 sleep(1);
102
103 close_journal_fd();
104
105 return EXIT_SUCCESS;
106 }
107
108 DEFINE_TEST_MAIN_FULL(LOG_INFO, NULL, outro);