blob: 12a8fbd890e141acc6b69233e3ad1cab33776eed [file] [log] [blame]
Kristian Høgsberg73274712013-04-01 12:41:23 -04001/*
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
29#include "config-parser.h"
30
31static struct weston_config *
32run_test(const char *text)
33{
34 struct weston_config *config;
35 char file[] = "/tmp/weston-config-parser-test-XXXXXX";
36 int fd, len;
37
38 fd = mkstemp(file);
39 len = write(fd, text, strlen(text));
40 assert(len == strlen(text));
41
42 config = weston_config_parse(fd);
43 close(fd);
44 unlink(file);
45
46 return config;
47}
48
49static const char t0[] =
50 "# nothing in this file...\n";
51
52static const char t1[] =
53 "# comment line here...\n"
54 "\n"
55 "[foo]\n"
56 "a=b\n"
57 "name= Roy Batty \n"
58 "\n"
59 "\n"
60 "[bar]\n"
61 "# more comments\n"
62 "number=5252\n"
63 "flag=false\n"
64 "\n"
65 "[stuff]\n"
66 "flag= true \n"
67 "\n"
68 "[bucket]\n"
69 "color=blue \n"
70 "contents=live crabs\n"
71 "pinchy=true\n"
72 "\n"
73 "[bucket]\n"
74 "material=plastic \n"
75 "color=red\n"
76 "contents=sand\n";
77
Kristian Høgsbergf73f3162013-05-26 20:50:53 -040078static const char *section_names[] = {
79 "foo", "bar", "stuff", "bucket", "bucket"
80};
81
Kristian Høgsberg73274712013-04-01 12:41:23 -040082static const char t2[] =
83 "# invalid section...\n"
84 "[this bracket isn't closed\n";
85
86static const char t3[] =
87 "# line without = ...\n"
88 "[bambam]\n"
89 "this line isn't any kind of valid\n";
90
91static const char t4[] =
92 "# starting with = ...\n"
93 "[bambam]\n"
94 "=not valid at all\n";
95
96int main(int argc, char *argv[])
97{
98 struct weston_config *config;
99 struct weston_config_section *section;
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400100 const char *name;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400101 char *s;
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400102 int r, b, i;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400103 int32_t n;
104 uint32_t u;
105
106 config = run_test(t0);
107 assert(config);
108 weston_config_destroy(config);
109
110 config = run_test(t1);
111 assert(config);
112 section = weston_config_get_section(config, "mollusc", NULL, NULL);
113 assert(section == NULL);
114
115 section = weston_config_get_section(config, "foo", NULL, NULL);
116 r = weston_config_section_get_string(section, "a", &s, NULL);
117 assert(r == 0 && strcmp(s, "b") == 0);
118 free(s);
119
120 section = weston_config_get_section(config, "foo", NULL, NULL);
121 r = weston_config_section_get_string(section, "b", &s, NULL);
122 assert(r == -1 && errno == ENOENT && s == NULL);
123
124 section = weston_config_get_section(config, "foo", NULL, NULL);
125 r = weston_config_section_get_string(section, "name", &s, NULL);
126 assert(r == 0 && strcmp(s, "Roy Batty") == 0);
127 free(s);
128
129 section = weston_config_get_section(config, "bar", NULL, NULL);
130 r = weston_config_section_get_string(section, "a", &s, "boo");
131 assert(r == -1 && errno == ENOENT && strcmp(s, "boo") == 0);
132 free(s);
133
134 section = weston_config_get_section(config, "bar", NULL, NULL);
135 r = weston_config_section_get_int(section, "number", &n, 600);
136 assert(r == 0 && n == 5252);
137
138 section = weston_config_get_section(config, "bar", NULL, NULL);
139 r = weston_config_section_get_int(section, "+++", &n, 700);
140 assert(r == -1 && errno == ENOENT && n == 700);
141
142 section = weston_config_get_section(config, "bar", NULL, NULL);
143 r = weston_config_section_get_uint(section, "number", &u, 600);
144 assert(r == 0 && u == 5252);
145
146 section = weston_config_get_section(config, "bar", NULL, NULL);
147 r = weston_config_section_get_uint(section, "+++", &u, 600);
148 assert(r == -1 && errno == ENOENT && u == 600);
149
150 section = weston_config_get_section(config, "bar", NULL, NULL);
151 r = weston_config_section_get_bool(section, "flag", &b, 600);
152 assert(r == 0 && b == 0);
153
154 section = weston_config_get_section(config, "stuff", NULL, NULL);
155 r = weston_config_section_get_bool(section, "flag", &b, -1);
156 assert(r == 0 && b == 1);
157
158 section = weston_config_get_section(config, "stuff", NULL, NULL);
159 r = weston_config_section_get_bool(section, "bonk", &b, -1);
160 assert(r == -1 && errno == ENOENT && b == -1);
161
162 section = weston_config_get_section(config, "bucket", "color", "blue");
163 r = weston_config_section_get_string(section, "contents", &s, NULL);
164 assert(r == 0 && strcmp(s, "live crabs") == 0);
165 free(s);
166
167 section = weston_config_get_section(config, "bucket", "color", "red");
168 r = weston_config_section_get_string(section, "contents", &s, NULL);
169 assert(r == 0 && strcmp(s, "sand") == 0);
170 free(s);
171
172 section = weston_config_get_section(config, "bucket", "color", "pink");
173 assert(section == NULL);
174 r = weston_config_section_get_string(section, "contents", &s, "eels");
175 assert(r == -1 && errno == ENOENT && strcmp(s, "eels") == 0);
176 free(s);
177
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400178 section = NULL;
179 i = 0;
180 while (weston_config_next_section(config, &section, &name))
181 assert(strcmp(section_names[i++], name) == 0);
182 assert(i == 5);
183
Kristian Høgsberg73274712013-04-01 12:41:23 -0400184 weston_config_destroy(config);
185
186 config = run_test(t2);
187 assert(config == NULL);
188
189 config = run_test(t3);
190 assert(config == NULL);
191
192 config = run_test(t4);
193 assert(config == NULL);
194
Kristian Høgsberg82189f72013-05-28 15:34:46 -0400195 weston_config_destroy(NULL);
196 assert(weston_config_next_section(NULL, NULL, NULL) == 0);
197
198 section = weston_config_get_section(NULL, "bucket", NULL, NULL);
199 assert(section == NULL);
200
Kristian Høgsberg73274712013-04-01 12:41:23 -0400201 return 0;
202}