3cd076f0f2f29966624b828124cc0bccd974a7bc
[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 #ifndef MINGW
71   if (0 != strcmp (c, "/hello/world"))
72 #else
73 #define HI "\\hello\\world"
74   if (strstr (c, HI) != c + strlen (c) - strlen (HI))
75 #endif
76     {
77       GNUNET_free (c);
78       return 9;
79     }
80   GNUNET_free (c);
81
82   return 0;
83 }
84
85 static const char *want[] = {
86   "/Hello",
87   "/File Name",
88   "/World",
89   NULL,
90   NULL,
91 };
92
93 static int
94 check (void *data, const char *fn)
95 {
96   int *idx = data;
97
98   if (0 == strcmp (want[*idx], fn))
99     {
100       (*idx)++;
101       return GNUNET_OK;
102     }
103   return GNUNET_SYSERR;
104 }
105
106 static int
107 testConfigFilenames ()
108 {
109   int idx;
110
111   idx = 0;
112   if (3 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
113                                                          "FILENAMES",
114                                                          "test",
115                                                          &check, &idx))
116     return 8;
117   if (idx != 3)
118     return 16;
119   if (GNUNET_OK !=
120       GNUNET_CONFIGURATION_remove_value_filename (cfg,
121                                                   "FILENAMES",
122                                                   "test", "/File Name"))
123     return 24;
124
125   if (GNUNET_NO !=
126       GNUNET_CONFIGURATION_remove_value_filename (cfg,
127                                                   "FILENAMES",
128                                                   "test", "/File Name"))
129     return 32;
130   if (GNUNET_NO !=
131       GNUNET_CONFIGURATION_remove_value_filename (cfg,
132                                                   "FILENAMES",
133                                                   "test", "Stuff"))
134     return 40;
135
136   if (GNUNET_NO !=
137       GNUNET_CONFIGURATION_append_value_filename (cfg,
138                                                   "FILENAMES",
139                                                   "test", "/Hello"))
140     return 48;
141   if (GNUNET_NO !=
142       GNUNET_CONFIGURATION_append_value_filename (cfg,
143                                                   "FILENAMES",
144                                                   "test", "/World"))
145     return 56;
146
147   if (GNUNET_YES !=
148       GNUNET_CONFIGURATION_append_value_filename (cfg,
149                                                   "FILENAMES",
150                                                   "test", "/File 1"))
151     return 64;
152
153   if (GNUNET_YES !=
154       GNUNET_CONFIGURATION_append_value_filename (cfg,
155                                                   "FILENAMES",
156                                                   "test", "/File 2"))
157     return 72;
158
159   idx = 0;
160   want[1] = "/World";
161   want[2] = "/File 1";
162   want[3] = "/File 2";
163   if (4 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
164                                                          "FILENAMES",
165                                                          "test",
166                                                          &check, &idx))
167     return 80;
168   if (idx != 4)
169     return 88;
170   return 0;
171 }
172
173 int
174 main (int argc, char *argv[])
175 {
176 //  int failureCount = 0;
177 //  char *c;
178
179 //  GNUNET_log_setup ("test_configuration", "WARNING", NULL);
180 //  cfg = GNUNET_CONFIGURATION_create ();
181 //  GNUNET_assert (cfg != NULL);
182 //  if (GNUNET_OK !=
183 //      GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
184 //    {
185 //      fprintf (stderr, "Failed to parse configuration file\n");
186 //      GNUNET_CONFIGURATION_destroy (cfg);
187 //      return 1;
188 //    }
189 //  failureCount += testConfig ();
190 //  failureCount += 2 * testConfigFilenames ();
191 //
192 //  if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
193 //    {
194 //      fprintf (stderr, "Failed to write configuration file\n");
195 //      GNUNET_CONFIGURATION_destroy (cfg);
196 //      return 1;
197 //    }
198 //  GNUNET_CONFIGURATION_destroy (cfg);
199 //  GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));
200 //
201 //  cfg = GNUNET_CONFIGURATION_create ();
202 //  if (GNUNET_OK !=
203 //      GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
204 //    {
205 //      GNUNET_break (0);
206 //      GNUNET_CONFIGURATION_destroy (cfg);
207 //      return 1;
208 //    }
209 //  if ((GNUNET_OK !=
210 //       GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM",
211 //                                              &c))
212 //      || (0 != strcmp (c, "YES")))
213 //    {
214 //      GNUNET_CONFIGURATION_destroy (cfg);
215 //      return 1;
216 //    }
217 //  GNUNET_free (c);
218 //  if ((GNUNET_OK !=
219 //       GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
220 //                                              &c))
221 //      || (0 != strcmp (c, "/var/lib/gnunet/")))
222 //    {
223 //      GNUNET_CONFIGURATION_destroy (cfg);
224 //      return 1;
225 //    }
226 //  GNUNET_free (c);
227 //  GNUNET_CONFIGURATION_destroy (cfg);
228 //  if (failureCount != 0)
229 //    {
230 //      fprintf (stderr, "Test failed: %u\n", failureCount);
231 //      return 1;
232 //    }
233 //  
234   struct GNUNET_CONFIGURATION_Handle *cfgDefault =
235     GNUNET_CONFIGURATION_create ();
236   struct GNUNET_CONFIGURATION_Handle *cfgNew = GNUNET_CONFIGURATION_create ();
237   if (GNUNET_OK !=
238       GNUNET_CONFIGURATION_parse (cfgDefault,
239                                   "src/util/test_configuration_data.conf"))
240     {
241       fprintf (stderr, "Failed to parse configuration file\n");
242       GNUNET_CONFIGURATION_destroy (cfgDefault);
243       return 1;
244     }
245   if (GNUNET_OK !=
246       GNUNET_CONFIGURATION_parse (cfgNew,
247                                   "/Users/soufi/Desktop/test_configuration_data.conf"))
248     {
249       fprintf (stderr, "Failed to parse configuration file\n");
250       GNUNET_CONFIGURATION_destroy (cfgNew);
251       return 1;
252     }
253
254   GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfgNew, "/tmp/safey.conf");
255   return 0;
256 }