blob: 11eb9aad2592f21e57b9c82312d8fc64e9ce8212 [file] [log] [blame]
Kristian Høgsberg73274712013-04-01 12:41:23 -04001/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdlib.h>
24#include <stdint.h>
25#include <string.h>
26#include <assert.h>
27#include <errno.h>
28
29#include "config-parser.h"
30
31static struct weston_config *
32run_test(const char *text)
33{
34 struct weston_config *config;
35 char file[] = "/tmp/weston-config-parser-test-XXXXXX";
36 int fd, len;
37
38 fd = mkstemp(file);
39 len = write(fd, text, strlen(text));
40 assert(len == strlen(text));
41
42 config = weston_config_parse(fd);
43 close(fd);
44 unlink(file);
45
46 return config;
47}
48
49static const char t0[] =
50 "# nothing in this file...\n";
51
52static const char t1[] =
53 "# comment line here...\n"
54 "\n"
55 "[foo]\n"
56 "a=b\n"
57 "name= Roy Batty \n"
58 "\n"
59 "\n"
60 "[bar]\n"
61 "# more comments\n"
62 "number=5252\n"
63 "flag=false\n"
64 "\n"
65 "[stuff]\n"
66 "flag= true \n"
67 "\n"
68 "[bucket]\n"
69 "color=blue \n"
70 "contents=live crabs\n"
71 "pinchy=true\n"
72 "\n"
73 "[bucket]\n"
74 "material=plastic \n"
75 "color=red\n"
76 "contents=sand\n";
77
78static const char t2[] =
79 "# invalid section...\n"
80 "[this bracket isn't closed\n";
81
82static const char t3[] =
83 "# line without = ...\n"
84 "[bambam]\n"
85 "this line isn't any kind of valid\n";
86
87static const char t4[] =
88 "# starting with = ...\n"
89 "[bambam]\n"
90 "=not valid at all\n";
91
92int main(int argc, char *argv[])
93{
94 struct weston_config *config;
95 struct weston_config_section *section;
96 char *s;
97 int r, b;
98 int32_t n;
99 uint32_t u;
100
101 config = run_test(t0);
102 assert(config);
103 weston_config_destroy(config);
104
105 config = run_test(t1);
106 assert(config);
107 section = weston_config_get_section(config, "mollusc", NULL, NULL);
108 assert(section == NULL);
109
110 section = weston_config_get_section(config, "foo", NULL, NULL);
111 r = weston_config_section_get_string(section, "a", &s, NULL);
112 assert(r == 0 && strcmp(s, "b") == 0);
113 free(s);
114
115 section = weston_config_get_section(config, "foo", NULL, NULL);
116 r = weston_config_section_get_string(section, "b", &s, NULL);
117 assert(r == -1 && errno == ENOENT && s == NULL);
118
119 section = weston_config_get_section(config, "foo", NULL, NULL);
120 r = weston_config_section_get_string(section, "name", &s, NULL);
121 assert(r == 0 && strcmp(s, "Roy Batty") == 0);
122 free(s);
123
124 section = weston_config_get_section(config, "bar", NULL, NULL);
125 r = weston_config_section_get_string(section, "a", &s, "boo");
126 assert(r == -1 && errno == ENOENT && strcmp(s, "boo") == 0);
127 free(s);
128
129 section = weston_config_get_section(config, "bar", NULL, NULL);
130 r = weston_config_section_get_int(section, "number", &n, 600);
131 assert(r == 0 && n == 5252);
132
133 section = weston_config_get_section(config, "bar", NULL, NULL);
134 r = weston_config_section_get_int(section, "+++", &n, 700);
135 assert(r == -1 && errno == ENOENT && n == 700);
136
137 section = weston_config_get_section(config, "bar", NULL, NULL);
138 r = weston_config_section_get_uint(section, "number", &u, 600);
139 assert(r == 0 && u == 5252);
140
141 section = weston_config_get_section(config, "bar", NULL, NULL);
142 r = weston_config_section_get_uint(section, "+++", &u, 600);
143 assert(r == -1 && errno == ENOENT && u == 600);
144
145 section = weston_config_get_section(config, "bar", NULL, NULL);
146 r = weston_config_section_get_bool(section, "flag", &b, 600);
147 assert(r == 0 && b == 0);
148
149 section = weston_config_get_section(config, "stuff", NULL, NULL);
150 r = weston_config_section_get_bool(section, "flag", &b, -1);
151 assert(r == 0 && b == 1);
152
153 section = weston_config_get_section(config, "stuff", NULL, NULL);
154 r = weston_config_section_get_bool(section, "bonk", &b, -1);
155 assert(r == -1 && errno == ENOENT && b == -1);
156
157 section = weston_config_get_section(config, "bucket", "color", "blue");
158 r = weston_config_section_get_string(section, "contents", &s, NULL);
159 assert(r == 0 && strcmp(s, "live crabs") == 0);
160 free(s);
161
162 section = weston_config_get_section(config, "bucket", "color", "red");
163 r = weston_config_section_get_string(section, "contents", &s, NULL);
164 assert(r == 0 && strcmp(s, "sand") == 0);
165 free(s);
166
167 section = weston_config_get_section(config, "bucket", "color", "pink");
168 assert(section == NULL);
169 r = weston_config_section_get_string(section, "contents", &s, "eels");
170 assert(r == -1 && errno == ENOENT && strcmp(s, "eels") == 0);
171 free(s);
172
173 weston_config_destroy(config);
174
175 config = run_test(t2);
176 assert(config == NULL);
177
178 config = run_test(t3);
179 assert(config == NULL);
180
181 config = run_test(t4);
182 assert(config == NULL);
183
184 return 0;
185}