From: Safey A.Halim Date: Mon, 9 Nov 2009 17:56:54 +0000 (+0000) Subject: * test_peer.c is a test case for peer.c X-Git-Tag: initial-import-from-subversion-38251~23136 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=8518be376f51fc3e682597a97157914d4745d5bc;p=oweals%2Fgnunet.git * test_peer.c is a test case for peer.c * Changes in configuration.c and gnunet_configuration_lib.h for dumping configuration diffs between the default configuration object and an edited configuration. --- 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" */ struct GNUNET_CONFIGURATION_Handle; + +/** + * Used for diffing a configuration object against + * the default one + */ +typedef struct { + struct GNUNET_CONFIGURATION_Handle* cfgNew; + struct GNUNET_CONFIGURATION_Handle* cfgDiff; +} GNUNNET_CONFIGURATION_Diff_Handle; + + /** * Create a new configuration object. * @return fresh configuration object @@ -105,6 +116,18 @@ int GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg, int GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename); +/** + * Write only configuration entries that have been changed to configuration file + * @param cfgDefault default configuration + * @param cfgNew new configuration + * @param filename where to write the configuration diff between default and new + * @return GNUNET_OK on success, GNUNET_SYSERR on error + */ +int +GNUNET_CONFIGURATION_write_diffs( + struct GNUNET_CONFIGURATION_Handle *cfgDefault, + struct GNUNET_CONFIGURATION_Handle *cfgNew, + const char* filename); /** * 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 = \ test_os_load \ test_os_network \ test_os_priority \ + test_peer \ test_plugin \ test_program \ test_pseudonym \ @@ -289,6 +290,11 @@ test_os_priority_SOURCES = \ test_os_priority_LDADD = \ $(top_builddir)/src/util/libgnunetutil.la +test_peer_SOURCES = \ + test_peer.c +test_peer_LDADD = \ +$(top_builddir)/src/util/libgnunetutil.la + test_plugin_SOURCES = \ test_plugin.c 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 @@ -461,6 +461,107 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, } +/** + * A callback function, compares entries from two configurations (default against a new configuration) + * and write the diffs in a diff-configuration object (the callback object). + * @param cls the diff configuration (ConfigurationDiffHandle*) + * @param section section for the value (of the default conf.) + * @param option option name of the value (of the default conf.) + * @param value value to copy (of the default conf.) + */ +void +compareEntries( + void *cls, + const char *section, + const char *option, + const char *value) +{ + struct ConfigSection *secNew; + struct ConfigEntry *entNew; + GNUNNET_CONFIGURATION_Diff_Handle* cfgDiff = (GNUNNET_CONFIGURATION_Diff_Handle*)cls; + + secNew = findSection(cfgDiff->cfgNew, section); + entNew = findEntry(cfgDiff->cfgNew, section, option); + if (secNew && strcmp(entNew->val, value) != 0) { + /* Value in the new configuration has been changed */ + /* Add the changed value to the diff configuration object */ + struct ConfigEntry *diffEntry = NULL; + struct ConfigSection *diffSection = NULL; + + diffSection = cfgDiff->cfgDiff->sections; + if (diffSection == NULL) { + /* First section */ + diffSection = GNUNET_malloc(sizeof(struct ConfigSection)); + memcpy(diffSection, secNew, sizeof(struct ConfigSection)); + cfgDiff->cfgDiff->sections = diffSection; + diffSection->entries = NULL; + diffSection->next = NULL; + } + else { + while ((strcmp(diffSection->name, secNew->name) != 0) && (diffSection->next != NULL)) { + diffSection = diffSection->next; + } + if (strcmp(diffSection->name, secNew->name) != 0) { + /* Section not found in diffs configuration */ + diffSection->next = GNUNET_malloc(sizeof(struct ConfigSection)); + memcpy(diffSection->next, secNew, sizeof(struct ConfigSection)); + diffSection->next->entries = NULL; + diffSection->next->next = NULL; + } + else { + diffEntry = diffSection->entries; + } + } + + if (diffEntry == NULL) { + /* First Entry */ + diffEntry = GNUNET_malloc(sizeof(struct ConfigEntry)); + memcpy(diffEntry, entNew, sizeof(struct ConfigEntry)); + if (diffSection->next == NULL) + /* The first Entry of the first Section */ + diffSection->entries = diffEntry; + else + /* The first entry of the non-first Section */ + diffSection->next->entries = diffEntry; + diffEntry->next = NULL; + } + else { + while (diffEntry->next != NULL) { + diffEntry = diffEntry->next; + } + diffEntry->next = GNUNET_malloc(sizeof(struct ConfigEntry)); + memcpy(diffEntry->next, entNew, sizeof(struct ConfigEntry)); + diffEntry->next->next = NULL; + } + } +} + + +/** + * Write only configuration entries that have been changed to configuration file + * @param cfgDefault default configuration + * @param cfgNew new configuration + * @param filename where to write the configuration diff between default and new + * @return GNUNET_OK on success, GNUNET_SYSERR on error + */ +int +GNUNET_CONFIGURATION_write_diffs( + struct GNUNET_CONFIGURATION_Handle *cfgDefault, + struct GNUNET_CONFIGURATION_Handle *cfgNew, + const char* filename + ) +{ + GNUNNET_CONFIGURATION_Diff_Handle *diffHandle = + GNUNET_malloc(sizeof(GNUNNET_CONFIGURATION_Diff_Handle)); + diffHandle->cfgDiff = GNUNET_CONFIGURATION_create(); + diffHandle->cfgDiff->sections = NULL; + diffHandle->cfgNew = cfgNew; + GNUNET_CONFIGURATION_iterate(cfgDefault, compareEntries, diffHandle); + + return GNUNET_CONFIGURATION_write(diffHandle->cfgDiff, filename); +} + + /** * Set a configuration value that should be a string. * 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 @@ +/* + This file is part of GNUnet. + (C) 2009 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 2, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ +/** + * @file util/test_peer.c + * @brief testcase for peer.c + * @author Safey Mohammed + */ + +#include "platform.h" +#include "gnunet_crypto_lib.h" +#include "gnunet_peer_lib.h" + +#define NUMBER_OF_PEERS 10 +/*#define DEBUG*/ + +/* Global Variables */ +static struct GNUNET_PeerIdentity** pidArr; /* A list of Peer ID's to play with */ + + +static void generatePeerIdList() +{ + int i; /* Loop Index */ + + pidArr = GNUNET_malloc(NUMBER_OF_PEERS * sizeof(struct GNUNET_PeerIdentity*)); + for (i = 0; i < NUMBER_OF_PEERS; i++ ) { + pidArr[i] = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity)); + GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &(pidArr[i]->hashPubKey)); + +#ifdef DEBUG + { + struct GNUNET_CRYPTO_HashAsciiEncoded hashAsciiEncoded; + GNUNET_CRYPTO_hash_to_enc(&(pidArr[i]->hashPubKey), &hashAsciiEncoded); + printf ("I'm Peer: %s\n", (char*) &hashAsciiEncoded); + } +#endif + } +} + +static void destroyPeerIdList() +{ + int i; + for (i = 0; i < NUMBER_OF_PEERS; i++) { + GNUNET_free(pidArr[i]); + } + GNUNET_free(pidArr); +} + +static int check() +{ + int i = 0; + GNUNET_PEER_Id pid; + + /* Insert Peers into PeerEntry table and hashmap */ + for (; i < NUMBER_OF_PEERS; i++) { + pid = GNUNET_PEER_intern(pidArr[i]); + if ( pid != (i + 1)) { + fprintf(stderr, "Unexpected Peer ID returned by intern function \n"); + return 1; + } + } + + /* Referencing the first 3 peers once again */ + for (i = 0; i < 3; i++) { + pid = GNUNET_PEER_intern(pidArr[i]); + if (pid != (i + 1)) { + fprintf(stderr, "Unexpcted Peer ID returned by intern function \n"); + return 1; + } + } + + /* Dereferencing the first 3 peers once [decrementing their reference count] */ + { + GNUNET_PEER_Id ids[] = {1, 2, 3}; + GNUNET_PEER_decrement_rcs(ids, 3); + } + + /* re-referencing the first 3 peers using the change_rc function */ + for (i = 0; i < 3; i++) { + GNUNET_PEER_change_rc(i, 1); + } + + /* Removing the second Peer from the PeerEntry hash map */ + GNUNET_PEER_change_rc(2, -2); + + /* convert the pid of the first PeerEntry into that of the third */ + GNUNET_PEER_resolve(1, pidArr[3]); + + return 0; +} + +int main() +{ + int ret; + GNUNET_log_setup ("test-peer", "ERROR", NULL); + generatePeerIdList(); + ret = check(); + destroyPeerIdList(); + + return ret; +}