]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fstab-util.c
Merge pull request #8676 from keszybz/drop-license-boilerplate
[thirdparty/systemd.git] / src / test / test-fstab-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2015 Zbigniew Jędrzejewski-Szmek
6 ***/
7
8 #include "alloc-util.h"
9 #include "fstab-util.h"
10 #include "log.h"
11 #include "string-util.h"
12 #include "util.h"
13
14 /*
15 int fstab_filter_options(const char *opts, const char *names,
16 const char **namefound, char **value, char **filtered);
17 */
18
19 static void do_fstab_filter_options(const char *opts,
20 const char *remove,
21 int r_expected,
22 const char *name_expected,
23 const char *value_expected,
24 const char *filtered_expected) {
25
26 int r;
27 const char *name;
28 _cleanup_free_ char *value, *filtered;
29
30 r = fstab_filter_options(opts, remove, &name, &value, &filtered);
31 log_info("\"%s\" → %d, \"%s\", \"%s\", \"%s\", expected %d, \"%s\", \"%s\", \"%s\"",
32 opts, r, name, value, filtered,
33 r_expected, name_expected, value_expected, filtered_expected ?: opts);
34 assert_se(r == r_expected);
35 assert_se(streq_ptr(name, name_expected));
36 assert_se(streq_ptr(value, value_expected));
37 assert_se(streq_ptr(filtered, filtered_expected ?: opts));
38
39 /* also test the malloc-less mode */
40 r = fstab_filter_options(opts, remove, &name, NULL, NULL);
41 log_info("\"%s\" → %d, \"%s\", expected %d, \"%s\"",
42 opts, r, name,
43 r_expected, name_expected);
44 assert_se(r == r_expected);
45 assert_se(streq_ptr(name, name_expected));
46 }
47
48 static void test_fstab_filter_options(void) {
49 do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, "opt", "0", "");
50 do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, "opt", "0", "");
51 do_fstab_filter_options("opt", "opt\0x-opt\0", 1, "opt", NULL, "");
52 do_fstab_filter_options("opt", "x-opt\0opt\0", 1, "opt", NULL, "");
53 do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, "x-opt", NULL, "");
54
55 do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, "opt", "0", "other");
56 do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, "opt", "0", "other");
57 do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, "opt", NULL, "other");
58 do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, "opt", NULL, "other");
59 do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, "x-opt", NULL, "other");
60
61 do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
62 do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
63 do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
64
65 do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first");
66 do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first=1");
67 do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, "opt", "", "first");
68 do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, "opt", NULL, "first=1");
69 do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, "x-opt", NULL, "first=1");
70
71 do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, "opt", "0", "first,last=1");
72 do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, "opt", "0", "first=1,last=2");
73 do_fstab_filter_options("first,opt,last", "opt\0", 1, "opt", NULL, "first,last");
74 do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, "opt", NULL, "first=1,last");
75 do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, "opt", NULL, "first=,last");
76
77 /* check repeated options */
78 do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, "noopt", "1", "first,last=1");
79 do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, "opt", "1", "first=1,last=2");
80 do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
81 do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
82
83 /* check that semicolons are not misinterpreted */
84 do_fstab_filter_options("opt=0;", "opt\0", 1, "opt", "0;", "");
85 do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
86 do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, NULL, NULL, NULL);
87
88 /* check that spaces are not misinterpreted */
89 do_fstab_filter_options("opt=0 ", "opt\0", 1, "opt", "0 ", "");
90 do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
91 do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, NULL, NULL, NULL);
92
93 /* check function will NULL args */
94 do_fstab_filter_options(NULL, "opt\0", 0, NULL, NULL, "");
95 do_fstab_filter_options("", "opt\0", 0, NULL, NULL, "");
96 }
97
98 static void test_fstab_find_pri(void) {
99 int pri = -1;
100
101 assert_se(fstab_find_pri("pri", &pri) == 0);
102 assert_se(pri == -1);
103
104 assert_se(fstab_find_pri("pri=11", &pri) == 1);
105 assert_se(pri == 11);
106
107 assert_se(fstab_find_pri("opt,pri=12,opt", &pri) == 1);
108 assert_se(pri == 12);
109
110 assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri) == 1);
111 assert_se(pri == 13);
112 }
113
114 static void test_fstab_yes_no_option(void) {
115 assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
116 assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
117 assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
118 assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
119 assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
120 }
121
122 static void test_fstab_node_to_udev_node(void) {
123 char *n;
124
125 n = fstab_node_to_udev_node("LABEL=applé/jack");
126 puts(n);
127 assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
128 free(n);
129
130 n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
131 puts(n);
132 assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
133 free(n);
134
135 n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
136 puts(n);
137 assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
138 free(n);
139
140 n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
141 puts(n);
142 assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
143 free(n);
144
145 n = fstab_node_to_udev_node("PONIES=awesome");
146 puts(n);
147 assert_se(streq(n, "PONIES=awesome"));
148 free(n);
149
150 n = fstab_node_to_udev_node("/dev/xda1");
151 puts(n);
152 assert_se(streq(n, "/dev/xda1"));
153 free(n);
154 }
155
156 int main(void) {
157 test_fstab_filter_options();
158 test_fstab_find_pri();
159 test_fstab_yes_no_option();
160 test_fstab_node_to_udev_node();
161
162 return 0;
163 }