blob: 4b255b7d00fbe3f4cc9f146bbdba364549017658 [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
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010023#include "config.h"
24
Kristian Høgsberg73274712013-04-01 12:41:23 -040025#include <stdlib.h>
26#include <stdint.h>
27#include <string.h>
28#include <assert.h>
29#include <errno.h>
Kristian Høgsbergac3a8b82013-07-08 19:06:06 -040030#include <unistd.h>
Kristian Høgsberg73274712013-04-01 12:41:23 -040031
32#include "config-parser.h"
33
34static struct weston_config *
35run_test(const char *text)
36{
37 struct weston_config *config;
38 char file[] = "/tmp/weston-config-parser-test-XXXXXX";
39 int fd, len;
40
41 fd = mkstemp(file);
42 len = write(fd, text, strlen(text));
Kristian Høgsbergac3a8b82013-07-08 19:06:06 -040043 assert(len == (int) strlen(text));
Kristian Høgsberg73274712013-04-01 12:41:23 -040044
Kristian Høgsberg1abe0482013-09-21 23:02:31 -070045 config = weston_config_parse(file);
Kristian Høgsberg73274712013-04-01 12:41:23 -040046 close(fd);
47 unlink(file);
48
49 return config;
50}
51
52static const char t0[] =
53 "# nothing in this file...\n";
54
55static const char t1[] =
56 "# comment line here...\n"
57 "\n"
58 "[foo]\n"
59 "a=b\n"
60 "name= Roy Batty \n"
61 "\n"
62 "\n"
63 "[bar]\n"
64 "# more comments\n"
65 "number=5252\n"
66 "flag=false\n"
67 "\n"
68 "[stuff]\n"
69 "flag= true \n"
70 "\n"
71 "[bucket]\n"
72 "color=blue \n"
73 "contents=live crabs\n"
74 "pinchy=true\n"
75 "\n"
76 "[bucket]\n"
77 "material=plastic \n"
78 "color=red\n"
79 "contents=sand\n";
80
Kristian Høgsbergf73f3162013-05-26 20:50:53 -040081static const char *section_names[] = {
82 "foo", "bar", "stuff", "bucket", "bucket"
83};
84
Kristian Høgsberg73274712013-04-01 12:41:23 -040085static const char t2[] =
86 "# invalid section...\n"
87 "[this bracket isn't closed\n";
88
89static const char t3[] =
90 "# line without = ...\n"
91 "[bambam]\n"
92 "this line isn't any kind of valid\n";
93
94static const char t4[] =
95 "# starting with = ...\n"
96 "[bambam]\n"
97 "=not valid at all\n";
98
99int main(int argc, char *argv[])
100{
101 struct weston_config *config;
102 struct weston_config_section *section;
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400103 const char *name;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400104 char *s;
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400105 int r, b, i;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400106 int32_t n;
107 uint32_t u;
108
109 config = run_test(t0);
110 assert(config);
111 weston_config_destroy(config);
112
113 config = run_test(t1);
114 assert(config);
115 section = weston_config_get_section(config, "mollusc", NULL, NULL);
116 assert(section == NULL);
117
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);
121 free(s);
122
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);
126
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);
130 free(s);
131
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);
135 free(s);
136
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);
140
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);
144
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);
148
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);
152
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);
156
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);
160
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);
164
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);
168 free(s);
169
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);
173 free(s);
174
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);
179 free(s);
180
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400181 section = NULL;
182 i = 0;
183 while (weston_config_next_section(config, &section, &name))
184 assert(strcmp(section_names[i++], name) == 0);
185 assert(i == 5);
186
Kristian Høgsberg73274712013-04-01 12:41:23 -0400187 weston_config_destroy(config);
188
189 config = run_test(t2);
190 assert(config == NULL);
191
192 config = run_test(t3);
193 assert(config == NULL);
194
195 config = run_test(t4);
196 assert(config == NULL);
197
Kristian Høgsberg82189f72013-05-28 15:34:46 -0400198 weston_config_destroy(NULL);
199 assert(weston_config_next_section(NULL, NULL, NULL) == 0);
200
201 section = weston_config_get_section(NULL, "bucket", NULL, NULL);
202 assert(section == NULL);
203
Kristian Høgsberg73274712013-04-01 12:41:23 -0400204 return 0;
205}