| 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 |
|
| 24 |
#include <errno.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <check.h> |
| 28 |
|
| 29 |
|
| 30 |
#define TRACE_HOME |
| 31 |
#include "trace.h" |
| 32 |
#include "ini_configobj.h" |
| 33 |
#include "ini_config_priv.h" |
| 34 |
|
| 35 |
static int write_to_file(char *path, char *text) |
| 36 |
{ |
| 37 |
FILE *f = fopen(path, "w"); |
| 38 |
int bytes = 0; |
| 39 |
if (f == NULL) |
| 40 |
return 1; |
| 41 |
|
| 42 |
bytes = fprintf(f, "%s", text); |
| 43 |
if (bytes != strlen(text)) { |
| 44 |
return 1; |
| 45 |
} |
| 46 |
|
| 47 |
return fclose(f); |
| 48 |
} |
| 49 |
|
| 50 |
static int exists_array(const char *needle, char **haystack, uint32_t count) |
| 51 |
{ |
| 52 |
uint32_t i = 0; |
| 53 |
|
| 54 |
for (i = 0; i < count; i++) { |
| 55 |
fprintf(stderr, "%s == %s?\n", needle, haystack[i]); |
| 56 |
if (strcmp(needle, haystack[i]) == 0) { |
| 57 |
return 1; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
START_TEST(test_ini_augment_merge_sections) |
| 65 |
{ |
| 66 |
char base_path[PATH_MAX]; |
| 67 |
char augment_path[PATH_MAX]; |
| 68 |
|
| 69 |
char config_base[] = |
| 70 |
"[section]\n" |
| 71 |
"key1 = first\n" |
| 72 |
"key2 = exists\n"; |
| 73 |
|
| 74 |
char config_augment[] = |
| 75 |
"[section]\n" |
| 76 |
"key1 = augment\n" |
| 77 |
"key3 = exists\n"; |
| 78 |
|
| 79 |
const char *builddir; |
| 80 |
|
| 81 |
uint32_t flags[3] = { INI_MS_DETECT , INI_MS_DETECT | INI_MS_PRESERVE, |
| 82 |
INI_MS_DETECT | INI_MS_OVERWRITE }; |
| 83 |
|
| 84 |
int expected_attributes_counts[3] = { 3, 2, 2 }; |
| 85 |
const char *test_sections[3] = { "section", "section", "section" }; |
| 86 |
const char *test_attributes[3] = { "key3", "key1", "key1" }; |
| 87 |
const char *test_attribute_values[3] = {"exists", "first", "augment" }; |
| 88 |
|
| 89 |
int ret; |
| 90 |
int iter; |
| 91 |
|
| 92 |
builddir = getenv("builddir"); |
| 93 |
if (builddir == NULL) { |
| 94 |
builddir = "."; |
| 95 |
} |
| 96 |
|
| 97 |
snprintf(base_path, PATH_MAX, "%s/tmp_augment_base.conf", builddir); |
| 98 |
snprintf(augment_path, PATH_MAX, "%s/tmp_augment_augment.conf", builddir); |
| 99 |
|
| 100 |
ret = write_to_file(base_path, config_base); |
| 101 |
fail_unless(ret == 0, "Failed to write %s: ret %d.\n", base_path, ret); |
| 102 |
|
| 103 |
write_to_file(augment_path, config_augment); |
| 104 |
fail_unless(ret == 0, "Failed to write %s: ret %d.\n", augment_path, ret); |
| 105 |
|
| 106 |
for (iter = 0; iter < 3; iter++) { |
| 107 |
uint32_t merge_flags = flags[iter]; |
| 108 |
int expected_attributes_count = expected_attributes_counts[iter]; |
| 109 |
const char *test_section = test_sections[iter]; |
| 110 |
const char *test_attribute = test_attributes[iter]; |
| 111 |
const char *test_attribute_value = test_attribute_values[iter]; |
| 112 |
struct ini_cfgobj *in_cfg; |
| 113 |
struct ini_cfgobj *result_cfg; |
| 114 |
struct ini_cfgfile *file_ctx; |
| 115 |
struct ref_array *error_list; |
| 116 |
struct ref_array *success_list; |
| 117 |
|
| 118 |
char **sections; |
| 119 |
int sections_count; |
| 120 |
|
| 121 |
char **attributes; |
| 122 |
int attributes_count; |
| 123 |
|
| 124 |
struct value_obj *val; |
| 125 |
char *val_str; |
| 126 |
|
| 127 |
|
| 128 |
const char *m_patterns[] = { "^tmp_augment_augment.conf$", NULL }; |
| 129 |
|
| 130 |
|
| 131 |
const char *m_sections[] = { ".*", NULL }; |
| 132 |
|
| 133 |
|
| 134 |
ret = ini_config_create(&in_cfg); |
| 135 |
fail_unless(ret == EOK, "Failed to create collection. Error %d\n", |
| 136 |
ret); |
| 137 |
|
| 138 |
|
| 139 |
ret = ini_config_file_open(base_path, 0, &file_ctx); |
| 140 |
fail_unless(ret == EOK, "Failed to open file. Error %d\n", ret); |
| 141 |
|
| 142 |
|
| 143 |
ret = ini_config_parse(file_ctx, 1, 0, 0, in_cfg); |
| 144 |
fail_unless(ret == EOK, "Failed to parse file context. Error %d\n", |
| 145 |
ret); |
| 146 |
|
| 147 |
|
| 148 |
ret = ini_config_augment(in_cfg, |
| 149 |
builddir, |
| 150 |
m_patterns, |
| 151 |
m_sections, |
| 152 |
NULL, |
| 153 |
INI_STOP_ON_NONE, |
| 154 |
0, |
| 155 |
INI_PARSE_NOSPACE|INI_PARSE_NOTAB, |
| 156 |
merge_flags, |
| 157 |
&result_cfg, |
| 158 |
&error_list, |
| 159 |
&success_list); |
| 160 |
|
| 161 |
fail_unless(ret == EEXIST, |
| 162 |
"Failed to augment context. Error %d\n", ret); |
| 163 |
|
| 164 |
if (result_cfg) { |
| 165 |
ini_config_destroy(in_cfg); |
| 166 |
in_cfg = result_cfg; |
| 167 |
result_cfg = NULL; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
sections = ini_get_section_list(in_cfg, §ions_count, &ret); |
| 172 |
fail_unless(ret == EOK, "Failed to get section list. Error %d\n", ret); |
| 173 |
|
| 174 |
|
| 175 |
ret = exists_array(test_section, sections, sections_count); |
| 176 |
fail_if(ret == 0, "Failed to find expected section.\n"); |
| 177 |
|
| 178 |
|
| 179 |
attributes = ini_get_attribute_list(in_cfg, test_section, |
| 180 |
&attributes_count, |
| 181 |
&ret); |
| 182 |
fail_unless(ret == EOK, "Failed to get attribute list. Error %d\n", |
| 183 |
ret); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
fail_unless(expected_attributes_count == attributes_count, |
| 188 |
"Expected %d attributes, but received %d.\n", |
| 189 |
expected_attributes_count, attributes_count); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
ret = exists_array(test_attribute, attributes, attributes_count); |
| 194 |
fail_if(ret == 0, "Failed to find expected attribute.\n"); |
| 195 |
|
| 196 |
ret = ini_get_config_valueobj(test_section, test_attribute, in_cfg, |
| 197 |
0, &val); |
| 198 |
fail_unless(ret == EOK, "Failed to load value object. Error %d\n", |
| 199 |
ret); |
| 200 |
|
| 201 |
val_str = ini_get_string_config_value(val, &ret); |
| 202 |
fail_unless(ret == EOK, "Failed to get config value. Error %d\n", ret); |
| 203 |
|
| 204 |
|
| 205 |
ret = strcmp(val_str, test_attribute_value); |
| 206 |
|
| 207 |
fail_unless(ret == 0, "Attribute %s didn't have expected value of " |
| 208 |
"(%s): saw %s\n", test_attribute, test_attribute_value, |
| 209 |
val_str); |
| 210 |
|
| 211 |
|
| 212 |
free(val_str); |
| 213 |
ini_free_attribute_list(attributes); |
| 214 |
ini_free_section_list(sections); |
| 215 |
ref_array_destroy(error_list); |
| 216 |
ini_config_file_destroy(file_ctx); |
| 217 |
ref_array_destroy(success_list); |
| 218 |
ini_config_destroy(in_cfg); |
| 219 |
ini_config_destroy(result_cfg); |
| 220 |
} |
| 221 |
|
| 222 |
remove(base_path); |
| 223 |
remove(augment_path); |
| 224 |
} |
| 225 |
END_TEST |
| 226 |
|
| 227 |
START_TEST(test_ini_augment_empty_dir) |
| 228 |
{ |
| 229 |
int ret; |
| 230 |
struct ini_cfgobj *ini_cfg; |
| 231 |
struct ini_cfgfile *file_ctx; |
| 232 |
struct value_obj *vo; |
| 233 |
const char *patterns[] = { ".*", NULL }; |
| 234 |
const char *sections[] = { ".*", NULL }; |
| 235 |
char **section_list; |
| 236 |
char **attrs_list; |
| 237 |
struct ini_cfgobj *result_cfg = NULL; |
| 238 |
int size; |
| 239 |
char empty_dir_path[PATH_MAX] = {0}; |
| 240 |
const char *builddir; |
| 241 |
int32_t val; |
| 242 |
char base_cfg[] = |
| 243 |
"[section_one]\n" |
| 244 |
"one = 1\n"; |
| 245 |
|
| 246 |
builddir = getenv("builddir"); |
| 247 |
if (builddir == NULL) { |
| 248 |
builddir = "."; |
| 249 |
} |
| 250 |
|
| 251 |
ret = snprintf(empty_dir_path, PATH_MAX, "%s/tmp_empty_dir", builddir); |
| 252 |
fail_if(ret > PATH_MAX || ret < 0, "snprintf failed\n"); |
| 253 |
|
| 254 |
ret = ini_config_file_from_mem(base_cfg, strlen(base_cfg), |
| 255 |
&file_ctx); |
| 256 |
fail_unless(ret == EOK, "Failed to load config. Error %d.\n", ret); |
| 257 |
|
| 258 |
ret = ini_config_create(&ini_cfg); |
| 259 |
fail_unless(ret == EOK, "Failed to create config. Error %d.\n", ret); |
| 260 |
ret = ini_config_parse(file_ctx, INI_STOP_ON_ERROR, INI_MV1S_ALLOW, 0, |
| 261 |
ini_cfg); |
| 262 |
fail_unless(ret == EOK, "Failed to parse configuration. Error %d.\n", ret); |
| 263 |
|
| 264 |
|
| 265 |
ret = mkdir(empty_dir_path, 0700); |
| 266 |
if (ret == -1) { |
| 267 |
ret = errno; |
| 268 |
fail_if(ret != EEXIST, |
| 269 |
"Failed to create empty directory. Error %d.\n", errno); |
| 270 |
} |
| 271 |
|
| 272 |
ret = ini_config_augment(ini_cfg, |
| 273 |
empty_dir_path, |
| 274 |
patterns, |
| 275 |
sections, |
| 276 |
NULL, |
| 277 |
INI_STOP_ON_ANY, |
| 278 |
INI_MV1S_OVERWRITE, |
| 279 |
INI_PARSE_NOWRAP, |
| 280 |
INI_MV2S_OVERWRITE, |
| 281 |
&result_cfg, |
| 282 |
NULL, |
| 283 |
NULL); |
| 284 |
|
| 285 |
fail_unless(ret == EOK); |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
fail_if(result_cfg == NULL); |
| 290 |
|
| 291 |
|
| 292 |
section_list = ini_get_section_list(result_cfg, &size, NULL); |
| 293 |
fail_unless(size == 1); |
| 294 |
fail_unless(strcmp(section_list[0], "section_one") == 0); |
| 295 |
|
| 296 |
attrs_list = ini_get_attribute_list(result_cfg, section_list[0], |
| 297 |
&size, NULL); |
| 298 |
fail_unless(size == 1); |
| 299 |
fail_unless(strcmp(attrs_list[0], "one") == 0); |
| 300 |
|
| 301 |
ret = ini_get_config_valueobj(section_list[0], |
| 302 |
attrs_list[0], |
| 303 |
result_cfg, |
| 304 |
INI_GET_FIRST_VALUE, |
| 305 |
&vo); |
| 306 |
fail_unless(ret == 0); |
| 307 |
|
| 308 |
val = ini_get_int32_config_value(vo, 1, 100, NULL); |
| 309 |
fail_unless(val == 1, "Expected attribute value not found.\n"); |
| 310 |
|
| 311 |
ini_free_attribute_list(attrs_list); |
| 312 |
ini_free_section_list(section_list); |
| 313 |
ini_config_destroy(result_cfg); |
| 314 |
ini_config_destroy(ini_cfg); |
| 315 |
ini_config_file_destroy(file_ctx); |
| 316 |
remove(empty_dir_path); |
| 317 |
} |
| 318 |
END_TEST |
| 319 |
|
| 320 |
static Suite *ini_augment_suite(void) |
| 321 |
{ |
| 322 |
Suite *s = suite_create("ini_augment_suite"); |
| 323 |
|
| 324 |
TCase *tc_augment = tcase_create("ini_augment"); |
| 325 |
tcase_add_test(tc_augment, test_ini_augment_merge_sections); |
| 326 |
tcase_add_test(tc_augment, test_ini_augment_empty_dir); |
| 327 |
|
| 328 |
suite_add_tcase(s, tc_augment); |
| 329 |
|
| 330 |
return s; |
| 331 |
} |
| 332 |
|
| 333 |
int main(void) |
| 334 |
{ |
| 335 |
int number_failed; |
| 336 |
|
| 337 |
Suite *s = ini_augment_suite(); |
| 338 |
SRunner *sr = srunner_create(s); |
| 339 |
srunner_run_all(sr, CK_ENV); |
| 340 |
number_failed = srunner_ntests_failed(sr); |
| 341 |
srunner_free(sr); |
| 342 |
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 343 |
} |