]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/test-journal-stream.c
homed: make it easier to run multiple instances of homed
[thirdparty/systemd.git] / src / journal / test-journal-stream.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
cbdca852 2
cbdca852 3#include <fcntl.h>
6bedfcbb 4#include <unistd.h>
cbdca852 5
c6878637 6#include "sd-journal.h"
6bedfcbb 7
b5efdb8a 8#include "alloc-util.h"
949082ac 9#include "chattr-util.h"
6bedfcbb
LP
10#include "journal-file.h"
11#include "journal-internal.h"
cbdca852 12#include "log.h"
0c0cdb06 13#include "macro.h"
6bedfcbb 14#include "parse-util.h"
c6878637 15#include "rm-rf.h"
317bb217 16#include "tests.h"
6bedfcbb 17#include "util.h"
cbdca852
LP
18
19#define N_ENTRIES 200
20
21static void verify_contents(sd_journal *j, unsigned skip) {
22 unsigned i;
23
0c0cdb06 24 assert_se(j);
cbdca852
LP
25
26 i = 0;
27 SD_JOURNAL_FOREACH(j) {
28 const void *d;
c6511e85 29 char *k, *c;
cbdca852 30 size_t l;
a7f7d1bd 31 unsigned u = 0;
cbdca852
LP
32
33 assert_se(sd_journal_get_cursor(j, &k) >= 0);
34 printf("cursor: %s\n", k);
35 free(k);
36
37 assert_se(sd_journal_get_data(j, "MAGIC", &d, &l) >= 0);
38 printf("\t%.*s\n", (int) l, (const char*) d);
39
40 assert_se(sd_journal_get_data(j, "NUMBER", &d, &l) >= 0);
41 assert_se(k = strndup(d, l));
42 printf("\t%s\n", k);
43
44 if (skip > 0) {
45 assert_se(safe_atou(k + 7, &u) >= 0);
46 assert_se(i == u);
47 i += skip;
48 }
49
50 free(k);
c6511e85
LP
51
52 assert_se(sd_journal_get_cursor(j, &c) >= 0);
53 assert_se(sd_journal_test_cursor(j, c) > 0);
54 free(c);
cbdca852
LP
55 }
56
57 if (skip > 0)
58 assert_se(i == N_ENTRIES);
59}
60
4ce534f4 61static void run_test(void) {
cbdca852 62 JournalFile *one, *two, *three;
949082ac 63 char t[] = "/var/tmp/journal-stream-XXXXXX";
cbdca852 64 unsigned i;
4afd3348 65 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
cbdca852 66 char *z;
3c1668da
LP
67 const void *data;
68 size_t l;
98d2a534 69 dual_timestamp previous_ts = DUAL_TIMESTAMP_NULL;
cbdca852 70
cbdca852
LP
71 assert_se(mkdtemp(t));
72 assert_se(chdir(t) >= 0);
949082ac 73 (void) chattr_path(t, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
cbdca852 74
57850536
AG
75 assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &one) == 0);
76 assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &two) == 0);
77 assert_se(journal_file_open(-1, "three.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &three) == 0);
cbdca852
LP
78
79 for (i = 0; i < N_ENTRIES; i++) {
80 char *p, *q;
81 dual_timestamp ts;
82 struct iovec iovec[2];
83
84 dual_timestamp_get(&ts);
85
98d2a534
LP
86 if (ts.monotonic <= previous_ts.monotonic)
87 ts.monotonic = previous_ts.monotonic + 1;
88
89 if (ts.realtime <= previous_ts.realtime)
90 ts.realtime = previous_ts.realtime + 1;
91
92 previous_ts = ts;
93
cbdca852
LP
94 assert_se(asprintf(&p, "NUMBER=%u", i) >= 0);
95 iovec[0].iov_base = p;
96 iovec[0].iov_len = strlen(p);
97
98 assert_se(asprintf(&q, "MAGIC=%s", i % 5 == 0 ? "quux" : "waldo") >= 0);
99
100 iovec[1].iov_base = q;
101 iovec[1].iov_len = strlen(q);
102
103 if (i % 10 == 0)
d180c349 104 assert_se(journal_file_append_entry(three, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0);
cbdca852
LP
105 else {
106 if (i % 3 == 0)
d180c349 107 assert_se(journal_file_append_entry(two, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0);
cbdca852 108
d180c349 109 assert_se(journal_file_append_entry(one, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0);
cbdca852
LP
110 }
111
112 free(p);
113 free(q);
114 }
115
69a3a6fd
VC
116 (void) journal_file_close(one);
117 (void) journal_file_close(two);
118 (void) journal_file_close(three);
cbdca852
LP
119
120 assert_se(sd_journal_open_directory(&j, t, 0) >= 0);
121
122 assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
123 SD_JOURNAL_FOREACH_BACKWARDS(j) {
7fd1b19b 124 _cleanup_free_ char *c;
cbdca852 125
3c1668da
LP
126 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
127 printf("\t%.*s\n", (int) l, (const char*) data);
c6511e85
LP
128
129 assert_se(sd_journal_get_cursor(j, &c) >= 0);
130 assert_se(sd_journal_test_cursor(j, c) > 0);
cbdca852
LP
131 }
132
133 SD_JOURNAL_FOREACH(j) {
7fd1b19b 134 _cleanup_free_ char *c;
cbdca852 135
3c1668da
LP
136 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
137 printf("\t%.*s\n", (int) l, (const char*) data);
c6511e85
LP
138
139 assert_se(sd_journal_get_cursor(j, &c) >= 0);
140 assert_se(sd_journal_test_cursor(j, c) > 0);
cbdca852
LP
141 }
142
143 sd_journal_flush_matches(j);
144
145 verify_contents(j, 1);
146
147 printf("NEXT TEST\n");
148 assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
149
150 assert_se(z = journal_make_match_string(j));
151 printf("resulting match expression is: %s\n", z);
152 free(z);
153
154 verify_contents(j, 5);
155
156 printf("NEXT TEST\n");
157 sd_journal_flush_matches(j);
158 assert_se(sd_journal_add_match(j, "MAGIC=waldo", 0) >= 0);
159 assert_se(sd_journal_add_match(j, "NUMBER=10", 0) >= 0);
160 assert_se(sd_journal_add_match(j, "NUMBER=11", 0) >= 0);
161 assert_se(sd_journal_add_match(j, "NUMBER=12", 0) >= 0);
162
163 assert_se(z = journal_make_match_string(j));
164 printf("resulting match expression is: %s\n", z);
165 free(z);
166
167 verify_contents(j, 0);
168
3c1668da
LP
169 assert_se(sd_journal_query_unique(j, "NUMBER") >= 0);
170 SD_JOURNAL_FOREACH_UNIQUE(j, data, l)
171 printf("%.*s\n", (int) l, (const char*) data);
172
c6878637 173 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
4ce534f4
LP
174}
175
176int main(int argc, char *argv[]) {
177
178 /* journal_file_open requires a valid machine id */
179 if (access("/etc/machine-id", F_OK) != 0)
180 return log_tests_skipped("/etc/machine-id not found");
181
182 test_setup_logging(LOG_DEBUG);
183
184 /* Run this test twice. Once with old hashing and once with new hashing */
185 assert_se(setenv("SYSTEMD_JOURNAL_KEYED_HASH", "1", 1) >= 0);
186 run_test();
187
188 assert_se(setenv("SYSTEMD_JOURNAL_KEYED_HASH", "0", 1) >= 0);
189 run_test();
cbdca852
LP
190
191 return 0;
192}