1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "sd-journal.h"
8 #include "alloc-util.h"
9 #include "chattr-util.h"
11 #include "journal-file.h"
12 #include "journal-internal.h"
15 #include "parse-util.h"
22 static void verify_contents(sd_journal
*j
, unsigned skip
) {
28 SD_JOURNAL_FOREACH(j
) {
34 assert_se(sd_journal_get_cursor(j
, &k
) >= 0);
35 printf("cursor: %s\n", k
);
38 assert_se(sd_journal_get_data(j
, "MAGIC", &d
, &l
) >= 0);
39 printf("\t%.*s\n", (int) l
, (const char*) d
);
41 assert_se(sd_journal_get_data(j
, "NUMBER", &d
, &l
) >= 0);
42 assert_se(k
= strndup(d
, l
));
46 assert_se(safe_atou(k
+ 7, &u
) >= 0);
53 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
54 assert_se(sd_journal_test_cursor(j
, c
) > 0);
59 assert_se(i
== N_ENTRIES
);
62 static void run_test(void) {
63 JournalFile
*one
, *two
, *three
;
64 char t
[] = "/var/tmp/journal-stream-XXXXXX";
66 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
70 dual_timestamp previous_ts
= DUAL_TIMESTAMP_NULL
;
72 assert_se(mkdtemp(t
));
73 assert_se(chdir(t
) >= 0);
74 (void) chattr_path(t
, FS_NOCOW_FL
, FS_NOCOW_FL
, NULL
);
76 assert_se(journal_file_open(-1, "one.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &one
) == 0);
77 assert_se(journal_file_open(-1, "two.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &two
) == 0);
78 assert_se(journal_file_open(-1, "three.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &three
) == 0);
80 for (i
= 0; i
< N_ENTRIES
; i
++) {
83 struct iovec iovec
[2];
85 dual_timestamp_get(&ts
);
87 if (ts
.monotonic
<= previous_ts
.monotonic
)
88 ts
.monotonic
= previous_ts
.monotonic
+ 1;
90 if (ts
.realtime
<= previous_ts
.realtime
)
91 ts
.realtime
= previous_ts
.realtime
+ 1;
95 assert_se(asprintf(&p
, "NUMBER=%u", i
) >= 0);
96 iovec
[0] = IOVEC_MAKE(p
, strlen(p
));
98 assert_se(asprintf(&q
, "MAGIC=%s", i
% 5 == 0 ? "quux" : "waldo") >= 0);
100 iovec
[1] = IOVEC_MAKE(q
, strlen(q
));
103 assert_se(journal_file_append_entry(three
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
106 assert_se(journal_file_append_entry(two
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
108 assert_se(journal_file_append_entry(one
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
115 (void) journal_file_close(one
);
116 (void) journal_file_close(two
);
117 (void) journal_file_close(three
);
119 assert_se(sd_journal_open_directory(&j
, t
, 0) >= 0);
121 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
122 SD_JOURNAL_FOREACH_BACKWARDS(j
) {
123 _cleanup_free_
char *c
;
125 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
126 printf("\t%.*s\n", (int) l
, (const char*) data
);
128 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
129 assert_se(sd_journal_test_cursor(j
, c
) > 0);
132 SD_JOURNAL_FOREACH(j
) {
133 _cleanup_free_
char *c
;
135 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
136 printf("\t%.*s\n", (int) l
, (const char*) data
);
138 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
139 assert_se(sd_journal_test_cursor(j
, c
) > 0);
142 sd_journal_flush_matches(j
);
144 verify_contents(j
, 1);
146 printf("NEXT TEST\n");
147 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
149 assert_se(z
= journal_make_match_string(j
));
150 printf("resulting match expression is: %s\n", z
);
153 verify_contents(j
, 5);
155 printf("NEXT TEST\n");
156 sd_journal_flush_matches(j
);
157 assert_se(sd_journal_add_match(j
, "MAGIC=waldo", 0) >= 0);
158 assert_se(sd_journal_add_match(j
, "NUMBER=10", 0) >= 0);
159 assert_se(sd_journal_add_match(j
, "NUMBER=11", 0) >= 0);
160 assert_se(sd_journal_add_match(j
, "NUMBER=12", 0) >= 0);
162 assert_se(z
= journal_make_match_string(j
));
163 printf("resulting match expression is: %s\n", z
);
166 verify_contents(j
, 0);
168 assert_se(sd_journal_query_unique(j
, "NUMBER") >= 0);
169 SD_JOURNAL_FOREACH_UNIQUE(j
, data
, l
)
170 printf("%.*s\n", (int) l
, (const char*) data
);
172 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
175 int main(int argc
, char *argv
[]) {
177 /* journal_file_open requires a valid machine id */
178 if (access("/etc/machine-id", F_OK
) != 0)
179 return log_tests_skipped("/etc/machine-id not found");
181 test_setup_logging(LOG_DEBUG
);
183 /* Run this test twice. Once with old hashing and once with new hashing */
184 assert_se(setenv("SYSTEMD_JOURNAL_KEYED_HASH", "1", 1) >= 0);
187 assert_se(setenv("SYSTEMD_JOURNAL_KEYED_HASH", "0", 1) >= 0);