]>
Commit | Line | Data |
---|---|---|
d15d0333 ZJS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2015 Zbigniew Jędrzejewski-Szmek | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include "fstab-util.h" | |
d15d0333 | 23 | #include "log.h" |
07630cea LP |
24 | #include "string-util.h" |
25 | #include "util.h" | |
d15d0333 ZJS |
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 | ||
a75f4e2a ZJS |
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 | ||
b9f111b9 ZJS |
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 | ||
d15d0333 ZJS |
135 | int main(void) { |
136 | test_fstab_filter_options(); | |
a75f4e2a | 137 | test_fstab_find_pri(); |
b9f111b9 | 138 | test_fstab_yes_no_option(); |
d15d0333 | 139 | } |