xref: /dpdk/app/test/test_cfgfile.c (revision 68a03efeed657e6e05f281479b33b51102797e15)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Wind River Systems, Inc.
3  */
4 
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdint.h>
8 #include <sys/queue.h>
9 
10 #include <rte_cfgfile.h>
11 
12 #include "test.h"
13 #include "resource.h"
14 
15 
16 #define CFG_FILES_ETC "test_cfgfiles/etc"
17 
18 REGISTER_LINKED_RESOURCE(test_cfgfiles);
19 
20 static int
21 test_cfgfile_setup(void)
22 {
23 	const struct resource *r;
24 	int ret;
25 
26 	r = resource_find("test_cfgfiles");
27 	TEST_ASSERT_NOT_NULL(r, "missing resource test_cfgfiles");
28 
29 	ret = resource_untar(r);
30 	TEST_ASSERT_SUCCESS(ret, "failed to untar %s", r->name);
31 
32 	return 0;
33 }
34 
35 static int
36 test_cfgfile_cleanup(void)
37 {
38 	const struct resource *r;
39 	int ret;
40 
41 	r = resource_find("test_cfgfiles");
42 	TEST_ASSERT_NOT_NULL(r, "missing resource test_cfgfiles");
43 
44 	ret = resource_rm_by_tar(r);
45 	TEST_ASSERT_SUCCESS(ret, "Failed to delete resource %s", r->name);
46 
47 	return 0;
48 }
49 
50 static int
51 _test_cfgfile_sample(struct rte_cfgfile *cfgfile)
52 {
53 	const char *value;
54 	int ret;
55 
56 	ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
57 	TEST_ASSERT(ret == 2, "Unexpected number of sections: %d", ret);
58 
59 	ret = rte_cfgfile_has_section(cfgfile, "section1");
60 	TEST_ASSERT(ret, "section1 section missing");
61 
62 	ret = rte_cfgfile_section_num_entries(cfgfile, "section1");
63 	TEST_ASSERT(ret == 1, "section1 unexpected number of entries: %d", ret);
64 
65 	value = rte_cfgfile_get_entry(cfgfile, "section1", "key1");
66 	TEST_ASSERT(strcmp("value1", value) == 0,
67 		    "key1 unexpected value: %s", value);
68 
69 	ret = rte_cfgfile_has_section(cfgfile, "section2");
70 	TEST_ASSERT(ret, "section2 section missing");
71 
72 	ret = rte_cfgfile_section_num_entries(cfgfile, "section2");
73 	TEST_ASSERT(ret == 2, "section2 unexpected number of entries: %d", ret);
74 
75 	value = rte_cfgfile_get_entry(cfgfile, "section2", "key2");
76 	TEST_ASSERT(strcmp("value2", value) == 0,
77 		    "key2 unexpected value: %s", value);
78 
79 	value = rte_cfgfile_get_entry(cfgfile, "section2", "key3");
80 	TEST_ASSERT(strcmp("value3", value) == 0,
81 		    "key3 unexpected value: %s", value);
82 
83 	return 0;
84 }
85 
86 static int
87 test_cfgfile_sample1(void)
88 {
89 	struct rte_cfgfile *cfgfile;
90 	int ret;
91 
92 	cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/sample1.ini", 0);
93 	TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
94 
95 	ret = _test_cfgfile_sample(cfgfile);
96 	TEST_ASSERT_SUCCESS(ret, "Failed to validate sample file: %d", ret);
97 
98 	ret = rte_cfgfile_close(cfgfile);
99 	TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
100 
101 	return 0;
102 }
103 
104 static int
105 test_cfgfile_sample2(void)
106 {
107 	struct rte_cfgfile_parameters params;
108 	struct rte_cfgfile *cfgfile;
109 	int ret;
110 
111 	/* override comment character */
112 	memset(&params, 0, sizeof(params));
113 	params.comment_character = '#';
114 
115 	cfgfile = rte_cfgfile_load_with_params(CFG_FILES_ETC "/sample2.ini", 0,
116 					       &params);
117 	TEST_ASSERT_NOT_NULL(cfgfile, "Failed to parse sample2.ini");
118 
119 	ret = _test_cfgfile_sample(cfgfile);
120 	TEST_ASSERT_SUCCESS(ret, "Failed to validate sample file: %d", ret);
121 
122 	ret = rte_cfgfile_close(cfgfile);
123 	TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
124 
125 	return 0;
126 }
127 
128 static int
129 test_cfgfile_realloc_sections(void)
130 {
131 	struct rte_cfgfile *cfgfile;
132 	int ret;
133 	const char *value;
134 
135 	cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/realloc_sections.ini", 0);
136 	TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
137 
138 	ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
139 	TEST_ASSERT(ret == 9, "Unexpected number of sections: %d", ret);
140 
141 	ret = rte_cfgfile_has_section(cfgfile, "section9");
142 	TEST_ASSERT(ret, "section9 missing");
143 
144 	ret = rte_cfgfile_section_num_entries(cfgfile, "section3");
145 	TEST_ASSERT(ret == 21,
146 			"section3 unexpected number of entries: %d", ret);
147 
148 	ret = rte_cfgfile_section_num_entries(cfgfile, "section9");
149 	TEST_ASSERT(ret == 8, "section9 unexpected number of entries: %d", ret);
150 
151 	value = rte_cfgfile_get_entry(cfgfile, "section9", "key8");
152 	TEST_ASSERT(strcmp("value8_section9", value) == 0,
153 		    "key unexpected value: %s", value);
154 
155 	ret = rte_cfgfile_save(cfgfile, "/tmp/cfgfile_save.ini");
156 	TEST_ASSERT_SUCCESS(ret, "Failed to save *.ini file");
157 	remove("/tmp/cfgfile_save.ini");
158 
159 	ret = rte_cfgfile_close(cfgfile);
160 	TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
161 
162 	return 0;
163 }
164 
165 static int
166 test_cfgfile_invalid_section_header(void)
167 {
168 	struct rte_cfgfile *cfgfile;
169 
170 	cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/invalid_section.ini", 0);
171 	TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
172 
173 	return 0;
174 }
175 
176 static int
177 test_cfgfile_invalid_comment(void)
178 {
179 	struct rte_cfgfile_parameters params;
180 	struct rte_cfgfile *cfgfile;
181 
182 	/* override comment character with an invalid one */
183 	memset(&params, 0, sizeof(params));
184 	params.comment_character = '$';
185 
186 	cfgfile = rte_cfgfile_load_with_params(CFG_FILES_ETC "/sample2.ini", 0,
187 					       &params);
188 	TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
189 
190 	return 0;
191 }
192 
193 static int
194 test_cfgfile_invalid_key_value_pair(void)
195 {
196 	struct rte_cfgfile *cfgfile;
197 
198 	cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty_key_value.ini", 0);
199 	TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
200 
201 	return 0;
202 }
203 
204 static int
205 test_cfgfile_empty_key_value_pair(void)
206 {
207 	struct rte_cfgfile *cfgfile;
208 	const char *value;
209 	int ret;
210 
211 	cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty_key_value.ini",
212 				   CFG_FLAG_EMPTY_VALUES);
213 	TEST_ASSERT_NOT_NULL(cfgfile, "Failed to parse empty_key_value.ini");
214 
215 	ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
216 	TEST_ASSERT(ret == 1, "Unexpected number of sections: %d", ret);
217 
218 	ret = rte_cfgfile_has_section(cfgfile, "section1");
219 	TEST_ASSERT(ret, "section1 missing");
220 
221 	ret = rte_cfgfile_section_num_entries(cfgfile, "section1");
222 	TEST_ASSERT(ret == 1, "section1 unexpected number of entries: %d", ret);
223 
224 	value = rte_cfgfile_get_entry(cfgfile, "section1", "key");
225 	TEST_ASSERT(strlen(value) == 0, "key unexpected value: %s", value);
226 
227 	ret = rte_cfgfile_close(cfgfile);
228 	TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
229 
230 	return 0;
231 }
232 
233 static int
234 test_cfgfile_missing_section(void)
235 {
236 	struct rte_cfgfile *cfgfile;
237 
238 	cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/missing_section.ini", 0);
239 	TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
240 
241 	return 0;
242 }
243 
244 static int
245 test_cfgfile_global_properties(void)
246 {
247 	struct rte_cfgfile *cfgfile;
248 	const char *value;
249 	int ret;
250 
251 	cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/missing_section.ini",
252 				   CFG_FLAG_GLOBAL_SECTION);
253 	TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
254 
255 	ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
256 	TEST_ASSERT(ret == 1, "Unexpected number of sections: %d", ret);
257 
258 	ret = rte_cfgfile_has_section(cfgfile, "GLOBAL");
259 	TEST_ASSERT(ret, "global section missing");
260 
261 	ret = rte_cfgfile_section_num_entries(cfgfile, "GLOBAL");
262 	TEST_ASSERT(ret == 1, "GLOBAL unexpected number of entries: %d", ret);
263 
264 	value = rte_cfgfile_get_entry(cfgfile, "GLOBAL", "key");
265 	TEST_ASSERT(strcmp("value", value) == 0,
266 		    "key unexpected value: %s", value);
267 
268 	ret = rte_cfgfile_close(cfgfile);
269 	TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
270 
271 	return 0;
272 }
273 
274 static int
275 test_cfgfile_empty_file(void)
276 {
277 	struct rte_cfgfile *cfgfile;
278 	int ret;
279 
280 	cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty.ini", 0);
281 	TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
282 
283 	ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
284 	TEST_ASSERT(ret == 0, "Unexpected number of sections: %d", ret);
285 
286 	ret = rte_cfgfile_close(cfgfile);
287 	TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
288 
289 	return 0;
290 }
291 
292 static int
293 test_cfgfile(void)
294 {
295 	if (test_cfgfile_setup())
296 		return -1;
297 
298 	if (test_cfgfile_sample1())
299 		return -1;
300 
301 	if (test_cfgfile_sample2())
302 		return -1;
303 
304 	if (test_cfgfile_realloc_sections())
305 		return -1;
306 
307 	if (test_cfgfile_invalid_section_header())
308 		return -1;
309 
310 	if (test_cfgfile_invalid_comment())
311 		return -1;
312 
313 	if (test_cfgfile_invalid_key_value_pair())
314 		return -1;
315 
316 	if (test_cfgfile_empty_key_value_pair())
317 		return -1;
318 
319 	if (test_cfgfile_missing_section())
320 		return -1;
321 
322 	if (test_cfgfile_global_properties())
323 		return -1;
324 
325 	if (test_cfgfile_empty_file())
326 		return -1;
327 
328 	if (test_cfgfile_cleanup())
329 		return -1;
330 
331 	return 0;
332 }
333 
334 REGISTER_TEST_COMMAND(cfgfile_autotest, test_cfgfile);
335