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"
30 /* Directives used for testing Configuration Diffs */
31 #define DEBUG GNUNET_NO
32 #define CONFIGURATION_DIFFS_PATH "/tmp/gnunet-diff.conf"
33 #define EDIT_NOTHING 0
34 #define EDIT_SECTION 1
36 #define ADD_NEW_SECTION 3
37 #define ADD_NEW_ENTRY 4
38 #define REMOVE_SECTION 5
39 #define REMOVE_ENTRY 6
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,
76 const char *section, const char *option, const char *value)
78 struct DiffsCBData *cbData = cls;
79 int cbOption = cbData->callBackOption;
84 if (cbData->section == NULL)
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,
95 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
97 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
106 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section,
107 "new-key", "new-value");
108 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
109 "new-key", "new-value");
118 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
120 if (ret == GNUNET_SYSERR || diffValue == NULL
121 || strcmp (diffValue, value) != 0)
127 if (cbData->section == NULL || strcmp (cbData->section, section) != 0)
129 cbData->section = section;
130 printf ("\nSection: %s\n", section);
132 printf ("%s = %s\n", option, value);
140 static struct GNUNET_CONFIGURATION_Handle *
141 editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
143 struct DiffsCBData diffsCB;
144 initDiffsCBData (&diffsCB);
145 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
152 diffsCB.callBackOption = option;
154 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
159 case ADD_NEW_SECTION:
165 GNUNET_asprintf (&key, "key%d", i);
166 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
168 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs,
182 return diffsCB.cfgDiffs;
186 * Checking configuration diffs
189 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
191 struct GNUNET_CONFIGURATION_Handle *cfg, *cfgDiffs;
192 struct DiffsCBData cbData;
193 initDiffsCBData (&cbData);
196 cfg = GNUNET_CONFIGURATION_create ();
197 GNUNET_CONFIGURATION_load (cfg, NULL);
199 /* Modify configuration and save it */
200 cfgDiffs = editConfiguration (cfg, option);
201 GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg,
202 CONFIGURATION_DIFFS_PATH);
203 GNUNET_CONFIGURATION_destroy (cfg);
205 /* Compare the dumped configuration with modifications done */
206 cfg = GNUNET_CONFIGURATION_create ();
207 GNUNET_CONFIGURATION_parse (cfg, CONFIGURATION_DIFFS_PATH);
208 cbData.callBackOption = COMPARE;
209 cbData.cfgDiffs = cfgDiffs;
210 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
211 if ((ret = cbData.status) == 1)
214 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
217 cbData.cfgDiffs = cfg;
218 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
219 if ((ret = cbData.status) == 1)
221 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
225 cbData.section = NULL;
226 cbData.callBackOption = PRINT;
227 printf ("\nExpected Diffs:\n");
228 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
229 cbData.section = NULL;
230 printf ("\nActual Diffs:\n");
231 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
233 GNUNET_CONFIGURATION_destroy (cfg);
234 GNUNET_CONFIGURATION_destroy (cfgDiffs);
243 unsigned long long l;
246 GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
248 if (0 != strcmp ("b", c))
250 fprintf (stderr, "Got `%s'\n", c);
255 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
260 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
261 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
263 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
265 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
267 if (0 != strcmp (c, "10"))
275 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
278 if (0 != strcmp (c, "/hello/world"))
280 #define HI "\\hello\\world"
281 if (strstr (c, HI) != c + strlen (c) - strlen (HI))
292 static const char *want[] = {
301 check (void *data, const char *fn)
305 if (0 == strcmp (want[*idx], fn))
310 return GNUNET_SYSERR;
314 testConfigFilenames ()
319 if (3 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
327 GNUNET_CONFIGURATION_remove_value_filename (cfg,
329 "test", "/File Name"))
333 GNUNET_CONFIGURATION_remove_value_filename (cfg,
335 "test", "/File Name"))
338 GNUNET_CONFIGURATION_remove_value_filename (cfg,
344 GNUNET_CONFIGURATION_append_value_filename (cfg,
349 GNUNET_CONFIGURATION_append_value_filename (cfg,
355 GNUNET_CONFIGURATION_append_value_filename (cfg,
361 GNUNET_CONFIGURATION_append_value_filename (cfg,
370 if (4 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
382 main (int argc, char *argv[])
384 int failureCount = 0;
387 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
388 cfg = GNUNET_CONFIGURATION_create ();
389 GNUNET_assert (cfg != NULL);
391 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
393 fprintf (stderr, "Failed to parse configuration file\n");
394 GNUNET_CONFIGURATION_destroy (cfg);
397 failureCount += testConfig ();
398 failureCount += 2 * testConfigFilenames ();
400 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
402 fprintf (stderr, "Failed to write configuration file\n");
403 GNUNET_CONFIGURATION_destroy (cfg);
406 GNUNET_CONFIGURATION_destroy (cfg);
407 GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
409 cfg = GNUNET_CONFIGURATION_create ();
411 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
414 GNUNET_CONFIGURATION_destroy (cfg);
418 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM",
420 || (0 != strcmp (c, "YES")))
422 GNUNET_CONFIGURATION_destroy (cfg);
427 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
429 || (0 != strcmp (c, "/var/lib/gnunet/")))
431 GNUNET_CONFIGURATION_destroy (cfg);
435 GNUNET_CONFIGURATION_destroy (cfg);
437 /* Testing configuration diffs */
438 cfgDefault = GNUNET_CONFIGURATION_create ();
439 if (GNUNET_CONFIGURATION_load (cfgDefault, NULL) == GNUNET_SYSERR)
441 printf ("\n Error! \n");
444 /* Nothing changed in the new configuration */
445 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
447 /* Modify all entries of the last section */
448 failureCount += checkDiffs (cfgDefault, EDIT_SECTION);
450 /* Add a new section */
451 failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);
453 /* Add a new entry to the last section */
454 failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);
456 /* Modify all entries in the configuration */
457 failureCount += checkDiffs (cfgDefault, EDIT_ALL);
459 GNUNET_CONFIGURATION_destroy (cfgDefault);
461 if (failureCount != 0)
463 fprintf (stderr, "Test failed: %u\n", failureCount);