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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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,
93 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
95 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
105 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key",
107 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
108 "new-key", "new-value");
119 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
121 if (NULL != diffValue)
123 if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
128 GNUNET_free_non_null (diffValue);
133 if (NULL == cbData->section)
135 cbData->section = section;
136 printf ("\nSection: %s\n", section);
138 else if (strcmp (cbData->section, section) != 0)
140 cbData->section = section;
141 printf ("\nSection: %s\n", section);
143 printf ("%s = %s\n", option, value);
151 static struct GNUNET_CONFIGURATION_Handle *
152 editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
154 struct DiffsCBData diffsCB;
156 initDiffsCBData (&diffsCB);
157 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
164 diffsCB.callBackOption = option;
166 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
171 case ADD_NEW_SECTION:
176 for (i = 0; i < 5; i++)
178 GNUNET_asprintf (&key, "key%d", i);
179 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
181 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
195 return diffsCB.cfgDiffs;
199 * Checking configuration diffs
202 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
204 struct GNUNET_CONFIGURATION_Handle *cfg;
205 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
206 struct DiffsCBData cbData;
210 initDiffsCBData (&cbData);
212 cfg = GNUNET_CONFIGURATION_create ();
214 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
216 /* Modify configuration and save it */
217 cfgDiffs = editConfiguration (cfg, option);
218 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
219 if (diffsFileName == NULL)
222 GNUNET_CONFIGURATION_destroy (cfg);
223 GNUNET_CONFIGURATION_destroy (cfgDiffs);
226 GNUNET_CONFIGURATION_write_diffs (cfg_default, cfg, diffsFileName);
227 GNUNET_CONFIGURATION_destroy (cfg);
229 /* Compare the dumped configuration with modifications done */
230 cfg = GNUNET_CONFIGURATION_create ();
231 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
232 if (0 != remove (diffsFileName))
233 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", diffsFileName);
234 cbData.callBackOption = COMPARE;
235 cbData.cfgDiffs = cfgDiffs;
236 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
237 if (1 == (ret = cbData.status))
239 FPRINTF (stderr, "%s",
240 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
243 cbData.cfgDiffs = cfg;
244 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
245 if ((ret = cbData.status) == 1)
246 FPRINTF (stderr, "%s",
247 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
251 cbData.section = NULL;
252 cbData.callBackOption = PRINT;
253 printf ("\nExpected Diffs:\n");
254 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
255 cbData.section = NULL;
256 printf ("\nActual Diffs:\n");
257 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
259 GNUNET_CONFIGURATION_destroy (cfg);
260 GNUNET_CONFIGURATION_destroy (cfgDiffs);
261 GNUNET_free (diffsFileName);
270 unsigned long long l;
272 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
274 if (0 != strcmp ("b", c))
276 FPRINTF (stderr, "Got `%s'\n", c);
282 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
292 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
293 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
298 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
300 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
305 if (0 != strcmp (c, "10"))
314 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
320 if (0 != strcmp (c, "/hello/world"))
322 #define HI "\\hello\\world"
323 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
333 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
346 static const char *want[] = {
355 check (void *data, const char *fn)
359 if (0 == strcmp (want[*idx], fn))
365 return GNUNET_SYSERR;
369 testConfigFilenames ()
375 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
384 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
392 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
399 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
407 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
414 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
422 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
430 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
442 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
458 main (int argc, char *argv[])
460 int failureCount = 0;
463 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
464 cfg = GNUNET_CONFIGURATION_create ();
465 GNUNET_assert (cfg != NULL);
467 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
469 FPRINTF (stderr, "%s", "Failed to parse configuration file\n");
470 GNUNET_CONFIGURATION_destroy (cfg);
473 failureCount += testConfig ();
474 if (failureCount > 0)
477 failureCount = testConfigFilenames ();
478 if (failureCount > 0)
481 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
483 FPRINTF (stderr, "%s", "Failed to write configuration file\n");
484 GNUNET_CONFIGURATION_destroy (cfg);
487 GNUNET_CONFIGURATION_destroy (cfg);
488 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
490 cfg = GNUNET_CONFIGURATION_create ();
492 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
495 GNUNET_CONFIGURATION_destroy (cfg);
499 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
502 GNUNET_CONFIGURATION_destroy (cfg);
505 if (0 != strcmp (c, "YES"))
509 GNUNET_CONFIGURATION_destroy (cfg);
514 GNUNET_CONFIGURATION_destroy (cfg);
516 /* Testing configuration diffs */
517 cfg_default = GNUNET_CONFIGURATION_create ();
518 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_default, NULL))
521 GNUNET_CONFIGURATION_destroy (cfg_default);
525 /* Nothing changed in the new configuration */
526 failureCount += checkDiffs (cfg_default, EDIT_NOTHING);
528 /* Modify all entries of the last section */
529 failureCount += checkDiffs (cfg_default, EDIT_SECTION);
531 /* Add a new section */
532 failureCount += checkDiffs (cfg_default, ADD_NEW_SECTION);
534 /* Add a new entry to the last section */
535 failureCount += checkDiffs (cfg_default, ADD_NEW_ENTRY);
537 /* Modify all entries in the configuration */
538 failureCount += checkDiffs (cfg_default, EDIT_ALL);
540 GNUNET_CONFIGURATION_destroy (cfg_default);
543 if (failureCount != 0)
545 FPRINTF (stderr, "Test failed: %u\n", failureCount);