chiark / gitweb /
c83a1ea981918b3fb09c624e2cb7b2339ef53613
[elogind.git] / src / journal / test-journal-interleaving.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Marius Vollmer
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
25 #include <systemd/sd-journal.h>
26
27 #include "journal-file.h"
28 #include "journal-internal.h"
29 #include "util.h"
30 #include "log.h"
31
32 /* This program tests skipping around in a multi-file journal.
33  */
34
35 static void log_assert_errno(const char *text, int eno, const char *file, int line, const char *func) {
36         log_meta(LOG_CRIT, file, line, func,
37                  "'%s' failed at %s:%u (%s): %s.",
38                  text, file, line, func, strerror(eno));
39         abort();
40 }
41
42 #define assert_ret(expr)                                                \
43         do {                                                            \
44                 int _r_ = (expr);                                       \
45                 if (_unlikely_(_r_ < 0))                                \
46                         log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
47         } while (false)
48
49 static JournalFile *test_open (const char *name)
50 {
51         JournalFile *f;
52         assert_ret(journal_file_open(name, O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &f));
53         return f;
54 }
55
56 static void test_close (JournalFile *f)
57 {
58         journal_file_close (f);
59 }
60
61 static void test_append_number(JournalFile *f, int n)
62 {
63         char *p;
64         dual_timestamp ts;
65         struct iovec iovec[1];
66
67         dual_timestamp_get(&ts);
68
69         assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
70         iovec[0].iov_base = p;
71         iovec[0].iov_len = strlen(p);
72         assert_ret(journal_file_append_entry(f, &ts, iovec, 1, NULL, NULL, NULL));
73         free (p);
74 }
75
76 static void test_check_number (sd_journal *j, int n)
77 {
78         const void *d;
79         char *k;
80         size_t l;
81         int x;
82
83         assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
84         assert_se(k = strndup(d, l));
85         printf("%s\n", k);
86
87         assert_se(safe_atoi(k + 7, &x) >= 0);
88         assert_se(n == x);
89 }
90
91 static void test_check_numbers_down (sd_journal *j, int count)
92 {
93         for (int i = 1; i <= count; i++) {
94                 int r;
95                 test_check_number(j, i);
96                 assert_ret(r = sd_journal_next(j));
97                 if (i == count)
98                         assert_se(r == 0);
99                 else
100                         assert_se(r == 1);
101         }
102
103 }
104
105 static void test_check_numbers_up (sd_journal *j, int count)
106 {
107         for (int i = count; i >= 1; i--) {
108                 int r;
109                 test_check_number(j, i);
110                 assert_ret(r = sd_journal_previous(j));
111                 if (i == 1)
112                         assert_se(r == 0);
113                 else
114                         assert_se(r == 1);
115         }
116
117 }
118
119 static void setup_sequential(void) {
120         JournalFile *one, *two;
121         one = test_open("one.journal");
122         two = test_open("two.journal");
123         test_append_number(one, 1);
124         test_append_number(one, 2);
125         test_append_number(two, 3);
126         test_append_number(two, 4);
127         test_close(one);
128         test_close(two);
129 }
130
131 static void setup_interleaved(void) {
132         JournalFile *one, *two;
133         one = test_open("one.journal");
134         two = test_open("two.journal");
135         test_append_number(one, 1);
136         test_append_number(two, 2);
137         test_append_number(one, 3);
138         test_append_number(two, 4);
139         test_close(one);
140         test_close(two);
141 }
142
143 static void test_skip(void (*setup)(void))
144 {
145         char t[] = "/tmp/journal-skip-XXXXXX";
146         sd_journal *j;
147         int r;
148
149         log_set_max_level(LOG_DEBUG);
150
151         assert_se(mkdtemp(t));
152         assert_se(chdir(t) >= 0);
153
154         setup();
155
156         /* Seek to head, iterate down.
157          */
158         assert_ret(sd_journal_open_directory(&j, t, 0));
159         assert_ret(sd_journal_seek_head(j));
160         assert_ret(sd_journal_next(j));
161         test_check_numbers_down(j, 4);
162         sd_journal_close(j);
163
164         /* Seek to tail, iterate up.
165          */
166         assert_ret(sd_journal_open_directory(&j, t, 0));
167         assert_ret(sd_journal_seek_tail(j));
168         assert_ret(sd_journal_previous(j));
169         test_check_numbers_up(j, 4);
170         sd_journal_close(j);
171
172         /* Seek to tail, skip to head, iterate down.
173          */
174         assert_ret(sd_journal_open_directory(&j, t, 0));
175         assert_ret(sd_journal_seek_tail(j));
176         assert_ret(r = sd_journal_previous_skip(j, 4));
177         assert_se(r == 4);
178         test_check_numbers_down(j, 4);
179         sd_journal_close(j);
180
181         /* Seek to head, skip to tail, iterate up.
182          */
183         assert_ret(sd_journal_open_directory(&j, t, 0));
184         assert_ret(sd_journal_seek_head(j));
185         assert_ret(r = sd_journal_next_skip(j, 4));
186         assert_se(r == 4);
187         test_check_numbers_up(j, 4);
188         sd_journal_close(j);
189
190         assert_ret(rm_rf_dangerous(t, false, true, false));
191 }
192
193 int main(int argc, char *argv[]) {
194         test_skip(setup_sequential);
195         test_skip(setup_interleaved);
196
197         return 0;
198 }