1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
INI LIBRARY
Check based unit test for ini_config_augment.
Copyright (C) Alexander Scheel <ascheel@redhat.com> 2017
INI Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
INI Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with INI Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <check.h>
/* #define TRACE_LEVEL 7 */
#define TRACE_HOME
#include "trace.h"
#include "ini_configobj.h"
#include "ini_config_priv.h"
static int write_to_file(char *path, char *text)
{
FILE *f = fopen(path, "w");
int bytes = 0;
if (f == NULL)
return 1;
bytes = fprintf(f, "%s", text);
if (bytes != strlen(text)) {
return 1;
}
return fclose(f);
}
static int exists_array(const char *needle, char **haystack, uint32_t count)
{
uint32_t i = 0;
for (i = 0; i < count; i++) {
fprintf(stderr, "%s == %s?\n", needle, haystack[i]);
if (strcmp(needle, haystack[i]) == 0) {
return 1;
}
}
return 0;
}
START_TEST(test_ini_augment_merge_sections)
{
char base_path[PATH_MAX];
char augment_path[PATH_MAX];
char config_base[] =
"[section]\n"
"key1 = first\n"
"key2 = exists\n";
char config_augment[] =
"[section]\n"
"key1 = augment\n"
"key3 = exists\n";
const char *builddir;
uint32_t flags[3] = { INI_MS_DETECT , INI_MS_DETECT | INI_MS_PRESERVE,
INI_MS_DETECT | INI_MS_OVERWRITE };
int expected_attributes_counts[3] = { 3, 2, 2 };
const char *test_sections[3] = { "section", "section", "section" };
const char *test_attributes[3] = { "key3", "key1", "key1" };
const char *test_attribute_values[3] = {"exists", "first", "augment" };
int ret;
int iter;
builddir = getenv("builddir");
if (builddir == NULL) {
builddir = ".";
}
snprintf(base_path, PATH_MAX, "%s/tmp_augment_base.conf", builddir);
snprintf(augment_path, PATH_MAX, "%s/tmp_augment_augment.conf", builddir);
ret = write_to_file(base_path, config_base);
fail_unless(ret == 0, "Failed to write %s: ret %d.\n", base_path, ret);
write_to_file(augment_path, config_augment);
fail_unless(ret == 0, "Failed to write %s: ret %d.\n", augment_path, ret);
for (iter = 0; iter < 3; iter++) {
uint32_t merge_flags = flags[iter];
int expected_attributes_count = expected_attributes_counts[iter];
const char *test_section = test_sections[iter];
const char *test_attribute = test_attributes[iter];
const char *test_attribute_value = test_attribute_values[iter];
struct ini_cfgobj *in_cfg;
struct ini_cfgobj *result_cfg;
struct ini_cfgfile *file_ctx;
struct ref_array *error_list;
struct ref_array *success_list;
char **sections;
int sections_count;
char **attributes;
int attributes_count;
struct value_obj *val;
char *val_str;
/* Match only augment.conf */
const char *m_patterns[] = { "^tmp_augment_augment.conf$", NULL };
/* Match all sections */
const char *m_sections[] = { ".*", NULL };
/* Create config collection */
ret = ini_config_create(&in_cfg);
fail_unless(ret == EOK, "Failed to create collection. Error %d\n",
ret);
/* Open base.conf */
ret = ini_config_file_open(base_path, 0, &file_ctx);
fail_unless(ret == EOK, "Failed to open file. Error %d\n", ret);
/* Seed in_cfg with base.conf */
ret = ini_config_parse(file_ctx, 1, 0, 0, in_cfg);
fail_unless(ret == EOK, "Failed to parse file context. Error %d\n",
ret);
/* Update base.conf with augment.conf */
ret = ini_config_augment(in_cfg,
builddir,
m_patterns,
m_sections,
NULL,
INI_STOP_ON_NONE,
0,
INI_PARSE_NOSPACE|INI_PARSE_NOTAB,
merge_flags,
&result_cfg,
&error_list,
&success_list);
/* We always expect EEXIST due to DETECT being set. */
fail_unless(ret == EEXIST,
"Failed to augment context. Error %d\n", ret);
if (result_cfg) {
ini_config_destroy(in_cfg);
in_cfg = result_cfg;
result_cfg = NULL;
}
/* Get a list of sections from the resulting cfg. */
sections = ini_get_section_list(in_cfg, §ions_count, &ret);
fail_unless(ret == EOK, "Failed to get section list. Error %d\n", ret);
/* Validate that the tested section exists. */
ret = exists_array(test_section, sections, sections_count);
fail_if(ret == 0, "Failed to find expected section.\n");
/* Get a list of attributes from the resulting cfg. */
attributes = ini_get_attribute_list(in_cfg, test_section,
&attributes_count,
&ret);
fail_unless(ret == EOK, "Failed to get attribute list. Error %d\n",
ret);
/* Validate that the expected number of attributes exist. This
* distinguishes MERGE from PRESERVE/OVERWRITE. */
fail_unless(expected_attributes_count == attributes_count,
"Expected %d attributes, but received %d.\n",
expected_attributes_count, attributes_count);
/* Validate that the test attribute exists. This distinguishes
* PRESERVE from OVERWRITE. */
ret = exists_array(test_attribute, attributes, attributes_count);
fail_if(ret == 0, "Failed to find expected attribute.\n");
ret = ini_get_config_valueobj(test_section, test_attribute, in_cfg,
0, &val);
fail_unless(ret == EOK, "Failed to load value object. Error %d\n",
ret);
val_str = ini_get_string_config_value(val, &ret);
fail_unless(ret == EOK, "Failed to get config value. Error %d\n", ret);
/* Validate the value of the test attribute. */
ret = strcmp(val_str, test_attribute_value);
fail_unless(ret == 0, "Attribute %s didn't have expected value of "
"(%s): saw %s\n", test_attribute, test_attribute_value,
val_str);
/* Cleanup */
free(val_str);
ini_free_attribute_list(attributes);
ini_free_section_list(sections);
ref_array_destroy(error_list);
ini_config_file_destroy(file_ctx);
ref_array_destroy(success_list);
ini_config_destroy(in_cfg);
ini_config_destroy(result_cfg);
}
remove(base_path);
remove(augment_path);
}
END_TEST
START_TEST(test_ini_augment_empty_dir)
{
int ret;
struct ini_cfgobj *ini_cfg;
struct ini_cfgfile *file_ctx;
struct value_obj *vo;
const char *patterns[] = { ".*", NULL };
const char *sections[] = { ".*", NULL };
char **section_list;
char **attrs_list;
struct ini_cfgobj *result_cfg = NULL;
int size;
char empty_dir_path[PATH_MAX] = {0};
const char *builddir;
int32_t val;
char base_cfg[] =
"[section_one]\n"
"one = 1\n";
builddir = getenv("builddir");
if (builddir == NULL) {
builddir = ".";
}
ret = snprintf(empty_dir_path, PATH_MAX, "%s/tmp_empty_dir", builddir);
fail_if(ret > PATH_MAX || ret < 0, "snprintf failed\n");
ret = ini_config_file_from_mem(base_cfg, strlen(base_cfg),
&file_ctx);
fail_unless(ret == EOK, "Failed to load config. Error %d.\n", ret);
ret = ini_config_create(&ini_cfg);
fail_unless(ret == EOK, "Failed to create config. Error %d.\n", ret);
ret = ini_config_parse(file_ctx, INI_STOP_ON_ERROR, INI_MV1S_ALLOW, 0,
ini_cfg);
fail_unless(ret == EOK, "Failed to parse configuration. Error %d.\n", ret);
/* Create an empty directory */
ret = mkdir(empty_dir_path, 0700);
if (ret == -1) {
ret = errno;
fail_if(ret != EEXIST,
"Failed to create empty directory. Error %d.\n", errno);
}
ret = ini_config_augment(ini_cfg,
empty_dir_path,
patterns,
sections,
NULL,
INI_STOP_ON_ANY,
INI_MV1S_OVERWRITE,
INI_PARSE_NOWRAP,
INI_MV2S_OVERWRITE,
&result_cfg,
NULL,
NULL);
fail_unless(ret == EOK);
/* If the snippet directory is empty, result_cfg should be the original
* ini_cfg and not NULL */
fail_if(result_cfg == NULL);
/* Now check if the content of result_cfg is what we expected */
section_list = ini_get_section_list(result_cfg, &size, NULL);
fail_unless(size == 1);
fail_unless(strcmp(section_list[0], "section_one") == 0);
attrs_list = ini_get_attribute_list(result_cfg, section_list[0],
&size, NULL);
fail_unless(size == 1);
fail_unless(strcmp(attrs_list[0], "one") == 0);
ret = ini_get_config_valueobj(section_list[0],
attrs_list[0],
result_cfg,
INI_GET_FIRST_VALUE,
&vo);
fail_unless(ret == 0);
val = ini_get_int32_config_value(vo, 1, 100, NULL);
fail_unless(val == 1, "Expected attribute value not found.\n");
ini_free_attribute_list(attrs_list);
ini_free_section_list(section_list);
ini_config_destroy(result_cfg);
ini_config_destroy(ini_cfg);
ini_config_file_destroy(file_ctx);
remove(empty_dir_path);
}
END_TEST
static Suite *ini_augment_suite(void)
{
Suite *s = suite_create("ini_augment_suite");
TCase *tc_augment = tcase_create("ini_augment");
tcase_add_test(tc_augment, test_ini_augment_merge_sections);
tcase_add_test(tc_augment, test_ini_augment_empty_dir);
suite_add_tcase(s, tc_augment);
return s;
}
int main(void)
{
int number_failed;
Suite *s = ini_augment_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_ENV);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
|