aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_configuration.c
blob: 2f37c684ce143a702e2e164bc703d55e69640f4a (plain) (blame)
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
/*
     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 2, 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"

static struct GNUNET_CONFIGURATION_Handle *cfg;

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))
    return 3;
  if (5 != l)
    return 4;
  GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
  if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
    return 5;
  GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
    return 6;
  if (0 != strcmp (c, "10"))
    {
      GNUNET_free (c);
      return 7;
    }
  GNUNET_free (c);

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
    return 8;
  if (0 != strcmp (c, "/hello/world"))
    {
      GNUNET_free (c);
      return 9;
    }
  GNUNET_free (c);

  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;
    }
  return GNUNET_SYSERR;
}

static int
testConfigFilenames ()
{
  int idx;

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

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

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

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

  if (GNUNET_YES !=
      GNUNET_CONFIGURATION_append_value_filename (cfg,
                                                  "FILENAMES",
                                                  "test", "/File 2"))
    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))
    return 80;
  if (idx != 4)
    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, "Failed to parse configuration file\n");
      GNUNET_CONFIGURATION_destroy (cfg);
      return 1;
    }
  failureCount += testConfig ();
  failureCount += 2 * testConfigFilenames ();

  if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
    {
      fprintf (stderr, "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))
      || (0 != strcmp (c, "YES")))
    {
      GNUNET_CONFIGURATION_destroy (cfg);
      return 1;
    }
  GNUNET_free (c);
  if ((GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
                                              &c))
      || (0 != strcmp (c, "/var/lib/gnunet/")))
    {
      GNUNET_CONFIGURATION_destroy (cfg);
      return 1;
    }
  GNUNET_free (c);
  GNUNET_CONFIGURATION_destroy (cfg);
  if (failureCount != 0)
    {
      fprintf (stderr, "Test failed: %u\n", failureCount);
      return 1;
    }
  return 0;
}