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