]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fstab-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Zbigniew Jędrzejewski-Szmek
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "alloc-util.h"
22 #include "fstab-util.h"
24 #include "string-util.h"
28 int fstab_filter_options(const char *opts, const char *names,
29 const char **namefound, char **value, char **filtered);
32 static void do_fstab_filter_options(const char *opts
,
35 const char *name_expected
,
36 const char *value_expected
,
37 const char *filtered_expected
) {
41 _cleanup_free_
char *value
, *filtered
;
43 r
= fstab_filter_options(opts
, remove
, &name
, &value
, &filtered
);
44 log_info("\"%s\" → %d, \"%s\", \"%s\", \"%s\", expected %d, \"%s\", \"%s\", \"%s\"",
45 opts
, r
, name
, value
, filtered
,
46 r_expected
, name_expected
, value_expected
, filtered_expected
?: opts
);
47 assert_se(r
== r_expected
);
48 assert_se(streq_ptr(name
, name_expected
));
49 assert_se(streq_ptr(value
, value_expected
));
50 assert_se(streq_ptr(filtered
, filtered_expected
?: opts
));
52 /* also test the malloc-less mode */
53 r
= fstab_filter_options(opts
, remove
, &name
, NULL
, NULL
);
54 log_info("\"%s\" → %d, \"%s\", expected %d, \"%s\"",
56 r_expected
, name_expected
);
57 assert_se(r
== r_expected
);
58 assert_se(streq_ptr(name
, name_expected
));
61 static void test_fstab_filter_options(void) {
62 do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, "opt", "0", "");
63 do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, "opt", "0", "");
64 do_fstab_filter_options("opt", "opt\0x-opt\0", 1, "opt", NULL
, "");
65 do_fstab_filter_options("opt", "x-opt\0opt\0", 1, "opt", NULL
, "");
66 do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, "x-opt", NULL
, "");
68 do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, "opt", "0", "other");
69 do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, "opt", "0", "other");
70 do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, "opt", NULL
, "other");
71 do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, "opt", NULL
, "other");
72 do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, "x-opt", NULL
, "other");
74 do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
75 do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
76 do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
78 do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first");
79 do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first=1");
80 do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, "opt", "", "first");
81 do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, "opt", NULL
, "first=1");
82 do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, "x-opt", NULL
, "first=1");
84 do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, "opt", "0", "first,last=1");
85 do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, "opt", "0", "first=1,last=2");
86 do_fstab_filter_options("first,opt,last", "opt\0", 1, "opt", NULL
, "first,last");
87 do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, "opt", NULL
, "first=1,last");
88 do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, "opt", NULL
, "first=,last");
90 /* check repeated options */
91 do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, "noopt", "1", "first,last=1");
92 do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, "opt", "1", "first=1,last=2");
93 do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
94 do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
96 /* check that semicolons are not misinterpreted */
97 do_fstab_filter_options("opt=0;", "opt\0", 1, "opt", "0;", "");
98 do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL
, NULL
, NULL
);
99 do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
101 /* check that spaces are not misinterpreted */
102 do_fstab_filter_options("opt=0 ", "opt\0", 1, "opt", "0 ", "");
103 do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL
, NULL
, NULL
);
104 do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
106 /* check function will NULL args */
107 do_fstab_filter_options(NULL
, "opt\0", 0, NULL
, NULL
, "");
108 do_fstab_filter_options("", "opt\0", 0, NULL
, NULL
, "");
111 static void test_fstab_find_pri(void) {
114 assert_se(fstab_find_pri("pri", &pri
) == 0);
115 assert_se(pri
== -1);
117 assert_se(fstab_find_pri("pri=11", &pri
) == 1);
118 assert_se(pri
== 11);
120 assert_se(fstab_find_pri("opt,pri=12,opt", &pri
) == 1);
121 assert_se(pri
== 12);
123 assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri
) == 1);
124 assert_se(pri
== 13);
127 static void test_fstab_yes_no_option(void) {
128 assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
129 assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
130 assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
131 assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
132 assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
135 static void test_fstab_node_to_udev_node(void) {
138 n
= fstab_node_to_udev_node("LABEL=applé/jack");
140 assert_se(streq(n
, "/dev/disk/by-label/applé\\x2fjack"));
143 n
= fstab_node_to_udev_node("PARTLABEL=pinkié pie");
145 assert_se(streq(n
, "/dev/disk/by-partlabel/pinkié\\x20pie"));
148 n
= fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
150 assert_se(streq(n
, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
153 n
= fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
155 assert_se(streq(n
, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
158 n
= fstab_node_to_udev_node("PONIES=awesome");
160 assert_se(streq(n
, "PONIES=awesome"));
163 n
= fstab_node_to_udev_node("/dev/xda1");
165 assert_se(streq(n
, "/dev/xda1"));
170 test_fstab_filter_options();
171 test_fstab_find_pri();
172 test_fstab_yes_no_option();
173 test_fstab_node_to_udev_node();