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