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' of type %d.\n", GNUNET_i2s (peer), ntohs(message->type));
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));
117 outbound_notify (void *cls,
118 const struct GNUNET_PeerIdentity *other,
119 const struct GNUNET_MessageHeader *message,
120 struct GNUNET_TIME_Relative latency,
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "Core notifies about outbound data for `%4s'.\n",
132 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "End badly was called... stopping daemons.\n");
139 if (peer1handle != NULL)
141 GNUNET_CORE_disconnect(peer1handle);
144 if (peer2handle != NULL)
146 GNUNET_CORE_disconnect(peer2handle);
152 GNUNET_TESTING_daemons_stop (pg);
153 ok = 7331; /* Opposite of leet */
156 ok = 401; /* Never got peers started */
160 transmit_ready (void *cls, size_t size, void *buf)
162 struct GNUNET_MessageHeader *m;
164 GNUNET_assert (buf != NULL);
165 m = (struct GNUNET_MessageHeader *) buf;
166 m->type = htons (MTYPE);
167 m->size = htons (sizeof (struct GNUNET_MessageHeader));
168 GNUNET_SCHEDULER_cancel(sched, die_task);
170 GNUNET_SCHEDULER_add_delayed (sched,
171 TIMEOUT, &end_badly, "from transmit ready");
173 return sizeof (struct GNUNET_MessageHeader);
177 static struct GNUNET_CORE_MessageHandler handlers[] = {
178 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
184 init_notify (void *cls,
185 struct GNUNET_CORE_Handle *server,
186 const struct GNUNET_PeerIdentity *my_identity,
187 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
189 struct GNUNET_TESTING_Daemon *connected_peer = cls;
190 struct GNUNET_TESTING_Daemon *peer1;
191 struct GNUNET_TESTING_Daemon *peer2;
193 peer1 = GNUNET_TESTING_daemon_get(pg, 0);
194 peer2 = GNUNET_TESTING_daemon_get(pg, 1);
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Core connection to `%4s' established, setting up handles\n",
198 GNUNET_i2s (my_identity));
201 if (connected_peer == peer1)
203 peer1handle = server;
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting core to peer 2\n");
208 GNUNET_CORE_connect (sched,
218 &outbound_notify, GNUNET_YES, handlers);
222 GNUNET_assert(connected_peer == peer2);
223 peer2handle = server;
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Asking core (1) for transmission to peer `%4s'\n",
227 GNUNET_i2s (&peer2->id));
230 if (NULL == GNUNET_CORE_notify_transmit_ready (peer1->server,
234 sizeof (struct GNUNET_MessageHeader),
235 &transmit_ready, &peer1))
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
239 GNUNET_i2s (&peer2->id));
247 send_test_messages ()
249 struct GNUNET_TESTING_Daemon *peer1;
250 struct GNUNET_TESTING_Daemon *peer2;
252 peer1 = GNUNET_TESTING_daemon_get(pg, 0);
253 peer2 = GNUNET_TESTING_daemon_get(pg, 1);
255 die_task = GNUNET_SCHEDULER_add_delayed (sched,
257 &end_badly, "from send test messages");
259 /* Send a message from peer 1 to peer 2 */
260 GNUNET_CORE_connect (sched,
269 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
273 topology_callback (void *cls,
274 const struct GNUNET_PeerIdentity *first,
275 const struct GNUNET_PeerIdentity *second,
276 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
277 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
278 struct GNUNET_TESTING_Daemon *first_daemon,
279 struct GNUNET_TESTING_Daemon *second_daemon,
282 /* Keep track of connections here if the client needs to know?
283 * Still, we have no real handle to say the i'th peer of the peer group
284 * even though we know that X peers exist in i... But we may want to
285 * know about the peer for logging purposes here (I'm sure we will actually
286 * so the API may need changed). Question, should the API expose what
287 * a peer group is, or provide convenience/accessor functions?
289 * For now, I've added accessor functions, which seems like a software
290 * engineering kind of solution, but who knows/cares. */
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
296 first_daemon->shortname,
297 second_daemon->shortname);
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error %s\n",
305 first_daemon->shortname,
306 second_daemon->shortname, emsg);
310 if (total_connections == expected_connections)
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Created %d total connections, which is our target number! Ending test.\n",
318 GNUNET_SCHEDULER_cancel (sched, die_task);
319 die_task = GNUNET_SCHEDULER_add_now (sched, &send_test_messages, NULL);
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Have %d total connections, Need %d\n",
326 total_connections, expected_connections);
335 expected_connections = -1;
336 if ((pg != NULL) && (peers_left == 0))
338 /* create_topology will read the topology information from
339 the config already contained in the peer group, so should
340 we have create_topology called from start peers? I think
341 maybe this way is best so that the client can know both
342 when peers are started, and when they are connected.
344 expected_connections = GNUNET_TESTING_create_topology (pg);
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Have %d expected connections\n", expected_connections);
351 GNUNET_SCHEDULER_cancel (sched, die_task);
352 die_task = GNUNET_SCHEDULER_add_delayed (sched,
360 const struct GNUNET_PeerIdentity *id,
361 const struct GNUNET_CONFIGURATION_Handle *cfg,
362 struct GNUNET_TESTING_Daemon *d, const char *emsg)
364 GNUNET_assert (id != NULL);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %d out of %d\n",
367 (num_peers - peers_left) + 1, num_peers);
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "All %d daemons started, now creating topology!\n",
377 GNUNET_SCHEDULER_cancel (sched, die_task);
378 /* Set up task in case topology creation doesn't finish
379 * within a reasonable amount of time */
380 die_task = GNUNET_SCHEDULER_add_delayed (sched,
381 GNUNET_TIME_relative_multiply
382 (GNUNET_TIME_UNIT_MINUTES, 5),
392 struct GNUNET_SCHEDULER_Handle *s,
394 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Starting daemons based on config file %s\n", cfgfile);
403 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
405 num_peers = DEFAULT_NUM_PEERS;
409 peers_left = num_peers;
411 /* Set up a task to end testing if peer start fails */
412 die_task = GNUNET_SCHEDULER_add_delayed (sched,
413 GNUNET_TIME_relative_multiply
414 (GNUNET_TIME_UNIT_MINUTES, 5),
417 pg = GNUNET_TESTING_daemons_start (sched, cfg,
418 peers_left, &my_cb, NULL,
419 &topology_callback, NULL, NULL);
422 if (ret != GNUNET_SYSERR)
423 ret = send_test_messages (pg);
431 char *const argv[] = { "test-testing-topology-clique",
433 "test_testing_data_topology_clique.conf",
439 struct GNUNET_GETOPT_CommandLineOption options[] = {
440 GNUNET_GETOPT_OPTION_END
442 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
443 argv, "test-testing-topology-clique", "nohelp",
449 main (int argc, char *argv[])
453 GNUNET_log_setup ("test-testing-topology_clique",
462 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
466 /* end of test_testing_group.c */