LCOV - code coverage report
Current view: top level - test - test-fstab-util.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 100 100 100.0 %
Date: 2019-08-23 13:36:53 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 31 58 53.4 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <stdio.h>
       4                 :            : 
       5                 :            : #include "alloc-util.h"
       6                 :            : #include "fstab-util.h"
       7                 :            : #include "log.h"
       8                 :            : #include "string-util.h"
       9                 :            : 
      10                 :            : /*
      11                 :            : int fstab_filter_options(const char *opts, const char *names,
      12                 :            :                          const char **namefound, char **value, char **filtered);
      13                 :            : */
      14                 :            : 
      15                 :        140 : static void do_fstab_filter_options(const char *opts,
      16                 :            :                                       const char *remove,
      17                 :            :                                       int r_expected,
      18                 :            :                                       const char *name_expected,
      19                 :            :                                       const char *value_expected,
      20                 :            :                                       const char *filtered_expected) {
      21                 :            : 
      22                 :            :         int r;
      23                 :            :         const char *name;
      24                 :        140 :         _cleanup_free_ char *value, *filtered;
      25                 :            : 
      26                 :        140 :         r = fstab_filter_options(opts, remove, &name, &value, &filtered);
      27   [ +  -  +  + ]:        140 :         log_info("\"%s\" → %d, \"%s\", \"%s\", \"%s\", expected %d, \"%s\", \"%s\", \"%s\"",
      28                 :            :                  opts, r, name, value, filtered,
      29                 :            :                  r_expected, name_expected, value_expected, filtered_expected ?: opts);
      30         [ -  + ]:        140 :         assert_se(r == r_expected);
      31         [ -  + ]:        140 :         assert_se(streq_ptr(name, name_expected));
      32         [ -  + ]:        140 :         assert_se(streq_ptr(value, value_expected));
      33   [ +  +  -  + ]:        140 :         assert_se(streq_ptr(filtered, filtered_expected ?: opts));
      34                 :            : 
      35                 :            :         /* also test the malloc-less mode */
      36                 :        140 :         r = fstab_filter_options(opts, remove, &name, NULL, NULL);
      37         [ +  - ]:        140 :         log_info("\"%s\" → %d, \"%s\", expected %d, \"%s\"",
      38                 :            :                  opts, r, name,
      39                 :            :                  r_expected, name_expected);
      40         [ -  + ]:        140 :         assert_se(r == r_expected);
      41         [ -  + ]:        140 :         assert_se(streq_ptr(name, name_expected));
      42                 :        140 : }
      43                 :            : 
      44                 :          4 : static void test_fstab_filter_options(void) {
      45                 :          4 :         do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, "opt", "0", "");
      46                 :          4 :         do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, "opt", "0", "");
      47                 :          4 :         do_fstab_filter_options("opt", "opt\0x-opt\0", 1, "opt", NULL, "");
      48                 :          4 :         do_fstab_filter_options("opt", "x-opt\0opt\0", 1, "opt", NULL, "");
      49                 :          4 :         do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, "x-opt", NULL, "");
      50                 :            : 
      51                 :          4 :         do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, "opt", "0", "other");
      52                 :          4 :         do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, "opt", "0", "other");
      53                 :          4 :         do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, "opt", NULL, "other");
      54                 :          4 :         do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, "opt", NULL, "other");
      55                 :          4 :         do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, "x-opt", NULL, "other");
      56                 :            : 
      57                 :          4 :         do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
      58                 :          4 :         do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
      59                 :          4 :         do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
      60                 :            : 
      61                 :          4 :         do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first");
      62                 :          4 :         do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first=1");
      63                 :          4 :         do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, "opt", "", "first");
      64                 :          4 :         do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, "opt", NULL, "first=1");
      65                 :          4 :         do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, "x-opt", NULL, "first=1");
      66                 :            : 
      67                 :          4 :         do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, "opt", "0", "first,last=1");
      68                 :          4 :         do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, "opt", "0", "first=1,last=2");
      69                 :          4 :         do_fstab_filter_options("first,opt,last", "opt\0", 1, "opt", NULL, "first,last");
      70                 :          4 :         do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, "opt", NULL, "first=1,last");
      71                 :          4 :         do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, "opt", NULL, "first=,last");
      72                 :            : 
      73                 :            :         /* check repeated options */
      74                 :          4 :         do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, "noopt", "1", "first,last=1");
      75                 :          4 :         do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, "opt", "1", "first=1,last=2");
      76                 :          4 :         do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
      77                 :          4 :         do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
      78                 :            : 
      79                 :            :         /* check that semicolons are not misinterpreted */
      80                 :          4 :         do_fstab_filter_options("opt=0;", "opt\0", 1, "opt", "0;", "");
      81                 :          4 :         do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
      82                 :          4 :         do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, NULL, NULL, NULL);
      83                 :            : 
      84                 :            :         /* check that spaces are not misinterpreted */
      85                 :          4 :         do_fstab_filter_options("opt=0 ", "opt\0", 1, "opt", "0 ", "");
      86                 :          4 :         do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
      87                 :          4 :         do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, NULL, NULL, NULL);
      88                 :            : 
      89                 :            :         /* check function will NULL args */
      90                 :          4 :         do_fstab_filter_options(NULL, "opt\0", 0, NULL, NULL, "");
      91                 :          4 :         do_fstab_filter_options("", "opt\0", 0, NULL, NULL, "");
      92                 :          4 : }
      93                 :            : 
      94                 :          4 : static void test_fstab_find_pri(void) {
      95                 :          4 :         int pri = -1;
      96                 :            : 
      97         [ -  + ]:          4 :         assert_se(fstab_find_pri("pri", &pri) == 0);
      98         [ -  + ]:          4 :         assert_se(pri == -1);
      99                 :            : 
     100         [ -  + ]:          4 :         assert_se(fstab_find_pri("pri=11", &pri) == 1);
     101         [ -  + ]:          4 :         assert_se(pri == 11);
     102                 :            : 
     103         [ -  + ]:          4 :         assert_se(fstab_find_pri("opt,pri=12,opt", &pri) == 1);
     104         [ -  + ]:          4 :         assert_se(pri == 12);
     105                 :            : 
     106         [ -  + ]:          4 :         assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri) == 1);
     107         [ -  + ]:          4 :         assert_se(pri == 13);
     108                 :          4 : }
     109                 :            : 
     110                 :          4 : static void test_fstab_yes_no_option(void) {
     111         [ -  + ]:          4 :         assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
     112         [ -  + ]:          4 :         assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
     113         [ -  + ]:          4 :         assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
     114         [ -  + ]:          4 :         assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
     115         [ -  + ]:          4 :         assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
     116                 :          4 : }
     117                 :            : 
     118                 :          4 : static void test_fstab_node_to_udev_node(void) {
     119                 :            :         char *n;
     120                 :            : 
     121                 :          4 :         n = fstab_node_to_udev_node("LABEL=applé/jack");
     122                 :          4 :         puts(n);
     123         [ -  + ]:          4 :         assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
     124                 :          4 :         free(n);
     125                 :            : 
     126                 :          4 :         n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
     127                 :          4 :         puts(n);
     128         [ -  + ]:          4 :         assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
     129                 :          4 :         free(n);
     130                 :            : 
     131                 :          4 :         n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
     132                 :          4 :         puts(n);
     133         [ -  + ]:          4 :         assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
     134                 :          4 :         free(n);
     135                 :            : 
     136                 :          4 :         n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
     137                 :          4 :         puts(n);
     138         [ -  + ]:          4 :         assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
     139                 :          4 :         free(n);
     140                 :            : 
     141                 :          4 :         n = fstab_node_to_udev_node("PONIES=awesome");
     142                 :          4 :         puts(n);
     143         [ -  + ]:          4 :         assert_se(streq(n, "PONIES=awesome"));
     144                 :          4 :         free(n);
     145                 :            : 
     146                 :          4 :         n = fstab_node_to_udev_node("/dev/xda1");
     147                 :          4 :         puts(n);
     148         [ -  + ]:          4 :         assert_se(streq(n, "/dev/xda1"));
     149                 :          4 :         free(n);
     150                 :          4 : }
     151                 :            : 
     152                 :          4 : int main(void) {
     153                 :          4 :         test_fstab_filter_options();
     154                 :          4 :         test_fstab_find_pri();
     155                 :          4 :         test_fstab_yes_no_option();
     156                 :          4 :         test_fstab_node_to_udev_node();
     157                 :            : 
     158                 :          4 :         return 0;
     159                 :            : }

Generated by: LCOV version 1.14