cfgfile: support configurable comment character
[dpdk.git] / test / test / test_cfgfile.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Wind River Systems Inc. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <sys/queue.h>
37
38 #include <rte_cfgfile.h>
39
40 #include "test.h"
41 #include "resource.h"
42
43
44 #define CFG_FILES_ETC "test_cfgfiles/etc"
45
46 REGISTER_LINKED_RESOURCE(test_cfgfiles);
47
48 static int
49 test_cfgfile_setup(void)
50 {
51         const struct resource *r;
52         int ret;
53
54         r = resource_find("test_cfgfiles");
55         TEST_ASSERT_NOT_NULL(r, "missing resource test_cfgfiles");
56
57         ret = resource_untar(r);
58         TEST_ASSERT_SUCCESS(ret, "failed to untar %s", r->name);
59
60         return 0;
61 }
62
63 static int
64 test_cfgfile_cleanup(void)
65 {
66         const struct resource *r;
67         int ret;
68
69         r = resource_find("test_cfgfiles");
70         TEST_ASSERT_NOT_NULL(r, "missing resource test_cfgfiles");
71
72         ret = resource_rm_by_tar(r);
73         TEST_ASSERT_SUCCESS(ret, "Failed to delete resource %s", r->name);
74
75         return 0;
76 }
77
78 static int
79 _test_cfgfile_sample(struct rte_cfgfile *cfgfile)
80 {
81         const char *value;
82         int ret;
83
84         ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
85         TEST_ASSERT(ret == 2, "Unexpected number of sections: %d", ret);
86
87         ret = rte_cfgfile_has_section(cfgfile, "section1");
88         TEST_ASSERT(ret, "section1 section missing");
89
90         ret = rte_cfgfile_section_num_entries(cfgfile, "section1");
91         TEST_ASSERT(ret == 1, "section1 unexpected number of entries: %d", ret);
92
93         value = rte_cfgfile_get_entry(cfgfile, "section1", "key1");
94         TEST_ASSERT(strcmp("value1", value) == 0,
95                     "key1 unexpected value: %s", value);
96
97         ret = rte_cfgfile_has_section(cfgfile, "section2");
98         TEST_ASSERT(ret, "section2 section missing");
99
100         ret = rte_cfgfile_section_num_entries(cfgfile, "section2");
101         TEST_ASSERT(ret == 2, "section2 unexpected number of entries: %d", ret);
102
103         value = rte_cfgfile_get_entry(cfgfile, "section2", "key2");
104         TEST_ASSERT(strcmp("value2", value) == 0,
105                     "key2 unexpected value: %s", value);
106
107         value = rte_cfgfile_get_entry(cfgfile, "section2", "key3");
108         TEST_ASSERT(strcmp("value3", value) == 0,
109                     "key3 unexpected value: %s", value);
110
111         return 0;
112 }
113
114 static int
115 test_cfgfile_sample1(void)
116 {
117         struct rte_cfgfile *cfgfile;
118         int ret;
119
120         cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/sample1.ini", 0);
121         TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
122
123         ret = _test_cfgfile_sample(cfgfile);
124         TEST_ASSERT_SUCCESS(ret, "Failed to validate sample file: %d", ret);
125
126         ret = rte_cfgfile_close(cfgfile);
127         TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
128
129         return 0;
130 }
131
132 static int
133 test_cfgfile_sample2(void)
134 {
135         struct rte_cfgfile_parameters params;
136         struct rte_cfgfile *cfgfile;
137         int ret;
138
139         /* override comment character */
140         memset(&params, 0, sizeof(params));
141         params.comment_character = '#';
142
143         cfgfile = rte_cfgfile_load_with_params(CFG_FILES_ETC "/sample2.ini", 0,
144                                                &params);
145         TEST_ASSERT_NOT_NULL(cfgfile, "Failed to parse sample2.ini");
146
147         ret = _test_cfgfile_sample(cfgfile);
148         TEST_ASSERT_SUCCESS(ret, "Failed to validate sample file: %d", ret);
149
150         ret = rte_cfgfile_close(cfgfile);
151         TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
152
153         return 0;
154 }
155
156 static int
157 test_cfgfile_invalid_section_header(void)
158 {
159         struct rte_cfgfile *cfgfile;
160
161         cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/invalid_section.ini", 0);
162         TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
163
164         return 0;
165 }
166
167 static int
168 test_cfgfile_invalid_comment(void)
169 {
170         struct rte_cfgfile_parameters params;
171         struct rte_cfgfile *cfgfile;
172
173         /* override comment character with an invalid one */
174         memset(&params, 0, sizeof(params));
175         params.comment_character = '$';
176
177         cfgfile = rte_cfgfile_load_with_params(CFG_FILES_ETC "/sample2.ini", 0,
178                                                &params);
179         TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
180
181         return 0;
182 }
183
184 static int
185 test_cfgfile_invalid_key_value_pair(void)
186 {
187         struct rte_cfgfile *cfgfile;
188
189         cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty_key_value.ini", 0);
190         TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
191
192         return 0;
193 }
194
195 static int
196 test_cfgfile_missing_section(void)
197 {
198         struct rte_cfgfile *cfgfile;
199
200         cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/missing_section.ini", 0);
201         TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
202
203         return 0;
204 }
205
206 static int
207 test_cfgfile_global_properties(void)
208 {
209         struct rte_cfgfile *cfgfile;
210         const char *value;
211         int ret;
212
213         cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/missing_section.ini",
214                                    CFG_FLAG_GLOBAL_SECTION);
215         TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
216
217         ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
218         TEST_ASSERT(ret == 1, "Unexpected number of sections: %d", ret);
219
220         ret = rte_cfgfile_has_section(cfgfile, "GLOBAL");
221         TEST_ASSERT(ret, "global section missing");
222
223         ret = rte_cfgfile_section_num_entries(cfgfile, "GLOBAL");
224         TEST_ASSERT(ret == 1, "GLOBAL unexpected number of entries: %d", ret);
225
226         value = rte_cfgfile_get_entry(cfgfile, "GLOBAL", "key");
227         TEST_ASSERT(strcmp("value", value) == 0,
228                     "key unexpected value: %s", value);
229
230         ret = rte_cfgfile_close(cfgfile);
231         TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
232
233         return 0;
234 }
235
236 static int
237 test_cfgfile_empty_file(void)
238 {
239         struct rte_cfgfile *cfgfile;
240         int ret;
241
242         cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/empty.ini", 0);
243         TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file");
244
245         ret = rte_cfgfile_num_sections(cfgfile, NULL, 0);
246         TEST_ASSERT(ret == 0, "Unexpected number of sections: %d", ret);
247
248         ret = rte_cfgfile_close(cfgfile);
249         TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile");
250
251         return 0;
252 }
253
254 static int
255 test_cfgfile(void)
256 {
257         if (test_cfgfile_setup())
258                 return -1;
259
260         if (test_cfgfile_sample1())
261                 return -1;
262
263         if (test_cfgfile_sample2())
264                 return -1;
265
266         if (test_cfgfile_invalid_section_header())
267                 return -1;
268
269         if (test_cfgfile_invalid_comment())
270                 return -1;
271
272         if (test_cfgfile_invalid_key_value_pair())
273                 return -1;
274
275         if (test_cfgfile_missing_section())
276                 return -1;
277
278         if (test_cfgfile_global_properties())
279                 return -1;
280
281         if (test_cfgfile_empty_file())
282                 return -1;
283
284         if (test_cfgfile_cleanup())
285                 return -1;
286
287         return 0;
288 }
289
290 REGISTER_TEST_COMMAND(cfgfile_autotest, test_cfgfile);