chiark / gitweb /
test-env-replace: assert results instead of printing them
[elogind.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
28 static void test_strv_env_merge(void) {
29         _cleanup_strv_free_ char **a = NULL, **b = NULL, **r = NULL;
30
31         a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
32         b = strv_new("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
33
34         r = strv_env_merge(2, a, b);
35         assert(streq(r[0], "FOO="));
36         assert(streq(r[1], "WALDO="));
37         assert(streq(r[2], "PIEP"));
38         assert(streq(r[3], "SCHLUMPF=SMURFF"));
39         assert(streq(r[4], "PIEP="));
40         assert(streq(r[5], "NANANANA=YES"));
41         assert(strv_length(r) == 6);
42
43         strv_env_clean(r);
44         assert(streq(r[0], "PIEP"));
45         assert(streq(r[1], "SCHLUMPF=SMURFF"));
46         assert(streq(r[2], "NANANANA=YES"));
47         assert(strv_length(r) == 3);
48 }
49
50 static void test_replace_env_arg(void) {
51         const char *env[] = {
52                 "FOO=BAR BAR",
53                 "BAR=waldo",
54                 NULL
55         };
56         const char *line[] = {
57                 "FOO$FOO",
58                 "FOO$FOOFOO",
59                 "FOO${FOO}$FOO",
60                 "FOO${FOO}",
61                 "${FOO}",
62                 "$FOO",
63                 "$FOO$FOO",
64                 "${FOO}${BAR}",
65                 "${FOO",
66                 NULL
67         };
68         _cleanup_strv_free_ char **r = NULL;
69
70         r = replace_env_argv((char**) line, (char**) env);
71         assert(streq(r[0], "FOO$FOO"));
72         assert(streq(r[1], "FOO$FOOFOO"));
73         assert(streq(r[2], "FOOBAR BAR$FOO"));
74         assert(streq(r[3], "FOOBAR BAR"));
75         assert(streq(r[4], "BAR BAR"));
76         assert(streq(r[5], "BAR"));
77         assert(streq(r[6], "BAR"));
78         assert(streq(r[7], "BAR BARwaldo"));
79         assert(streq(r[8], "${FOO"));
80         assert(strv_length(r) == 9);
81 }
82
83 static void test_one_normalize(const char *input, const char *output)
84 {
85         _cleanup_free_ char *t;
86
87         t = normalize_env_assignment(input);
88         assert(streq(t, output));
89 }
90
91 static void test_normalize_env_assignment(void) {
92         test_one_normalize("foo=bar", "foo=bar");
93         test_one_normalize("=bar", "=bar");
94         test_one_normalize("foo=", "foo=");
95         test_one_normalize("=", "=");
96         test_one_normalize("", "");
97         test_one_normalize("a=\"waldo\"", "a=waldo");
98         test_one_normalize("a=\"waldo", "a=\"waldo");
99         test_one_normalize("a=waldo\"", "a=waldo\"");
100         test_one_normalize("a=\'", "a='");
101         test_one_normalize("a=\'\'", "a=");
102         test_one_normalize(" xyz  ", "xyz");
103         test_one_normalize(" xyz = bar  ", "xyz=bar");
104         test_one_normalize(" xyz = 'bar ' ", "xyz=bar ");
105         test_one_normalize(" ' xyz' = 'bar ' ", "' xyz'=bar ");
106 }
107
108 int main(int argc, char *argv[]) {
109         test_strv_env_merge();
110         test_replace_env_arg();
111         test_normalize_env_assignment();
112
113         return 0;
114 }