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