]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal-stream.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / journal / test-journal-stream.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #include "sd-journal.h"
25
26 #include "alloc-util.h"
27 #include "journal-file.h"
28 #include "journal-internal.h"
29 #include "log.h"
30 #include "macro.h"
31 #include "parse-util.h"
32 #include "rm-rf.h"
33 #include "util.h"
34
35 #define N_ENTRIES 200
36
37 static void verify_contents(sd_journal *j, unsigned skip) {
38 unsigned i;
39
40 assert_se(j);
41
42 i = 0;
43 SD_JOURNAL_FOREACH(j) {
44 const void *d;
45 char *k, *c;
46 size_t l;
47 unsigned u = 0;
48
49 assert_se(sd_journal_get_cursor(j, &k) >= 0);
50 printf("cursor: %s\n", k);
51 free(k);
52
53 assert_se(sd_journal_get_data(j, "MAGIC", &d, &l) >= 0);
54 printf("\t%.*s\n", (int) l, (const char*) d);
55
56 assert_se(sd_journal_get_data(j, "NUMBER", &d, &l) >= 0);
57 assert_se(k = strndup(d, l));
58 printf("\t%s\n", k);
59
60 if (skip > 0) {
61 assert_se(safe_atou(k + 7, &u) >= 0);
62 assert_se(i == u);
63 i += skip;
64 }
65
66 free(k);
67
68 assert_se(sd_journal_get_cursor(j, &c) >= 0);
69 assert_se(sd_journal_test_cursor(j, c) > 0);
70 free(c);
71 }
72
73 if (skip > 0)
74 assert_se(i == N_ENTRIES);
75 }
76
77 int main(int argc, char *argv[]) {
78 JournalFile *one, *two, *three;
79 char t[] = "/tmp/journal-stream-XXXXXX";
80 unsigned i;
81 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
82 char *z;
83 const void *data;
84 size_t l;
85 dual_timestamp previous_ts = DUAL_TIMESTAMP_NULL;
86
87 /* journal_file_open requires a valid machine id */
88 if (access("/etc/machine-id", F_OK) != 0)
89 return EXIT_TEST_SKIP;
90
91 log_set_max_level(LOG_DEBUG);
92
93 assert_se(mkdtemp(t));
94 assert_se(chdir(t) >= 0);
95
96 assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, NULL, &one) == 0);
97 assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, NULL, &two) == 0);
98 assert_se(journal_file_open(-1, "three.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, NULL, &three) == 0);
99
100 for (i = 0; i < N_ENTRIES; i++) {
101 char *p, *q;
102 dual_timestamp ts;
103 struct iovec iovec[2];
104
105 dual_timestamp_get(&ts);
106
107 if (ts.monotonic <= previous_ts.monotonic)
108 ts.monotonic = previous_ts.monotonic + 1;
109
110 if (ts.realtime <= previous_ts.realtime)
111 ts.realtime = previous_ts.realtime + 1;
112
113 previous_ts = ts;
114
115 assert_se(asprintf(&p, "NUMBER=%u", i) >= 0);
116 iovec[0].iov_base = p;
117 iovec[0].iov_len = strlen(p);
118
119 assert_se(asprintf(&q, "MAGIC=%s", i % 5 == 0 ? "quux" : "waldo") >= 0);
120
121 iovec[1].iov_base = q;
122 iovec[1].iov_len = strlen(q);
123
124 if (i % 10 == 0)
125 assert_se(journal_file_append_entry(three, &ts, iovec, 2, NULL, NULL, NULL) == 0);
126 else {
127 if (i % 3 == 0)
128 assert_se(journal_file_append_entry(two, &ts, iovec, 2, NULL, NULL, NULL) == 0);
129
130 assert_se(journal_file_append_entry(one, &ts, iovec, 2, NULL, NULL, NULL) == 0);
131 }
132
133 free(p);
134 free(q);
135 }
136
137 (void) journal_file_close(one);
138 (void) journal_file_close(two);
139 (void) journal_file_close(three);
140
141 assert_se(sd_journal_open_directory(&j, t, 0) >= 0);
142
143 assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
144 SD_JOURNAL_FOREACH_BACKWARDS(j) {
145 _cleanup_free_ char *c;
146
147 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
148 printf("\t%.*s\n", (int) l, (const char*) data);
149
150 assert_se(sd_journal_get_cursor(j, &c) >= 0);
151 assert_se(sd_journal_test_cursor(j, c) > 0);
152 }
153
154 SD_JOURNAL_FOREACH(j) {
155 _cleanup_free_ char *c;
156
157 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
158 printf("\t%.*s\n", (int) l, (const char*) data);
159
160 assert_se(sd_journal_get_cursor(j, &c) >= 0);
161 assert_se(sd_journal_test_cursor(j, c) > 0);
162 }
163
164 sd_journal_flush_matches(j);
165
166 verify_contents(j, 1);
167
168 printf("NEXT TEST\n");
169 assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
170
171 assert_se(z = journal_make_match_string(j));
172 printf("resulting match expression is: %s\n", z);
173 free(z);
174
175 verify_contents(j, 5);
176
177 printf("NEXT TEST\n");
178 sd_journal_flush_matches(j);
179 assert_se(sd_journal_add_match(j, "MAGIC=waldo", 0) >= 0);
180 assert_se(sd_journal_add_match(j, "NUMBER=10", 0) >= 0);
181 assert_se(sd_journal_add_match(j, "NUMBER=11", 0) >= 0);
182 assert_se(sd_journal_add_match(j, "NUMBER=12", 0) >= 0);
183
184 assert_se(z = journal_make_match_string(j));
185 printf("resulting match expression is: %s\n", z);
186 free(z);
187
188 verify_contents(j, 0);
189
190 assert_se(sd_journal_query_unique(j, "NUMBER") >= 0);
191 SD_JOURNAL_FOREACH_UNIQUE(j, data, l)
192 printf("%.*s\n", (int) l, (const char*) data);
193
194 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
195
196 return 0;
197 }