Skip to content

Commit 48ccb60

Browse files
committed
test-journal-importer: new test file to check the newly exported importer code
Only one test case is added, but it is enough to check basic sanity of the code (single-line and binary fields and trusted fields, allocation and freeing).
1 parent b18453e commit 48ccb60

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
/test-journal
221221
/test-journal-enum
222222
/test-journal-flush
223+
/test-journal-importer
223224
/test-journal-init
224225
/test-journal-interleaving
225226
/test-journal-match

Makefile.am

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,8 @@ tests += \
15981598
test-rlimit-util \
15991599
test-signal-util \
16001600
test-selinux \
1601-
test-sizeof
1601+
test-sizeof \
1602+
test-journal-importer
16021603

16031604
if HAVE_ACL
16041605
tests += \
@@ -2462,6 +2463,15 @@ test_arphrd_list_SOURCES = \
24622463
test_arphrd_list_LDADD = \
24632464
libsystemd-shared.la
24642465

2466+
test_journal_importer_SOURCES = \
2467+
src/test/test-journal-importer.c
2468+
2469+
test_journal_importer_LDADD = \
2470+
libsystemd-shared.la
2471+
2472+
EXTRA_DIST += \
2473+
test/journal-data/journal-1.txt
2474+
24652475
# ------------------------------------------------------------------------------
24662476
## .PHONY so it always rebuilds it
24672477
.PHONY: coverage lcov-run lcov-report coverage-sync

src/test/test-journal-importer.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 "test-helper.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(TEST_DATA_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);
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+
int main(int argc, char **argv) {
68+
log_set_max_level(LOG_DEBUG);
69+
log_parse_environment();
70+
71+
test_basic_parsing();
72+
73+
return 0;
74+
}

test/journal-data/journal-1.txt

586 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)