summaryrefslogtreecommitdiff
path: root/tests/config-parser-test.c
blob: 4b8fc7e787d76c16a105d8e78093a6b432eafbd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>

#include "config-parser.h"

static struct weston_config *
run_test(const char *text)
{
	struct weston_config *config;
	char file[] = "/tmp/weston-config-parser-test-XXXXXX";
	int fd, len;

	fd = mkstemp(file);
	len = write(fd, text, strlen(text));
	assert(len == (int) strlen(text));

	config = weston_config_parse(file);
	close(fd);
	unlink(file);

	return config;
}

static const char t0[] =
	"# nothing in this file...\n";

static const char t1[] =
	"# comment line here...\n"
	"\n"
	"[foo]\n"
	"a=b\n"
	"name=  Roy Batty    \n"
	"\n"
	"\n"
	"[bar]\n"
	"# more comments\n"
	"number=5252\n"
	"flag=false\n"
	"\n"
	"[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"
	"contents=sand\n";

static const char *section_names[] = {
	"foo", "bar", "stuff", "bucket", "bucket"
};

static const char t2[] =
	"# invalid section...\n"
	"[this bracket isn't closed\n";

static const char t3[] =
	"# line without = ...\n"
	"[bambam]\n"
	"this line isn't any kind of valid\n";

static const char t4[] =
	"# starting with = ...\n"
	"[bambam]\n"
	"=not valid at all\n";

int main(int argc, char *argv[])
{
	struct weston_config *config;
	struct weston_config_section *section;
	const char *name;
	char *s;
	int r, b, i;
	int32_t n;
	uint32_t u;

	config = run_test(t0);
	assert(config);
	weston_config_destroy(config);

	config = run_test(t1);
	assert(config);
	section = weston_config_get_section(config, "mollusc", NULL, NULL);
	assert(section == NULL);

	section = weston_config_get_section(config, "foo", NULL, NULL);
	r = weston_config_section_get_string(section, "a", &s, NULL);
	assert(r == 0 && strcmp(s, "b") == 0);
	free(s);

	section = weston_config_get_section(config, "foo", NULL, NULL);
	r = weston_config_section_get_string(section, "b", &s, NULL);
	assert(r == -1 && errno == ENOENT && s == NULL);

	section = weston_config_get_section(config, "foo", NULL, NULL);
	r = weston_config_section_get_string(section, "name", &s, NULL);
	assert(r == 0 && strcmp(s, "Roy Batty") == 0);
	free(s);

	section = weston_config_get_section(config, "bar", NULL, NULL);
	r = weston_config_section_get_string(section, "a", &s, "boo");
	assert(r == -1 && errno == ENOENT && strcmp(s, "boo") == 0);
	free(s);

	section = weston_config_get_section(config, "bar", NULL, NULL);
	r = weston_config_section_get_int(section, "number", &n, 600);
	assert(r == 0 && n == 5252);

	section = weston_config_get_section(config, "bar", NULL, NULL);
	r = weston_config_section_get_int(section, "+++", &n, 700);
	assert(r == -1 && errno == ENOENT && n == 700);

	section = weston_config_get_section(config, "bar", NULL, NULL);
	r = weston_config_section_get_uint(section, "number", &u, 600);
	assert(r == 0 && u == 5252);

	section = weston_config_get_section(config, "bar", NULL, NULL);
	r = weston_config_section_get_uint(section, "+++", &u, 600);
	assert(r == -1 && errno == ENOENT && u == 600);

	section = weston_config_get_section(config, "bar", NULL, NULL);
	r = weston_config_section_get_bool(section, "flag", &b, 600);
	assert(r == 0 && b == 0);

	section = weston_config_get_section(config, "stuff", NULL, NULL);
	r = weston_config_section_get_bool(section, "flag", &b, -1);
	assert(r == 0 && b == 1);

	section = weston_config_get_section(config, "stuff", NULL, NULL);
	r = weston_config_section_get_bool(section, "bonk", &b, -1);
	assert(r == -1 && errno == ENOENT && b == -1);

	section = weston_config_get_section(config, "bucket", "color", "blue");
	r = weston_config_section_get_string(section, "contents", &s, NULL);
	assert(r == 0 && strcmp(s, "live crabs") == 0);
	free(s);

	section = weston_config_get_section(config, "bucket", "color", "red");
	r = weston_config_section_get_string(section, "contents", &s, NULL);
	assert(r == 0 && strcmp(s, "sand") == 0);
	free(s);

	section = weston_config_get_section(config, "bucket", "color", "pink");
	assert(section == NULL);
	r = weston_config_section_get_string(section, "contents", &s, "eels");
	assert(r == -1 && errno == ENOENT && strcmp(s, "eels") == 0);
	free(s);

	section = NULL;
	i = 0;
	while (weston_config_next_section(config, &section, &name))
		assert(strcmp(section_names[i++], name) == 0);
	assert(i == 5);

	weston_config_destroy(config);

	config = run_test(t2);
	assert(config == NULL);

	config = run_test(t3);
	assert(config == NULL);

	config = run_test(t4);
	assert(config == NULL);

	weston_config_destroy(NULL);
	assert(weston_config_next_section(NULL, NULL, NULL) == 0);

	section = weston_config_get_section(NULL, "bucket", NULL, NULL);
	assert(section == NULL);

	return 0;
}