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;
208 * Checking configuration diffs
211 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
213 struct GNUNET_CONFIGURATION_Handle *cfg;
214 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
215 struct DiffsCBData cbData;
219 initDiffsCBData (&cbData);
221 cfg = GNUNET_CONFIGURATION_create ();
223 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
225 /* Modify configuration and save it */
226 cfgDiffs = editConfiguration (cfg, option);
227 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
228 if (diffsFileName == NULL)
231 GNUNET_CONFIGURATION_destroy (cfg);
232 GNUNET_CONFIGURATION_destroy (cfgDiffs);
235 GNUNET_CONFIGURATION_write_diffs (cfg_default, cfg, diffsFileName);
236 GNUNET_CONFIGURATION_destroy (cfg);
238 /* Compare the dumped configuration with modifications done */
239 cfg = GNUNET_CONFIGURATION_create ();
240 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
241 if (0 != remove (diffsFileName))
242 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove",
244 cbData.callBackOption = COMPARE;
245 cbData.cfgDiffs = cfgDiffs;
246 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
247 if (1 == (ret = cbData.status))
249 fprintf (stderr, "%s",
250 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
253 cbData.cfgDiffs = cfg;
254 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
255 if ((ret = cbData.status) == 1)
256 fprintf (stderr, "%s",
257 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
261 cbData.section = NULL;
262 cbData.callBackOption = PRINT;
263 printf ("\nExpected Diffs:\n");
264 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
265 cbData.section = NULL;
266 printf ("\nActual Diffs:\n");
267 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
269 GNUNET_CONFIGURATION_destroy (cfg);
270 GNUNET_CONFIGURATION_destroy (cfgDiffs);
271 GNUNET_free (diffsFileName);
280 unsigned long long l;
282 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
284 if (0 != strcmp ("b", c))
286 fprintf (stderr, "Got `%s'\n", c);
292 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
302 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
303 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
308 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
310 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
315 if (0 != strcmp (c, "10"))
324 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
330 if (0 != strcmp (c, "/hello/world"))
339 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
352 static const char *want[] = {
361 check (void *data, const char *fn)
365 if (0 == strcmp (want[*idx], fn))
371 return GNUNET_SYSERR;
375 testConfigFilenames ()
381 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
390 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
398 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
405 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
413 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
420 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
428 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
436 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
448 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
464 main (int argc, char *argv[])
466 int failureCount = 0;
469 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
470 cfg = GNUNET_CONFIGURATION_create ();
471 GNUNET_assert (cfg != NULL);
473 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
475 fprintf (stderr, "%s", "Failed to parse configuration file\n");
476 GNUNET_CONFIGURATION_destroy (cfg);
479 failureCount += testConfig ();
480 if (failureCount > 0)
483 failureCount = testConfigFilenames ();
484 if (failureCount > 0)
487 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
489 fprintf (stderr, "%s", "Failed to write configuration file\n");
490 GNUNET_CONFIGURATION_destroy (cfg);
493 GNUNET_CONFIGURATION_destroy (cfg);
494 GNUNET_assert (0 == unlink ("/tmp/gnunet-test.conf"));
496 cfg = GNUNET_CONFIGURATION_create ();
498 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
501 GNUNET_CONFIGURATION_destroy (cfg);
505 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
508 GNUNET_CONFIGURATION_destroy (cfg);
511 if (0 != strcmp (c, "YES"))
515 GNUNET_CONFIGURATION_destroy (cfg);
520 GNUNET_CONFIGURATION_destroy (cfg);
522 /* Testing configuration diffs */
523 cfg_default = GNUNET_CONFIGURATION_create ();
524 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_default, NULL))
527 GNUNET_CONFIGURATION_destroy (cfg_default);
531 /* Nothing changed in the new configuration */
532 failureCount += checkDiffs (cfg_default, EDIT_NOTHING);
534 /* Modify all entries of the last section */
535 failureCount += checkDiffs (cfg_default, EDIT_SECTION);
537 /* Add a new section */
538 failureCount += checkDiffs (cfg_default, ADD_NEW_SECTION);
540 /* Add a new entry to the last section */
541 failureCount += checkDiffs (cfg_default, ADD_NEW_ENTRY);
543 /* Modify all entries in the configuration */
544 failureCount += checkDiffs (cfg_default, EDIT_ALL);
546 GNUNET_CONFIGURATION_destroy (cfg_default);
549 if (failureCount != 0)
551 fprintf (stderr, "Test failed: %u\n", failureCount);