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 remove (diffsFileName);
235 cbData.callBackOption = COMPARE;
236 cbData.cfgDiffs = cfgDiffs;
237 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
238 if (1 == (ret = cbData.status))
240 FPRINTF (stderr, "%s",
241 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
244 cbData.cfgDiffs = cfg;
245 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
246 if ((ret = cbData.status) == 1)
247 FPRINTF (stderr, "%s",
248 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
252 cbData.section = NULL;
253 cbData.callBackOption = PRINT;
254 printf ("\nExpected Diffs:\n");
255 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
256 cbData.section = NULL;
257 printf ("\nActual Diffs:\n");
258 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
260 GNUNET_CONFIGURATION_destroy (cfg);
261 GNUNET_CONFIGURATION_destroy (cfgDiffs);
262 GNUNET_free (diffsFileName);
271 unsigned long long l;
273 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
275 if (0 != strcmp ("b", c))
277 FPRINTF (stderr, "Got `%s'\n", c);
283 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
293 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
294 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
299 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
301 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
306 if (0 != strcmp (c, "10"))
315 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
321 if (0 != strcmp (c, "/hello/world"))
323 #define HI "\\hello\\world"
324 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
334 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
347 static const char *want[] = {
356 check (void *data, const char *fn)
360 if (0 == strcmp (want[*idx], fn))
366 return GNUNET_SYSERR;
370 testConfigFilenames ()
376 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
385 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
393 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
400 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
408 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
415 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",
443 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
459 main (int argc, char *argv[])
461 int failureCount = 0;
464 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
465 cfg = GNUNET_CONFIGURATION_create ();
466 GNUNET_assert (cfg != NULL);
468 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
470 FPRINTF (stderr, "%s", "Failed to parse configuration file\n");
471 GNUNET_CONFIGURATION_destroy (cfg);
474 failureCount += testConfig ();
475 if (failureCount > 0)
478 failureCount = testConfigFilenames ();
479 if (failureCount > 0)
482 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
484 FPRINTF (stderr, "%s", "Failed to write configuration file\n");
485 GNUNET_CONFIGURATION_destroy (cfg);
488 GNUNET_CONFIGURATION_destroy (cfg);
489 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
491 cfg = GNUNET_CONFIGURATION_create ();
493 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
496 GNUNET_CONFIGURATION_destroy (cfg);
500 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
503 GNUNET_CONFIGURATION_destroy (cfg);
506 if (0 != strcmp (c, "YES"))
510 GNUNET_CONFIGURATION_destroy (cfg);
515 GNUNET_CONFIGURATION_destroy (cfg);
517 /* Testing configuration diffs */
518 cfgDefault = GNUNET_CONFIGURATION_create ();
519 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
522 GNUNET_CONFIGURATION_destroy (cfgDefault);
526 /* Nothing changed in the new configuration */
527 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
529 /* Modify all entries of the last section */
530 failureCount += checkDiffs (cfgDefault, EDIT_SECTION);
532 /* Add a new section */
533 failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);
535 /* Add a new entry to the last section */
536 failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);
538 /* Modify all entries in the configuration */
539 failureCount += checkDiffs (cfgDefault, EDIT_ALL);
541 GNUNET_CONFIGURATION_destroy (cfgDefault);
544 if (failureCount != 0)
546 FPRINTF (stderr, "Test failed: %u\n", failureCount);