blob: 89983453493c1493124c52a31a373ff2b2670651 [file] [log] [blame]
Kristian Høgsberg73274712013-04-01 12:41:23 -04001/*
2 * Copyright © 2013 Intel Corporation
3 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg73274712013-04-01 12:41:23 -040011 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg73274712013-04-01 12:41:23 -040024 */
25
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010026#include "config.h"
27
Kristian Høgsberg73274712013-04-01 12:41:23 -040028#include <stdlib.h>
29#include <stdint.h>
Jon A. Cruza67c5412015-07-15 19:22:42 -070030#include <stdio.h>
Kristian Høgsberg73274712013-04-01 12:41:23 -040031#include <string.h>
32#include <assert.h>
33#include <errno.h>
Kristian Høgsbergac3a8b82013-07-08 19:06:06 -040034#include <unistd.h>
Kristian Høgsberg73274712013-04-01 12:41:23 -040035
36#include "config-parser.h"
37
Jon A. Cruza67c5412015-07-15 19:22:42 -070038#include "shared/helpers.h"
39#include "zunitc/zunitc.h"
40
41struct fixture_data {
42 const char *text;
Kristian Høgsberg73274712013-04-01 12:41:23 -040043 struct weston_config *config;
Jon A. Cruza67c5412015-07-15 19:22:42 -070044};
45
46static struct weston_config *
47load_config(const char *text)
48{
49 struct weston_config *config = NULL;
50 int len = 0;
51 int fd = -1;
Kristian Høgsberg73274712013-04-01 12:41:23 -040052 char file[] = "/tmp/weston-config-parser-test-XXXXXX";
Jon A. Cruza67c5412015-07-15 19:22:42 -070053
54 ZUC_ASSERTG_NOT_NULL(text, out);
Kristian Høgsberg73274712013-04-01 12:41:23 -040055
56 fd = mkstemp(file);
Jon A. Cruza67c5412015-07-15 19:22:42 -070057 ZUC_ASSERTG_NE(-1, fd, out);
58
Kristian Høgsberg73274712013-04-01 12:41:23 -040059 len = write(fd, text, strlen(text));
Jon A. Cruza67c5412015-07-15 19:22:42 -070060 ZUC_ASSERTG_EQ((int)strlen(text), len, out_close);
Kristian Høgsberg73274712013-04-01 12:41:23 -040061
Kristian Høgsberg1abe0482013-09-21 23:02:31 -070062 config = weston_config_parse(file);
Jon A. Cruza67c5412015-07-15 19:22:42 -070063
64out_close:
Kristian Høgsberg73274712013-04-01 12:41:23 -040065 close(fd);
66 unlink(file);
Jon A. Cruza67c5412015-07-15 19:22:42 -070067out:
Kristian Høgsberg73274712013-04-01 12:41:23 -040068 return config;
69}
70
Jon A. Cruza67c5412015-07-15 19:22:42 -070071static void *
72setup_test_config(void *data)
73{
74 struct weston_config *config = load_config(data);
75 ZUC_ASSERTG_NOT_NULL(config, out);
Kristian Høgsberg73274712013-04-01 12:41:23 -040076
Jon A. Cruza67c5412015-07-15 19:22:42 -070077out:
78 return config;
79}
80
81static void *
82setup_test_config_failing(void *data)
83{
84 struct weston_config *config = load_config(data);
85 ZUC_ASSERTG_NULL(config, err_free);
86
87 return config;
88err_free:
89 weston_config_destroy(config);
90 return NULL;
91}
92
93static void
94cleanup_test_config(void *data)
95{
96 struct weston_config *config = data;
97 ZUC_ASSERT_NOT_NULL(config);
98 weston_config_destroy(config);
99}
100
101static struct zuc_fixture config_test_t0 = {
102 .data = "# nothing in this file...\n",
103 .set_up = setup_test_config,
104 .tear_down = cleanup_test_config
105};
106
107static struct zuc_fixture config_test_t1 = {
108 .data =
Kristian Høgsberg73274712013-04-01 12:41:23 -0400109 "# comment line here...\n"
110 "\n"
111 "[foo]\n"
112 "a=b\n"
113 "name= Roy Batty \n"
114 "\n"
115 "\n"
116 "[bar]\n"
117 "# more comments\n"
118 "number=5252\n"
119 "flag=false\n"
120 "\n"
121 "[stuff]\n"
122 "flag= true \n"
123 "\n"
124 "[bucket]\n"
125 "color=blue \n"
126 "contents=live crabs\n"
127 "pinchy=true\n"
128 "\n"
129 "[bucket]\n"
130 "material=plastic \n"
131 "color=red\n"
Jon A. Cruza67c5412015-07-15 19:22:42 -0700132 "contents=sand\n",
133 .set_up = setup_test_config,
134 .tear_down = cleanup_test_config
135};
Kristian Høgsberg73274712013-04-01 12:41:23 -0400136
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400137static const char *section_names[] = {
138 "foo", "bar", "stuff", "bucket", "bucket"
139};
140
Jon A. Cruza67c5412015-07-15 19:22:42 -0700141/*
142 * Since these next few won't parse, we don't add the tear_down to
143 * attempt cleanup.
144 */
Kristian Høgsberg73274712013-04-01 12:41:23 -0400145
Jon A. Cruza67c5412015-07-15 19:22:42 -0700146static struct zuc_fixture config_test_t2 = {
147 .data =
148 "# invalid section...\n"
149 "[this bracket isn't closed\n",
150 .set_up = setup_test_config_failing,
151};
152
153static struct zuc_fixture config_test_t3 = {
154 .data =
Kristian Høgsberg73274712013-04-01 12:41:23 -0400155 "# line without = ...\n"
156 "[bambam]\n"
Jon A. Cruza67c5412015-07-15 19:22:42 -0700157 "this line isn't any kind of valid\n",
158 .set_up = setup_test_config_failing,
159};
Kristian Høgsberg73274712013-04-01 12:41:23 -0400160
Jon A. Cruza67c5412015-07-15 19:22:42 -0700161static struct zuc_fixture config_test_t4 = {
162 .data =
Kristian Høgsberg73274712013-04-01 12:41:23 -0400163 "# starting with = ...\n"
164 "[bambam]\n"
Jon A. Cruza67c5412015-07-15 19:22:42 -0700165 "=not valid at all\n",
166 .set_up = setup_test_config_failing,
167};
Kristian Høgsberg73274712013-04-01 12:41:23 -0400168
Jon A. Cruza67c5412015-07-15 19:22:42 -0700169ZUC_TEST_F(config_test_t0, comment_only)
Kristian Høgsberg73274712013-04-01 12:41:23 -0400170{
Jon A. Cruza67c5412015-07-15 19:22:42 -0700171 struct weston_config *config = data;
172 ZUC_ASSERT_NOT_NULL(config);
173}
174
175/** @todo individual t1 tests should have more descriptive names. */
176
177ZUC_TEST_F(config_test_t1, test001)
178{
Kristian Høgsberg73274712013-04-01 12:41:23 -0400179 struct weston_config_section *section;
Jon A. Cruza67c5412015-07-15 19:22:42 -0700180 struct weston_config *config = data;
181 ZUC_ASSERT_NOT_NULL(config);
182 section = weston_config_get_section(config,
183 "mollusc", NULL, NULL);
184 ZUC_ASSERT_NULL(section);
185}
186
187ZUC_TEST_F(config_test_t1, test002)
188{
Kristian Høgsberg73274712013-04-01 12:41:23 -0400189 char *s;
Jon A. Cruza67c5412015-07-15 19:22:42 -0700190 int r;
191 struct weston_config_section *section;
192 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400193
194 section = weston_config_get_section(config, "foo", NULL, NULL);
195 r = weston_config_section_get_string(section, "a", &s, NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700196
197 ZUC_ASSERTG_EQ(0, r, out_free);
198 ZUC_ASSERTG_STREQ("b", s, out_free);
199
200out_free:
Kristian Høgsberg73274712013-04-01 12:41:23 -0400201 free(s);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700202}
203
204ZUC_TEST_F(config_test_t1, test003)
205{
206 char *s;
207 int r;
208 struct weston_config_section *section;
209 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400210
211 section = weston_config_get_section(config, "foo", NULL, NULL);
212 r = weston_config_section_get_string(section, "b", &s, NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700213
214 ZUC_ASSERT_EQ(-1, r);
215 ZUC_ASSERT_EQ(ENOENT, errno);
216 ZUC_ASSERT_NULL(s);
217}
218
219ZUC_TEST_F(config_test_t1, test004)
220{
221 char *s;
222 int r;
223 struct weston_config_section *section;
224 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400225
226 section = weston_config_get_section(config, "foo", NULL, NULL);
227 r = weston_config_section_get_string(section, "name", &s, NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700228
229 ZUC_ASSERTG_EQ(0, r, out_free);
230 ZUC_ASSERTG_STREQ("Roy Batty", s, out_free);
231
232out_free:
Kristian Høgsberg73274712013-04-01 12:41:23 -0400233 free(s);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700234}
235
236ZUC_TEST_F(config_test_t1, test005)
237{
238 char *s;
239 int r;
240 struct weston_config_section *section;
241 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400242
243 section = weston_config_get_section(config, "bar", NULL, NULL);
244 r = weston_config_section_get_string(section, "a", &s, "boo");
Jon A. Cruza67c5412015-07-15 19:22:42 -0700245
246 ZUC_ASSERTG_EQ(-1, r, out_free);
247 ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
248 ZUC_ASSERTG_STREQ("boo", s, out_free);
249
250out_free:
Kristian Høgsberg73274712013-04-01 12:41:23 -0400251 free(s);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700252}
253
254ZUC_TEST_F(config_test_t1, test006)
255{
256 int r;
257 int32_t n;
258 struct weston_config_section *section;
259 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400260
261 section = weston_config_get_section(config, "bar", NULL, NULL);
262 r = weston_config_section_get_int(section, "number", &n, 600);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700263
264 ZUC_ASSERT_EQ(0, r);
265 ZUC_ASSERT_EQ(5252, n);
266}
267
268ZUC_TEST_F(config_test_t1, test007)
269{
270 int r;
271 int32_t n;
272 struct weston_config_section *section;
Derek Foremanbdc8c722015-10-07 11:51:29 -0500273 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400274
275 section = weston_config_get_section(config, "bar", NULL, NULL);
276 r = weston_config_section_get_int(section, "+++", &n, 700);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700277
278 ZUC_ASSERT_EQ(-1, r);
279 ZUC_ASSERT_EQ(ENOENT, errno);
280 ZUC_ASSERT_EQ(700, n);
281}
282
283ZUC_TEST_F(config_test_t1, test008)
284{
285 int r;
286 uint32_t u;
287 struct weston_config_section *section;
288 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400289
290 section = weston_config_get_section(config, "bar", NULL, NULL);
291 r = weston_config_section_get_uint(section, "number", &u, 600);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700292 ZUC_ASSERT_EQ(0, r);
293 ZUC_ASSERT_EQ(5252, u);
294}
295
296ZUC_TEST_F(config_test_t1, test009)
297{
298 int r;
299 uint32_t u;
300 struct weston_config_section *section;
301 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400302
303 section = weston_config_get_section(config, "bar", NULL, NULL);
304 r = weston_config_section_get_uint(section, "+++", &u, 600);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700305 ZUC_ASSERT_EQ(-1, r);
306 ZUC_ASSERT_EQ(ENOENT, errno);
307 ZUC_ASSERT_EQ(600, u);
308}
309
310ZUC_TEST_F(config_test_t1, test010)
311{
312 int r, b;
313 struct weston_config_section *section;
314 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400315
316 section = weston_config_get_section(config, "bar", NULL, NULL);
317 r = weston_config_section_get_bool(section, "flag", &b, 600);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700318 ZUC_ASSERT_EQ(0, r);
319 ZUC_ASSERT_EQ(0, b);
320}
321
322ZUC_TEST_F(config_test_t1, test011)
323{
324 int r, b;
325 struct weston_config_section *section;
326 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400327
328 section = weston_config_get_section(config, "stuff", NULL, NULL);
329 r = weston_config_section_get_bool(section, "flag", &b, -1);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700330 ZUC_ASSERT_EQ(0, r);
331 ZUC_ASSERT_EQ(1, b);
332}
333
334ZUC_TEST_F(config_test_t1, test012)
335{
336 int r, b;
337 struct weston_config_section *section;
338 struct weston_config *config = data;
339
340 section = weston_config_get_section(config, "stuff", NULL, NULL);
341 r = weston_config_section_get_bool(section, "flag", &b, -1);
342 ZUC_ASSERT_EQ(0, r);
343 ZUC_ASSERT_EQ(1, b);
344}
345
346ZUC_TEST_F(config_test_t1, test013)
347{
348 int r, b;
349 struct weston_config_section *section;
350 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400351
352 section = weston_config_get_section(config, "stuff", NULL, NULL);
353 r = weston_config_section_get_bool(section, "bonk", &b, -1);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700354 ZUC_ASSERT_EQ(-1, r);
355 ZUC_ASSERT_EQ(ENOENT, errno);
356 ZUC_ASSERT_EQ(-1, b);
357}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400358
Jon A. Cruza67c5412015-07-15 19:22:42 -0700359ZUC_TEST_F(config_test_t1, test014)
360{
361 char *s;
362 int r;
363 struct weston_config_section *section;
364 struct weston_config *config = data;
365
366 section = weston_config_get_section(config,
367 "bucket", "color", "blue");
Kristian Høgsberg73274712013-04-01 12:41:23 -0400368 r = weston_config_section_get_string(section, "contents", &s, NULL);
Kristian Høgsberg73274712013-04-01 12:41:23 -0400369
Jon A. Cruza67c5412015-07-15 19:22:42 -0700370 ZUC_ASSERTG_EQ(0, r, out_free);
371 ZUC_ASSERTG_STREQ("live crabs", s, out_free);
372
373out_free:
374 free(s);
375}
376
377ZUC_TEST_F(config_test_t1, test015)
378{
379 char *s;
380 int r;
381 struct weston_config_section *section;
382 struct weston_config *config = data;
383
384 section = weston_config_get_section(config,
385 "bucket", "color", "red");
Kristian Høgsberg73274712013-04-01 12:41:23 -0400386 r = weston_config_section_get_string(section, "contents", &s, NULL);
Kristian Høgsberg73274712013-04-01 12:41:23 -0400387
Jon A. Cruza67c5412015-07-15 19:22:42 -0700388 ZUC_ASSERTG_EQ(0, r, out_free);
389 ZUC_ASSERTG_STREQ("sand", s, out_free);
390
391out_free:
392 free(s);
393}
394
395ZUC_TEST_F(config_test_t1, test016)
396{
397 char *s;
398 int r;
399 struct weston_config_section *section;
400 struct weston_config *config = data;
401
402 section = weston_config_get_section(config,
403 "bucket", "color", "pink");
404 ZUC_ASSERT_NULL(section);
Kristian Høgsberg73274712013-04-01 12:41:23 -0400405 r = weston_config_section_get_string(section, "contents", &s, "eels");
Jon A. Cruza67c5412015-07-15 19:22:42 -0700406
407 ZUC_ASSERTG_EQ(-1, r, out_free);
408 ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
409 ZUC_ASSERTG_STREQ("eels", s, out_free);
410
411out_free:
Kristian Høgsberg73274712013-04-01 12:41:23 -0400412 free(s);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700413}
414
415ZUC_TEST_F(config_test_t1, test017)
416{
417 const char *name;
418 int i;
419 struct weston_config_section *section;
420 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400421
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400422 section = NULL;
423 i = 0;
424 while (weston_config_next_section(config, &section, &name))
Jon A. Cruza67c5412015-07-15 19:22:42 -0700425 ZUC_ASSERT_STREQ(section_names[i++], name);
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400426
Jon A. Cruza67c5412015-07-15 19:22:42 -0700427 ZUC_ASSERT_EQ(5, i);
428}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400429
Jon A. Cruza67c5412015-07-15 19:22:42 -0700430ZUC_TEST_F(config_test_t2, doesnt_parse)
431{
432 struct weston_config *config = data;
433 ZUC_ASSERT_NULL(config);
434}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400435
Jon A. Cruza67c5412015-07-15 19:22:42 -0700436ZUC_TEST_F(config_test_t3, doesnt_parse)
437{
438 struct weston_config *config = data;
439 ZUC_ASSERT_NULL(config);
440}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400441
Jon A. Cruza67c5412015-07-15 19:22:42 -0700442ZUC_TEST_F(config_test_t4, doesnt_parse)
443{
444 struct weston_config *config = data;
445 ZUC_ASSERT_NULL(config);
446}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400447
Jon A. Cruza67c5412015-07-15 19:22:42 -0700448ZUC_TEST(config_test, destroy_null)
449{
Kristian Høgsberg82189f72013-05-28 15:34:46 -0400450 weston_config_destroy(NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700451 ZUC_ASSERT_EQ(0, weston_config_next_section(NULL, NULL, NULL));
452}
Kristian Høgsberg82189f72013-05-28 15:34:46 -0400453
Jon A. Cruza67c5412015-07-15 19:22:42 -0700454ZUC_TEST(config_test, section_from_null)
455{
456 struct weston_config_section *section;
Kristian Høgsberg82189f72013-05-28 15:34:46 -0400457 section = weston_config_get_section(NULL, "bucket", NULL, NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700458 ZUC_ASSERT_NULL(section);
Kristian Høgsberg73274712013-04-01 12:41:23 -0400459}