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