2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
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.
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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file util/test_peer.c
20 * @brief testcase for peer.c
21 * @author Safey Mohammed
25 #include "gnunet_util_lib.h"
28 #define NUMBER_OF_PEERS 10
31 * A list of Peer ID's to play with
33 static struct GNUNET_PeerIdentity pidArr[NUMBER_OF_PEERS];
41 for (i = 0; i < NUMBER_OF_PEERS; i++)
43 gcry_randomize (&pidArr[i],
44 sizeof (struct GNUNET_PeerIdentity),
46 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
47 "Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i]));
57 struct GNUNET_PeerIdentity res;
58 struct GNUNET_PeerIdentity zero;
59 GNUNET_PEER_Id ids[] = { 1, 2, 3 };
61 GNUNET_assert (0 == GNUNET_PEER_intern (NULL));
62 /* Insert Peers into PeerEntry table and hashmap */
63 for (i = 0; i < NUMBER_OF_PEERS; i++)
65 pid = GNUNET_PEER_intern (&pidArr[i]);
68 FPRINTF (stderr, "%s", "Unexpected Peer ID returned by intern function\n");
73 /* Referencing the first 3 peers once again */
74 for (i = 0; i < 3; i++)
76 pid = GNUNET_PEER_intern (&pidArr[i]);
79 FPRINTF (stderr, "%s", "Unexpected Peer ID returned by intern function\n");
84 /* Dereferencing the first 3 peers once [decrementing their reference count] */
85 GNUNET_PEER_decrement_rcs (ids, 3);
87 /* re-referencing the first 3 peers using the change_rc function */
88 for (i = 1; i <= 3; i++)
89 GNUNET_PEER_change_rc (i, 1);
91 /* Removing the second Peer from the PeerEntry hash map */
92 GNUNET_PEER_change_rc (2, -2);
94 /* convert the pid of the first PeerEntry into that of the third */
95 GNUNET_PEER_resolve (1, &res);
96 GNUNET_assert (0 == memcmp (&res, &pidArr[0], sizeof (res)));
99 * Attempt to convert pid = 0 (which is reserved)
100 * into a peer identity object, the peer identity memory
101 * is expected to be set to zero
103 memset (&zero, 0, sizeof (struct GNUNET_PeerIdentity));
104 GNUNET_log_skip (1, GNUNET_YES);
105 GNUNET_PEER_resolve (0, &res);
106 GNUNET_assert (0 == memcmp (&res, &zero, sizeof (res)));
108 /* Removing peer entries 1 and 3 from table using the list decrement function */
109 /* If count = 0, nothing should be done whatsoever */
110 GNUNET_PEER_decrement_rcs (ids, 0);
113 GNUNET_PEER_decrement_rcs (ids, 2);
114 GNUNET_PEER_decrement_rcs (ids, 2);
125 GNUNET_log_setup ("test-peer", "ERROR", NULL);
126 for (i = 0; i < 1; i++)
128 generatePeerIdList ();
135 /* end of test_peer.c */