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