diff options
Diffstat (limited to 'src/rps/test_rps_malicious_1.c')
-rw-r--r-- | src/rps/test_rps_malicious_1.c | 383 |
1 files changed, 0 insertions, 383 deletions
diff --git a/src/rps/test_rps_malicious_1.c b/src/rps/test_rps_malicious_1.c deleted file mode 100644 index 50ae2ac86..000000000 --- a/src/rps/test_rps_malicious_1.c +++ /dev/null | |||
@@ -1,383 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009, 2012 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 rps/test_rps_multipeer.c | ||
22 | * @brief Testcase for the random peer sampling service. Starts | ||
23 | * a peergroup with a given number of peers, then waits to | ||
24 | * receive size pushes/pulls from each peer. Expects to wait | ||
25 | * for one message from each peer. | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_testbed_service.h" | ||
29 | #include "gnunet_rps_service.h" | ||
30 | |||
31 | #include <inttypes.h> | ||
32 | |||
33 | |||
34 | /** | ||
35 | * How many peers do we start? | ||
36 | */ | ||
37 | #define NUM_PEERS 10 | ||
38 | |||
39 | /** | ||
40 | * How long do we run the test? | ||
41 | */ | ||
42 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) | ||
43 | |||
44 | |||
45 | /** | ||
46 | * Portion of malicious peers | ||
47 | */ | ||
48 | static double portion = .1; | ||
49 | |||
50 | |||
51 | /** | ||
52 | * Information we track for each peer. | ||
53 | */ | ||
54 | struct RPSPeer | ||
55 | { | ||
56 | /** | ||
57 | * Index of the peer. | ||
58 | */ | ||
59 | unsigned int index; | ||
60 | |||
61 | /** | ||
62 | * Handle for RPS connect operation. | ||
63 | */ | ||
64 | struct GNUNET_TESTBED_Operation *op; | ||
65 | |||
66 | /** | ||
67 | * Handle to RPS service. | ||
68 | */ | ||
69 | struct GNUNET_RPS_Handle *rps_handle; | ||
70 | |||
71 | /** | ||
72 | * ID of the peer. | ||
73 | */ | ||
74 | struct GNUNET_PeerIdentity *peer_id; | ||
75 | }; | ||
76 | |||
77 | |||
78 | /** | ||
79 | * Information for all the peers. | ||
80 | */ | ||
81 | static struct RPSPeer rps_peers[NUM_PEERS]; | ||
82 | |||
83 | /** | ||
84 | * IDs of the peers. | ||
85 | */ | ||
86 | static struct GNUNET_PeerIdentity rps_peer_ids[NUM_PEERS]; | ||
87 | |||
88 | /** | ||
89 | * Return value from 'main'. | ||
90 | */ | ||
91 | static int ok; | ||
92 | |||
93 | |||
94 | /** | ||
95 | * Task run on timeout to shut everything down. | ||
96 | */ | ||
97 | static void | ||
98 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
99 | { | ||
100 | unsigned int i; | ||
101 | |||
102 | for (i=0;i<NUM_PEERS;i++) | ||
103 | GNUNET_TESTBED_operation_done (rps_peers[i].op); | ||
104 | GNUNET_SCHEDULER_shutdown (); | ||
105 | } | ||
106 | |||
107 | |||
108 | /** | ||
109 | * Callback to call when network size estimate is updated. | ||
110 | * | ||
111 | * @param cls closure | ||
112 | * @param timestamp server timestamp | ||
113 | * @param estimate the value of the current network size estimate | ||
114 | * @param std_dev standard deviation (rounded down to nearest integer) | ||
115 | * of the size estimation values seen | ||
116 | * | ||
117 | */ | ||
118 | static void | ||
119 | handle_reply (void *cls, uint64_t n, const struct GNUNET_PeerIdentity *peers) | ||
120 | { | ||
121 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got peer %s\n", GNUNET_i2s (peers)); | ||
122 | |||
123 | ok = 0; | ||
124 | } | ||
125 | |||
126 | |||
127 | /** | ||
128 | * (Randomly) request random peers. | ||
129 | */ | ||
130 | void | ||
131 | request_peers (void *cls, | ||
132 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
133 | { | ||
134 | struct RPSPeer *peer = (struct RPSPeer *) cls; | ||
135 | struct GNUNET_RPS_Request_Handle *req_handle; | ||
136 | |||
137 | req_handle = GNUNET_RPS_request_peers (peer->rps_handle, 1, handle_reply, NULL); | ||
138 | GNUNET_free (req_handle); | ||
139 | } | ||
140 | |||
141 | |||
142 | /** | ||
143 | * Seed peers. | ||
144 | */ | ||
145 | void | ||
146 | seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
147 | { | ||
148 | unsigned int amount; | ||
149 | struct RPSPeer *peer = (struct RPSPeer *) cls; | ||
150 | unsigned int i; | ||
151 | |||
152 | GNUNET_assert (1 >= portion | ||
153 | && 0 < portion); | ||
154 | |||
155 | amount = round (portion * NUM_PEERS); | ||
156 | |||
157 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n"); | ||
158 | for (i = 0 ; i < amount ; i++) | ||
159 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n", | ||
160 | i, | ||
161 | GNUNET_i2s (&rps_peer_ids[i])); | ||
162 | |||
163 | GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids); | ||
164 | } | ||
165 | |||
166 | |||
167 | /** | ||
168 | * Get the id of peer i. | ||
169 | */ | ||
170 | void | ||
171 | info_cb (void *cb_cls, | ||
172 | struct GNUNET_TESTBED_Operation *op, | ||
173 | const struct GNUNET_TESTBED_PeerInformation *pinfo, | ||
174 | const char *emsg) | ||
175 | { | ||
176 | unsigned int i = *((unsigned int *) cb_cls); | ||
177 | |||
178 | if (NULL == pinfo || NULL != emsg) | ||
179 | { | ||
180 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg); | ||
181 | return; | ||
182 | } | ||
183 | |||
184 | GNUNET_free (cb_cls); | ||
185 | |||
186 | rps_peer_ids[i] = *(pinfo->result.id); | ||
187 | rps_peers[i].peer_id = &rps_peer_ids[i]; | ||
188 | |||
189 | } | ||
190 | |||
191 | |||
192 | /** | ||
193 | * Callback to be called when RPS service connect operation is completed | ||
194 | * | ||
195 | * @param cls the callback closure from functions generating an operation | ||
196 | * @param op the operation that has been finished | ||
197 | * @param ca_result the RPS service handle returned from rps_connect_adapter | ||
198 | * @param emsg error message in case the operation has failed; will be NULL if | ||
199 | * operation has executed successfully. | ||
200 | */ | ||
201 | static void | ||
202 | rps_connect_complete_cb (void *cls, | ||
203 | struct GNUNET_TESTBED_Operation *op, | ||
204 | void *ca_result, | ||
205 | const char *emsg) | ||
206 | { | ||
207 | struct RPSPeer *rps_peer = cls; | ||
208 | struct GNUNET_RPS_Handle *rps = ca_result; | ||
209 | struct GNUNET_RPS_Request_Handle *req_handle; | ||
210 | uint32_t num_mal_peers; | ||
211 | |||
212 | rps_peer->rps_handle = rps; | ||
213 | |||
214 | GNUNET_assert (op == rps_peer->op); | ||
215 | if (NULL != emsg) | ||
216 | { | ||
217 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
218 | "Failed to connect to RPS service: %s\n", | ||
219 | emsg); | ||
220 | ok = 1; | ||
221 | GNUNET_SCHEDULER_shutdown (); | ||
222 | return; | ||
223 | } | ||
224 | |||
225 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n"); | ||
226 | |||
227 | #ifdef ENABLE_MALICIOUS | ||
228 | GNUNET_assert (1 >= portion | ||
229 | && 0 < portion); | ||
230 | num_mal_peers = round (portion * NUM_PEERS); | ||
231 | |||
232 | if (rps_peer->index >= num_mal_peers) | ||
233 | { | ||
234 | req_handle = GNUNET_RPS_request_peers (rps, 1, handle_reply, NULL); | ||
235 | GNUNET_free (req_handle); | ||
236 | } | ||
237 | #else /* ENABLE_MALICIOUS */ | ||
238 | req_handle = GNUNET_RPS_request_peers (rps, 1, handle_reply, NULL); | ||
239 | GNUNET_free (req_handle); | ||
240 | #endif /* ENABLE_MALICIOUS */ | ||
241 | |||
242 | //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), | ||
243 | // request_peers, peer); | ||
244 | //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), | ||
245 | // seed_peers, peer); | ||
246 | // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers | ||
247 | } | ||
248 | |||
249 | |||
250 | /** | ||
251 | * Adapter function called to establish a connection to | ||
252 | * the RPS service. | ||
253 | * | ||
254 | * @param cls closure | ||
255 | * @param cfg configuration of the peer to connect to; will be available until | ||
256 | * GNUNET_TESTBED_operation_done() is called on the operation returned | ||
257 | * from GNUNET_TESTBED_service_connect() | ||
258 | * @return service handle to return in 'op_result', NULL on error | ||
259 | */ | ||
260 | static void * | ||
261 | rps_connect_adapter (void *cls, | ||
262 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
263 | { | ||
264 | struct GNUNET_RPS_Handle *h; | ||
265 | #ifdef ENABLE_MALICIOUS | ||
266 | uint32_t num_mal_peers; | ||
267 | struct RPSPeer *rps_peer = (struct RPSPeer *) cls; | ||
268 | #endif /* ENABLE_MALICIOUS */ | ||
269 | |||
270 | h = GNUNET_RPS_connect (cfg); | ||
271 | |||
272 | #ifdef ENABLE_MALICIOUS | ||
273 | GNUNET_assert (1 >= portion | ||
274 | && 0 < portion); | ||
275 | num_mal_peers = round (portion * NUM_PEERS); | ||
276 | |||
277 | if (rps_peer->index < num_mal_peers) | ||
278 | { | ||
279 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
280 | "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n", | ||
281 | rps_peer->index, | ||
282 | GNUNET_i2s (rps_peer->peer_id), | ||
283 | num_mal_peers); | ||
284 | |||
285 | GNUNET_RPS_act_malicious (h, 1, num_mal_peers, rps_peer_ids); | ||
286 | #endif /* ENABLE_MALICIOUS */ | ||
287 | } | ||
288 | |||
289 | return h; | ||
290 | } | ||
291 | |||
292 | |||
293 | /** | ||
294 | * Adapter function called to destroy connection to | ||
295 | * RPS service. | ||
296 | * | ||
297 | * @param cls closure | ||
298 | * @param op_result service handle returned from the connect adapter | ||
299 | */ | ||
300 | static void | ||
301 | rps_disconnect_adapter (void *cls, | ||
302 | void *op_result) | ||
303 | { | ||
304 | struct GNUNET_RPS_Handle *h = op_result; | ||
305 | GNUNET_RPS_disconnect (h); | ||
306 | } | ||
307 | |||
308 | |||
309 | /** | ||
310 | * Actual "main" function for the testcase. | ||
311 | * | ||
312 | * @param cls closure | ||
313 | * @param h the run handle | ||
314 | * @param num_peers number of peers in 'peers' | ||
315 | * @param peers handle to peers run in the testbed | ||
316 | * @param links_succeeded the number of overlay link connection attempts that | ||
317 | * succeeded | ||
318 | * @param links_failed the number of overlay link connection attempts that | ||
319 | * failed | ||
320 | */ | ||
321 | static void | ||
322 | run (void *cls, | ||
323 | struct GNUNET_TESTBED_RunHandle *h, | ||
324 | unsigned int num_peers, | ||
325 | struct GNUNET_TESTBED_Peer **peers, | ||
326 | unsigned int links_succeeded, | ||
327 | unsigned int links_failed) | ||
328 | { | ||
329 | unsigned int i; | ||
330 | unsigned int *tmp_i; | ||
331 | |||
332 | tmp_i = GNUNET_new (unsigned int); | ||
333 | |||
334 | for (i = 0 ; i < NUM_PEERS ; i++) | ||
335 | { | ||
336 | tmp_i = GNUNET_new (unsigned int); | ||
337 | *tmp_i = i; | ||
338 | |||
339 | (void) GNUNET_TESTBED_peer_get_information (peers[i], | ||
340 | GNUNET_TESTBED_PIT_IDENTITY, | ||
341 | &info_cb, | ||
342 | tmp_i); | ||
343 | } | ||
344 | |||
345 | |||
346 | GNUNET_assert (NUM_PEERS == num_peers); | ||
347 | for (i = 0 ; i < num_peers ; i++) | ||
348 | { | ||
349 | rps_peers[i].index = i; | ||
350 | rps_peers[i].op = | ||
351 | GNUNET_TESTBED_service_connect (&rps_peers[i], | ||
352 | peers[i], | ||
353 | "rps", | ||
354 | &rps_connect_complete_cb, | ||
355 | &rps_peers[i], | ||
356 | &rps_connect_adapter, | ||
357 | &rps_disconnect_adapter, | ||
358 | &rps_peers[i]); | ||
359 | } | ||
360 | GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL); | ||
361 | } | ||
362 | |||
363 | |||
364 | /** | ||
365 | * Entry point for the testcase, sets up the testbed. | ||
366 | * | ||
367 | * @param argc unused | ||
368 | * @param argv unused | ||
369 | * @return 0 on success | ||
370 | */ | ||
371 | int | ||
372 | main (int argc, char *argv[]) | ||
373 | { | ||
374 | ok = 1; | ||
375 | (void) GNUNET_TESTBED_test_run ("test-rps-multipeer", | ||
376 | "test_rps.conf", | ||
377 | NUM_PEERS, | ||
378 | 0, NULL, NULL, | ||
379 | &run, NULL); | ||
380 | return ok; | ||
381 | } | ||
382 | |||
383 | /* end of test_rps_multipeer.c */ | ||