e2fd100fd2198ff18df2e125e37b5dd02efc0d54
[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 /**
34  * A list of Peer ID's to play with 
35  */
36 static struct GNUNET_PeerIdentity pidArr[NUMBER_OF_PEERS];
37
38
39 static void
40 generatePeerIdList ()
41 {
42   int i;
43
44   for (i = 0; i < NUMBER_OF_PEERS; i++)
45     {
46       GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
47                                         &pidArr[i].hashPubKey);
48 #ifdef DEBUG
49       printf ("Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i]));
50 #endif
51     }
52 }
53
54
55 static int
56 check ()
57 {
58   int i;
59   GNUNET_PEER_Id pid;
60
61   /* Insert Peers into PeerEntry table and hashmap */
62   for (i = 0; i < NUMBER_OF_PEERS; i++)
63     {
64       pid = GNUNET_PEER_intern (&pidArr[i]);
65       if (pid != (i + 1))
66         {
67           fprintf (stderr,
68                    "Unexpected Peer ID returned by intern function \n");
69           return 1;
70         }
71     }
72
73   /* Referencing the first 3 peers once again */
74   for (i = 0; i < 3; i++)
75     {
76       pid = GNUNET_PEER_intern (&pidArr[i]);
77       if (pid != (i + 1))
78         {
79           fprintf (stderr,
80                    "Unexpcted Peer ID returned by intern function \n");
81           return 1;
82         }
83     }
84
85   /* Dereferencing the first 3 peers once [decrementing their reference count] */
86   {
87     GNUNET_PEER_Id ids[] = { 1, 2, 3 };
88     GNUNET_PEER_decrement_rcs (ids, 3);
89   }
90
91   /* re-referencing the first 3 peers using the change_rc function */
92   for (i = 0; i < 3; i++)
93     {
94       GNUNET_PEER_change_rc (i, 1);
95     }
96
97   /* Removing the second Peer from the PeerEntry hash map */
98   GNUNET_PEER_change_rc (2, -2);
99
100   /* convert the pid of the first PeerEntry into that of the third */
101   GNUNET_PEER_resolve (1, &pidArr[3]);
102
103   return 0;
104 }
105
106
107 int
108 main ()
109 {
110   GNUNET_log_setup ("test-peer", "ERROR", NULL);
111   generatePeerIdList ();
112   return check ();
113 }
114
115 /* end of test_peer.c */