1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Wind River Systems, Inc.
10 #include <rte_cfgfile.h>
16 #define CFG_FILES_ETC "test_cfgfiles/etc"
18 REGISTER_LINKED_RESOURCE(test_cfgfiles);
21 test_cfgfile_setup(void)
23 const struct resource *r;
26 r = resource_find("test_cfgfiles");
27 TEST_ASSERT_NOT_NULL(r, "missing resource test_cfgfiles");
29 ret = resource_untar(r);
30 TEST_ASSERT_SUCCESS(ret, "failed to untar %s", r->name);
36 test_cfgfile_cleanup(void)
38 const struct resource *r;
41 r = resource_find("test_cfgfiles");
42 TEST_ASSERT_NOT_NULL(r, "missing resource test_cfgfiles");
44 ret = resource_rm_by_tar(r);
45 TEST_ASSERT_SUCCESS(ret, "Failed to delete resource %s", r->name);
51 _test_cfgfile_sample(struct rte_cfgfile *cfgfile)
56 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
57 TEST_ASSERT(ret == 2, "Unexpected number of sections: %d", ret);
59 ret = rte_cfgfile_has_section(cfgfile, "section1");
60 TEST_ASSERT(ret, "section1 section missing");
62 ret = rte_cfgfile_section_num_entries(cfgfile, "section1");
63 TEST_ASSERT(ret == 1, "section1 unexpected number of entries: %d", ret);
65 value = rte_cfgfile_get_entry(cfgfile, "section1", "key1");
66 TEST_ASSERT(strcmp("value1", value) == 0,
67 "key1 unexpected value: %s", value);
69 ret = rte_cfgfile_has_section(cfgfile, "section2");
70 TEST_ASSERT(ret, "section2 section missing");
72 ret = rte_cfgfile_section_num_entries(cfgfile, "section2");
73 TEST_ASSERT(ret == 2, "section2 unexpected number of entries: %d", ret);
75 value = rte_cfgfile_get_entry(cfgfile, "section2", "key2");
76 TEST_ASSERT(strcmp("value2", value) == 0,
77 "key2 unexpected value: %s", value);
79 value = rte_cfgfile_get_entry(cfgfile, "section2", "key3");
80 TEST_ASSERT(strcmp("value3", value) == 0,
81 "key3 unexpected value: %s", value);
87 test_cfgfile_sample1(void)
89 struct rte_cfgfile *cfgfile;
92 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/sample1.ini", 0);
93 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
95 ret = _test_cfgfile_sample(cfgfile);
96 TEST_ASSERT_SUCCESS(ret, "Failed to validate sample file: %d", ret);
98 ret = rte_cfgfile_close(cfgfile);
99 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
105 test_cfgfile_sample2(void)
107 struct rte_cfgfile_parameters params;
108 struct rte_cfgfile *cfgfile;
111 /* override comment character */
112 memset(¶ms, 0, sizeof(params));
113 params.comment_character = '#';
115 cfgfile = rte_cfgfile_load_with_params(CFG_FILES_ETC "/sample2.ini", 0,
117 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to parse sample2.ini");
119 ret = _test_cfgfile_sample(cfgfile);
120 TEST_ASSERT_SUCCESS(ret, "Failed to validate sample file: %d", ret);
122 ret = rte_cfgfile_close(cfgfile);
123 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
129 test_cfgfile_realloc_sections(void)
131 struct rte_cfgfile *cfgfile;
135 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/realloc_sections.ini", 0);
136 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
138 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
139 TEST_ASSERT(ret == 9, "Unexpected number of sections: %d", ret);
141 ret = rte_cfgfile_has_section(cfgfile, "section9");
142 TEST_ASSERT(ret, "section9 missing");
144 ret = rte_cfgfile_section_num_entries(cfgfile, "section3");
145 TEST_ASSERT(ret == 21,
146 "section3 unexpected number of entries: %d", ret);
148 ret = rte_cfgfile_section_num_entries(cfgfile, "section9");
149 TEST_ASSERT(ret == 8, "section9 unexpected number of entries: %d", ret);
151 value = rte_cfgfile_get_entry(cfgfile, "section9", "key8");
152 TEST_ASSERT(strcmp("value8_section9", value) == 0,
153 "key unexpected value: %s", value);
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");
159 ret = rte_cfgfile_close(cfgfile);
160 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
166 test_cfgfile_invalid_section_header(void)
168 struct rte_cfgfile *cfgfile;
170 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/invalid_section.ini", 0);
171 TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
177 test_cfgfile_invalid_comment(void)
179 struct rte_cfgfile_parameters params;
180 struct rte_cfgfile *cfgfile;
182 /* override comment character with an invalid one */
183 memset(¶ms, 0, sizeof(params));
184 params.comment_character = '$';
186 cfgfile = rte_cfgfile_load_with_params(CFG_FILES_ETC "/sample2.ini", 0,
188 TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
194 test_cfgfile_invalid_key_value_pair(void)
196 struct rte_cfgfile *cfgfile;
198 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty_key_value.ini", 0);
199 TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
205 test_cfgfile_empty_key_value_pair(void)
207 struct rte_cfgfile *cfgfile;
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");
215 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
216 TEST_ASSERT(ret == 1, "Unexpected number of sections: %d", ret);
218 ret = rte_cfgfile_has_section(cfgfile, "section1");
219 TEST_ASSERT(ret, "section1 missing");
221 ret = rte_cfgfile_section_num_entries(cfgfile, "section1");
222 TEST_ASSERT(ret == 1, "section1 unexpected number of entries: %d", ret);
224 value = rte_cfgfile_get_entry(cfgfile, "section1", "key");
225 TEST_ASSERT(strlen(value) == 0, "key unexpected value: %s", value);
227 ret = rte_cfgfile_close(cfgfile);
228 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
234 test_cfgfile_missing_section(void)
236 struct rte_cfgfile *cfgfile;
238 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/missing_section.ini", 0);
239 TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
245 test_cfgfile_global_properties(void)
247 struct rte_cfgfile *cfgfile;
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");
255 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
256 TEST_ASSERT(ret == 1, "Unexpected number of sections: %d", ret);
258 ret = rte_cfgfile_has_section(cfgfile, "GLOBAL");
259 TEST_ASSERT(ret, "global section missing");
261 ret = rte_cfgfile_section_num_entries(cfgfile, "GLOBAL");
262 TEST_ASSERT(ret == 1, "GLOBAL unexpected number of entries: %d", ret);
264 value = rte_cfgfile_get_entry(cfgfile, "GLOBAL", "key");
265 TEST_ASSERT(strcmp("value", value) == 0,
266 "key unexpected value: %s", value);
268 ret = rte_cfgfile_close(cfgfile);
269 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
275 test_cfgfile_empty_file(void)
277 struct rte_cfgfile *cfgfile;
280 cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty.ini", 0);
281 TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
283 ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
284 TEST_ASSERT(ret == 0, "Unexpected number of sections: %d", ret);
286 ret = rte_cfgfile_close(cfgfile);
287 TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
295 if (test_cfgfile_setup())
298 if (test_cfgfile_sample1())
301 if (test_cfgfile_sample2())
304 if (test_cfgfile_realloc_sections())
307 if (test_cfgfile_invalid_section_header())
310 if (test_cfgfile_invalid_comment())
313 if (test_cfgfile_invalid_key_value_pair())
316 if (test_cfgfile_empty_key_value_pair())
319 if (test_cfgfile_missing_section())
322 if (test_cfgfile_global_properties())
325 if (test_cfgfile_empty_file())
328 if (test_cfgfile_cleanup())
334 REGISTER_TEST_COMMAND(cfgfile_autotest, test_cfgfile);