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,
98 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
100 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
101 option, "new-value");
110 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section,
111 "new-key", "new-value");
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,
201 return diffsCB.cfgDiffs;
205 * Checking configuration diffs
208 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
210 struct GNUNET_CONFIGURATION_Handle *cfg;
211 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
212 struct DiffsCBData cbData;
216 initDiffsCBData (&cbData);
218 cfg = GNUNET_CONFIGURATION_create ();
220 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
222 /* Modify configuration and save it */
223 cfgDiffs = editConfiguration (cfg, option);
225 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 (cfgDefault, 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 ==
239 GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
240 remove (diffsFileName);
241 cbData.callBackOption = COMPARE;
242 cbData.cfgDiffs = cfgDiffs;
243 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
244 if (1 == (ret = cbData.status))
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)
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;
280 GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
282 if (0 != strcmp ("b", c))
284 fprintf (stderr, "Got `%s'\n", c);
290 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
300 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
301 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
306 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
308 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
313 if (0 != strcmp (c, "10"))
322 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
328 if (0 != strcmp (c, "/hello/world"))
330 #define HI "\\hello\\world"
331 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
343 static const char *want[] = {
352 check (void *data, const char *fn)
356 if (0 == strcmp (want[*idx], fn))
362 return GNUNET_SYSERR;
366 testConfigFilenames ()
372 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
381 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
389 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
396 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
404 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
411 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
419 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
427 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
439 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
455 main (int argc, char *argv[])
457 int failureCount = 0;
460 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
461 cfg = GNUNET_CONFIGURATION_create ();
462 GNUNET_assert (cfg != NULL);
464 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
466 fprintf (stderr, "Failed to parse configuration file\n");
467 GNUNET_CONFIGURATION_destroy (cfg);
470 failureCount += testConfig ();
471 if (failureCount > 0)
474 failureCount = testConfigFilenames ();
475 if (failureCount > 0)
478 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
480 fprintf (stderr, "Failed to write configuration file\n");
481 GNUNET_CONFIGURATION_destroy (cfg);
484 GNUNET_CONFIGURATION_destroy (cfg);
485 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
487 cfg = GNUNET_CONFIGURATION_create ();
489 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
492 GNUNET_CONFIGURATION_destroy (cfg);
496 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM",
500 GNUNET_CONFIGURATION_destroy (cfg);
503 if (0 != strcmp (c, "YES"))
507 GNUNET_CONFIGURATION_destroy (cfg);
512 GNUNET_CONFIGURATION_destroy (cfg);
514 /* Testing configuration diffs */
515 cfgDefault = GNUNET_CONFIGURATION_create ();
516 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
519 GNUNET_CONFIGURATION_destroy (cfgDefault);
523 /* Nothing changed in the new configuration */
524 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
526 /* Modify all entries of the last section */
527 failureCount += checkDiffs (cfgDefault, EDIT_SECTION);
529 /* Add a new section */
530 failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);
532 /* Add a new entry to the last section */
533 failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);
535 /* Modify all entries in the configuration */
536 failureCount += checkDiffs (cfgDefault, EDIT_ALL);
538 GNUNET_CONFIGURATION_destroy (cfgDefault);
541 if (failureCount != 0)
543 fprintf (stderr, "Test failed: %u\n", failureCount);