2 This file is part of GNUnet.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file util/test_configuration.c
22 * @brief Test that the configuration module works.
23 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
30 /* Test Configuration Diffs Options */
43 static struct GNUNET_CONFIGURATION_Handle *cfg;
44 static struct GNUNET_CONFIGURATION_Handle *cfg_default;
47 struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
56 initDiffsCBData(struct DiffsCBData *cbData)
58 cbData->section = NULL;
60 cbData->cfgDiffs = NULL;
61 cbData->callBackOption = -1;
67 * callback function for modifying
68 * and comparing configuration
71 diffsCallBack(void *cls, const char *section, const char *option,
74 struct DiffsCBData *cbData = cls;
75 int cbOption = cbData->callBackOption;
80 if (NULL == cbData->section)
81 cbData->section = section;
82 if (strcmp(cbData->section, section) == 0)
84 GNUNET_CONFIGURATION_set_value_string(cbData->cfg, section, option,
86 GNUNET_CONFIGURATION_set_value_string(cbData->cfgDiffs, section, option,
92 GNUNET_CONFIGURATION_set_value_string(cbData->cfg, section, option,
94 GNUNET_CONFIGURATION_set_value_string(cbData->cfgDiffs, section, option,
105 GNUNET_CONFIGURATION_set_value_string(cbData->cfg, section, "new-key",
107 GNUNET_CONFIGURATION_set_value_string(cbData->cfgDiffs, section,
108 "new-key", "new-value");
120 GNUNET_CONFIGURATION_get_value_string(cbData->cfgDiffs, section,
122 if (NULL != diffValue)
124 if (ret == GNUNET_SYSERR || strcmp(diffValue, value) != 0)
129 GNUNET_free_non_null(diffValue);
135 if (NULL == cbData->section)
137 cbData->section = section;
138 printf("\nSection: %s\n", section);
140 else if (strcmp(cbData->section, section) != 0)
142 cbData->section = section;
143 printf("\nSection: %s\n", section);
145 printf("%s = %s\n", option, value);
153 static struct GNUNET_CONFIGURATION_Handle *
154 editConfiguration(struct GNUNET_CONFIGURATION_Handle *cfg, int option)
156 struct DiffsCBData diffsCB;
158 initDiffsCBData(&diffsCB);
159 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create();
166 diffsCB.callBackOption = option;
168 GNUNET_CONFIGURATION_iterate(cfg, diffsCallBack, &diffsCB);
175 case ADD_NEW_SECTION:
180 for (i = 0; i < 5; i++)
182 GNUNET_asprintf(&key, "key%d", i);
183 GNUNET_CONFIGURATION_set_value_string(cfg, "new-section", key,
185 GNUNET_CONFIGURATION_set_value_string(diffsCB.cfgDiffs, "new-section",
202 return diffsCB.cfgDiffs;
206 * Checking configuration diffs
209 checkDiffs(struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
211 struct GNUNET_CONFIGURATION_Handle *cfg;
212 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
213 struct DiffsCBData cbData;
217 initDiffsCBData(&cbData);
219 cfg = GNUNET_CONFIGURATION_create();
221 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(cfg, NULL));
223 /* Modify configuration and save it */
224 cfgDiffs = editConfiguration(cfg, option);
225 diffsFileName = GNUNET_DISK_mktemp("gnunet-test-configurations-diffs.conf");
226 if (diffsFileName == NULL)
229 GNUNET_CONFIGURATION_destroy(cfg);
230 GNUNET_CONFIGURATION_destroy(cfgDiffs);
233 GNUNET_CONFIGURATION_write_diffs(cfg_default, cfg, diffsFileName);
234 GNUNET_CONFIGURATION_destroy(cfg);
236 /* Compare the dumped configuration with modifications done */
237 cfg = GNUNET_CONFIGURATION_create();
238 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_parse(cfg, diffsFileName));
239 if (0 != remove(diffsFileName))
240 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "remove", diffsFileName);
241 cbData.callBackOption = COMPARE;
242 cbData.cfgDiffs = cfgDiffs;
243 GNUNET_CONFIGURATION_iterate(cfg, diffsCallBack, &cbData);
244 if (1 == (ret = cbData.status))
246 fprintf(stderr, "%s",
247 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
250 cbData.cfgDiffs = cfg;
251 GNUNET_CONFIGURATION_iterate(cfgDiffs, diffsCallBack, &cbData);
252 if ((ret = cbData.status) == 1)
253 fprintf(stderr, "%s",
254 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
258 cbData.section = NULL;
259 cbData.callBackOption = PRINT;
260 printf("\nExpected Diffs:\n");
261 GNUNET_CONFIGURATION_iterate(cfgDiffs, diffsCallBack, &cbData);
262 cbData.section = NULL;
263 printf("\nActual Diffs:\n");
264 GNUNET_CONFIGURATION_iterate(cfg, diffsCallBack, &cbData);
266 GNUNET_CONFIGURATION_destroy(cfg);
267 GNUNET_CONFIGURATION_destroy(cfgDiffs);
268 GNUNET_free(diffsFileName);
277 unsigned long long l;
279 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "test", "b", &c))
281 if (0 != strcmp("b", c))
283 fprintf(stderr, "Got `%s'\n", c);
289 GNUNET_CONFIGURATION_get_value_number(cfg, "test", "five", &l))
299 GNUNET_CONFIGURATION_set_value_string(cfg, "more", "c", "YES");
300 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, "more", "c"))
305 GNUNET_CONFIGURATION_set_value_number(cfg, "NUMBERS", "TEN", 10);
307 GNUNET_CONFIGURATION_get_value_string(cfg, "NUMBERS", "TEN", &c))
312 if (0 != strcmp(c, "10"))
321 GNUNET_CONFIGURATION_get_value_filename(cfg, "last", "test", &c))
327 if (0 != strcmp(c, "/hello/world"))
336 GNUNET_CONFIGURATION_get_value_size(cfg, "last", "size", &l))
349 static const char *want[] = {
358 check(void *data, const char *fn)
362 if (0 == strcmp(want[*idx], fn))
368 return GNUNET_SYSERR;
372 testConfigFilenames()
378 GNUNET_CONFIGURATION_iterate_value_filenames(cfg, "FILENAMES", "test",
387 GNUNET_CONFIGURATION_remove_value_filename(cfg, "FILENAMES", "test",
395 GNUNET_CONFIGURATION_remove_value_filename(cfg, "FILENAMES", "test",
402 GNUNET_CONFIGURATION_remove_value_filename(cfg, "FILENAMES", "test",
410 GNUNET_CONFIGURATION_append_value_filename(cfg, "FILENAMES", "test",
417 GNUNET_CONFIGURATION_append_value_filename(cfg, "FILENAMES", "test",
425 GNUNET_CONFIGURATION_append_value_filename(cfg, "FILENAMES", "test",
433 GNUNET_CONFIGURATION_append_value_filename(cfg, "FILENAMES", "test",
445 GNUNET_CONFIGURATION_iterate_value_filenames(cfg, "FILENAMES", "test",
461 main(int argc, char *argv[])
463 int failureCount = 0;
466 GNUNET_log_setup("test_configuration", "WARNING", NULL);
467 cfg = GNUNET_CONFIGURATION_create();
468 GNUNET_assert(cfg != NULL);
470 GNUNET_CONFIGURATION_parse(cfg, "test_configuration_data.conf"))
472 fprintf(stderr, "%s", "Failed to parse configuration file\n");
473 GNUNET_CONFIGURATION_destroy(cfg);
476 failureCount += testConfig();
477 if (failureCount > 0)
480 failureCount = testConfigFilenames();
481 if (failureCount > 0)
484 if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg, "/tmp/gnunet-test.conf"))
486 fprintf(stderr, "%s", "Failed to write configuration file\n");
487 GNUNET_CONFIGURATION_destroy(cfg);
490 GNUNET_CONFIGURATION_destroy(cfg);
491 GNUNET_assert(0 == unlink("/tmp/gnunet-test.conf"));
493 cfg = GNUNET_CONFIGURATION_create();
495 GNUNET_CONFIGURATION_load(cfg, "test_configuration_data.conf"))
498 GNUNET_CONFIGURATION_destroy(cfg);
502 GNUNET_CONFIGURATION_get_value_string(cfg, "TESTING", "WEAKRANDOM", &c))
505 GNUNET_CONFIGURATION_destroy(cfg);
508 if (0 != strcmp(c, "YES"))
512 GNUNET_CONFIGURATION_destroy(cfg);
517 GNUNET_CONFIGURATION_destroy(cfg);
519 /* Testing configuration diffs */
520 cfg_default = GNUNET_CONFIGURATION_create();
521 if (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_default, NULL))
524 GNUNET_CONFIGURATION_destroy(cfg_default);
528 /* Nothing changed in the new configuration */
529 failureCount += checkDiffs(cfg_default, EDIT_NOTHING);
531 /* Modify all entries of the last section */
532 failureCount += checkDiffs(cfg_default, EDIT_SECTION);
534 /* Add a new section */
535 failureCount += checkDiffs(cfg_default, ADD_NEW_SECTION);
537 /* Add a new entry to the last section */
538 failureCount += checkDiffs(cfg_default, ADD_NEW_ENTRY);
540 /* Modify all entries in the configuration */
541 failureCount += checkDiffs(cfg_default, EDIT_ALL);
543 GNUNET_CONFIGURATION_destroy(cfg_default);
546 if (failureCount != 0)
548 fprintf(stderr, "Test failed: %u\n", failureCount);