X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Ftest_peer.c;h=45633119cf84a2b2df2842c8f158b2b52fe3a8ce;hb=555214089c7045298f23fea9e060ea931804e75f;hp=63610e42087aa8294147fcf21fdeadfa10f90115;hpb=8518be376f51fc3e682597a97157914d4745d5bc;p=oweals%2Fgnunet.git diff --git a/src/util/test_peer.c b/src/util/test_peer.c index 63610e420..45633119c 100644 --- a/src/util/test_peer.c +++ b/src/util/test_peer.c @@ -28,90 +28,113 @@ #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 */ +#define DEBUG GNUNET_NO + +/** + * A list of Peer ID's to play with + */ +static struct GNUNET_PeerIdentity pidArr[NUMBER_OF_PEERS]; -static void generatePeerIdList() +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); - } + int i; + + for (i = 0; i < NUMBER_OF_PEERS; i++) + { + GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, + &pidArr[i].hashPubKey); +#if DEBUG + printf ("Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i])); #endif - } + } } -static void destroyPeerIdList() -{ - int i; - for (i = 0; i < NUMBER_OF_PEERS; i++) { - GNUNET_free(pidArr[i]); - } - GNUNET_free(pidArr); -} -static int check() +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] */ + int i; + GNUNET_PEER_Id pid; + struct GNUNET_PeerIdentity res; + struct GNUNET_PeerIdentity zero; + GNUNET_PEER_Id ids[] = { 1, 2, 3 }; + + GNUNET_assert (0 == GNUNET_PEER_intern (NULL)); + /* Insert Peers into PeerEntry table and hashmap */ + for (i = 0; i < NUMBER_OF_PEERS; i++) + { + pid = GNUNET_PEER_intern (&pidArr[i]); + if (pid != (i + 1)) { - GNUNET_PEER_Id ids[] = {1, 2, 3}; - GNUNET_PEER_decrement_rcs(ids, 3); + fprintf (stderr, + "Unexpected Peer ID returned by intern function \n"); + return 1; } - - /* re-referencing the first 3 peers using the change_rc function */ - for (i = 0; i < 3; i++) { - GNUNET_PEER_change_rc(i, 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, + "Unexpected Peer ID returned by intern function \n"); + return 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; + } + + /* Dereferencing the first 3 peers once [decrementing their reference count] */ + GNUNET_PEER_decrement_rcs (ids, 3); + + /* re-referencing the first 3 peers using the change_rc function */ + for (i = 1; 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, &res); + GNUNET_assert (0 == memcmp (&res, &pidArr[0], sizeof (res))); + + /* + * Attempt to convert pid = 0 (which is reserved) + * into a peer identity object, the peer identity memory + * is expected to be set to zero + */ + memset (&zero, 0, sizeof (struct GNUNET_PeerIdentity)); + GNUNET_log_skip (1, GNUNET_YES); + GNUNET_PEER_resolve (0, &res); + GNUNET_assert (0 == memcmp (&res, &zero, sizeof (res))); + + /* Removing peer entries 1 and 3 from table using the list decrement function */ + /* If count = 0, nothing should be done whatsoever */ + GNUNET_PEER_decrement_rcs (ids, 0); + + ids[1] = 3; + GNUNET_PEER_decrement_rcs (ids, 2); + GNUNET_PEER_decrement_rcs (ids, 2); + + return 0; } -int main() + +int +main () { - int ret; - GNUNET_log_setup ("test-peer", "ERROR", NULL); - generatePeerIdList(); - ret = check(); - destroyPeerIdList(); - - return ret; + int i; + GNUNET_log_setup ("test-peer", "ERROR", NULL); + for (i = 0; i < 1; i++) + { + generatePeerIdList (); + if (0 != check ()) + return 1; + } + return 0; } + +/* end of test_peer.c */