4 * Copyright(c) 2017 Wind River Systems Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/queue.h>
38 #include <rte_cfgfile.h>
44 #define CFG_FILES_ETC "test_cfgfiles/etc"
46 REGISTER_LINKED_RESOURCE(test_cfgfiles);
49 test_cfgfile_setup(void)
51 const struct resource *r;
54 r = resource_find("test_cfgfiles");
55 TEST_ASSERT_NOT_NULL(r, "missing resource test_cfgfiles");
57 ret = resource_untar(r);
58 TEST_ASSERT_SUCCESS(ret, "failed to untar %s", r->name);
64 test_cfgfile_cleanup(void)
66 const struct resource *r;
69 r = resource_find("test_cfgfiles");
70 TEST_ASSERT_NOT_NULL(r, "missing resource test_cfgfiles");
72 ret = resource_rm_by_tar(r);
73 TEST_ASSERT_SUCCESS(ret, "Failed to delete resource %s", r->name);
79 _test_cfgfile_sample(struct rte_cfgfile *cfgfile)
84 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
85 TEST_ASSERT(ret == 2, "Unexpected number of sections: %d", ret);
87 ret = rte_cfgfile_has_section(cfgfile, "section1");
88 TEST_ASSERT(ret, "section1 section missing");
90 ret = rte_cfgfile_section_num_entries(cfgfile, "section1");
91 TEST_ASSERT(ret == 1, "section1 unexpected number of entries: %d", ret);
93 value = rte_cfgfile_get_entry(cfgfile, "section1", "key1");
94 TEST_ASSERT(strcmp("value1", value) == 0,
95 "key1 unexpected value: %s", value);
97 ret = rte_cfgfile_has_section(cfgfile, "section2");
98 TEST_ASSERT(ret, "section2 section missing");
100 ret = rte_cfgfile_section_num_entries(cfgfile, "section2");
101 TEST_ASSERT(ret == 2, "section2 unexpected number of entries: %d", ret);
103 value = rte_cfgfile_get_entry(cfgfile, "section2", "key2");
104 TEST_ASSERT(strcmp("value2", value) == 0,
105 "key2 unexpected value: %s", value);
107 value = rte_cfgfile_get_entry(cfgfile, "section2", "key3");
108 TEST_ASSERT(strcmp("value3", value) == 0,
109 "key3 unexpected value: %s", value);
115 test_cfgfile_sample1(void)
117 struct rte_cfgfile *cfgfile;
120 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/sample1.ini", 0);
121 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
123 ret = _test_cfgfile_sample(cfgfile);
124 TEST_ASSERT_SUCCESS(ret, "Failed to validate sample file: %d", ret);
126 ret = rte_cfgfile_close(cfgfile);
127 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
133 test_cfgfile_sample2(void)
135 struct rte_cfgfile_parameters params;
136 struct rte_cfgfile *cfgfile;
139 /* override comment character */
140 memset(¶ms, 0, sizeof(params));
141 params.comment_character = '#';
143 cfgfile = rte_cfgfile_load_with_params(CFG_FILES_ETC "/sample2.ini", 0,
145 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to parse sample2.ini");
147 ret = _test_cfgfile_sample(cfgfile);
148 TEST_ASSERT_SUCCESS(ret, "Failed to validate sample file: %d", ret);
150 ret = rte_cfgfile_close(cfgfile);
151 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
157 test_cfgfile_realloc_sections(void)
159 struct rte_cfgfile *cfgfile;
163 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/realloc_sections.ini", 0);
164 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
166 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
167 TEST_ASSERT(ret == 9, "Unexpected number of sections: %d", ret);
169 ret = rte_cfgfile_has_section(cfgfile, "section9");
170 TEST_ASSERT(ret, "section9 missing");
172 ret = rte_cfgfile_section_num_entries(cfgfile, "section3");
173 TEST_ASSERT(ret == 21,
174 "section3 unexpected number of entries: %d", ret);
176 ret = rte_cfgfile_section_num_entries(cfgfile, "section9");
177 TEST_ASSERT(ret == 8, "section9 unexpected number of entries: %d", ret);
179 value = rte_cfgfile_get_entry(cfgfile, "section9", "key8");
180 TEST_ASSERT(strcmp("value8_section9", value) == 0,
181 "key unexpected value: %s", value);
183 ret = rte_cfgfile_save(cfgfile, "/tmp/cfgfile_save.ini");
184 TEST_ASSERT_SUCCESS(ret, "Failed to save *.ini file");
185 remove("/tmp/cfgfile_save.ini");
187 ret = rte_cfgfile_close(cfgfile);
188 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
194 test_cfgfile_invalid_section_header(void)
196 struct rte_cfgfile *cfgfile;
198 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/invalid_section.ini", 0);
199 TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
205 test_cfgfile_invalid_comment(void)
207 struct rte_cfgfile_parameters params;
208 struct rte_cfgfile *cfgfile;
210 /* override comment character with an invalid one */
211 memset(¶ms, 0, sizeof(params));
212 params.comment_character = '$';
214 cfgfile = rte_cfgfile_load_with_params(CFG_FILES_ETC "/sample2.ini", 0,
216 TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
222 test_cfgfile_invalid_key_value_pair(void)
224 struct rte_cfgfile *cfgfile;
226 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty_key_value.ini", 0);
227 TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
233 test_cfgfile_empty_key_value_pair(void)
235 struct rte_cfgfile *cfgfile;
239 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty_key_value.ini",
240 CFG_FLAG_EMPTY_VALUES);
241 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to parse empty_key_value.ini");
243 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
244 TEST_ASSERT(ret == 1, "Unexpected number of sections: %d", ret);
246 ret = rte_cfgfile_has_section(cfgfile, "section1");
247 TEST_ASSERT(ret, "section1 missing");
249 ret = rte_cfgfile_section_num_entries(cfgfile, "section1");
250 TEST_ASSERT(ret == 1, "section1 unexpected number of entries: %d", ret);
252 value = rte_cfgfile_get_entry(cfgfile, "section1", "key");
253 TEST_ASSERT(strlen(value) == 0, "key unexpected value: %s", value);
255 ret = rte_cfgfile_close(cfgfile);
256 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
262 test_cfgfile_missing_section(void)
264 struct rte_cfgfile *cfgfile;
266 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/missing_section.ini", 0);
267 TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
273 test_cfgfile_global_properties(void)
275 struct rte_cfgfile *cfgfile;
279 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/missing_section.ini",
280 CFG_FLAG_GLOBAL_SECTION);
281 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
283 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
284 TEST_ASSERT(ret == 1, "Unexpected number of sections: %d", ret);
286 ret = rte_cfgfile_has_section(cfgfile, "GLOBAL");
287 TEST_ASSERT(ret, "global section missing");
289 ret = rte_cfgfile_section_num_entries(cfgfile, "GLOBAL");
290 TEST_ASSERT(ret == 1, "GLOBAL unexpected number of entries: %d", ret);
292 value = rte_cfgfile_get_entry(cfgfile, "GLOBAL", "key");
293 TEST_ASSERT(strcmp("value", value) == 0,
294 "key unexpected value: %s", value);
296 ret = rte_cfgfile_close(cfgfile);
297 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
303 test_cfgfile_empty_file(void)
305 struct rte_cfgfile *cfgfile;
308 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty.ini", 0);
309 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
311 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
312 TEST_ASSERT(ret == 0, "Unexpected number of sections: %d", ret);
314 ret = rte_cfgfile_close(cfgfile);
315 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
323 if (test_cfgfile_setup())
326 if (test_cfgfile_sample1())
329 if (test_cfgfile_sample2())
332 if (test_cfgfile_realloc_sections())
335 if (test_cfgfile_invalid_section_header())
338 if (test_cfgfile_invalid_comment())
341 if (test_cfgfile_invalid_key_value_pair())
344 if (test_cfgfile_empty_key_value_pair())
347 if (test_cfgfile_missing_section())
350 if (test_cfgfile_global_properties())
353 if (test_cfgfile_empty_file())
356 if (test_cfgfile_cleanup())
362 REGISTER_TEST_COMMAND(cfgfile_autotest, test_cfgfile);