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