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 */
44 static struct GNUNET_CONFIGURATION_Handle *cfg;
45 static struct GNUNET_CONFIGURATION_Handle *cfg_default;
49 struct GNUNET_CONFIGURATION_Handle *cfg;
50 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
58 initDiffsCBData (struct DiffsCBData *cbData)
60 cbData->section = NULL;
62 cbData->cfgDiffs = NULL;
63 cbData->callBackOption = -1;
69 * callback function for modifying
70 * and comparing configuration
73 diffsCallBack (void *cls, const char *section, const char *option,
76 struct DiffsCBData *cbData = cls;
77 int cbOption = cbData->callBackOption;
82 if (NULL == cbData->section)
83 cbData->section = section;
84 if (strcmp (cbData->section, section) == 0)
86 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
88 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
94 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
96 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
107 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key",
109 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
110 "new-key", "new-value");
122 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
124 if (NULL != diffValue)
126 if ((ret == GNUNET_SYSERR) || (strcmp (diffValue, value) != 0) )
131 GNUNET_free_non_null (diffValue);
137 if (NULL == cbData->section)
139 cbData->section = section;
140 printf ("\nSection: %s\n", section);
142 else if (strcmp (cbData->section, section) != 0)
144 cbData->section = section;
145 printf ("\nSection: %s\n", section);
147 printf ("%s = %s\n", option, value);
155 static struct GNUNET_CONFIGURATION_Handle *
156 editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
158 struct DiffsCBData diffsCB;
160 initDiffsCBData (&diffsCB);
161 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
168 diffsCB.callBackOption = option;
170 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
177 case ADD_NEW_SECTION:
182 for (i = 0; i < 5; i++)
184 GNUNET_asprintf (&key, "key%d", i);
185 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
187 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
204 return diffsCB.cfgDiffs;
209 * Checking configuration diffs
212 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
214 struct GNUNET_CONFIGURATION_Handle *cfg;
215 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
216 struct DiffsCBData cbData;
220 initDiffsCBData (&cbData);
222 cfg = GNUNET_CONFIGURATION_create ();
224 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
226 /* Modify configuration and save it */
227 cfgDiffs = editConfiguration (cfg, option);
228 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
229 if (diffsFileName == NULL)
232 GNUNET_CONFIGURATION_destroy (cfg);
233 GNUNET_CONFIGURATION_destroy (cfgDiffs);
236 GNUNET_CONFIGURATION_write_diffs (cfg_default, cfg, diffsFileName);
237 GNUNET_CONFIGURATION_destroy (cfg);
239 /* Compare the dumped configuration with modifications done */
240 cfg = GNUNET_CONFIGURATION_create ();
241 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
242 if (0 != remove (diffsFileName))
243 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove",
245 cbData.callBackOption = COMPARE;
246 cbData.cfgDiffs = cfgDiffs;
247 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
248 if (1 == (ret = cbData.status))
250 fprintf (stderr, "%s",
251 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
254 cbData.cfgDiffs = cfg;
255 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
256 if ((ret = cbData.status) == 1)
257 fprintf (stderr, "%s",
258 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
262 cbData.section = NULL;
263 cbData.callBackOption = PRINT;
264 printf ("\nExpected Diffs:\n");
265 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
266 cbData.section = NULL;
267 printf ("\nActual Diffs:\n");
268 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
270 GNUNET_CONFIGURATION_destroy (cfg);
271 GNUNET_CONFIGURATION_destroy (cfgDiffs);
272 GNUNET_free (diffsFileName);
281 unsigned long long l;
283 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
285 if (0 != strcmp ("b", c))
287 fprintf (stderr, "Got `%s'\n", c);
293 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
303 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
304 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
309 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
311 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
316 if (0 != strcmp (c, "10"))
325 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
331 if (0 != strcmp (c, "/hello/world"))
340 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
354 static const char *want[] = {
363 check (void *data, const char *fn)
367 if (0 == strcmp (want[*idx], fn))
373 return GNUNET_SYSERR;
378 testConfigFilenames ()
384 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
393 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
401 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
408 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
416 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
423 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
431 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
439 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
451 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
467 main (int argc, char *argv[])
469 int failureCount = 0;
472 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
473 cfg = GNUNET_CONFIGURATION_create ();
474 GNUNET_assert (cfg != NULL);
476 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
478 fprintf (stderr, "%s", "Failed to parse configuration file\n");
479 GNUNET_CONFIGURATION_destroy (cfg);
482 failureCount += testConfig ();
483 if (failureCount > 0)
486 failureCount = testConfigFilenames ();
487 if (failureCount > 0)
490 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
492 fprintf (stderr, "%s", "Failed to write configuration file\n");
493 GNUNET_CONFIGURATION_destroy (cfg);
496 GNUNET_CONFIGURATION_destroy (cfg);
497 GNUNET_assert (0 == unlink ("/tmp/gnunet-test.conf"));
499 cfg = GNUNET_CONFIGURATION_create ();
501 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
504 GNUNET_CONFIGURATION_destroy (cfg);
508 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
511 GNUNET_CONFIGURATION_destroy (cfg);
514 if (0 != strcmp (c, "YES"))
518 GNUNET_CONFIGURATION_destroy (cfg);
523 GNUNET_CONFIGURATION_destroy (cfg);
525 /* Testing configuration diffs */
526 cfg_default = GNUNET_CONFIGURATION_create ();
527 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_default, NULL))
530 GNUNET_CONFIGURATION_destroy (cfg_default);
534 /* Nothing changed in the new configuration */
535 failureCount += checkDiffs (cfg_default, EDIT_NOTHING);
537 /* Modify all entries of the last section */
538 failureCount += checkDiffs (cfg_default, EDIT_SECTION);
540 /* Add a new section */
541 failureCount += checkDiffs (cfg_default, ADD_NEW_SECTION);
543 /* Add a new entry to the last section */
544 failureCount += checkDiffs (cfg_default, ADD_NEW_ENTRY);
546 /* Modify all entries in the configuration */
547 failureCount += checkDiffs (cfg_default, EDIT_ALL);
549 GNUNET_CONFIGURATION_destroy (cfg_default);
552 if (failureCount != 0)
554 fprintf (stderr, "Test failed: %u\n", failureCount);