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