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 2, 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_NO
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,
79 const char *section, const char *option, const char *value)
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");
109 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section,
110 "new-key", "new-value");
111 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
112 "new-key", "new-value");
123 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
125 if (NULL != diffValue)
127 if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
132 GNUNET_free_non_null (diffValue);
137 if (NULL == cbData->section)
139 cbData->section = section;
140 printf ("\nSection: %s\n", section);
142 else if (strcmp (cbData->section, section) != 0)
144 cbData->section = section;
145 printf ("\nSection: %s\n", section);
147 printf ("%s = %s\n", option, value);
155 static struct GNUNET_CONFIGURATION_Handle *
156 editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
158 struct DiffsCBData diffsCB;
159 initDiffsCBData (&diffsCB);
160 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
167 diffsCB.callBackOption = option;
169 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
174 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,
198 return diffsCB.cfgDiffs;
202 * Checking configuration diffs
205 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
207 struct GNUNET_CONFIGURATION_Handle *cfg;
208 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
209 struct DiffsCBData cbData;
213 initDiffsCBData (&cbData);
215 cfg = GNUNET_CONFIGURATION_create ();
217 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
219 /* Modify configuration and save it */
220 cfgDiffs = editConfiguration (cfg, option);
222 GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
223 if (diffsFileName == NULL)
226 GNUNET_CONFIGURATION_destroy (cfg);
227 GNUNET_CONFIGURATION_destroy (cfgDiffs);
230 GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName);
231 GNUNET_CONFIGURATION_destroy (cfg);
233 /* Compare the dumped configuration with modifications done */
234 cfg = GNUNET_CONFIGURATION_create ();
235 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
236 remove (diffsFileName);
237 cbData.callBackOption = COMPARE;
238 cbData.cfgDiffs = cfgDiffs;
239 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
240 if (1 == (ret = cbData.status))
243 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
246 cbData.cfgDiffs = cfg;
247 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
248 if ((ret = cbData.status) == 1)
250 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
254 cbData.section = NULL;
255 cbData.callBackOption = PRINT;
256 printf ("\nExpected Diffs:\n");
257 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
258 cbData.section = NULL;
259 printf ("\nActual Diffs:\n");
260 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
262 GNUNET_CONFIGURATION_destroy (cfg);
263 GNUNET_CONFIGURATION_destroy (cfgDiffs);
264 GNUNET_free (diffsFileName);
273 unsigned long long l;
276 GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
278 if (0 != strcmp ("b", c))
280 fprintf (stderr, "Got `%s'\n", c);
285 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
296 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
297 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
302 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
304 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
309 if (0 != strcmp (c, "10"))
318 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
324 if (0 != strcmp (c, "/hello/world"))
326 #define HI "\\hello\\world"
327 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
339 static const char *want[] = {
348 check (void *data, const char *fn)
352 if (0 == strcmp (want[*idx], fn))
358 return GNUNET_SYSERR;
362 testConfigFilenames ()
367 if (3 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
378 GNUNET_CONFIGURATION_remove_value_filename (cfg,
380 "test", "/File Name"))
387 GNUNET_CONFIGURATION_remove_value_filename (cfg,
389 "test", "/File Name"))
395 GNUNET_CONFIGURATION_remove_value_filename (cfg,
404 GNUNET_CONFIGURATION_append_value_filename (cfg,
412 GNUNET_CONFIGURATION_append_value_filename (cfg,
421 GNUNET_CONFIGURATION_append_value_filename (cfg,
430 GNUNET_CONFIGURATION_append_value_filename (cfg,
442 if (4 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
460 main (int argc, char *argv[])
462 int failureCount = 0;
465 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
466 cfg = GNUNET_CONFIGURATION_create ();
467 GNUNET_assert (cfg != NULL);
469 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
471 fprintf (stderr, "Failed to parse configuration file\n");
472 GNUNET_CONFIGURATION_destroy (cfg);
475 failureCount += testConfig ();
476 failureCount += 2 * testConfigFilenames ();
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",
498 || (0 != strcmp (c, "YES")))
501 GNUNET_CONFIGURATION_destroy (cfg);
505 GNUNET_CONFIGURATION_destroy (cfg);
507 /* Testing configuration diffs */
508 cfgDefault = GNUNET_CONFIGURATION_create ();
509 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
512 GNUNET_CONFIGURATION_destroy (cfgDefault);
516 /* Nothing changed in the new configuration */
517 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
519 /* Modify all entries of the last section */
520 failureCount += checkDiffs (cfgDefault, EDIT_SECTION);
522 /* Add a new section */
523 failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);
525 /* Add a new entry to the last section */
526 failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);
528 /* Modify all entries in the configuration */
529 failureCount += checkDiffs (cfgDefault, EDIT_ALL);
531 GNUNET_CONFIGURATION_destroy (cfgDefault);
533 if (failureCount != 0)
535 fprintf (stderr, "Test failed: %u\n", failureCount);