tests: use variable for test name in weston-tests-env
[platform/upstream/weston.git] / tests / config-parser-test.c
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <unistd.h>
29
30 #include "config-parser.h"
31
32 static struct weston_config *
33 run_test(const char *text)
34 {
35         struct weston_config *config;
36         char file[] = "/tmp/weston-config-parser-test-XXXXXX";
37         int fd, len;
38
39         fd = mkstemp(file);
40         len = write(fd, text, strlen(text));
41         assert(len == (int) strlen(text));
42
43         config = weston_config_parse(fd);
44         close(fd);
45         unlink(file);
46
47         return config;
48 }
49
50 static const char t0[] =
51         "# nothing in this file...\n";
52
53 static const char t1[] =
54         "# comment line here...\n"
55         "\n"
56         "[foo]\n"
57         "a=b\n"
58         "name=  Roy Batty    \n"
59         "\n"
60         "\n"
61         "[bar]\n"
62         "# more comments\n"
63         "number=5252\n"
64         "flag=false\n"
65         "\n"
66         "[stuff]\n"
67         "flag=     true \n"
68         "\n"
69         "[bucket]\n"
70         "color=blue \n"
71         "contents=live crabs\n"
72         "pinchy=true\n"
73         "\n"
74         "[bucket]\n"
75         "material=plastic \n"
76         "color=red\n"
77         "contents=sand\n";
78
79 static const char *section_names[] = {
80         "foo", "bar", "stuff", "bucket", "bucket"
81 };
82
83 static const char t2[] =
84         "# invalid section...\n"
85         "[this bracket isn't closed\n";
86
87 static const char t3[] =
88         "# line without = ...\n"
89         "[bambam]\n"
90         "this line isn't any kind of valid\n";
91
92 static const char t4[] =
93         "# starting with = ...\n"
94         "[bambam]\n"
95         "=not valid at all\n";
96
97 int main(int argc, char *argv[])
98 {
99         struct weston_config *config;
100         struct weston_config_section *section;
101         const char *name;
102         char *s;
103         int r, b, i;
104         int32_t n;
105         uint32_t u;
106
107         config = run_test(t0);
108         assert(config);
109         weston_config_destroy(config);
110
111         config = run_test(t1);
112         assert(config);
113         section = weston_config_get_section(config, "mollusc", NULL, NULL);
114         assert(section == NULL);
115
116         section = weston_config_get_section(config, "foo", NULL, NULL);
117         r = weston_config_section_get_string(section, "a", &s, NULL);
118         assert(r == 0 && strcmp(s, "b") == 0);
119         free(s);
120
121         section = weston_config_get_section(config, "foo", NULL, NULL);
122         r = weston_config_section_get_string(section, "b", &s, NULL);
123         assert(r == -1 && errno == ENOENT && s == NULL);
124
125         section = weston_config_get_section(config, "foo", NULL, NULL);
126         r = weston_config_section_get_string(section, "name", &s, NULL);
127         assert(r == 0 && strcmp(s, "Roy Batty") == 0);
128         free(s);
129
130         section = weston_config_get_section(config, "bar", NULL, NULL);
131         r = weston_config_section_get_string(section, "a", &s, "boo");
132         assert(r == -1 && errno == ENOENT && strcmp(s, "boo") == 0);
133         free(s);
134
135         section = weston_config_get_section(config, "bar", NULL, NULL);
136         r = weston_config_section_get_int(section, "number", &n, 600);
137         assert(r == 0 && n == 5252);
138
139         section = weston_config_get_section(config, "bar", NULL, NULL);
140         r = weston_config_section_get_int(section, "+++", &n, 700);
141         assert(r == -1 && errno == ENOENT && n == 700);
142
143         section = weston_config_get_section(config, "bar", NULL, NULL);
144         r = weston_config_section_get_uint(section, "number", &u, 600);
145         assert(r == 0 && u == 5252);
146
147         section = weston_config_get_section(config, "bar", NULL, NULL);
148         r = weston_config_section_get_uint(section, "+++", &u, 600);
149         assert(r == -1 && errno == ENOENT && u == 600);
150
151         section = weston_config_get_section(config, "bar", NULL, NULL);
152         r = weston_config_section_get_bool(section, "flag", &b, 600);
153         assert(r == 0 && b == 0);
154
155         section = weston_config_get_section(config, "stuff", NULL, NULL);
156         r = weston_config_section_get_bool(section, "flag", &b, -1);
157         assert(r == 0 && b == 1);
158
159         section = weston_config_get_section(config, "stuff", NULL, NULL);
160         r = weston_config_section_get_bool(section, "bonk", &b, -1);
161         assert(r == -1 && errno == ENOENT && b == -1);
162
163         section = weston_config_get_section(config, "bucket", "color", "blue");
164         r = weston_config_section_get_string(section, "contents", &s, NULL);
165         assert(r == 0 && strcmp(s, "live crabs") == 0);
166         free(s);
167
168         section = weston_config_get_section(config, "bucket", "color", "red");
169         r = weston_config_section_get_string(section, "contents", &s, NULL);
170         assert(r == 0 && strcmp(s, "sand") == 0);
171         free(s);
172
173         section = weston_config_get_section(config, "bucket", "color", "pink");
174         assert(section == NULL);
175         r = weston_config_section_get_string(section, "contents", &s, "eels");
176         assert(r == -1 && errno == ENOENT && strcmp(s, "eels") == 0);
177         free(s);
178
179         section = NULL;
180         i = 0;
181         while (weston_config_next_section(config, &section, &name))
182                 assert(strcmp(section_names[i++], name) == 0);
183         assert(i == 5);
184
185         weston_config_destroy(config);
186
187         config = run_test(t2);
188         assert(config == NULL);
189
190         config = run_test(t3);
191         assert(config == NULL);
192
193         config = run_test(t4);
194         assert(config == NULL);
195
196         weston_config_destroy(NULL);
197         assert(weston_config_next_section(NULL, NULL, NULL) == 0);
198
199         section = weston_config_get_section(NULL, "bucket", NULL, NULL);
200         assert(section == NULL);
201
202         return 0;
203 }