- added check against statistics
[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, 80)
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   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &finish_testing, NULL);
139 }
140
141 /**
142  * Check if the get_handle is being used, if so stop the request.  Either
143  * way, schedule the end_badly_cont function which actually shuts down the
144  * test.
145  */
146 static void
147 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
148 {
149   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failing test with error: `%s'!\n",
150               (char *) cls);
151   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
152   ok = 1;
153 }
154
155
156 static void
157 on_lookup_result(void *cls, uint32_t rd_count,
158                  const struct GNUNET_NAMESTORE_RecordData *rd)
159 {
160   int i;
161   char* string_val;
162   const char* typename;
163
164   if (rd_count == 0)
165   {
166     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167                 "Lookup failed!\n");
168     ok = 2;
169   }
170   else
171   {
172     ok = 1;
173     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
174     for (i=0; i<rd_count; i++)
175     {
176       typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
177       string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
178                                                     rd[i].data,
179                                                     rd[i].data_size);
180       printf("Got %s record: %s\n", typename, string_val);
181       if (0 == strcmp(string_val, TEST_IP))
182       {
183         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
184                     "%s correctly resolved to %s!\n", TEST_DOMAIN, string_val);
185         ok = 0;
186       }
187     }
188   }
189   GNUNET_GNS_disconnect(gh);
190   GNUNET_SCHEDULER_cancel(die_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
201   gh = GNUNET_GNS_connect(alice_cfg);
202
203   GNUNET_GNS_lookup(gh, TEST_DOMAIN, GNUNET_GNS_RECORD_TYPE_A,
204                     &on_lookup_result, TEST_DOMAIN);
205   die_task =
206     GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from lookup");
207 }
208
209
210 /**
211  * This function is called whenever a connection attempt is finished between two of
212  * the started peers (started with GNUNET_TESTING_daemons_start).  The total
213  * number of times this function is called should equal the number returned
214  * from the GNUNET_TESTING_connect_topology call.
215  *
216  * The emsg variable is NULL on success (peers connected), and non-NULL on
217  * failure (peers failed to connect).
218  */
219 void
220 daemon_connected (void *cls, const struct GNUNET_PeerIdentity *first,
221                    const struct GNUNET_PeerIdentity *second, uint32_t distance,
222                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
223                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
224                    struct GNUNET_TESTING_Daemon *first_daemon,
225                    struct GNUNET_TESTING_Daemon *second_daemon,
226                    const char *emsg)
227 {
228   if (emsg == NULL)
229   {
230     total_connections++;
231 #if VERBOSE
232     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
233                 "connected peer %s to peer %s, distance %u\n",
234                 first_daemon->shortname, second_daemon->shortname, distance);
235 #endif
236   }
237 #if VERBOSE
238   else
239   {
240     failed_connections++;
241     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
242                 "Failed to connect peer %s to peer %s with error :\n%s\n",
243                 first_daemon->shortname, second_daemon->shortname, emsg);
244   }
245 #endif
246
247   if (total_connections == expected_connections)
248   {
249 #if VERBOSE
250     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
251                 "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
252                 total_connections);
253 #endif
254     GNUNET_SCHEDULER_cancel (die_task);
255     //die_task =
256     //    GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from connect");
257    
258     //commence_testing();
259     
260   }
261   else if (total_connections + failed_connections == expected_connections)
262   {
263     GNUNET_SCHEDULER_cancel (die_task);
264     die_task =
265         GNUNET_SCHEDULER_add_now (&end_badly,
266                                   "from topology_callback (too many failed connections)");
267   }
268 }
269
270 void
271 all_connected(void *cls, const char *emsg)
272 {
273   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
274               "Created all connections!  Starting next phase of testing.\n");
275   GNUNET_SCHEDULER_add_delayed (ZONE_PUT_WAIT_TIME, &commence_testing, NULL);
276 }
277
278 void
279 ns_create_cont(void *cls, int32_t s, const char *emsg)
280 {
281   GNUNET_NAMESTORE_disconnect((struct GNUNET_NAMESTORE_Handle *)cls, 0);
282 }
283
284 static void
285 daemon_started (void *cls, const struct GNUNET_PeerIdentity *id,
286                 const struct GNUNET_CONFIGURATION_Handle *cfg,
287                 struct GNUNET_TESTING_Daemon *d, const char *emsg)
288 {
289   struct GNUNET_NAMESTORE_Handle *ns;
290   char* keyfile;
291   struct GNUNET_CRYPTO_RsaPrivateKey *key;
292   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
293   struct in_addr *web;
294   struct GNUNET_NAMESTORE_RecordData rd;
295
296   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY | GNUNET_NAMESTORE_RF_NONE;
297   rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
298   
299   if (NULL == dave_daemon)
300   {
301     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
302                                                               "ZONEKEY",
303                                                               &keyfile))
304     {
305       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
306       ok = -1;
307       return;
308     }
309     dave_daemon = d;
310
311     key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
312
313     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is now dave\n");
314     ns = GNUNET_NAMESTORE_connect(cfg);
315     
316     GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
317     GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &dave_hash);
318     
319     web = GNUNET_malloc(sizeof(struct in_addr));
320     GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP, web));
321     rd.data_size = sizeof(struct in_addr);
322     rd.data = web;
323     rd.record_type = GNUNET_GNS_RECORD_TYPE_A;
324
325     GNUNET_NAMESTORE_record_create (ns, key, "www", &rd, NULL, NULL);
326
327     rd.data_size = strlen(TEST_DAVE_PSEU);
328     rd.data = TEST_DAVE_PSEU;
329     rd.record_type = GNUNET_GNS_RECORD_PSEU;
330
331     GNUNET_NAMESTORE_record_create (ns, key, "+", &rd, ns_create_cont, ns);
332
333     GNUNET_CRYPTO_rsa_key_free(key);
334     GNUNET_free(keyfile);
335     GNUNET_free(web);
336
337     return;
338   }
339   
340   
341   if (NULL == bob_daemon)
342   {
343     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
344                                                               "ZONEKEY",
345                                                               &keyfile))
346     {
347       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
348       ok = -1;
349       return;
350     }
351     bob_daemon = d;
352
353     key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
354
355     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is now bob\n");
356     ns = GNUNET_NAMESTORE_connect(cfg);
357     
358     GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
359     GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &bob_hash);
360     
361     rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
362     rd.data = &dave_hash;
363     rd.record_type = GNUNET_GNS_RECORD_PKEY;
364
365     GNUNET_NAMESTORE_record_create (ns, key, "buddy", &rd, ns_create_cont, ns);
366
367     GNUNET_CRYPTO_rsa_key_free(key);
368     GNUNET_free(keyfile);
369
370     return;
371   }
372
373   
374   
375   if (NULL == alice_daemon)
376   {
377
378     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
379                                                               "ZONEKEY",
380                                                               &keyfile))
381     {
382       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
383       ok = -1;
384       return;
385     }
386     alice_daemon = d;
387     alice_cfg = cfg;
388
389     key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
390
391     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is now alice\n");
392     ns = GNUNET_NAMESTORE_connect(cfg);
393     
394     rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
395     rd.data = &bob_hash;
396     rd.record_type = GNUNET_GNS_RECORD_PKEY;
397
398     GNUNET_NAMESTORE_record_create (ns, key, "bob", &rd, ns_create_cont, ns);
399
400     GNUNET_CRYPTO_rsa_key_free(key);
401     GNUNET_free(keyfile);
402
403     GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE,
404                                      GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
405                                      0,
406                                      TIMEOUT,
407                                      3,
408                                      &all_connected, NULL);
409     return;
410
411   }
412
413   
414
415   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is a random guy\n");
416 }
417
418 static void
419 run (void *cls, char *const *args, const char *cfgfile,
420      const struct GNUNET_CONFIGURATION_Handle *cfg)
421 {
422
423   /* Get path from configuration file */
424   if (GNUNET_YES !=
425       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
426                                              &test_directory))
427   {
428     ok = 404;
429     return;
430   }
431
432   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "starting\n");
433
434   /* Get number of peers to start from configuration (should be two) */
435   if (GNUNET_SYSERR ==
436       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
437                                              &num_peers))
438     num_peers = DEFAULT_NUM_PEERS;
439
440   /* Set peers_left so we know when all peers started */
441   peers_left = num_peers;
442   
443   bob_daemon = NULL;
444   dave_daemon = NULL;
445   alice_daemon = NULL;
446
447   pg = GNUNET_TESTING_daemons_start (cfg, TEST_NUM_PEERS, TEST_NUM_CON,
448                                 TEST_NUM_CON, TIMEOUT, NULL, NULL, &daemon_started, NULL,
449                                 &daemon_connected, NULL, NULL);
450   
451   /* Set up a task to end testing if peer start fails */
452   die_task =
453       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
454                                     "didn't start all daemons in reasonable amount of time!!!");
455   
456   alice_online = 0;
457   bob_online = 0;
458   dave_online = 0;
459   expected_connections = 2;
460   
461   /* Start alice */
462   //d1 = GNUNET_TESTING_daemon_start(cfg_alice, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
463   //                                 NULL, NULL, NULL, &alice_started, NULL);
464   
465   
466
467
468 }
469
470 static int
471 check ()
472 {
473   int ret;
474
475   /* Arguments for GNUNET_PROGRAM_run */
476   char *const argv[] = { "test-gns-twopeer",    /* Name to give running binary */
477     "-c",
478     "test_gns_dht_default.conf",       /* Config file to use */
479 #if VERBOSE
480     "-L", "DEBUG",
481 #endif
482     NULL
483   };
484   struct GNUNET_GETOPT_CommandLineOption options[] = {
485     GNUNET_GETOPT_OPTION_END
486   };
487   /* Run the run function as a new program */
488   ret =
489       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
490                           "test-gns-twopeer", "nohelp", options, &run,
491                           &ok);
492   if (ret != GNUNET_OK)
493   {
494     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
495                 "`test-gns-twopeer': Failed with error code %d\n", ret);
496   }
497   return ok;
498 }
499
500 int
501 main (int argc, char *argv[])
502 {
503   int ret;
504
505   GNUNET_log_setup ("test-gns-twopeer",
506 #if VERBOSE
507                     "DEBUG",
508 #else
509                     "WARNING",
510 #endif
511                     NULL);
512   ret = check ();
513   /**
514    * Need to remove base directory, subdirectories taken care
515    * of by the testing framework.
516    */
517   return ret;
518 }
519
520 /* end of test_gns_twopeer.c */