Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Intel Corporation |
| 3 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 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: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 11 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 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. |
| 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øgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 24 | */ |
| 25 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <stdint.h> |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 31 | #include <string.h> |
| 32 | #include <assert.h> |
| 33 | #include <errno.h> |
Kristian Høgsberg | ac3a8b8 | 2013-07-08 19:06:06 -0400 | [diff] [blame] | 34 | #include <unistd.h> |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 35 | |
| 36 | #include "config-parser.h" |
| 37 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 38 | #include "shared/helpers.h" |
| 39 | #include "zunitc/zunitc.h" |
| 40 | |
| 41 | struct fixture_data { |
| 42 | const char *text; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 43 | struct weston_config *config; |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static struct weston_config * |
| 47 | load_config(const char *text) |
| 48 | { |
| 49 | struct weston_config *config = NULL; |
| 50 | int len = 0; |
| 51 | int fd = -1; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 52 | char file[] = "/tmp/weston-config-parser-test-XXXXXX"; |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 53 | |
| 54 | ZUC_ASSERTG_NOT_NULL(text, out); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 55 | |
| 56 | fd = mkstemp(file); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 57 | ZUC_ASSERTG_NE(-1, fd, out); |
| 58 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 59 | len = write(fd, text, strlen(text)); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 60 | ZUC_ASSERTG_EQ((int)strlen(text), len, out_close); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 61 | |
Kristian Høgsberg | 1abe048 | 2013-09-21 23:02:31 -0700 | [diff] [blame] | 62 | config = weston_config_parse(file); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 63 | |
| 64 | out_close: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 65 | close(fd); |
| 66 | unlink(file); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 67 | out: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 68 | return config; |
| 69 | } |
| 70 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 71 | static void * |
| 72 | setup_test_config(void *data) |
| 73 | { |
| 74 | struct weston_config *config = load_config(data); |
| 75 | ZUC_ASSERTG_NOT_NULL(config, out); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 76 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 77 | out: |
| 78 | return config; |
| 79 | } |
| 80 | |
| 81 | static void * |
| 82 | setup_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; |
| 88 | err_free: |
| 89 | weston_config_destroy(config); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | static void |
| 94 | cleanup_test_config(void *data) |
| 95 | { |
| 96 | struct weston_config *config = data; |
| 97 | ZUC_ASSERT_NOT_NULL(config); |
| 98 | weston_config_destroy(config); |
| 99 | } |
| 100 | |
| 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 |
| 105 | }; |
| 106 | |
| 107 | static struct zuc_fixture config_test_t1 = { |
| 108 | .data = |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 109 | "# 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 Harrington | cbc0537 | 2016-07-07 14:08:28 -0700 | [diff] [blame] | 119 | "zero=0\n" |
Bryce Harrington | d0716f4 | 2016-07-14 18:28:04 -0700 | [diff] [blame] | 120 | "negative=-42\n" |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 121 | "flag=false\n" |
| 122 | "\n" |
Bryce Harrington | e776f2a | 2016-07-14 18:28:03 -0700 | [diff] [blame] | 123 | "[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øgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 131 | "[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. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 142 | "contents=sand\n", |
| 143 | .set_up = setup_test_config, |
| 144 | .tear_down = cleanup_test_config |
| 145 | }; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 146 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 147 | static const char *section_names[] = { |
Bryce Harrington | e776f2a | 2016-07-14 18:28:03 -0700 | [diff] [blame] | 148 | "foo", "bar", "colors", "stuff", "bucket", "bucket" |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 149 | }; |
| 150 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 151 | /* |
| 152 | * Since these next few won't parse, we don't add the tear_down to |
| 153 | * attempt cleanup. |
| 154 | */ |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 155 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 156 | static 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 | |
| 163 | static struct zuc_fixture config_test_t3 = { |
| 164 | .data = |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 165 | "# line without = ...\n" |
| 166 | "[bambam]\n" |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 167 | "this line isn't any kind of valid\n", |
| 168 | .set_up = setup_test_config_failing, |
| 169 | }; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 170 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 171 | static struct zuc_fixture config_test_t4 = { |
| 172 | .data = |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 173 | "# starting with = ...\n" |
| 174 | "[bambam]\n" |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 175 | "=not valid at all\n", |
| 176 | .set_up = setup_test_config_failing, |
| 177 | }; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 178 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 179 | ZUC_TEST_F(config_test_t0, comment_only, data) |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 180 | { |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 181 | 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 Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 187 | ZUC_TEST_F(config_test_t1, test001, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 188 | { |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 189 | struct weston_config_section *section; |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 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); |
| 195 | } |
| 196 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 197 | ZUC_TEST_F(config_test_t1, test002, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 198 | { |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 199 | char *s; |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 200 | int r; |
| 201 | struct weston_config_section *section; |
| 202 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 203 | |
| 204 | section = weston_config_get_section(config, "foo", NULL, NULL); |
| 205 | r = weston_config_section_get_string(section, "a", &s, NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 206 | |
| 207 | ZUC_ASSERTG_EQ(0, r, out_free); |
| 208 | ZUC_ASSERTG_STREQ("b", s, out_free); |
| 209 | |
| 210 | out_free: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 211 | free(s); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 214 | ZUC_TEST_F(config_test_t1, test003, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 215 | { |
| 216 | char *s; |
| 217 | int r; |
| 218 | struct weston_config_section *section; |
| 219 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 220 | |
| 221 | section = weston_config_get_section(config, "foo", NULL, NULL); |
| 222 | r = weston_config_section_get_string(section, "b", &s, NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 223 | |
| 224 | ZUC_ASSERT_EQ(-1, r); |
| 225 | ZUC_ASSERT_EQ(ENOENT, errno); |
| 226 | ZUC_ASSERT_NULL(s); |
| 227 | } |
| 228 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 229 | ZUC_TEST_F(config_test_t1, test004, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 230 | { |
| 231 | char *s; |
| 232 | int r; |
| 233 | struct weston_config_section *section; |
| 234 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 235 | |
| 236 | section = weston_config_get_section(config, "foo", NULL, NULL); |
| 237 | r = weston_config_section_get_string(section, "name", &s, NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 238 | |
| 239 | ZUC_ASSERTG_EQ(0, r, out_free); |
| 240 | ZUC_ASSERTG_STREQ("Roy Batty", s, out_free); |
| 241 | |
| 242 | out_free: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 243 | free(s); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 246 | ZUC_TEST_F(config_test_t1, test005, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 247 | { |
| 248 | char *s; |
| 249 | int r; |
| 250 | struct weston_config_section *section; |
| 251 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 252 | |
| 253 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 254 | r = weston_config_section_get_string(section, "a", &s, "boo"); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 255 | |
| 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 | |
| 260 | out_free: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 261 | free(s); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 264 | ZUC_TEST_F(config_test_t1, test006, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 265 | { |
| 266 | int r; |
| 267 | int32_t n; |
| 268 | struct weston_config_section *section; |
| 269 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 270 | |
| 271 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 272 | r = weston_config_section_get_int(section, "number", &n, 600); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 273 | |
| 274 | ZUC_ASSERT_EQ(0, r); |
| 275 | ZUC_ASSERT_EQ(5252, n); |
Bryce Harrington | cbc0537 | 2016-07-07 14:08:28 -0700 | [diff] [blame] | 276 | ZUC_ASSERT_EQ(0, errno); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 279 | ZUC_TEST_F(config_test_t1, test007, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 280 | { |
| 281 | int r; |
| 282 | int32_t n; |
| 283 | struct weston_config_section *section; |
Derek Foreman | bdc8c72 | 2015-10-07 11:51:29 -0500 | [diff] [blame] | 284 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 285 | |
| 286 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 287 | r = weston_config_section_get_int(section, "+++", &n, 700); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 288 | |
| 289 | ZUC_ASSERT_EQ(-1, r); |
| 290 | ZUC_ASSERT_EQ(ENOENT, errno); |
| 291 | ZUC_ASSERT_EQ(700, n); |
| 292 | } |
| 293 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 294 | ZUC_TEST_F(config_test_t1, test008, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 295 | { |
| 296 | int r; |
| 297 | uint32_t u; |
| 298 | struct weston_config_section *section; |
| 299 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 300 | |
| 301 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 302 | r = weston_config_section_get_uint(section, "number", &u, 600); |
Bryce Harrington | cbc0537 | 2016-07-07 14:08:28 -0700 | [diff] [blame] | 303 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 304 | ZUC_ASSERT_EQ(0, r); |
| 305 | ZUC_ASSERT_EQ(5252, u); |
Bryce Harrington | cbc0537 | 2016-07-07 14:08:28 -0700 | [diff] [blame] | 306 | ZUC_ASSERT_EQ(0, errno); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 309 | ZUC_TEST_F(config_test_t1, test009, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 310 | { |
| 311 | int r; |
| 312 | uint32_t u; |
| 313 | struct weston_config_section *section; |
| 314 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 315 | |
| 316 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 317 | r = weston_config_section_get_uint(section, "+++", &u, 600); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 318 | ZUC_ASSERT_EQ(-1, r); |
| 319 | ZUC_ASSERT_EQ(ENOENT, errno); |
| 320 | ZUC_ASSERT_EQ(600, u); |
| 321 | } |
| 322 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 323 | ZUC_TEST_F(config_test_t1, test010, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 324 | { |
| 325 | int r, b; |
| 326 | struct weston_config_section *section; |
| 327 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 328 | |
| 329 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 330 | r = weston_config_section_get_bool(section, "flag", &b, 600); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 331 | ZUC_ASSERT_EQ(0, r); |
| 332 | ZUC_ASSERT_EQ(0, b); |
| 333 | } |
| 334 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 335 | ZUC_TEST_F(config_test_t1, test011, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 336 | { |
| 337 | int r, b; |
| 338 | struct weston_config_section *section; |
| 339 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 340 | |
| 341 | section = weston_config_get_section(config, "stuff", NULL, NULL); |
| 342 | r = weston_config_section_get_bool(section, "flag", &b, -1); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 343 | ZUC_ASSERT_EQ(0, r); |
| 344 | ZUC_ASSERT_EQ(1, b); |
| 345 | } |
| 346 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 347 | ZUC_TEST_F(config_test_t1, test012, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 348 | { |
| 349 | int r, b; |
| 350 | struct weston_config_section *section; |
| 351 | struct weston_config *config = data; |
| 352 | |
| 353 | section = weston_config_get_section(config, "stuff", NULL, NULL); |
| 354 | r = weston_config_section_get_bool(section, "flag", &b, -1); |
| 355 | ZUC_ASSERT_EQ(0, r); |
| 356 | ZUC_ASSERT_EQ(1, b); |
| 357 | } |
| 358 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 359 | ZUC_TEST_F(config_test_t1, test013, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 360 | { |
| 361 | int r, b; |
| 362 | struct weston_config_section *section; |
| 363 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 364 | |
| 365 | section = weston_config_get_section(config, "stuff", NULL, NULL); |
| 366 | r = weston_config_section_get_bool(section, "bonk", &b, -1); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 367 | ZUC_ASSERT_EQ(-1, r); |
| 368 | ZUC_ASSERT_EQ(ENOENT, errno); |
| 369 | ZUC_ASSERT_EQ(-1, b); |
| 370 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 371 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 372 | ZUC_TEST_F(config_test_t1, test014, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 373 | { |
| 374 | char *s; |
| 375 | int r; |
| 376 | struct weston_config_section *section; |
| 377 | struct weston_config *config = data; |
| 378 | |
| 379 | section = weston_config_get_section(config, |
| 380 | "bucket", "color", "blue"); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 381 | r = weston_config_section_get_string(section, "contents", &s, NULL); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 382 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 383 | ZUC_ASSERTG_EQ(0, r, out_free); |
| 384 | ZUC_ASSERTG_STREQ("live crabs", s, out_free); |
| 385 | |
| 386 | out_free: |
| 387 | free(s); |
| 388 | } |
| 389 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 390 | ZUC_TEST_F(config_test_t1, test015, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 391 | { |
| 392 | char *s; |
| 393 | int r; |
| 394 | struct weston_config_section *section; |
| 395 | struct weston_config *config = data; |
| 396 | |
| 397 | section = weston_config_get_section(config, |
| 398 | "bucket", "color", "red"); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 399 | r = weston_config_section_get_string(section, "contents", &s, NULL); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 400 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 401 | ZUC_ASSERTG_EQ(0, r, out_free); |
| 402 | ZUC_ASSERTG_STREQ("sand", s, out_free); |
| 403 | |
| 404 | out_free: |
| 405 | free(s); |
| 406 | } |
| 407 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 408 | ZUC_TEST_F(config_test_t1, test016, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 409 | { |
| 410 | char *s; |
| 411 | int r; |
| 412 | struct weston_config_section *section; |
| 413 | struct weston_config *config = data; |
| 414 | |
| 415 | section = weston_config_get_section(config, |
| 416 | "bucket", "color", "pink"); |
| 417 | ZUC_ASSERT_NULL(section); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 418 | r = weston_config_section_get_string(section, "contents", &s, "eels"); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 419 | |
| 420 | ZUC_ASSERTG_EQ(-1, r, out_free); |
| 421 | ZUC_ASSERTG_EQ(ENOENT, errno, out_free); |
| 422 | ZUC_ASSERTG_STREQ("eels", s, out_free); |
| 423 | |
| 424 | out_free: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 425 | free(s); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 428 | ZUC_TEST_F(config_test_t1, test017, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 429 | { |
| 430 | const char *name; |
| 431 | int i; |
| 432 | struct weston_config_section *section; |
| 433 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 434 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 435 | section = NULL; |
| 436 | i = 0; |
| 437 | while (weston_config_next_section(config, §ion, &name)) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 438 | ZUC_ASSERT_STREQ(section_names[i++], name); |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 439 | |
Bryce Harrington | e776f2a | 2016-07-14 18:28:03 -0700 | [diff] [blame] | 440 | ZUC_ASSERT_EQ(6, i); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 441 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 442 | |
Bryce Harrington | cbc0537 | 2016-07-07 14:08:28 -0700 | [diff] [blame] | 443 | ZUC_TEST_F(config_test_t1, test018, data) |
| 444 | { |
| 445 | int r; |
| 446 | int32_t n; |
| 447 | struct weston_config_section *section; |
| 448 | struct weston_config *config = data; |
| 449 | |
| 450 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 451 | r = weston_config_section_get_int(section, "zero", &n, 600); |
| 452 | |
| 453 | ZUC_ASSERT_EQ(0, r); |
| 454 | ZUC_ASSERT_EQ(0, n); |
| 455 | ZUC_ASSERT_EQ(0, errno); |
| 456 | } |
| 457 | |
| 458 | ZUC_TEST_F(config_test_t1, test019, data) |
| 459 | { |
| 460 | int r; |
| 461 | uint32_t n; |
| 462 | struct weston_config_section *section; |
| 463 | struct weston_config *config = data; |
| 464 | |
| 465 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 466 | r = weston_config_section_get_uint(section, "zero", &n, 600); |
| 467 | |
| 468 | ZUC_ASSERT_EQ(0, r); |
| 469 | ZUC_ASSERT_EQ(0, n); |
| 470 | ZUC_ASSERT_EQ(0, errno); |
| 471 | } |
| 472 | |
Bryce Harrington | e776f2a | 2016-07-14 18:28:03 -0700 | [diff] [blame] | 473 | ZUC_TEST_F(config_test_t1, test020, data) |
| 474 | { |
| 475 | int r; |
| 476 | uint32_t n; |
| 477 | struct weston_config_section *section; |
| 478 | struct weston_config *config = data; |
| 479 | |
| 480 | section = weston_config_get_section(config, "colors", NULL, NULL); |
| 481 | r = weston_config_section_get_color(section, "none", &n, 0xff336699); |
| 482 | |
| 483 | ZUC_ASSERT_EQ(0, r); |
| 484 | ZUC_ASSERT_EQ(0x000000, n); |
| 485 | ZUC_ASSERT_EQ(0, errno); |
| 486 | } |
| 487 | |
| 488 | ZUC_TEST_F(config_test_t1, test021, data) |
| 489 | { |
| 490 | int r; |
| 491 | uint32_t n; |
| 492 | struct weston_config_section *section; |
| 493 | struct weston_config *config = data; |
| 494 | |
| 495 | section = weston_config_get_section(config, "colors", NULL, NULL); |
| 496 | r = weston_config_section_get_color(section, "low", &n, 0xff336699); |
| 497 | |
| 498 | ZUC_ASSERT_EQ(0, r); |
| 499 | ZUC_ASSERT_EQ(0x11223344, n); |
| 500 | ZUC_ASSERT_EQ(0, errno); |
| 501 | } |
| 502 | |
| 503 | ZUC_TEST_F(config_test_t1, test022, data) |
| 504 | { |
| 505 | int r; |
| 506 | uint32_t n; |
| 507 | struct weston_config_section *section; |
| 508 | struct weston_config *config = data; |
| 509 | |
| 510 | section = weston_config_get_section(config, "colors", NULL, NULL); |
| 511 | r = weston_config_section_get_color(section, "high", &n, 0xff336699); |
| 512 | |
| 513 | ZUC_ASSERT_EQ(0, r); |
| 514 | ZUC_ASSERT_EQ(0xff00ff00, n); |
| 515 | ZUC_ASSERT_EQ(0, errno); |
| 516 | } |
| 517 | |
| 518 | ZUC_TEST_F(config_test_t1, test023, data) |
| 519 | { |
| 520 | int r; |
| 521 | uint32_t n; |
| 522 | struct weston_config_section *section; |
| 523 | struct weston_config *config = data; |
| 524 | |
| 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); |
| 528 | |
| 529 | ZUC_ASSERT_EQ(0, r); |
| 530 | ZUC_ASSERT_EQ(0x01234567, n); |
| 531 | ZUC_ASSERT_EQ(0, errno); |
| 532 | } |
| 533 | |
| 534 | ZUC_TEST_F(config_test_t1, test024, data) |
| 535 | { |
| 536 | int r; |
| 537 | uint32_t n; |
| 538 | struct weston_config_section *section; |
| 539 | struct weston_config *config = data; |
| 540 | |
| 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); |
| 544 | |
| 545 | ZUC_ASSERT_EQ(0, r); |
| 546 | ZUC_ASSERT_EQ(0x12345670, n); |
| 547 | ZUC_ASSERT_EQ(0, errno); |
| 548 | } |
| 549 | |
| 550 | ZUC_TEST_F(config_test_t1, test025, data) |
| 551 | { |
| 552 | int r; |
| 553 | uint32_t n; |
| 554 | struct weston_config_section *section; |
| 555 | struct weston_config *config = data; |
| 556 | |
| 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); |
| 560 | |
| 561 | ZUC_ASSERT_EQ(-1, r); |
| 562 | ZUC_ASSERT_EQ(0xff336699, n); |
| 563 | ZUC_ASSERT_EQ(EINVAL, errno); |
| 564 | } |
| 565 | |
| 566 | ZUC_TEST_F(config_test_t1, test026, data) |
| 567 | { |
| 568 | int r; |
| 569 | uint32_t n; |
| 570 | struct weston_config_section *section; |
| 571 | struct weston_config *config = data; |
| 572 | |
| 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); |
| 576 | |
| 577 | ZUC_ASSERT_EQ(-1, r); |
| 578 | ZUC_ASSERT_EQ(0xff336699, n); |
| 579 | ZUC_ASSERT_EQ(EINVAL, errno); |
| 580 | } |
| 581 | |
Bryce Harrington | d0716f4 | 2016-07-14 18:28:04 -0700 | [diff] [blame] | 582 | ZUC_TEST_F(config_test_t1, test027, data) |
| 583 | { |
| 584 | int r; |
| 585 | int32_t n; |
| 586 | struct weston_config_section *section; |
| 587 | struct weston_config *config = data; |
| 588 | |
| 589 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 590 | r = weston_config_section_get_int(section, "negative", &n, 600); |
| 591 | |
| 592 | ZUC_ASSERT_EQ(0, r); |
| 593 | ZUC_ASSERT_EQ(-42, n); |
| 594 | ZUC_ASSERT_EQ(0, errno); |
| 595 | } |
| 596 | |
| 597 | ZUC_TEST_F(config_test_t1, test028, data) |
| 598 | { |
| 599 | int r; |
| 600 | uint32_t n; |
| 601 | struct weston_config_section *section; |
| 602 | struct weston_config *config = data; |
| 603 | |
| 604 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 605 | r = weston_config_section_get_uint(section, "negative", &n, 600); |
| 606 | |
| 607 | ZUC_ASSERT_EQ(-1, r); |
| 608 | ZUC_ASSERT_EQ(600, n); |
| 609 | ZUC_ASSERT_EQ(ERANGE, errno); |
| 610 | } |
| 611 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 612 | ZUC_TEST_F(config_test_t2, doesnt_parse, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 613 | { |
| 614 | struct weston_config *config = data; |
| 615 | ZUC_ASSERT_NULL(config); |
| 616 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 617 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 618 | ZUC_TEST_F(config_test_t3, doesnt_parse, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 619 | { |
| 620 | struct weston_config *config = data; |
| 621 | ZUC_ASSERT_NULL(config); |
| 622 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 623 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 624 | ZUC_TEST_F(config_test_t4, doesnt_parse, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 625 | { |
| 626 | struct weston_config *config = data; |
| 627 | ZUC_ASSERT_NULL(config); |
| 628 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 629 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 630 | ZUC_TEST(config_test, destroy_null) |
| 631 | { |
Kristian Høgsberg | 82189f7 | 2013-05-28 15:34:46 -0400 | [diff] [blame] | 632 | weston_config_destroy(NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 633 | ZUC_ASSERT_EQ(0, weston_config_next_section(NULL, NULL, NULL)); |
| 634 | } |
Kristian Høgsberg | 82189f7 | 2013-05-28 15:34:46 -0400 | [diff] [blame] | 635 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 636 | ZUC_TEST(config_test, section_from_null) |
| 637 | { |
| 638 | struct weston_config_section *section; |
Kristian Høgsberg | 82189f7 | 2013-05-28 15:34:46 -0400 | [diff] [blame] | 639 | section = weston_config_get_section(NULL, "bucket", NULL, NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 640 | ZUC_ASSERT_NULL(section); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 641 | } |