]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fstab-util.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-fstab-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2015 Zbigniew Jędrzejewski-Szmek
6
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.
11
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.
16
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/>.
19 ***/
20
21 #include "alloc-util.h"
22 #include "fstab-util.h"
23 #include "log.h"
24 #include "string-util.h"
25 #include "util.h"
26
27 /*
28 int fstab_filter_options(const char *opts, const char *names,
29 const char **namefound, char **value, char **filtered);
30 */
31
32 static void do_fstab_filter_options(const char *opts,
33 const char *remove,
34 int r_expected,
35 const char *name_expected,
36 const char *value_expected,
37 const char *filtered_expected) {
38
39 int r;
40 const char *name;
41 _cleanup_free_ char *value, *filtered;
42
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));
51
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\"",
55 opts, r, name,
56 r_expected, name_expected);
57 assert_se(r == r_expected);
58 assert_se(streq_ptr(name, name_expected));
59 }
60
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, "");
67
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");
73
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);
77
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");
83
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");
89
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", "");
95
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);
100
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);
105
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, "");
109 }
110
111 static void test_fstab_find_pri(void) {
112 int pri = -1;
113
114 assert_se(fstab_find_pri("pri", &pri) == 0);
115 assert_se(pri == -1);
116
117 assert_se(fstab_find_pri("pri=11", &pri) == 1);
118 assert_se(pri == 11);
119
120 assert_se(fstab_find_pri("opt,pri=12,opt", &pri) == 1);
121 assert_se(pri == 12);
122
123 assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri) == 1);
124 assert_se(pri == 13);
125 }
126
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);
133 }
134
135 static void test_fstab_node_to_udev_node(void) {
136 char *n;
137
138 n = fstab_node_to_udev_node("LABEL=applé/jack");
139 puts(n);
140 assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
141 free(n);
142
143 n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
144 puts(n);
145 assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
146 free(n);
147
148 n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
149 puts(n);
150 assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
151 free(n);
152
153 n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
154 puts(n);
155 assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
156 free(n);
157
158 n = fstab_node_to_udev_node("PONIES=awesome");
159 puts(n);
160 assert_se(streq(n, "PONIES=awesome"));
161 free(n);
162
163 n = fstab_node_to_udev_node("/dev/xda1");
164 puts(n);
165 assert_se(streq(n, "/dev/xda1"));
166 free(n);
167 }
168
169 int main(void) {
170 test_fstab_filter_options();
171 test_fstab_find_pri();
172 test_fstab_yes_no_option();
173 test_fstab_node_to_udev_node();
174
175 return 0;
176 }