2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006, 2007 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file util/test_configuration.c
22 * @brief Test that the configuration module works.
23 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_disk_lib.h"
31 #define DEBUG GNUNET_EXTRA_LOGGING
33 /* Test Configuration Diffs Options */
49 static struct GNUNET_CONFIGURATION_Handle *cfg;
50 static struct GNUNET_CONFIGURATION_Handle *cfgDefault;
54 struct GNUNET_CONFIGURATION_Handle *cfg;
55 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
63 initDiffsCBData (struct DiffsCBData *cbData)
65 cbData->section = NULL;
67 cbData->cfgDiffs = NULL;
68 cbData->callBackOption = -1;
74 * callback function for modifying
75 * and comparing configuration
78 diffsCallBack (void *cls, const char *section, const char *option,
81 struct DiffsCBData *cbData = cls;
82 int cbOption = cbData->callBackOption;
87 if (NULL == cbData->section)
88 cbData->section = section;
89 if (strcmp (cbData->section, section) == 0)
91 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
93 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
98 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
100 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
110 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key",
112 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
113 "new-key", "new-value");
124 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
126 if (NULL != diffValue)
128 if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
133 GNUNET_free_non_null (diffValue);
138 if (NULL == cbData->section)
140 cbData->section = section;
141 printf ("\nSection: %s\n", section);
143 else if (strcmp (cbData->section, section) != 0)
145 cbData->section = section;
146 printf ("\nSection: %s\n", section);
148 printf ("%s = %s\n", option, value);
156 static struct GNUNET_CONFIGURATION_Handle *
157 editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
159 struct DiffsCBData diffsCB;
161 initDiffsCBData (&diffsCB);
162 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
169 diffsCB.callBackOption = option;
171 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
176 case ADD_NEW_SECTION:
181 for (i = 0; i < 5; i++)
183 GNUNET_asprintf (&key, "key%d", i);
184 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
186 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
200 return diffsCB.cfgDiffs;
204 * Checking configuration diffs
207 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
209 struct GNUNET_CONFIGURATION_Handle *cfg;
210 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
211 struct DiffsCBData cbData;
215 initDiffsCBData (&cbData);
217 cfg = GNUNET_CONFIGURATION_create ();
219 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
221 /* Modify configuration and save it */
222 cfgDiffs = editConfiguration (cfg, option);
223 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
224 if (diffsFileName == NULL)
227 GNUNET_CONFIGURATION_destroy (cfg);
228 GNUNET_CONFIGURATION_destroy (cfgDiffs);
231 GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName);
232 GNUNET_CONFIGURATION_destroy (cfg);
234 /* Compare the dumped configuration with modifications done */
235 cfg = GNUNET_CONFIGURATION_create ();
236 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
237 remove (diffsFileName);
238 cbData.callBackOption = COMPARE;
239 cbData.cfgDiffs = cfgDiffs;
240 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
241 if (1 == (ret = cbData.status))
243 FPRINTF (stderr, "%s",
244 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
247 cbData.cfgDiffs = cfg;
248 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
249 if ((ret = cbData.status) == 1)
250 FPRINTF (stderr, "%s",
251 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
255 cbData.section = NULL;
256 cbData.callBackOption = PRINT;
257 printf ("\nExpected Diffs:\n");
258 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
259 cbData.section = NULL;
260 printf ("\nActual Diffs:\n");
261 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
263 GNUNET_CONFIGURATION_destroy (cfg);
264 GNUNET_CONFIGURATION_destroy (cfgDiffs);
265 GNUNET_free (diffsFileName);
274 unsigned long long l;
276 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
278 if (0 != strcmp ("b", c))
280 FPRINTF (stderr, "Got `%s'\n", c);
286 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
296 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
297 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
302 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
304 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
309 if (0 != strcmp (c, "10"))
318 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
324 if (0 != strcmp (c, "/hello/world"))
326 #define HI "\\hello\\world"
327 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
337 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
350 static const char *want[] = {
359 check (void *data, const char *fn)
363 if (0 == strcmp (want[*idx], fn))
369 return GNUNET_SYSERR;
373 testConfigFilenames ()
379 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
388 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
396 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
403 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
411 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
418 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
426 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
434 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
446 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
462 main (int argc, char *argv[])
464 int failureCount = 0;
467 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
468 cfg = GNUNET_CONFIGURATION_create ();
469 GNUNET_assert (cfg != NULL);
471 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
473 FPRINTF (stderr, "%s", "Failed to parse configuration file\n");
474 GNUNET_CONFIGURATION_destroy (cfg);
477 failureCount += testConfig ();
478 if (failureCount > 0)
481 failureCount = testConfigFilenames ();
482 if (failureCount > 0)
485 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
487 FPRINTF (stderr, "%s", "Failed to write configuration file\n");
488 GNUNET_CONFIGURATION_destroy (cfg);
491 GNUNET_CONFIGURATION_destroy (cfg);
492 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
494 cfg = GNUNET_CONFIGURATION_create ();
496 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
499 GNUNET_CONFIGURATION_destroy (cfg);
503 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
506 GNUNET_CONFIGURATION_destroy (cfg);
509 if (0 != strcmp (c, "YES"))
513 GNUNET_CONFIGURATION_destroy (cfg);
518 GNUNET_CONFIGURATION_destroy (cfg);
520 /* Testing configuration diffs */
521 cfgDefault = GNUNET_CONFIGURATION_create ();
522 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
525 GNUNET_CONFIGURATION_destroy (cfgDefault);
529 /* Nothing changed in the new configuration */
530 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
532 /* Modify all entries of the last section */
533 failureCount += checkDiffs (cfgDefault, EDIT_SECTION);
535 /* Add a new section */
536 failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);
538 /* Add a new entry to the last section */
539 failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);
541 /* Modify all entries in the configuration */
542 failureCount += checkDiffs (cfgDefault, EDIT_ALL);
544 GNUNET_CONFIGURATION_destroy (cfgDefault);
547 if (failureCount != 0)
549 FPRINTF (stderr, "Test failed: %u\n", failureCount);