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