diff options
-rw-r--r-- | src/include/gnunet_configuration_lib.h | 23 | ||||
-rw-r--r-- | src/util/Makefile.am | 6 | ||||
-rw-r--r-- | src/util/configuration.c | 101 | ||||
-rw-r--r-- | src/util/test_peer.c | 117 |
4 files changed, 247 insertions, 0 deletions
diff --git a/src/include/gnunet_configuration_lib.h b/src/include/gnunet_configuration_lib.h index f05d29421..5510ad095 100644 --- a/src/include/gnunet_configuration_lib.h +++ b/src/include/gnunet_configuration_lib.h | |||
@@ -45,6 +45,17 @@ extern "C" | |||
45 | */ | 45 | */ |
46 | struct GNUNET_CONFIGURATION_Handle; | 46 | struct GNUNET_CONFIGURATION_Handle; |
47 | 47 | ||
48 | |||
49 | /** | ||
50 | * Used for diffing a configuration object against | ||
51 | * the default one | ||
52 | */ | ||
53 | typedef struct { | ||
54 | struct GNUNET_CONFIGURATION_Handle* cfgNew; | ||
55 | struct GNUNET_CONFIGURATION_Handle* cfgDiff; | ||
56 | } GNUNNET_CONFIGURATION_Diff_Handle; | ||
57 | |||
58 | |||
48 | /** | 59 | /** |
49 | * Create a new configuration object. | 60 | * Create a new configuration object. |
50 | * @return fresh configuration object | 61 | * @return fresh configuration object |
@@ -105,6 +116,18 @@ int GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg, | |||
105 | int GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, | 116 | int GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, |
106 | const char *filename); | 117 | const char *filename); |
107 | 118 | ||
119 | /** | ||
120 | * Write only configuration entries that have been changed to configuration file | ||
121 | * @param cfgDefault default configuration | ||
122 | * @param cfgNew new configuration | ||
123 | * @param filename where to write the configuration diff between default and new | ||
124 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
125 | */ | ||
126 | int | ||
127 | GNUNET_CONFIGURATION_write_diffs( | ||
128 | struct GNUNET_CONFIGURATION_Handle *cfgDefault, | ||
129 | struct GNUNET_CONFIGURATION_Handle *cfgNew, | ||
130 | const char* filename); | ||
108 | 131 | ||
109 | /** | 132 | /** |
110 | * Test if there are configuration options that were | 133 | * Test if there are configuration options that were |
diff --git a/src/util/Makefile.am b/src/util/Makefile.am index b616de4d1..47744001b 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am | |||
@@ -126,6 +126,7 @@ check_PROGRAMS = \ | |||
126 | test_os_load \ | 126 | test_os_load \ |
127 | test_os_network \ | 127 | test_os_network \ |
128 | test_os_priority \ | 128 | test_os_priority \ |
129 | test_peer \ | ||
129 | test_plugin \ | 130 | test_plugin \ |
130 | test_program \ | 131 | test_program \ |
131 | test_pseudonym \ | 132 | test_pseudonym \ |
@@ -289,6 +290,11 @@ test_os_priority_SOURCES = \ | |||
289 | test_os_priority_LDADD = \ | 290 | test_os_priority_LDADD = \ |
290 | $(top_builddir)/src/util/libgnunetutil.la | 291 | $(top_builddir)/src/util/libgnunetutil.la |
291 | 292 | ||
293 | test_peer_SOURCES = \ | ||
294 | test_peer.c | ||
295 | test_peer_LDADD = \ | ||
296 | $(top_builddir)/src/util/libgnunetutil.la | ||
297 | |||
292 | test_plugin_SOURCES = \ | 298 | test_plugin_SOURCES = \ |
293 | test_plugin.c | 299 | test_plugin.c |
294 | test_plugin_LDADD = \ | 300 | test_plugin_LDADD = \ |
diff --git a/src/util/configuration.c b/src/util/configuration.c index c14645067..e35dfacfe 100644 --- a/src/util/configuration.c +++ b/src/util/configuration.c | |||
@@ -462,6 +462,107 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
462 | 462 | ||
463 | 463 | ||
464 | /** | 464 | /** |
465 | * A callback function, compares entries from two configurations (default against a new configuration) | ||
466 | * and write the diffs in a diff-configuration object (the callback object). | ||
467 | * @param cls the diff configuration (ConfigurationDiffHandle*) | ||
468 | * @param section section for the value (of the default conf.) | ||
469 | * @param option option name of the value (of the default conf.) | ||
470 | * @param value value to copy (of the default conf.) | ||
471 | */ | ||
472 | void | ||
473 | compareEntries( | ||
474 | void *cls, | ||
475 | const char *section, | ||
476 | const char *option, | ||
477 | const char *value) | ||
478 | { | ||
479 | struct ConfigSection *secNew; | ||
480 | struct ConfigEntry *entNew; | ||
481 | GNUNNET_CONFIGURATION_Diff_Handle* cfgDiff = (GNUNNET_CONFIGURATION_Diff_Handle*)cls; | ||
482 | |||
483 | secNew = findSection(cfgDiff->cfgNew, section); | ||
484 | entNew = findEntry(cfgDiff->cfgNew, section, option); | ||
485 | if (secNew && strcmp(entNew->val, value) != 0) { | ||
486 | /* Value in the new configuration has been changed */ | ||
487 | /* Add the changed value to the diff configuration object */ | ||
488 | struct ConfigEntry *diffEntry = NULL; | ||
489 | struct ConfigSection *diffSection = NULL; | ||
490 | |||
491 | diffSection = cfgDiff->cfgDiff->sections; | ||
492 | if (diffSection == NULL) { | ||
493 | /* First section */ | ||
494 | diffSection = GNUNET_malloc(sizeof(struct ConfigSection)); | ||
495 | memcpy(diffSection, secNew, sizeof(struct ConfigSection)); | ||
496 | cfgDiff->cfgDiff->sections = diffSection; | ||
497 | diffSection->entries = NULL; | ||
498 | diffSection->next = NULL; | ||
499 | } | ||
500 | else { | ||
501 | while ((strcmp(diffSection->name, secNew->name) != 0) && (diffSection->next != NULL)) { | ||
502 | diffSection = diffSection->next; | ||
503 | } | ||
504 | if (strcmp(diffSection->name, secNew->name) != 0) { | ||
505 | /* Section not found in diffs configuration */ | ||
506 | diffSection->next = GNUNET_malloc(sizeof(struct ConfigSection)); | ||
507 | memcpy(diffSection->next, secNew, sizeof(struct ConfigSection)); | ||
508 | diffSection->next->entries = NULL; | ||
509 | diffSection->next->next = NULL; | ||
510 | } | ||
511 | else { | ||
512 | diffEntry = diffSection->entries; | ||
513 | } | ||
514 | } | ||
515 | |||
516 | if (diffEntry == NULL) { | ||
517 | /* First Entry */ | ||
518 | diffEntry = GNUNET_malloc(sizeof(struct ConfigEntry)); | ||
519 | memcpy(diffEntry, entNew, sizeof(struct ConfigEntry)); | ||
520 | if (diffSection->next == NULL) | ||
521 | /* The first Entry of the first Section */ | ||
522 | diffSection->entries = diffEntry; | ||
523 | else | ||
524 | /* The first entry of the non-first Section */ | ||
525 | diffSection->next->entries = diffEntry; | ||
526 | diffEntry->next = NULL; | ||
527 | } | ||
528 | else { | ||
529 | while (diffEntry->next != NULL) { | ||
530 | diffEntry = diffEntry->next; | ||
531 | } | ||
532 | diffEntry->next = GNUNET_malloc(sizeof(struct ConfigEntry)); | ||
533 | memcpy(diffEntry->next, entNew, sizeof(struct ConfigEntry)); | ||
534 | diffEntry->next->next = NULL; | ||
535 | } | ||
536 | } | ||
537 | } | ||
538 | |||
539 | |||
540 | /** | ||
541 | * Write only configuration entries that have been changed to configuration file | ||
542 | * @param cfgDefault default configuration | ||
543 | * @param cfgNew new configuration | ||
544 | * @param filename where to write the configuration diff between default and new | ||
545 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
546 | */ | ||
547 | int | ||
548 | GNUNET_CONFIGURATION_write_diffs( | ||
549 | struct GNUNET_CONFIGURATION_Handle *cfgDefault, | ||
550 | struct GNUNET_CONFIGURATION_Handle *cfgNew, | ||
551 | const char* filename | ||
552 | ) | ||
553 | { | ||
554 | GNUNNET_CONFIGURATION_Diff_Handle *diffHandle = | ||
555 | GNUNET_malloc(sizeof(GNUNNET_CONFIGURATION_Diff_Handle)); | ||
556 | diffHandle->cfgDiff = GNUNET_CONFIGURATION_create(); | ||
557 | diffHandle->cfgDiff->sections = NULL; | ||
558 | diffHandle->cfgNew = cfgNew; | ||
559 | GNUNET_CONFIGURATION_iterate(cfgDefault, compareEntries, diffHandle); | ||
560 | |||
561 | return GNUNET_CONFIGURATION_write(diffHandle->cfgDiff, filename); | ||
562 | } | ||
563 | |||
564 | |||
565 | /** | ||
465 | * Set a configuration value that should be a string. | 566 | * Set a configuration value that should be a string. |
466 | * | 567 | * |
467 | * @param cfg configuration to update | 568 | * @param cfg configuration to update |
diff --git a/src/util/test_peer.c b/src/util/test_peer.c new file mode 100644 index 000000000..63610e420 --- /dev/null +++ b/src/util/test_peer.c | |||
@@ -0,0 +1,117 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 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_peer.c | ||
22 | * @brief testcase for peer.c | ||
23 | * @author Safey Mohammed | ||
24 | */ | ||
25 | |||
26 | #include "platform.h" | ||
27 | #include "gnunet_crypto_lib.h" | ||
28 | #include "gnunet_peer_lib.h" | ||
29 | |||
30 | #define NUMBER_OF_PEERS 10 | ||
31 | /*#define DEBUG*/ | ||
32 | |||
33 | /* Global Variables */ | ||
34 | static struct GNUNET_PeerIdentity** pidArr; /* A list of Peer ID's to play with */ | ||
35 | |||
36 | |||
37 | static void generatePeerIdList() | ||
38 | { | ||
39 | int i; /* Loop Index */ | ||
40 | |||
41 | pidArr = GNUNET_malloc(NUMBER_OF_PEERS * sizeof(struct GNUNET_PeerIdentity*)); | ||
42 | for (i = 0; i < NUMBER_OF_PEERS; i++ ) { | ||
43 | pidArr[i] = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity)); | ||
44 | GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &(pidArr[i]->hashPubKey)); | ||
45 | |||
46 | #ifdef DEBUG | ||
47 | { | ||
48 | struct GNUNET_CRYPTO_HashAsciiEncoded hashAsciiEncoded; | ||
49 | GNUNET_CRYPTO_hash_to_enc(&(pidArr[i]->hashPubKey), &hashAsciiEncoded); | ||
50 | printf ("I'm Peer: %s\n", (char*) &hashAsciiEncoded); | ||
51 | } | ||
52 | #endif | ||
53 | } | ||
54 | } | ||
55 | |||
56 | static void destroyPeerIdList() | ||
57 | { | ||
58 | int i; | ||
59 | for (i = 0; i < NUMBER_OF_PEERS; i++) { | ||
60 | GNUNET_free(pidArr[i]); | ||
61 | } | ||
62 | GNUNET_free(pidArr); | ||
63 | } | ||
64 | |||
65 | static int check() | ||
66 | { | ||
67 | int i = 0; | ||
68 | GNUNET_PEER_Id pid; | ||
69 | |||
70 | /* Insert Peers into PeerEntry table and hashmap */ | ||
71 | for (; i < NUMBER_OF_PEERS; i++) { | ||
72 | pid = GNUNET_PEER_intern(pidArr[i]); | ||
73 | if ( pid != (i + 1)) { | ||
74 | fprintf(stderr, "Unexpected Peer ID returned by intern function \n"); | ||
75 | return 1; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | /* Referencing the first 3 peers once again */ | ||
80 | for (i = 0; i < 3; i++) { | ||
81 | pid = GNUNET_PEER_intern(pidArr[i]); | ||
82 | if (pid != (i + 1)) { | ||
83 | fprintf(stderr, "Unexpcted Peer ID returned by intern function \n"); | ||
84 | return 1; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | /* Dereferencing the first 3 peers once [decrementing their reference count] */ | ||
89 | { | ||
90 | GNUNET_PEER_Id ids[] = {1, 2, 3}; | ||
91 | GNUNET_PEER_decrement_rcs(ids, 3); | ||
92 | } | ||
93 | |||
94 | /* re-referencing the first 3 peers using the change_rc function */ | ||
95 | for (i = 0; i < 3; i++) { | ||
96 | GNUNET_PEER_change_rc(i, 1); | ||
97 | } | ||
98 | |||
99 | /* Removing the second Peer from the PeerEntry hash map */ | ||
100 | GNUNET_PEER_change_rc(2, -2); | ||
101 | |||
102 | /* convert the pid of the first PeerEntry into that of the third */ | ||
103 | GNUNET_PEER_resolve(1, pidArr[3]); | ||
104 | |||
105 | return 0; | ||
106 | } | ||
107 | |||
108 | int main() | ||
109 | { | ||
110 | int ret; | ||
111 | GNUNET_log_setup ("test-peer", "ERROR", NULL); | ||
112 | generatePeerIdList(); | ||
113 | ret = check(); | ||
114 | destroyPeerIdList(); | ||
115 | |||
116 | return ret; | ||
117 | } | ||