]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fstab-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "fstab-util.h"
8 #include "string-util.h"
11 int fstab_filter_options(const char *opts, const char *names,
12 const char **namefound, char **value, char **filtered);
15 static void do_fstab_filter_options(const char *opts
,
18 const char *name_expected
,
19 const char *value_expected
,
20 const char *filtered_expected
) {
23 _cleanup_free_
char *value
= NULL
, *filtered
= NULL
;
25 r
= fstab_filter_options(opts
, remove
, &name
, &value
, &filtered
);
26 log_info("\"%s\" → %d, \"%s\", \"%s\", \"%s\", expected %d, \"%s\", \"%s\", \"%s\"",
27 opts
, r
, name
, value
, filtered
,
28 r_expected
, name_expected
, value_expected
, filtered_expected
?: opts
);
29 assert_se(r
== r_expected
);
30 assert_se(streq_ptr(name
, name_expected
));
31 assert_se(streq_ptr(value
, value_expected
));
32 assert_se(streq_ptr(filtered
, filtered_expected
?: opts
));
34 /* also test the malloc-less mode */
35 r
= fstab_filter_options(opts
, remove
, &name
, NULL
, NULL
);
36 log_info("\"%s\" → %d, \"%s\", expected %d, \"%s\"\n-",
38 r_expected
, name_expected
);
39 assert_se(r
== r_expected
);
40 assert_se(streq_ptr(name
, name_expected
));
43 static void test_fstab_filter_options(void) {
44 do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, "opt", "0", "");
45 do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, "opt", "0", "");
46 do_fstab_filter_options("opt", "opt\0x-opt\0", 1, "opt", NULL
, "");
47 do_fstab_filter_options("opt", "x-opt\0opt\0", 1, "opt", NULL
, "");
48 do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, "x-opt", NULL
, "");
50 do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, "opt", "0", "other");
51 do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, "opt", "0", "other");
52 do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, "opt", NULL
, "other");
53 do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, "opt", NULL
, "other");
54 do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, "x-opt", NULL
, "other");
56 do_fstab_filter_options("opt=0\\,1,other", "opt\0x-opt\0", 1, "opt", "0,1", "other");
57 do_fstab_filter_options("opt=0,other,x-opt\\,foobar", "x-opt\0opt\0", 1, "opt", "0", "other,x-opt\\,foobar");
58 do_fstab_filter_options("opt,other,x-opt\\,part", "opt\0x-opt\0", 1, "opt", NULL
, "other,x-opt\\,part");
59 do_fstab_filter_options("opt,other,part\\,x-opt", "x-opt\0opt\0", 1, "opt", NULL
, "other,part\\,x-opt");
60 do_fstab_filter_options("opt,other\\,\\,\\,opt,x-part", "opt\0x-opt\0", 1, "opt", NULL
, "other\\,\\,\\,opt,x-part");
62 do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
63 do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
64 do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
66 do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first");
67 do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first=1");
68 do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, "opt", "", "first");
69 do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, "opt", NULL
, "first=1");
70 do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, "x-opt", NULL
, "first=1");
72 do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, "opt", "0", "first,last=1");
73 do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, "opt", "0", "first=1,last=2");
74 do_fstab_filter_options("first,opt,last", "opt\0", 1, "opt", NULL
, "first,last");
75 do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, "opt", NULL
, "first=1,last");
76 do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, "opt", NULL
, "first=,last");
78 /* check repeated options */
79 do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, "noopt", "1", "first,last=1");
80 do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, "opt", "1", "first=1,last=2");
81 do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
82 do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
84 /* check that semicolons are not misinterpreted */
85 do_fstab_filter_options("opt=0;", "opt\0", 1, "opt", "0;", "");
86 do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL
, NULL
, NULL
);
87 do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
89 /* check that spaces are not misinterpreted */
90 do_fstab_filter_options("opt=0 ", "opt\0", 1, "opt", "0 ", "");
91 do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL
, NULL
, NULL
);
92 do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
94 /* check function will NULL args */
95 do_fstab_filter_options(NULL
, "opt\0", 0, NULL
, NULL
, "");
96 do_fstab_filter_options("", "opt\0", 0, NULL
, NULL
, "");
99 static void test_fstab_find_pri(void) {
102 assert_se(fstab_find_pri("pri", &pri
) == 0);
103 assert_se(pri
== -1);
105 assert_se(fstab_find_pri("pri=11", &pri
) == 1);
106 assert_se(pri
== 11);
108 assert_se(fstab_find_pri("pri=-2", &pri
) == 1);
109 assert_se(pri
== -2);
111 assert_se(fstab_find_pri("opt,pri=12,opt", &pri
) == 1);
112 assert_se(pri
== 12);
114 assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri
) == 1);
115 assert_se(pri
== 13);
118 static void test_fstab_yes_no_option(void) {
119 assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
120 assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
121 assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
122 assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
123 assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
126 static void test_fstab_node_to_udev_node(void) {
129 n
= fstab_node_to_udev_node("LABEL=applé/jack");
131 assert_se(streq(n
, "/dev/disk/by-label/applé\\x2fjack"));
134 n
= fstab_node_to_udev_node("PARTLABEL=pinkié pie");
136 assert_se(streq(n
, "/dev/disk/by-partlabel/pinkié\\x20pie"));
139 n
= fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
141 assert_se(streq(n
, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
144 n
= fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
146 assert_se(streq(n
, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
149 n
= fstab_node_to_udev_node("PONIES=awesome");
151 assert_se(streq(n
, "PONIES=awesome"));
154 n
= fstab_node_to_udev_node("/dev/xda1");
156 assert_se(streq(n
, "/dev/xda1"));
161 test_fstab_filter_options();
162 test_fstab_find_pri();
163 test_fstab_yes_no_option();
164 test_fstab_node_to_udev_node();