]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fstab-util.c
Merge pull request #1983 from dmedri/master
[thirdparty/systemd.git] / src / test / test-fstab-util.c
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 "alloc-util.h"
23 #include "fstab-util.h"
24 #include "log.h"
25 #include "string-util.h"
26 #include "util.h"
27
28 /*
29 int fstab_filter_options(const char *opts, const char *names,
30 const char **namefound, char **value, char **filtered);
31 */
32
33 static void do_fstab_filter_options(const char *opts,
34 const char *remove,
35 int r_expected,
36 const char *name_expected,
37 const char *value_expected,
38 const char *filtered_expected) {
39
40 int r;
41 const char *name;
42 _cleanup_free_ char *value, *filtered;
43
44 r = fstab_filter_options(opts, remove, &name, &value, &filtered);
45 log_info("\"%s\" → %d, \"%s\", \"%s\", \"%s\", expected %d, \"%s\", \"%s\", \"%s\"",
46 opts, r, name, value, filtered,
47 r_expected, name_expected, value_expected, filtered_expected ?: opts);
48 assert_se(r == r_expected);
49 assert_se(streq_ptr(name, name_expected));
50 assert_se(streq_ptr(value, value_expected));
51 assert_se(streq_ptr(filtered, filtered_expected ?: opts));
52
53 /* also test the malloc-less mode */
54 r = fstab_filter_options(opts, remove, &name, NULL, NULL);
55 log_info("\"%s\" → %d, \"%s\", expected %d, \"%s\"",
56 opts, r, name,
57 r_expected, name_expected);
58 assert_se(r == r_expected);
59 assert_se(streq_ptr(name, name_expected));
60 }
61
62 static void test_fstab_filter_options(void) {
63 do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, "opt", "0", "");
64 do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, "opt", "0", "");
65 do_fstab_filter_options("opt", "opt\0x-opt\0", 1, "opt", NULL, "");
66 do_fstab_filter_options("opt", "x-opt\0opt\0", 1, "opt", NULL, "");
67 do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, "x-opt", NULL, "");
68
69 do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, "opt", "0", "other");
70 do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, "opt", "0", "other");
71 do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, "opt", NULL, "other");
72 do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, "opt", NULL, "other");
73 do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, "x-opt", NULL, "other");
74
75 do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
76 do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
77 do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
78
79 do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first");
80 do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first=1");
81 do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, "opt", "", "first");
82 do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, "opt", NULL, "first=1");
83 do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, "x-opt", NULL, "first=1");
84
85 do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, "opt", "0", "first,last=1");
86 do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, "opt", "0", "first=1,last=2");
87 do_fstab_filter_options("first,opt,last", "opt\0", 1, "opt", NULL, "first,last");
88 do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, "opt", NULL, "first=1,last");
89 do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, "opt", NULL, "first=,last");
90
91 /* check repeated options */
92 do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, "noopt", "1", "first,last=1");
93 do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, "opt", "1", "first=1,last=2");
94 do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
95 do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
96
97 /* check that semicolons are not misinterpreted */
98 do_fstab_filter_options("opt=0;", "opt\0", 1, "opt", "0;", "");
99 do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
100 do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, NULL, NULL, NULL);
101
102 /* check that spaces are not misinterpreted */
103 do_fstab_filter_options("opt=0 ", "opt\0", 1, "opt", "0 ", "");
104 do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
105 do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, NULL, NULL, NULL);
106
107 /* check function will NULL args */
108 do_fstab_filter_options(NULL, "opt\0", 0, NULL, NULL, "");
109 do_fstab_filter_options("", "opt\0", 0, NULL, NULL, "");
110 }
111
112 static void test_fstab_find_pri(void) {
113 int pri = -1;
114
115 assert_se(fstab_find_pri("pri", &pri) == 0);
116 assert_se(pri == -1);
117
118 assert_se(fstab_find_pri("pri=11", &pri) == 1);
119 assert_se(pri == 11);
120
121 assert_se(fstab_find_pri("opt,pri=12,opt", &pri) == 1);
122 assert_se(pri == 12);
123
124 assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri) == 1);
125 assert_se(pri == 13);
126 }
127
128 static void test_fstab_yes_no_option(void) {
129 assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
130 assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
131 assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
132 assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
133 assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
134 }
135
136 int main(void) {
137 test_fstab_filter_options();
138 test_fstab_find_pri();
139 test_fstab_yes_no_option();
140 }