2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 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 General Public License for more details.
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.
21 * @file testing/test_testing_group.c
22 * @brief testcase for functions to connect two peers in testing.c
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_core_service.h"
28 #define VERBOSE GNUNET_NO
31 * How long until we give up on connecting the peers?
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
35 #define DEFAULT_NUM_PEERS 4;
39 static unsigned long long num_peers;
41 static unsigned int total_connections;
43 static unsigned int expected_connections;
45 static int peers_left;
47 static struct GNUNET_TESTING_PeerGroup *pg;
49 static struct GNUNET_SCHEDULER_Handle *sched;
51 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
53 GNUNET_SCHEDULER_TaskIdentifier die_task;
55 static struct GNUNET_CORE_Handle *peer1handle;
57 static struct GNUNET_CORE_Handle *peer2handle;
64 GNUNET_assert (pg != NULL);
66 if (peer1handle != NULL)
67 GNUNET_CORE_disconnect(peer1handle);
68 if (peer2handle != NULL)
69 GNUNET_CORE_disconnect(peer2handle);
71 GNUNET_TESTING_daemons_stop (pg);
76 process_mtype (void *cls,
77 const struct GNUNET_PeerIdentity *peer,
78 const struct GNUNET_MessageHeader *message,
79 struct GNUNET_TIME_Relative latency,
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84 "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
86 GNUNET_SCHEDULER_cancel (sched, die_task);
87 GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
93 connect_notify (void *cls,
94 const struct GNUNET_PeerIdentity *peer,
95 struct GNUNET_TIME_Relative latency,
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
100 "Encrypted connection established to peer `%4s' with latency %llu\n",
101 GNUNET_i2s (peer), latency.value);
107 disconnect_notify (void *cls,
108 const struct GNUNET_PeerIdentity *peer)
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
118 inbound_notify (void *cls,
119 const struct GNUNET_PeerIdentity *other,
120 const struct GNUNET_MessageHeader *message,
121 struct GNUNET_TIME_Relative latency,
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
133 outbound_notify (void *cls,
134 const struct GNUNET_PeerIdentity *other,
135 const struct GNUNET_MessageHeader *message,
136 struct GNUNET_TIME_Relative latency,
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141 "Core notifies about outbound data for `%4s'.\n",
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "End badly was called... stopping daemons.\n");
155 if (peer1handle != NULL)
157 GNUNET_CORE_disconnect(peer1handle);
160 if (peer2handle != NULL)
162 GNUNET_CORE_disconnect(peer2handle);
168 GNUNET_TESTING_daemons_stop (pg);
169 ok = 7331; /* Opposite of leet */
172 ok = 401; /* Never got peers started */
176 transmit_ready (void *cls, size_t size, void *buf)
178 struct GNUNET_MessageHeader *m;
180 GNUNET_assert (buf != NULL);
181 m = (struct GNUNET_MessageHeader *) buf;
182 m->type = htons (MTYPE);
183 m->size = htons (sizeof (struct GNUNET_MessageHeader));
184 GNUNET_SCHEDULER_cancel(sched, die_task);
186 GNUNET_SCHEDULER_add_delayed (sched,
187 TIMEOUT, &end_badly, "from transmit ready");
189 return sizeof (struct GNUNET_MessageHeader);
193 static struct GNUNET_CORE_MessageHandler handlers[] = {
194 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
200 init_notify (void *cls,
201 struct GNUNET_CORE_Handle *server,
202 const struct GNUNET_PeerIdentity *my_identity,
203 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
205 struct GNUNET_TESTING_Daemon *connected_peer = cls;
206 struct GNUNET_TESTING_Daemon *peer1;
207 struct GNUNET_TESTING_Daemon *peer2;
209 peer1 = GNUNET_TESTING_daemon_get(pg, 0);
210 peer2 = GNUNET_TESTING_daemon_get(pg, 1);
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Core connection to `%4s' established, setting up handles\n",
214 GNUNET_i2s (my_identity));
217 if (connected_peer == peer1)
219 peer1handle = server;
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting core to peer 2\n");
224 GNUNET_CORE_connect (sched,
234 &outbound_notify, GNUNET_YES, handlers);
238 GNUNET_assert(connected_peer == peer2);
239 peer2handle = server;
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Asking core (1) for transmission to peer `%4s'\n",
243 GNUNET_i2s (&peer2->id));
246 if (NULL == GNUNET_CORE_notify_transmit_ready (peer1->server,
250 sizeof (struct GNUNET_MessageHeader),
251 &transmit_ready, &peer1))
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
255 GNUNET_i2s (&peer2->id));
263 send_test_messages ()
265 struct GNUNET_TESTING_Daemon *peer1;
266 struct GNUNET_TESTING_Daemon *peer2;
268 peer1 = GNUNET_TESTING_daemon_get(pg, 0);
269 peer2 = GNUNET_TESTING_daemon_get(pg, 1);
271 die_task = GNUNET_SCHEDULER_add_delayed (sched,
273 &end_badly, "from send test messages");
275 /* Send a message from peer 1 to peer 2 */
276 GNUNET_CORE_connect (sched,
285 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
289 topology_callback (void *cls,
290 const struct GNUNET_PeerIdentity *first,
291 const struct GNUNET_PeerIdentity *second,
292 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
293 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
294 struct GNUNET_TESTING_Daemon *first_daemon,
295 struct GNUNET_TESTING_Daemon *second_daemon,
298 /* Keep track of connections here if the client needs to know?
299 * Still, we have no real handle to say the i'th peer of the peer group
300 * even though we know that X peers exist in i... But we may want to
301 * know about the peer for logging purposes here (I'm sure we will actually
302 * so the API may need changed). Question, should the API expose what
303 * a peer group is, or provide convenience/accessor functions?
305 * For now, I've added accessor functions, which seems like a software
306 * engineering kind of solution, but who knows/cares. */
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
312 first_daemon->shortname,
313 second_daemon->shortname);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error %s\n",
321 first_daemon->shortname,
322 second_daemon->shortname, emsg);
326 if (total_connections == expected_connections)
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "Created %d total connections, which is our target number! Ending test.\n",
334 GNUNET_SCHEDULER_cancel (sched, die_task);
335 die_task = GNUNET_SCHEDULER_add_now (sched, &send_test_messages, NULL);
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Have %d total connections, Need %d\n",
342 total_connections, expected_connections);
351 expected_connections = -1;
352 if ((pg != NULL) && (peers_left == 0))
354 /* create_topology will read the topology information from
355 the config already contained in the peer group, so should
356 we have create_topology called from start peers? I think
357 maybe this way is best so that the client can know both
358 when peers are started, and when they are connected.
360 expected_connections = GNUNET_TESTING_create_topology (pg);
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Have %d expected connections\n", expected_connections);
367 GNUNET_SCHEDULER_cancel (sched, die_task);
368 die_task = GNUNET_SCHEDULER_add_delayed (sched,
376 const struct GNUNET_PeerIdentity *id,
377 const struct GNUNET_CONFIGURATION_Handle *cfg,
378 struct GNUNET_TESTING_Daemon *d, const char *emsg)
380 GNUNET_assert (id != NULL);
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %d out of %d\n",
383 (num_peers - peers_left) + 1, num_peers);
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "All %d daemons started, now creating topology!\n",
393 GNUNET_SCHEDULER_cancel (sched, die_task);
394 /* Set up task in case topology creation doesn't finish
395 * within a reasonable amount of time */
396 die_task = GNUNET_SCHEDULER_add_delayed (sched,
397 GNUNET_TIME_relative_multiply
398 (GNUNET_TIME_UNIT_MINUTES, 5),
408 struct GNUNET_SCHEDULER_Handle *s,
410 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 "Starting daemons based on config file %s\n", cfgfile);
419 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
421 num_peers = DEFAULT_NUM_PEERS;
425 peers_left = num_peers;
427 /* Set up a task to end testing if peer start fails */
428 die_task = GNUNET_SCHEDULER_add_delayed (sched,
429 GNUNET_TIME_relative_multiply
430 (GNUNET_TIME_UNIT_MINUTES, 5),
433 pg = GNUNET_TESTING_daemons_start (sched, cfg,
434 peers_left, &my_cb, NULL,
435 &topology_callback, NULL, NULL);
438 if (ret != GNUNET_SYSERR)
439 ret = send_test_messages (pg);
447 char *const argv[] = { "test-testing-topology-clique",
449 "test_testing_data_topology_clique.conf",
455 struct GNUNET_GETOPT_CommandLineOption options[] = {
456 GNUNET_GETOPT_OPTION_END
458 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
459 argv, "test-testing-topology-clique", "nohelp",
465 main (int argc, char *argv[])
469 GNUNET_log_setup ("test-testing-topology_clique",
478 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
482 /* end of test_testing_group.c */