]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-env-replace.c
tests: more tests
[thirdparty/systemd.git] / src / test / test-env-replace.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
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 <unistd.h>
23 #include <string.h>
24
25 #include "util.h"
26 #include "strv.h"
27 #include "env-util.h"
28
29 static void test_strv_env_delete(void) {
30 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
31
32 a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
33 assert_se(a);
34
35 b = strv_new("PIEP", "FOO", NULL);
36 assert_se(b);
37
38 c = strv_new("SCHLUMPF", NULL);
39 assert_se(c);
40
41 d = strv_env_delete(a, 2, b, c);
42 assert_se(d);
43
44 assert_se(streq(d[0], "WALDO=WALDO"));
45 assert_se(streq(d[1], "WALDO="));
46 assert_se(strv_length(d) == 2);
47 }
48
49 static void test_strv_env_unset(void) {
50 _cleanup_strv_free_ char **l = NULL;
51
52 l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
53 assert_se(l);
54
55 assert_se(strv_env_unset(l, "SCHLUMPF") == l);
56
57 assert_se(streq(l[0], "PIEP"));
58 assert_se(streq(l[1], "NANANANA=YES"));
59 assert_se(strv_length(l) == 2);
60 }
61
62 static void test_strv_env_set(void) {
63 _cleanup_strv_free_ char **l = NULL, **r = NULL;
64
65 l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
66 assert_se(l);
67
68 r = strv_env_set(l, "WALDO=WALDO");
69 assert_se(r);
70
71 assert_se(streq(r[0], "PIEP"));
72 assert_se(streq(r[1], "SCHLUMPF=SMURFF"));
73 assert_se(streq(r[2], "NANANANA=YES"));
74 assert_se(streq(r[3], "WALDO=WALDO"));
75 assert_se(strv_length(r) == 4);
76 }
77
78 static void test_strv_env_merge(void) {
79 _cleanup_strv_free_ char **a = NULL, **b = NULL, **r = NULL;
80
81 a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
82 assert_se(a);
83
84 b = strv_new("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
85 assert_se(b);
86
87 r = strv_env_merge(2, a, b);
88 assert_se(r);
89 assert_se(streq(r[0], "FOO="));
90 assert_se(streq(r[1], "WALDO="));
91 assert_se(streq(r[2], "PIEP"));
92 assert_se(streq(r[3], "SCHLUMPF=SMURFF"));
93 assert_se(streq(r[4], "PIEP="));
94 assert_se(streq(r[5], "NANANANA=YES"));
95 assert_se(strv_length(r) == 6);
96
97 assert_se(strv_env_clean(r) == r);
98 assert_se(streq(r[0], "FOO="));
99 assert_se(streq(r[1], "WALDO="));
100 assert_se(streq(r[2], "SCHLUMPF=SMURFF"));
101 assert_se(streq(r[3], "PIEP="));
102 assert_se(streq(r[4], "NANANANA=YES"));
103 assert_se(strv_length(r) == 5);
104 }
105
106 static void test_replace_env_arg(void) {
107 const char *env[] = {
108 "FOO=BAR BAR",
109 "BAR=waldo",
110 NULL
111 };
112 const char *line[] = {
113 "FOO$FOO",
114 "FOO$FOOFOO",
115 "FOO${FOO}$FOO",
116 "FOO${FOO}",
117 "${FOO}",
118 "$FOO",
119 "$FOO$FOO",
120 "${FOO}${BAR}",
121 "${FOO",
122 NULL
123 };
124 _cleanup_strv_free_ char **r = NULL;
125
126 r = replace_env_argv((char**) line, (char**) env);
127 assert_se(r);
128 assert_se(streq(r[0], "FOO$FOO"));
129 assert_se(streq(r[1], "FOO$FOOFOO"));
130 assert_se(streq(r[2], "FOOBAR BAR$FOO"));
131 assert_se(streq(r[3], "FOOBAR BAR"));
132 assert_se(streq(r[4], "BAR BAR"));
133 assert_se(streq(r[5], "BAR"));
134 assert_se(streq(r[6], "BAR"));
135 assert_se(streq(r[7], "BAR BARwaldo"));
136 assert_se(streq(r[8], "${FOO"));
137 assert_se(strv_length(r) == 9);
138 }
139
140 static void test_one_normalize(const char *input, const char *output) {
141 _cleanup_free_ char *t;
142
143 t = normalize_env_assignment(input);
144 assert_se(t);
145 assert_se(streq(t, output));
146 }
147
148 static void test_normalize_env_assignment(void) {
149 test_one_normalize("foo=bar", "foo=bar");
150 test_one_normalize("=bar", "=bar");
151 test_one_normalize("foo=", "foo=");
152 test_one_normalize("=", "=");
153 test_one_normalize("", "");
154 test_one_normalize("a=\"waldo\"", "a=waldo");
155 test_one_normalize("a=\"waldo", "a=\"waldo");
156 test_one_normalize("a=waldo\"", "a=waldo\"");
157 test_one_normalize("a=\'", "a='");
158 test_one_normalize("a=\'\'", "a=");
159 test_one_normalize(" xyz ", "xyz");
160 test_one_normalize(" xyz = bar ", "xyz=bar");
161 test_one_normalize(" xyz = 'bar ' ", "xyz=bar ");
162 test_one_normalize(" ' xyz' = 'bar ' ", "' xyz'=bar ");
163 }
164
165 static void test_env_clean(void) {
166 _cleanup_strv_free_ char **e;
167
168 e = strv_new("FOOBAR=WALDO",
169 "FOOBAR=WALDO",
170 "FOOBAR",
171 "F",
172 "X=",
173 "F=F",
174 "=",
175 "=F",
176 "",
177 "0000=000",
178 "äöüß=abcd",
179 "abcd=äöüß",
180 "xyz\n=xyz",
181 "xyz=xyz\n",
182 "another=one",
183 "another=final one",
184 NULL);
185 assert_se(e);
186 assert_se(!strv_env_is_valid(e));
187 assert_se(strv_env_clean(e) == e);
188 assert_se(strv_env_is_valid(e));
189
190 assert_se(streq(e[0], "FOOBAR=WALDO"));
191 assert_se(streq(e[1], "X="));
192 assert_se(streq(e[2], "F=F"));
193 assert_se(streq(e[3], "abcd=äöüß"));
194 assert_se(streq(e[4], "another=final one"));
195 assert_se(e[5] == NULL);
196 }
197
198 static void test_env_name_is_valid(void) {
199 assert_se(env_name_is_valid("test"));
200
201 assert_se(!env_name_is_valid(NULL));
202 assert_se(!env_name_is_valid(""));
203 assert_se(!env_name_is_valid("5_starting_with_a_number_is_wrong"));
204 assert_se(!env_name_is_valid("#¤%&?_only_numbers_letters_and_underscore_allowed"));
205 }
206
207 int main(int argc, char *argv[]) {
208 test_strv_env_delete();
209 test_strv_env_unset();
210 test_strv_env_set();
211 test_strv_env_merge();
212 test_replace_env_arg();
213 test_normalize_env_assignment();
214 test_env_clean();
215 test_env_name_is_valid();
216
217 return 0;
218 }