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