2013-04-01 20:41:23 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2013 Intel Corporation
|
|
|
|
*
|
2015-06-12 01:39:40 +03:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
2013-04-01 20:41:23 +04:00
|
|
|
*
|
2015-06-12 01:39:40 +03:00
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial
|
|
|
|
* portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2013-04-01 20:41:23 +04:00
|
|
|
*/
|
|
|
|
|
2014-04-07 15:40:35 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-04-01 20:41:23 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2015-07-16 05:22:42 +03:00
|
|
|
#include <stdio.h>
|
2013-04-01 20:41:23 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2013-07-09 03:06:06 +04:00
|
|
|
#include <unistd.h>
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2019-04-04 14:27:31 +03:00
|
|
|
#include <libweston/config-parser.h>
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
#include "shared/helpers.h"
|
|
|
|
#include "zunitc/zunitc.h"
|
|
|
|
|
|
|
|
struct fixture_data {
|
|
|
|
const char *text;
|
|
|
|
struct weston_config *config;
|
|
|
|
};
|
|
|
|
|
2013-04-01 20:41:23 +04:00
|
|
|
static struct weston_config *
|
2015-07-16 05:22:42 +03:00
|
|
|
load_config(const char *text)
|
2013-04-01 20:41:23 +04:00
|
|
|
{
|
2015-07-16 05:22:42 +03:00
|
|
|
struct weston_config *config = NULL;
|
|
|
|
int len = 0;
|
|
|
|
int fd = -1;
|
2013-04-01 20:41:23 +04:00
|
|
|
char file[] = "/tmp/weston-config-parser-test-XXXXXX";
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERTG_NOT_NULL(text, out);
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
fd = mkstemp(file);
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERTG_NE(-1, fd, out);
|
|
|
|
|
2013-04-01 20:41:23 +04:00
|
|
|
len = write(fd, text, strlen(text));
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERTG_EQ((int)strlen(text), len, out_close);
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2013-09-22 10:02:31 +04:00
|
|
|
config = weston_config_parse(file);
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
out_close:
|
2013-04-01 20:41:23 +04:00
|
|
|
close(fd);
|
|
|
|
unlink(file);
|
2015-07-16 05:22:42 +03:00
|
|
|
out:
|
|
|
|
return config;
|
|
|
|
}
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
static void *
|
|
|
|
setup_test_config(void *data)
|
|
|
|
{
|
|
|
|
struct weston_config *config = load_config(data);
|
|
|
|
ZUC_ASSERTG_NOT_NULL(config, out);
|
|
|
|
|
|
|
|
out:
|
2013-04-01 20:41:23 +04:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
static void *
|
|
|
|
setup_test_config_failing(void *data)
|
|
|
|
{
|
|
|
|
struct weston_config *config = load_config(data);
|
|
|
|
ZUC_ASSERTG_NULL(config, err_free);
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
return config;
|
|
|
|
err_free:
|
|
|
|
weston_config_destroy(config);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_test_config(void *data)
|
|
|
|
{
|
|
|
|
struct weston_config *config = data;
|
|
|
|
ZUC_ASSERT_NOT_NULL(config);
|
|
|
|
weston_config_destroy(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct zuc_fixture config_test_t0 = {
|
|
|
|
.data = "# nothing in this file...\n",
|
|
|
|
.set_up = setup_test_config,
|
|
|
|
.tear_down = cleanup_test_config
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct zuc_fixture config_test_t1 = {
|
|
|
|
.data =
|
2013-04-01 20:41:23 +04:00
|
|
|
"# comment line here...\n"
|
|
|
|
"\n"
|
|
|
|
"[foo]\n"
|
|
|
|
"a=b\n"
|
|
|
|
"name= Roy Batty \n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"[bar]\n"
|
|
|
|
"# more comments\n"
|
|
|
|
"number=5252\n"
|
2016-07-08 00:08:28 +03:00
|
|
|
"zero=0\n"
|
2016-07-15 04:28:04 +03:00
|
|
|
"negative=-42\n"
|
2013-04-01 20:41:23 +04:00
|
|
|
"flag=false\n"
|
|
|
|
"\n"
|
config-parser: Add weston_config_section_get_color
Previously weston_config_section_get_uint was serving dual purpose for
parsing both unsigned decimal integer values (ids, counts, seconds,
etc.) and hexadecimal values (colors), by relying on strtoul's
auto-detection mechanism.
However, this usage is unable to catch certain kinds of error
conditions, such as specifying a negative number where an unsigned
should be used. And for colors in particular, it would misparse hex
values if the leading 0x was omitted. E.g. "background-color=99999999"
would render a near-black background (effectively 0x05f5e0ff) instead of
medium grey, and "background-color=ffffffff" would be treated as an
error rather than white. "background-color=0x01234567",
"background-color=01234567", and "background-color=1234567" each
resulted in the value being parsed as hexadecimal, octal, and decimal
respectively, resulting in colors 0x01234567, 0x00053977, and 0x0012d687
being displayed.
This new routine forces hexadecimal to be used in all cases when parsing
color values, so "0x01234567" and "01234567" result in the same color
value, "99999999" is grey, and "ffffffff" is white. It also requires
exactly 8 or 10 digits (other lengths likely indicate typos), or the
value "0" (black).
Signed-off-by: Bryce Harrington <bryce@osg.samsung.com>
Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
2016-07-15 04:28:03 +03:00
|
|
|
"[colors]\n"
|
|
|
|
"none=0x00000000\n"
|
|
|
|
"low=0x11223344\n"
|
|
|
|
"high=0xff00ff00\n"
|
|
|
|
"oct=01234567\n"
|
|
|
|
"dec=12345670\n"
|
|
|
|
"short=1234567\n"
|
|
|
|
"\n"
|
2013-04-01 20:41:23 +04:00
|
|
|
"[stuff]\n"
|
|
|
|
"flag= true \n"
|
|
|
|
"\n"
|
|
|
|
"[bucket]\n"
|
|
|
|
"color=blue \n"
|
|
|
|
"contents=live crabs\n"
|
|
|
|
"pinchy=true\n"
|
|
|
|
"\n"
|
|
|
|
"[bucket]\n"
|
|
|
|
"material=plastic \n"
|
|
|
|
"color=red\n"
|
2015-07-16 05:22:42 +03:00
|
|
|
"contents=sand\n",
|
|
|
|
.set_up = setup_test_config,
|
|
|
|
.tear_down = cleanup_test_config
|
|
|
|
};
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2013-05-27 04:50:53 +04:00
|
|
|
static const char *section_names[] = {
|
config-parser: Add weston_config_section_get_color
Previously weston_config_section_get_uint was serving dual purpose for
parsing both unsigned decimal integer values (ids, counts, seconds,
etc.) and hexadecimal values (colors), by relying on strtoul's
auto-detection mechanism.
However, this usage is unable to catch certain kinds of error
conditions, such as specifying a negative number where an unsigned
should be used. And for colors in particular, it would misparse hex
values if the leading 0x was omitted. E.g. "background-color=99999999"
would render a near-black background (effectively 0x05f5e0ff) instead of
medium grey, and "background-color=ffffffff" would be treated as an
error rather than white. "background-color=0x01234567",
"background-color=01234567", and "background-color=1234567" each
resulted in the value being parsed as hexadecimal, octal, and decimal
respectively, resulting in colors 0x01234567, 0x00053977, and 0x0012d687
being displayed.
This new routine forces hexadecimal to be used in all cases when parsing
color values, so "0x01234567" and "01234567" result in the same color
value, "99999999" is grey, and "ffffffff" is white. It also requires
exactly 8 or 10 digits (other lengths likely indicate typos), or the
value "0" (black).
Signed-off-by: Bryce Harrington <bryce@osg.samsung.com>
Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
2016-07-15 04:28:03 +03:00
|
|
|
"foo", "bar", "colors", "stuff", "bucket", "bucket"
|
2013-05-27 04:50:53 +04:00
|
|
|
};
|
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
/*
|
|
|
|
* Since these next few won't parse, we don't add the tear_down to
|
|
|
|
* attempt cleanup.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct zuc_fixture config_test_t2 = {
|
|
|
|
.data =
|
2013-04-01 20:41:23 +04:00
|
|
|
"# invalid section...\n"
|
2015-07-16 05:22:42 +03:00
|
|
|
"[this bracket isn't closed\n",
|
|
|
|
.set_up = setup_test_config_failing,
|
|
|
|
};
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
static struct zuc_fixture config_test_t3 = {
|
|
|
|
.data =
|
2013-04-01 20:41:23 +04:00
|
|
|
"# line without = ...\n"
|
|
|
|
"[bambam]\n"
|
2015-07-16 05:22:42 +03:00
|
|
|
"this line isn't any kind of valid\n",
|
|
|
|
.set_up = setup_test_config_failing,
|
|
|
|
};
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
static struct zuc_fixture config_test_t4 = {
|
|
|
|
.data =
|
2013-04-01 20:41:23 +04:00
|
|
|
"# starting with = ...\n"
|
|
|
|
"[bambam]\n"
|
2015-07-16 05:22:42 +03:00
|
|
|
"=not valid at all\n",
|
|
|
|
.set_up = setup_test_config_failing,
|
|
|
|
};
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t0, comment_only, data)
|
2013-04-01 20:41:23 +04:00
|
|
|
{
|
2015-07-16 05:22:42 +03:00
|
|
|
struct weston_config *config = data;
|
|
|
|
ZUC_ASSERT_NOT_NULL(config);
|
|
|
|
}
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
/** @todo individual t1 tests should have more descriptive names. */
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test001, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
ZUC_ASSERT_NOT_NULL(config);
|
|
|
|
section = weston_config_get_section(config,
|
|
|
|
"mollusc", NULL, NULL);
|
|
|
|
ZUC_ASSERT_NULL(section);
|
|
|
|
}
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test002, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int r;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "foo", NULL, NULL);
|
|
|
|
r = weston_config_section_get_string(section, "a", &s, NULL);
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERTG_EQ(0, r, out_free);
|
|
|
|
ZUC_ASSERTG_STREQ("b", s, out_free);
|
|
|
|
|
|
|
|
out_free:
|
2013-04-01 20:41:23 +04:00
|
|
|
free(s);
|
2015-07-16 05:22:42 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test003, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int r;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "foo", NULL, NULL);
|
|
|
|
r = weston_config_section_get_string(section, "b", &s, NULL);
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(-1, r);
|
|
|
|
ZUC_ASSERT_EQ(ENOENT, errno);
|
|
|
|
ZUC_ASSERT_NULL(s);
|
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test004, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int r;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "foo", NULL, NULL);
|
|
|
|
r = weston_config_section_get_string(section, "name", &s, NULL);
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERTG_EQ(0, r, out_free);
|
|
|
|
ZUC_ASSERTG_STREQ("Roy Batty", s, out_free);
|
|
|
|
|
|
|
|
out_free:
|
2013-04-01 20:41:23 +04:00
|
|
|
free(s);
|
2015-07-16 05:22:42 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test005, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int r;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_string(section, "a", &s, "boo");
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERTG_EQ(-1, r, out_free);
|
|
|
|
ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
|
|
|
|
ZUC_ASSERTG_STREQ("boo", s, out_free);
|
|
|
|
|
|
|
|
out_free:
|
2013-04-01 20:41:23 +04:00
|
|
|
free(s);
|
2015-07-16 05:22:42 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test006, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
int32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_int(section, "number", &n, 600);
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(5252, n);
|
2016-07-08 00:08:28 +03:00
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
2015-07-16 05:22:42 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test007, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
int32_t n;
|
|
|
|
struct weston_config_section *section;
|
2015-10-07 19:51:29 +03:00
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_int(section, "+++", &n, 700);
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(-1, r);
|
|
|
|
ZUC_ASSERT_EQ(ENOENT, errno);
|
|
|
|
ZUC_ASSERT_EQ(700, n);
|
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test008, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t u;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_uint(section, "number", &u, 600);
|
2016-07-08 00:08:28 +03:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(5252, u);
|
2016-07-08 00:08:28 +03:00
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
2015-07-16 05:22:42 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test009, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t u;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_uint(section, "+++", &u, 600);
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERT_EQ(-1, r);
|
|
|
|
ZUC_ASSERT_EQ(ENOENT, errno);
|
|
|
|
ZUC_ASSERT_EQ(600, u);
|
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test010, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
int r, b;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_bool(section, "flag", &b, 600);
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(0, b);
|
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test011, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
int r, b;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config, "stuff", NULL, NULL);
|
|
|
|
r = weston_config_section_get_bool(section, "flag", &b, -1);
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(1, b);
|
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test012, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
int r, b;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "stuff", NULL, NULL);
|
|
|
|
r = weston_config_section_get_bool(section, "flag", &b, -1);
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(1, b);
|
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test013, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
int r, b;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
|
|
|
section = weston_config_get_section(config, "stuff", NULL, NULL);
|
|
|
|
r = weston_config_section_get_bool(section, "bonk", &b, -1);
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERT_EQ(-1, r);
|
|
|
|
ZUC_ASSERT_EQ(ENOENT, errno);
|
|
|
|
ZUC_ASSERT_EQ(-1, b);
|
|
|
|
}
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test014, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int r;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config,
|
|
|
|
"bucket", "color", "blue");
|
2013-04-01 20:41:23 +04:00
|
|
|
r = weston_config_section_get_string(section, "contents", &s, NULL);
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERTG_EQ(0, r, out_free);
|
|
|
|
ZUC_ASSERTG_STREQ("live crabs", s, out_free);
|
|
|
|
|
|
|
|
out_free:
|
2013-04-01 20:41:23 +04:00
|
|
|
free(s);
|
2015-07-16 05:22:42 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test015, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int r;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
section = weston_config_get_section(config,
|
|
|
|
"bucket", "color", "red");
|
2013-04-01 20:41:23 +04:00
|
|
|
r = weston_config_section_get_string(section, "contents", &s, NULL);
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERTG_EQ(0, r, out_free);
|
|
|
|
ZUC_ASSERTG_STREQ("sand", s, out_free);
|
|
|
|
|
|
|
|
out_free:
|
2013-04-01 20:41:23 +04:00
|
|
|
free(s);
|
2015-07-16 05:22:42 +03:00
|
|
|
}
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test016, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int r;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config,
|
|
|
|
"bucket", "color", "pink");
|
|
|
|
ZUC_ASSERT_NULL(section);
|
2013-04-01 20:41:23 +04:00
|
|
|
r = weston_config_section_get_string(section, "contents", &s, "eels");
|
2015-07-16 05:22:42 +03:00
|
|
|
|
|
|
|
ZUC_ASSERTG_EQ(-1, r, out_free);
|
|
|
|
ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
|
|
|
|
ZUC_ASSERTG_STREQ("eels", s, out_free);
|
|
|
|
|
|
|
|
out_free:
|
2013-04-01 20:41:23 +04:00
|
|
|
free(s);
|
2015-07-16 05:22:42 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test017, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
int i;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2013-05-27 04:50:53 +04:00
|
|
|
section = NULL;
|
|
|
|
i = 0;
|
|
|
|
while (weston_config_next_section(config, §ion, &name))
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERT_STREQ(section_names[i++], name);
|
2013-05-27 04:50:53 +04:00
|
|
|
|
config-parser: Add weston_config_section_get_color
Previously weston_config_section_get_uint was serving dual purpose for
parsing both unsigned decimal integer values (ids, counts, seconds,
etc.) and hexadecimal values (colors), by relying on strtoul's
auto-detection mechanism.
However, this usage is unable to catch certain kinds of error
conditions, such as specifying a negative number where an unsigned
should be used. And for colors in particular, it would misparse hex
values if the leading 0x was omitted. E.g. "background-color=99999999"
would render a near-black background (effectively 0x05f5e0ff) instead of
medium grey, and "background-color=ffffffff" would be treated as an
error rather than white. "background-color=0x01234567",
"background-color=01234567", and "background-color=1234567" each
resulted in the value being parsed as hexadecimal, octal, and decimal
respectively, resulting in colors 0x01234567, 0x00053977, and 0x0012d687
being displayed.
This new routine forces hexadecimal to be used in all cases when parsing
color values, so "0x01234567" and "01234567" result in the same color
value, "99999999" is grey, and "ffffffff" is white. It also requires
exactly 8 or 10 digits (other lengths likely indicate typos), or the
value "0" (black).
Signed-off-by: Bryce Harrington <bryce@osg.samsung.com>
Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
2016-07-15 04:28:03 +03:00
|
|
|
ZUC_ASSERT_EQ(6, i);
|
2015-07-16 05:22:42 +03:00
|
|
|
}
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2016-07-08 00:08:28 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test018, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
int32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_int(section, "zero", &n, 600);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(0, n);
|
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZUC_TEST_F(config_test_t1, test019, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_uint(section, "zero", &n, 600);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(0, n);
|
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
|
|
|
}
|
|
|
|
|
config-parser: Add weston_config_section_get_color
Previously weston_config_section_get_uint was serving dual purpose for
parsing both unsigned decimal integer values (ids, counts, seconds,
etc.) and hexadecimal values (colors), by relying on strtoul's
auto-detection mechanism.
However, this usage is unable to catch certain kinds of error
conditions, such as specifying a negative number where an unsigned
should be used. And for colors in particular, it would misparse hex
values if the leading 0x was omitted. E.g. "background-color=99999999"
would render a near-black background (effectively 0x05f5e0ff) instead of
medium grey, and "background-color=ffffffff" would be treated as an
error rather than white. "background-color=0x01234567",
"background-color=01234567", and "background-color=1234567" each
resulted in the value being parsed as hexadecimal, octal, and decimal
respectively, resulting in colors 0x01234567, 0x00053977, and 0x0012d687
being displayed.
This new routine forces hexadecimal to be used in all cases when parsing
color values, so "0x01234567" and "01234567" result in the same color
value, "99999999" is grey, and "ffffffff" is white. It also requires
exactly 8 or 10 digits (other lengths likely indicate typos), or the
value "0" (black).
Signed-off-by: Bryce Harrington <bryce@osg.samsung.com>
Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
2016-07-15 04:28:03 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test020, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config, "colors", NULL, NULL);
|
|
|
|
r = weston_config_section_get_color(section, "none", &n, 0xff336699);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(0x000000, n);
|
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZUC_TEST_F(config_test_t1, test021, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config, "colors", NULL, NULL);
|
|
|
|
r = weston_config_section_get_color(section, "low", &n, 0xff336699);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(0x11223344, n);
|
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZUC_TEST_F(config_test_t1, test022, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config, "colors", NULL, NULL);
|
|
|
|
r = weston_config_section_get_color(section, "high", &n, 0xff336699);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(0xff00ff00, n);
|
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZUC_TEST_F(config_test_t1, test023, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
// Treat colors as hex values even if missing the leading 0x
|
|
|
|
section = weston_config_get_section(config, "colors", NULL, NULL);
|
|
|
|
r = weston_config_section_get_color(section, "oct", &n, 0xff336699);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(0x01234567, n);
|
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZUC_TEST_F(config_test_t1, test024, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
// Treat colors as hex values even if missing the leading 0x
|
|
|
|
section = weston_config_get_section(config, "colors", NULL, NULL);
|
|
|
|
r = weston_config_section_get_color(section, "dec", &n, 0xff336699);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(0x12345670, n);
|
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZUC_TEST_F(config_test_t1, test025, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
// 7-digit colors are not valid (most likely typos)
|
|
|
|
section = weston_config_get_section(config, "colors", NULL, NULL);
|
|
|
|
r = weston_config_section_get_color(section, "short", &n, 0xff336699);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(-1, r);
|
|
|
|
ZUC_ASSERT_EQ(0xff336699, n);
|
|
|
|
ZUC_ASSERT_EQ(EINVAL, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZUC_TEST_F(config_test_t1, test026, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
// String color names are unsupported
|
|
|
|
section = weston_config_get_section(config, "bucket", NULL, NULL);
|
|
|
|
r = weston_config_section_get_color(section, "color", &n, 0xff336699);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(-1, r);
|
|
|
|
ZUC_ASSERT_EQ(0xff336699, n);
|
|
|
|
ZUC_ASSERT_EQ(EINVAL, errno);
|
|
|
|
}
|
|
|
|
|
2016-07-15 04:28:04 +03:00
|
|
|
ZUC_TEST_F(config_test_t1, test027, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
int32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_int(section, "negative", &n, 600);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(0, r);
|
|
|
|
ZUC_ASSERT_EQ(-42, n);
|
|
|
|
ZUC_ASSERT_EQ(0, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZUC_TEST_F(config_test_t1, test028, data)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
uint32_t n;
|
|
|
|
struct weston_config_section *section;
|
|
|
|
struct weston_config *config = data;
|
|
|
|
|
|
|
|
section = weston_config_get_section(config, "bar", NULL, NULL);
|
|
|
|
r = weston_config_section_get_uint(section, "negative", &n, 600);
|
|
|
|
|
|
|
|
ZUC_ASSERT_EQ(-1, r);
|
|
|
|
ZUC_ASSERT_EQ(600, n);
|
|
|
|
ZUC_ASSERT_EQ(ERANGE, errno);
|
|
|
|
}
|
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t2, doesnt_parse, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
struct weston_config *config = data;
|
|
|
|
ZUC_ASSERT_NULL(config);
|
|
|
|
}
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t3, doesnt_parse, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
struct weston_config *config = data;
|
|
|
|
ZUC_ASSERT_NULL(config);
|
|
|
|
}
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-10-23 07:11:12 +03:00
|
|
|
ZUC_TEST_F(config_test_t4, doesnt_parse, data)
|
2015-07-16 05:22:42 +03:00
|
|
|
{
|
|
|
|
struct weston_config *config = data;
|
|
|
|
ZUC_ASSERT_NULL(config);
|
|
|
|
}
|
2013-04-01 20:41:23 +04:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_TEST(config_test, destroy_null)
|
|
|
|
{
|
2013-05-28 23:34:46 +04:00
|
|
|
weston_config_destroy(NULL);
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERT_EQ(0, weston_config_next_section(NULL, NULL, NULL));
|
|
|
|
}
|
2013-05-28 23:34:46 +04:00
|
|
|
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_TEST(config_test, section_from_null)
|
|
|
|
{
|
|
|
|
struct weston_config_section *section;
|
2013-05-28 23:34:46 +04:00
|
|
|
section = weston_config_get_section(NULL, "bucket", NULL, NULL);
|
2015-07-16 05:22:42 +03:00
|
|
|
ZUC_ASSERT_NULL(section);
|
2013-04-01 20:41:23 +04:00
|
|
|
}
|