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