chiark / gitweb /
7adf2efdd1b8ae70261006b97e55999822c27736
[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;
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               "seven=", f);
57
58         fflush(f);
59         fclose(f);
60
61         r = parse_env_file(
62                         t, NULL,
63                        "one", &one,
64                        "two", &two,
65                        "three", &three,
66                        "four", &four,
67                        "five", &five,
68                        "six", &six,
69                        "seven", &seven,
70                        NULL);
71
72         assert_se(r >= 0);
73
74         log_info("one=[%s]", strna(one));
75         log_info("two=[%s]", strna(two));
76         log_info("three=[%s]", strna(three));
77         log_info("four=[%s]", strna(four));
78         log_info("five=[%s]", strna(five));
79         log_info("six=[%s]", strna(six));
80         log_info("seven=[%s]", strna(seven));
81
82         assert_se(streq(one, "BAR"));
83         assert_se(streq(two, "bar"));
84         assert_se(streq(three, "333\nxxxx"));
85         assert_se(streq(four, "44\"44"));
86         assert_se(streq(five, "55\'55FIVEcinco"));
87         assert_se(streq(six, "seis sechs sis"));
88         assert_se(seven == NULL);
89
90         r = load_env_file(t, NULL, &a);
91         assert_se(r >= 0);
92
93         STRV_FOREACH(i, a)
94                 log_info("Got: <%s>", *i);
95
96         assert_se(streq(a[0], "one=BAR"));
97         assert_se(streq(a[1], "two=bar"));
98         assert_se(streq(a[2], "three=333\nxxxx"));
99         assert_se(streq(a[3], "four=44\"44"));
100         assert_se(streq(a[4], "five=55\'55FIVEcinco"));
101         assert_se(streq(a[5], "six=seis sechs sis"));
102         assert_se(streq(a[6], "seven="));
103         assert_se(a[7] == NULL);
104
105         r = write_env_file("/tmp/test-fileio", a);
106         assert_se(r >= 0);
107
108         r = load_env_file("/tmp/test-fileio", NULL, &b);
109         assert_se(r >= 0);
110
111         k = 0;
112         STRV_FOREACH(i, b) {
113                 log_info("Got2: <%s>", *i);
114                 assert_se(streq(*i, a[k++]));
115         }
116
117         unlink(t);
118         unlink("/tmp/test-fileio");
119 }
120
121 int main(int argc, char *argv[]) {
122         test_parse_env_file();
123         return 0;
124 }