diff options
Diffstat (limited to 'src/lib/util/test_peer.c')
-rw-r--r-- | src/lib/util/test_peer.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/lib/util/test_peer.c b/src/lib/util/test_peer.c new file mode 100644 index 000000000..ad4e6aac9 --- /dev/null +++ b/src/lib/util/test_peer.c | |||
@@ -0,0 +1,140 @@ | |||
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 | 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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file util/test_peer.c | ||
22 | * @brief testcase for peer.c | ||
23 | * @author Safey Mohammed | ||
24 | */ | ||
25 | |||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include <gcrypt.h> | ||
30 | |||
31 | #define NUMBER_OF_PEERS 10 | ||
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 | for (unsigned int i = 0; i < NUMBER_OF_PEERS; i++) | ||
43 | { | ||
44 | gcry_randomize (&pidArr[i], | ||
45 | sizeof(struct GNUNET_PeerIdentity), | ||
46 | GCRY_STRONG_RANDOM); | ||
47 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
48 | "Peer %u: %s\n", | ||
49 | i, | ||
50 | GNUNET_i2s (&pidArr[i])); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | |||
55 | static int | ||
56 | check () | ||
57 | { | ||
58 | GNUNET_PEER_Id pid; | ||
59 | struct GNUNET_PeerIdentity res; | ||
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 (unsigned int 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", | ||
70 | "Unexpected Peer ID returned by intern function\n"); | ||
71 | return 1; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | /* Referencing the first 3 peers once again */ | ||
76 | for (unsigned int i = 0; i < 3; i++) | ||
77 | { | ||
78 | pid = GNUNET_PEER_intern (&pidArr[i]); | ||
79 | if (pid != (i + 1)) | ||
80 | { | ||
81 | fprintf (stderr, "%s", | ||
82 | "Unexpected Peer ID returned by intern function\n"); | ||
83 | return 1; | ||
84 | } | ||
85 | } | ||
86 | |||
87 | /* Dereferencing the first 3 peers once [decrementing their reference count] */ | ||
88 | GNUNET_PEER_decrement_rcs (ids, 3); | ||
89 | |||
90 | /* re-referencing the first 3 peers using the change_rc function */ | ||
91 | for (unsigned int i = 1; i <= 3; i++) | ||
92 | GNUNET_PEER_change_rc (i, 1); | ||
93 | |||
94 | /* Removing the second Peer from the PeerEntry hash map */ | ||
95 | GNUNET_PEER_change_rc (2, -2); | ||
96 | |||
97 | /* convert the pid of the first PeerEntry into that of the third */ | ||
98 | GNUNET_PEER_resolve (1, | ||
99 | &res); | ||
100 | GNUNET_assert (0 == | ||
101 | GNUNET_memcmp (&res, | ||
102 | &pidArr[0])); | ||
103 | |||
104 | /* | ||
105 | * Attempt to convert pid = 0 (which is reserved) | ||
106 | * into a peer identity object, the peer identity memory | ||
107 | * is expected to be set to zero | ||
108 | */GNUNET_log_skip (1, GNUNET_YES); | ||
109 | GNUNET_PEER_resolve (0, &res); | ||
110 | GNUNET_assert (GNUNET_YES == GNUNET_is_zero (&res)); | ||
111 | |||
112 | /* Removing peer entries 1 and 3 from table using the list decrement function */ | ||
113 | /* If count = 0, nothing should be done whatsoever */ | ||
114 | GNUNET_PEER_decrement_rcs (ids, 0); | ||
115 | |||
116 | ids[1] = 3; | ||
117 | GNUNET_PEER_decrement_rcs (ids, 2); | ||
118 | GNUNET_PEER_decrement_rcs (ids, 2); | ||
119 | |||
120 | return 0; | ||
121 | } | ||
122 | |||
123 | |||
124 | int | ||
125 | main () | ||
126 | { | ||
127 | GNUNET_log_setup ("test-peer", | ||
128 | "ERROR", | ||
129 | NULL); | ||
130 | for (unsigned int i = 0; i < 1; i++) | ||
131 | { | ||
132 | generatePeerIdList (); | ||
133 | if (0 != check ()) | ||
134 | return 1; | ||
135 | } | ||
136 | return 0; | ||
137 | } | ||
138 | |||
139 | |||
140 | /* end of test_peer.c */ | ||