- test for external iterator
[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 3, 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 /**
33  * A list of Peer ID's to play with
34  */
35 static struct GNUNET_PeerIdentity pidArr[NUMBER_OF_PEERS];
36
37
38 static void
39 generatePeerIdList ()
40 {
41   int i;
42
43   for (i = 0; i < NUMBER_OF_PEERS; i++)
44   {
45     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
46                                       &pidArr[i].hashPubKey);
47     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
48                 "Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i]));
49   }
50 }
51
52
53 static int
54 check ()
55 {
56   int i;
57   GNUNET_PEER_Id pid;
58   struct GNUNET_PeerIdentity res;
59   struct GNUNET_PeerIdentity zero;
60   GNUNET_PEER_Id ids[] = { 1, 2, 3 };
61
62   GNUNET_assert (0 == GNUNET_PEER_intern (NULL));
63   /* Insert Peers into PeerEntry table and hashmap */
64   for (i = 0; i < NUMBER_OF_PEERS; i++)
65   {
66     pid = GNUNET_PEER_intern (&pidArr[i]);
67     if (pid != (i + 1))
68     {
69       FPRINTF (stderr, "%s",  "Unexpected Peer ID returned by intern function\n");
70       return 1;
71     }
72   }
73
74   /* Referencing the first 3 peers once again */
75   for (i = 0; i < 3; i++)
76   {
77     pid = GNUNET_PEER_intern (&pidArr[i]);
78     if (pid != (i + 1))
79     {
80       FPRINTF (stderr, "%s",  "Unexpected 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   GNUNET_PEER_decrement_rcs (ids, 3);
87
88   /* re-referencing the first 3 peers using the change_rc function */
89   for (i = 1; i <= 3; i++)
90     GNUNET_PEER_change_rc (i, 1);
91
92   /* Removing the second Peer from the PeerEntry hash map */
93   GNUNET_PEER_change_rc (2, -2);
94
95   /* convert the pid of the first PeerEntry into that of the third */
96   GNUNET_PEER_resolve (1, &res);
97   GNUNET_assert (0 == memcmp (&res, &pidArr[0], sizeof (res)));
98
99   /*
100    * Attempt to convert pid = 0 (which is reserved)
101    * into a peer identity object, the peer identity memory
102    * is expected to be set to zero
103    */
104   memset (&zero, 0, sizeof (struct GNUNET_PeerIdentity));
105   GNUNET_log_skip (1, GNUNET_YES);
106   GNUNET_PEER_resolve (0, &res);
107   GNUNET_assert (0 == memcmp (&res, &zero, sizeof (res)));
108
109   /* Removing peer entries 1 and 3 from table using the list decrement function */
110   /* If count = 0, nothing should be done whatsoever */
111   GNUNET_PEER_decrement_rcs (ids, 0);
112
113   ids[1] = 3;
114   GNUNET_PEER_decrement_rcs (ids, 2);
115   GNUNET_PEER_decrement_rcs (ids, 2);
116
117   return 0;
118 }
119
120
121 int
122 main ()
123 {
124   unsigned int i;
125
126   GNUNET_log_setup ("test-peer", "ERROR", NULL);
127   for (i = 0; i < 1; i++)
128   {
129     generatePeerIdList ();
130     if (0 != check ())
131       return 1;
132   }
133   return 0;
134 }
135
136 /* end of test_peer.c */