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 3, 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 gns/test_gns_twopeer.c
22 * @brief base testcase for testing DHT service with
25 * This testcase starts peers using the GNUNET_TESTING_daemons_start
26 * function call. On peer start, connects to the peers DHT service
27 * by calling GNUNET_DHT_connected. Once notified about all peers
28 * being started (by the peers_started_callback function), calls
29 * GNUNET_TESTING_connect_topology, which connects the peers in a
30 * "straight line" topology. On notification that all peers have
31 * been properly connected, calls the do_get function which initiates
32 * a GNUNET_DHT_get from the *second* peer. Once the GNUNET_DHT_get
33 * function starts, runs the do_put function to insert data at the first peer.
34 * If the GET is successful, schedules finish_testing
35 * to stop the test and shut down peers. If GET is unsuccessful
36 * after GET_TIMEOUT seconds, prints an error message and shuts down
40 #include "gnunet_testing_lib.h"
41 #include "gnunet_core_service.h"
42 #include "gnunet_dht_service.h"
43 #include "block_dns.h"
44 #include "gnunet_signatures.h"
45 #include "gnunet_namestore_service.h"
46 #include "gnunet_dnsparser_lib.h"
47 #include "gnunet_gns_service.h"
50 #define VERBOSE GNUNET_YES
52 /* Timeout for entire testcase */
53 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 80)
55 /* If number of peers not in config file, use this number */
56 #define DEFAULT_NUM_PEERS 2
61 * Directory to store temp data in, defined in config file
63 static char *test_directory;
66 * Variable used to store the number of connections we should wait for.
68 static unsigned int expected_connections;
71 * Variable used to keep track of how many peers aren't yet started.
73 static unsigned long long peers_left;
75 struct GNUNET_TESTING_Daemon *d1;
76 struct GNUNET_TESTING_Daemon *d2;
80 * Total number of peers to run, set based on config file.
82 static unsigned long long num_peers;
85 * Global used to count how many connections we have currently
86 * been notified about (how many times has topology_callback been called
89 static unsigned int total_connections;
92 * Global used to count how many failed connections we have
93 * been notified about (how many times has topology_callback
94 * been called with failure?)
96 static unsigned int failed_connections;
98 /* Task handle to use to schedule test failure */
99 GNUNET_SCHEDULER_TaskIdentifier die_task;
101 GNUNET_SCHEDULER_TaskIdentifier bob_task;
103 /* Global return value (0 for success, anything else for failure) */
106 int bob_online, alice_online;
109 * Check whether peers successfully shut down.
112 shutdown_callback (void *cls, const char *emsg)
122 * Function scheduled to be run on the successful completion of this
123 * testcase. Specifically, called when our get request completes.
126 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
129 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
130 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
131 GNUNET_YES, GNUNET_NO);
132 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer2!\n");
133 GNUNET_TESTING_daemon_stop (d2, TIMEOUT, &shutdown_callback, NULL,
134 GNUNET_YES, GNUNET_NO);
135 GNUNET_SCHEDULER_cancel(bob_task);
136 GNUNET_SCHEDULER_cancel(die_task);
140 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
141 * down the peers without freeing memory associated with GET request.
144 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
148 GNUNET_YES, GNUNET_NO);
150 GNUNET_TESTING_daemon_stop (d2, TIMEOUT, &shutdown_callback, NULL,
151 GNUNET_YES, GNUNET_NO);
155 * Check if the get_handle is being used, if so stop the request. Either
156 * way, schedule the end_badly_cont function which actually shuts down the
160 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failing test with error: `%s'!\n",
164 GNUNET_SCHEDULER_cancel(bob_task);
165 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
170 do_lookup(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
173 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
174 (GNUNET_TIME_UNIT_SECONDS, 30),
175 &finish_testing, NULL);
179 gns_started(void *cls, const struct GNUNET_PeerIdentity *id,
180 const struct GNUNET_CONFIGURATION_Handle *cfg,
181 struct GNUNET_TESTING_Daemon *d, const char *emsg)
186 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "GNS failed to start alice\n");
188 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "GNS failed to start bob\n");
193 /* start gns for bob */
194 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "GNS started on alice\n");
195 GNUNET_TESTING_daemon_start_service (d2, "gns", TIMEOUT, &gns_started,
200 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "GNS started on bob\n");
202 /* start the lookup tests */
203 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
204 (GNUNET_TIME_UNIT_SECONDS, 1),
209 * This function is called whenever a connection attempt is finished between two of
210 * the started peers (started with GNUNET_TESTING_daemons_start). The total
211 * number of times this function is called should equal the number returned
212 * from the GNUNET_TESTING_connect_topology call.
214 * The emsg variable is NULL on success (peers connected), and non-NULL on
215 * failure (peers failed to connect).
218 notify_connect (void *cls, const struct GNUNET_PeerIdentity *first,
219 const struct GNUNET_PeerIdentity *second, uint32_t distance,
220 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
221 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
222 struct GNUNET_TESTING_Daemon *first_daemon,
223 struct GNUNET_TESTING_Daemon *second_daemon,
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
231 "connected peer %s to peer %s, distance %u\n",
232 first_daemon->shortname, second_daemon->shortname, distance);
238 failed_connections++;
239 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
240 "Failed to connect peer %s to peer %s with error :\n%s\n",
241 first_daemon->shortname, second_daemon->shortname, emsg);
245 if (total_connections == expected_connections)
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
252 GNUNET_SCHEDULER_cancel (die_task);
254 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from test lookup");
256 /* start gns for alice */
257 GNUNET_TESTING_daemon_start_service (d1, "gns", TIMEOUT, &gns_started, NULL);
260 else if (total_connections + failed_connections == expected_connections)
262 GNUNET_SCHEDULER_cancel (die_task);
264 GNUNET_SCHEDULER_add_now (&end_badly,
265 "from topology_callback (too many failed connections)");
270 * Set up some data, and call API PUT function
273 alice_idle (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
279 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
280 (GNUNET_TIME_UNIT_SECONDS, 2),
285 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting peers\n");
286 GNUNET_TESTING_daemons_connect (d1, d2, TIMEOUT, 5, 1,
287 ¬ify_connect, NULL);
291 bob_idle (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
293 /* he's lazy FIXME forever */
295 bob_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
296 (GNUNET_TIME_UNIT_SECONDS, 20),
304 * Callback which is called whenever a peer is started (as a result of the
305 * GNUNET_TESTING_daemons_start call.
307 * @param cls closure argument given to GNUNET_TESTING_daemons_start
308 * @param id the GNUNET_PeerIdentity of the started peer
309 * @param cfg the configuration for this specific peer (needed to connect
311 * @param d the handle to the daemon started
312 * @param emsg NULL if peer started, non-NULL on error
315 alice_started (void *cls, const struct GNUNET_PeerIdentity *id,
316 const struct GNUNET_CONFIGURATION_Handle *cfg,
317 struct GNUNET_TESTING_Daemon *d, const char *emsg)
321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
322 "Failed to start daemon with error: `%s'\n", emsg);
325 GNUNET_assert (id != NULL);
327 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
328 (GNUNET_TIME_UNIT_SECONDS, 2),
333 bob_started (void *cls, const struct GNUNET_PeerIdentity *id,
334 const struct GNUNET_CONFIGURATION_Handle *cfg,
335 struct GNUNET_TESTING_Daemon *d, const char *emsg)
339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
340 "Failed to start daemon with error: `%s'\n", emsg);
343 GNUNET_assert (id != NULL);
345 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
346 (GNUNET_TIME_UNIT_SECONDS, 2),
351 run (void *cls, char *const *args, const char *cfgfile,
352 const struct GNUNET_CONFIGURATION_Handle *cfg)
355 struct GNUNET_NAMESTORE_Handle* namestore_handle;
356 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey, bob_pkey;
357 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key, *bob_key;
361 /* Get path from configuration file */
363 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
370 /* Get number of peers to start from configuration (should be two) */
372 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
374 num_peers = DEFAULT_NUM_PEERS;
376 /* Set peers_left so we know when all peers started */
377 peers_left = num_peers;
379 /* Somebody care to explain? */
380 uint16_t port = 6000;
385 * Modify some config options for bob
386 * namely swap keys and disable dns hijacking
388 struct GNUNET_CONFIGURATION_Handle *cfg2 = GNUNET_TESTING_create_cfg(cfg,
392 GNUNET_CONFIGURATION_set_value_string (cfg2, "paths", "servicehome",
393 "/tmp/test-gnunetd-gns-peer-2/");
394 GNUNET_CONFIGURATION_set_value_string (cfg2, "gns", "HIJACK_DNS",
396 GNUNET_CONFIGURATION_set_value_string (cfg2, "gns", "ZONEKEY",
399 /* put records into namestore */
400 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
401 if (NULL == namestore_handle)
403 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
408 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns",
412 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get alice's key from cfg\n");
417 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg2, "gns",
421 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get bob's key from cfg\n");
426 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
427 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (bob_keyfile);
429 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
430 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
432 struct GNUNET_NAMESTORE_RecordData rd;
434 rd.expiration = GNUNET_TIME_absolute_get_forever ();
435 rd.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
436 rd.record_type = GNUNET_GNS_RECORD_PKEY;
438 GNUNET_NAMESTORE_record_create (namestore_handle,
445 rd.data = &alice_pkey;
446 GNUNET_NAMESTORE_record_create (namestore_handle,
453 char* ip = "127.0.0.1";
454 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
455 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
457 rd.data_size = sizeof(struct in_addr);
459 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
461 GNUNET_NAMESTORE_record_create (namestore_handle,
468 /* Set up a task to end testing if peer start fails */
470 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
471 "didn't start all daemons in reasonable amount of time!!!");
475 expected_connections = 1;
478 d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
479 NULL, NULL, NULL, &alice_started, NULL);
483 d2 = GNUNET_TESTING_daemon_start(cfg2, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
484 NULL, NULL, NULL, &bob_started, NULL);
494 /* Arguments for GNUNET_PROGRAM_run */
495 char *const argv[] = { "test-gns-twopeer", /* Name to give running binary */
497 "test_gns_twopeer.conf", /* Config file to use */
503 struct GNUNET_GETOPT_CommandLineOption options[] = {
504 GNUNET_GETOPT_OPTION_END
506 /* Run the run function as a new program */
508 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
509 "test-gns-twopeer", "nohelp", options, &run,
511 if (ret != GNUNET_OK)
513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
514 "`test-gns-twopeer': Failed with error code %d\n", ret);
520 main (int argc, char *argv[])
524 GNUNET_log_setup ("test-gns-twopeer",
533 * Need to remove base directory, subdirectories taken care
534 * of by the testing framework.
539 /* end of test_gns_twopeer.c */