]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/test-journal-verify.c
journal: fix verification without key
[thirdparty/systemd.git] / src / journal / test-journal-verify.c
CommitLineData
0284adc6
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 <stdio.h>
23#include <unistd.h>
24#include <fcntl.h>
25
26#include "util.h"
27#include "log.h"
28#include "journal-file.h"
29#include "journal-verify.h"
30
31#define N_ENTRIES 6000
32#define RANDOM_RANGE 77
33
34int main(int argc, char *argv[]) {
35 char t[] = "/tmp/journal-XXXXXX";
36 unsigned n;
37 JournalFile *f;
14d10188 38 const char *verification_key = argv[1];
6c7be122
LP
39 usec_t from, to, total;
40 char a[FORMAT_TIMESTAMP_MAX];
41 char b[FORMAT_TIMESTAMP_MAX];
42 char c[FORMAT_TIMESPAN_MAX];
0284adc6
LP
43
44 log_set_max_level(LOG_DEBUG);
45
46 assert_se(mkdtemp(t));
47 assert_se(chdir(t) >= 0);
48
49 log_info("Generating...");
50
14d10188 51 assert_se(journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, true, !!verification_key, NULL, NULL, NULL, &f) == 0);
0284adc6
LP
52
53 for (n = 0; n < N_ENTRIES; n++) {
54 struct iovec iovec;
55 struct dual_timestamp ts;
56 char *test;
57
58 dual_timestamp_get(&ts);
59
60 assert_se(asprintf(&test, "RANDOM=%lu", random() % RANDOM_RANGE));
61
62 iovec.iov_base = (void*) test;
63 iovec.iov_len = strlen(test);
64
65 assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
66
67 free(test);
68 }
69
70 journal_file_close(f);
71
72 log_info("Verifying...");
73
74 assert_se(journal_file_open("test.journal", O_RDONLY, 0666, false, false, NULL, NULL, NULL, &f) == 0);
3223f44f
LP
75
76 journal_file_print_header(f);
77
6c7be122
LP
78 assert_se(journal_file_verify(f, verification_key, &from, &to, &total) >= 0);
79
80 log_info("=> Validated from %s to %s, %s missing",
81 format_timestamp(a, sizeof(a), from),
82 format_timestamp(b, sizeof(b), to),
83 format_timespan(c, sizeof(c), total > to ? total - to : 0));
84
0284adc6
LP
85 journal_file_close(f);
86
87 log_info("Exiting...");
88
89 assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
90
91 return 0;
92}