]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fileio.c
shared: rework env file reader
[thirdparty/systemd.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;
36 char **i;
37
38 fd = mkostemp(t, O_CLOEXEC);
39 assert_se(fd >= 0);
40
41 f = fdopen(fd, "w");
42 assert_se(f);
43
44 fputs("one=BAR \n"
45 "# comment\n"
46 " # comment \n"
47 " two = bar \n"
48 "invalid line\n"
49 "three = \"333\n"
50 "xxxx\"\n"
51 "four = \'44\\\"44\'\n"
52 "five = \'55\\\'55\' \"FIVE\" cinco \n"
53 "six = seis sechs\\\n"
54 " sis\n"
55 "seven=", f);
56
57 fflush(f);
58 fclose(f);
59
60 r = parse_env_file(
61 t, NULL,
62 "one", &one,
63 "two", &two,
64 "three", &three,
65 "four", &four,
66 "five", &five,
67 "six", &six,
68 "seven", &seven,
69 NULL);
70
71 assert_se(r >= 0);
72
73 log_info("one=[%s]", strna(one));
74 log_info("two=[%s]", strna(two));
75 log_info("three=[%s]", strna(three));
76 log_info("four=[%s]", strna(four));
77 log_info("five=[%s]", strna(five));
78 log_info("six=[%s]", strna(six));
79 log_info("seven=[%s]", strna(seven));
80
81 assert_se(streq(one, "BAR"));
82 assert_se(streq(two, "bar"));
83 assert_se(streq(three, "333\nxxxx"));
84 assert_se(streq(four, "44\"44"));
85 assert_se(streq(five, "55\'55FIVEcinco"));
86 assert_se(streq(six, "seis sechs sis"));
87 assert_se(seven == NULL);
88
89 r = load_env_file(t, NULL, &a);
90 assert_se(r >= 0);
91
92 STRV_FOREACH(i, a)
93 log_info("Got: %s", *i);
94
95 unlink(t);
96 }
97
98 int main(int argc, char *argv[]) {
99 test_parse_env_file();
100 return 0;
101 }