summaryrefslogtreecommitdiff
path: root/src/util/test_configuration.c
blob: b1a446f86c4406c82289818ba5aea8d43b4d4c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
     This file is part of GNUnet.
     (C) 2003, 2004, 2005, 2006, 2007 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/
/**
 * @file util/test_configuration.c
 * @brief Test that the configuration module works.
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_configuration_lib.h"
#include "gnunet_disk_lib.h"

#define DEBUG GNUNET_EXTRA_LOGGING

/* Test Configuration Diffs Options */
enum
{
  EDIT_NOTHING,
  EDIT_SECTION,
  EDIT_ALL,
  ADD_NEW_SECTION,
  ADD_NEW_ENTRY,
  REMOVE_SECTION,
  REMOVE_ENTRY,
  COMPARE
#if DEBUG
      , PRINT
#endif
};

static struct GNUNET_CONFIGURATION_Handle *cfg;
static struct GNUNET_CONFIGURATION_Handle *cfgDefault;

struct DiffsCBData
{
  struct GNUNET_CONFIGURATION_Handle *cfg;
  struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
  const char *section;
  int callBackOption;
  int status;
};


static void
initDiffsCBData (struct DiffsCBData *cbData)
{
  cbData->section = NULL;
  cbData->cfg = NULL;
  cbData->cfgDiffs = NULL;
  cbData->callBackOption = -1;
  cbData->status = 0;
}


/**
 * callback function for modifying
 * and comparing configuration
*/
static void
diffsCallBack (void *cls, const char *section, const char *option,
               const char *value)
{
  struct DiffsCBData *cbData = cls;
  int cbOption = cbData->callBackOption;

  switch (cbOption)
  {
  case EDIT_SECTION:
    if (NULL == cbData->section)
      cbData->section = section;
    if (strcmp (cbData->section, section) == 0)
    {
      GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
                                             "new-value");
      GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
                                             "new-value");
    }
    break;
  case EDIT_ALL:
    GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
                                           "new-value");
    GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section, option,
                                           "new-value");
    break;
  case ADD_NEW_ENTRY:
  {
    static int hit = 0;

    if (hit == 0)
    {
      hit = 1;
      GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, "new-key",
                                             "new-value");
      GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
                                             "new-key", "new-value");
    }
    break;
  }
  case COMPARE:
  {
    int ret;
    char *diffValue;

    diffValue = NULL;
    ret =
        GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
                                               option, &diffValue);
    if (NULL != diffValue)
    {
      if (ret == GNUNET_SYSERR || strcmp (diffValue, value) != 0)
        cbData->status = 1;
    }
    else
      cbData->status = 1;
    GNUNET_free_non_null (diffValue);
    break;
  }
#if 0
  case PRINT:
    if (NULL == cbData->section)
    {
      cbData->section = section;
      printf ("\nSection: %s\n", section);
    }
    else if (strcmp (cbData->section, section) != 0)
    {
      cbData->section = section;
      printf ("\nSection: %s\n", section);
    }
    printf ("%s = %s\n", option, value);
#endif
  default:
    break;
  }
}


static struct GNUNET_CONFIGURATION_Handle *
editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
{
  struct DiffsCBData diffsCB;

  initDiffsCBData (&diffsCB);
  diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();

  switch (option)
  {
  case EDIT_SECTION:
  case EDIT_ALL:
  case ADD_NEW_ENTRY:
    diffsCB.callBackOption = option;
    diffsCB.cfg = cfg;
    GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
    break;
  case EDIT_NOTHING:
    /* Do nothing */
    break;
  case ADD_NEW_SECTION:
  {
    int i;
    char *key;

    for (i = 0; i < 5; i++)
    {
      GNUNET_asprintf (&key, "key%d", i);
      GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
                                             "new-value");
      GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs, "new-section",
                                             key, "new-value");
      GNUNET_free (key);
    }
    break;
  }
  case REMOVE_SECTION:
    break;
  case REMOVE_ENTRY:
    break;
  default:
    break;
  }

  return diffsCB.cfgDiffs;
}

/**
 * Checking configuration diffs
 */
static int
checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
{
  struct GNUNET_CONFIGURATION_Handle *cfg;
  struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
  struct DiffsCBData cbData;
  int ret;
  char *diffsFileName;

  initDiffsCBData (&cbData);

  cfg = GNUNET_CONFIGURATION_create ();
  /* load defaults */
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));

  /* Modify configuration and save it */
  cfgDiffs = editConfiguration (cfg, option);
  diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
  if (diffsFileName == NULL)
  {
    GNUNET_break (0);
    GNUNET_CONFIGURATION_destroy (cfg);
    GNUNET_CONFIGURATION_destroy (cfgDiffs);
    return 1;
  }
  GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName);
  GNUNET_CONFIGURATION_destroy (cfg);

  /* Compare the dumped configuration with modifications done */
  cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
  remove (diffsFileName);
  cbData.callBackOption = COMPARE;
  cbData.cfgDiffs = cfgDiffs;
  GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
  if (1 == (ret = cbData.status))
  {
    FPRINTF (stderr, "%s", 
             "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
    goto housekeeping;
  }
  cbData.cfgDiffs = cfg;
  GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
  if ((ret = cbData.status) == 1)
    FPRINTF (stderr, "%s", 
             "Incorrect Configuration Diffs: Data may be missing in diffs\n");

housekeeping:
#if 0
  cbData.section = NULL;
  cbData.callBackOption = PRINT;
  printf ("\nExpected Diffs:\n");
  GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
  cbData.section = NULL;
  printf ("\nActual Diffs:\n");
  GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
#endif
  GNUNET_CONFIGURATION_destroy (cfg);
  GNUNET_CONFIGURATION_destroy (cfgDiffs);
  GNUNET_free (diffsFileName);
  return ret;
}


