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
30 #include "config-parser.h"
32 static struct weston_config *
33 run_test(const char *text)
35 struct weston_config *config;
36 char file[] = "/tmp/weston-config-parser-test-XXXXXX";
40 len = write(fd, text, strlen(text));
41 assert(len == (int) strlen(text));
43 config = weston_config_parse(fd);
50 static const char t0[] =
51 "# nothing in this file...\n";
53 static const char t1[] =
54 "# comment line here...\n"
71 "contents=live crabs\n"
79 static const char *section_names[] = {
80 "foo", "bar", "stuff", "bucket", "bucket"
83 static const char t2[] =
84 "# invalid section...\n"
85 "[this bracket isn't closed\n";
87 static const char t3[] =
88 "# line without = ...\n"
90 "this line isn't any kind of valid\n";
92 static const char t4[] =
93 "# starting with = ...\n"
95 "=not valid at all\n";
97 int main(int argc, char *argv[])
99 struct weston_config *config;
100 struct weston_config_section *section;
107 config = run_test(t0);
109 weston_config_destroy(config);
111 config = run_test(t1);
113 section = weston_config_get_section(config, "mollusc", NULL, NULL);
114 assert(section == NULL);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
181 while (weston_config_next_section(config, §ion, &name))
182 assert(strcmp(section_names[i++], name) == 0);
185 weston_config_destroy(config);
187 config = run_test(t2);
188 assert(config == NULL);
190 config = run_test(t3);
191 assert(config == NULL);
193 config = run_test(t4);
194 assert(config == NULL);
196 weston_config_destroy(NULL);
197 assert(weston_config_next_section(NULL, NULL, NULL) == 0);
199 section = weston_config_get_section(NULL, "bucket", NULL, NULL);
200 assert(section == NULL);