From 48ccb60d453b06584e58f6ffca47b2204f9c65db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 5 Nov 2016 23:27:00 -0400 Subject: [PATCH] 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). --- .gitignore | 1 + Makefile.am | 12 ++++- src/test/test-journal-importer.c | 74 +++++++++++++++++++++++++++++++ test/journal-data/journal-1.txt | Bin 0 -> 586 bytes 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/test/test-journal-importer.c create mode 100644 test/journal-data/journal-1.txt diff --git a/.gitignore b/.gitignore index fe7859c265..924b995bb3 100644 --- a/.gitignore +++ b/.gitignore @@ -220,6 +220,7 @@ /test-journal /test-journal-enum /test-journal-flush +/test-journal-importer /test-journal-init /test-journal-interleaving /test-journal-match diff --git a/Makefile.am b/Makefile.am index 64343b6b26..fef13fabcd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1598,7 +1598,8 @@ tests += \ test-rlimit-util \ test-signal-util \ test-selinux \ - test-sizeof + test-sizeof \ + test-journal-importer if HAVE_ACL tests += \ @@ -2462,6 +2463,15 @@ test_arphrd_list_SOURCES = \ test_arphrd_list_LDADD = \ libsystemd-shared.la +test_journal_importer_SOURCES = \ + src/test/test-journal-importer.c + +test_journal_importer_LDADD = \ + libsystemd-shared.la + +EXTRA_DIST += \ + test/journal-data/journal-1.txt + # ------------------------------------------------------------------------------ ## .PHONY so it always rebuilds it .PHONY: coverage lcov-run lcov-report coverage-sync diff --git a/src/test/test-journal-importer.c b/src/test/test-journal-importer.c new file mode 100644 index 0000000000..fea5c1d51c --- /dev/null +++ b/src/test/test-journal-importer.c @@ -0,0 +1,74 @@ +/*** + This file is part of systemd. + + Copyright 2016 Zbigniew Jędrzejewski-Szmek + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include + +#include "log.h" +#include "journal-importer.h" +#include "string-util.h" +#include "test-helper.h" + +static void assert_iovec_entry(const struct iovec *iovec, const char* content) { + assert_se(strlen(content) == iovec->iov_len); + assert_se(memcmp(content, iovec->iov_base, iovec->iov_len) == 0); +} + +#define COREDUMP_PROC_GROUP \ + "COREDUMP_PROC_CGROUP=1:name=systemd:/\n" \ + "0::/user.slice/user-1002.slice/user@1002.service/gnome-terminal-server.service\n" + +static void test_basic_parsing(void) { + _cleanup_(journal_importer_cleanup) JournalImporter imp = {}; + int r; + + imp.fd = open(TEST_DATA_DIR("/journal-data/journal-1.txt"), O_RDONLY|O_CLOEXEC); + assert_se(imp.fd >= 0); + + do + r = journal_importer_process_data(&imp); + while (r == 0); + assert_se(r == 1); + + /* We read one entry, so we should get EOF on next read, but not yet */ + assert_se(!journal_importer_eof(&imp)); + + assert_se(imp.iovw.count == 6); + assert_iovec_entry(&imp.iovw.iovec[0], "_BOOT_ID=1531fd22ec84429e85ae888b12fadb91"); + assert_iovec_entry(&imp.iovw.iovec[1], "_TRANSPORT=journal"); + assert_iovec_entry(&imp.iovw.iovec[2], COREDUMP_PROC_GROUP); + assert_iovec_entry(&imp.iovw.iovec[3], "COREDUMP_RLIMIT=-1"); + assert_iovec_entry(&imp.iovw.iovec[4], COREDUMP_PROC_GROUP); + assert_iovec_entry(&imp.iovw.iovec[5], "_SOURCE_REALTIME_TIMESTAMP=1478389147837945"); + + /* Let's check if we get EOF now */ + r = journal_importer_process_data(&imp); + assert_se(r == 0); + assert_se(journal_importer_eof(&imp)); +} + +int main(int argc, char **argv) { + log_set_max_level(LOG_DEBUG); + log_parse_environment(); + + test_basic_parsing(); + + return 0; +} diff --git a/test/journal-data/journal-1.txt b/test/journal-data/journal-1.txt new file mode 100644 index 0000000000000000000000000000000000000000..92a9199a93024812bed9b2b9f1f276e7b2dfbe7a GIT binary patch literal 586 zcmc(cO>crg5Qcl+|3ckQ*oACTi=-hDmn?AJF0k9!h$f(>{r5#Z=*?s2FrUxlon(wj z8(k?aC&{4?F~^X&D53;Yj>53(Y|C3DT471DmDpm&qokFH5#)P}UB@ZK!f|FD=QD}D z?OTD8K?>%Ow+K0&93`VrgtYY@hX^M>(m@i!&RXP|Wt@0mjLw$pIxjMFaaFx6N{ML1 z2^ZH8i3rYAp*E^kn>>BMLP{cvW)O1@%)3&x$yZNL1g6%@O;svg%kRT!+}j;UmCja8 zQJPY#)TAF;H6{3bxfzQ0c5rg~osMqk;{||myf{s6e4TdN&Rx?0L5Lsfw_EMT^M&}@ s9|jj3-8gJ#wP0R7U$$>ka97vsyvS=ApnL0I4l