7c17ecf21a8ca5ae999a667c950f68899bd6261b
[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     {
225       GNUNET_CONFIGURATION_destroy (cfg);
226       GNUNET_CONFIGURATION_destroy (cfgDiffs);      
227       return 1;
228     }
229   GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName);
230   GNUNET_CONFIGURATION_destroy (cfg);
231
232   /* Compare the dumped configuration with modifications done */
233   cfg = GNUNET_CONFIGURATION_create ();
234   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
235   remove (diffsFileName);
236   cbData.callBackOption = COMPARE;
237   cbData.cfgDiffs = cfgDiffs;
238   GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
239   if (1 == (ret = cbData.status))
240     {
241       fprintf (stderr,
242                "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
243       goto housekeeping;
244     }
245   cbData.cfgDiffs = cfg;
246   GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
247   if ((ret = cbData.status) == 1)
248     fprintf (stderr,
249              "Incorrect Configuration Diffs: Data may be missing in diffs\n");
250
251 housekeeping:
252 #if DEBUG
253   cbData.section = NULL;
254   cbData.callBackOption = PRINT;
255   printf ("\nExpected Diffs:\n");
256   GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
257   cbData.section = NULL;
258   printf ("\nActual Diffs:\n");
259   GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
260 #endif
261   GNUNET_CONFIGURATION_destroy (cfg);
262   GNUNET_CONFIGURATION_destroy (cfgDiffs);
263   GNUNET_free (diffsFileName);
264   return ret;
265 }
266
267
268 static int
269 testConfig ()
270 {
271   char *c;
272   unsigned long long l;
273
274   if (GNUNET_OK !=
275       GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
276     return 1;
277   if (0 != strcmp ("b", c))
278     {
279       fprintf (stderr, "Got `%s'\n", c);
280       GNUNET_free (c);
281       return 2;
282     }
283   GNUNET_free (c);
284   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
285                                                           "test", "five", &l))
286     return 3;
287   if (5 != l)
288     return 4;
289   GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
290   if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
291     return 5;
292   GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
293   if (GNUNET_OK !=
294       GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
295     return 6;
296   if (0 != strcmp (c, "10"))
297     {
298       GNUNET_free (c);
299       return 7;
300     }
301   GNUNET_free (c);
302
303   if (GNUNET_OK !=
304       GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
305     return 8;
306 #ifndef MINGW
307   if (0 != strcmp (c, "/hello/world"))
308 #else
309 #define HI "\\hello\\world"
310   if (strstr (c, HI) != c + strlen (c) - strlen (HI))
311 #endif
312     {
313       GNUNET_free (c);
314       return 9;
315     }
316   GNUNET_free (c);
317
318   return 0;
319 }
320
321 static const char *want[] = {
322   "/Hello",
323   "/File Name",
324   "/World",
325   NULL,
326   NULL,
327 };
328
329 static int
330 check (void *data, const char *fn)
331 {
332   int *idx = data;
333
334   if (0 == strcmp (want[*idx], fn))
335     {
336       (*idx)++;
337       return GNUNET_OK;
338     }
339   return GNUNET_SYSERR;
340 }
341
342 static int
343 testConfigFilenames ()
344 {
345   int idx;
346
347   idx = 0;
348   if (3 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
349                                                          "FILENAMES",
350                                                          "test",
351                                                          &check, &idx))
352     return 8;
353   if (idx != 3)
354     return 16;
355   if (GNUNET_OK !=
356       GNUNET_CONFIGURATION_remove_value_filename (cfg,
357                                                   "FILENAMES",
358                                                   "test", "/File Name"))
359     return 24;
360
361   if (GNUNET_NO !=
362       GNUNET_CONFIGURATION_remove_value_filename (cfg,
363                                                   "FILENAMES",
364                                                   "test", "/File Name"))
365     return 32;
366   if (GNUNET_NO !=
367       GNUNET_CONFIGURATION_remove_value_filename (cfg,
368                                                   "FILENAMES",
369                                                   "test", "Stuff"))
370     return 40;
371
372   if (GNUNET_NO !=
373       GNUNET_CONFIGURATION_append_value_filename (cfg,
374                                                   "FILENAMES",
375                                                   "test", "/Hello"))
376     return 48;
377   if (GNUNET_NO !=
378       GNUNET_CONFIGURATION_append_value_filename (cfg,
379                                                   "FILENAMES",
380                                                   "test", "/World"))
381     return 56;
382
383   if (GNUNET_YES !=
384       GNUNET_CONFIGURATION_append_value_filename (cfg,
385                                                   "FILENAMES",
386                                                   "test", "/File 1"))
387     return 64;
388
389   if (GNUNET_YES !=
390       GNUNET_CONFIGURATION_append_value_filename (cfg,
391                                                   "FILENAMES",
392                                                   "test", "/File 2"))
393     return 72;
394
395   idx = 0;
396   want[1] = "/World";
397   want[2] = "/File 1";
398   want[3] = "/File 2";
399   if (4 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
400                                                          "FILENAMES",
401                                                          "test",
402                                                          &check, &idx))
403     return 80;
404   if (idx != 4)
405     return 88;
406   return 0;
407 }
408
409
410 int
411 main (int argc, char *argv[])
412 {
413   int failureCount = 0;
414   char *c;
415
416   GNUNET_log_setup ("test_configuration", "WARNING", NULL);
417   cfg = GNUNET_CONFIGURATION_create ();
418   GNUNET_assert (cfg != NULL);
419   if (GNUNET_OK !=
420       GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
421     {
422       fprintf (stderr, "Failed to parse configuration file\n");
423       GNUNET_CONFIGURATION_destroy (cfg);
424       return 1;
425     }
426   failureCount += testConfig ();
427   failureCount += 2 * testConfigFilenames ();
428
429   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
430     {
431       fprintf (stderr, "Failed to write configuration file\n");
432       GNUNET_CONFIGURATION_destroy (cfg);
433       return 1;
434     }
435   GNUNET_CONFIGURATION_destroy (cfg);
436   GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
437
438   cfg = GNUNET_CONFIGURATION_create ();
439   if (GNUNET_OK !=
440       GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
441     {
442       GNUNET_break (0);
443       GNUNET_CONFIGURATION_destroy (cfg);
444       return 1;
445     }
446   if ((GNUNET_OK !=
447        GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM",
448                                               &c))
449       || (0 != strcmp (c, "YES")))
450     {
451       GNUNET_CONFIGURATION_destroy (cfg);
452       return 1;
453     }
454   GNUNET_free (c);
455   if ((GNUNET_OK !=
456        GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
457                                               &c))
458       || (0 != strcmp (c, "/var/lib/gnunet/")))
459     {
460       GNUNET_CONFIGURATION_destroy (cfg);
461       return 1;
462     }
463   GNUNET_free (c);
464   GNUNET_CONFIGURATION_destroy (cfg);
465
466   /* Testing configuration diffs */
467   cfgDefault = GNUNET_CONFIGURATION_create ();
468   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
469     {
470       GNUNET_break (0);
471       GNUNET_CONFIGURATION_destroy (cfgDefault);
472       return 1;
473     }
474
475   /* Nothing changed in the new configuration */
476   failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
477
478   /* Modify all entries of the last section */
479   failureCount += checkDiffs (cfgDefault, EDIT_SECTION);
480
481   /* Add a new section */
482   failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);
483
484   /* Add a new entry to the last section */
485   failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);
486
487   /* Modify all entries in the configuration */
488   failureCount += checkDiffs (cfgDefault, EDIT_ALL);
489
490   GNUNET_CONFIGURATION_destroy (cfgDefault);
491
492   if (failureCount != 0)
493     {
494       fprintf (stderr, "Test failed: %u\n", failureCount);
495       return 1;
496     }
497   return 0;
498 }