chiark / gitweb /
2a74104e7aaeb1615ddae5f7e94418fa43137af9
[elogind.git] / src / test / test-fileio.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 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 <stdio.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 #include "util.h"
27 #include "fileio.h"
28 #include "strv.h"
29 #include "env-util.h"
30
31 static void test_parse_env_file(void) {
32         char t[] = "/tmp/test-parse-env-file-XXXXXX";
33         int fd, r;
34         FILE *f;
35         _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
36                         *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
37         _cleanup_strv_free_ char **a = NULL, **b = NULL;
38         char **i;
39         unsigned k;
40
41         fd = mkostemp(t, O_CLOEXEC);
42         assert_se(fd >= 0);
43
44         f = fdopen(fd, "w");
45         assert_se(f);
46
47         fputs("one=BAR   \n"
48               "# comment\n"
49               " # comment \n"
50               "  two   =   bar    \n"
51               "invalid line\n"
52               "three = \"333\n"
53               "xxxx\"\n"
54               "four = \'44\\\"44\'\n"
55               "five = \'55\\\'55\' \"FIVE\" cinco   \n"
56               "six = seis sechs\\\n"
57               " sis\n"
58               "seven=\"sevenval\"#comment\n"
59               "eight=#comment\n"
60               "export nine=nineval\n"
61               "ten=", f);
62
63         fflush(f);
64         fclose(f);
65
66         r = load_env_file(t, NULL, &a);
67         assert_se(r >= 0);
68
69         STRV_FOREACH(i, a)
70                 log_info("Got: <%s>", *i);
71
72         assert_se(streq(a[0], "one=BAR"));
73         assert_se(streq(a[1], "two=bar"));
74         assert_se(streq(a[2], "three=333\nxxxx"));
75         assert_se(streq(a[3], "four=44\"44"));
76         assert_se(streq(a[4], "five=55\'55FIVEcinco"));
77         assert_se(streq(a[5], "six=seis sechs sis"));
78         assert_se(streq(a[6], "seven=sevenval"));
79         assert_se(streq(a[7], "eight="));
80         assert_se(streq(a[8], "export nine=nineval"));
81         assert_se(streq(a[9], "ten="));
82         assert_se(a[10] == NULL);
83
84         strv_env_clean_log(a, "/tmp/test-fileio");
85
86         r = write_env_file("/tmp/test-fileio", a);
87         assert_se(r >= 0);
88
89         r = load_env_file("/tmp/test-fileio", NULL, &b);
90         assert_se(r >= 0);
91
92         k = 0;
93         STRV_FOREACH(i, b) {
94                 log_info("Got2: <%s>", *i);
95                 assert_se(streq(*i, a[k++]));
96         }
97
98         r = parse_env_file(
99                         t, NULL,
100                        "one", &one,
101                        "two", &two,
102                        "three", &three,
103                        "four", &four,
104                        "five", &five,
105                        "six", &six,
106                        "seven", &seven,
107                        "eight", &eight,
108                        "export nine", &nine,
109                        "ten", &ten,
110                        NULL);
111
112         assert_se(r >= 0);
113
114         log_info("one=[%s]", strna(one));
115         log_info("two=[%s]", strna(two));
116         log_info("three=[%s]", strna(three));
117         log_info("four=[%s]", strna(four));
118         log_info("five=[%s]", strna(five));
119         log_info("six=[%s]", strna(six));
120         log_info("seven=[%s]", strna(seven));
121         log_info("eight=[%s]", strna(eight));
122         log_info("export nine=[%s]", strna(nine));
123         log_info("ten=[%s]", strna(nine));
124
125         assert_se(streq(one, "BAR"));
126         assert_se(streq(two, "bar"));
127         assert_se(streq(three, "333\nxxxx"));
128         assert_se(streq(four, "44\"44"));
129         assert_se(streq(five, "55\'55FIVEcinco"));
130         assert_se(streq(six, "seis sechs sis"));
131         assert_se(streq(seven, "sevenval"));
132         assert_se(eight == NULL);
133         assert_se(streq(nine, "nineval"));
134         assert_se(ten == NULL);
135
136         unlink(t);
137         unlink("/tmp/test-fileio");
138 }
139
140 int main(int argc, char *argv[]) {
141         test_parse_env_file();
142         return 0;
143 }