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" |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 120 | "flag=false\n" |
| 121 | "\n" |
Bryce Harrington | e776f2a | 2016-07-14 18:28:03 -0700 | [diff] [blame^] | 122 | "[colors]\n" |
| 123 | "none=0x00000000\n" |
| 124 | "low=0x11223344\n" |
| 125 | "high=0xff00ff00\n" |
| 126 | "oct=01234567\n" |
| 127 | "dec=12345670\n" |
| 128 | "short=1234567\n" |
| 129 | "\n" |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 130 | "[stuff]\n" |
| 131 | "flag= true \n" |
| 132 | "\n" |
| 133 | "[bucket]\n" |
| 134 | "color=blue \n" |
| 135 | "contents=live crabs\n" |
| 136 | "pinchy=true\n" |
| 137 | "\n" |
| 138 | "[bucket]\n" |
| 139 | "material=plastic \n" |
| 140 | "color=red\n" |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 141 | "contents=sand\n", |
| 142 | .set_up = setup_test_config, |
| 143 | .tear_down = cleanup_test_config |
| 144 | }; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 145 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 146 | static const char *section_names[] = { |
Bryce Harrington | e776f2a | 2016-07-14 18:28:03 -0700 | [diff] [blame^] | 147 | "foo", "bar", "colors", "stuff", "bucket", "bucket" |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 148 | }; |
| 149 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 150 | /* |
| 151 | * Since these next few won't parse, we don't add the tear_down to |
| 152 | * attempt cleanup. |
| 153 | */ |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 154 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 155 | static struct zuc_fixture config_test_t2 = { |
| 156 | .data = |
| 157 | "# invalid section...\n" |
| 158 | "[this bracket isn't closed\n", |
| 159 | .set_up = setup_test_config_failing, |
| 160 | }; |
| 161 | |
| 162 | static struct zuc_fixture config_test_t3 = { |
| 163 | .data = |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 164 | "# line without = ...\n" |
| 165 | "[bambam]\n" |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 166 | "this line isn't any kind of valid\n", |
| 167 | .set_up = setup_test_config_failing, |
| 168 | }; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 169 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 170 | static struct zuc_fixture config_test_t4 = { |
| 171 | .data = |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 172 | "# starting with = ...\n" |
| 173 | "[bambam]\n" |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 174 | "=not valid at all\n", |
| 175 | .set_up = setup_test_config_failing, |
| 176 | }; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 177 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 178 | ZUC_TEST_F(config_test_t0, comment_only, data) |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 179 | { |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 180 | struct weston_config *config = data; |
| 181 | ZUC_ASSERT_NOT_NULL(config); |
| 182 | } |
| 183 | |
| 184 | /** @todo individual t1 tests should have more descriptive names. */ |
| 185 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 186 | ZUC_TEST_F(config_test_t1, test001, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 187 | { |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 188 | struct weston_config_section *section; |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 189 | struct weston_config *config = data; |
| 190 | ZUC_ASSERT_NOT_NULL(config); |
| 191 | section = weston_config_get_section(config, |
| 192 | "mollusc", NULL, NULL); |
| 193 | ZUC_ASSERT_NULL(section); |
| 194 | } |
| 195 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 196 | ZUC_TEST_F(config_test_t1, test002, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 197 | { |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 198 | char *s; |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 199 | int r; |
| 200 | struct weston_config_section *section; |
| 201 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 202 | |
| 203 | section = weston_config_get_section(config, "foo", NULL, NULL); |
| 204 | r = weston_config_section_get_string(section, "a", &s, NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 205 | |
| 206 | ZUC_ASSERTG_EQ(0, r, out_free); |
| 207 | ZUC_ASSERTG_STREQ("b", s, out_free); |
| 208 | |
| 209 | out_free: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 210 | free(s); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 213 | ZUC_TEST_F(config_test_t1, test003, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 214 | { |
| 215 | char *s; |
| 216 | int r; |
| 217 | struct weston_config_section *section; |
| 218 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 219 | |
| 220 | section = weston_config_get_section(config, "foo", NULL, NULL); |
| 221 | r = weston_config_section_get_string(section, "b", &s, NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 222 | |
| 223 | ZUC_ASSERT_EQ(-1, r); |
| 224 | ZUC_ASSERT_EQ(ENOENT, errno); |
| 225 | ZUC_ASSERT_NULL(s); |
| 226 | } |
| 227 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 228 | ZUC_TEST_F(config_test_t1, test004, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 229 | { |
| 230 | char *s; |
| 231 | int r; |
| 232 | struct weston_config_section *section; |
| 233 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 234 | |
| 235 | section = weston_config_get_section(config, "foo", NULL, NULL); |
| 236 | r = weston_config_section_get_string(section, "name", &s, NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 237 | |
| 238 | ZUC_ASSERTG_EQ(0, r, out_free); |
| 239 | ZUC_ASSERTG_STREQ("Roy Batty", s, out_free); |
| 240 | |
| 241 | out_free: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 242 | free(s); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 245 | ZUC_TEST_F(config_test_t1, test005, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 246 | { |
| 247 | char *s; |
| 248 | int r; |
| 249 | struct weston_config_section *section; |
| 250 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 251 | |
| 252 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 253 | r = weston_config_section_get_string(section, "a", &s, "boo"); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 254 | |
| 255 | ZUC_ASSERTG_EQ(-1, r, out_free); |
| 256 | ZUC_ASSERTG_EQ(ENOENT, errno, out_free); |
| 257 | ZUC_ASSERTG_STREQ("boo", s, out_free); |
| 258 | |
| 259 | out_free: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 260 | free(s); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 263 | ZUC_TEST_F(config_test_t1, test006, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 264 | { |
| 265 | int r; |
| 266 | int32_t n; |
| 267 | struct weston_config_section *section; |
| 268 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 269 | |
| 270 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 271 | r = weston_config_section_get_int(section, "number", &n, 600); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 272 | |
| 273 | ZUC_ASSERT_EQ(0, r); |
| 274 | ZUC_ASSERT_EQ(5252, n); |
Bryce Harrington | cbc0537 | 2016-07-07 14:08:28 -0700 | [diff] [blame] | 275 | ZUC_ASSERT_EQ(0, errno); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 278 | ZUC_TEST_F(config_test_t1, test007, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 279 | { |
| 280 | int r; |
| 281 | int32_t n; |
| 282 | struct weston_config_section *section; |
Derek Foreman | bdc8c72 | 2015-10-07 11:51:29 -0500 | [diff] [blame] | 283 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 284 | |
| 285 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 286 | r = weston_config_section_get_int(section, "+++", &n, 700); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 287 | |
| 288 | ZUC_ASSERT_EQ(-1, r); |
| 289 | ZUC_ASSERT_EQ(ENOENT, errno); |
| 290 | ZUC_ASSERT_EQ(700, n); |
| 291 | } |
| 292 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 293 | ZUC_TEST_F(config_test_t1, test008, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 294 | { |
| 295 | int r; |
| 296 | uint32_t u; |
| 297 | struct weston_config_section *section; |
| 298 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 299 | |
| 300 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 301 | r = weston_config_section_get_uint(section, "number", &u, 600); |
Bryce Harrington | cbc0537 | 2016-07-07 14:08:28 -0700 | [diff] [blame] | 302 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 303 | ZUC_ASSERT_EQ(0, r); |
| 304 | ZUC_ASSERT_EQ(5252, u); |
Bryce Harrington | cbc0537 | 2016-07-07 14:08:28 -0700 | [diff] [blame] | 305 | ZUC_ASSERT_EQ(0, errno); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 308 | ZUC_TEST_F(config_test_t1, test009, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 309 | { |
| 310 | int r; |
| 311 | uint32_t u; |
| 312 | struct weston_config_section *section; |
| 313 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 314 | |
| 315 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 316 | r = weston_config_section_get_uint(section, "+++", &u, 600); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 317 | ZUC_ASSERT_EQ(-1, r); |
| 318 | ZUC_ASSERT_EQ(ENOENT, errno); |
| 319 | ZUC_ASSERT_EQ(600, u); |
| 320 | } |
| 321 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 322 | ZUC_TEST_F(config_test_t1, test010, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 323 | { |
| 324 | int r, b; |
| 325 | struct weston_config_section *section; |
| 326 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 327 | |
| 328 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 329 | r = weston_config_section_get_bool(section, "flag", &b, 600); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 330 | ZUC_ASSERT_EQ(0, r); |
| 331 | ZUC_ASSERT_EQ(0, b); |
| 332 | } |
| 333 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 334 | ZUC_TEST_F(config_test_t1, test011, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 335 | { |
| 336 | int r, b; |
| 337 | struct weston_config_section *section; |
| 338 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 339 | |
| 340 | section = weston_config_get_section(config, "stuff", NULL, NULL); |
| 341 | r = weston_config_section_get_bool(section, "flag", &b, -1); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 342 | ZUC_ASSERT_EQ(0, r); |
| 343 | ZUC_ASSERT_EQ(1, b); |
| 344 | } |
| 345 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 346 | ZUC_TEST_F(config_test_t1, test012, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 347 | { |
| 348 | int r, b; |
| 349 | struct weston_config_section *section; |
| 350 | struct weston_config *config = data; |
| 351 | |
| 352 | section = weston_config_get_section(config, "stuff", NULL, NULL); |
| 353 | r = weston_config_section_get_bool(section, "flag", &b, -1); |
| 354 | ZUC_ASSERT_EQ(0, r); |
| 355 | ZUC_ASSERT_EQ(1, b); |
| 356 | } |
| 357 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 358 | ZUC_TEST_F(config_test_t1, test013, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 359 | { |
| 360 | int r, b; |
| 361 | struct weston_config_section *section; |
| 362 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 363 | |
| 364 | section = weston_config_get_section(config, "stuff", NULL, NULL); |
| 365 | r = weston_config_section_get_bool(section, "bonk", &b, -1); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 366 | ZUC_ASSERT_EQ(-1, r); |
| 367 | ZUC_ASSERT_EQ(ENOENT, errno); |
| 368 | ZUC_ASSERT_EQ(-1, b); |
| 369 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 370 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 371 | ZUC_TEST_F(config_test_t1, test014, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 372 | { |
| 373 | char *s; |
| 374 | int r; |
| 375 | struct weston_config_section *section; |
| 376 | struct weston_config *config = data; |
| 377 | |
| 378 | section = weston_config_get_section(config, |
| 379 | "bucket", "color", "blue"); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 380 | r = weston_config_section_get_string(section, "contents", &s, NULL); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 381 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 382 | ZUC_ASSERTG_EQ(0, r, out_free); |
| 383 | ZUC_ASSERTG_STREQ("live crabs", s, out_free); |
| 384 | |
| 385 | out_free: |
| 386 | free(s); |
| 387 | } |
| 388 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 389 | ZUC_TEST_F(config_test_t1, test015, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 390 | { |
| 391 | char *s; |
| 392 | int r; |
| 393 | struct weston_config_section *section; |
| 394 | struct weston_config *config = data; |
| 395 | |
| 396 | section = weston_config_get_section(config, |
| 397 | "bucket", "color", "red"); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 398 | r = weston_config_section_get_string(section, "contents", &s, NULL); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 399 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 400 | ZUC_ASSERTG_EQ(0, r, out_free); |
| 401 | ZUC_ASSERTG_STREQ("sand", s, out_free); |
| 402 | |
| 403 | out_free: |
| 404 | free(s); |
| 405 | } |
| 406 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 407 | ZUC_TEST_F(config_test_t1, test016, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 408 | { |
| 409 | char *s; |
| 410 | int r; |
| 411 | struct weston_config_section *section; |
| 412 | struct weston_config *config = data; |
| 413 | |
| 414 | section = weston_config_get_section(config, |
| 415 | "bucket", "color", "pink"); |
| 416 | ZUC_ASSERT_NULL(section); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 417 | r = weston_config_section_get_string(section, "contents", &s, "eels"); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 418 | |
| 419 | ZUC_ASSERTG_EQ(-1, r, out_free); |
| 420 | ZUC_ASSERTG_EQ(ENOENT, errno, out_free); |
| 421 | ZUC_ASSERTG_STREQ("eels", s, out_free); |
| 422 | |
| 423 | out_free: |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 424 | free(s); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 427 | ZUC_TEST_F(config_test_t1, test017, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 428 | { |
| 429 | const char *name; |
| 430 | int i; |
| 431 | struct weston_config_section *section; |
| 432 | struct weston_config *config = data; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 433 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 434 | section = NULL; |
| 435 | i = 0; |
| 436 | while (weston_config_next_section(config, §ion, &name)) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 437 | ZUC_ASSERT_STREQ(section_names[i++], name); |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 438 | |
Bryce Harrington | e776f2a | 2016-07-14 18:28:03 -0700 | [diff] [blame^] | 439 | ZUC_ASSERT_EQ(6, i); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 440 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 441 | |
Bryce Harrington | cbc0537 | 2016-07-07 14:08:28 -0700 | [diff] [blame] | 442 | ZUC_TEST_F(config_test_t1, test018, data) |
| 443 | { |
| 444 | int r; |
| 445 | int32_t n; |
| 446 | struct weston_config_section *section; |
| 447 | struct weston_config *config = data; |
| 448 | |
| 449 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 450 | r = weston_config_section_get_int(section, "zero", &n, 600); |
| 451 | |
| 452 | ZUC_ASSERT_EQ(0, r); |
| 453 | ZUC_ASSERT_EQ(0, n); |
| 454 | ZUC_ASSERT_EQ(0, errno); |
| 455 | } |
| 456 | |
| 457 | ZUC_TEST_F(config_test_t1, test019, data) |
| 458 | { |
| 459 | int r; |
| 460 | uint32_t n; |
| 461 | struct weston_config_section *section; |
| 462 | struct weston_config *config = data; |
| 463 | |
| 464 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 465 | r = weston_config_section_get_uint(section, "zero", &n, 600); |
| 466 | |
| 467 | ZUC_ASSERT_EQ(0, r); |
| 468 | ZUC_ASSERT_EQ(0, n); |
| 469 | ZUC_ASSERT_EQ(0, errno); |
| 470 | } |
| 471 | |
Bryce Harrington | e776f2a | 2016-07-14 18:28:03 -0700 | [diff] [blame^] | 472 | ZUC_TEST_F(config_test_t1, test020, data) |
| 473 | { |
| 474 | int r; |
| 475 | uint32_t n; |
| 476 | struct weston_config_section *section; |
| 477 | struct weston_config *config = data; |
| 478 | |
| 479 | section = weston_config_get_section(config, "colors", NULL, NULL); |
| 480 | r = weston_config_section_get_color(section, "none", &n, 0xff336699); |
| 481 | |
| 482 | ZUC_ASSERT_EQ(0, r); |
| 483 | ZUC_ASSERT_EQ(0x000000, n); |
| 484 | ZUC_ASSERT_EQ(0, errno); |
| 485 | } |
| 486 | |
| 487 | ZUC_TEST_F(config_test_t1, test021, data) |
| 488 | { |
| 489 | int r; |
| 490 | uint32_t n; |
| 491 | struct weston_config_section *section; |
| 492 | struct weston_config *config = data; |
| 493 | |
| 494 | section = weston_config_get_section(config, "colors", NULL, NULL); |
| 495 | r = weston_config_section_get_color(section, "low", &n, 0xff336699); |
| 496 | |
| 497 | ZUC_ASSERT_EQ(0, r); |
| 498 | ZUC_ASSERT_EQ(0x11223344, n); |
| 499 | ZUC_ASSERT_EQ(0, errno); |
| 500 | } |
| 501 | |
| 502 | ZUC_TEST_F(config_test_t1, test022, data) |
| 503 | { |
| 504 | int r; |
| 505 | uint32_t n; |
| 506 | struct weston_config_section *section; |
| 507 | struct weston_config *config = data; |
| 508 | |
| 509 | section = weston_config_get_section(config, "colors", NULL, NULL); |
| 510 | r = weston_config_section_get_color(section, "high", &n, 0xff336699); |
| 511 | |
| 512 | ZUC_ASSERT_EQ(0, r); |
| 513 | ZUC_ASSERT_EQ(0xff00ff00, n); |
| 514 | ZUC_ASSERT_EQ(0, errno); |
| 515 | } |
| 516 | |
| 517 | ZUC_TEST_F(config_test_t1, test023, data) |
| 518 | { |
| 519 | int r; |
| 520 | uint32_t n; |
| 521 | struct weston_config_section *section; |
| 522 | struct weston_config *config = data; |
| 523 | |
| 524 | // Treat colors as hex values even if missing the leading 0x |
| 525 | section = weston_config_get_section(config, "colors", NULL, NULL); |
| 526 | r = weston_config_section_get_color(section, "oct", &n, 0xff336699); |
| 527 | |
| 528 | ZUC_ASSERT_EQ(0, r); |
| 529 | ZUC_ASSERT_EQ(0x01234567, n); |
| 530 | ZUC_ASSERT_EQ(0, errno); |
| 531 | } |
| 532 | |
| 533 | ZUC_TEST_F(config_test_t1, test024, data) |
| 534 | { |
| 535 | int r; |
| 536 | uint32_t n; |
| 537 | struct weston_config_section *section; |
| 538 | struct weston_config *config = data; |
| 539 | |
| 540 | // Treat colors as hex values even if missing the leading 0x |
| 541 | section = weston_config_get_section(config, "colors", NULL, NULL); |
| 542 | r = weston_config_section_get_color(section, "dec", &n, 0xff336699); |
| 543 | |
| 544 | ZUC_ASSERT_EQ(0, r); |
| 545 | ZUC_ASSERT_EQ(0x12345670, n); |
| 546 | ZUC_ASSERT_EQ(0, errno); |
| 547 | } |
| 548 | |
| 549 | ZUC_TEST_F(config_test_t1, test025, data) |
| 550 | { |
| 551 | int r; |
| 552 | uint32_t n; |
| 553 | struct weston_config_section *section; |
| 554 | struct weston_config *config = data; |
| 555 | |
| 556 | // 7-digit colors are not valid (most likely typos) |
| 557 | section = weston_config_get_section(config, "colors", NULL, NULL); |
| 558 | r = weston_config_section_get_color(section, "short", &n, 0xff336699); |
| 559 | |
| 560 | ZUC_ASSERT_EQ(-1, r); |
| 561 | ZUC_ASSERT_EQ(0xff336699, n); |
| 562 | ZUC_ASSERT_EQ(EINVAL, errno); |
| 563 | } |
| 564 | |
| 565 | ZUC_TEST_F(config_test_t1, test026, data) |
| 566 | { |
| 567 | int r; |
| 568 | uint32_t n; |
| 569 | struct weston_config_section *section; |
| 570 | struct weston_config *config = data; |
| 571 | |
| 572 | // String color names are unsupported |
| 573 | section = weston_config_get_section(config, "bucket", NULL, NULL); |
| 574 | r = weston_config_section_get_color(section, "color", &n, 0xff336699); |
| 575 | |
| 576 | ZUC_ASSERT_EQ(-1, r); |
| 577 | ZUC_ASSERT_EQ(0xff336699, n); |
| 578 | ZUC_ASSERT_EQ(EINVAL, errno); |
| 579 | } |
| 580 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 581 | ZUC_TEST_F(config_test_t2, doesnt_parse, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 582 | { |
| 583 | struct weston_config *config = data; |
| 584 | ZUC_ASSERT_NULL(config); |
| 585 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 586 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 587 | ZUC_TEST_F(config_test_t3, doesnt_parse, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 588 | { |
| 589 | struct weston_config *config = data; |
| 590 | ZUC_ASSERT_NULL(config); |
| 591 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 592 | |
Jon Cruz | ecf819b | 2015-10-22 21:11:12 -0700 | [diff] [blame] | 593 | ZUC_TEST_F(config_test_t4, doesnt_parse, data) |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 594 | { |
| 595 | struct weston_config *config = data; |
| 596 | ZUC_ASSERT_NULL(config); |
| 597 | } |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 598 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 599 | ZUC_TEST(config_test, destroy_null) |
| 600 | { |
Kristian Høgsberg | 82189f7 | 2013-05-28 15:34:46 -0400 | [diff] [blame] | 601 | weston_config_destroy(NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 602 | ZUC_ASSERT_EQ(0, weston_config_next_section(NULL, NULL, NULL)); |
| 603 | } |
Kristian Høgsberg | 82189f7 | 2013-05-28 15:34:46 -0400 | [diff] [blame] | 604 | |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 605 | ZUC_TEST(config_test, section_from_null) |
| 606 | { |
| 607 | struct weston_config_section *section; |
Kristian Høgsberg | 82189f7 | 2013-05-28 15:34:46 -0400 | [diff] [blame] | 608 | section = weston_config_get_section(NULL, "bucket", NULL, NULL); |
Jon A. Cruz | a67c541 | 2015-07-15 19:22:42 -0700 | [diff] [blame] | 609 | ZUC_ASSERT_NULL(section); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 610 | } |