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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file util/test_configuration.c
20 * @brief Test that the configuration module works.
21 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
28 /* Test Configuration Diffs Options */
42 static struct GNUNET_CONFIGURATION_Handle *cfg;
43 static struct GNUNET_CONFIGURATION_Handle *cfg_default;
47 struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
56 initDiffsCBData (struct DiffsCBData *cbData)
58 cbData->section = NULL;
60 cbData->cfgDiffs = NULL;
61 cbData->callBackOption = -1;
67 * callback function for modifying
68 * and comparing configuration
71 diffsCallBack (void *cls, const char *section, const char *option,
74 struct DiffsCBData *cbData = cls;
75 int cbOption = cbData->callBackOption;
80 if (NULL == cbData->section)
81 cbData->section = section;
82 if (strcmp (cbData->section, section) == 0)
84 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
86 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
91 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
93 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
103 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key",
105 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
106 "new-key", "new-value");
117 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
119 if (NULL != diffValue)
121 if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
126 GNUNET_free_non_null (diffValue);
131 if (NULL == cbData->section)
133 cbData->section = section;
134 printf ("\nSection: %s\n", section);
136 else if (strcmp (cbData->section, section) != 0)
138 cbData->section = section;
139 printf ("\nSection: %s\n", section);
141 printf ("%s = %s\n", option, value);
149 static struct GNUNET_CONFIGURATION_Handle *
150 editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
152 struct DiffsCBData diffsCB;
154 initDiffsCBData (&diffsCB);
155 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
162 diffsCB.callBackOption = option;
164 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
169 case ADD_NEW_SECTION:
174 for (i = 0; i < 5; i++)
176 GNUNET_asprintf (&key, "key%d", i);
177 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
179 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
193 return diffsCB.cfgDiffs;
197 * Checking configuration diffs
200 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
202 struct GNUNET_CONFIGURATION_Handle *cfg;
203 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
204 struct DiffsCBData cbData;
208 initDiffsCBData (&cbData);
210 cfg = GNUNET_CONFIGURATION_create ();
212 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
214 /* Modify configuration and save it */
215 cfgDiffs = editConfiguration (cfg, option);
216 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
217 if (diffsFileName == NULL)
220 GNUNET_CONFIGURATION_destroy (cfg);
221 GNUNET_CONFIGURATION_destroy (cfgDiffs);
224 GNUNET_CONFIGURATION_write_diffs (cfg_default, cfg, diffsFileName);
225 GNUNET_CONFIGURATION_destroy (cfg);
227 /* Compare the dumped configuration with modifications done */
228 cfg = GNUNET_CONFIGURATION_create ();
229 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
230 if (0 != remove (diffsFileName))
231 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", diffsFileName);
232 cbData.callBackOption = COMPARE;
233 cbData.cfgDiffs = cfgDiffs;
234 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
235 if (1 == (ret = cbData.status))
237 FPRINTF (stderr, "%s",
238 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
241 cbData.cfgDiffs = cfg;
242 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
243 if ((ret = cbData.status) == 1)
244 FPRINTF (stderr, "%s",
245 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
249 cbData.section = NULL;
250 cbData.callBackOption = PRINT;
251 printf ("\nExpected Diffs:\n");
252 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
253 cbData.section = NULL;
254 printf ("\nActual Diffs:\n");
255 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
257 GNUNET_CONFIGURATION_destroy (cfg);
258 GNUNET_CONFIGURATION_destroy (cfgDiffs);
259 GNUNET_free (diffsFileName);
268 unsigned long long l;
270 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
272 if (0 != strcmp ("b", c))
274 FPRINTF (stderr, "Got `%s'\n", c);
280 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
290 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
291 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
296 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
298 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
303 if (0 != strcmp (c, "10"))
312 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
318 if (0 != strcmp (c, "/hello/world"))
320 #define HI "\\hello\\world"
321 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
331 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
344 static const char *want[] = {
353 check (void *data, const char *fn)
357 if (0 == strcmp (want[*idx], fn))
363 return GNUNET_SYSERR;
367 testConfigFilenames ()
373 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
382 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
390 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
397 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
405 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
412 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
420 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
428 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
440 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
456 main (int argc, char *argv[])
458 int failureCount = 0;
461 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
462 cfg = GNUNET_CONFIGURATION_create ();
463 GNUNET_assert (cfg != NULL);
465 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
467 FPRINTF (stderr, "%s", "Failed to parse configuration file\n");
468 GNUNET_CONFIGURATION_destroy (cfg);
471 failureCount += testConfig ();
472 if (failureCount > 0)
475 failureCount = testConfigFilenames ();
476 if (failureCount > 0)
479 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
481 FPRINTF (stderr, "%s", "Failed to write configuration file\n");
482 GNUNET_CONFIGURATION_destroy (cfg);
485 GNUNET_CONFIGURATION_destroy (cfg);
486 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
488 cfg = GNUNET_CONFIGURATION_create ();
490 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
493 GNUNET_CONFIGURATION_destroy (cfg);
497 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
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 cfg_default = GNUNET_CONFIGURATION_create ();
516 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_default, NULL))
519 GNUNET_CONFIGURATION_destroy (cfg_default);
523 /* Nothing changed in the new configuration */
524 failureCount += checkDiffs (cfg_default, EDIT_NOTHING);
526 /* Modify all entries of the last section */
527 failureCount += checkDiffs (cfg_default, EDIT_SECTION);
529 /* Add a new section */
530 failureCount += checkDiffs (cfg_default, ADD_NEW_SECTION);
532 /* Add a new entry to the last section */
533 failureCount += checkDiffs (cfg_default, ADD_NEW_ENTRY);
535 /* Modify all entries in the configuration */
536 failureCount += checkDiffs (cfg_default, EDIT_ALL);
538 GNUNET_CONFIGURATION_destroy (cfg_default);
541 if (failureCount != 0)
543 FPRINTF (stderr, "Test failed: %u\n", failureCount);