]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2011 Lennart Poettering
9 #include "journal-authenticate.h"
10 #include "journal-file.h"
11 #include "journal-vacuum.h"
15 static bool arg_keep
= false;
17 static void test_non_empty(void) {
21 static const char test
[] = "TEST1=1", test2
[] = "TEST2=2";
24 sd_id128_t fake_boot_id
;
25 char t
[] = "/tmp/journal-XXXXXX";
27 log_set_max_level(LOG_DEBUG
);
29 assert_se(mkdtemp(t
));
30 assert_se(chdir(t
) >= 0);
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);
34 assert_se(dual_timestamp_get(&ts
));
35 assert_se(sd_id128_randomize(&fake_boot_id
) == 0);
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);
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);
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);
50 journal_file_append_tag(f
);
54 assert_se(journal_file_next_entry(f
, 0, DIRECTION_DOWN
, &o
, &p
) == 1);
55 assert_se(le64toh(o
->entry
.seqnum
) == 1);
57 assert_se(journal_file_next_entry(f
, p
, DIRECTION_DOWN
, &o
, &p
) == 1);
58 assert_se(le64toh(o
->entry
.seqnum
) == 2);
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
));
64 assert_se(journal_file_next_entry(f
, p
, DIRECTION_DOWN
, &o
, &p
) == 0);
66 assert_se(journal_file_next_entry(f
, 0, DIRECTION_DOWN
, &o
, &p
) == 1);
67 assert_se(le64toh(o
->entry
.seqnum
) == 1);
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);
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);
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);
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);
83 assert_se(journal_file_find_data_object(f
, "quux", 4, NULL
, &p
) == 0);
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);
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);
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);
94 assert_se(journal_file_move_to_entry_by_seqnum(f
, 10, DIRECTION_DOWN
, &o
, NULL
) == 0);
96 journal_file_rotate(&f
, true, (uint64_t) -1, true, NULL
);
97 journal_file_rotate(&f
, true, (uint64_t) -1, true, NULL
);
99 (void) journal_file_close(f
);
104 log_info("Not removing %s", t
);
106 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
108 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
111 puts("------------------------------------------------------------");
114 static void test_empty(void) {
115 JournalFile
*f1
, *f2
, *f3
, *f4
;
116 char t
[] = "/tmp/journal-XXXXXX";
118 log_set_max_level(LOG_DEBUG
);
120 assert_se(mkdtemp(t
));
121 assert_se(chdir(t
) >= 0);
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);
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);
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);
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);
131 journal_file_print_header(f1
);
133 journal_file_print_header(f2
);
135 journal_file_print_header(f3
);
137 journal_file_print_header(f4
);
143 log_info("Not removing %s", t
);
145 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
147 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
150 (void) journal_file_close(f1
);
151 (void) journal_file_close(f2
);
152 (void) journal_file_close(f3
);
153 (void) journal_file_close(f4
);
156 #if HAVE_XZ || HAVE_LZ4
157 static bool check_compressed(uint64_t compress_threshold
, uint64_t data_size
) {
163 char t
[] = "/tmp/journal-XXXXXX";
164 char data
[2048] = {0};
168 assert_se(data_size
<= sizeof(data
));
170 log_set_max_level(LOG_DEBUG
);
172 assert_se(mkdtemp(t
));
173 assert_se(chdir(t
) >= 0);
175 assert_se(journal_file_open(-1, "test.journal", O_RDWR
|O_CREAT
, 0666, true, compress_threshold
, true, NULL
, NULL
, NULL
, NULL
, &f
) == 0);
177 dual_timestamp_get(&ts
);
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);
184 journal_file_append_tag(f
);
186 journal_file_dump(f
);
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
);
192 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, p
, &o
);
194 if (o
->object
.type
== OBJECT_DATA
)
197 assert_se(p
< le64toh(f
->header
->tail_object_offset
));
198 p
= p
+ ALIGN64(le64toh(o
->object
.size
));
201 is_compressed
= (o
->object
.flags
& OBJECT_COMPRESSION_MASK
) != 0;
203 (void) journal_file_close(f
);
208 log_info("Not removing %s", t
);
210 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
212 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
215 puts("------------------------------------------------------------");
217 return is_compressed
;
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
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));
228 /* compress everything */
229 assert_se(check_compressed(0, 96));
230 assert_se(check_compressed(8, 96));
232 /* Ensure we don't try to compress less than 8 bytes */
233 assert_se(!check_compressed(0, 7));
235 /* check boundary conditions */
236 assert_se(check_compressed(256, 256));
237 assert_se(!check_compressed(256, 255));
241 int main(int argc
, char *argv
[]) {
244 /* journal_file_open requires a valid machine id */
245 if (access("/etc/machine-id", F_OK
) != 0)
246 return EXIT_TEST_SKIP
;
250 #if HAVE_XZ || HAVE_LZ4
251 test_min_compress_size();