]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fstab-util.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / test / test-fstab-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2015 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "fstab-util.h"
22 #include "log.h"
23 #include "string-util.h"
24 #include "util.h"
25
26 /*
27 int fstab_filter_options(const char *opts, const char *names,
28 const char **namefound, char **value, char **filtered);
29 */
30
31 static void do_fstab_filter_options(const char *opts,
32 const char *remove,
33 int r_expected,
34 const char *name_expected,
35 const char *value_expected,
36 const char *filtered_expected) {
37
38 int r;
39 const char *name;
40 _cleanup_free_ char *value, *filtered;
41
42 r = fstab_filter_options(opts, remove, &name, &value, &filtered);
43 log_info("\"%s\" → %d, \"%s\", \"%s\", \"%s\", expected %d, \"%s\", \"%s\", \"%s\"",
44 opts, r, name, value, filtered,
45 r_expected, name_expected, value_expected, filtered_expected ?: opts);
46 assert_se(r == r_expected);
47 assert_se(streq_ptr(name, name_expected));
48 assert_se(streq_ptr(value, value_expected));
49 assert_se(streq_ptr(filtered, filtered_expected ?: opts));
50
51 /* also test the malloc-less mode */
52 r = fstab_filter_options(opts, remove, &name, NULL, NULL);
53 log_info("\"%s\" → %d, \"%s\", expected %d, \"%s\"",
54 opts, r, name,
55 r_expected, name_expected);
56 assert_se(r == r_expected);
57 assert_se(streq_ptr(name, name_expected));
58 }
59
60 static void test_fstab_filter_options(void) {
61 do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, "opt", "0", "");
62 do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, "opt", "0", "");
63 do_fstab_filter_options("opt", "opt\0x-opt\0", 1, "opt", NULL, "");
64 do_fstab_filter_options("opt", "x-opt\0opt\0", 1, "opt", NULL, "");
65 do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, "x-opt", NULL, "");
66
67 do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, "opt", "0", "other");
68 do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, "opt", "0", "other");
69 do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, "opt", NULL, "other");
70 do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, "opt", NULL, "other");
71 do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, "x-opt", NULL, "other");
72
73 do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
74 do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
75 do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
76
77 do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first");
78 do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first=1");
79 do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, "opt", "", "first");
80 do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, "opt", NULL, "first=1");
81 do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, "x-opt", NULL, "first=1");
82
83 do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, "opt", "0", "first,last=1");
84 do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, "opt", "0", "first=1,last=2");
85 do_fstab_filter_options("first,opt,last", "opt\0", 1, "opt", NULL, "first,last");
86 do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, "opt", NULL, "first=1,last");
87 do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, "opt", NULL, "first=,last");
88
89 /* check repeated options */
90 do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, "noopt", "1", "first,last=1");
91 do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, "opt", "1", "first=1,last=2");
92 do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
93 do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
94
95 /* check that semicolons are not misinterpreted */
96 do_fstab_filter_options("opt=0;", "opt\0", 1, "opt", "0;", "");
97 do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
98 do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, NULL, NULL, NULL);
99
100 /* check that spaces are not misinterpreted */
101 do_fstab_filter_options("opt=0 ", "opt\0", 1, "opt", "0 ", "");
102 do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
103 do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, NULL, NULL, NULL);
104
105 /* check function will NULL args */
106 do_fstab_filter_options(NULL, "opt\0", 0, NULL, NULL, "");
107 do_fstab_filter_options("", "opt\0", 0, NULL, NULL, "");
108 }
109
110 static void test_fstab_find_pri(void) {
111 int pri = -1;
112
113 assert_se(fstab_find_pri("pri", &pri) == 0);
114 assert_se(pri == -1);
115
116 assert_se(fstab_find_pri("pri=11", &pri) == 1);
117 assert_se(pri == 11);
118
119 assert_se(fstab_find_pri("opt,pri=12,opt", &pri) == 1);
120 assert_se(pri == 12);
121
122 assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri) == 1);
123 assert_se(pri == 13);
124 }
125
126 static void test_fstab_yes_no_option(void) {
127 assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
128 assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
129 assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
130 assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
131 assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
132 }
133
134 int main(void) {
135 test_fstab_filter_options();
136 test_fstab_find_pri();
137 test_fstab_yes_no_option();
138 }