diff options
-rw-r--r-- | src/util/configuration.c | 111 | ||||
-rw-r--r-- | src/util/test_configuration.c | 116 |
2 files changed, 72 insertions, 155 deletions
diff --git a/src/util/configuration.c b/src/util/configuration.c index 250f9d3d5..bdcc231e1 100644 --- a/src/util/configuration.c +++ b/src/util/configuration.c | |||
@@ -98,14 +98,15 @@ struct GNUNET_CONFIGURATION_Handle | |||
98 | 98 | ||
99 | }; | 99 | }; |
100 | 100 | ||
101 | |||
101 | /** | 102 | /** |
102 | * Used for diffing a configuration object against | 103 | * Used for diffing a configuration object against |
103 | * the default one | 104 | * the default one |
104 | */ | 105 | */ |
105 | struct GNUNNET_CONFIGURATION_Diff_Handle | 106 | struct DiffHandle |
106 | { | 107 | { |
107 | struct GNUNET_CONFIGURATION_Handle *cfgNew; | 108 | const struct GNUNET_CONFIGURATION_Handle *cfgDefault; |
108 | struct GNUNET_CONFIGURATION_Handle *cfgDiff; | 109 | const struct GNUNET_CONFIGURATION_Handle *cfgDiff; |
109 | }; | 110 | }; |
110 | 111 | ||
111 | 112 | ||
@@ -473,88 +474,29 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
473 | 474 | ||
474 | 475 | ||
475 | /** | 476 | /** |
476 | * A callback function, compares entries from two configurations (default against a new configuration) | 477 | * A callback function, compares entries from two configurations |
477 | * and write the diffs in a diff-configuration object (the callback object). | 478 | * (default against a new configuration) and write the diffs in a |
478 | * @param cls the diff configuration (ConfigurationDiffHandle*) | 479 | * diff-configuration object (the callback object). |
480 | * | ||
481 | * @param cls the diff configuration (struct DiffHandle*) | ||
479 | * @param section section for the value (of the default conf.) | 482 | * @param section section for the value (of the default conf.) |
480 | * @param option option name of the value (of the default conf.) | 483 | * @param option option name of the value (of the default conf.) |
481 | * @param value value to copy (of the default conf.) | 484 | * @param value value to copy (of the default conf.) |
482 | */ | 485 | */ |
483 | void | 486 | static void |
484 | compareEntries (void *cls, | 487 | compareEntries (void *cls, |
485 | const char *section, const char *option, const char *value) | 488 | const char *section, const char *option, const char *value) |
486 | { | 489 | { |
487 | struct ConfigSection *secNew; | 490 | struct DiffHandle *dh = cls; |
488 | struct ConfigEntry *entNew; | 491 | struct ConfigEntry *entNew; |
489 | struct GNUNNET_CONFIGURATION_Diff_Handle *cfgDiff = | 492 | |
490 | (struct GNUNNET_CONFIGURATION_Diff_Handle *) cls; | 493 | entNew = findEntry (dh->cfgDefault, section, option); |
491 | 494 | if ( (entNew != NULL) && | |
492 | secNew = findSection (cfgDiff->cfgNew, section); | 495 | (strcmp (entNew->val, value) == 0) ) |
493 | entNew = findEntry (cfgDiff->cfgNew, section, option); | 496 | return; |
494 | if (secNew && strcmp (entNew->val, value) != 0) | 497 | GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, |
495 | { | 498 | option, |
496 | /* Value in the new configuration has been changed */ | 499 | value); |
497 | /* Add the changed value to the diff configuration object */ | ||
498 | struct ConfigEntry *diffEntry = NULL; | ||
499 | struct ConfigSection *diffSection = NULL; | ||
500 | |||
501 | diffSection = cfgDiff->cfgDiff->sections; | ||
502 | if (diffSection == NULL) | ||
503 | { | ||
504 | /* First section */ | ||
505 | diffSection = GNUNET_malloc (sizeof (struct ConfigSection)); | ||
506 | memcpy (diffSection, secNew, sizeof (struct ConfigSection)); | ||
507 | cfgDiff->cfgDiff->sections = diffSection; | ||
508 | diffSection->entries = NULL; | ||
509 | diffSection->next = NULL; | ||
510 | } | ||
511 | else | ||
512 | { | ||
513 | while ((strcmp (diffSection->name, secNew->name) != 0) | ||
514 | && (diffSection->next != NULL)) | ||
515 | { | ||
516 | diffSection = diffSection->next; | ||
517 | } | ||
518 | if (strcmp (diffSection->name, secNew->name) != 0) | ||
519 | { | ||
520 | /* Section not found in diffs configuration */ | ||
521 | diffSection->next = | ||
522 | GNUNET_malloc (sizeof (struct ConfigSection)); | ||
523 | memcpy (diffSection->next, secNew, | ||
524 | sizeof (struct ConfigSection)); | ||
525 | diffSection->next->entries = NULL; | ||
526 | diffSection->next->next = NULL; | ||
527 | } | ||
528 | else | ||
529 | { | ||
530 | diffEntry = diffSection->entries; | ||
531 | } | ||
532 | } | ||
533 | |||
534 | if (diffEntry == NULL) | ||
535 | { | ||
536 | /* First Entry */ | ||
537 | diffEntry = GNUNET_malloc (sizeof (struct ConfigEntry)); | ||
538 | memcpy (diffEntry, entNew, sizeof (struct ConfigEntry)); | ||
539 | if (diffSection->next == NULL) | ||
540 | /* The first Entry of the first Section */ | ||
541 | diffSection->entries = diffEntry; | ||
542 | else | ||
543 | /* The first entry of the non-first Section */ | ||
544 | diffSection->next->entries = diffEntry; | ||
545 | diffEntry->next = NULL; | ||
546 | } | ||
547 | else | ||
548 | { | ||
549 | while (diffEntry->next != NULL) | ||
550 | { | ||
551 | diffEntry = diffEntry->next; | ||
552 | } | ||
553 | diffEntry->next = GNUNET_malloc (sizeof (struct ConfigEntry)); | ||
554 | memcpy (diffEntry->next, entNew, sizeof (struct ConfigEntry)); | ||
555 | diffEntry->next->next = NULL; | ||
556 | } | ||
557 | } | ||
558 | } | 500 | } |
559 | 501 | ||
560 | 502 | ||
@@ -566,21 +508,18 @@ compareEntries (void *cls, | |||
566 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | 508 | * @return GNUNET_OK on success, GNUNET_SYSERR on error |
567 | */ | 509 | */ |
568 | int | 510 | int |
569 | GNUNET_CONFIGURATION_write_diffs (struct GNUNET_CONFIGURATION_Handle | 511 | GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle |
570 | *cfgDefault, | 512 | *cfgDefault, |
571 | struct GNUNET_CONFIGURATION_Handle *cfgNew, | 513 | const struct GNUNET_CONFIGURATION_Handle *cfgNew, |
572 | const char *filename) | 514 | const char *filename) |
573 | { | 515 | { |
574 | int ret; | 516 | int ret; |
575 | struct GNUNNET_CONFIGURATION_Diff_Handle diffHandle; | 517 | struct DiffHandle diffHandle; |
576 | diffHandle.cfgDiff = GNUNET_CONFIGURATION_create (); | ||
577 | diffHandle.cfgDiff->sections = NULL; | ||
578 | diffHandle.cfgNew = cfgNew; | ||
579 | GNUNET_CONFIGURATION_iterate (cfgDefault, compareEntries, &diffHandle); | ||
580 | 518 | ||
519 | diffHandle.cfgDiff = GNUNET_CONFIGURATION_create (); | ||
520 | diffHandle.cfgDefault = cfgDefault; | ||
521 | GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle); | ||
581 | ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename); | 522 | ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename); |
582 | |||
583 | /* Housekeeping */ | ||
584 | GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff); | 523 | GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff); |
585 | return ret; | 524 | return ret; |
586 | } | 525 | } |
diff --git a/src/util/test_configuration.c b/src/util/test_configuration.c index 3cd076f0f..4c7f69a14 100644 --- a/src/util/test_configuration.c +++ b/src/util/test_configuration.c | |||
@@ -173,84 +173,62 @@ testConfigFilenames () | |||
173 | int | 173 | int |
174 | main (int argc, char *argv[]) | 174 | main (int argc, char *argv[]) |
175 | { | 175 | { |
176 | // int failureCount = 0; | 176 | int failureCount = 0; |
177 | // char *c; | 177 | char *c; |
178 | 178 | ||
179 | // GNUNET_log_setup ("test_configuration", "WARNING", NULL); | 179 | GNUNET_log_setup ("test_configuration", "WARNING", NULL); |
180 | // cfg = GNUNET_CONFIGURATION_create (); | 180 | cfg = GNUNET_CONFIGURATION_create (); |
181 | // GNUNET_assert (cfg != NULL); | 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 != | 182 | if (GNUNET_OK != |
238 | GNUNET_CONFIGURATION_parse (cfgDefault, | 183 | GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf")) |
239 | "src/util/test_configuration_data.conf")) | ||
240 | { | 184 | { |
241 | fprintf (stderr, "Failed to parse configuration file\n"); | 185 | fprintf (stderr, "Failed to parse configuration file\n"); |
242 | GNUNET_CONFIGURATION_destroy (cfgDefault); | 186 | GNUNET_CONFIGURATION_destroy (cfg); |
243 | return 1; | 187 | return 1; |
244 | } | 188 | } |
245 | if (GNUNET_OK != | 189 | failureCount += testConfig (); |
246 | GNUNET_CONFIGURATION_parse (cfgNew, | 190 | failureCount += 2 * testConfigFilenames (); |
247 | "/Users/soufi/Desktop/test_configuration_data.conf")) | 191 | |
192 | if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf")) | ||
248 | { | 193 | { |
249 | fprintf (stderr, "Failed to parse configuration file\n"); | 194 | fprintf (stderr, "Failed to write configuration file\n"); |
250 | GNUNET_CONFIGURATION_destroy (cfgNew); | 195 | GNUNET_CONFIGURATION_destroy (cfg); |
251 | return 1; | 196 | return 1; |
252 | } | 197 | } |
198 | GNUNET_CONFIGURATION_destroy (cfg); | ||
199 | GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf")); | ||
253 | 200 | ||
254 | GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfgNew, "/tmp/safey.conf"); | 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 | } | ||
255 | return 0; | 233 | return 0; |
256 | } | 234 | } |