aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_configuration.c
diff options
context:
space:
mode:
authorSafey A.Halim <safey.allah@gmail.com>2009-11-16 14:23:54 +0000
committerSafey A.Halim <safey.allah@gmail.com>2009-11-16 14:23:54 +0000
commit2387be3a722b64e2594d6300eb3882ed6b1ebf8c (patch)
tree1a6ce99ad76e554fe8e677348bdd74518f2592bf /src/util/test_configuration.c
parent6801b39f5b650ce9719ebf1475ca6d084f40c8c9 (diff)
downloadgnunet-2387be3a722b64e2594d6300eb3882ed6b1ebf8c.tar.gz
gnunet-2387be3a722b64e2594d6300eb3882ed6b1ebf8c.zip
Adding test cases for "Configuration Diffs writing"
Diffstat (limited to 'src/util/test_configuration.c')
-rw-r--r--src/util/test_configuration.c279
1 files changed, 256 insertions, 23 deletions
diff --git a/src/util/test_configuration.c b/src/util/test_configuration.c
index 4c7f69a14..637c8ed00 100644
--- a/src/util/test_configuration.c
+++ b/src/util/test_configuration.c
@@ -27,7 +27,214 @@
27#include "gnunet_common.h" 27#include "gnunet_common.h"
28#include "gnunet_configuration_lib.h" 28#include "gnunet_configuration_lib.h"
29 29
30/* Directives used for testing Configuration Diffs */
31#define DEBUG GNUNET_NO
32#define CONFIGURATION_DIFFS_PATH "/tmp/gnunet-diff.conf"
33#define EDIT_NOTHING 0
34#define EDIT_SECTION 1
35#define EDIT_ALL 2
36#define ADD_NEW_SECTION 3
37#define ADD_NEW_ENTRY 4
38#define REMOVE_SECTION 5
39#define REMOVE_ENTRY 6
40#define COMPARE 7
41
42#if DEBUG
43#define PRINT 8
44#endif
45
30static struct GNUNET_CONFIGURATION_Handle *cfg; 46static struct GNUNET_CONFIGURATION_Handle *cfg;
47static struct GNUNET_CONFIGURATION_Handle *cfgDefault;
48
49struct DiffsCBData
50{
51 struct GNUNET_CONFIGURATION_Handle *cfg;
52 struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
53 const char *section;
54 int callBackOption;
55 int status;
56};
57
58
59static void
60initDiffsCBData (struct DiffsCBData *cbData)
61{
62 cbData->section = NULL;
63 cbData->cfg = NULL;
64 cbData->cfgDiffs = NULL;
65 cbData->callBackOption = -1;
66 cbData->status = 0;
67}
68
69
70/**
71 * callback function for modifying
72 * and comparing configuration
73*/
74static void
75diffsCallBack (void *cls,
76 const char *section, const char *option, const char *value)
77{
78 struct DiffsCBData *cbData = cls;
79 int cbOption = cbData->callBackOption;
80
81 switch (cbOption)
82 {
83 case EDIT_SECTION:
84 if (cbData->section == NULL)
85 cbData->section = section;
86 if (strcmp (cbData->section, section) == 0)
87 {
88 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
89 "new-value");
90 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
91 option, "new-value");
92 }
93 break;
94 case EDIT_ALL:
95 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section, option,
96 "new-value");
97 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
98 option, "new-value");
99 break;
100 case ADD_NEW_ENTRY:
101 {
102 static int hit = 0;
103 if (hit == 0)
104 {
105 hit = 1;
106 GNUNET_CONFIGURATION_set_value_string (cbData->cfg, section,
107 "new-key", "new-value");
108 GNUNET_CONFIGURATION_set_value_string (cbData->cfgDiffs, section,
109 "new-key", "new-value");
110 }
111 break;
112 }
113 case COMPARE:
114 {
115 int ret;
116 char *diffValue;
117 ret =
118 GNUNET_CONFIGURATION_get_value_string (cbData->cfgDiffs, section,
119 option, &diffValue);
120 if (ret == GNUNET_SYSERR || diffValue == NULL
121 || strcmp (diffValue, value) != 0)
122 cbData->status = 1;
123 break;
124 }
125#if DEBUG
126 case PRINT:
127 if (cbData->section == NULL || strcmp (cbData->section, section) != 0)
128 {
129 cbData->section = section;
130 printf ("\nSection: %s\n", section);
131 }
132 printf ("%s = %s\n", option, value);
133#endif
134 default:
135 break;
136 }
137}
138
139
140static struct GNUNET_CONFIGURATION_Handle *
141editConfiguration (struct GNUNET_CONFIGURATION_Handle *cfg, int option)
142{
143 struct DiffsCBData diffsCB;
144 initDiffsCBData (&diffsCB);
145 diffsCB.cfgDiffs = GNUNET_CONFIGURATION_create ();
146
147 switch (option)
148 {
149 case EDIT_SECTION:
150 case EDIT_ALL:
151 case ADD_NEW_ENTRY:
152 diffsCB.callBackOption = option;
153 diffsCB.cfg = cfg;
154 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &diffsCB);
155 break;
156 case EDIT_NOTHING:
157 /* Do nothing */
158 break;
159 case ADD_NEW_SECTION:
160 {
161 int i = 0;
162 char *key;
163 for (; i < 5; i++)
164 {
165 GNUNET_asprintf (&key, "key%d", i);
166 GNUNET_CONFIGURATION_set_value_string (cfg, "new-section", key,
167 "new-value");
168 GNUNET_CONFIGURATION_set_value_string (diffsCB.cfgDiffs,
169 "new-section", key,
170 "new-value");
171 }
172 break;
173 }
174 case REMOVE_SECTION:
175 break;
176 case REMOVE_ENTRY:
177 break;
178 default:
179 break;
180 }
181
182 return diffsCB.cfgDiffs;
183}
184
185/**
186 * Checking configuration diffs
187 */
188static int
189checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
190{
191 struct GNUNET_CONFIGURATION_Handle *cfg, *cfgDiffs;
192 struct DiffsCBData cbData;
193 initDiffsCBData (&cbData);
194 int ret = 0;
195
196 cfg = GNUNET_CONFIGURATION_create ();
197 GNUNET_CONFIGURATION_load (cfg, NULL);
198
199 /* Modify configuration and save it */
200 cfgDiffs = editConfiguration (cfg, option);
201 GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg,
202 CONFIGURATION_DIFFS_PATH);
203 GNUNET_CONFIGURATION_destroy (cfg);
204
205 /* Compare the dumped configuration with modifications done */
206 cfg = GNUNET_CONFIGURATION_create ();
207 GNUNET_CONFIGURATION_parse (cfg, CONFIGURATION_DIFFS_PATH);
208 cbData.callBackOption = COMPARE;
209 cbData.cfgDiffs = cfgDiffs;
210 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
211 if ((ret = cbData.status) == 1)
212 {
213 fprintf (stderr,
214 "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
215 goto housekeeping;
216 }
217 cbData.cfgDiffs = cfg;
218 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
219 if ((ret = cbData.status) == 1)
220 fprintf (stderr,
221 "Incorrect Configuration Diffs: Data may be missing in diffs\n");
222
223housekeeping:
224#if DEBUG
225 cbData.section = NULL;
226 cbData.callBackOption = PRINT;
227 printf ("\nExpected Diffs:\n");
228 GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
229 cbData.section = NULL;
230 printf ("\nActual Diffs:\n");
231 GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
232#endif
233 GNUNET_CONFIGURATION_destroy (cfg);
234 GNUNET_CONFIGURATION_destroy (cfgDiffs);
235 return ret;
236}
237
31 238
32static int 239static int
33testConfig () 240testConfig ()
@@ -46,7 +253,7 @@ testConfig ()
46 } 253 }
47 GNUNET_free (c); 254 GNUNET_free (c);
48 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, 255 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
49 "test", "five", &l)) 256 "test", "five", &l))
50 return 3; 257 return 3;
51 if (5 != l) 258 if (5 != l)
52 return 4; 259 return 4;
@@ -110,50 +317,50 @@ testConfigFilenames ()
110 317
111 idx = 0; 318 idx = 0;
112 if (3 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg, 319 if (3 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
113 "FILENAMES", 320 "FILENAMES",
114 "test", 321 "test",
115 &check, &idx)) 322 &check, &idx))
116 return 8; 323 return 8;
117 if (idx != 3) 324 if (idx != 3)
118 return 16; 325 return 16;
119 if (GNUNET_OK != 326 if (GNUNET_OK !=
120 GNUNET_CONFIGURATION_remove_value_filename (cfg, 327 GNUNET_CONFIGURATION_remove_value_filename (cfg,
121 "FILENAMES", 328 "FILENAMES",
122 "test", "/File Name")) 329 "test", "/File Name"))
123 return 24; 330 return 24;
124 331
125 if (GNUNET_NO != 332 if (GNUNET_NO !=
126 GNUNET_CONFIGURATION_remove_value_filename (cfg, 333 GNUNET_CONFIGURATION_remove_value_filename (cfg,
127 "FILENAMES", 334 "FILENAMES",
128 "test", "/File Name")) 335 "test", "/File Name"))
129 return 32; 336 return 32;
130 if (GNUNET_NO != 337 if (GNUNET_NO !=
131 GNUNET_CONFIGURATION_remove_value_filename (cfg, 338 GNUNET_CONFIGURATION_remove_value_filename (cfg,
132 "FILENAMES", 339 "FILENAMES",
133 "test", "Stuff")) 340 "test", "Stuff"))
134 return 40; 341 return 40;
135 342
136 if (GNUNET_NO != 343 if (GNUNET_NO !=
137 GNUNET_CONFIGURATION_append_value_filename (cfg, 344 GNUNET_CONFIGURATION_append_value_filename (cfg,
138 "FILENAMES", 345 "FILENAMES",
139 "test", "/Hello")) 346 "test", "/Hello"))
140 return 48; 347 return 48;
141 if (GNUNET_NO != 348 if (GNUNET_NO !=
142 GNUNET_CONFIGURATION_append_value_filename (cfg, 349 GNUNET_CONFIGURATION_append_value_filename (cfg,
143 "FILENAMES", 350 "FILENAMES",
144 "test", "/World")) 351 "test", "/World"))
145 return 56; 352 return 56;
146 353
147 if (GNUNET_YES != 354 if (GNUNET_YES !=
148 GNUNET_CONFIGURATION_append_value_filename (cfg, 355 GNUNET_CONFIGURATION_append_value_filename (cfg,
149 "FILENAMES", 356 "FILENAMES",
150 "test", "/File 1")) 357 "test", "/File 1"))
151 return 64; 358 return 64;
152 359
153 if (GNUNET_YES != 360 if (GNUNET_YES !=
154 GNUNET_CONFIGURATION_append_value_filename (cfg, 361 GNUNET_CONFIGURATION_append_value_filename (cfg,
155 "FILENAMES", 362 "FILENAMES",
156 "test", "/File 2")) 363 "test", "/File 2"))
157 return 72; 364 return 72;
158 365
159 idx = 0; 366 idx = 0;
@@ -161,15 +368,16 @@ testConfigFilenames ()
161 want[2] = "/File 1"; 368 want[2] = "/File 1";
162 want[3] = "/File 2"; 369 want[3] = "/File 2";
163 if (4 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg, 370 if (4 != GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
164 "FILENAMES", 371 "FILENAMES",
165 "test", 372 "test",
166 &check, &idx)) 373 &check, &idx))
167 return 80; 374 return 80;
168 if (idx != 4) 375 if (idx != 4)
169 return 88; 376 return 88;
170 return 0; 377 return 0;
171} 378}
172 379
380
173int 381int
174main (int argc, char *argv[]) 382main (int argc, char *argv[])
175{ 383{
@@ -208,7 +416,7 @@ main (int argc, char *argv[])
208 } 416 }
209 if ((GNUNET_OK != 417 if ((GNUNET_OK !=
210 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", 418 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM",
211 &c)) 419 &c))
212 || (0 != strcmp (c, "YES"))) 420 || (0 != strcmp (c, "YES")))
213 { 421 {
214 GNUNET_CONFIGURATION_destroy (cfg); 422 GNUNET_CONFIGURATION_destroy (cfg);
@@ -217,7 +425,7 @@ main (int argc, char *argv[])
217 GNUNET_free (c); 425 GNUNET_free (c);
218 if ((GNUNET_OK != 426 if ((GNUNET_OK !=
219 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME", 427 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME",
220 &c)) 428 &c))
221 || (0 != strcmp (c, "/var/lib/gnunet/"))) 429 || (0 != strcmp (c, "/var/lib/gnunet/")))
222 { 430 {
223 GNUNET_CONFIGURATION_destroy (cfg); 431 GNUNET_CONFIGURATION_destroy (cfg);
@@ -225,6 +433,31 @@ main (int argc, char *argv[])
225 } 433 }
226 GNUNET_free (c); 434 GNUNET_free (c);
227 GNUNET_CONFIGURATION_destroy (cfg); 435 GNUNET_CONFIGURATION_destroy (cfg);
436
437 /* Testing configuration diffs */
438 cfgDefault = GNUNET_CONFIGURATION_create ();
439 if (GNUNET_CONFIGURATION_load (cfgDefault, NULL) == GNUNET_SYSERR)
440 {
441 printf ("\n Error! \n");
442 }
443
444 /* Nothing changed in the new configuration */
445 failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);
446
447 /* Modify all entries of the last section */
448 failureCount += checkDiffs (cfgDefault, EDIT_SECTION);
449
450 /* Add a new section */
451 failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);
452
453 /* Add a new entry to the last section */
454 failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);
455
456 /* Modify all entries in the configuration */
457 failureCount += checkDiffs (cfgDefault, EDIT_ALL);
458
459 GNUNET_CONFIGURATION_destroy (cfgDefault);
460
228 if (failureCount != 0) 461 if (failureCount != 0)
229 { 462 {
230 fprintf (stderr, "Test failed: %u\n", failureCount); 463 fprintf (stderr, "Test failed: %u\n", failureCount);