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