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