LCOV - code coverage report
Current view: top level - test - test-conf-files.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 85 85 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: 94 160 58.8 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : /***
       3                 :            :   Copyright © 2014 Michael Marineau
       4                 :            : ***/
       5                 :            : 
       6                 :            : #include <stdarg.h>
       7                 :            : #include <stdio.h>
       8                 :            : 
       9                 :            : #include "alloc-util.h"
      10                 :            : #include "conf-files.h"
      11                 :            : #include "fileio.h"
      12                 :            : #include "fs-util.h"
      13                 :            : #include "macro.h"
      14                 :            : #include "mkdir.h"
      15                 :            : #include "parse-util.h"
      16                 :            : #include "path-util.h"
      17                 :            : #include "rm-rf.h"
      18                 :            : #include "string-util.h"
      19                 :            : #include "strv.h"
      20                 :            : #include "tests.h"
      21                 :            : #include "user-util.h"
      22                 :            : #include "util.h"
      23                 :            : 
      24                 :          8 : static void setup_test_dir(char *tmp_dir, const char *files, ...) {
      25                 :            :         va_list ap;
      26                 :            : 
      27         [ -  + ]:          8 :         assert_se(mkdtemp(tmp_dir));
      28                 :            : 
      29                 :          8 :         va_start(ap, files);
      30         [ +  + ]:         48 :         while (files) {
      31                 :         40 :                 _cleanup_free_ char *path;
      32                 :            : 
      33         [ -  + ]:         40 :                 assert_se(path = path_join(tmp_dir, files));
      34                 :         40 :                 (void) mkdir_parents(path, 0755);
      35         [ -  + ]:         40 :                 assert_se(write_string_file(path, "foobar", WRITE_STRING_FILE_CREATE) >= 0);
      36                 :            : 
      37                 :         40 :                 files = va_arg(ap, const char *);
      38                 :            :         }
      39                 :          8 :         va_end(ap);
      40                 :          8 : }
      41                 :            : 
      42                 :          8 : static void test_conf_files_list(bool use_root) {
      43                 :          8 :         char tmp_dir[] = "/tmp/test-conf-files-XXXXXX";
      44                 :          8 :         _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL;
      45                 :            :         const char *root_dir, *search_1, *search_2, *expect_a, *expect_b, *expect_c, *mask;
      46                 :            : 
      47         [ +  - ]:          8 :         log_debug("/* %s(%s) */", __func__, yes_no(use_root));
      48                 :            : 
      49                 :          8 :         setup_test_dir(tmp_dir,
      50                 :            :                        "/dir1/a.conf",
      51                 :            :                        "/dir2/a.conf",
      52                 :            :                        "/dir2/b.conf",
      53                 :            :                        "/dir2/c.foo",
      54                 :            :                        "/dir2/d.conf",
      55                 :            :                        NULL);
      56                 :            : 
      57   [ +  +  +  -  :         40 :         mask = strjoina(tmp_dir, "/dir1/d.conf");
          -  +  -  +  +  
                +  +  - ]
      58         [ -  + ]:          8 :         assert_se(symlink("/dev/null", mask) >= 0);
      59                 :            : 
      60         [ +  + ]:          8 :         if (use_root) {
      61                 :          4 :                 root_dir = tmp_dir;
      62                 :          4 :                 search_1 = "/dir1";
      63                 :          4 :                 search_2 = "/dir2";
      64                 :            :         } else {
      65                 :          4 :                 root_dir = NULL;
      66   [ +  +  +  -  :         20 :                 search_1 = strjoina(tmp_dir, "/dir1");
          -  +  -  +  +  
                +  +  - ]
      67   [ +  +  +  -  :         20 :                 search_2 = strjoina(tmp_dir, "/dir2");
          -  +  -  +  +  
                +  +  - ]
      68                 :            :         }
      69                 :            : 
      70   [ +  +  +  -  :         40 :         expect_a = strjoina(tmp_dir, "/dir1/a.conf");
          -  +  -  +  +  
                +  +  - ]
      71   [ +  +  +  -  :         40 :         expect_b = strjoina(tmp_dir, "/dir2/b.conf");
          -  +  -  +  +  
                +  +  - ]
      72   [ +  +  +  -  :         40 :         expect_c = strjoina(tmp_dir, "/dir2/c.foo");
          -  +  -  +  +  
                +  +  - ]
      73                 :            : 
      74         [ +  - ]:          8 :         log_debug("/* Check when filtered by suffix */");
      75                 :            : 
      76         [ -  + ]:          8 :         assert_se(conf_files_list(&found_files, ".conf", root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0);
      77                 :          8 :         strv_print(found_files);
      78                 :            : 
      79         [ -  + ]:          8 :         assert_se(found_files);
      80         [ -  + ]:          8 :         assert_se(streq_ptr(found_files[0], expect_a));
      81         [ -  + ]:          8 :         assert_se(streq_ptr(found_files[1], expect_b));
      82         [ -  + ]:          8 :         assert_se(!found_files[2]);
      83                 :            : 
      84         [ +  - ]:          8 :         log_debug("/* Check when unfiltered */");
      85         [ -  + ]:          8 :         assert_se(conf_files_list(&found_files2, NULL, root_dir, CONF_FILES_FILTER_MASKED, search_1, search_2, NULL) == 0);
      86                 :          8 :         strv_print(found_files2);
      87                 :            : 
      88         [ -  + ]:          8 :         assert_se(found_files2);
      89         [ -  + ]:          8 :         assert_se(streq_ptr(found_files2[0], expect_a));
      90         [ -  + ]:          8 :         assert_se(streq_ptr(found_files2[1], expect_b));
      91         [ -  + ]:          8 :         assert_se(streq_ptr(found_files2[2], expect_c));
      92         [ -  + ]:          8 :         assert_se(!found_files2[3]);
      93                 :            : 
      94         [ -  + ]:          8 :         assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
      95                 :          8 : }
      96                 :            : 
      97                 :         12 : static void test_conf_files_insert(const char *root) {
      98                 :         12 :         _cleanup_strv_free_ char **s = NULL;
      99                 :            : 
     100         [ +  - ]:         12 :         log_info("/* %s root=%s */", __func__, strempty(root));
     101                 :            : 
     102                 :         12 :         char **dirs = STRV_MAKE("/dir1", "/dir2", "/dir3");
     103                 :            : 
     104                 :            :         _cleanup_free_ const char
     105                 :         24 :                 *foo1 = path_join(root, "/dir1/foo.conf"),
     106                 :         24 :                 *foo2 = path_join(root, "/dir2/foo.conf"),
     107                 :         24 :                 *bar2 = path_join(root, "/dir2/bar.conf"),
     108                 :         24 :                 *zzz3 = path_join(root, "/dir3/zzz.conf"),
     109                 :         24 :                 *whatever = path_join(root, "/whatever.conf");
     110                 :            : 
     111         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
     112         [ -  + ]:         12 :         assert_se(strv_equal(s, STRV_MAKE(foo2)));
     113                 :            : 
     114                 :            :         /* The same file again, https://github.com/systemd/systemd/issues/11124 */
     115         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
     116         [ -  + ]:         12 :         assert_se(strv_equal(s, STRV_MAKE(foo2)));
     117                 :            : 
     118                 :            :         /* Lower priority → new entry is ignored */
     119         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir3/foo.conf") == 0);
     120         [ -  + ]:         12 :         assert_se(strv_equal(s, STRV_MAKE(foo2)));
     121                 :            : 
     122                 :            :         /* Higher priority → new entry replaces */
     123         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir1/foo.conf") == 0);
     124         [ -  + ]:         12 :         assert_se(strv_equal(s, STRV_MAKE(foo1)));
     125                 :            : 
     126                 :            :         /* Earlier basename */
     127         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir2/bar.conf") == 0);
     128         [ -  + ]:         12 :         assert_se(strv_equal(s, STRV_MAKE(bar2, foo1)));
     129                 :            : 
     130                 :            :         /* Later basename */
     131         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir3/zzz.conf") == 0);
     132         [ -  + ]:         12 :         assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
     133                 :            : 
     134                 :            :         /* All lower priority → all ignored */
     135         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir3/zzz.conf") == 0);
     136         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir2/bar.conf") == 0);
     137         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir3/bar.conf") == 0);
     138         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
     139         [ -  + ]:         12 :         assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
     140                 :            : 
     141                 :            :         /* Two entries that don't match any of the directories, but match basename */
     142         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/dir4/zzz.conf") == 0);
     143         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/zzz.conf") == 0);
     144         [ -  + ]:         12 :         assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, zzz3)));
     145                 :            : 
     146                 :            :         /* An entry that doesn't match any of the directories, no match at all */
     147         [ -  + ]:         12 :         assert_se(conf_files_insert(&s, root, dirs, "/whatever.conf") == 0);
     148         [ -  + ]:         12 :         assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, whatever, zzz3)));
     149                 :         12 : }
     150                 :            : 
     151                 :          4 : int main(int argc, char **argv) {
     152                 :          4 :         test_setup_logging(LOG_DEBUG);
     153                 :            : 
     154                 :          4 :         test_conf_files_list(false);
     155                 :          4 :         test_conf_files_list(true);
     156                 :          4 :         test_conf_files_insert(NULL);
     157                 :          4 :         test_conf_files_insert("/root");
     158                 :          4 :         test_conf_files_insert("/root/");
     159                 :            : 
     160                 :          4 :         return 0;
     161                 :            : }

Generated by: LCOV version 1.14