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"
32 /* Test Configuration Diffs Options */
46 static struct GNUNET_CONFIGURATION_Handle *cfg;
47 static struct GNUNET_CONFIGURATION_Handle *cfgDefault;
51 struct GNUNET_CONFIGURATION_Handle *cfg;
52 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
60 initDiffsCBData (struct DiffsCBData *cbData)
62 cbData->section = NULL;
64 cbData->cfgDiffs = NULL;
65 cbData->callBackOption = -1;
71 * callback function for modifying
72 * and comparing configuration
75 diffsCallBack (void *cls, const char *section, const char *option,
78 struct DiffsCBData *cbData = cls;
79 int cbOption = cbData->callBackOption;
84 if (NULL == cbData->section)
85 cbData->section = section;
86 if (strcmp (cbData->section, section) == 0)
88 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
90 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
95 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
97 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");
121 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
123 if (NULL != diffValue)
125 if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
130 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);
173 case ADD_NEW_SECTION:
178 for (i = 0; i < 5; i++)
180 GNUNET_asprintf (&key, "key%d", i);
181 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
183 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
197 return diffsCB.cfgDiffs;
201 * Checking configuration diffs
204 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
206 struct GNUNET_CONFIGURATION_Handle *cfg;
207 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
208 struct DiffsCBData cbData;
212 initDiffsCBData (&cbData);
214 cfg = GNUNET_CONFIGURATION_create ();
216 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
218 /* Modify configuration and save it */
219 cfgDiffs = editConfiguration (cfg, option);
220 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
221 if (diffsFileName == NULL)
224 GNUNET_CONFIGURATION_destroy (cfg);
225 GNUNET_CONFIGURATION_destroy (cfgDiffs);
228 GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName);
229 GNUNET_CONFIGURATION_destroy (cfg);
231 /* Compare the dumped configuration with modifications done */
232 cfg = GNUNET_CONFIGURATION_create ();
233 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
234 if (0 != remove (diffsFileName))
235 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", diffsFileName);
236 cbData.callBackOption = COMPARE;
237 cbData.cfgDiffs = cfgDiffs;
238 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
239 if (1 == (ret = cbData.status))
241 FPRINTF (stderr, "%s",
242 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
245 cbData.cfgDiffs = cfg;
246 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
247 if ((ret = cbData.status) == 1)
248 FPRINTF (stderr, "%s",
249 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
253 cbData.section = NULL;
254 cbData.callBackOption = PRINT;
255 printf ("\nExpected Diffs:\n");
256 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
257 cbData.section = NULL;
258 printf ("\nActual Diffs:\n");
259 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
261 GNUNET_CONFIGURATION_destroy (cfg);
262 GNUNET_CONFIGURATION_destroy (cfgDiffs);
263 GNUNET_free (diffsFileName);
272 unsigned long long l;
274 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
276 if (0 != strcmp ("b", c))
278 FPRINTF (stderr, "Got `%s'\n", c);
284 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
294 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
295 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
300 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
302 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
307 if (0 != strcmp (c, "10"))
316 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
322 if (0 != strcmp (c, "/hello/world"))
324 #define HI "\\hello\\world"
325 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
335 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
348 static const char *want[] = {
357 check (void *data, const char *fn)
361 if (0 == strcmp (want[*idx], fn))
367 return GNUNET_SYSERR;
371 testConfigFilenames ()
377 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
386 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
394 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
401 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
409 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
416 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
424 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
432 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
444 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
460 main (int argc, char *argv[])
462 int failureCount = 0;
465 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
466 cfg = GNUNET_CONFIGURATION_create ();
467 GNUNET_assert (cfg != NULL);
469 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
471 FPRINTF (stderr, "%s", "Failed to parse configuration file\n");
472 GNUNET_CONFIGURATION_destroy (cfg);
475 failureCount += testConfig ();
476 if (failureCount > 0)
479 failureCount = testConfigFilenames ();
480 if (failureCount > 0)
483 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
485 FPRINTF (stderr, "%s", "Failed to write configuration file\n");
486 GNUNET_CONFIGURATION_destroy (cfg);
489 GNUNET_CONFIGURATION_destroy (cfg);
490 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
492 cfg = GNUNET_CONFIGURATION_create ();
494 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
497 GNUNET_CONFIGURATION_destroy (cfg);
501 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
504 GNUNET_CONFIGURATION_destroy (cfg);
507 if (0 != strcmp (c, "YES"))
511 GNUNET_CONFIGURATION_destroy (cfg);
516 GNUNET_CONFIGURATION_destroy (cfg);
518 /* Testing configuration diffs */
519 cfgDefault = GNUNET_CONFIGURATION_create ();
520 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
523 GNUNET_CONFIGURATION_destroy (cfgDefault);
527 /* Nothing changed in the new configuration */
528 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
530 /* Modify all entries of the last section */
531 failureCount += checkDiffs (cfgDefault, EDIT_SECTION);
533 /* Add a new section */
534 failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);
536 /* Add a new entry to the last section */
537 failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);
539 /* Modify all entries in the configuration */
540 failureCount += checkDiffs (cfgDefault, EDIT_ALL);
542 GNUNET_CONFIGURATION_destroy (cfgDefault);
545 if (failureCount != 0)
547 FPRINTF (stderr, "Test failed: %u\n", failureCount);