1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 #include "strv.h"
10 #include "tests.h"
11 
12 /*
13 int fstab_filter_options(
14         const char *opts,
15         const char *names,
16         const char **ret_namefound,
17         const char **ret_value,
18         const char **ret_values,
19         char **ret_filtered);
20 */
21 
do_fstab_filter_options(const char * opts,const char * remove,int r_expected,int r_values_expected,const char * name_expected,const char * value_expected,const char * values_expected,const char * filtered_expected)22 static void do_fstab_filter_options(const char *opts,
23                                     const char *remove,
24                                     int r_expected,
25                                     int r_values_expected,
26                                     const char *name_expected,
27                                     const char *value_expected,
28                                     const char *values_expected,
29                                     const char *filtered_expected) {
30         int r;
31         const char *name;
32         _cleanup_free_ char *value = NULL, *filtered = NULL, *joined = NULL;
33         _cleanup_strv_free_ char **values = NULL;
34 
35         /* test mode which returns the last value */
36 
37         r = fstab_filter_options(opts, remove, &name, &value, NULL, &filtered);
38         log_info("1: \"%s\" → %d, \"%s\", \"%s\", \"%s\", expected %d, \"%s\", \"%s\", \"%s\"",
39                  opts, r, strnull(name), value, filtered,
40                  r_expected, name_expected, value_expected, filtered_expected ?: opts);
41         assert_se(r == r_expected);
42         assert_se(streq_ptr(name, name_expected));
43         assert_se(streq_ptr(value, value_expected));
44         assert_se(streq_ptr(filtered, filtered_expected ?: opts));
45 
46         /* test mode which returns all the values */
47 
48         r = fstab_filter_options(opts, remove, &name, NULL, &values, NULL);
49         assert_se(joined = strv_join(values, ":"));
50         log_info("2: \"%s\" → %d, \"%s\", \"%s\", expected %d, \"%s\", \"%s\"",
51                  opts, r, strnull(name), joined,
52                  r_values_expected, name_expected, values_expected);
53         assert_se(r == r_values_expected);
54         assert_se(streq_ptr(name, r_values_expected > 0 ? name_expected : NULL));
55         assert_se(streq_ptr(joined, values_expected));
56 
57         /* also test the malloc-less mode */
58         r = fstab_filter_options(opts, remove, &name, NULL, NULL, NULL);
59         log_info("3: \"%s\" → %d, \"%s\", expected %d, \"%s\"\n-",
60                  opts, r, strnull(name),
61                  r_expected, name_expected);
62         assert_se(r == r_expected);
63         assert_se(streq_ptr(name, name_expected));
64 }
65 
TEST(fstab_filter_options)66 TEST(fstab_filter_options) {
67         do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, 1, "opt", "0", "0", "");
68         do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, 1, "opt", "0", "0", "");
69         do_fstab_filter_options("opt", "opt\0x-opt\0", 1, 0, "opt", NULL, "", "");
70         do_fstab_filter_options("opt", "x-opt\0opt\0", 1, 0, "opt", NULL, "", "");
71         do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, 0, "x-opt", NULL, "", "");
72 
73         do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, 1, "opt", "0", "0", "other");
74         do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, 1, "opt", "0", "0", "other");
75         do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, 0, "opt", NULL, "", "other");
76         do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, 0, "opt", NULL, "", "other");
77         do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, 0, "x-opt", NULL, "", "other");
78 
79         do_fstab_filter_options("opt=0\\,1,other", "opt\0x-opt\0", 1, 1, "opt", "0,1", "0,1", "other");
80         do_fstab_filter_options("opt=0,other,x-opt\\,foobar", "x-opt\0opt\0", 1, 1, "opt", "0", "0", "other,x-opt\\,foobar");
81         do_fstab_filter_options("opt,other,x-opt\\,part", "opt\0x-opt\0", 1, 0, "opt", NULL, "", "other,x-opt\\,part");
82         do_fstab_filter_options("opt,other,part\\,x-opt", "x-opt\0opt\0", 1, 0, "opt", NULL, "", "other,part\\,x-opt");
83         do_fstab_filter_options("opt,other\\,\\,\\,opt,x-part", "opt\0x-opt\0", 1, 0, "opt", NULL, "", "other\\,\\,\\,opt,x-part");
84 
85         do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL);
86         do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL);
87         do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL);
88 
89         do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, 1, "opt", "0", "0", "first");
90         do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, 1, "opt", "0", "0", "first=1");
91         do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, 1, "opt", "", "", "first");
92         do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, 0, "opt", NULL, "", "first=1");
93         do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, 0, "x-opt", NULL, "", "first=1");
94 
95         do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, 1, "opt", "0", "0", "first,last=1");
96         do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, 1, "opt", "0", "0", "first=1,last=2");
97         do_fstab_filter_options("first,opt,last", "opt\0", 1, 0, "opt", NULL, "", "first,last");
98         do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, 0, "opt", NULL, "", "first=1,last");
99         do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, 0, "opt", NULL, "", "first=,last");
100 
101         /* check repeated options */
102         do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, 1, "noopt", "1", "0:1", "first,last=1");
103         do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, 1, "opt", "1", "0:1", "first=1,last=2");
104         do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, 1, "x-opt", "1", "0:1", "");
105         do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, 1, "x-opt", "1", "0:1", "");
106         do_fstab_filter_options("opt=0,opt=1,opt=,opt=,opt=2", "opt\0noopt\0", 1, 1, "opt", "2", "0:1:::2", "");
107 
108         /* check that semicolons are not misinterpreted */
109         do_fstab_filter_options("opt=0;", "opt\0", 1, 1, "opt", "0;", "0;", "");
110         do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, 0, NULL, NULL, "", NULL);
111         do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL);
112 
113         /* check that spaces are not misinterpreted */
114         do_fstab_filter_options("opt=0 ", "opt\0", 1, 1, "opt", "0 ", "0 ", "");
115         do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, 0, NULL, NULL, "", NULL);
116         do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, 0, NULL, NULL, "", NULL);
117 
118         /* check function with NULL args */
119         do_fstab_filter_options(NULL, "opt\0", 0, 0, NULL, NULL, "", "");
120         do_fstab_filter_options("", "opt\0", 0, 0, NULL, NULL, "", "");
121 
122         /* unnecessary comma separators */
123         do_fstab_filter_options("opt=x,,,,", "opt\0", 1, 1, "opt", "x", "x", "");
124         do_fstab_filter_options(",,,opt=x,,,,", "opt\0", 1, 1, "opt", "x", "x", "");
125 
126         /* escaped characters */
127         do_fstab_filter_options("opt1=\\\\,opt2=\\xff", "opt1\0", 1, 1, "opt1", "\\", "\\", "opt2=\\xff");
128         do_fstab_filter_options("opt1=\\\\,opt2=\\xff", "opt2\0", 1, 1, "opt2", "\\xff", "\\xff", "opt1=\\");
129 }
130 
TEST(fstab_find_pri)131 TEST(fstab_find_pri) {
132         int pri = -1;
133 
134         assert_se(fstab_find_pri("pri", &pri) == 0);
135         assert_se(pri == -1);
136 
137         assert_se(fstab_find_pri("pri=11", &pri) == 1);
138         assert_se(pri == 11);
139 
140         assert_se(fstab_find_pri("pri=-2", &pri) == 1);
141         assert_se(pri == -2);
142 
143         assert_se(fstab_find_pri("opt,pri=12,opt", &pri) == 1);
144         assert_se(pri == 12);
145 
146         assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri) == 1);
147         assert_se(pri == 13);
148 }
149 
TEST(fstab_yes_no_option)150 TEST(fstab_yes_no_option) {
151         assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
152         assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
153         assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
154         assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
155         assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
156 }
157 
TEST(fstab_node_to_udev_node)158 TEST(fstab_node_to_udev_node) {
159         char *n;
160 
161         n = fstab_node_to_udev_node("LABEL=applé/jack");
162         puts(n);
163         assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
164         free(n);
165 
166         n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
167         puts(n);
168         assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
169         free(n);
170 
171         n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
172         puts(n);
173         assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
174         free(n);
175 
176         n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
177         puts(n);
178         assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
179         free(n);
180 
181         n = fstab_node_to_udev_node("PONIES=awesome");
182         puts(n);
183         assert_se(streq(n, "PONIES=awesome"));
184         free(n);
185 
186         n = fstab_node_to_udev_node("/dev/xda1");
187         puts(n);
188         assert_se(streq(n, "/dev/xda1"));
189         free(n);
190 }
191 
192 DEFINE_TEST_MAIN(LOG_INFO);
193