7d87d03a882252df13c3f9c9d141c7281d95f48e
[oweals/gnunet.git] / src / util / test_configuration.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2005, 2006, 2007 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file util/test_configuration.c
22  * @brief Test that the configuration module works.
23  * @author Christian Grothoff
24  */
25
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_disk_lib.h"
30
31 #define DEBUG GNUNET_NO
32
33 /* Test Configuration Diffs Options */
34 enum
35 {
36   EDIT_NOTHING,
37   EDIT_SECTION,
38   EDIT_ALL,
39   ADD_NEW_SECTION,
40   ADD_NEW_ENTRY,
41   REMOVE_SECTION,
42   REMOVE_ENTRY,
43   COMPARE
44 #if DEBUG
45     , PRINT
46 #endif
47 };
48
49 static struct GNUNET_CONFIGURATION_Handle *cfg;
50 static struct GNUNET_CONFIGURATION_Handle *cfgDefault;
51
52 struct DiffsCBData
53 {
54   struct GNUNET_CONFIGURATION_Handle *cfg;
55   struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
56   const char *section;
57   int callBackOption;
58   int status;
59 };
60
61
62 static void
63 initDiffsCBData (struct DiffsCBData *cbData)
64 {
65   cbData->section = NULL;
66   cbData->cfg = NULL;
67   cbData->cfgDiffs = NULL;
68   cbData->callBackOption = -1;
69   cbData->status = 0;
70 }
71
72
73 /**
74  * callback function for modifying
75  * and comparing configuration
76 */
77 static void
78 diffsCallBack (void *cls,
79                const char *section, const char *option, const char *value)
80 {
81   struct DiffsCBData *cbData = cls;
82   int cbOption = cbData->callBackOption;
83
84   switch (cbOption)
85     {
86     case EDIT_SECTION:
87       if (NULL == cbData->section)
88         cbData->section = section;
89       if (strcmp (cbData->section, section) == 0)
90         {
91           GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
92                                                  "new-value");
93           GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
94                                                  option, "new-value");
95         }
96       break;
97     case EDIT_ALL:
98       GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
99                                              "new-value");
100       GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
101                                              option, "new-value");
102       break;
103     case ADD_NEW_ENTRY:
104       {
105         static int hit = 0;
106         if (hit == 0)
107           {
108             hit = 1;
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");
113           }
114         break;
115       }
116     case COMPARE:
117       {
118         int ret;
119         char *diffValue;
120
121         diffValue = NULL;
122         ret =
123           GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
124                                                  option, &diffValue);
125         if (NULL != diffValue)
126           {
127             if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
128               cbData->status = 1;
129           }
130         else
131           cbData->status = 1;
132         GNUNET_free_non_null (diffValue);
133         break;
134       }
135 #if DEBUG
136     case PRINT:
137       if (NULL == cbData->section)
138         {
139           cbData->section = section;
140           printf ("\nSection: %s\n", section);
141         }
142       else if (strcmp (cbData->section, section) != 0)
143         {
144           cbData->section = section;
145           printf ("\nSection: %s\n", section);
146         }
147       printf ("%s = %s\n", option, value);
148 #endif
149     default:
150       break;
151     }
152 }
153
154
155 static struct GNUNET_CONFIGURATION_Handle *
156 editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
157 {
158   struct DiffsCBData diffsCB;
159   initDiffsCBData (&diffsCB);
160   diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
161
162   switch (option)
163     {
164     case EDIT_SECTION:
165     case EDIT_ALL:
166     case ADD_NEW_ENTRY:
167       diffsCB.callBackOption = option;
168       diffsCB.cfg = cfg;
169       GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
170       break;
171     case EDIT_NOTHING:
172       /* Do nothing */
173       break;
174     case ADD_NEW_SECTION:
175       {
176         int i;
177         char *key;
178         for (i = 0; i < 5; i++)
179           {
180             GNUNET_asprintf (&key, "key%d", i);
181             GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
182                                                    "new-value");
183             GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs,
184                                                    "new-section", key,
185                                                    "new-value");
186             GNUNET_free (key);
187           }
188         break;
189       }
190     case REMOVE_SECTION:
191       break;
192     case REMOVE_ENTRY:
193       break;
194     default:
195       break;
196     }
197
198   return diffsCB.cfgDiffs;
199 }
200
201 /**
202  * Checking configuration diffs
203  */
204 static int
205 checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
206 {
207   struct GNUNET_CONFIGURATION_Handle *cfg;
208   struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
209   struct DiffsCBData cbData;
210   int ret;
211   char *diffsFileName;
212
213   initDiffsCBData (&cbData);
214
215   cfg = GNUNET_CONFIGURATION_create ();
216   /* load defaults */
217   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
218
219   /* Modify configuration and save it */
220   cfgDiffs = editConfiguration (cfg, option);
221   diffsFileName =
222     GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
223   if (diffsFileName == NULL)
224     return 1;
225   GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName);
226   GNUNET_CONFIGURATION_destroy (cfg);
227
228   /* Compare the dumped configuration with modifications done */
229   cfg = GNUNET_CONFIGURATION_create ();
230   GNUNET_CONFIGURATION_parse (cfg, diffsFileName);
231   remove (diffsFileName);
232   cbData.callBackOption = COMPARE;
233   cbData.cfgDiffs = cfgDiffs;
234   GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
235   if (1 == (ret = cbData.status))
236     {
237       fprintf (stderr,
238                "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
239       goto housekeeping;
240     }
241   cbData.cfgDiffs = cfg;
242   GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
243   if ((ret = cbData.status) == 1)
244     fprintf (stderr,
245              "Incorrect Configuration Diffs: Data may be missing in diffs\n");
246
247 housekeeping:
248 #if DEBUG
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);
256 #endif
257   GNUNET_CONFIGURATION_destroy (cfg);
258   GNUNET_CONFIGURATION_destroy (cfgDiffs);
259   GNUNET_free (diffsFileName);
260   return ret;
261 }
262
263
264 static int
265 testConfig ()
266 {
267   char *c;
268   unsigned long long l;
269
270   if (GNUNET_OK !=
271       GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
272     return 1;
273   if (0 != strcmp ("b", c))
274     {
275       fprintf (stderr, "Got `%s'\n", c);
276       GNUNET_free (c);
277       return 2;
278     }
279   GNUNET_free (c);
280   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
281                                                           "test", "five", &l))
282     return 3;
283   if (5 != l)
284     return 4;
285   GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
286   if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
287     return 5;
288   GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
289   if (GNUNET_OK !=
290       GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
291     return 6;
292   if (0 != strcmp (c, "10"))
293     {
294       GNUNET_free (c);
295       return 7;
296     }
297   GNUNET_free (c);
298
299   if (GNUNET_OK !=
300       GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
301     return 8;
302 #ifndef MINGW
303   if (0 != strcmp (c, "/hello/world"))
304 #else
305 #define HI "\\hello\\world"
306   if (strstr (c, HI) != c + strlen (c) - strlen (HI))
307 #endif
308     {
309       GNUNET_free (c);
310       return 9;
311     }
312   GNUNET_free (c);
313
314   return 0;
315 }
316
317 static const char *want[] = {
318   "/Hello",
319   "/File Name",
320   "/World",
321   NULL,
322   NULL,
323 };
324
325 static int
326 check (void *data, const char *fn)
327 {
328   int *idx = data;
329
330   if (0 == strcmp (want[*idx], fn))
331     {
332       (*idx)++;
333       return GNUNET_OK;
334     }
335   return GNUNET_SYSERR;
336 }
337
338 static int
339 testConfigFilenames ()
340 {
341   int idx;
342
343   idx = 0;
344   if (3 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
345                                                          "FILENAMES",
346                                                          "test",
347                                                          &check, &idx))
348     return 8;
349   if (idx != 3)
350     return 16;
351   if (GNUNET_OK !=
352       GNUNET_CONFIGURATION_remove_value_filename (cfg,
353                                                   "FILENAMES",
354                                                   "test", "/File Name"))
355     return 24;
356
357   if (GNUNET_NO !=
358       GNUNET_CONFIGURATION_remove_value_filename (cfg,
359                                                   "FILENAMES",
360                                                   "test", "/File Name"))
361     return 32;
362   if (GNUNET_NO !=
363       GNUNET_CONFIGURATION_remove_value_filename (cfg,
364                                                   "FILENAMES",
365                                                   "test", "Stuff"))
366     return 40;
367
368   if (GNUNET_NO !=
369       GNUNET_CONFIGURATION_append_value_filename (cfg,
370                                                   "FILENAMES",
371                                                   "test", "/Hello"))
372     return 48;
373   if (GNUNET_NO !=
374       GNUNET_CONFIGURATION_append_value_filename (cfg,
375                                                   "FILENAMES",
376                                                   "test", "/World"))
377     return 56;
378
379   if (GNUNET_YES !=
380       GNUNET_CONFIGURATION_append_value_filename (cfg,
381                                                   "FILENAMES",
382                                                   "test", "/File 1"))
383     return 64;
384
385   if (GNUNET_YES !=
386       GNUNET_CONFIGURATION_append_value_filename (cfg,
387                                                   "FILENAMES",
388                                                   "test", "/File 2"))
389     return 72;
390
391   idx = 0;
392   want[1] = "/World";
393   want[2] = "/File 1";
394   want[3] = "/File 2";
395   if (4 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
396                                                          "FILENAMES",
397                                                          "test",
398                                                          &check, &idx))
399     return 80;
400   if (idx != 4)
401     return 88;
402   return 0;
403 }
404
405
406 int
407 main (int argc, char *argv[])
408 {
409   int failureCount = 0;
410   char *c;
411
412   GNUNET_log_setup ("test_configuration", "WARNING", NULL);
413   cfg = GNUNET_CONFIGURATION_create ();
414   GNUNET_assert (cfg != NULL);
415   if (GNUNET_OK !=
416       GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
417     {
418       fprintf (stderr, "Failed to parse configuration file\n");
419       GNUNET_CONFIGURATION_destroy (cfg);
420       return 1;
421     }
422   failureCount += testConfig ();
423   failureCount += 2 * testConfigFilenames ();
424
425   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
426     {
427       fprintf (stderr, "Failed to write configuration file\n");
428       GNUNET_CONFIGURATION_destroy (cfg);
429       return 1;
430     }
431   GNUNET_CONFIGURATION_destroy (cfg);
432   GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
433
434   cfg = GNUNET_CONFIGURATION_create ();
435   if (GNUNET_OK !=
436       GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
437     {
438       GNUNET_break (0);
439       GNUNET_CONFIGURATION_destroy (cfg);
440       return 1;
441     }
442   if ((GNUNET_OK !=
443        GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM",
444                                               &c))
445       || (0 != strcmp (c, "YES")))
446     {
447       GNUNET_CONFIGURATION_destroy (cfg);
448       return 1;
449     }
450   GNUNET_free (c);
451   if ((GNUNET_OK !=
452        GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
453                                               &c))
454       || (0 != strcmp (c, "/var/lib/gnunet/")))
455     {
456       GNUNET_CONFIGURATION_destroy (cfg);
457       return 1;
458     }
459   GNUNET_free (c);
460   GNUNET_CONFIGURATION_destroy (cfg);
461
462   /* Testing configuration diffs */
463   cfgDefault = GNUNET_CONFIGURATION_create ();
464   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
465     {
466       GNUNET_break (0);
467       GNUNET_CONFIGURATION_destroy (cfgDefault);
468       return 1;
469     }
470
471   /* Nothing changed in the new configuration */
472   failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
473
474   /* Modify all entries of the last section */
475   failureCount += checkDiffs (cfgDefault, EDIT_SECTION);
476
477   /* Add a new section */
478   failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);
479
480   /* Add a new entry to the last section */
481   failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);
482
483   /* Modify all entries in the configuration */
484   failureCount += checkDiffs (cfgDefault, EDIT_ALL);
485
486   GNUNET_CONFIGURATION_destroy (cfgDefault);
487
488   if (failureCount != 0)
489     {
490       fprintf (stderr, "Test failed: %u\n", failureCount);
491       return 1;
492     }
493   return 0;
494 }