]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "journal-authenticate.h"
7 #include "journal-file.h"
8 #include "journal-vacuum.h"
12 static bool arg_keep
= false;
14 static void test_non_empty(void) {
18 static const char test
[] = "TEST1=1", test2
[] = "TEST2=2";
21 sd_id128_t fake_boot_id
;
22 char t
[] = "/tmp/journal-XXXXXX";
24 log_set_max_level(LOG_DEBUG
);
26 assert_se(mkdtemp(t
));
27 assert_se(chdir(t
) >= 0);
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);
31 assert_se(dual_timestamp_get(&ts
));
32 assert_se(sd_id128_randomize(&fake_boot_id
) == 0);
34 iovec
.iov_base
= (void*) test
;
35 iovec
.iov_len
= strlen(test
);
36 assert_se(journal_file_append_entry(f
, &ts
, NULL
, &iovec
, 1, NULL
, NULL
, NULL
) == 0);
38 iovec
.iov_base
= (void*) test2
;
39 iovec
.iov_len
= strlen(test2
);
40 assert_se(journal_file_append_entry(f
, &ts
, NULL
, &iovec
, 1, NULL
, NULL
, NULL
) == 0);
42 iovec
.iov_base
= (void*) test
;
43 iovec
.iov_len
= strlen(test
);
44 assert_se(journal_file_append_entry(f
, &ts
, &fake_boot_id
, &iovec
, 1, NULL
, NULL
, NULL
) == 0);
47 journal_file_append_tag(f
);
51 assert_se(journal_file_next_entry(f
, 0, DIRECTION_DOWN
, &o
, &p
) == 1);
52 assert_se(le64toh(o
->entry
.seqnum
) == 1);
54 assert_se(journal_file_next_entry(f
, p
, DIRECTION_DOWN
, &o
, &p
) == 1);
55 assert_se(le64toh(o
->entry
.seqnum
) == 2);
57 assert_se(journal_file_next_entry(f
, p
, DIRECTION_DOWN
, &o
, &p
) == 1);
58 assert_se(le64toh(o
->entry
.seqnum
) == 3);
59 assert_se(sd_id128_equal(o
->entry
.boot_id
, fake_boot_id
));
61 assert_se(journal_file_next_entry(f
, p
, DIRECTION_DOWN
, &o
, &p
) == 0);
63 assert_se(journal_file_next_entry(f
, 0, DIRECTION_DOWN
, &o
, &p
) == 1);
64 assert_se(le64toh(o
->entry
.seqnum
) == 1);
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);
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);
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);
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);
80 assert_se(journal_file_find_data_object(f
, "quux", 4, NULL
, &p
) == 0);
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);
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);
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);
91 assert_se(journal_file_move_to_entry_by_seqnum(f
, 10, DIRECTION_DOWN
, &o
, NULL
) == 0);
93 journal_file_rotate(&f
, true, (uint64_t) -1, true, NULL
);
94 journal_file_rotate(&f
, true, (uint64_t) -1, true, NULL
);
96 (void) journal_file_close(f
);
101 log_info("Not removing %s", t
);
103 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
105 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
108 puts("------------------------------------------------------------");
111 static void test_empty(void) {
112 JournalFile
*f1
, *f2
, *f3
, *f4
;
113 char t
[] = "/tmp/journal-XXXXXX";
115 log_set_max_level(LOG_DEBUG
);
117 assert_se(mkdtemp(t
));
118 assert_se(chdir(t
) >= 0);
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);
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);
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);
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);
128 journal_file_print_header(f1
);
130 journal_file_print_header(f2
);
132 journal_file_print_header(f3
);
134 journal_file_print_header(f4
);
140 log_info("Not removing %s", t
);
142 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
144 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
147 (void) journal_file_close(f1
);
148 (void) journal_file_close(f2
);
149 (void) journal_file_close(f3
);
150 (void) journal_file_close(f4
);
153 #if HAVE_XZ || HAVE_LZ4
154 static bool check_compressed(uint64_t compress_threshold
, uint64_t data_size
) {
160 char t
[] = "/tmp/journal-XXXXXX";
161 char data
[2048] = {0};
165 assert_se(data_size
<= sizeof(data
));
167 log_set_max_level(LOG_DEBUG
);
169 assert_se(mkdtemp(t
));
170 assert_se(chdir(t
) >= 0);
172 assert_se(journal_file_open(-1, "test.journal", O_RDWR
|O_CREAT
, 0666, true, compress_threshold
, true, NULL
, NULL
, NULL
, NULL
, &f
) == 0);
174 dual_timestamp_get(&ts
);
176 iovec
.iov_base
= (void*) data
;
177 iovec
.iov_len
= data_size
;
178 assert_se(journal_file_append_entry(f
, &ts
, NULL
, &iovec
, 1, NULL
, NULL
, NULL
) == 0);
181 journal_file_append_tag(f
);
183 journal_file_dump(f
);
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
);
189 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, p
, &o
);
191 if (o
->object
.type
== OBJECT_DATA
)
194 assert_se(p
< le64toh(f
->header
->tail_object_offset
));
195 p
= p
+ ALIGN64(le64toh(o
->object
.size
));
198 is_compressed
= (o
->object
.flags
& OBJECT_COMPRESSION_MASK
) != 0;
200 (void) journal_file_close(f
);
205 log_info("Not removing %s", t
);
207 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
209 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
212 puts("------------------------------------------------------------");
214 return is_compressed
;
217 static 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
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));
225 /* compress everything */
226 assert_se(check_compressed(0, 96));
227 assert_se(check_compressed(8, 96));
229 /* Ensure we don't try to compress less than 8 bytes */
230 assert_se(!check_compressed(0, 7));
232 /* check boundary conditions */
233 assert_se(check_compressed(256, 256));
234 assert_se(!check_compressed(256, 255));
238 int main(int argc
, char *argv
[]) {
241 /* journal_file_open requires a valid machine id */
242 if (access("/etc/machine-id", F_OK
) != 0)
243 return EXIT_TEST_SKIP
;
247 #if HAVE_XZ || HAVE_LZ4
248 test_min_compress_size();