]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/test-journal-stream.c
Merge pull request #17549 from yuwata/tiny-fixes
[thirdparty/systemd.git] / src / journal / test-journal-stream.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
cbdca852 2
cbdca852 3#include <fcntl.h>
6bedfcbb 4#include <unistd.h>
cbdca852 5
c6878637 6#include "sd-journal.h"
6bedfcbb 7
b5efdb8a 8#include "alloc-util.h"
949082ac 9#include "chattr-util.h"
d7a0f1f4 10#include "io-util.h"
6bedfcbb
LP
11#include "journal-file.h"
12#include "journal-internal.h"
cbdca852 13#include "log.h"
0c0cdb06 14#include "macro.h"
6bedfcbb 15#include "parse-util.h"
c6878637 16#include "rm-rf.h"
317bb217 17#include "tests.h"
6bedfcbb 18#include "util.h"
cbdca852
LP
19
20#define N_ENTRIES 200
21
22static void verify_contents(sd_journal *j, unsigned skip) {
23 unsigned i;
24
0c0cdb06 25 assert_se(j);
cbdca852
LP
26
27 i = 0;
28 SD_JOURNAL_FOREACH(j) {
29 const void *d;
c6511e85 30 char *k, *c;
cbdca852 31 size_t l;
a7f7d1bd 32 unsigned u = 0;
cbdca852
LP
33
34 assert_se(sd_journal_get_cursor(j, &k) >= 0);
35 printf("cursor: %s\n", k);
36 free(k);
37
38 assert_se(sd_journal_get_data(j, "MAGIC", &d, &l) >= 0);
39 printf("\t%.*s\n", (int) l, (const char*) d);
40
41 assert_se(sd_journal_get_data(j, "NUMBER", &d, &l) >= 0);
42 assert_se(k = strndup(d, l));
43 printf("\t%s\n", k);
44
45 if (skip > 0) {
46 assert_se(safe_atou(k + 7, &u) >= 0);
47 assert_se(i == u);
48 i += skip;
49 }
50
51 free(k);
c6511e85
LP
52
53 assert_se(sd_journal_get_cursor(j, &c) >= 0);
54 assert_se(sd_journal_test_cursor(j, c) > 0);
55 free(c);
cbdca852
LP
56 }
57
58 if (skip > 0)
59 assert_se(i == N_ENTRIES);
60}
61
4ce534f4 62static void run_test(void) {
cbdca852 63 JournalFile *one, *two, *three;
949082ac 64 char t[] = "/var/tmp/journal-stream-XXXXXX";
cbdca852 65 unsigned i;
4afd3348 66 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
cbdca852 67 char *z;
3c1668da
LP
68 const void *data;
69 size_t l;
98d2a534 70 dual_timestamp previous_ts = DUAL_TIMESTAMP_NULL;
cbdca852 71
cbdca852
LP
72 assert_se(mkdtemp(t));
73 assert_se(chdir(t) >= 0);
949082ac 74 (void) chattr_path(t, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
cbdca852 75
57850536
AG
76 assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &one) == 0);
77 assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &two) == 0);
78 assert_se(journal_file_open(-1, "three.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &three) == 0);
cbdca852
LP
79
80 for (i = 0; i < N_ENTRIES; i++) {
81 char *p, *q;
82 dual_timestamp ts;
83 struct iovec iovec[2];
84
85 dual_timestamp_get(&ts);
86
98d2a534
LP
87 if (ts.monotonic <= previous_ts.monotonic)
88 ts.monotonic = previous_ts.monotonic + 1;
89
90 if (ts.realtime <= previous_ts.realtime)
91 ts.realtime = previous_ts.realtime + 1;
92
93 previous_ts = ts;
94
cbdca852 95 assert_se(asprintf(&p, "NUMBER=%u", i) >= 0);
d7a0f1f4 96 iovec[0] = IOVEC_MAKE(p, strlen(p));
cbdca852
LP
97
98 assert_se(asprintf(&q, "MAGIC=%s", i % 5 == 0 ? "quux" : "waldo") >= 0);
99
d7a0f1f4 100 iovec[1] = IOVEC_MAKE(q, strlen(q));
cbdca852
LP
101
102 if (i % 10 == 0)
d180c349 103 assert_se(journal_file_append_entry(three, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0);
cbdca852
LP
104 else {
105 if (i % 3 == 0)
d180c349 106 assert_se(journal_file_append_entry(two, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0);
cbdca852 107
d180c349 108 assert_se(journal_file_append_entry(one, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0);
cbdca852
LP
109 }
110
111 free(p);
112 free(q);
113 }
114
69a3a6fd
VC
115 (void) journal_file_close(one);
116 (void) journal_file_close(two);
117 (void) journal_file_close(three);
cbdca852
LP
118
119 assert_se(sd_journal_open_directory(&j, t, 0) >= 0);
120
121 assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
122 SD_JOURNAL_FOREACH_BACKWARDS(j) {
7fd1b19b 123 _cleanup_free_ char *c;
cbdca852 124
3c1668da
LP
125 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
126 printf("\t%.*s\n", (int) l, (const char*) data);
c6511e85
LP
127
128 assert_se(sd_journal_get_cursor(j, &c) >= 0);
129 assert_se(sd_journal_test_cursor(j, c) > 0);
cbdca852
LP
130 }
131
132 SD_JOURNAL_FOREACH(j) {
7fd1b19b 133 _cleanup_free_ char *c;
cbdca852 134
3c1668da
LP
135 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
136 printf("\t%.*s\n", (int) l, (const char*) data);
c6511e85
LP
137
138 assert_se(sd_journal_get_cursor(j, &c) >= 0);
139 assert_se(sd_journal_test_cursor(j, c) > 0);
cbdca852
LP
140 }
141
142 sd_journal_flush_matches(j);
143
144 verify_contents(j, 1);
145
146 printf("NEXT TEST\n");
147 assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
148
149 assert_se(z = journal_make_match_string(j));
150 printf("resulting match expression is: %s\n", z);
151 free(z);
152
153 verify_contents(j, 5);
154
155 printf("NEXT TEST\n");
156 sd_journal_flush_matches(j);
157 assert_se(sd_journal_add_match(j, "MAGIC=waldo", 0) >= 0);
158 assert_se(sd_journal_add_match(j, "NUMBER=10", 0) >= 0);
159 assert_se(sd_journal_add_match(j, "NUMBER=11", 0) >= 0);
160 assert_se(sd_journal_add_match(j, "NUMBER=12", 0) >= 0);
161
162 assert_se(z = journal_make_match_string(j));
163 printf("resulting match expression is: %s\n", z);
164 free(z);
165
166 verify_contents(j, 0);
167
3c1668da
LP
168 assert_se(sd_journal_query_unique(j, "NUMBER") >= 0);
169 SD_JOURNAL_FOREACH_UNIQUE(j, data, l)
170 printf("%.*s\n", (int) l, (const char*) data);
171
c6878637 172 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
4ce534f4
LP
173}
174
175int main(int argc, char *argv[]) {
176
177 /* journal_file_open requires a valid machine id */
178 if (access("/etc/machine-id", F_OK) != 0)
179 return log_tests_skipped("/etc/machine-id not found");
180
181 test_setup_logging(LOG_DEBUG);
182
183 /* Run this test twice. Once with old hashing and once with new hashing */
184 assert_se(setenv("SYSTEMD_JOURNAL_KEYED_HASH", "1", 1) >= 0);
185 run_test();
186
187 assert_se(setenv("SYSTEMD_JOURNAL_KEYED_HASH", "0", 1) >= 0);
188 run_test();
cbdca852
LP
189
190 return 0;
191}