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_dht_threepeer.c
22 * @brief tests dht lookup over 3 peers
25 * alice <----> bob <-----> dave
27 * alice queries for www.buddy.bob.gnunet
31 #include "gnunet_common.h"
32 #include "gnunet_disk_lib.h"
33 #include "gnunet_testing_lib-new.h"
34 #include "gnunet_testbed_service.h"
35 #include "gnunet_core_service.h"
36 #include "gnunet_dht_service.h"
37 #include "block_dns.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_namestore_service.h"
40 #include "gnunet_dnsparser_lib.h"
41 #include "gnunet_gns_service.h"
43 #define ZONE_PUT_WAIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
45 /* If number of peers not in config file, use this number */
46 #define DEFAULT_NUM_PEERS 2
48 #define TEST_DOMAIN "www.buddy.bob.gnunet"
49 #define TEST_IP "1.1.1.1"
50 #define TEST_DAVE_PSEU "hagbard"
51 #define TEST_NUM_PEERS 3
52 #define TEST_NUM_CON 3
55 /* Timeout for entire testcase */
56 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
58 /* Global return value (0 for success, anything else for failure) */
61 /* Task handle to use to schedule test failure */
62 GNUNET_SCHEDULER_TaskIdentifier die_task;
63 GNUNET_SCHEDULER_TaskIdentifier wait_task;
65 struct GNUNET_CRYPTO_ShortHashCode dave_hash;
67 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
69 struct GNUNET_TESTBED_Peer **cpeers;
71 struct GNUNET_GNS_Handle *gh;
73 struct GNUNET_TESTBED_Operation *get_cfg_ops[3];
74 struct GNUNET_TESTBED_Operation *connect_ops[3];
75 struct GNUNET_CONFIGURATION_Handle *cfg_handles[3];
76 struct GNUNET_NAMESTORE_Handle *nh[3];
79 * Check if the get_handle is being used, if so stop the request. Either
80 * way, schedule the end_badly_cont function which actually shuts down the
84 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 die_task = GNUNET_SCHEDULER_NO_TASK;
89 if (GNUNET_SCHEDULER_NO_TASK != wait_task)
91 GNUNET_SCHEDULER_cancel (wait_task);
92 wait_task = GNUNET_SCHEDULER_NO_TASK;
95 for (c = 0; c < 3; c++)
99 GNUNET_NAMESTORE_disconnect(nh[c]);
102 if (NULL != get_cfg_ops[c])
104 GNUNET_TESTBED_operation_cancel(get_cfg_ops[c]);
105 get_cfg_ops[c] = NULL;
107 if (NULL != connect_ops[c])
109 GNUNET_TESTBED_operation_cancel(connect_ops[c]);
110 connect_ops[c] = NULL;
112 if (NULL != cfg_handles[c])
114 GNUNET_CONFIGURATION_destroy (cfg_handles[c]);
115 cfg_handles[c] = NULL;
121 GNUNET_GNS_disconnect (gh);
125 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test failed \n");
127 GNUNET_SCHEDULER_shutdown ();
132 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 if (GNUNET_SCHEDULER_NO_TASK != die_task)
137 GNUNET_SCHEDULER_cancel (die_task);
138 die_task = GNUNET_SCHEDULER_NO_TASK;
141 for (c = 0; c < 3; c++)
145 GNUNET_NAMESTORE_disconnect(nh[c]);
148 if (NULL != cfg_handles[c])
150 GNUNET_CONFIGURATION_destroy (cfg_handles[c]);
151 cfg_handles[c] = NULL;
155 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test successful \n");
156 GNUNET_SCHEDULER_shutdown ();
162 GNUNET_SCHEDULER_add_now (&end, NULL);
167 disconnect_ns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 GNUNET_NAMESTORE_disconnect (cls);
181 cont_ns (void* cls, int32_t s, const char* emsg)
183 GNUNET_SCHEDULER_add_now (&disconnect_ns, cls);
187 on_lookup_result(void *cls, uint32_t rd_count,
188 const struct GNUNET_NAMESTORE_RecordData *rd)
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
203 for (i=0; i<rd_count; i++)
205 string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
208 if (0 == strcmp(string_val, TEST_IP))
210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
211 "%s correctly resolved to %s!\n", TEST_DOMAIN, string_val);
216 GNUNET_GNS_disconnect(gh);
222 commence_testing(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 if ((ZONE_PUT_WAIT_TIME.rel_value / 1000) == wait)
228 fprintf (stderr, "\n");
229 wait_task = GNUNET_SCHEDULER_NO_TASK;
230 GNUNET_GNS_lookup(gh, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
233 &on_lookup_result, TEST_DOMAIN);
234 if (GNUNET_SCHEDULER_NO_TASK != die_task)
235 GNUNET_SCHEDULER_cancel(die_task);
236 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from lookup");
240 fprintf (stderr, ".");
241 wait_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &commence_testing, NULL);
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created all connections! Waiting for PUTs\n");
249 wait_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &commence_testing, NULL);
253 static void connect_peers ()
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers started\n");
262 connect_ops[0] = GNUNET_TESTBED_overlay_connect (NULL,
266 connect_ops[1] = GNUNET_TESTBED_overlay_connect (NULL,
270 connect_ops[2] = GNUNET_TESTBED_overlay_connect (NULL,
277 setup_dave (const struct GNUNET_CONFIGURATION_Handle * cfg)
281 struct GNUNET_CRYPTO_RsaPrivateKey *key;
282 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
284 struct GNUNET_NAMESTORE_RecordData rd;
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up dave\n");
287 cfg_handles[0] = GNUNET_CONFIGURATION_dup (cfg);
288 GNUNET_assert (NULL != cfg_handles[0]);
289 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg_handles[0], "gns",
293 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
294 return GNUNET_SYSERR;
297 GNUNET_asprintf (&source, "zonefiles%s%s", DIR_SEPARATOR_STR, "test_zonekey");
298 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_test (source));
299 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Copy `%s' to `%s'\n", source, keyfile);
300 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_copy (source, keyfile));
301 GNUNET_free (source);
303 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
307 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
308 GNUNET_free (keyfile);
309 return GNUNET_SYSERR;
312 nh[0] = GNUNET_NAMESTORE_connect (cfg_handles[0]);
315 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
316 GNUNET_CRYPTO_rsa_key_free (key);
317 GNUNET_free (keyfile);
318 return GNUNET_SYSERR;
321 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
322 GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &dave_hash);
324 web = GNUNET_malloc(sizeof(struct in_addr));
325 GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP, web));
326 rd.data_size = sizeof(struct in_addr);
328 rd.record_type = GNUNET_GNS_RECORD_A;
329 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
331 GNUNET_NAMESTORE_record_create (nh[0], key, "www", &rd, NULL, NULL);
333 rd.data_size = strlen(TEST_DAVE_PSEU);
334 rd.data = TEST_DAVE_PSEU;
335 rd.record_type = GNUNET_GNS_RECORD_PSEU;
337 GNUNET_NAMESTORE_record_create (nh[0], key, "+", &rd, &cont_ns, nh[0]);
339 GNUNET_CRYPTO_rsa_key_free(key);
340 GNUNET_free(keyfile);
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up dave done\n");
347 setup_bob (const struct GNUNET_CONFIGURATION_Handle * cfg)
351 struct GNUNET_CRYPTO_RsaPrivateKey *key;
352 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
353 struct GNUNET_NAMESTORE_RecordData rd;
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up bob\n");
356 GNUNET_assert (NULL != cfg);
357 cfg_handles[1] = GNUNET_CONFIGURATION_dup (cfg);
358 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg_handles[1], "gns",
362 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
363 return GNUNET_SYSERR;
366 GNUNET_asprintf (&source, "zonefiles%s%s", DIR_SEPARATOR_STR, "OEFL7A4VEF1B40QLEMTG5D8G1CN6EN16QUSG5R2DT71GRJN34LSG.zkey");
367 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_test (source));
368 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Copy `%s' to `%s'\n", source, keyfile);
369 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_copy (source, keyfile));
370 GNUNET_free (source);
372 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
376 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
377 GNUNET_free (keyfile);
378 return GNUNET_SYSERR;
381 nh[1] = GNUNET_NAMESTORE_connect (cfg_handles[1]);
384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
385 GNUNET_CRYPTO_rsa_key_free (key);
386 GNUNET_free (keyfile);
387 return GNUNET_SYSERR;
390 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
391 GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &bob_hash);
393 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
394 rd.data = &dave_hash;
395 rd.record_type = GNUNET_GNS_RECORD_PKEY;
396 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
398 GNUNET_NAMESTORE_record_create (nh[1], key, "buddy", &rd, &cont_ns, nh[1]);
400 GNUNET_CRYPTO_rsa_key_free(key);
401 GNUNET_free(keyfile);
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up bob done\n");
407 setup_alice (const struct GNUNET_CONFIGURATION_Handle * cfg)
411 struct GNUNET_CRYPTO_RsaPrivateKey *key;
412 struct GNUNET_NAMESTORE_RecordData rd;
415 GNUNET_assert (NULL != cfg);
416 cfg_handles[2] = GNUNET_CONFIGURATION_dup (cfg);
417 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg_handles[2], "gns",
421 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
422 return GNUNET_SYSERR;
425 GNUNET_asprintf (&source, "zonefiles%s%s", DIR_SEPARATOR_STR, "188JSUMKEF25GVU8TTV0PBNNN8JVCPUEDFV1UHJJU884JD25V0T0.zkey");
426 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_test (source));
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copy `%s' to `%s'\n", source, keyfile);
428 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_copy (source, keyfile));
429 GNUNET_free (source);
431 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
435 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
436 GNUNET_free (keyfile);
437 return GNUNET_SYSERR;
440 nh[2] = GNUNET_NAMESTORE_connect (cfg_handles[2]);
443 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
444 GNUNET_CRYPTO_rsa_key_free (key);
445 GNUNET_free (keyfile);
446 return GNUNET_SYSERR;
449 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
451 rd.record_type = GNUNET_GNS_RECORD_PKEY;
452 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
454 GNUNET_NAMESTORE_record_create (nh[2], key, "bob", &rd, &cont_ns, nh[2]);
456 gh = GNUNET_GNS_connect(cfg_handles[2]);
459 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
460 GNUNET_CRYPTO_rsa_key_free (key);
461 GNUNET_free (keyfile);
462 return GNUNET_SYSERR;
465 GNUNET_CRYPTO_rsa_key_free (key);
466 GNUNET_free (keyfile);
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up alice done\n");
474 if (GNUNET_SCHEDULER_NO_TASK != die_task)
475 GNUNET_SCHEDULER_cancel (die_task);
476 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
479 void testbed_master (void *cls,
480 unsigned int num_peers,
481 struct GNUNET_TESTBED_Peer **peers)
483 GNUNET_assert (NULL != peers);
487 GNUNET_assert (NULL != peers[0]);
488 get_cfg_ops[0] = GNUNET_TESTBED_peer_get_information (peers[0], GNUNET_TESTBED_PIT_CONFIGURATION);
491 GNUNET_assert (NULL != peers[1]);
492 get_cfg_ops[1] = GNUNET_TESTBED_peer_get_information (peers[1], GNUNET_TESTBED_PIT_CONFIGURATION);
495 GNUNET_assert (NULL != peers[2]);
496 get_cfg_ops[2] = GNUNET_TESTBED_peer_get_information (peers[2], GNUNET_TESTBED_PIT_CONFIGURATION);
500 void testbed_controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
502 static int connections = 0;
507 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
508 if (get_cfg_ops[0] == event->details.operation_finished.operation)
510 res = setup_dave (event->details.operation_finished.op_result.cfg);
511 GNUNET_TESTBED_operation_done (get_cfg_ops[0]);
512 get_cfg_ops[0] = NULL;
513 if (GNUNET_SYSERR == res)
515 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup dave \n");
523 else if (get_cfg_ops[1] == event->details.operation_finished.operation)
525 res = setup_bob (event->details.operation_finished.op_result.cfg);
526 GNUNET_TESTBED_operation_done (get_cfg_ops[1]);
527 get_cfg_ops[1] = NULL;
528 if (GNUNET_SYSERR == res)
530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup dave \n");
538 else if (get_cfg_ops[2] == event->details.operation_finished.operation)
540 res = setup_alice (event->details.operation_finished.op_result.cfg);
541 GNUNET_TESTBED_operation_done (get_cfg_ops[2]);
542 get_cfg_ops[2] = NULL;
543 if (GNUNET_SYSERR == res)
545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup dave \n");
554 case GNUNET_TESTBED_ET_CONNECT:
556 if (connections == 3)
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers connected\n");
559 GNUNET_TESTBED_operation_done (connect_ops[0]);
560 connect_ops[0] = NULL;
561 GNUNET_TESTBED_operation_done (connect_ops[1]);
562 connect_ops[1] = NULL;
563 GNUNET_TESTBED_operation_done (connect_ops[2]);
564 connect_ops[2] = NULL;
575 main (int argc, char *argv[])
581 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
582 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
583 GNUNET_TESTBED_test_run ("test_gns_dht_three_peers", "test_gns_dht_default.conf",
585 &testbed_controller_cb, NULL,
586 &testbed_master, NULL);
587 if (GNUNET_SYSERR == ok)
592 /* end of test_gns_dht_three_peers.c */