LCOV - code coverage report
Current view: top level - test - test-journal-importer.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 41 41 100.0 %
Date: 2019-08-23 13:36:53 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 16 28 57.1 %

           Branch data     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                 :         24 : static void assert_iovec_entry(const struct iovec *iovec, const char* content) {
      15         [ -  + ]:         24 :         assert_se(strlen(content) == iovec->iov_len);
      16         [ -  + ]:         24 :         assert_se(memcmp(content, iovec->iov_base, iovec->iov_len) == 0);
      17                 :         24 : }
      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                 :          4 : static void test_basic_parsing(void) {
      24                 :          4 :         _cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1);
      25                 :          4 :         _cleanup_free_ char *journal_data_path = NULL;
      26                 :            :         int r;
      27                 :            : 
      28                 :          4 :         journal_data_path = path_join(get_testdata_dir(), "journal-data/journal-1.txt");
      29                 :          4 :         imp.fd = open(journal_data_path, O_RDONLY|O_CLOEXEC);
      30         [ -  + ]:          4 :         assert_se(imp.fd >= 0);
      31                 :            : 
      32                 :            :         do
      33                 :         64 :                 r = journal_importer_process_data(&imp);
      34   [ +  +  +  - ]:         64 :         while (r == 0 && !journal_importer_eof(&imp));
      35         [ -  + ]:          4 :         assert_se(r == 1);
      36                 :            : 
      37                 :            :         /* We read one entry, so we should get EOF on next read, but not yet */
      38         [ -  + ]:          4 :         assert_se(!journal_importer_eof(&imp));
      39                 :            : 
      40         [ -  + ]:          4 :         assert_se(imp.iovw.count == 6);
      41                 :          4 :         assert_iovec_entry(&imp.iovw.iovec[0], "_BOOT_ID=1531fd22ec84429e85ae888b12fadb91");
      42                 :          4 :         assert_iovec_entry(&imp.iovw.iovec[1], "_TRANSPORT=journal");
      43                 :          4 :         assert_iovec_entry(&imp.iovw.iovec[2], COREDUMP_PROC_GROUP);
      44                 :          4 :         assert_iovec_entry(&imp.iovw.iovec[3], "COREDUMP_RLIMIT=-1");
      45                 :          4 :         assert_iovec_entry(&imp.iovw.iovec[4], COREDUMP_PROC_GROUP);
      46                 :          4 :         assert_iovec_entry(&imp.iovw.iovec[5], "_SOURCE_REALTIME_TIMESTAMP=1478389147837945");
      47                 :            : 
      48                 :            :         /* Let's check if we get EOF now */
      49                 :          4 :         r = journal_importer_process_data(&imp);
      50         [ -  + ]:          4 :         assert_se(r == 0);
      51         [ -  + ]:          4 :         assert_se(journal_importer_eof(&imp));
      52                 :          4 : }
      53                 :            : 
      54                 :          4 : static void test_bad_input(void) {
      55                 :          4 :         _cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1);
      56                 :          4 :         _cleanup_free_ char *journal_data_path = NULL;
      57                 :            :         int r;
      58                 :            : 
      59                 :          4 :         journal_data_path = path_join(get_testdata_dir(), "journal-data/journal-2.txt");
      60                 :          4 :         imp.fd = open(journal_data_path, O_RDONLY|O_CLOEXEC);
      61         [ -  + ]:          4 :         assert_se(imp.fd >= 0);
      62                 :            : 
      63                 :            :         do
      64                 :         20 :                 r = journal_importer_process_data(&imp);
      65         [ +  + ]:         20 :         while (!journal_importer_eof(&imp));
      66         [ -  + ]:          4 :         assert_se(r == 0); /* If we don't have enough input, 0 is returned */
      67                 :            : 
      68         [ -  + ]:          4 :         assert_se(journal_importer_eof(&imp));
      69                 :          4 : }
      70                 :            : 
      71                 :          4 : int main(int argc, char **argv) {
      72                 :          4 :         test_setup_logging(LOG_DEBUG);
      73                 :            : 
      74                 :          4 :         test_basic_parsing();
      75                 :          4 :         test_bad_input();
      76                 :            : 
      77                 :          4 :         return 0;
      78                 :            : }

Generated by: LCOV version 1.14