-doxy
[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_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                   (char*)cls, 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   gns_handle = GNUNET_GNS_connect(cfg);
240
241   if (NULL == gns_handle)
242   {
243     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
244                 "Failed to connect to GNS!\n");
245   }
246
247   GNUNET_GNS_lookup_zone (gns_handle, TEST_DOMAIN,
248                           &our_zone,
249                           GNUNET_GNS_RECORD_TYPE_A,
250                           GNUNET_NO,
251                           short_key,
252                           &on_lookup_result, TEST_DOMAIN);
253 }
254
255 /**
256  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
257  * down the peers without freeing memory associated with GET request.
258  */
259 static void
260 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
261 {
262   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
263               (char *) cls);
264   ok = 1;
265   
266   if (disco_task != GNUNET_SCHEDULER_NO_TASK)
267   {
268     disco_task = GNUNET_SCHEDULER_NO_TASK;
269     GNUNET_SCHEDULER_cancel(disco_task);
270     GNUNET_DHT_disconnect(dht_handle);
271     dht_handle = NULL;
272   }
273   if (pg != NULL)
274     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
275   GNUNET_SCHEDULER_cancel (die_task);
276 }
277
278 static void
279 put_pseu_dht(void *cls, int success)
280 {
281   struct GNSNameRecordBlock *nrb;
282   struct GNUNET_CRYPTO_ShortHashCode name_hash;
283   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
284   struct GNUNET_HashCode xor_hash;
285   struct GNUNET_HashCode name_hash_double;
286   struct GNUNET_HashCode zone_hash_double;
287   uint32_t rd_payload_length;
288   char* nrb_data = NULL;
289   struct GNUNET_CRYPTO_RsaSignature *sig;
290   struct GNUNET_NAMESTORE_RecordData rd;
291   
292   memset (&rd, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
293   rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
294   rd.data_size = strlen(TEST_PSEU_ALICE)+1;
295   rd.data = TEST_PSEU_ALICE;
296   rd.record_type = GNUNET_GNS_RECORD_PSEU;
297   rd.flags = 0;
298
299   sig = GNUNET_NAMESTORE_create_signature(alice_key,
300                                            GNUNET_TIME_UNIT_FOREVER_ABS,
301                                            "+",
302                                            &rd, 1);
303
304   GNUNET_assert (NULL != sig);
305
306   GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
307                                                                  GNUNET_TIME_UNIT_FOREVER_ABS,
308                                                                  "+",
309                                                                  1,
310                                                                  &rd,
311                                                                  sig));
312   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
313   nrb = GNUNET_malloc(rd_payload_length + strlen("+") + 1
314                       + sizeof(struct GNSNameRecordBlock));
315   nrb->signature = *sig;
316   nrb->public_key = alice_pkey;
317   nrb->rd_count = htonl(1);
318   memset(&nrb[1], 0, strlen("+") + 1);
319   strcpy((char*)&nrb[1], "+");
320   nrb_data = (char*)&nrb[1];
321   nrb_data += strlen("+") + 1;
322
323   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
324                                                 &rd,
325                                                 rd_payload_length,
326                                                 nrb_data))
327   {
328     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
329     ok = 3;
330     GNUNET_DHT_disconnect(dht_handle);
331     
332     
333     GNUNET_CRYPTO_rsa_key_free(our_key);
334     GNUNET_CRYPTO_rsa_key_free(bob_key);
335     GNUNET_CRYPTO_rsa_key_free(alice_key);
336     GNUNET_free(sig);
337     GNUNET_free (nrb);
338     return;
339   }
340   GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
341   GNUNET_CRYPTO_short_hash(&alice_pkey,
342                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
343                      &zone_hash);
344
345   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
346   GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
347   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
348
349   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
350     strlen("+") + 1;
351
352   GNUNET_DHT_put (dht_handle, &xor_hash,
353                   0,
354                   GNUNET_DHT_RO_NONE,
355                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
356                   rd_payload_length,
357                   (char*)nrb,
358                   GNUNET_TIME_UNIT_FOREVER_ABS,
359                   DHT_OPERATION_TIMEOUT,
360                   &commence_testing,
361                   NULL);
362   
363   GNUNET_free(sig);
364   GNUNET_free (nrb);
365 }
366
367 static void
368 put_www_dht(void *cls, int success)
369 {
370   struct GNSNameRecordBlock *nrb;
371   struct GNUNET_CRYPTO_ShortHashCode name_hash;
372   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
373   struct GNUNET_HashCode xor_hash;
374   struct GNUNET_HashCode name_hash_double;
375   struct GNUNET_HashCode zone_hash_double;
376   uint32_t rd_payload_length;
377   char* nrb_data = NULL;
378   struct GNUNET_CRYPTO_RsaSignature *sig;
379   struct GNUNET_NAMESTORE_RecordData rd;
380   char* ip = TEST_IP;
381   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
382   
383   rd.expiration_time = UINT64_MAX;
384   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
385   rd.data_size = sizeof(struct in_addr);
386   rd.data = web;
387   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
388   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
389   
390   sig = GNUNET_NAMESTORE_create_signature(alice_key,
391                                           GNUNET_TIME_UNIT_FOREVER_ABS,
392                                           TEST_RECORD_NAME,
393                                           &rd, 1);
394   
395   GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
396                                                                  GNUNET_TIME_UNIT_FOREVER_ABS,
397                                                                  TEST_RECORD_NAME,
398                                                                  1,
399                                                                  &rd,
400                                                                  sig));
401   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
402   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
403                       + sizeof(struct GNSNameRecordBlock));
404   nrb->signature = *sig;
405   nrb->public_key = alice_pkey;
406   nrb->rd_count = htonl(1);
407   memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
408   strcpy((char*)&nrb[1], TEST_RECORD_NAME);
409   nrb_data = (char*)&nrb[1];
410   nrb_data += strlen(TEST_RECORD_NAME) + 1;
411
412   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
413                                                 &rd,
414                                                 rd_payload_length,
415                                                 nrb_data))
416   {
417     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
418     ok = 3;
419     GNUNET_DHT_disconnect(dht_handle);
420     
421     GNUNET_CRYPTO_rsa_key_free(our_key);
422     GNUNET_CRYPTO_rsa_key_free(bob_key);
423     GNUNET_CRYPTO_rsa_key_free(alice_key);
424     GNUNET_free(web);
425     GNUNET_free (nrb);
426     return;
427   }
428   GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
429   GNUNET_CRYPTO_short_hash(&alice_pkey,
430                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
431                      &zone_hash);
432   GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
433   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
434   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
435
436   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
437     strlen(TEST_RECORD_NAME) + 1;
438
439   GNUNET_DHT_put (dht_handle, &xor_hash,
440                   0,
441                   GNUNET_DHT_RO_NONE,
442                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
443                   rd_payload_length,
444                   (char*)nrb,
445                   GNUNET_TIME_UNIT_FOREVER_ABS,
446                   DHT_OPERATION_TIMEOUT,
447                   &put_pseu_dht,
448                   NULL);
449
450   GNUNET_free(web);
451   GNUNET_free (nrb);
452 }
453
454
455 static void
456 put_pkey_dht(void *cls, int32_t success, const char *emsg)
457 {
458   struct GNSNameRecordBlock *nrb;
459   struct GNUNET_CRYPTO_ShortHashCode name_hash;
460   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
461   struct GNUNET_HashCode xor_hash;
462   struct GNUNET_HashCode name_hash_double;
463   struct GNUNET_HashCode zone_hash_double;
464   uint32_t rd_payload_length;
465   char* nrb_data = NULL;
466   struct GNUNET_CRYPTO_RsaSignature *sig;
467   struct GNUNET_NAMESTORE_RecordData rd;
468   
469   rd.expiration_time = UINT64_MAX;
470   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
471   rd.data = &alice_hash;
472   rd.record_type = GNUNET_GNS_RECORD_PKEY;
473   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
474   
475   sig = GNUNET_NAMESTORE_create_signature(bob_key,
476                                           GNUNET_TIME_UNIT_FOREVER_ABS,
477                                           TEST_AUTHORITY_ALICE,
478                                           &rd,
479                                           1);
480
481   GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&bob_pkey,
482                                                                  GNUNET_TIME_UNIT_FOREVER_ABS,
483                                                                  TEST_AUTHORITY_ALICE,
484                                                                  1,
485                                                                  &rd,
486                                                                  sig));
487   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
488   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_AUTHORITY_ALICE) + 1
489                       + sizeof(struct GNSNameRecordBlock));
490   nrb->signature = *sig;
491   nrb->public_key = bob_pkey;
492   nrb->rd_count = htonl(1);
493   memset(&nrb[1], 0, strlen(TEST_AUTHORITY_ALICE) + 1);
494   strcpy((char*)&nrb[1], TEST_AUTHORITY_ALICE);
495   nrb_data = (char*)&nrb[1];
496   nrb_data += strlen(TEST_AUTHORITY_ALICE) + 1;
497
498   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
499                                                 &rd,
500                                                 rd_payload_length,
501                                                 nrb_data))
502   {
503     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
504     ok = 3;
505     
506     GNUNET_CRYPTO_rsa_key_free(our_key);
507     GNUNET_CRYPTO_rsa_key_free(bob_key);
508     GNUNET_CRYPTO_rsa_key_free(alice_key);
509     GNUNET_free(sig);
510     GNUNET_free (nrb);
511     return;
512   }
513
514
515   GNUNET_CRYPTO_short_hash(TEST_AUTHORITY_ALICE,
516                      strlen(TEST_AUTHORITY_ALICE), &name_hash);
517   GNUNET_CRYPTO_short_hash(&bob_pkey,
518                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
519                      &zone_hash);
520   GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
521   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
522   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash); 
523
524   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
525     strlen(TEST_AUTHORITY_ALICE) + 1;
526   GNUNET_DHT_put (dht_handle, &xor_hash,
527                   0,
528                   GNUNET_DHT_RO_NONE,
529                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
530                   rd_payload_length,
531                   (char*)nrb,
532                   GNUNET_TIME_UNIT_FOREVER_ABS,
533                   DHT_OPERATION_TIMEOUT,
534                   &put_www_dht,
535                   NULL);
536   GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_NO);
537   GNUNET_free (nrb);
538 }
539
540 static void
541 fin_init_zone (void *cls, int32_t success, const char *emsg)
542 {
543   struct GNUNET_NAMESTORE_RecordData rd;
544   rd.expiration_time = UINT64_MAX;
545   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
546   rd.data = &bob_hash;
547   rd.record_type = GNUNET_GNS_RECORD_PKEY;
548   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
549   
550   GNUNET_NAMESTORE_record_create (namestore_handle,
551                                   our_key,
552                                   TEST_AUTHORITY_BOB,
553                                   &rd,
554                                   &put_pkey_dht,
555                                   NULL);
556
557 }
558
559 static void
560 cont_init_zone (void *cls, int32_t success, const char *emsg)
561 {
562
563   struct GNUNET_NAMESTORE_RecordData rd;
564   rd.expiration_time = UINT64_MAX;
565   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
566   rd.data = &short_zone;
567   rd.record_type = GNUNET_GNS_RECORD_PKEY;
568   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
569
570   GNUNET_NAMESTORE_record_create (namestore_handle,
571                                   priv_key,
572                                   TEST_SHORTEN_ZONE,
573                                   &rd,
574                                   &fin_init_zone,
575                                   NULL);
576 }
577
578 static void
579 do_lookup (void *cls, const struct GNUNET_PeerIdentity *id,
580            const struct GNUNET_CONFIGURATION_Handle *_cfg,
581            struct GNUNET_TESTING_Daemon *d, const char *emsg)
582 {
583   
584   char* private_keyfile;
585   char* shorten_keyfile;
586   char* our_keyfile;
587   
588   cfg = _cfg;
589
590   GNUNET_SCHEDULER_cancel (die_task);
591
592   /* put records into namestore */
593   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
594   if (NULL == namestore_handle)
595   {
596     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
597     ok = -1;
598     return;
599   }
600   
601   /* dht */
602   dht_handle = GNUNET_DHT_connect(cfg, 1);
603   if (NULL == dht_handle)
604   {
605     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
606     ok = -1;
607     return;
608   }
609
610   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
611                                                           "ZONEKEY",
612                                                           &our_keyfile))
613   {
614     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
615     ok = -1;
616     return;
617   }
618   
619   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
620                                                           "SHORTEN_ZONEKEY",
621                                                           &shorten_keyfile))
622   {
623     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
624                "Failed to get shorten zone key from cfg\n");
625     ok = -1;
626     return;
627   }
628   
629   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
630                                                           "PRIVATE_ZONEKEY",
631                                                           &private_keyfile))
632   {
633     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
634                "Failed to get private zone key from cfg\n");
635     ok = -1;
636     return;
637   }
638   our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
639   priv_key = GNUNET_CRYPTO_rsa_key_create_from_file (private_keyfile);
640   short_key = GNUNET_CRYPTO_rsa_key_create_from_file (shorten_keyfile);
641   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
642   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_ALICE);
643   
644   GNUNET_free(our_keyfile);
645   GNUNET_free(shorten_keyfile);
646   GNUNET_free(private_keyfile);
647
648   GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
649   GNUNET_CRYPTO_rsa_key_get_public (priv_key, &priv_pkey);
650   GNUNET_CRYPTO_rsa_key_get_public (short_key, &short_pkey);
651   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
652   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
653   GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
654   GNUNET_CRYPTO_short_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
655   GNUNET_CRYPTO_short_hash(&our_pkey, sizeof(our_pkey), &our_zone);
656   GNUNET_CRYPTO_short_hash(&priv_pkey, sizeof(priv_pkey), &priv_zone);
657   GNUNET_CRYPTO_short_hash(&short_pkey, sizeof(short_pkey), &short_zone);
658   
659   struct GNUNET_NAMESTORE_RecordData rd;
660   rd.expiration_time = UINT64_MAX;
661   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
662   rd.data = &priv_zone;
663   rd.record_type = GNUNET_GNS_RECORD_PKEY;
664   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
665
666   GNUNET_NAMESTORE_record_create (namestore_handle,
667                                   our_key,
668                                   TEST_PRIVATE_ZONE,
669                                   &rd,
670                                   &cont_init_zone,
671                                   NULL);
672
673
674
675
676 }
677
678 static void
679 run (void *cls, char *const *args, const char *cfgfile,
680      const struct GNUNET_CONFIGURATION_Handle *c)
681 {
682   cfg = c;
683    /* Get path from configuration file */
684   if (GNUNET_YES !=
685       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
686                                              &test_directory))
687   {
688     ok = 404;
689     return;
690   }
691
692     
693   /* Set up a task to end testing if peer start fails */
694   die_task =
695       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
696                                     "didn't start all daemons in reasonable amount of time!!!");
697   
698   /* Start alice */
699   //d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
700   //                                 NULL, NULL, NULL, &do_lookup, NULL);
701   pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
702                                     NULL, NULL, &do_lookup, NULL,
703                                     NULL, NULL, NULL);
704 }
705
706 static int
707 check ()
708 {
709   int ret;
710
711   /* Arguments for GNUNET_PROGRAM_run */
712   char *const argv[] = { "test-gns-pseu-shorten", /* Name to give running binary */
713     "-c",
714     "test_gns_simple_lookup.conf",       /* Config file to use */
715 #if VERBOSE
716     "-L", "DEBUG",
717 #endif
718     NULL
719   };
720   struct GNUNET_GETOPT_CommandLineOption options[] = {
721     GNUNET_GETOPT_OPTION_END
722   };
723   /* Run the run function as a new program */
724   ret =
725       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
726                           "test-gns-pseu-shorten", "nohelp", options, &run,
727                           &ok);
728   if (ret != GNUNET_OK)
729   {
730     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
731                 "`test-gns-pseu-shorten': Failed with error code %d\n", ret);
732   }
733   return ok;
734 }
735
736 int
737 main (int argc, char *argv[])
738 {
739   int ret;
740
741   GNUNET_log_setup ("test-gns-pseu-shorten",
742 #if VERBOSE
743                     "DEBUG",
744 #else
745                     "WARNING",
746 #endif
747                     NULL);
748   ret = check ();
749   /**
750    * Need to remove base directory, subdirectories taken care
751    * of by the testing framework.
752    */
753   return ret;
754 }
755
756 /* end of test_gns_twopeer.c */