aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_configuration.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_configuration.c')
-rw-r--r--src/lib/util/test_configuration.c581
1 files changed, 581 insertions, 0 deletions
diff --git a/src/lib/util/test_configuration.c b/src/lib/util/test_configuration.c
new file mode 100644
index 000000000..75610fc74
--- /dev/null
+++ b/src/lib/util/test_configuration.c
@@ -0,0 +1,581 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007 GNUnet e.V.
4
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.
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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file util/test_configuration.c
22 * @brief Test that the configuration module works.
23 * @author Christian Grothoff
24 */
25
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29
30
31/* Test Configuration Diffs Options */
32enum
33{
34 EDIT_NOTHING,
35 EDIT_SECTION,
36 EDIT_ALL,
37 ADD_NEW_SECTION,
38 ADD_NEW_ENTRY,
39 REMOVE_SECTION,
40 REMOVE_ENTRY,
41 COMPARE,
42 PRINT
43};
44
45static struct GNUNET_CONFIGURATION_Handle *cfg;
46static struct GNUNET_CONFIGURATION_Handle *cfg_default;
47
48struct DiffsCBData
49{
50 struct GNUNET_CONFIGURATION_Handle *cfg;
51 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
52 const char *section;
53 int callBackOption;
54 int status;
55};
56
57
58static void
59initDiffsCBData (struct DiffsCBData *cbData)
60{
61 cbData->section = NULL;
62 cbData->cfg = NULL;
63 cbData->cfgDiffs = NULL;
64 cbData->callBackOption = -1;
65 cbData->status = 0;
66}
67
68
69/**
70 * callback function for modifying
71 * and comparing configuration
72 */
73static void
74diffsCallBack (void *cls,
75 const char *section,
76 const char *option,
77 const char *value)
78{
79 struct DiffsCBData *cbData = cls;
80 int cbOption = cbData->callBackOption;
81
82 if (0 == strcasecmp ("PATHS",
83 section))
84 return;
85 switch (cbOption)
86 {
87 case EDIT_SECTION:
88 if (NULL == cbData->section)
89 cbData->section = section;
90 if (strcmp (cbData->section, section) == 0)
91 {
92 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
93 "new-value");
94 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
95 "new-value");
96 }
97 break;
98 case EDIT_ALL:
99 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
100 "new-value");
101 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
102 "new-value");
103 break;
104 case ADD_NEW_ENTRY:
105 {
106 static int hit = 0;
107
108 if (hit == 0)
109 {
110 hit = 1;
111 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key",
112 "new-value");
113 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
114 "new-key", "new-value");
115 }
116 break;
117 }
118 case COMPARE:
119 {
120 int ret;
121 char *diffValue;
122
123 diffValue = NULL;
124 ret =
125 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs,
126 section,
127 option,
128 &diffValue);
129 if (NULL != diffValue)
130 {
131 if ( (ret == GNUNET_SYSERR) ||
132 (strcmp (diffValue, value) != 0) )
133 {
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
135 "Unexpected value %s in %s/%s\n",
136 diffValue,
137 section,
138 option);
139 cbData->status = 1;
140 }
141 }
142 else
143 {
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145 "Unexpected value %s in %s/%s\n",
146 diffValue,
147 section,
148 option);
149 cbData->status = 1;
150 }
151 GNUNET_free (diffValue);
152 break;
153 }
154
155#if 0
156 case PRINT:
157 if (NULL == cbData->section)
158 {
159 cbData->section = section;
160 printf ("\nSection: %s\n", section);
161 }
162 else if (strcmp (cbData->section, section) != 0)
163 {
164 cbData->section = section;
165 printf ("\nSection: %s\n", section);
166 }
167 printf ("%s = %s\n", option, value);
168#endif
169 default:
170 break;
171 }
172}
173
174
175static struct GNUNET_CONFIGURATION_Handle *
176editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg,
177 int option)
178{
179 struct DiffsCBData diffsCB;
180
181 initDiffsCBData (&diffsCB);
182 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
183
184 switch (option)
185 {
186 case EDIT_SECTION:
187 case EDIT_ALL:
188 case ADD_NEW_ENTRY:
189 diffsCB.callBackOption = option;
190 diffsCB.cfg = cfg;
191 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
192 break;
193
194 case EDIT_NOTHING:
195 /* Do nothing */
196 break;
197
198 case ADD_NEW_SECTION:
199 {
200 int i;
201 char *key;
202
203 for (i = 0; i < 5; i++)
204 {
205 GNUNET_asprintf (&key, "key%d", i);
206 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
207 "new-value");
208 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
209 key, "new-value");
210 GNUNET_free (key);
211 }
212 break;
213 }
214
215 case REMOVE_SECTION:
216 break;
217
218 case REMOVE_ENTRY:
219 break;
220
221 default:
222 break;
223 }
224
225 return diffsCB.cfgDiffs;
226}
227
228
229/**
230 * Checking configuration diffs
231 */
232static int
233checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfg_default, int option)
234{
235 struct GNUNET_CONFIGURATION_Handle *cfg;
236 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
237 struct DiffsCBData cbData;
238 int ret;
239 char *diffsFileName;
240
241 initDiffsCBData (&cbData);
242
243 cfg = GNUNET_CONFIGURATION_create ();
244 /* load defaults */
245 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));
246
247 /* Modify configuration and save it */
248 cfgDiffs = editConfiguration (cfg, option);
249 diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
250 if (diffsFileName == NULL)
251 {
252 GNUNET_break (0);
253 GNUNET_CONFIGURATION_destroy (cfg);
254 GNUNET_CONFIGURATION_destroy (cfgDiffs);
255 return 1;
256 }
257 GNUNET_CONFIGURATION_write_diffs (cfg_default, cfg, diffsFileName);
258 GNUNET_CONFIGURATION_destroy (cfg);
259
260 /* Compare the dumped configuration with modifications done */
261 cfg = GNUNET_CONFIGURATION_create ();
262 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
263 if (0 != remove (diffsFileName))
264 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove",
265 diffsFileName);
266 cbData.callBackOption = COMPARE;
267 cbData.cfgDiffs = cfgDiffs;
268 GNUNET_CONFIGURATION_iterate (cfg,
269 &diffsCallBack,
270 &cbData);
271 if (1 == (ret = cbData.status))
272 {
273 fprintf (stderr, "%s",
274 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
275 goto housekeeping;
276 }
277 cbData.cfgDiffs = cfg;
278 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
279 if ((ret = cbData.status) == 1)
280 fprintf (stderr, "%s",
281 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
282
283housekeeping:
284#if 0
285 cbData.section = NULL;
286 cbData.callBackOption = PRINT;
287 printf ("\nExpected Diffs:\n");
288 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
289 cbData.section = NULL;
290 printf ("\nActual Diffs:\n");
291 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
292#endif
293 GNUNET_CONFIGURATION_destroy (cfg);
294 GNUNET_CONFIGURATION_destroy (cfgDiffs);
295 GNUNET_free (diffsFileName);
296 return ret;
297}
298
299
300static int
301testConfig ()
302{
303 char *c;
304 unsigned long long l;
305
306 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
307 return 1;
308 if (0 != strcmp ("b", c))
309 {
310 fprintf (stderr, "Got `%s'\n", c);
311 GNUNET_free (c);
312 return 2;
313 }
314 GNUNET_free (c);
315 if (GNUNET_OK !=
316 GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
317 {
318 GNUNET_break (0);
319 return 3;
320 }
321 if (5 != l)
322 {
323 GNUNET_break (0);
324 return 4;
325 }
326 GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
327 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
328 {
329 GNUNET_break (0);
330 return 5;
331 }
332 GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
333 if (GNUNET_OK !=
334 GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
335 {
336 GNUNET_break (0);
337 return 6;
338 }
339 if (0 != strcmp (c, "10"))
340 {
341 GNUNET_free (c);
342 GNUNET_break (0);
343 return 7;
344 }
345 GNUNET_free (c);
346
347 if (GNUNET_OK !=
348 GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
349 {
350 GNUNET_break (0);
351 return 8;
352 }
353
354 if (0 != strcmp (c, "/hello/world"))
355 {
356 GNUNET_break (0);
357 GNUNET_free (c);
358 return 9;
359 }
360 GNUNET_free (c);
361
362 if (GNUNET_OK !=
363 GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
364 {
365 GNUNET_break (0);
366 return 10;
367 }
368 if (l != 512 * 1024)
369 {
370 GNUNET_break (0);
371 return 11;
372 }
373 return 0;
374}
375
376
377static const char *want[] = {
378 "/Hello",
379 "/File Name",
380 "/World",
381 NULL,
382 NULL,
383};
384
385static int
386check (void *data, const char *fn)
387{
388 int *idx = data;
389
390 if (0 == strcmp (want[*idx], fn))
391 {
392 (*idx)++;
393 return GNUNET_OK;
394 }
395 GNUNET_break (0);
396 return GNUNET_SYSERR;
397}
398
399
400static int
401testConfigFilenames ()
402{
403 int idx;
404
405 idx = 0;
406 if (3 !=
407 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
408 &check, &idx))
409 {
410 GNUNET_break (0);
411 return 8;
412 }
413 if (idx != 3)
414 return 16;
415 if (GNUNET_OK !=
416 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
417 "/File Name"))
418 {
419 GNUNET_break (0);
420 return 24;
421 }
422
423 if (GNUNET_NO !=
424 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
425 "/File Name"))
426 {
427 GNUNET_break (0);
428 return 32;
429 }
430 if (GNUNET_NO !=
431 GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
432 "Stuff"))
433 {
434 GNUNET_break (0);
435 return 40;
436 }
437
438 if (GNUNET_NO !=
439 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
440 "/Hello"))
441 {
442 GNUNET_break (0);
443 return 48;
444 }
445 if (GNUNET_NO !=
446 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
447 "/World"))
448 {
449 GNUNET_break (0);
450 return 56;
451 }
452
453 if (GNUNET_YES !=
454 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
455 "/File 1"))
456 {
457 GNUNET_break (0);
458 return 64;
459 }
460
461 if (GNUNET_YES !=
462 GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
463 "/File 2"))
464 {
465 GNUNET_break (0);
466 return 72;
467 }
468
469 idx = 0;
470 want[1] = "/World";
471 want[2] = "/File 1";
472 want[3] = "/File 2";
473 if (4 !=
474 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
475 &check, &idx))
476 {
477 GNUNET_break (0);
478 return 80;
479 }
480 if (idx != 4)
481 {
482 GNUNET_break (0);
483 return 88;
484 }
485 return 0;
486}
487
488
489int
490main (int argc, char *argv[])
491{
492 int failureCount = 0;
493 char *c;
494
495 GNUNET_log_setup ("test_configuration", "WARNING", NULL);
496 cfg = GNUNET_CONFIGURATION_create ();
497 GNUNET_assert (cfg != NULL);
498 if (GNUNET_OK !=
499 GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
500 {
501 fprintf (stderr, "%s", "Failed to parse configuration file\n");
502 GNUNET_CONFIGURATION_destroy (cfg);
503 return 1;
504 }
505 failureCount += testConfig ();
506 if (failureCount > 0)
507 goto error;
508
509 failureCount = testConfigFilenames ();
510 if (failureCount > 0)
511 goto error;
512
513 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
514 {
515 fprintf (stderr, "%s", "Failed to write configuration file\n");
516 GNUNET_CONFIGURATION_destroy (cfg);
517 return 1;
518 }
519 GNUNET_CONFIGURATION_destroy (cfg);
520 GNUNET_assert (0 == unlink ("/tmp/gnunet-test.conf"));
521
522 cfg = GNUNET_CONFIGURATION_create ();
523 if (GNUNET_OK !=
524 GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
525 {
526 GNUNET_break (0);
527 GNUNET_CONFIGURATION_destroy (cfg);
528 return 1;
529 }
530 if (GNUNET_OK !=
531 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
532 {
533 GNUNET_break (0);
534 GNUNET_CONFIGURATION_destroy (cfg);
535 return 1;
536 }
537 if (0 != strcmp (c, "YES"))
538 {
539 GNUNET_break (0);
540 GNUNET_free (c);
541 GNUNET_CONFIGURATION_destroy (cfg);
542 return 1;
543 }
544
545 GNUNET_free (c);
546 GNUNET_CONFIGURATION_destroy (cfg);
547
548 /* Testing configuration diffs */
549 cfg_default = GNUNET_CONFIGURATION_create ();
550 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_default, NULL))
551 {
552 GNUNET_break (0);
553 GNUNET_CONFIGURATION_destroy (cfg_default);
554 return 1;
555 }
556
557 /* Nothing changed in the new configuration */
558 failureCount += checkDiffs (cfg_default, EDIT_NOTHING);
559
560 /* Modify all entries of the last section */
561 failureCount += checkDiffs (cfg_default, EDIT_SECTION);
562
563 /* Add a new section */
564 failureCount += checkDiffs (cfg_default, ADD_NEW_SECTION);
565
566 /* Add a new entry to the last section */
567 failureCount += checkDiffs (cfg_default, ADD_NEW_ENTRY);
568
569 /* Modify all entries in the configuration */
570 failureCount += checkDiffs (cfg_default, EDIT_ALL);
571
572 GNUNET_CONFIGURATION_destroy (cfg_default);
573
574error:
575 if (failureCount != 0)
576 {
577 fprintf (stderr, "Test failed: %u\n", failureCount);
578 return 1;
579 }
580 return 0;
581}