blob: 583c83f2a17e8a19daa02d42d4b11c572b0b740c [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
Pekka Paalanen91b10102019-04-04 14:27:31 +030036#include <libweston/config-parser.h>
Kristian Høgsberg73274712013-04-01 12:41:23 -040037
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"
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700119 "zero=0\n"
Bryce Harringtond0716f42016-07-14 18:28:04 -0700120 "negative=-42\n"
Kristian Høgsberg73274712013-04-01 12:41:23 -0400121 "flag=false\n"
122 "\n"
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700123 "[colors]\n"
124 "none=0x00000000\n"
125 "low=0x11223344\n"
126 "high=0xff00ff00\n"
127 "oct=01234567\n"
128 "dec=12345670\n"
129 "short=1234567\n"
130 "\n"
Kristian Høgsberg73274712013-04-01 12:41:23 -0400131 "[stuff]\n"
132 "flag= true \n"
133 "\n"
134 "[bucket]\n"
135 "color=blue \n"
136 "contents=live crabs\n"
137 "pinchy=true\n"
138 "\n"
139 "[bucket]\n"
140 "material=plastic \n"
141 "color=red\n"
Jon A. Cruza67c5412015-07-15 19:22:42 -0700142 "contents=sand\n",
143 .set_up = setup_test_config,
144 .tear_down = cleanup_test_config
145};
Kristian Høgsberg73274712013-04-01 12:41:23 -0400146
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400147static const char *section_names[] = {
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700148 "foo", "bar", "colors", "stuff", "bucket", "bucket"
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400149};
150
Jon A. Cruza67c5412015-07-15 19:22:42 -0700151/*
152 * Since these next few won't parse, we don't add the tear_down to
153 * attempt cleanup.
154 */
Kristian Høgsberg73274712013-04-01 12:41:23 -0400155
Jon A. Cruza67c5412015-07-15 19:22:42 -0700156static struct zuc_fixture config_test_t2 = {
157 .data =
158 "# invalid section...\n"
159 "[this bracket isn't closed\n",
160 .set_up = setup_test_config_failing,
161};
162
163static struct zuc_fixture config_test_t3 = {
164 .data =
Kristian Høgsberg73274712013-04-01 12:41:23 -0400165 "# line without = ...\n"
166 "[bambam]\n"
Jon A. Cruza67c5412015-07-15 19:22:42 -0700167 "this line isn't any kind of valid\n",
168 .set_up = setup_test_config_failing,
169};
Kristian Høgsberg73274712013-04-01 12:41:23 -0400170
Jon A. Cruza67c5412015-07-15 19:22:42 -0700171static struct zuc_fixture config_test_t4 = {
172 .data =
Kristian Høgsberg73274712013-04-01 12:41:23 -0400173 "# starting with = ...\n"
174 "[bambam]\n"
Jon A. Cruza67c5412015-07-15 19:22:42 -0700175 "=not valid at all\n",
176 .set_up = setup_test_config_failing,
177};
Kristian Høgsberg73274712013-04-01 12:41:23 -0400178
Jon Cruzecf819b2015-10-22 21:11:12 -0700179ZUC_TEST_F(config_test_t0, comment_only, data)
Kristian Høgsberg73274712013-04-01 12:41:23 -0400180{
Jon A. Cruza67c5412015-07-15 19:22:42 -0700181 struct weston_config *config = data;
182 ZUC_ASSERT_NOT_NULL(config);
183}
184
185/** @todo individual t1 tests should have more descriptive names. */
186
Jon Cruzecf819b2015-10-22 21:11:12 -0700187ZUC_TEST_F(config_test_t1, test001, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700188{
Kristian Høgsberg73274712013-04-01 12:41:23 -0400189 struct weston_config_section *section;
Jon A. Cruza67c5412015-07-15 19:22:42 -0700190 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);
195}
196
Jon Cruzecf819b2015-10-22 21:11:12 -0700197ZUC_TEST_F(config_test_t1, test002, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700198{
Kristian Høgsberg73274712013-04-01 12:41:23 -0400199 char *s;
Jon A. Cruza67c5412015-07-15 19:22:42 -0700200 int r;
201 struct weston_config_section *section;
202 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400203
204 section = weston_config_get_section(config, "foo", NULL, NULL);
205 r = weston_config_section_get_string(section, "a", &s, NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700206
207 ZUC_ASSERTG_EQ(0, r, out_free);
208 ZUC_ASSERTG_STREQ("b", s, out_free);
209
210out_free:
Kristian Høgsberg73274712013-04-01 12:41:23 -0400211 free(s);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700212}
213
Jon Cruzecf819b2015-10-22 21:11:12 -0700214ZUC_TEST_F(config_test_t1, test003, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700215{
216 char *s;
217 int r;
218 struct weston_config_section *section;
219 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400220
221 section = weston_config_get_section(config, "foo", NULL, NULL);
222 r = weston_config_section_get_string(section, "b", &s, NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700223
224 ZUC_ASSERT_EQ(-1, r);
225 ZUC_ASSERT_EQ(ENOENT, errno);
226 ZUC_ASSERT_NULL(s);
227}
228
Jon Cruzecf819b2015-10-22 21:11:12 -0700229ZUC_TEST_F(config_test_t1, test004, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700230{
231 char *s;
232 int r;
233 struct weston_config_section *section;
234 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400235
236 section = weston_config_get_section(config, "foo", NULL, NULL);
237 r = weston_config_section_get_string(section, "name", &s, NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700238
239 ZUC_ASSERTG_EQ(0, r, out_free);
240 ZUC_ASSERTG_STREQ("Roy Batty", s, out_free);
241
242out_free:
Kristian Høgsberg73274712013-04-01 12:41:23 -0400243 free(s);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700244}
245
Jon Cruzecf819b2015-10-22 21:11:12 -0700246ZUC_TEST_F(config_test_t1, test005, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700247{
248 char *s;
249 int r;
250 struct weston_config_section *section;
251 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400252
253 section = weston_config_get_section(config, "bar", NULL, NULL);
254 r = weston_config_section_get_string(section, "a", &s, "boo");
Jon A. Cruza67c5412015-07-15 19:22:42 -0700255
256 ZUC_ASSERTG_EQ(-1, r, out_free);
257 ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
258 ZUC_ASSERTG_STREQ("boo", s, out_free);
259
260out_free:
Kristian Høgsberg73274712013-04-01 12:41:23 -0400261 free(s);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700262}
263
Jon Cruzecf819b2015-10-22 21:11:12 -0700264ZUC_TEST_F(config_test_t1, test006, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700265{
266 int r;
267 int32_t n;
268 struct weston_config_section *section;
269 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400270
271 section = weston_config_get_section(config, "bar", NULL, NULL);
272 r = weston_config_section_get_int(section, "number", &n, 600);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700273
274 ZUC_ASSERT_EQ(0, r);
275 ZUC_ASSERT_EQ(5252, n);
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700276 ZUC_ASSERT_EQ(0, errno);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700277}
278
Jon Cruzecf819b2015-10-22 21:11:12 -0700279ZUC_TEST_F(config_test_t1, test007, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700280{
281 int r;
282 int32_t n;
283 struct weston_config_section *section;
Derek Foremanbdc8c722015-10-07 11:51:29 -0500284 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400285
286 section = weston_config_get_section(config, "bar", NULL, NULL);
287 r = weston_config_section_get_int(section, "+++", &n, 700);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700288
289 ZUC_ASSERT_EQ(-1, r);
290 ZUC_ASSERT_EQ(ENOENT, errno);
291 ZUC_ASSERT_EQ(700, n);
292}
293
Jon Cruzecf819b2015-10-22 21:11:12 -0700294ZUC_TEST_F(config_test_t1, test008, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700295{
296 int r;
297 uint32_t u;
298 struct weston_config_section *section;
299 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400300
301 section = weston_config_get_section(config, "bar", NULL, NULL);
302 r = weston_config_section_get_uint(section, "number", &u, 600);
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700303
Jon A. Cruza67c5412015-07-15 19:22:42 -0700304 ZUC_ASSERT_EQ(0, r);
305 ZUC_ASSERT_EQ(5252, u);
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700306 ZUC_ASSERT_EQ(0, errno);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700307}
308
Jon Cruzecf819b2015-10-22 21:11:12 -0700309ZUC_TEST_F(config_test_t1, test009, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700310{
311 int r;
312 uint32_t u;
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_uint(section, "+++", &u, 600);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700318 ZUC_ASSERT_EQ(-1, r);
319 ZUC_ASSERT_EQ(ENOENT, errno);
320 ZUC_ASSERT_EQ(600, u);
321}
322
Jon Cruzecf819b2015-10-22 21:11:12 -0700323ZUC_TEST_F(config_test_t1, test010, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700324{
Daniel Stone51d995a2019-11-26 00:14:24 +0000325 int r;
326 bool b;
Jon A. Cruza67c5412015-07-15 19:22:42 -0700327 struct weston_config_section *section;
328 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400329
330 section = weston_config_get_section(config, "bar", NULL, NULL);
Daniel Stone51d995a2019-11-26 00:14:24 +0000331 r = weston_config_section_get_bool(section, "flag", &b, true);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700332 ZUC_ASSERT_EQ(0, r);
Daniel Stone51d995a2019-11-26 00:14:24 +0000333 ZUC_ASSERT_EQ(false, b);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700334}
335
Jon Cruzecf819b2015-10-22 21:11:12 -0700336ZUC_TEST_F(config_test_t1, test011, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700337{
Daniel Stone51d995a2019-11-26 00:14:24 +0000338 int r;
339 bool b;
Jon A. Cruza67c5412015-07-15 19:22:42 -0700340 struct weston_config_section *section;
341 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400342
343 section = weston_config_get_section(config, "stuff", NULL, NULL);
Daniel Stone51d995a2019-11-26 00:14:24 +0000344 r = weston_config_section_get_bool(section, "flag", &b, false);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700345 ZUC_ASSERT_EQ(0, r);
Daniel Stone51d995a2019-11-26 00:14:24 +0000346 ZUC_ASSERT_EQ(true, b);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700347}
348
Jon Cruzecf819b2015-10-22 21:11:12 -0700349ZUC_TEST_F(config_test_t1, test012, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700350{
Daniel Stone51d995a2019-11-26 00:14:24 +0000351 int r;
352 bool b;
Jon A. Cruza67c5412015-07-15 19:22:42 -0700353 struct weston_config_section *section;
354 struct weston_config *config = data;
355
356 section = weston_config_get_section(config, "stuff", NULL, NULL);
Daniel Stone51d995a2019-11-26 00:14:24 +0000357 r = weston_config_section_get_bool(section, "bonk", &b, false);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700358 ZUC_ASSERT_EQ(-1, r);
359 ZUC_ASSERT_EQ(ENOENT, errno);
Daniel Stone51d995a2019-11-26 00:14:24 +0000360 ZUC_ASSERT_EQ(false, b);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700361}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400362
Daniel Stoneb8c39262019-11-26 00:16:35 +0000363ZUC_TEST_F(config_test_t1, test013, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700364{
365 char *s;
366 int r;
367 struct weston_config_section *section;
368 struct weston_config *config = data;
369
370 section = weston_config_get_section(config,
371 "bucket", "color", "blue");
Kristian Høgsberg73274712013-04-01 12:41:23 -0400372 r = weston_config_section_get_string(section, "contents", &s, NULL);
Kristian Høgsberg73274712013-04-01 12:41:23 -0400373
Jon A. Cruza67c5412015-07-15 19:22:42 -0700374 ZUC_ASSERTG_EQ(0, r, out_free);
375 ZUC_ASSERTG_STREQ("live crabs", s, out_free);
376
377out_free:
378 free(s);
379}
380
Daniel Stoneb8c39262019-11-26 00:16:35 +0000381ZUC_TEST_F(config_test_t1, test014, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700382{
383 char *s;
384 int r;
385 struct weston_config_section *section;
386 struct weston_config *config = data;
387
388 section = weston_config_get_section(config,
389 "bucket", "color", "red");
Kristian Høgsberg73274712013-04-01 12:41:23 -0400390 r = weston_config_section_get_string(section, "contents", &s, NULL);
Kristian Høgsberg73274712013-04-01 12:41:23 -0400391
Jon A. Cruza67c5412015-07-15 19:22:42 -0700392 ZUC_ASSERTG_EQ(0, r, out_free);
393 ZUC_ASSERTG_STREQ("sand", s, out_free);
394
395out_free:
396 free(s);
397}
398
Daniel Stoneb8c39262019-11-26 00:16:35 +0000399ZUC_TEST_F(config_test_t1, test015, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700400{
401 char *s;
402 int r;
403 struct weston_config_section *section;
404 struct weston_config *config = data;
405
406 section = weston_config_get_section(config,
407 "bucket", "color", "pink");
408 ZUC_ASSERT_NULL(section);
Kristian Høgsberg73274712013-04-01 12:41:23 -0400409 r = weston_config_section_get_string(section, "contents", &s, "eels");
Jon A. Cruza67c5412015-07-15 19:22:42 -0700410
411 ZUC_ASSERTG_EQ(-1, r, out_free);
412 ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
413 ZUC_ASSERTG_STREQ("eels", s, out_free);
414
415out_free:
Kristian Høgsberg73274712013-04-01 12:41:23 -0400416 free(s);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700417}
418
Daniel Stoneb8c39262019-11-26 00:16:35 +0000419ZUC_TEST_F(config_test_t1, test016, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700420{
421 const char *name;
422 int i;
423 struct weston_config_section *section;
424 struct weston_config *config = data;
Kristian Høgsberg73274712013-04-01 12:41:23 -0400425
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400426 section = NULL;
427 i = 0;
428 while (weston_config_next_section(config, &section, &name))
Jon A. Cruza67c5412015-07-15 19:22:42 -0700429 ZUC_ASSERT_STREQ(section_names[i++], name);
Kristian Høgsbergf73f3162013-05-26 20:50:53 -0400430
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700431 ZUC_ASSERT_EQ(6, i);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700432}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400433
Daniel Stoneb8c39262019-11-26 00:16:35 +0000434ZUC_TEST_F(config_test_t1, test017, data)
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700435{
436 int r;
437 int32_t n;
438 struct weston_config_section *section;
439 struct weston_config *config = data;
440
441 section = weston_config_get_section(config, "bar", NULL, NULL);
442 r = weston_config_section_get_int(section, "zero", &n, 600);
443
444 ZUC_ASSERT_EQ(0, r);
445 ZUC_ASSERT_EQ(0, n);
446 ZUC_ASSERT_EQ(0, errno);
447}
448
Daniel Stoneb8c39262019-11-26 00:16:35 +0000449ZUC_TEST_F(config_test_t1, test018, data)
Bryce Harringtoncbc05372016-07-07 14:08:28 -0700450{
451 int r;
452 uint32_t n;
453 struct weston_config_section *section;
454 struct weston_config *config = data;
455
456 section = weston_config_get_section(config, "bar", NULL, NULL);
457 r = weston_config_section_get_uint(section, "zero", &n, 600);
458
459 ZUC_ASSERT_EQ(0, r);
460 ZUC_ASSERT_EQ(0, n);
461 ZUC_ASSERT_EQ(0, errno);
462}
463
Daniel Stoneb8c39262019-11-26 00:16:35 +0000464ZUC_TEST_F(config_test_t1, test019, data)
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700465{
466 int r;
467 uint32_t n;
468 struct weston_config_section *section;
469 struct weston_config *config = data;
470
471 section = weston_config_get_section(config, "colors", NULL, NULL);
472 r = weston_config_section_get_color(section, "none", &n, 0xff336699);
473
474 ZUC_ASSERT_EQ(0, r);
475 ZUC_ASSERT_EQ(0x000000, n);
476 ZUC_ASSERT_EQ(0, errno);
477}
478
Daniel Stoneb8c39262019-11-26 00:16:35 +0000479ZUC_TEST_F(config_test_t1, test020, data)
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700480{
481 int r;
482 uint32_t n;
483 struct weston_config_section *section;
484 struct weston_config *config = data;
485
486 section = weston_config_get_section(config, "colors", NULL, NULL);
487 r = weston_config_section_get_color(section, "low", &n, 0xff336699);
488
489 ZUC_ASSERT_EQ(0, r);
490 ZUC_ASSERT_EQ(0x11223344, n);
491 ZUC_ASSERT_EQ(0, errno);
492}
493
Daniel Stoneb8c39262019-11-26 00:16:35 +0000494ZUC_TEST_F(config_test_t1, test021, data)
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700495{
496 int r;
497 uint32_t n;
498 struct weston_config_section *section;
499 struct weston_config *config = data;
500
501 section = weston_config_get_section(config, "colors", NULL, NULL);
502 r = weston_config_section_get_color(section, "high", &n, 0xff336699);
503
504 ZUC_ASSERT_EQ(0, r);
505 ZUC_ASSERT_EQ(0xff00ff00, n);
506 ZUC_ASSERT_EQ(0, errno);
507}
508
Daniel Stoneb8c39262019-11-26 00:16:35 +0000509ZUC_TEST_F(config_test_t1, test022, data)
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700510{
511 int r;
512 uint32_t n;
513 struct weston_config_section *section;
514 struct weston_config *config = data;
515
516 // Treat colors as hex values even if missing the leading 0x
517 section = weston_config_get_section(config, "colors", NULL, NULL);
518 r = weston_config_section_get_color(section, "oct", &n, 0xff336699);
519
520 ZUC_ASSERT_EQ(0, r);
521 ZUC_ASSERT_EQ(0x01234567, n);
522 ZUC_ASSERT_EQ(0, errno);
523}
524
Daniel Stoneb8c39262019-11-26 00:16:35 +0000525ZUC_TEST_F(config_test_t1, test023, data)
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700526{
527 int r;
528 uint32_t n;
529 struct weston_config_section *section;
530 struct weston_config *config = data;
531
532 // Treat colors as hex values even if missing the leading 0x
533 section = weston_config_get_section(config, "colors", NULL, NULL);
534 r = weston_config_section_get_color(section, "dec", &n, 0xff336699);
535
536 ZUC_ASSERT_EQ(0, r);
537 ZUC_ASSERT_EQ(0x12345670, n);
538 ZUC_ASSERT_EQ(0, errno);
539}
540
Daniel Stoneb8c39262019-11-26 00:16:35 +0000541ZUC_TEST_F(config_test_t1, test024, data)
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700542{
543 int r;
544 uint32_t n;
545 struct weston_config_section *section;
546 struct weston_config *config = data;
547
548 // 7-digit colors are not valid (most likely typos)
549 section = weston_config_get_section(config, "colors", NULL, NULL);
550 r = weston_config_section_get_color(section, "short", &n, 0xff336699);
551
552 ZUC_ASSERT_EQ(-1, r);
553 ZUC_ASSERT_EQ(0xff336699, n);
554 ZUC_ASSERT_EQ(EINVAL, errno);
555}
556
Daniel Stoneb8c39262019-11-26 00:16:35 +0000557ZUC_TEST_F(config_test_t1, test025, data)
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700558{
559 int r;
560 uint32_t n;
561 struct weston_config_section *section;
562 struct weston_config *config = data;
563
564 // String color names are unsupported
565 section = weston_config_get_section(config, "bucket", NULL, NULL);
566 r = weston_config_section_get_color(section, "color", &n, 0xff336699);
567
568 ZUC_ASSERT_EQ(-1, r);
569 ZUC_ASSERT_EQ(0xff336699, n);
570 ZUC_ASSERT_EQ(EINVAL, errno);
571}
572
Daniel Stoneb8c39262019-11-26 00:16:35 +0000573ZUC_TEST_F(config_test_t1, test026, data)
Bryce Harringtond0716f42016-07-14 18:28:04 -0700574{
575 int r;
576 int32_t n;
577 struct weston_config_section *section;
578 struct weston_config *config = data;
579
580 section = weston_config_get_section(config, "bar", NULL, NULL);
581 r = weston_config_section_get_int(section, "negative", &n, 600);
582
583 ZUC_ASSERT_EQ(0, r);
584 ZUC_ASSERT_EQ(-42, n);
585 ZUC_ASSERT_EQ(0, errno);
586}
587
Daniel Stoneb8c39262019-11-26 00:16:35 +0000588ZUC_TEST_F(config_test_t1, test027, data)
Bryce Harringtond0716f42016-07-14 18:28:04 -0700589{
590 int r;
591 uint32_t n;
592 struct weston_config_section *section;
593 struct weston_config *config = data;
594
595 section = weston_config_get_section(config, "bar", NULL, NULL);
596 r = weston_config_section_get_uint(section, "negative", &n, 600);
597
598 ZUC_ASSERT_EQ(-1, r);
599 ZUC_ASSERT_EQ(600, n);
600 ZUC_ASSERT_EQ(ERANGE, errno);
601}
602
Jon Cruzecf819b2015-10-22 21:11:12 -0700603ZUC_TEST_F(config_test_t2, doesnt_parse, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700604{
605 struct weston_config *config = data;
606 ZUC_ASSERT_NULL(config);
607}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400608
Jon Cruzecf819b2015-10-22 21:11:12 -0700609ZUC_TEST_F(config_test_t3, doesnt_parse, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700610{
611 struct weston_config *config = data;
612 ZUC_ASSERT_NULL(config);
613}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400614
Jon Cruzecf819b2015-10-22 21:11:12 -0700615ZUC_TEST_F(config_test_t4, doesnt_parse, data)
Jon A. Cruza67c5412015-07-15 19:22:42 -0700616{
617 struct weston_config *config = data;
618 ZUC_ASSERT_NULL(config);
619}
Kristian Høgsberg73274712013-04-01 12:41:23 -0400620
Jon A. Cruza67c5412015-07-15 19:22:42 -0700621ZUC_TEST(config_test, destroy_null)
622{
Kristian Høgsberg82189f72013-05-28 15:34:46 -0400623 weston_config_destroy(NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700624 ZUC_ASSERT_EQ(0, weston_config_next_section(NULL, NULL, NULL));
625}
Kristian Høgsberg82189f72013-05-28 15:34:46 -0400626
Jon A. Cruza67c5412015-07-15 19:22:42 -0700627ZUC_TEST(config_test, section_from_null)
628{
629 struct weston_config_section *section;
Kristian Høgsberg82189f72013-05-28 15:34:46 -0400630 section = weston_config_get_section(NULL, "bucket", NULL, NULL);
Jon A. Cruza67c5412015-07-15 19:22:42 -0700631 ZUC_ASSERT_NULL(section);
Kristian Høgsberg73274712013-04-01 12:41:23 -0400632}