chiark / gitweb /
ddf965dde5d05d31355c0ebcb42cc4ab40f72158
[elogind.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 "fstab-util.h"
23 #include "util.h"
24 #include "log.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 int main(void) {
111         test_fstab_filter_options();
112 }