glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / test_peer.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file util/test_peer.c
17  * @brief testcase for peer.c
18  * @author Safey Mohammed
19  */
20
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include <gcrypt.h>
24
25 #define NUMBER_OF_PEERS 10
26
27 /**
28  * A list of Peer ID's to play with
29  */
30 static struct GNUNET_PeerIdentity pidArr[NUMBER_OF_PEERS];
31
32
33 static void
34 generatePeerIdList ()
35 {
36   int i;
37
38   for (i = 0; i < NUMBER_OF_PEERS; i++)
39   {
40     gcry_randomize (&pidArr[i],
41                     sizeof (struct GNUNET_PeerIdentity),
42                     GCRY_STRONG_RANDOM);
43     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
44                 "Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i]));
45   }
46 }
47
48
49 static int
50 check ()
51 {
52   int i;
53   GNUNET_PEER_Id pid;
54   struct GNUNET_PeerIdentity res;
55   struct GNUNET_PeerIdentity zero;
56   GNUNET_PEER_Id ids[] = { 1, 2, 3 };
57
58   GNUNET_assert (0 == GNUNET_PEER_intern (NULL));
59   /* Insert Peers into PeerEntry table and hashmap */
60   for (i = 0; i < NUMBER_OF_PEERS; i++)
61   {
62     pid = GNUNET_PEER_intern (&pidArr[i]);
63     if (pid != (i + 1))
64     {
65       FPRINTF (stderr, "%s",  "Unexpected Peer ID returned by intern function\n");
66       return 1;
67     }
68   }
69
70   /* Referencing the first 3 peers once again */
71   for (i = 0; i < 3; i++)
72   {
73     pid = GNUNET_PEER_intern (&pidArr[i]);
74     if (pid != (i + 1))
75     {
76       FPRINTF (stderr, "%s",  "Unexpected Peer ID returned by intern function\n");
77       return 1;
78     }
79   }
80
81   /* Dereferencing the first 3 peers once [decrementing their reference count] */
82   GNUNET_PEER_decrement_rcs (ids, 3);
83
84   /* re-referencing the first 3 peers using the change_rc function */
85   for (i = 1; i <= 3; i++)
86     GNUNET_PEER_change_rc (i, 1);
87
88   /* Removing the second Peer from the PeerEntry hash map */
89   GNUNET_PEER_change_rc (2, -2);
90
91   /* convert the pid of the first PeerEntry into that of the third */
92   GNUNET_PEER_resolve (1, &res);
93   GNUNET_assert (0 == memcmp (&res, &pidArr[0], sizeof (res)));
94
95   /*
96    * Attempt to convert pid = 0 (which is reserved)
97    * into a peer identity object, the peer identity memory
98    * is expected to be set to zero
99    */
100   memset (&zero, 0, sizeof (struct GNUNET_PeerIdentity));
101   GNUNET_log_skip (1, GNUNET_YES);
102   GNUNET_PEER_resolve (0, &res);
103   GNUNET_assert (0 == memcmp (&res, &zero, sizeof (res)));
104
105   /* Removing peer entries 1 and 3 from table using the list decrement function */
106   /* If count = 0, nothing should be done whatsoever */
107   GNUNET_PEER_decrement_rcs (ids, 0);
108
109   ids[1] = 3;
110   GNUNET_PEER_decrement_rcs (ids, 2);
111   GNUNET_PEER_decrement_rcs (ids, 2);
112
113   return 0;
114 }
115
116
117 int
118 main ()
119 {
120   unsigned int i;
121
122   GNUNET_log_setup ("test-peer", "ERROR", NULL);
123   for (i = 0; i < 1; i++)
124   {
125     generatePeerIdList ();
126     if (0 != check ())
127       return 1;
128   }
129   return 0;
130 }
131
132 /* end of test_peer.c */