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