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