static int
testConfig ()
{
  char *c;
  unsigned long long l;

  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
    return 1;
  if (0 != strcmp ("b", c))
  {
    FPRINTF (stderr, "Got `%s'\n", c);
    GNUNET_free (c);
    return 2;
  }
  GNUNET_free (c);
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_number (cfg, "test", "five", &l))
  {
    GNUNET_break (0);
    return 3;
  }
  if (5 != l)
  {
    GNUNET_break (0);
    return 4;
  }
  GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
  if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
  {
    GNUNET_break (0);
    return 5;
  }
  GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
  {
    GNUNET_break (0);
    return 6;
  }
  if (0 != strcmp (c, "10"))
  {
    GNUNET_free (c);
    GNUNET_break (0);
    return 7;
  }
  GNUNET_free (c);

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
  {
    GNUNET_break (0);
    return 8;
  }
#ifndef MINGW
  if (0 != strcmp (c, "/hello/world"))
#else
#define HI "\\hello\\world"
  if (strstr (c, HI) != c + strlen (c) - strlen (HI))
#endif
  {
    GNUNET_break (0);
    GNUNET_free (c);
    return 9;
  }
  GNUNET_free (c);

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_size (cfg, "last", "size", &l))
  {
    GNUNET_break (0);
    return 10;
  }
  if (l != 512 * 1024)
  {
    GNUNET_break (0);
    return 11;
  }
  return 0;
}

static const char *want[] = {
  "/Hello",
  "/File Name",
  "/World",
  NULL,
  NULL,
};

static int
check (void *data, const char *fn)
{
  int *idx = data;

  if (0 == strcmp (want[*idx], fn))
  {
    (*idx)++;
    return GNUNET_OK;
  }
  GNUNET_break (0);
  return GNUNET_SYSERR;
}

static int
testConfigFilenames ()
{
  int idx;

  idx = 0;
  if (3 !=
      GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
                                                    &check, &idx))
  {
    GNUNET_break (0);
    return 8;
  }
  if (idx != 3)
    return 16;
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
                                                  "/File Name"))
  {
    GNUNET_break (0);
    return 24;
  }

  if (GNUNET_NO !=
      GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
                                                  "/File Name"))
  {
    GNUNET_break (0);
    return 32;
  }
  if (GNUNET_NO !=
      GNUNET_CONFIGURATION_remove_value_filename (cfg, "FILENAMES", "test",
                                                  "Stuff"))
  {
    GNUNET_break (0);
    return 40;
  }

  if (GNUNET_NO !=
      GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
                                                  "/Hello"))
  {
    GNUNET_break (0);
    return 48;
  }
  if (GNUNET_NO !=
      GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
                                                  "/World"))
  {
    GNUNET_break (0);
    return 56;
  }

  if (GNUNET_YES !=
      GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
                                                  "/File 1"))
  {
    GNUNET_break (0);
    return 64;
  }

  if (GNUNET_YES !=
      GNUNET_CONFIGURATION_append_value_filename (cfg, "FILENAMES", "test",
                                                  "/File 2"))
  {
    GNUNET_break (0);
    return 72;
  }

  idx = 0;
  want[1] = "/World";
  want[2] = "/File 1";
  want[3] = "/File 2";
  if (4 !=
      GNUNET_CONFIGURATION_iterate_value_filenames (cfg, "FILENAMES", "test",
                                                    &check, &idx))
  {
    GNUNET_break (0);
    return 80;
  }
  if (idx != 4)
  {
    GNUNET_break (0);
    return 88;
  }
  return 0;
}


int
main (int argc, char *argv[])
{
  int failureCount = 0;
  char *c;

  GNUNET_log_setup ("test_configuration", "WARNING", NULL);
  cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_assert (cfg != NULL);
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
  {
    FPRINTF (stderr, "%s",  "Failed to parse configuration file\n");
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }
  failureCount += testConfig ();
  if (failureCount > 0)
    goto error;

  failureCount = testConfigFilenames ();
  if (failureCount > 0)
    goto error;

  if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
  {
    FPRINTF (stderr, "%s",  "Failed to write configuration file\n");
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }
  GNUNET_CONFIGURATION_destroy (cfg);
  GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));

  cfg = GNUNET_CONFIGURATION_create ();
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
  {
    GNUNET_break (0);
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
  {
    GNUNET_break (0);
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }
  if (0 != strcmp (c, "YES"))
  {
    GNUNET_break (0);
    GNUNET_free (c);
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }

  GNUNET_free (c);
  GNUNET_CONFIGURATION_destroy (cfg);

  /* Testing configuration diffs */
  cfgDefault = GNUNET_CONFIGURATION_create ();
  if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
  {
    GNUNET_break (0);
    GNUNET_CONFIGURATION_destroy (cfgDefault);
    return 1;
  }

  /* Nothing changed in the new configuration */
  failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);

  /* Modify all entries of the last section */
  failureCount += checkDiffs (cfgDefault, EDIT_SECTION);

  /* Add a new section */
  failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);

  /* Add a new entry to the last section */
  failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);

  /* Modify all entries in the configuration */
  failureCount += checkDiffs (cfgDefault, EDIT_ALL);

  GNUNET_CONFIGURATION_destroy (cfgDefault);

error:
  if (failureCount != 0)
  {
    FPRINTF (stderr, "Test failed: %u\n", failureCount);
    return 1;
  }
  return 0;
}