2 * Copyright © 2013 Intel Corporation
4 * 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:
12 * 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.
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
36 #include "config-parser.h"
38 #include "shared/helpers.h"
39 #include "zunitc/zunitc.h"
43 struct weston_config *config;
46 static struct weston_config *
47 load_config(const char *text)
49 struct weston_config *config = NULL;
52 char file[] = "/tmp/weston-config-parser-test-XXXXXX";
54 ZUC_ASSERTG_NOT_NULL(text, out);
57 ZUC_ASSERTG_NE(-1, fd, out);
59 len = write(fd, text, strlen(text));
60 ZUC_ASSERTG_EQ((int)strlen(text), len, out_close);
62 config = weston_config_parse(file);
72 setup_test_config(void *data)
74 struct weston_config *config = load_config(data);
75 ZUC_ASSERTG_NOT_NULL(config, out);
82 setup_test_config_failing(void *data)
84 struct weston_config *config = load_config(data);
85 ZUC_ASSERTG_NULL(config, err_free);
89 weston_config_destroy(config);
94 cleanup_test_config(void *data)
96 struct weston_config *config = data;
97 ZUC_ASSERT_NOT_NULL(config);
98 weston_config_destroy(config);
101 static 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
107 static struct zuc_fixture config_test_t1 = {
109 "# comment line here...\n"
136 "contents=live crabs\n"
140 "material=plastic \n"
143 .set_up = setup_test_config,
144 .tear_down = cleanup_test_config
147 static const char *section_names[] = {
148 "foo", "bar", "colors", "stuff", "bucket", "bucket"
152 * Since these next few won't parse, we don't add the tear_down to
156 static struct zuc_fixture config_test_t2 = {
158 "# invalid section...\n"
159 "[this bracket isn't closed\n",
160 .set_up = setup_test_config_failing,
163 static struct zuc_fixture config_test_t3 = {
165 "# line without = ...\n"
167 "this line isn't any kind of valid\n",
168 .set_up = setup_test_config_failing,
171 static struct zuc_fixture config_test_t4 = {
173 "# starting with = ...\n"
175 "=not valid at all\n",
176 .set_up = setup_test_config_failing,
179 ZUC_TEST_F(config_test_t0, comment_only, data)
181 struct weston_config *config = data;
182 ZUC_ASSERT_NOT_NULL(config);
185 /** @todo individual t1 tests should have more descriptive names. */
187 ZUC_TEST_F(config_test_t1, test001, data)
189 struct weston_config_section *section;
190 struct weston_config *config = data;
191 ZUC_ASSERT_NOT_NULL(config);
192 section = weston_config_get_section(config,
193 "mollusc", NULL, NULL);
194 ZUC_ASSERT_NULL(section);
197 ZUC_TEST_F(config_test_t1, test002, data)
201 struct weston_config_section *section;
202 struct weston_config *config = data;
204 section = weston_config_get_section(config, "foo", NULL, NULL);
205 r = weston_config_section_get_string(section, "a", &s, NULL);
207 ZUC_ASSERTG_EQ(0, r, out_free);
208 ZUC_ASSERTG_STREQ("b", s, out_free);
214 ZUC_TEST_F(config_test_t1, test003, data)
218 struct weston_config_section *section;
219 struct weston_config *config = data;
221 section = weston_config_get_section(config, "foo", NULL, NULL);
222 r = weston_config_section_get_string(section, "b", &s, NULL);
224 ZUC_ASSERT_EQ(-1, r);
225 ZUC_ASSERT_EQ(ENOENT, errno);
229 ZUC_TEST_F(config_test_t1, test004, data)
233 struct weston_config_section *section;
234 struct weston_config *config = data;
236 section = weston_config_get_section(config, "foo", NULL, NULL);
237 r = weston_config_section_get_string(section, "name", &s, NULL);
239 ZUC_ASSERTG_EQ(0, r, out_free);
240 ZUC_ASSERTG_STREQ("Roy Batty", s, out_free);
246 ZUC_TEST_F(config_test_t1, test005, data)
250 struct weston_config_section *section;
251 struct weston_config *config = data;
253 section = weston_config_get_section(config, "bar", NULL, NULL);
254 r = weston_config_section_get_string(section, "a", &s, "boo");
256 ZUC_ASSERTG_EQ(-1, r, out_free);
257 ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
258 ZUC_ASSERTG_STREQ("boo", s, out_free);
264 ZUC_TEST_F(config_test_t1, test006, data)
268 struct weston_config_section *section;
269 struct weston_config *config = data;
271 section = weston_config_get_section(config, "bar", NULL, NULL);
272 r = weston_config_section_get_int(section, "number", &n, 600);
275 ZUC_ASSERT_EQ(5252, n);
276 ZUC_ASSERT_EQ(0, errno);
279 ZUC_TEST_F(config_test_t1, test007, data)
283 struct weston_config_section *section;
284 struct weston_config *config = data;
286 section = weston_config_get_section(config, "bar", NULL, NULL);
287 r = weston_config_section_get_int(section, "+++", &n, 700);
289 ZUC_ASSERT_EQ(-1, r);
290 ZUC_ASSERT_EQ(ENOENT, errno);
291 ZUC_ASSERT_EQ(700, n);
294 ZUC_TEST_F(config_test_t1, test008, data)
298 struct weston_config_section *section;
299 struct weston_config *config = data;
301 section = weston_config_get_section(config, "bar", NULL, NULL);
302 r = weston_config_section_get_uint(section, "number", &u, 600);
305 ZUC_ASSERT_EQ(5252, u);
306 ZUC_ASSERT_EQ(0, errno);
309 ZUC_TEST_F(config_test_t1, test009, data)
313 struct weston_config_section *section;
314 struct weston_config *config = data;
316 section = weston_config_get_section(config, "bar", NULL, NULL);
317 r = weston_config_section_get_uint(section, "+++", &u, 600);
318 ZUC_ASSERT_EQ(-1, r);
319 ZUC_ASSERT_EQ(ENOENT, errno);
320 ZUC_ASSERT_EQ(600, u);
323 ZUC_TEST_F(config_test_t1, test010, data)
326 struct weston_config_section *section;
327 struct weston_config *config = data;
329 section = weston_config_get_section(config, "bar", NULL, NULL);
330 r = weston_config_section_get_bool(section, "flag", &b, 600);
335 ZUC_TEST_F(config_test_t1, test011, data)
338 struct weston_config_section *section;
339 struct weston_config *config = data;
341 section = weston_config_get_section(config, "stuff", NULL, NULL);
342 r = weston_config_section_get_bool(section, "flag", &b, -1);
347 ZUC_TEST_F(config_test_t1, test012, data)
350 struct weston_config_section *section;
351 struct weston_config *config = data;
353 section = weston_config_get_section(config, "stuff", NULL, NULL);
354 r = weston_config_section_get_bool(section, "flag", &b, -1);
359 ZUC_TEST_F(config_test_t1, test013, data)
362 struct weston_config_section *section;
363 struct weston_config *config = data;
365 section = weston_config_get_section(config, "stuff", NULL, NULL);
366 r = weston_config_section_get_bool(section, "bonk", &b, -1);
367 ZUC_ASSERT_EQ(-1, r);
368 ZUC_ASSERT_EQ(ENOENT, errno);
369 ZUC_ASSERT_EQ(-1, b);
372 ZUC_TEST_F(config_test_t1, test014, data)
376 struct weston_config_section *section;
377 struct weston_config *config = data;
379 section = weston_config_get_section(config,
380 "bucket", "color", "blue");
381 r = weston_config_section_get_string(section, "contents", &s, NULL);
383 ZUC_ASSERTG_EQ(0, r, out_free);
384 ZUC_ASSERTG_STREQ("live crabs", s, out_free);
390 ZUC_TEST_F(config_test_t1, test015, data)
394 struct weston_config_section *section;
395 struct weston_config *config = data;
397 section = weston_config_get_section(config,
398 "bucket", "color", "red");
399 r = weston_config_section_get_string(section, "contents", &s, NULL);
401 ZUC_ASSERTG_EQ(0, r, out_free);
402 ZUC_ASSERTG_STREQ("sand", s, out_free);
408 ZUC_TEST_F(config_test_t1, test016, data)
412 struct weston_config_section *section;
413 struct weston_config *config = data;
415 section = weston_config_get_section(config,
416 "bucket", "color", "pink");
417 ZUC_ASSERT_NULL(section);
418 r = weston_config_section_get_string(section, "contents", &s, "eels");
420 ZUC_ASSERTG_EQ(-1, r, out_free);
421 ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
422 ZUC_ASSERTG_STREQ("eels", s, out_free);
428 ZUC_TEST_F(config_test_t1, test017, data)
432 struct weston_config_section *section;
433 struct weston_config *config = data;
437 while (weston_config_next_section(config, §ion, &name))
438 ZUC_ASSERT_STREQ(section_names[i++], name);
443 ZUC_TEST_F(config_test_t1, test018, data)
447 struct weston_config_section *section;
448 struct weston_config *config = data;
450 section = weston_config_get_section(config, "bar", NULL, NULL);
451 r = weston_config_section_get_int(section, "zero", &n, 600);
455 ZUC_ASSERT_EQ(0, errno);
458 ZUC_TEST_F(config_test_t1, test019, data)
462 struct weston_config_section *section;
463 struct weston_config *config = data;
465 section = weston_config_get_section(config, "bar", NULL, NULL);
466 r = weston_config_section_get_uint(section, "zero", &n, 600);
470 ZUC_ASSERT_EQ(0, errno);
473 ZUC_TEST_F(config_test_t1, test020, data)
477 struct weston_config_section *section;
478 struct weston_config *config = data;
480 section = weston_config_get_section(config, "colors", NULL, NULL);
481 r = weston_config_section_get_color(section, "none", &n, 0xff336699);
484 ZUC_ASSERT_EQ(0x000000, n);
485 ZUC_ASSERT_EQ(0, errno);
488 ZUC_TEST_F(config_test_t1, test021, data)
492 struct weston_config_section *section;
493 struct weston_config *config = data;
495 section = weston_config_get_section(config, "colors", NULL, NULL);
496 r = weston_config_section_get_color(section, "low", &n, 0xff336699);
499 ZUC_ASSERT_EQ(0x11223344, n);
500 ZUC_ASSERT_EQ(0, errno);
503 ZUC_TEST_F(config_test_t1, test022, data)
507 struct weston_config_section *section;
508 struct weston_config *config = data;
510 section = weston_config_get_section(config, "colors", NULL, NULL);
511 r = weston_config_section_get_color(section, "high", &n, 0xff336699);
514 ZUC_ASSERT_EQ(0xff00ff00, n);
515 ZUC_ASSERT_EQ(0, errno);
518 ZUC_TEST_F(config_test_t1, test023, data)
522 struct weston_config_section *section;
523 struct weston_config *config = data;
525 // Treat colors as hex values even if missing the leading 0x
526 section = weston_config_get_section(config, "colors", NULL, NULL);
527 r = weston_config_section_get_color(section, "oct", &n, 0xff336699);
530 ZUC_ASSERT_EQ(0x01234567, n);
531 ZUC_ASSERT_EQ(0, errno);
534 ZUC_TEST_F(config_test_t1, test024, data)
538 struct weston_config_section *section;
539 struct weston_config *config = data;
541 // Treat colors as hex values even if missing the leading 0x
542 section = weston_config_get_section(config, "colors", NULL, NULL);
543 r = weston_config_section_get_color(section, "dec", &n, 0xff336699);
546 ZUC_ASSERT_EQ(0x12345670, n);
547 ZUC_ASSERT_EQ(0, errno);
550 ZUC_TEST_F(config_test_t1, test025, data)
554 struct weston_config_section *section;
555 struct weston_config *config = data;
557 // 7-digit colors are not valid (most likely typos)
558 section = weston_config_get_section(config, "colors", NULL, NULL);
559 r = weston_config_section_get_color(section, "short", &n, 0xff336699);
561 ZUC_ASSERT_EQ(-1, r);
562 ZUC_ASSERT_EQ(0xff336699, n);
563 ZUC_ASSERT_EQ(EINVAL, errno);
566 ZUC_TEST_F(config_test_t1, test026, data)
570 struct weston_config_section *section;
571 struct weston_config *config = data;
573 // String color names are unsupported
574 section = weston_config_get_section(config, "bucket", NULL, NULL);
575 r = weston_config_section_get_color(section, "color", &n, 0xff336699);
577 ZUC_ASSERT_EQ(-1, r);
578 ZUC_ASSERT_EQ(0xff336699, n);
579 ZUC_ASSERT_EQ(EINVAL, errno);
582 ZUC_TEST_F(config_test_t1, test027, data)
586 struct weston_config_section *section;
587 struct weston_config *config = data;
589 section = weston_config_get_section(config, "bar", NULL, NULL);
590 r = weston_config_section_get_int(section, "negative", &n, 600);
593 ZUC_ASSERT_EQ(-42, n);
594 ZUC_ASSERT_EQ(0, errno);
597 ZUC_TEST_F(config_test_t1, test028, data)
601 struct weston_config_section *section;
602 struct weston_config *config = data;
604 section = weston_config_get_section(config, "bar", NULL, NULL);
605 r = weston_config_section_get_uint(section, "negative", &n, 600);
607 ZUC_ASSERT_EQ(-1, r);
608 ZUC_ASSERT_EQ(600, n);
609 ZUC_ASSERT_EQ(ERANGE, errno);
612 ZUC_TEST_F(config_test_t2, doesnt_parse, data)
614 struct weston_config *config = data;
615 ZUC_ASSERT_NULL(config);
618 ZUC_TEST_F(config_test_t3, doesnt_parse, data)
620 struct weston_config *config = data;
621 ZUC_ASSERT_NULL(config);
624 ZUC_TEST_F(config_test_t4, doesnt_parse, data)
626 struct weston_config *config = data;
627 ZUC_ASSERT_NULL(config);
630 ZUC_TEST(config_test, destroy_null)
632 weston_config_destroy(NULL);
633 ZUC_ASSERT_EQ(0, weston_config_next_section(NULL, NULL, NULL));
636 ZUC_TEST(config_test, section_from_null)
638 struct weston_config_section *section;
639 section = weston_config_get_section(NULL, "bucket", NULL, NULL);
640 ZUC_ASSERT_NULL(section);