| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "journal-importer.h" |
| 7 |
#include "tests.h" |
| 8 |
|
| 9 |
static void assert_iovec_entry(const struct iovec *iovec, const char* content) { |
| 10 |
assert_se(strlen(content) == iovec->iov_len); |
| 11 |
assert_se(memcmp(content, iovec->iov_base, iovec->iov_len) == 0); |
| 12 |
} |
| 13 |
|
| 14 |
#define COREDUMP_PROC_GROUP \ |
| 15 |
"COREDUMP_PROC_CGROUP=1:name=systemd:/\n" \ |
| 16 |
"0::/user.slice/user-1002.slice/[email protected]/gnome-terminal-server.service\n" |
| 17 |
|
| 18 |
TEST(basic_parsing) { |
| 19 |
_cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1); |
| 20 |
_cleanup_free_ char *journal_data_path = NULL; |
| 21 |
int r; |
| 22 |
|
| 23 |
assert_se(get_testdata_dir("journal-data/journal-1.txt", &journal_data_path) >= 0); |
| 24 |
imp.fd = open(journal_data_path, O_RDONLY|O_CLOEXEC); |
| 25 |
assert_se(imp.fd >= 0); |
| 26 |
|
| 27 |
do |
| 28 |
r = journal_importer_process_data(&imp); |
| 29 |
while (r == 0 && !journal_importer_eof(&imp)); |
| 30 |
assert_se(r == 1); |
| 31 |
|
| 32 |
/* We read one entry, so we should get EOF on next read, but not yet */ |
| 33 |
assert_se(!journal_importer_eof(&imp)); |
| 34 |
|
| 35 |
assert_se(imp.iovw.count == 6); |
| 36 |
assert_iovec_entry(&imp.iovw.iovec[0], "_BOOT_ID=1531fd22ec84429e85ae888b12fadb91"); |
| 37 |
assert_iovec_entry(&imp.iovw.iovec[1], "_TRANSPORT=journal"); |
| 38 |
assert_iovec_entry(&imp.iovw.iovec[2], COREDUMP_PROC_GROUP); |
| 39 |
assert_iovec_entry(&imp.iovw.iovec[3], "COREDUMP_RLIMIT=-1"); |
| 40 |
assert_iovec_entry(&imp.iovw.iovec[4], COREDUMP_PROC_GROUP); |
| 41 |
assert_iovec_entry(&imp.iovw.iovec[5], "_SOURCE_REALTIME_TIMESTAMP=1478389147837945"); |
| 42 |
|
| 43 |
/* Let's check if we get EOF now */ |
| 44 |
r = journal_importer_process_data(&imp); |
| 45 |
assert_se(r == 0); |
| 46 |
assert_se(journal_importer_eof(&imp)); |
| 47 |
} |
| 48 |
|
| 49 |
TEST(bad_input) { |
| 50 |
_cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1); |
| 51 |
_cleanup_free_ char *journal_data_path = NULL; |
| 52 |
int r; |
| 53 |
|
| 54 |
assert_se(get_testdata_dir("journal-data/journal-1.txt", &journal_data_path) >= 0); |
| 55 |
imp.fd = open(journal_data_path, O_RDONLY|O_CLOEXEC); |
| 56 |
assert_se(imp.fd >= 0); |
| 57 |
|
| 58 |
do |
| 59 |
r = journal_importer_process_data(&imp); |
| 60 |
while (!journal_importer_eof(&imp)); |
| 61 |
assert_se(r == 0); /* If we don't have enough input, 0 is returned */ |
| 62 |
|
| 63 |
assert_se(journal_importer_eof(&imp)); |
| 64 |
} |
| 65 |
|
| 66 |
DEFINE_TEST_MAIN(LOG_DEBUG); |