LCOV - code coverage report
Current view: top level - test - test-tmpfiles.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 34 34 100.0 %
Date: 2019-08-23 13:36:53 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 38 68 55.9 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <fcntl.h>
       4                 :            : #include <stdio.h>
       5                 :            : #include <stdlib.h>
       6                 :            : #include <unistd.h>
       7                 :            : 
       8                 :            : #include "alloc-util.h"
       9                 :            : #include "fd-util.h"
      10                 :            : #include "fileio.h"
      11                 :            : #include "format-util.h"
      12                 :            : #include "fs-util.h"
      13                 :            : #include "log.h"
      14                 :            : #include "process-util.h"
      15                 :            : #include "string-util.h"
      16                 :            : #include "tests.h"
      17                 :            : #include "tmpfile-util.h"
      18                 :            : #include "util.h"
      19                 :            : 
      20                 :          4 : int main(int argc, char** argv) {
      21                 :          4 :         _cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL;
      22                 :          4 :         _cleanup_close_ int fd = -1, fd2 = -1;
      23         [ -  + ]:          4 :         const char *p = argv[1] ?: "/tmp";
      24                 :            :         char *pattern;
      25                 :            : 
      26                 :          4 :         test_setup_logging(LOG_DEBUG);
      27                 :            : 
      28   [ +  +  +  -  :         20 :         pattern = strjoina(p, "/systemd-test-XXXXXX");
          -  +  -  +  +  
                +  +  - ]
      29                 :            : 
      30                 :          4 :         fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
      31         [ -  + ]:          4 :         assert_se(fd >= 0);
      32                 :            : 
      33         [ -  + ]:          4 :         assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd) > 0);
      34                 :          4 :         (void) system(cmd);
      35         [ -  + ]:          4 :         assert_se(readlink_malloc(cmd + 6, &ans) >= 0);
      36         [ +  - ]:          4 :         log_debug("link1: %s", ans);
      37         [ -  + ]:          4 :         assert_se(endswith(ans, " (deleted)"));
      38                 :            : 
      39                 :          4 :         fd2 = mkostemp_safe(pattern);
      40         [ -  + ]:          4 :         assert_se(fd >= 0);
      41         [ -  + ]:          4 :         assert_se(unlink(pattern) == 0);
      42                 :            : 
      43         [ -  + ]:          4 :         assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd2) > 0);
      44                 :          4 :         (void) system(cmd2);
      45         [ -  + ]:          4 :         assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0);
      46         [ +  - ]:          4 :         log_debug("link2: %s", ans2);
      47         [ -  + ]:          4 :         assert_se(endswith(ans2, " (deleted)"));
      48                 :            : 
      49   [ +  +  +  -  :         20 :         pattern = strjoina(p, "/tmpfiles-test");
          -  +  -  +  +  
                +  +  - ]
      50         [ -  + ]:          4 :         assert_se(tempfn_random(pattern, NULL, &d) >= 0);
      51                 :            : 
      52                 :          4 :         fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
      53         [ -  + ]:          4 :         assert_se(fd >= 0);
      54         [ -  + ]:          4 :         assert_se(write(fd, "foobar\n", 7) == 7);
      55                 :            : 
      56         [ -  + ]:          4 :         assert_se(touch(d) >= 0);
      57         [ -  + ]:          4 :         assert_se(link_tmpfile(fd, tmp, d) == -EEXIST);
      58         [ -  + ]:          4 :         assert_se(unlink(d) >= 0);
      59         [ -  + ]:          4 :         assert_se(link_tmpfile(fd, tmp, d) >= 0);
      60                 :            : 
      61         [ -  + ]:          4 :         assert_se(read_one_line_file(d, &line) >= 0);
      62         [ -  + ]:          4 :         assert_se(streq(line, "foobar"));
      63         [ -  + ]:          4 :         assert_se(unlink(d) >= 0);
      64                 :            : 
      65                 :          4 :         return 0;
      66                 :            : }

Generated by: LCOV version 1.14