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> |
| 30 | #include <string.h> |
| 31 | #include <assert.h> |
| 32 | #include <errno.h> |
Kristian Høgsberg | ac3a8b8 | 2013-07-08 19:06:06 -0400 | [diff] [blame] | 33 | #include <unistd.h> |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 34 | |
| 35 | #include "config-parser.h" |
| 36 | |
| 37 | static struct weston_config * |
| 38 | run_test(const char *text) |
| 39 | { |
| 40 | struct weston_config *config; |
| 41 | char file[] = "/tmp/weston-config-parser-test-XXXXXX"; |
| 42 | int fd, len; |
| 43 | |
| 44 | fd = mkstemp(file); |
| 45 | len = write(fd, text, strlen(text)); |
Kristian Høgsberg | ac3a8b8 | 2013-07-08 19:06:06 -0400 | [diff] [blame] | 46 | assert(len == (int) strlen(text)); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 47 | |
Kristian Høgsberg | 1abe048 | 2013-09-21 23:02:31 -0700 | [diff] [blame] | 48 | config = weston_config_parse(file); |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 49 | close(fd); |
| 50 | unlink(file); |
| 51 | |
| 52 | return config; |
| 53 | } |
| 54 | |
| 55 | static const char t0[] = |
| 56 | "# nothing in this file...\n"; |
| 57 | |
| 58 | static const char t1[] = |
| 59 | "# comment line here...\n" |
| 60 | "\n" |
| 61 | "[foo]\n" |
| 62 | "a=b\n" |
| 63 | "name= Roy Batty \n" |
| 64 | "\n" |
| 65 | "\n" |
| 66 | "[bar]\n" |
| 67 | "# more comments\n" |
| 68 | "number=5252\n" |
| 69 | "flag=false\n" |
| 70 | "\n" |
| 71 | "[stuff]\n" |
| 72 | "flag= true \n" |
| 73 | "\n" |
| 74 | "[bucket]\n" |
| 75 | "color=blue \n" |
| 76 | "contents=live crabs\n" |
| 77 | "pinchy=true\n" |
| 78 | "\n" |
| 79 | "[bucket]\n" |
| 80 | "material=plastic \n" |
| 81 | "color=red\n" |
| 82 | "contents=sand\n"; |
| 83 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 84 | static const char *section_names[] = { |
| 85 | "foo", "bar", "stuff", "bucket", "bucket" |
| 86 | }; |
| 87 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 88 | static const char t2[] = |
| 89 | "# invalid section...\n" |
| 90 | "[this bracket isn't closed\n"; |
| 91 | |
| 92 | static const char t3[] = |
| 93 | "# line without = ...\n" |
| 94 | "[bambam]\n" |
| 95 | "this line isn't any kind of valid\n"; |
| 96 | |
| 97 | static const char t4[] = |
| 98 | "# starting with = ...\n" |
| 99 | "[bambam]\n" |
| 100 | "=not valid at all\n"; |
| 101 | |
| 102 | int main(int argc, char *argv[]) |
| 103 | { |
| 104 | struct weston_config *config; |
| 105 | struct weston_config_section *section; |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 106 | const char *name; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 107 | char *s; |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 108 | int r, b, i; |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 109 | int32_t n; |
| 110 | uint32_t u; |
| 111 | |
| 112 | config = run_test(t0); |
| 113 | assert(config); |
| 114 | weston_config_destroy(config); |
| 115 | |
| 116 | config = run_test(t1); |
| 117 | assert(config); |
| 118 | section = weston_config_get_section(config, "mollusc", NULL, NULL); |
| 119 | assert(section == NULL); |
| 120 | |
| 121 | section = weston_config_get_section(config, "foo", NULL, NULL); |
| 122 | r = weston_config_section_get_string(section, "a", &s, NULL); |
| 123 | assert(r == 0 && strcmp(s, "b") == 0); |
| 124 | free(s); |
| 125 | |
| 126 | section = weston_config_get_section(config, "foo", NULL, NULL); |
| 127 | r = weston_config_section_get_string(section, "b", &s, NULL); |
| 128 | assert(r == -1 && errno == ENOENT && s == NULL); |
| 129 | |
| 130 | section = weston_config_get_section(config, "foo", NULL, NULL); |
| 131 | r = weston_config_section_get_string(section, "name", &s, NULL); |
| 132 | assert(r == 0 && strcmp(s, "Roy Batty") == 0); |
| 133 | free(s); |
| 134 | |
| 135 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 136 | r = weston_config_section_get_string(section, "a", &s, "boo"); |
| 137 | assert(r == -1 && errno == ENOENT && strcmp(s, "boo") == 0); |
| 138 | free(s); |
| 139 | |
| 140 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 141 | r = weston_config_section_get_int(section, "number", &n, 600); |
| 142 | assert(r == 0 && n == 5252); |
| 143 | |
| 144 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 145 | r = weston_config_section_get_int(section, "+++", &n, 700); |
| 146 | assert(r == -1 && errno == ENOENT && n == 700); |
| 147 | |
| 148 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 149 | r = weston_config_section_get_uint(section, "number", &u, 600); |
| 150 | assert(r == 0 && u == 5252); |
| 151 | |
| 152 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 153 | r = weston_config_section_get_uint(section, "+++", &u, 600); |
| 154 | assert(r == -1 && errno == ENOENT && u == 600); |
| 155 | |
| 156 | section = weston_config_get_section(config, "bar", NULL, NULL); |
| 157 | r = weston_config_section_get_bool(section, "flag", &b, 600); |
| 158 | assert(r == 0 && b == 0); |
| 159 | |
| 160 | section = weston_config_get_section(config, "stuff", NULL, NULL); |
| 161 | r = weston_config_section_get_bool(section, "flag", &b, -1); |
| 162 | assert(r == 0 && b == 1); |
| 163 | |
| 164 | section = weston_config_get_section(config, "stuff", NULL, NULL); |
| 165 | r = weston_config_section_get_bool(section, "bonk", &b, -1); |
| 166 | assert(r == -1 && errno == ENOENT && b == -1); |
| 167 | |
| 168 | section = weston_config_get_section(config, "bucket", "color", "blue"); |
| 169 | r = weston_config_section_get_string(section, "contents", &s, NULL); |
| 170 | assert(r == 0 && strcmp(s, "live crabs") == 0); |
| 171 | free(s); |
| 172 | |
| 173 | section = weston_config_get_section(config, "bucket", "color", "red"); |
| 174 | r = weston_config_section_get_string(section, "contents", &s, NULL); |
| 175 | assert(r == 0 && strcmp(s, "sand") == 0); |
| 176 | free(s); |
| 177 | |
| 178 | section = weston_config_get_section(config, "bucket", "color", "pink"); |
| 179 | assert(section == NULL); |
| 180 | r = weston_config_section_get_string(section, "contents", &s, "eels"); |
| 181 | assert(r == -1 && errno == ENOENT && strcmp(s, "eels") == 0); |
| 182 | free(s); |
| 183 | |
Kristian Høgsberg | f73f316 | 2013-05-26 20:50:53 -0400 | [diff] [blame] | 184 | section = NULL; |
| 185 | i = 0; |
| 186 | while (weston_config_next_section(config, §ion, &name)) |
| 187 | assert(strcmp(section_names[i++], name) == 0); |
| 188 | assert(i == 5); |
| 189 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 190 | weston_config_destroy(config); |
| 191 | |
| 192 | config = run_test(t2); |
| 193 | assert(config == NULL); |
| 194 | |
| 195 | config = run_test(t3); |
| 196 | assert(config == NULL); |
| 197 | |
| 198 | config = run_test(t4); |
| 199 | assert(config == NULL); |
| 200 | |
Kristian Høgsberg | 82189f7 | 2013-05-28 15:34:46 -0400 | [diff] [blame] | 201 | weston_config_destroy(NULL); |
| 202 | assert(weston_config_next_section(NULL, NULL, NULL) == 0); |
| 203 | |
| 204 | section = weston_config_get_section(NULL, "bucket", NULL, NULL); |
| 205 | assert(section == NULL); |
| 206 | |
Kristian Høgsberg | 7327471 | 2013-04-01 12:41:23 -0400 | [diff] [blame] | 207 | return 0; |
| 208 | } |