1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
28 static void test_strv_env_delete(void) {
29 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
31 a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
32 b = strv_new("PIEP", "FOO", NULL);
33 c = strv_new("SCHLUMPF", NULL);
35 d = strv_env_delete(a, 2, b, c);
37 assert(streq(d[0], "WALDO=WALDO"));
38 assert(streq(d[1], "WALDO="));
39 assert(strv_length(d) == 2);
42 static void test_strv_env_unset(void) {
43 _cleanup_strv_free_ char **l = NULL;
45 l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
47 strv_env_unset(l, "SCHLUMPF");
49 assert(streq(l[0], "PIEP"));
50 assert(streq(l[1], "NANANANA=YES"));
51 assert(strv_length(l) == 2);
54 static void test_strv_env_set(void) {
55 _cleanup_strv_free_ char **l = NULL, **r = NULL;
57 l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
59 r = strv_env_set(l, "WALDO=WALDO");
61 assert(streq(r[0], "PIEP"));
62 assert(streq(r[1], "SCHLUMPF=SMURFF"));
63 assert(streq(r[2], "NANANANA=YES"));
64 assert(streq(r[3], "WALDO=WALDO"));
65 assert(strv_length(r) == 4);
68 static void test_strv_env_merge(void) {
69 _cleanup_strv_free_ char **a = NULL, **b = NULL, **r = NULL;
71 a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
72 b = strv_new("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
74 r = strv_env_merge(2, a, b);
75 assert(streq(r[0], "FOO="));
76 assert(streq(r[1], "WALDO="));
77 assert(streq(r[2], "PIEP"));
78 assert(streq(r[3], "SCHLUMPF=SMURFF"));
79 assert(streq(r[4], "PIEP="));
80 assert(streq(r[5], "NANANANA=YES"));
81 assert(strv_length(r) == 6);
84 assert(streq(r[0], "PIEP"));
85 assert(streq(r[1], "SCHLUMPF=SMURFF"));
86 assert(streq(r[2], "NANANANA=YES"));
87 assert(strv_length(r) == 3);
90 static void test_replace_env_arg(void) {
96 const char *line[] = {
108 _cleanup_strv_free_ char **r = NULL;
110 r = replace_env_argv((char**) line, (char**) env);
111 assert(streq(r[0], "FOO$FOO"));
112 assert(streq(r[1], "FOO$FOOFOO"));
113 assert(streq(r[2], "FOOBAR BAR$FOO"));
114 assert(streq(r[3], "FOOBAR BAR"));
115 assert(streq(r[4], "BAR BAR"));
116 assert(streq(r[5], "BAR"));
117 assert(streq(r[6], "BAR"));
118 assert(streq(r[7], "BAR BARwaldo"));
119 assert(streq(r[8], "${FOO"));
120 assert(strv_length(r) == 9);
123 static void test_one_normalize(const char *input, const char *output)
125 _cleanup_free_ char *t;
127 t = normalize_env_assignment(input);
128 assert(streq(t, output));
131 static void test_normalize_env_assignment(void) {
132 test_one_normalize("foo=bar", "foo=bar");
133 test_one_normalize("=bar", "=bar");
134 test_one_normalize("foo=", "foo=");
135 test_one_normalize("=", "=");
136 test_one_normalize("", "");
137 test_one_normalize("a=\"waldo\"", "a=waldo");
138 test_one_normalize("a=\"waldo", "a=\"waldo");
139 test_one_normalize("a=waldo\"", "a=waldo\"");
140 test_one_normalize("a=\'", "a='");
141 test_one_normalize("a=\'\'", "a=");
142 test_one_normalize(" xyz ", "xyz");
143 test_one_normalize(" xyz = bar ", "xyz=bar");
144 test_one_normalize(" xyz = 'bar ' ", "xyz=bar ");
145 test_one_normalize(" ' xyz' = 'bar ' ", "' xyz'=bar ");
148 int main(int argc, char *argv[]) {
149 test_strv_env_delete();
150 test_strv_env_unset();
152 test_strv_env_merge();
153 test_replace_env_arg();
154 test_normalize_env_assignment();