9a7a0bdf3c89e3e95fcf431f3869bc86fa4ebc8b
[oweals/gnunet.git] / src / gns / test_gns_dht_threepeer.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 gns/test_gns_dht_threepeer.c
22  * @brief tests dht lookup over 3 peers
23  *
24  * topology:
25  * alice <----> bob <-----> dave
26  *
27  * alice queries for www.buddy.bob.gnunet
28  *
29  */
30 #include "platform.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_dht_service.h"
35 #include "block_dns.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_namestore_service.h"
38 #include "gnunet_dnsparser_lib.h"
39 #include "gnunet_gns_service.h"
40
41 /* DEFINES */
42 #define VERBOSE GNUNET_YES
43
44 /* Timeout for entire testcase */
45 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 180)
46 #define ZONE_PUT_WAIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
47
48 /* If number of peers not in config file, use this number */
49 #define DEFAULT_NUM_PEERS 2
50
51 #define TEST_DOMAIN "www.buddy.bob.gnunet"
52 #define TEST_IP "1.1.1.1"
53 #define TEST_DAVE_PSEU "hagbard"
54 #define TEST_NUM_PEERS 3
55 #define TEST_NUM_CON 3
56
57 /* Globals */
58
59 /**
60  * Directory to store temp data in, defined in config file
61  */
62 static char *test_directory;
63
64 /**
65  * Variable used to store the number of connections we should wait for.
66  */
67 static unsigned int expected_connections;
68
69 /**
70  * Variable used to keep track of how many peers aren't yet started.
71  */
72 static unsigned long long peers_left;
73
74 struct GNUNET_TESTING_Daemon *d1;
75 struct GNUNET_TESTING_Daemon *d2;
76 struct GNUNET_TESTING_Daemon *d3;
77
78
79 /**
80  * Total number of peers to run, set based on config file.
81  */
82 static unsigned long long num_peers;
83
84 /**
85  * Global used to count how many connections we have currently
86  * been notified about (how many times has topology_callback been called
87  * with success?)
88  */
89 static unsigned int total_connections;
90
91 /**
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?)
95  */
96 static unsigned int failed_connections;
97
98 /* Task handle to use to schedule test failure */
99 GNUNET_SCHEDULER_TaskIdentifier die_task;
100
101 GNUNET_SCHEDULER_TaskIdentifier bob_task;
102
103 /* Global return value (0 for success, anything else for failure) */
104 static int ok;
105
106 int bob_online, alice_online, dave_online;
107
108 const struct GNUNET_CONFIGURATION_Handle *alice_cfg;
109 struct GNUNET_CONFIGURATION_Handle *cfg_bob;
110 struct GNUNET_CONFIGURATION_Handle *cfg_dave;
111
112 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
113 struct GNUNET_CRYPTO_ShortHashCode dave_hash;
114 struct GNUNET_TESTING_Daemon *alice_daemon;
115 struct GNUNET_TESTING_Daemon *bob_daemon;
116 struct GNUNET_TESTING_Daemon *dave_daemon;
117
118 struct GNUNET_TESTING_PeerGroup *pg;
119 struct GNUNET_GNS_Handle *gh;
120
121 /**
122  * Function scheduled to be run on the successful completion of this
123  * testcase.  Specifically, called when our get request completes.
124  */
125 static void
126 finish_testing (void *cls, const char *emsg)
127 {
128   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test finished! (ret=%d)\n", ok);
129 }
130
131 /**
132  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
133  * down the peers without freeing memory associated with GET request.
134  */
135 static void
136 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 {
138   die_task = GNUNET_SCHEDULER_NO_TASK;
139   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &finish_testing, NULL);
140 }
141
142 /**
143  * Check if the get_handle is being used, if so stop the request.  Either
144  * way, schedule the end_badly_cont function which actually shuts down the
145  * test.
146  */
147 static void
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 {
150   die_task = GNUNET_SCHEDULER_NO_TASK;
151   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failing test with error: `%s'!\n",
152               (char *) cls);
153   die_task = GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
154   ok = 1;
155 }
156
157
158 static void
159 on_lookup_result(void *cls, uint32_t rd_count,
160                  const struct GNUNET_NAMESTORE_RecordData *rd)
161 {
162   int i;
163   char* string_val;
164
165   if (rd_count == 0)
166   {
167     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168                 "Lookup failed!\n");
169     ok = 2;
170   }
171   else
172   {
173     ok = 1;
174     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
175     for (i=0; i<rd_count; i++)
176     {
177       string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
178                                                     rd[i].data,
179                                                     rd[i].data_size);
180       if (0 == strcmp(string_val, TEST_IP))
181       {
182         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
183                     "%s correctly resolved to %s!\n", TEST_DOMAIN, string_val);
184         ok = 0;
185       }
186     }
187   }
188   GNUNET_GNS_disconnect(gh);
189   GNUNET_SCHEDULER_cancel(die_task);
190   die_task = GNUNET_SCHEDULER_NO_TASK;
191   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &finish_testing, NULL);
192   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down!\n");
193
194 }
195
196 static void
197 commence_testing(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
198 {
199   
200   gh = GNUNET_GNS_connect(alice_cfg);
201
202   GNUNET_GNS_lookup(gh, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
203                     GNUNET_NO,
204                     NULL,
205                     &on_lookup_result, TEST_DOMAIN);
206   die_task =
207     GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from lookup");
208 }
209
210
211 /**
212  * This function is called whenever a connection attempt is finished between two of
213  * the started peers (started with GNUNET_TESTING_daemons_start).  The total
214  * number of times this function is called should equal the number returned
215  * from the GNUNET_TESTING_connect_topology call.
216  *
217  * The emsg variable is NULL on success (peers connected), and non-NULL on
218  * failure (peers failed to connect).
219  */
220 void
221 daemon_connected (void *cls, const struct GNUNET_PeerIdentity *first,
222                    const struct GNUNET_PeerIdentity *second, uint32_t distance,
223                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
224                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
225                    struct GNUNET_TESTING_Daemon *first_daemon,
226                    struct GNUNET_TESTING_Daemon *second_daemon,
227                    const char *emsg)
228 {
229   if (emsg == NULL)
230   {
231     total_connections++;
232 #if VERBOSE
233     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
234                 "connected peer %s to peer %s, distance %u\n",
235                 first_daemon->shortname, second_daemon->shortname, distance);
236 #endif
237   }
238 #if VERBOSE
239   else
240   {
241     failed_connections++;
242     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243                 "Failed to connect peer %s to peer %s with error :\n%s\n",
244                 first_daemon->shortname, second_daemon->shortname, emsg);
245   }
246 #endif
247
248   if (total_connections == expected_connections)
249   {
250 #if VERBOSE
251     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252                 "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
253                 total_connections);
254 #endif
255     GNUNET_SCHEDULER_cancel (die_task);
256     die_task = GNUNET_SCHEDULER_NO_TASK;
257     //die_task =
258     //    GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from connect");
259    
260     //commence_testing();
261     
262   }
263   else if (total_connections + failed_connections == expected_connections)
264   {
265     GNUNET_SCHEDULER_cancel (die_task);
266     die_task =
267         GNUNET_SCHEDULER_add_now (&end_badly,
268                                   "from topology_callback (too many failed connections)");
269   }
270 }
271
272 void
273 all_connected(void *cls, const char *emsg)
274 {
275   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
276               "Created all connections!  Starting next phase of testing.\n");
277   GNUNET_SCHEDULER_add_delayed (ZONE_PUT_WAIT_TIME, &commence_testing, NULL);
278 }
279
280 static void
281 disconnect_ns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
282 {
283   GNUNET_NAMESTORE_disconnect (cls);
284 }
285
286 static void
287 cont_ns (void* cls, int32_t s, const char* emsg)
288 {
289   GNUNET_SCHEDULER_add_now (&disconnect_ns, cls);
290 }
291
292 static void
293 daemon_started (void *cls, const struct GNUNET_PeerIdentity *id,
294                 const struct GNUNET_CONFIGURATION_Handle *cfg,
295                 struct GNUNET_TESTING_Daemon *d, const char *emsg)
296 {
297   struct GNUNET_NAMESTORE_Handle *ns;
298   char* keyfile;
299   struct GNUNET_CRYPTO_RsaPrivateKey *key;
300   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
301   struct in_addr *web;
302   struct GNUNET_NAMESTORE_RecordData rd;
303
304   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY | GNUNET_NAMESTORE_RF_NONE;
305   rd.expiration_time = UINT64_MAX;
306   
307   if (NULL == dave_daemon)
308   {
309     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
310                                                               "ZONEKEY",
311                                                               &keyfile))
312     {
313       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
314       ok = -1;
315       return;
316     }
317     dave_daemon = d;
318
319     key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
320
321     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is now dave\n");
322     ns = GNUNET_NAMESTORE_connect(cfg);
323     
324     GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
325     GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &dave_hash);
326     
327     web = GNUNET_malloc(sizeof(struct in_addr));
328     GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP, web));
329     rd.data_size = sizeof(struct in_addr);
330     rd.data = web;
331     rd.record_type = GNUNET_GNS_RECORD_A;
332     rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
333
334     GNUNET_NAMESTORE_record_create (ns, key, "www", &rd, NULL, NULL);
335
336     rd.data_size = strlen(TEST_DAVE_PSEU);
337     rd.data = TEST_DAVE_PSEU;
338     rd.record_type = GNUNET_GNS_RECORD_PSEU;
339
340     GNUNET_NAMESTORE_record_create (ns, key, "+", &rd, &cont_ns, ns);
341
342     GNUNET_CRYPTO_rsa_key_free(key);
343     GNUNET_free(keyfile);
344     GNUNET_free(web);
345
346     return;
347   }
348   
349   
350   if (NULL == bob_daemon)
351   {
352     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
353                                                               "ZONEKEY",
354                                                               &keyfile))
355     {
356       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
357       ok = -1;
358       return;
359     }
360     bob_daemon = d;
361
362     key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
363
364     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is now bob\n");
365     ns = GNUNET_NAMESTORE_connect(cfg);
366     
367     GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
368     GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &bob_hash);
369     
370     rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
371     rd.data = &dave_hash;
372     rd.record_type = GNUNET_GNS_RECORD_PKEY;
373     rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
374
375     GNUNET_NAMESTORE_record_create (ns, key, "buddy", &rd, &cont_ns, ns);
376
377     GNUNET_CRYPTO_rsa_key_free(key);
378     GNUNET_free(keyfile);
379
380     return;
381   }
382
383   
384   
385   if (NULL == alice_daemon)
386   {
387
388     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
389                                                               "ZONEKEY",
390                                                               &keyfile))
391     {
392       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
393       ok = -1;
394       return;
395     }
396     alice_daemon = d;
397     alice_cfg = cfg;
398
399     key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
400
401     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is now alice\n");
402     ns = GNUNET_NAMESTORE_connect(cfg);
403     
404     rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
405     rd.data = &bob_hash;
406     rd.record_type = GNUNET_GNS_RECORD_PKEY;
407     rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
408
409     GNUNET_NAMESTORE_record_create (ns, key, "bob", &rd, &cont_ns, ns);
410
411     GNUNET_CRYPTO_rsa_key_free(key);
412     GNUNET_free(keyfile);
413
414     GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE,
415                                      GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
416                                      0,
417                                      TIMEOUT,
418                                      3,
419                                      &all_connected, NULL);
420     return;
421
422   }
423
424   
425
426   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is a random guy\n");
427 }
428
429 static void
430 run (void *cls, char *const *args, const char *cfgfile,
431      const struct GNUNET_CONFIGURATION_Handle *cfg)
432 {
433
434   /* Get path from configuration file */
435   if (GNUNET_YES !=
436       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
437                                              &test_directory))
438   {
439     ok = 404;
440     return;
441   }
442
443   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "starting\n");
444
445   /* Get number of peers to start from configuration (should be two) */
446   if (GNUNET_SYSERR ==
447       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
448                                              &num_peers))
449     num_peers = DEFAULT_NUM_PEERS;
450
451   /* Set peers_left so we know when all peers started */
452   peers_left = num_peers;
453   
454   bob_daemon = NULL;
455   dave_daemon = NULL;
456   alice_daemon = NULL;
457
458   pg = GNUNET_TESTING_daemons_start (cfg, TEST_NUM_PEERS, TEST_NUM_CON,
459                                 TEST_NUM_CON, TIMEOUT, NULL, NULL, &daemon_started, NULL,
460                                 &daemon_connected, NULL, NULL);
461   
462   /* Set up a task to end testing if peer start fails */
463   die_task =
464       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
465                                     "didn't start all daemons in reasonable amount of time!!!");
466   
467   alice_online = 0;
468   bob_online = 0;
469   dave_online = 0;
470   expected_connections = 2;
471   
472   /* Start alice */
473   //d1 = GNUNET_TESTING_daemon_start(cfg_alice, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
474   //                                 NULL, NULL, NULL, &alice_started, NULL);
475   
476   
477
478
479 }
480
481 static int
482 check ()
483 {
484   int ret;
485
486   /* Arguments for GNUNET_PROGRAM_run */
487   char *const argv[] = { "test-gns-twopeer",    /* Name to give running binary */
488     "-c",
489     "test_gns_dht_default.conf",       /* Config file to use */
490 #if VERBOSE
491     "-L", "DEBUG",
492 #endif
493     NULL
494   };
495   struct GNUNET_GETOPT_CommandLineOption options[] = {
496     GNUNET_GETOPT_OPTION_END
497   };
498   /* Run the run function as a new program */
499   ret =
500       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
501                           "test-gns-threepeer", "nohelp", options, &run,
502                           &ok);
503   if (ret != GNUNET_OK)
504   {
505     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
506                 "`test-gns-threepeer': Failed with error code %d\n", ret);
507   }
508   return ok;
509 }
510
511 int
512 main (int argc, char *argv[])
513 {
514   int ret;
515
516   GNUNET_log_setup ("test-gns-threepeer",
517 #if VERBOSE
518                     "DEBUG",
519 #else
520                     "WARNING",
521 #endif
522                     NULL);
523   ret = check ();
524   /**
525    * Need to remove base directory, subdirectories taken care
526    * of by the testing framework.
527    */
528   return ret;
529 }
530
531 /* end of test_gns_threepeer.c */