| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 211 |
|
| 212 |
|
| 213 |
const char *patterns[] = { "#", |
| 214 |
"^[^r][a-z]*\\.conf$", |
| 215 |
"^real\\.conf$", |
| 216 |
NULL }; |
| 217 |
|
| 218 |
|
| 219 |
|
| 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 |
|
| 239 |
|
| 240 |
snprintf(command, PATH_MAX * 3, "rm %s/*~ > /dev/null 2>&1", indir); |
| 241 |
(void)system(command); |
| 242 |
|
| 243 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 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 |
|
| 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 |
} |