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