2 * Copyright © 2013 Intel Corporation
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.
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
32 #include "config-parser.h"
34 static struct weston_config *
35 run_test(const char *text)
37 struct weston_config *config;
38 char file[] = "/tmp/weston-config-parser-test-XXXXXX";
42 len = write(fd, text, strlen(text));
43 assert(len == (int) strlen(text));
45 config = weston_config_parse(file);
52 static const char t0[] =
53 "# nothing in this file...\n";
55 static const char t1[] =
56 "# comment line here...\n"
73 "contents=live crabs\n"
81 static const char *section_names[] = {
82 "foo", "bar", "stuff", "bucket", "bucket"
85 static const char t2[] =
86 "# invalid section...\n"
87 "[this bracket isn't closed\n";
89 static const char t3[] =
90 "# line without = ...\n"
92 "this line isn't any kind of valid\n";
94 static const char t4[] =
95 "# starting with = ...\n"
97 "=not valid at all\n";
99 int main(int argc, char *argv[])
101 struct weston_config *config;
102 struct weston_config_section *section;
109 config = run_test(t0);
111 weston_config_destroy(config);
113 config = run_test(t1);
115 section = weston_config_get_section(config, "mollusc", NULL, NULL);
116 assert(section == NULL);
118 section = weston_config_get_section(config, "foo", NULL, NULL);
119 r = weston_config_section_get_string(section, "a", &s, NULL);
120 assert(r == 0 && strcmp(s, "b") == 0);
123 section = weston_config_get_section(config, "foo", NULL, NULL);
124 r = weston_config_section_get_string(section, "b", &s, NULL);
125 assert(r == -1 && errno == ENOENT && s == NULL);
127 section = weston_config_get_section(config, "foo", NULL, NULL);
128 r = weston_config_section_get_string(section, "name", &s, NULL);
129 assert(r == 0 && strcmp(s, "Roy Batty") == 0);
132 section = weston_config_get_section(config, "bar", NULL, NULL);
133 r = weston_config_section_get_string(section, "a", &s, "boo");
134 assert(r == -1 && errno == ENOENT && strcmp(s, "boo") == 0);
137 section = weston_config_get_section(config, "bar", NULL, NULL);
138 r = weston_config_section_get_int(section, "number", &n, 600);
139 assert(r == 0 && n == 5252);
141 section = weston_config_get_section(config, "bar", NULL, NULL);
142 r = weston_config_section_get_int(section, "+++", &n, 700);
143 assert(r == -1 && errno == ENOENT && n == 700);
145 section = weston_config_get_section(config, "bar", NULL, NULL);
146 r = weston_config_section_get_uint(section, "number", &u, 600);
147 assert(r == 0 && u == 5252);
149 section = weston_config_get_section(config, "bar", NULL, NULL);
150 r = weston_config_section_get_uint(section, "+++", &u, 600);
151 assert(r == -1 && errno == ENOENT && u == 600);
153 section = weston_config_get_section(config, "bar", NULL, NULL);
154 r = weston_config_section_get_bool(section, "flag", &b, 600);
155 assert(r == 0 && b == 0);
157 section = weston_config_get_section(config, "stuff", NULL, NULL);
158 r = weston_config_section_get_bool(section, "flag", &b, -1);
159 assert(r == 0 && b == 1);
161 section = weston_config_get_section(config, "stuff", NULL, NULL);
162 r = weston_config_section_get_bool(section, "bonk", &b, -1);
163 assert(r == -1 && errno == ENOENT && b == -1);
165 section = weston_config_get_section(config, "bucket", "color", "blue");
166 r = weston_config_section_get_string(section, "contents", &s, NULL);
167 assert(r == 0 && strcmp(s, "live crabs") == 0);
170 section = weston_config_get_section(config, "bucket", "color", "red");
171 r = weston_config_section_get_string(section, "contents", &s, NULL);
172 assert(r == 0 && strcmp(s, "sand") == 0);
175 section = weston_config_get_section(config, "bucket", "color", "pink");
176 assert(section == NULL);
177 r = weston_config_section_get_string(section, "contents", &s, "eels");
178 assert(r == -1 && errno == ENOENT && strcmp(s, "eels") == 0);
183 while (weston_config_next_section(config, §ion, &name))
184 assert(strcmp(section_names[i++], name) == 0);
187 weston_config_destroy(config);
189 config = run_test(t2);
190 assert(config == NULL);
192 config = run_test(t3);
193 assert(config == NULL);
195 config = run_test(t4);
196 assert(config == NULL);
198 weston_config_destroy(NULL);
199 assert(weston_config_next_section(NULL, NULL, NULL) == 0);
201 section = weston_config_get_section(NULL, "bucket", NULL, NULL);
202 assert(section == NULL);