-big commit, make new ns and valgrind happy
[oweals/gnunet.git] / src / gns / test_gns_pseu_shorten.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_pseu_shorten.c
22  * @brief base testcase for testing on the fly pseu import and shorten
23  *
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_core_service.h"
28 #include "block_gns.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_namestore_service.h"
31 #include "../namestore/namestore.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_dht_service.h"
34 #include "gnunet_gns_service.h"
35
36 /* DEFINES */
37 #define VERBOSE GNUNET_YES
38
39 /* Timeout for entire testcase */
40 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
41
42 /* If number of peers not in config file, use this number */
43 #define DEFAULT_NUM_PEERS 2
44
45 /* test records to resolve */
46 #define TEST_DOMAIN "www.alicewonderland.bobbuilder.gnunet"
47 #define TEST_IP "127.0.0.1"
48 #define TEST_RECORD_NAME "www"
49
50 #define TEST_PRIVATE_ZONE "private"
51 #define TEST_SHORTEN_ZONE "short"
52 #define TEST_AUTHORITY_BOB "bobbuilder"
53 #define TEST_AUTHORITY_ALICE "alicewonderland"
54 #define TEST_PSEU_ALICE "carol"
55 #define TEST_EXPECTED_RESULT "www.carol.short.private.gnunet"
56
57 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
58
59 #define KEYFILE_SHORTEN = "zonefiles/188JSUMKEF25GVU8TTV0PBNNN8JVCPUEDFV1UHJJU884JD25V0T0.zkey"
60 #define KEYFILE_PRIVATE = "zonefiles/OEFL7A4VEF1B40QLEMTG5D8G1CN6EN16QUSG5R2DT71GRJN34LSG.zkey"
61 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
62 #define KEYFILE_ALICE "../namestore/zonefiles/N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"
63
64 /* Globals */
65
66 /**
67  * Directory to store temp data in, defined in config file
68  */
69 static char *test_directory;
70
71 static struct GNUNET_TESTING_PeerGroup *pg;
72
73 /* Task handle to use to schedule test failure */
74 static GNUNET_SCHEDULER_TaskIdentifier die_task;
75
76 static GNUNET_SCHEDULER_TaskIdentifier disco_task;
77
78 /* Global return value (0 for success, anything else for failure) */
79 static int ok;
80
81 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
82
83 static struct GNUNET_GNS_Handle *gns_handle;
84
85 static struct GNUNET_DHT_Handle *dht_handle;
86
87 const struct GNUNET_CONFIGURATION_Handle *cfg;
88
89 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
90 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
91 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded our_pkey;
92 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded priv_pkey;
93 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded short_pkey;
94 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
95 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
96 struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
97 struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
98 struct GNUNET_CRYPTO_RsaPrivateKey *short_key;
99 struct GNUNET_CRYPTO_ShortHashCode alice_hash;
100 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
101 struct GNUNET_CRYPTO_ShortHashCode our_zone;
102 struct GNUNET_CRYPTO_ShortHashCode priv_zone;
103 struct GNUNET_CRYPTO_ShortHashCode short_zone;
104
105 /**
106  * Check whether peers successfully shut down.
107  */
108 void
109 shutdown_callback (void *cls, const char *emsg)
110 {
111   if (disco_task != GNUNET_SCHEDULER_NO_TASK)
112   {
113     disco_task = GNUNET_SCHEDULER_NO_TASK;
114     GNUNET_SCHEDULER_cancel(disco_task);
115     GNUNET_DHT_disconnect(dht_handle);
116     dht_handle = NULL;
117   }
118
119   if (emsg != NULL)
120   {
121     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
122     if (ok == 0)
123       ok = 2;
124   }
125
126   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
127 }
128
129 static void
130 disco_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
131 {
132   disco_task = GNUNET_SCHEDULER_NO_TASK;
133   GNUNET_DHT_disconnect(dht_handle);
134   dht_handle = NULL;
135 }
136
137 /**
138  * Called when gns shorten finishes
139  */
140 static void
141 process_shorten_result(void* cls, const char* sname)
142 {
143   GNUNET_GNS_disconnect(gns_handle);
144   //GNUNET_SCHEDULER_add_now(disco_dht, NULL);
145   ok = 0;
146
147   if (sname == NULL)
148   {
149     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150                 "shorten test failed!\n");
151     ok = 1;
152   }
153   else
154   {
155     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156                 "%s shortened to %s\n", (char*)cls, sname);
157     if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
158     {
159       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160                   "shorten test failed! (wanted: %s got: %s\n",
161                   TEST_EXPECTED_RESULT, sname);
162       ok = 1;
163     }
164
165     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
166   }
167
168   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
169   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
170 }
171
172 static void
173 do_shorten(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
174 {
175   GNUNET_GNS_shorten_zone (gns_handle, TEST_DOMAIN,
176                      &our_zone,
177                      &process_shorten_result,
178                      TEST_DOMAIN);
179 }
180
181 static void
182 on_lookup_result(void *cls, uint32_t rd_count,
183                  const struct GNUNET_NAMESTORE_RecordData *rd)
184 {
185   struct in_addr a;
186   int i;
187   char* addr;
188   
189   if (rd_count == 0)
190   {
191     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192                 "Lookup failed, rp_filtering?\n");
193     ok = 2;
194   }
195   else
196   {
197     ok = 1;
198     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
199     for (i=0; i<rd_count; i++)
200     {
201       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
202       if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
203       {
204         memcpy(&a, rd[i].data, sizeof(a));
205         addr = inet_ntoa(a);
206         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
207         if (0 == strcmp(addr, TEST_IP))
208         {
209           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
210                     "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
211           ok = 0;
212         }
213       }
214       else
215       {
216         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
217       }
218     }
219   }
220   
221   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shorten, NULL);
222 }
223
224
225 /**
226  * Function scheduled to be run on the successful start of services
227  * tries to look up the dns record for TEST_DOMAIN
228  */
229 static void
230 commence_testing (void *cls, int success)
231 {
232   GNUNET_SCHEDULER_add_now(disco_dht, NULL);
233   //GNUNET_DHT_disconnect(dht_handle);
234
235   GNUNET_CRYPTO_rsa_key_free(our_key);
236   GNUNET_CRYPTO_rsa_key_free(bob_key);
237   GNUNET_CRYPTO_rsa_key_free(alice_key);
238
239   GNUNET_NAMESTORE_disconnect (namestore_handle);
240   gns_handle = GNUNET_GNS_connect(cfg);
241
242   if (NULL == gns_handle)
243   {
244     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245                 "Failed to connect to GNS!\n");
246   }
247
248   GNUNET_GNS_lookup_zone (gns_handle, TEST_DOMAIN,
249                           &our_zone,
250                           GNUNET_GNS_RECORD_TYPE_A,
251                           GNUNET_NO,
252                           short_key,
253                           &on_lookup_result, TEST_DOMAIN);
254 }
255
256 /**
257  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
258  * down the peers without freeing memory associated with GET request.
259  */
260 static void
261 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262 {
263   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
264               (char *) cls);
265   ok = 1;
266   
267   if (disco_task != GNUNET_SCHEDULER_NO_TASK)
268   {
269     disco_task = GNUNET_SCHEDULER_NO_TASK;
270     GNUNET_SCHEDULER_cancel(disco_task);
271     GNUNET_DHT_disconnect(dht_handle);
272     dht_handle = NULL;
273   }
274   if (pg != NULL)
275     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
276   GNUNET_SCHEDULER_cancel (die_task);
277 }
278
279 static void
280 put_pseu_dht(void *cls, int success)
281 {
282   struct GNSNameRecordBlock *nrb;
283   struct GNUNET_CRYPTO_ShortHashCode name_hash;
284   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
285   struct GNUNET_HashCode xor_hash;
286   struct GNUNET_HashCode name_hash_double;
287   struct GNUNET_HashCode zone_hash_double;
288   uint32_t rd_payload_length;
289   char* nrb_data = NULL;
290   struct GNUNET_CRYPTO_RsaSignature *sig;
291   struct GNUNET_NAMESTORE_RecordData rd;
292   
293   memset (&rd, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
294   rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
295   rd.data_size = strlen(TEST_PSEU_ALICE)+1;
296   rd.data = TEST_PSEU_ALICE;
297   rd.record_type = GNUNET_GNS_RECORD_PSEU;
298   rd.flags = 0;
299
300   sig = GNUNET_NAMESTORE_create_signature(alice_key,
301                                            GNUNET_TIME_UNIT_FOREVER_ABS,
302                                            "+",
303                                            &rd, 1);
304
305   GNUNET_assert (NULL != sig);
306
307   GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
308                                                                  GNUNET_TIME_UNIT_FOREVER_ABS,
309                                                                  "+",
310                                                                  1,
311                                                                  &rd,
312                                                                  sig));
313   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
314   nrb = GNUNET_malloc(rd_payload_length + strlen("+") + 1
315                       + sizeof(struct GNSNameRecordBlock));
316   nrb->signature = *sig;
317   nrb->public_key = alice_pkey;
318   nrb->rd_count = htonl(1);
319   memset(&nrb[1], 0, strlen("+") + 1);
320   strcpy((char*)&nrb[1], "+");
321   nrb_data = (char*)&nrb[1];
322   nrb_data += strlen("+") + 1;
323
324   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
325                                                 &rd,
326                                                 rd_payload_length,
327                                                 nrb_data))
328   {
329     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
330     ok = 3;
331     GNUNET_DHT_disconnect(dht_handle);
332     
333     
334     GNUNET_CRYPTO_rsa_key_free(our_key);
335     GNUNET_CRYPTO_rsa_key_free(bob_key);
336     GNUNET_CRYPTO_rsa_key_free(alice_key);
337     GNUNET_free(sig);
338     GNUNET_free (nrb);
339     return;
340   }
341   GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
342   GNUNET_CRYPTO_short_hash(&alice_pkey,
343                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
344                      &zone_hash);
345
346   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
347   GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
348   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
349
350   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
351     strlen("+") + 1;
352
353   GNUNET_DHT_put (dht_handle, &xor_hash,
354                   0,
355                   GNUNET_DHT_RO_NONE,
356                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
357                   rd_payload_length,
358                   (char*)nrb,
359                   GNUNET_TIME_UNIT_FOREVER_ABS,
360                   DHT_OPERATION_TIMEOUT,
361                   &commence_testing,
362                   NULL);
363   
364   GNUNET_free(sig);
365   GNUNET_free (nrb);
366 }
367
368 static void
369 put_www_dht(void *cls, int success)
370 {
371   struct GNSNameRecordBlock *nrb;
372   struct GNUNET_CRYPTO_ShortHashCode name_hash;
373   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
374   struct GNUNET_HashCode xor_hash;
375   struct GNUNET_HashCode name_hash_double;
376   struct GNUNET_HashCode zone_hash_double;
377   uint32_t rd_payload_length;
378   char* nrb_data = NULL;
379   struct GNUNET_CRYPTO_RsaSignature *sig;
380   struct GNUNET_NAMESTORE_RecordData rd;
381   char* ip = TEST_IP;
382   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
383   
384   rd.expiration_time = UINT64_MAX;
385   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
386   rd.data_size = sizeof(struct in_addr);
387   rd.data = web;
388   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
389   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
390   
391   sig = GNUNET_NAMESTORE_create_signature(alice_key,
392                                           GNUNET_TIME_UNIT_FOREVER_ABS,
393                                           TEST_RECORD_NAME,
394                                           &rd, 1);
395   
396   GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
397                                                                  GNUNET_TIME_UNIT_FOREVER_ABS,
398                                                                  TEST_RECORD_NAME,
399                                                                  1,
400                                                                  &rd,
401                                                                  sig));
402   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
403   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
404                       + sizeof(struct GNSNameRecordBlock));
405   nrb->signature = *sig;
406   nrb->public_key = alice_pkey;
407   nrb->rd_count = htonl(1);
408   memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
409   strcpy((char*)&nrb[1], TEST_RECORD_NAME);
410   nrb_data = (char*)&nrb[1];
411   nrb_data += strlen(TEST_RECORD_NAME) + 1;
412
413   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
414                                                 &rd,
415                                                 rd_payload_length,
416                                                 nrb_data))
417   {
418     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
419     ok = 3;
420     GNUNET_DHT_disconnect(dht_handle);
421     
422     GNUNET_CRYPTO_rsa_key_free(our_key);
423     GNUNET_CRYPTO_rsa_key_free(bob_key);
424     GNUNET_CRYPTO_rsa_key_free(alice_key);
425     GNUNET_free(web);
426     GNUNET_free (nrb);
427     return;
428   }
429   GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
430   GNUNET_CRYPTO_short_hash(&alice_pkey,
431                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
432                      &zone_hash);
433   GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
434   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
435   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
436
437   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
438     strlen(TEST_RECORD_NAME) + 1;
439
440   GNUNET_DHT_put (dht_handle, &xor_hash,
441                   0,
442                   GNUNET_DHT_RO_NONE,
443                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
444                   rd_payload_length,
445                   (char*)nrb,
446                   GNUNET_TIME_UNIT_FOREVER_ABS,
447                   DHT_OPERATION_TIMEOUT,
448                   &put_pseu_dht,
449                   NULL);
450
451   GNUNET_free(web);
452   GNUNET_free (nrb);
453 }
454
455
456 static void
457 put_pkey_dht(void *cls, int32_t success, const char *emsg)
458 {
459   struct GNSNameRecordBlock *nrb;
460   struct GNUNET_CRYPTO_ShortHashCode name_hash;
461   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
462   struct GNUNET_HashCode xor_hash;
463   struct GNUNET_HashCode name_hash_double;
464   struct GNUNET_HashCode zone_hash_double;
465   uint32_t rd_payload_length;
466   char* nrb_data = NULL;
467   struct GNUNET_CRYPTO_RsaSignature *sig;
468   struct GNUNET_NAMESTORE_RecordData rd;
469   
470   rd.expiration_time = UINT64_MAX;
471   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
472   rd.data = &alice_hash;
473   rd.record_type = GNUNET_GNS_RECORD_PKEY;
474   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
475   
476   sig = GNUNET_NAMESTORE_create_signature(bob_key,
477                                           GNUNET_TIME_UNIT_FOREVER_ABS,
478                                           TEST_AUTHORITY_ALICE,
479                                           &rd,
480                                           1);
481
482   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
483   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_AUTHORITY_ALICE) + 1
484                       + sizeof(struct GNSNameRecordBlock));
485   nrb->signature = *sig;
486   nrb->public_key = bob_pkey;
487   nrb->rd_count = htonl(1);
488   memset(&nrb[1], 0, strlen(TEST_AUTHORITY_ALICE) + 1);
489   strcpy((char*)&nrb[1], TEST_AUTHORITY_ALICE);
490   nrb_data = (char*)&nrb[1];
491   nrb_data += strlen(TEST_AUTHORITY_ALICE) + 1;
492
493   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
494                                                 &rd,
495                                                 rd_payload_length,
496                                                 nrb_data))
497   {
498     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
499     ok = 3;
500     
501     GNUNET_CRYPTO_rsa_key_free(our_key);
502     GNUNET_CRYPTO_rsa_key_free(bob_key);
503     GNUNET_CRYPTO_rsa_key_free(alice_key);
504     GNUNET_free(sig);
505     GNUNET_free (nrb);
506     return;
507   }
508
509
510   GNUNET_CRYPTO_short_hash(TEST_AUTHORITY_ALICE,
511                      strlen(TEST_AUTHORITY_ALICE), &name_hash);
512   GNUNET_CRYPTO_short_hash(&bob_pkey,
513                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
514                      &zone_hash);
515   GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
516   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
517   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash); 
518
519   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
520     strlen(TEST_AUTHORITY_ALICE) + 1;
521   GNUNET_DHT_put (dht_handle, &xor_hash,
522                   0,
523                   GNUNET_DHT_RO_NONE,
524                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
525                   rd_payload_length,
526                   (char*)nrb,
527                   GNUNET_TIME_UNIT_FOREVER_ABS,
528                   DHT_OPERATION_TIMEOUT,
529                   &put_www_dht,
530                   NULL);
531   GNUNET_free (nrb);
532 }
533
534 static void
535 fin_init_zone (void *cls, int32_t success, const char *emsg)
536 {
537   struct GNUNET_NAMESTORE_RecordData rd;
538   rd.expiration_time = UINT64_MAX;
539   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
540   rd.data = &bob_hash;
541   rd.record_type = GNUNET_GNS_RECORD_PKEY;
542   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
543   
544   GNUNET_NAMESTORE_record_create (namestore_handle,
545                                   our_key,
546                                   TEST_AUTHORITY_BOB,
547                                   &rd,
548                                   &put_pkey_dht,
549                                   NULL);
550
551 }
552
553 static void
554 cont_init_zone (void *cls, int32_t success, const char *emsg)
555 {
556
557   struct GNUNET_NAMESTORE_RecordData rd;
558   rd.expiration_time = UINT64_MAX;
559   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
560   rd.data = &short_zone;
561   rd.record_type = GNUNET_GNS_RECORD_PKEY;
562   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
563
564   GNUNET_NAMESTORE_record_create (namestore_handle,
565                                   priv_key,
566                                   TEST_SHORTEN_ZONE,
567                                   &rd,
568                                   &fin_init_zone,
569                                   NULL);
570 }
571
572 static void
573 do_lookup (void *cls, const struct GNUNET_PeerIdentity *id,
574            const struct GNUNET_CONFIGURATION_Handle *_cfg,
575            struct GNUNET_TESTING_Daemon *d, const char *emsg)
576 {
577   
578   char* private_keyfile;
579   char* shorten_keyfile;
580   char* our_keyfile;
581   
582   cfg = _cfg;
583
584   GNUNET_SCHEDULER_cancel (die_task);
585
586   /* put records into namestore */
587   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
588   if (NULL == namestore_handle)
589   {
590     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
591     ok = -1;
592     return;
593   }
594   
595   /* dht */
596   dht_handle = GNUNET_DHT_connect(cfg, 1);
597   if (NULL == dht_handle)
598   {
599     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
600     ok = -1;
601     return;
602   }
603
604   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
605                                                           "ZONEKEY",
606                                                           &our_keyfile))
607   {
608     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
609     ok = -1;
610     return;
611   }
612   
613   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
614                                                           "SHORTEN_ZONEKEY",
615                                                           &shorten_keyfile))
616   {
617     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
618                "Failed to get shorten zone key from cfg\n");
619     ok = -1;
620     return;
621   }
622   
623   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
624                                                           "PRIVATE_ZONEKEY",
625                                                           &private_keyfile))
626   {
627     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
628                "Failed to get private zone key from cfg\n");
629     ok = -1;
630     return;
631   }
632   our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
633   priv_key = GNUNET_CRYPTO_rsa_key_create_from_file (private_keyfile);
634   short_key = GNUNET_CRYPTO_rsa_key_create_from_file (shorten_keyfile);
635   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
636   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_ALICE);
637   
638   GNUNET_free(our_keyfile);
639   GNUNET_free(shorten_keyfile);
640   GNUNET_free(private_keyfile);
641
642   GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
643   GNUNET_CRYPTO_rsa_key_get_public (priv_key, &priv_pkey);
644   GNUNET_CRYPTO_rsa_key_get_public (short_key, &short_pkey);
645   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
646   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
647   GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
648   GNUNET_CRYPTO_short_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
649   GNUNET_CRYPTO_short_hash(&our_pkey, sizeof(our_pkey), &our_zone);
650   GNUNET_CRYPTO_short_hash(&priv_pkey, sizeof(priv_pkey), &priv_zone);
651   GNUNET_CRYPTO_short_hash(&short_pkey, sizeof(short_pkey), &short_zone);
652   
653   struct GNUNET_NAMESTORE_RecordData rd;
654   rd.expiration_time = UINT64_MAX;
655   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
656   rd.data = &priv_zone;
657   rd.record_type = GNUNET_GNS_RECORD_PKEY;
658   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
659
660   GNUNET_NAMESTORE_record_create (namestore_handle,
661                                   our_key,
662                                   TEST_PRIVATE_ZONE,
663                                   &rd,
664                                   &cont_init_zone,
665                                   NULL);
666
667
668
669
670 }
671
672 static void
673 run (void *cls, char *const *args, const char *cfgfile,
674      const struct GNUNET_CONFIGURATION_Handle *c)
675 {
676   cfg = c;
677    /* Get path from configuration file */
678   if (GNUNET_YES !=
679       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
680                                              &test_directory))
681   {
682     ok = 404;
683     return;
684   }
685
686     
687   /* Set up a task to end testing if peer start fails */
688   die_task =
689       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
690                                     "didn't start all daemons in reasonable amount of time!!!");
691   
692   /* Start alice */
693   //d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
694   //                                 NULL, NULL, NULL, &do_lookup, NULL);
695   pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
696                                     NULL, NULL, &do_lookup, NULL,
697                                     NULL, NULL, NULL);
698 }
699
700 static int
701 check ()
702 {
703   int ret;
704
705   /* Arguments for GNUNET_PROGRAM_run */
706   char *const argv[] = { "test-gns-pseu-shorten", /* Name to give running binary */
707     "-c",
708     "test_gns_simple_lookup.conf",       /* Config file to use */
709 #if VERBOSE
710     "-L", "DEBUG",
711 #endif
712     NULL
713   };
714   struct GNUNET_GETOPT_CommandLineOption options[] = {
715     GNUNET_GETOPT_OPTION_END
716   };
717   /* Run the run function as a new program */
718   ret =
719       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
720                           "test-gns-pseu-shorten", "nohelp", options, &run,
721                           &ok);
722   if (ret != GNUNET_OK)
723   {
724     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
725                 "`test-gns-pseu-shorten': Failed with error code %d\n", ret);
726   }
727   return ok;
728 }
729
730 int
731 main (int argc, char *argv[])
732 {
733   int ret;
734
735   GNUNET_log_setup ("test-gns-pseu-shorten",
736 #if VERBOSE
737                     "DEBUG",
738 #else
739                     "WARNING",
740 #endif
741                     NULL);
742   ret = check ();
743   /**
744    * Need to remove base directory, subdirectories taken care
745    * of by the testing framework.
746    */
747   return ret;
748 }
749
750 /* end of test_gns_twopeer.c */