keep addr
[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
30 static struct GNUNET_CONFIGURATION_Handle *cfg;
31
32 static int
33 testConfig ()
34 {
35   char *c;
36   unsigned long long l;
37
38   if (GNUNET_OK !=
39       GNUNET_CONFIGURATION_get_value_string (cfg, "test", "b", &c))
40     return 1;
41   if (0 != strcmp ("b", c))
42     {
43       fprintf (stderr, "Got `%s'\n", c);
44       GNUNET_free (c);
45       return 2;
46     }
47   GNUNET_free (c);
48   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
49                                                           "test", "five", &l))
50     return 3;
51   if (5 != l)
52     return 4;
53   GNUNET_CONFIGURATION_set_value_string (cfg, "more", "c", "YES");
54   if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "more", "c"))
55     return 5;
56   GNUNET_CONFIGURATION_set_value_number (cfg, "NUMBERS", "TEN", 10);
57   if (GNUNET_OK !=
58       GNUNET_CONFIGURATION_get_value_string (cfg, "NUMBERS", "TEN", &c))
59     return 6;
60   if (0 != strcmp (c, "10"))
61     {
62       GNUNET_free (c);
63       return 7;
64     }
65   GNUNET_free (c);
66
67   if (GNUNET_OK !=
68       GNUNET_CONFIGURATION_get_value_filename (cfg, "last", "test", &c))
69     return 8;
70   if (0 != strcmp (c, "/hello/world"))
71     {
72       GNUNET_free (c);
73       return 9;
74     }
75   GNUNET_free (c);
76
77   return 0;
78 }
79
80 static const char *want[] = {
81   "/Hello",
82   "/File Name",
83   "/World",
84   NULL,
85   NULL,
86 };
87
88 static int
89 check (void *data, const char *fn)
90 {
91   int *idx = data;
92
93   if (0 == strcmp (want[*idx], fn))
94     {
95       (*idx)++;
96       return GNUNET_OK;
97     }
98   return GNUNET_SYSERR;
99 }
100
101 static int
102 testConfigFilenames ()
103 {
104   int idx;
105
106   idx = 0;
107   if (3 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
108                                                          "FILENAMES",
109                                                          "test",
110                                                          &check, &idx))
111     return 8;
112   if (idx != 3)
113     return 16;
114   if (GNUNET_OK !=
115       GNUNET_CONFIGURATION_remove_value_filename (cfg,
116                                                   "FILENAMES",
117                                                   "test", "/File Name"))
118     return 24;
119
120   if (GNUNET_NO !=
121       GNUNET_CONFIGURATION_remove_value_filename (cfg,
122                                                   "FILENAMES",
123                                                   "test", "/File Name"))
124     return 32;
125   if (GNUNET_NO !=
126       GNUNET_CONFIGURATION_remove_value_filename (cfg,
127                                                   "FILENAMES",
128                                                   "test", "Stuff"))
129     return 40;
130
131   if (GNUNET_NO !=
132       GNUNET_CONFIGURATION_append_value_filename (cfg,
133                                                   "FILENAMES",
134                                                   "test", "/Hello"))
135     return 48;
136   if (GNUNET_NO !=
137       GNUNET_CONFIGURATION_append_value_filename (cfg,
138                                                   "FILENAMES",
139                                                   "test", "/World"))
140     return 56;
141
142   if (GNUNET_YES !=
143       GNUNET_CONFIGURATION_append_value_filename (cfg,
144                                                   "FILENAMES",
145                                                   "test", "/File 1"))
146     return 64;
147
148   if (GNUNET_YES !=
149       GNUNET_CONFIGURATION_append_value_filename (cfg,
150                                                   "FILENAMES",
151                                                   "test", "/File 2"))
152     return 72;
153
154   idx = 0;
155   want[1] = "/World";
156   want[2] = "/File 1";
157   want[3] = "/File 2";
158   if (4 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
159                                                          "FILENAMES",
160                                                          "test",
161                                                          &check, &idx))
162     return 80;
163   if (idx != 4)
164     return 88;
165   return 0;
166 }
167
168 int
169 main (int argc, char *argv[])
170 {
171   int failureCount = 0;
172   char *c;
173
174   GNUNET_log_setup ("test_configuration", "WARNING", NULL);
175   cfg = GNUNET_CONFIGURATION_create ();
176   GNUNET_assert (cfg != NULL);
177   if (GNUNET_OK !=
178       GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
179     {
180       fprintf (stderr, "Failed to parse configuration file\n");
181       GNUNET_CONFIGURATION_destroy (cfg);
182       return 1;
183     }
184   failureCount += testConfig ();
185   failureCount += 2 * testConfigFilenames ();
186
187   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
188     {
189       fprintf (stderr, "Failed to write configuration file\n");
190       GNUNET_CONFIGURATION_destroy (cfg);
191       return 1;
192     }
193   GNUNET_CONFIGURATION_destroy (cfg);
194   GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
195
196   cfg = GNUNET_CONFIGURATION_create ();
197   if (GNUNET_OK !=
198       GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
199     {
200       GNUNET_break (0);
201       GNUNET_CONFIGURATION_destroy (cfg);
202       return 1;
203     }
204   if ((GNUNET_OK !=
205        GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM",
206                                               &c))
207       || (0 != strcmp (c, "YES")))
208     {
209       GNUNET_CONFIGURATION_destroy (cfg);
210       return 1;
211     }
212   GNUNET_free (c);
213   if ((GNUNET_OK !=
214        GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
215                                               &c))
216       || (0 != strcmp (c, "/var/lib/gnunet/")))
217     {
218       GNUNET_CONFIGURATION_destroy (cfg);
219       return 1;
220     }
221   GNUNET_free (c);
222   GNUNET_CONFIGURATION_destroy (cfg);
223   if (failureCount != 0)
224     {
225       fprintf (stderr, "Test failed: %u\n", failureCount);
226       return 1;
227     }
228   return 0;
229 }