LCOV - code coverage report
Current view: top level - test - test-journal-importer.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 41 41 100.0 %
Date: 2019-08-22 15:41:25 Functions: 4 4 100.0 %

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

Generated by: LCOV version 1.14