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.
16 * @file util/test_configuration.c
17 * @brief Test that the configuration module works.
18 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
25 /* Test Configuration Diffs Options */
39 static struct GNUNET_CONFIGURATION_Handle *cfg;
40 static struct GNUNET_CONFIGURATION_Handle *cfg_default;
44 struct GNUNET_CONFIGURATION_Handle *cfg;
45 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
53 initDiffsCBData (struct DiffsCBData *cbData)
55 cbData->section = NULL;
57 cbData->cfgDiffs = NULL;
58 cbData->callBackOption = -1;
64 * callback function for modifying
65 * and comparing configuration
68 diffsCallBack (void *cls, const char *section, const char *option,
71 struct DiffsCBData *cbData = cls;
72 int cbOption = cbData->callBackOption;
77 if (NULL == cbData->section)
78 cbData->section = section;
79 if (strcmp (cbData->section, section) == 0)
81 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
83 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
88 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
90 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
100 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key",
102 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
103 "new-key", "new-value");
114 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
116 if (NULL != diffValue)
118 if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
123 GNUNET_free_non_null (diffValue);
128 if (NULL == cbData->section)
130 cbData->section = section;
131 printf ("\nSection: %s\n", section);
133 else if (strcmp (cbData->section, section) != 0)
135 cbData->section = section;
136 printf ("\nSection: %s\n", section);
138 printf ("%s = %s\n", option, value);
146 static struct GNUNET_CONFIGURATION_Handle *
147 editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
149 struct DiffsCBData diffsCB;
151 initDiffsCBData (&diffsCB);
152 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
159 diffsCB.callBackOption = option;
161 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
166 case ADD_NEW_SECTION:
171 for (i = 0; i < 5; i++)
173 GNUNET_asprintf (&key, "key%d", i);
174 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
176 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
190 return diffsCB.cfgDiffs;
194 * Checking configuration diffs
197 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
199 struct GNUNET_CONFIGURATION_Handle *cfg;
200 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
201 struct DiffsCBData cbData;
205 initDiffsCBData (&cbData);
207 cfg = GNUNET_CONFIGURATION_create ();
209 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
211 /* Modify configuration and save it */
212 cfgDiffs = editConfiguration (cfg, option);
213 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
214 if (diffsFileName == NULL)
217 GNUNET_CONFIGURATION_destroy (cfg);
218 GNUNET_CONFIGURATION_destroy (cfgDiffs);
221 GNUNET_CONFIGURATION_write_diffs (cfg_default, cfg, diffsFileName);
222 GNUNET_CONFIGURATION_destroy (cfg);
224 /* Compare the dumped configuration with modifications done */
225 cfg = GNUNET_CONFIGURATION_create ();
226 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
227 if (0 != remove (diffsFileName))
228 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", diffsFileName);
229 cbData.callBackOption = COMPARE;
230 cbData.cfgDiffs = cfgDiffs;
231 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
232 if (1 == (ret = cbData.status))
234 FPRINTF (stderr, "%s",
235 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
238 cbData.cfgDiffs = cfg;
239 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
240 if ((ret = cbData.status) == 1)
241 FPRINTF (stderr, "%s",
242 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
246 cbData.section = NULL;
247 cbData.callBackOption = PRINT;
248 printf ("\nExpected Diffs:\n");
249 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
250 cbData.section = NULL;
251 printf ("\nActual Diffs:\n");
252 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
254 GNUNET_CONFIGURATION_destroy (cfg);
255 GNUNET_CONFIGURATION_destroy (cfgDiffs);
256 GNUNET_free (diffsFileName);
265 unsigned long long l;
267 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
269 if (0 != strcmp ("b", c))
271 FPRINTF (stderr, "Got `%s'\n", c);
277 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
287 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
288 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
293 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
295 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
300 if (0 != strcmp (c, "10"))
309 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
315 if (0 != strcmp (c, "/hello/world"))
317 #define HI "\\hello\\world"
318 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
328 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
341 static const char *want[] = {
350 check (void *data, const char *fn)
354 if (0 == strcmp (want[*idx], fn))
360 return GNUNET_SYSERR;
364 testConfigFilenames ()
370 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
379 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
387 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
394 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
402 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
409 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
417 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
425 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
437 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
453 main (int argc, char *argv[])
455 int failureCount = 0;
458 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
459 cfg = GNUNET_CONFIGURATION_create ();
460 GNUNET_assert (cfg != NULL);
462 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
464 FPRINTF (stderr, "%s", "Failed to parse configuration file\n");
465 GNUNET_CONFIGURATION_destroy (cfg);
468 failureCount += testConfig ();
469 if (failureCount > 0)
472 failureCount = testConfigFilenames ();
473 if (failureCount > 0)
476 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
478 FPRINTF (stderr, "%s", "Failed to write configuration file\n");
479 GNUNET_CONFIGURATION_destroy (cfg);
482 GNUNET_CONFIGURATION_destroy (cfg);
483 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
485 cfg = GNUNET_CONFIGURATION_create ();
487 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
490 GNUNET_CONFIGURATION_destroy (cfg);
494 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
497 GNUNET_CONFIGURATION_destroy (cfg);
500 if (0 != strcmp (c, "YES"))
504 GNUNET_CONFIGURATION_destroy (cfg);
509 GNUNET_CONFIGURATION_destroy (cfg);
511 /* Testing configuration diffs */
512 cfg_default = GNUNET_CONFIGURATION_create ();
513 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_default, NULL))
516 GNUNET_CONFIGURATION_destroy (cfg_default);
520 /* Nothing changed in the new configuration */
521 failureCount += checkDiffs (cfg_default, EDIT_NOTHING);
523 /* Modify all entries of the last section */
524 failureCount += checkDiffs (cfg_default, EDIT_SECTION);
526 /* Add a new section */
527 failureCount += checkDiffs (cfg_default, ADD_NEW_SECTION);
529 /* Add a new entry to the last section */
530 failureCount += checkDiffs (cfg_default, ADD_NEW_ENTRY);
532 /* Modify all entries in the configuration */
533 failureCount += checkDiffs (cfg_default, EDIT_ALL);
535 GNUNET_CONFIGURATION_destroy (cfg_default);
538 if (failureCount != 0)
540 FPRINTF (stderr, "Test failed: %u\n", failureCount);