63610e42087aa8294147fcf21fdeadfa10f90115
[oweals/gnunet.git] / src / util / test_peer.c
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 }