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