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