]>
git.proxmox.com Git - systemd.git/blob - src/test/test-fstab-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "fstab-util.h"
8 #include "string-util.h"
11 int fstab_filter_options(const char *opts, const char *names,
12 const char **namefound, char **value, char **filtered);
15 static void do_fstab_filter_options(const char *opts
,
18 const char *name_expected
,
19 const char *value_expected
,
20 const char *filtered_expected
) {
24 _cleanup_free_
char *value
, *filtered
;
26 r
= fstab_filter_options(opts
, remove
, &name
, &value
, &filtered
);
27 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 assert_se(r
== r_expected
);
31 assert_se(streq_ptr(name
, name_expected
));
32 assert_se(streq_ptr(value
, value_expected
));
33 assert_se(streq_ptr(filtered
, filtered_expected
?: opts
));
35 /* also test the malloc-less mode */
36 r
= fstab_filter_options(opts
, remove
, &name
, NULL
, NULL
);
37 log_info("\"%s\" → %d, \"%s\", expected %d, \"%s\"",
39 r_expected
, name_expected
);
40 assert_se(r
== r_expected
);
41 assert_se(streq_ptr(name
, name_expected
));
44 static void test_fstab_filter_options(void) {
45 do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, "opt", "0", "");
46 do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, "opt", "0", "");
47 do_fstab_filter_options("opt", "opt\0x-opt\0", 1, "opt", NULL
, "");
48 do_fstab_filter_options("opt", "x-opt\0opt\0", 1, "opt", NULL
, "");
49 do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, "x-opt", NULL
, "");
51 do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, "opt", "0", "other");
52 do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, "opt", "0", "other");
53 do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, "opt", NULL
, "other");
54 do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, "opt", NULL
, "other");
55 do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, "x-opt", NULL
, "other");
57 do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
58 do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
59 do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
61 do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first");
62 do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first=1");
63 do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, "opt", "", "first");
64 do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, "opt", NULL
, "first=1");
65 do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, "x-opt", NULL
, "first=1");
67 do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, "opt", "0", "first,last=1");
68 do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, "opt", "0", "first=1,last=2");
69 do_fstab_filter_options("first,opt,last", "opt\0", 1, "opt", NULL
, "first,last");
70 do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, "opt", NULL
, "first=1,last");
71 do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, "opt", NULL
, "first=,last");
73 /* check repeated options */
74 do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, "noopt", "1", "first,last=1");
75 do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, "opt", "1", "first=1,last=2");
76 do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
77 do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
79 /* check that semicolons are not misinterpreted */
80 do_fstab_filter_options("opt=0;", "opt\0", 1, "opt", "0;", "");
81 do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL
, NULL
, NULL
);
82 do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
84 /* check that spaces are not misinterpreted */
85 do_fstab_filter_options("opt=0 ", "opt\0", 1, "opt", "0 ", "");
86 do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL
, NULL
, NULL
);
87 do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, NULL
, NULL
, NULL
);
89 /* check function will NULL args */
90 do_fstab_filter_options(NULL
, "opt\0", 0, NULL
, NULL
, "");
91 do_fstab_filter_options("", "opt\0", 0, NULL
, NULL
, "");
94 static void test_fstab_find_pri(void) {
97 assert_se(fstab_find_pri("pri", &pri
) == 0);
100 assert_se(fstab_find_pri("pri=11", &pri
) == 1);
101 assert_se(pri
== 11);
103 assert_se(fstab_find_pri("opt,pri=12,opt", &pri
) == 1);
104 assert_se(pri
== 12);
106 assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri
) == 1);
107 assert_se(pri
== 13);
110 static void test_fstab_yes_no_option(void) {
111 assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
112 assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
113 assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
114 assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
115 assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
118 static void test_fstab_node_to_udev_node(void) {
121 n
= fstab_node_to_udev_node("LABEL=applé/jack");
123 assert_se(streq(n
, "/dev/disk/by-label/applé\\x2fjack"));
126 n
= fstab_node_to_udev_node("PARTLABEL=pinkié pie");
128 assert_se(streq(n
, "/dev/disk/by-partlabel/pinkié\\x20pie"));
131 n
= fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
133 assert_se(streq(n
, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
136 n
= fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
138 assert_se(streq(n
, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
141 n
= fstab_node_to_udev_node("PONIES=awesome");
143 assert_se(streq(n
, "PONIES=awesome"));
146 n
= fstab_node_to_udev_node("/dev/xda1");
148 assert_se(streq(n
, "/dev/xda1"));
153 test_fstab_filter_options();
154 test_fstab_find_pri();
155 test_fstab_yes_no_option();
156 test_fstab_node_to_udev_node();