]>
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"
13 static bool arg_keep
= false;
15 static void test_non_empty(void) {
19 static const char test
[] = "TEST1=1", test2
[] = "TEST2=2";
22 sd_id128_t fake_boot_id
;
23 char t
[] = "/tmp/journal-XXXXXX";
25 test_setup_logging(LOG_DEBUG
);
27 assert_se(mkdtemp(t
));
28 assert_se(chdir(t
) >= 0);
30 assert_se(journal_file_open(-1, "test.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, true, NULL
, NULL
, NULL
, NULL
, &f
) == 0);
32 assert_se(dual_timestamp_get(&ts
));
33 assert_se(sd_id128_randomize(&fake_boot_id
) == 0);
35 iovec
.iov_base
= (void*) test
;
36 iovec
.iov_len
= strlen(test
);
37 assert_se(journal_file_append_entry(f
, &ts
, NULL
, &iovec
, 1, NULL
, NULL
, NULL
) == 0);
39 iovec
.iov_base
= (void*) test2
;
40 iovec
.iov_len
= strlen(test2
);
41 assert_se(journal_file_append_entry(f
, &ts
, NULL
, &iovec
, 1, NULL
, NULL
, NULL
) == 0);
43 iovec
.iov_base
= (void*) test
;
44 iovec
.iov_len
= strlen(test
);
45 assert_se(journal_file_append_entry(f
, &ts
, &fake_boot_id
, &iovec
, 1, NULL
, NULL
, NULL
) == 0);
48 journal_file_append_tag(f
);
52 assert_se(journal_file_next_entry(f
, 0, DIRECTION_DOWN
, &o
, &p
) == 1);
53 assert_se(le64toh(o
->entry
.seqnum
) == 1);
55 assert_se(journal_file_next_entry(f
, p
, DIRECTION_DOWN
, &o
, &p
) == 1);
56 assert_se(le64toh(o
->entry
.seqnum
) == 2);
58 assert_se(journal_file_next_entry(f
, p
, DIRECTION_DOWN
, &o
, &p
) == 1);
59 assert_se(le64toh(o
->entry
.seqnum
) == 3);
60 assert_se(sd_id128_equal(o
->entry
.boot_id
, fake_boot_id
));
62 assert_se(journal_file_next_entry(f
, p
, DIRECTION_DOWN
, &o
, &p
) == 0);
64 assert_se(journal_file_next_entry(f
, 0, DIRECTION_DOWN
, &o
, &p
) == 1);
65 assert_se(le64toh(o
->entry
.seqnum
) == 1);
67 assert_se(journal_file_find_data_object(f
, test
, strlen(test
), NULL
, &p
) == 1);
68 assert_se(journal_file_next_entry_for_data(f
, NULL
, 0, p
, DIRECTION_DOWN
, &o
, NULL
) == 1);
69 assert_se(le64toh(o
->entry
.seqnum
) == 1);
71 assert_se(journal_file_next_entry_for_data(f
, NULL
, 0, p
, DIRECTION_UP
, &o
, NULL
) == 1);
72 assert_se(le64toh(o
->entry
.seqnum
) == 3);
74 assert_se(journal_file_find_data_object(f
, test2
, strlen(test2
), NULL
, &p
) == 1);
75 assert_se(journal_file_next_entry_for_data(f
, NULL
, 0, p
, DIRECTION_UP
, &o
, NULL
) == 1);
76 assert_se(le64toh(o
->entry
.seqnum
) == 2);
78 assert_se(journal_file_next_entry_for_data(f
, NULL
, 0, p
, DIRECTION_DOWN
, &o
, NULL
) == 1);
79 assert_se(le64toh(o
->entry
.seqnum
) == 2);
81 assert_se(journal_file_find_data_object(f
, "quux", 4, NULL
, &p
) == 0);
83 assert_se(journal_file_move_to_entry_by_seqnum(f
, 1, DIRECTION_DOWN
, &o
, NULL
) == 1);
84 assert_se(le64toh(o
->entry
.seqnum
) == 1);
86 assert_se(journal_file_move_to_entry_by_seqnum(f
, 3, DIRECTION_DOWN
, &o
, NULL
) == 1);
87 assert_se(le64toh(o
->entry
.seqnum
) == 3);
89 assert_se(journal_file_move_to_entry_by_seqnum(f
, 2, DIRECTION_DOWN
, &o
, NULL
) == 1);
90 assert_se(le64toh(o
->entry
.seqnum
) == 2);
92 assert_se(journal_file_move_to_entry_by_seqnum(f
, 10, DIRECTION_DOWN
, &o
, NULL
) == 0);
94 journal_file_rotate(&f
, true, (uint64_t) -1, true, NULL
);
95 journal_file_rotate(&f
, true, (uint64_t) -1, true, NULL
);
97 (void) journal_file_close(f
);
102 log_info("Not removing %s", t
);
104 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
106 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
109 puts("------------------------------------------------------------");
112 static void test_empty(void) {
113 JournalFile
*f1
, *f2
, *f3
, *f4
;
114 char t
[] = "/tmp/journal-XXXXXX";
116 test_setup_logging(LOG_DEBUG
);
118 assert_se(mkdtemp(t
));
119 assert_se(chdir(t
) >= 0);
121 assert_se(journal_file_open(-1, "test.journal", O_RDWR
|O_CREAT
, 0666, false, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &f1
) == 0);
123 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);
125 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);
127 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);
129 journal_file_print_header(f1
);
131 journal_file_print_header(f2
);
133 journal_file_print_header(f3
);
135 journal_file_print_header(f4
);
141 log_info("Not removing %s", t
);
143 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
145 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
148 (void) journal_file_close(f1
);
149 (void) journal_file_close(f2
);
150 (void) journal_file_close(f3
);
151 (void) journal_file_close(f4
);
154 #if HAVE_XZ || HAVE_LZ4
155 static bool check_compressed(uint64_t compress_threshold
, uint64_t data_size
) {
161 char t
[] = "/tmp/journal-XXXXXX";
162 char data
[2048] = {0};
166 assert_se(data_size
<= sizeof(data
));
168 test_setup_logging(LOG_DEBUG
);
170 assert_se(mkdtemp(t
));
171 assert_se(chdir(t
) >= 0);
173 assert_se(journal_file_open(-1, "test.journal", O_RDWR
|O_CREAT
, 0666, true, compress_threshold
, true, NULL
, NULL
, NULL
, NULL
, &f
) == 0);
175 dual_timestamp_get(&ts
);
177 iovec
.iov_base
= (void*) data
;
178 iovec
.iov_len
= data_size
;
179 assert_se(journal_file_append_entry(f
, &ts
, NULL
, &iovec
, 1, NULL
, NULL
, NULL
) == 0);
182 journal_file_append_tag(f
);
184 journal_file_dump(f
);
186 /* We have to partially reimplement some of the dump logic, because the normal next_entry does the
187 * decompression for us. */
188 p
= le64toh(f
->header
->header_size
);
190 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, p
, &o
);
192 if (o
->object
.type
== OBJECT_DATA
)
195 assert_se(p
< le64toh(f
->header
->tail_object_offset
));
196 p
= p
+ ALIGN64(le64toh(o
->object
.size
));
199 is_compressed
= (o
->object
.flags
& OBJECT_COMPRESSION_MASK
) != 0;
201 (void) journal_file_close(f
);
206 log_info("Not removing %s", t
);
208 journal_directory_vacuum(".", 3000000, 0, 0, NULL
, true);
210 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
213 puts("------------------------------------------------------------");
215 return is_compressed
;
218 static void test_min_compress_size(void) {
219 /* Note that XZ will actually fail to compress anything under 80 bytes, so you have to choose the limits
222 /* DEFAULT_MIN_COMPRESS_SIZE is 512 */
223 assert_se(!check_compressed((uint64_t) -1, 255));
224 assert_se(check_compressed((uint64_t) -1, 513));
226 /* compress everything */
227 assert_se(check_compressed(0, 96));
228 assert_se(check_compressed(8, 96));
230 /* Ensure we don't try to compress less than 8 bytes */
231 assert_se(!check_compressed(0, 7));
233 /* check boundary conditions */
234 assert_se(check_compressed(256, 256));
235 assert_se(!check_compressed(256, 255));
239 int main(int argc
, char *argv
[]) {
242 test_setup_logging(LOG_INFO
);
244 /* journal_file_open requires a valid machine id */
245 if (access("/etc/machine-id", F_OK
) != 0)
246 return log_tests_skipped("/etc/machine-id not found");
250 #if HAVE_XZ || HAVE_LZ4
251 test_min_compress_size();