]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / journal / test-journal.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "journal-authenticate.h"
7 #include "journal-file.h"
8 #include "journal-vacuum.h"
9 #include "log.h"
10 #include "rm-rf.h"
11 #include "tests.h"
12
13 static bool arg_keep = false;
14
15 static void test_non_empty(void) {
16 dual_timestamp ts;
17 JournalFile *f;
18 struct iovec iovec;
19 static const char test[] = "TEST1=1", test2[] = "TEST2=2";
20 Object *o;
21 uint64_t p;
22 sd_id128_t fake_boot_id;
23 char t[] = "/tmp/journal-XXXXXX";
24
25 test_setup_logging(LOG_DEBUG);
26
27 assert_se(mkdtemp(t));
28 assert_se(chdir(t) >= 0);
29
30 assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, true, NULL, NULL, NULL, NULL, &f) == 0);
31
32 assert_se(dual_timestamp_get(&ts));
33 assert_se(sd_id128_randomize(&fake_boot_id) == 0);
34
35 iovec.iov_base = (void*) test;
36 iovec.iov_len = strlen(test);
37 assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0);
38
39 iovec.iov_base = (void*) test2;
40 iovec.iov_len = strlen(test2);
41 assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0);
42
43 iovec.iov_base = (void*) test;
44 iovec.iov_len = strlen(test);
45 assert_se(journal_file_append_entry(f, &ts, &fake_boot_id, &iovec, 1, NULL, NULL, NULL) == 0);
46
47 #if HAVE_GCRYPT
48 journal_file_append_tag(f);
49 #endif
50 journal_file_dump(f);
51
52 assert_se(journal_file_next_entry(f, 0, DIRECTION_DOWN, &o, &p) == 1);
53 assert_se(le64toh(o->entry.seqnum) == 1);
54
55 assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 1);
56 assert_se(le64toh(o->entry.seqnum) == 2);
57
58 assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 1);
59 assert_se(le64toh(o->entry.seqnum) == 3);
60 assert_se(sd_id128_equal(o->entry.boot_id, fake_boot_id));
61
62 assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 0);
63
64 assert_se(journal_file_next_entry(f, 0, DIRECTION_DOWN, &o, &p) == 1);
65 assert_se(le64toh(o->entry.seqnum) == 1);
66
67 assert_se(journal_file_find_data_object(f, test, strlen(test), NULL, &p) == 1);
68 assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_DOWN, &o, NULL) == 1);
69 assert_se(le64toh(o->entry.seqnum) == 1);
70
71 assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_UP, &o, NULL) == 1);
72 assert_se(le64toh(o->entry.seqnum) == 3);
73
74 assert_se(journal_file_find_data_object(f, test2, strlen(test2), NULL, &p) == 1);
75 assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_UP, &o, NULL) == 1);
76 assert_se(le64toh(o->entry.seqnum) == 2);
77
78 assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_DOWN, &o, NULL) == 1);
79 assert_se(le64toh(o->entry.seqnum) == 2);
80
81 assert_se(journal_file_find_data_object(f, "quux", 4, NULL, &p) == 0);
82
83 assert_se(journal_file_move_to_entry_by_seqnum(f, 1, DIRECTION_DOWN, &o, NULL) == 1);
84 assert_se(le64toh(o->entry.seqnum) == 1);
85
86 assert_se(journal_file_move_to_entry_by_seqnum(f, 3, DIRECTION_DOWN, &o, NULL) == 1);
87 assert_se(le64toh(o->entry.seqnum) == 3);
88
89 assert_se(journal_file_move_to_entry_by_seqnum(f, 2, DIRECTION_DOWN, &o, NULL) == 1);
90 assert_se(le64toh(o->entry.seqnum) == 2);
91
92 assert_se(journal_file_move_to_entry_by_seqnum(f, 10, DIRECTION_DOWN, &o, NULL) == 0);
93
94 journal_file_rotate(&f, true, (uint64_t) -1, true, NULL);
95 journal_file_rotate(&f, true, (uint64_t) -1, true, NULL);
96
97 (void) journal_file_close(f);
98
99 log_info("Done...");
100
101 if (arg_keep)
102 log_info("Not removing %s", t);
103 else {
104 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
105
106 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
107 }
108
109 puts("------------------------------------------------------------");
110 }
111
112 static void test_empty(void) {
113 JournalFile *f1, *f2, *f3, *f4;
114 char t[] = "/tmp/journal-XXXXXX";
115
116 test_setup_logging(LOG_DEBUG);
117
118 assert_se(mkdtemp(t));
119 assert_se(chdir(t) >= 0);
120
121 assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, false, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &f1) == 0);
122
123 assert_se(journal_file_open(-1, "test-compress.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &f2) == 0);
124
125 assert_se(journal_file_open(-1, "test-seal.journal", O_RDWR|O_CREAT, 0666, false, (uint64_t) -1, true, NULL, NULL, NULL, NULL, &f3) == 0);
126
127 assert_se(journal_file_open(-1, "test-seal-compress.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, true, NULL, NULL, NULL, NULL, &f4) == 0);
128
129 journal_file_print_header(f1);
130 puts("");
131 journal_file_print_header(f2);
132 puts("");
133 journal_file_print_header(f3);
134 puts("");
135 journal_file_print_header(f4);
136 puts("");
137
138 log_info("Done...");
139
140 if (arg_keep)
141 log_info("Not removing %s", t);
142 else {
143 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
144
145 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
146 }
147
148 (void) journal_file_close(f1);
149 (void) journal_file_close(f2);
150 (void) journal_file_close(f3);
151 (void) journal_file_close(f4);
152 }
153
154 #if HAVE_XZ || HAVE_LZ4
155 static bool check_compressed(uint64_t compress_threshold, uint64_t data_size) {
156 dual_timestamp ts;
157 JournalFile *f;
158 struct iovec iovec;
159 Object *o;
160 uint64_t p;
161 char t[] = "/tmp/journal-XXXXXX";
162 char data[2048] = {0};
163 bool is_compressed;
164 int r;
165
166 assert_se(data_size <= sizeof(data));
167
168 test_setup_logging(LOG_DEBUG);
169
170 assert_se(mkdtemp(t));
171 assert_se(chdir(t) >= 0);
172
173 assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, compress_threshold, true, NULL, NULL, NULL, NULL, &f) == 0);
174
175 dual_timestamp_get(&ts);
176
177 iovec.iov_base = (void*) data;
178 iovec.iov_len = data_size;
179 assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0);
180
181 #if HAVE_GCRYPT
182 journal_file_append_tag(f);
183 #endif
184 journal_file_dump(f);
185
186 /* We have to partially reimplement some of the dump logic, because the normal next_entry does the
187 * decompression for us. */
188 p = le64toh(f->header->header_size);
189 for (;;) {
190 r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o);
191 assert_se(r == 0);
192 if (o->object.type == OBJECT_DATA)
193 break;
194
195 assert_se(p < le64toh(f->header->tail_object_offset));
196 p = p + ALIGN64(le64toh(o->object.size));
197 }
198
199 is_compressed = (o->object.flags & OBJECT_COMPRESSION_MASK) != 0;
200
201 (void) journal_file_close(f);
202
203 log_info("Done...");
204
205 if (arg_keep)
206 log_info("Not removing %s", t);
207 else {
208 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
209
210 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
211 }
212
213 puts("------------------------------------------------------------");
214
215 return is_compressed;
216 }
217
218 static void test_min_compress_size(void) {
219 /* Note that XZ will actually fail to compress anything under 80 bytes, so you have to choose the limits
220 * carefully */
221
222 /* DEFAULT_MIN_COMPRESS_SIZE is 512 */
223 assert_se(!check_compressed((uint64_t) -1, 255));
224 assert_se(check_compressed((uint64_t) -1, 513));
225
226 /* compress everything */
227 assert_se(check_compressed(0, 96));
228 assert_se(check_compressed(8, 96));
229
230 /* Ensure we don't try to compress less than 8 bytes */
231 assert_se(!check_compressed(0, 7));
232
233 /* check boundary conditions */
234 assert_se(check_compressed(256, 256));
235 assert_se(!check_compressed(256, 255));
236 }
237 #endif
238
239 int main(int argc, char *argv[]) {
240 arg_keep = argc > 1;
241
242 test_setup_logging(LOG_INFO);
243
244 /* journal_file_open requires a valid machine id */
245 if (access("/etc/machine-id", F_OK) != 0)
246 return log_tests_skipped("/etc/machine-id not found");
247
248 test_non_empty();
249 test_empty();
250 #if HAVE_XZ || HAVE_LZ4
251 test_min_compress_size();
252 #endif
253
254 return 0;
255 }