]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-journal-importer.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-journal-importer.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2016 Zbigniew Jędrzejewski-Szmek
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25 #include "log.h"
26 #include "journal-importer.h"
27 #include "string-util.h"
28 #include "tests.h"
29
30 static void assert_iovec_entry(const struct iovec *iovec, const char* content) {
31 assert_se(strlen(content) == iovec->iov_len);
32 assert_se(memcmp(content, iovec->iov_base, iovec->iov_len) == 0);
33 }
34
35 #define COREDUMP_PROC_GROUP \
36 "COREDUMP_PROC_CGROUP=1:name=systemd:/\n" \
37 "0::/user.slice/user-1002.slice/user@1002.service/gnome-terminal-server.service\n"
38
39 static void test_basic_parsing(void) {
40 _cleanup_(journal_importer_cleanup) JournalImporter imp = {};
41 int r;
42
43 imp.fd = open(get_testdata_dir("/journal-data/journal-1.txt"), O_RDONLY|O_CLOEXEC);
44 assert_se(imp.fd >= 0);
45
46 do
47 r = journal_importer_process_data(&imp);
48 while (r == 0 && !journal_importer_eof(&imp));
49 assert_se(r == 1);
50
51 /* We read one entry, so we should get EOF on next read, but not yet */
52 assert_se(!journal_importer_eof(&imp));
53
54 assert_se(imp.iovw.count == 6);
55 assert_iovec_entry(&imp.iovw.iovec[0], "_BOOT_ID=1531fd22ec84429e85ae888b12fadb91");
56 assert_iovec_entry(&imp.iovw.iovec[1], "_TRANSPORT=journal");
57 assert_iovec_entry(&imp.iovw.iovec[2], COREDUMP_PROC_GROUP);
58 assert_iovec_entry(&imp.iovw.iovec[3], "COREDUMP_RLIMIT=-1");
59 assert_iovec_entry(&imp.iovw.iovec[4], COREDUMP_PROC_GROUP);
60 assert_iovec_entry(&imp.iovw.iovec[5], "_SOURCE_REALTIME_TIMESTAMP=1478389147837945");
61
62 /* Let's check if we get EOF now */
63 r = journal_importer_process_data(&imp);
64 assert_se(r == 0);
65 assert_se(journal_importer_eof(&imp));
66 }
67
68 static void test_bad_input(void) {
69 _cleanup_(journal_importer_cleanup) JournalImporter imp = {};
70 int r;
71
72 imp.fd = open(get_testdata_dir("/journal-data/journal-2.txt"), O_RDONLY|O_CLOEXEC);
73 assert_se(imp.fd >= 0);
74
75 do
76 r = journal_importer_process_data(&imp);
77 while (!journal_importer_eof(&imp));
78 assert_se(r == 0); /* If we don't have enough input, 0 is returned */
79
80 assert_se(journal_importer_eof(&imp));
81 }
82
83 int main(int argc, char **argv) {
84 log_set_max_level(LOG_DEBUG);
85 log_parse_environment();
86
87 test_basic_parsing();
88 test_bad_input();
89
90 return 0;
91 }