Debian Codegraph @

ini/ini_augment_ut.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Unit test for the comment object.
5
6 Copyright (C) Dmitri Pal <[email protected]> 2014
7
8 INI Library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 INI Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with INI Library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sys/wait.h>
28 /* #define TRACE_LEVEL 7 */
29 #define TRACE_HOME
30 #include "trace.h"
31 #include "ini_configobj.h"
32 #include "ini_config_priv.h"
33 #include "collection_tools.h"
34 #include "path_utils.h"
35
36 int verbose = 0;
37
38 #define INIOUT(foo) \
39 do { \
40 if (verbose) { printf("%30s(%4d): ",__FUNCTION__,__LINE__); foo; } \
41 } while(0)
42
43 typedef int (*test_fn)(void);
44
45 void print_list(struct ref_array *list);
46 int print_list_to_file(struct ref_array *list,
47 const char *filename,
48 const char *mode);
49 static int expand_path(const char *path, char **fullname);
50
51
52 /* Construct the full dir path */
53 static int expand_path(const char *path, char **fullname)
54 {
55 int error = EOK;
56 char *dirname = NULL;
57
58 TRACE_FLOW_ENTRY();
59 TRACE_INFO_STRING("Input path", path);
60
61 dirname = malloc(PATH_MAX + 1);
62 if (!dirname) {
63 INIOUT(printf("Failed to allocate memory for file path."));
64 return ENOMEM;
65 }
66
67 /* Make the path */
68 error = make_normalized_absolute_path(dirname,
69 PATH_MAX,
70 path);
71 if (error) {
72 INIOUT(printf("Failed to resolve path %d\n", error));
73 free(dirname);
74 return error;
75 }
76 else *fullname = dirname;
77
78 TRACE_INFO_STRING("Output path", *fullname);
79 TRACE_FLOW_EXIT();
80
81 return EOK;
82 }
83
84 static int prepare_results(const char *srcdir,
85 const char *srcfile,
86 const char *destfile)
87 {
88 int error = EOK;
89 char *exp_src= NULL;
90 FILE *fsrc = NULL;
91 FILE *fout = NULL;
92 char *line = NULL;
93 size_t len = 0;
94 ssize_t rd;
95
96 TRACE_FLOW_ENTRY();
97
98 error = expand_path(srcdir, &exp_src);
99 if (error) {
100 INIOUT(printf("Expand path returned error %d\n", error));
101 return error;
102 }
103
104 INIOUT(printf("Source file: %s\n", srcfile));
105 INIOUT(printf("Output file: %s\n", destfile));
106
107 fsrc = fopen(srcfile, "r");
108 if (!fsrc) {
109 error = errno;
110 free(exp_src);
111 INIOUT(printf("Failed to open source file %d\n", error));
112 return error;
113 }
114
115 fout = fopen(destfile, "w");
116 if (!fsrc) {
117 error = errno;
118 fclose(fsrc);
119 free(exp_src);
120 INIOUT(printf("Failed to open output file %d\n", error));
121 return error;
122 }
123
124 INIOUT(printf("Path %s\n", exp_src));
125
126 while ((rd = getline(&line, &len, fsrc)) != -1) {
127 if (strchr(line, '%')) fprintf(fout, line, exp_src, "/ini/ini.d");
128 else fprintf(fout, "%s", line);
129 }
130
131 if (line)
132 free(line);
133
134 fclose(fsrc);
135 fclose(fout);
136 free(exp_src);
137
138 TRACE_FLOW_EXIT();
139 return EOK;
140 }
141
142 /* Function to print contents of the list */
143 void print_list(struct ref_array *list)
144 {
145 uint32_t i = 0;
146 char *ret = NULL;
147 void *ptr = NULL;
148
149 for (;;) {
150 ptr = ref_array_get(list, i, &ret);
151 if (ptr) {
152 INIOUT(printf("%s\n", ret));
153 i++;
154 }
155 else break;
156 }
157 }
158
159 /* Function to print contents of the list */
160 int print_list_to_file(struct ref_array *list,
161 const char *filename,
162 const char *mode)
163 {
164 uint32_t i = 0;
165 char *ret = NULL;
166 void *ptr = NULL;
167 FILE *file = NULL;
168
169 file = fopen(filename, mode);
170 if (file) {
171 for (;;) {
172 ptr = ref_array_get(list, i, &ret);
173 if (ptr) {
174 fprintf(file,"%s\n", ret);
175 i++;
176 }
177 else break;
178 }
179 }
180 else {
181 printf("Failed to open file for results\n");
182 return -1;
183 }
184 fclose(file);
185 return 0;
186 }
187
188
189 /* Basic test */
190 static int basic_test(void)
191 {
192 int error = EOK;
193 char indir[PATH_MAX];
194 char srcname[PATH_MAX];
195 char filename[PATH_MAX];
196 char resname[PATH_MAX];
197 char command[PATH_MAX * 3];
198 char *builddir = NULL;
199 char *srcdir = NULL;
200 struct ini_cfgobj *in_cfg = NULL;
201 struct ini_cfgobj *result_cfg = NULL;
202 struct ref_array *error_list = NULL;
203 struct ref_array *success_list = NULL;
204 struct access_check ac = { INI_ACCESS_CHECK_MODE,
205 0,
206 0,
207 0444,
208 0444 };
209
210 /* Match all that do not start with 'r'
211 * and end with '.conf' and then match all
212 * ending with '.conf' */
213 const char *patterns[] = { "#",
214 "^[^r][a-z]*\\.conf$",
215 "^real\\.conf$",
216 NULL };
217
218 /* Match only the config, monitor, domains, services, and provider
219 * sections */
220 const char *sections[] = { "config",
221 "monitor",
222 "domains",
223 "services",
224 "provider",
225 NULL };
226
227
228 INIOUT(printf("<==== Start ====>\n"));
229
230 srcdir = getenv("srcdir");
231
232 builddir = getenv("builddir");
233
234 snprintf(indir, PATH_MAX, "%s/ini/ini.d",
235 (srcdir == NULL) ? "." : srcdir);
236
237
238 /* When run in dev environment there can be some temp files which
239 * we need to clean. */
240 snprintf(command, PATH_MAX * 3, "rm %s/*~ > /dev/null 2>&1", indir);
241 (void)system(command);
242
243 /* Make the file path independent */
244 snprintf(srcname, PATH_MAX, "%s/ini/ini.d/merge.validator",
245 (srcdir == NULL) ? "." : srcdir);
246
247 snprintf(filename, PATH_MAX, "%s/merge.validator.in",
248 (builddir == NULL) ? "." : builddir);
249
250
251 snprintf(resname, PATH_MAX, "%s/merge.validator.out",
252 (builddir == NULL) ? "." : builddir);
253
254 /* Prepare results file so that we can compare results */
255 error = prepare_results(srcdir, srcname, filename);
256 if (error) {
257 INIOUT(printf("Failed to results file. Error %d.\n", error));
258 return error;
259 }
260
261 /* Create config collection */
262 error = ini_config_create(&in_cfg);
263 if (error) {
264 INIOUT(printf("Failed to create collection. Error %d.\n", error));
265 return error;
266 }
267
268 error = ini_config_augment(in_cfg,
269 indir,
270 patterns,
271 sections,
272 &ac,
273 INI_STOP_ON_NONE,
274 INI_MV1S_DETECT|INI_MV2S_DETECT|INI_MS_DETECT,
275 INI_PARSE_NOSPACE|INI_PARSE_NOTAB,
276 INI_MV2S_DETECT|INI_MS_DETECT,
277 &result_cfg,
278 &error_list,
279 &success_list);
280 if (error) {
281 INIOUT(printf("Augmentation failed with error %d!\n", error));
282 }
283
284 print_list(error_list);
285 print_list(success_list);
286
287 if (!result_cfg) {
288 error = -1;
289 printf("Configuration is empty.\n");
290 }
291 else INIOUT(col_debug_collection(result_cfg->cfg, COL_TRAVERSE_DEFAULT));
292
293 /* Print results to file */
294 if ((print_list_to_file(error_list, resname, "w")) ||
295 (print_list_to_file(success_list, resname, "a"))) {
296 printf("Failed to save results in %s.\n", resname);
297 ref_array_destroy(error_list);
298 ref_array_destroy(success_list);
299 ini_config_destroy(in_cfg);
300 ini_config_destroy(result_cfg);
301 return -1;
302 }
303
304 /* NOTE: The order of the scanning of the files in the ini.d directory
305 * is not predicatble so before comparing the results we have to sort
306 * them since otherwise the projected output and real output might
307 * not match.
308 */
309
310 snprintf(command, PATH_MAX * 3, "sort %s > %s2", filename, filename);
311 error = system(command);
312 if ((error) || (WEXITSTATUS(error))) {
313 printf("Failed to run first sort command %d %d.\n", error,
314 WEXITSTATUS(error));
315 ref_array_destroy(error_list);
316 ref_array_destroy(success_list);
317 ini_config_destroy(in_cfg);
318 ini_config_destroy(result_cfg);
319 return -1;
320 }
321
322 snprintf(command, PATH_MAX * 3, "sort %s > %s2", resname, resname);
323 error = system(command);
324 error = system(command);
325 if ((error) || (WEXITSTATUS(error))) {
326 printf("Failed to run second sort command %d %d.\n", error,
327 WEXITSTATUS(error));
328 ref_array_destroy(error_list);
329 ref_array_destroy(success_list);
330 ini_config_destroy(in_cfg);
331 ini_config_destroy(result_cfg);
332 return -1;
333 }
334
335
336 snprintf(command, PATH_MAX * 3, "diff -q %s2 %s2", filename, resname);
337 error = system(command);
338 INIOUT(printf("Comparison of %s %s returned: %d\n",
339 filename, resname, error));
340
341 if ((error) || (WEXITSTATUS(error))) {
342 printf("Failed to run diff command %d %d.\n", error,
343 WEXITSTATUS(error));
344 ref_array_destroy(error_list);
345 ref_array_destroy(success_list);
346 ini_config_destroy(in_cfg);
347 ini_config_destroy(result_cfg);
348 return -1;
349 }
350
351 /* Cleanup */
352 ref_array_destroy(error_list);
353 ref_array_destroy(success_list);
354 ini_config_destroy(in_cfg);
355 ini_config_destroy(result_cfg);
356
357 INIOUT(printf("<==== End ====>\n"));
358
359 return error;
360 }
361
362
363 int main(int argc, char *argv[])
364 {
365 int error = EOK;
366 test_fn tests[] = { basic_test,
367 NULL };
368 test_fn t;
369 int i = 0;
370 char *var;
371
372 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
373 else {
374 var = getenv("COMMON_TEST_VERBOSE");
375 if (var) verbose = 1;
376 }
377
378 INIOUT(printf("Start\n"));
379
380 while ((t = tests[i++])) {
381 error = t();
382 if (error) {
383 printf("Failed with error %d!\n", error);
384 return error;
385 }
386 }
387
388 INIOUT(printf("Success!\n"));
389 return 0;
390 }