fix
[oweals/gnunet.git] / src / gns / test_gns_pseu_shorten.c
index 69301a8ebb16f034775a5a784d1b4665b5b5a7f3..f3a3c59a20154ac40d254e42da963840155c7c21 100644 (file)
 #define VERBOSE GNUNET_YES
 
 /* Timeout for entire testcase */
-#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
+#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
 
 /* If number of peers not in config file, use this number */
 #define DEFAULT_NUM_PEERS 2
 
 /* test records to resolve */
-#define TEST_DOMAIN "www.alice.bob.gnunet"
+#define TEST_DOMAIN "www.alicewonderland.bobbuilder.gnunet"
 #define TEST_IP "127.0.0.1"
 #define TEST_RECORD_NAME "www"
 
-#define TEST_AUTHORITY_BOB "bob"
-#define TEST_AUTHORITY_ALICE "alice"
+#define TEST_PRIVATE_ZONE "private"
+#define TEST_SHORTEN_ZONE "short"
+#define TEST_AUTHORITY_BOB "bobbuilder"
+#define TEST_AUTHORITY_ALICE "alicewonderland"
 #define TEST_PSEU_ALICE "carol"
-#define TEST_EXPECTED_RESULT "www.carol.gnunet"
+#define TEST_EXPECTED_RESULT "www.carol.short.private.gnunet"
 
 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 
+#define KEYFILE_SHORTEN = "zonefiles/188JSUMKEF25GVU8TTV0PBNNN8JVCPUEDFV1UHJJU884JD25V0T0.zkey"
+#define KEYFILE_PRIVATE = "zonefiles/OEFL7A4VEF1B40QLEMTG5D8G1CN6EN16QUSG5R2DT71GRJN34LSG.zkey"
 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
 #define KEYFILE_ALICE "../namestore/zonefiles/N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"
 
@@ -85,11 +89,18 @@ const struct GNUNET_CONFIGURATION_Handle *cfg;
 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded our_pkey;
+struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded priv_pkey;
+struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded short_pkey;
 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
 struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
+struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
+struct GNUNET_CRYPTO_RsaPrivateKey *short_key;
 struct GNUNET_CRYPTO_ShortHashCode alice_hash;
 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
+struct GNUNET_CRYPTO_ShortHashCode our_zone;
+struct GNUNET_CRYPTO_ShortHashCode priv_zone;
+struct GNUNET_CRYPTO_ShortHashCode short_zone;
 
 /**
  * Check whether peers successfully shut down.
@@ -147,7 +158,7 @@ process_shorten_result(void* cls, const char* sname)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                   "shorten test failed! (wanted: %s got: %s\n",
-                  (char*)cls, sname);
+                  TEST_EXPECTED_RESULT, sname);
       ok = 1;
     }
 
@@ -161,8 +172,12 @@ process_shorten_result(void* cls, const char* sname)
 static void
 do_shorten(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_GNS_shorten(gns_handle, TEST_DOMAIN, &process_shorten_result,
-TEST_DOMAIN);
+  GNUNET_GNS_shorten_zone (gns_handle, TEST_DOMAIN,
+                           &priv_zone,
+                           &short_zone,
+                           &our_zone,
+                           &process_shorten_result,
+                           TEST_DOMAIN);
 }
 
 static void
@@ -186,7 +201,7 @@ on_lookup_result(void *cls, uint32_t rd_count,
     for (i=0; i<rd_count; i++)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
-      if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
+      if (rd[i].record_type == GNUNET_GNS_RECORD_A)
       {
         memcpy(&a, rd[i].data, sizeof(a));
         addr = inet_ntoa(a);
@@ -223,6 +238,7 @@ commence_testing (void *cls, int success)
   GNUNET_CRYPTO_rsa_key_free(bob_key);
   GNUNET_CRYPTO_rsa_key_free(alice_key);
 
+  GNUNET_NAMESTORE_disconnect (namestore_handle);
   gns_handle = GNUNET_GNS_connect(cfg);
 
   if (NULL == gns_handle)
@@ -231,8 +247,12 @@ commence_testing (void *cls, int success)
                 "Failed to connect to GNS!\n");
   }
 
-  GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_TYPE_A,
-                    &on_lookup_result, TEST_DOMAIN);
+  GNUNET_GNS_lookup_zone (gns_handle, TEST_DOMAIN,
+                          &our_zone,
+                          GNUNET_GNS_RECORD_A,
+                          GNUNET_NO,
+                          short_key,
+                          &on_lookup_result, TEST_DOMAIN);
 }
 
 /**
@@ -264,23 +284,34 @@ put_pseu_dht(void *cls, int success)
   struct GNSNameRecordBlock *nrb;
   struct GNUNET_CRYPTO_ShortHashCode name_hash;
   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
-  GNUNET_HashCode xor_hash;
-  GNUNET_HashCode name_hash_double;
-  GNUNET_HashCode zone_hash_double;
+  struct GNUNET_HashCode xor_hash;
+  struct GNUNET_HashCode name_hash_double;
+  struct GNUNET_HashCode zone_hash_double;
   uint32_t rd_payload_length;
   char* nrb_data = NULL;
   struct GNUNET_CRYPTO_RsaSignature *sig;
   struct GNUNET_NAMESTORE_RecordData rd;
   
-  rd.expiration = GNUNET_TIME_absolute_get_forever ();
+  memset (&rd, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
+  rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
   rd.data_size = strlen(TEST_PSEU_ALICE)+1;
   rd.data = TEST_PSEU_ALICE;
   rd.record_type = GNUNET_GNS_RECORD_PSEU;
+  rd.flags = 0;
 
   sig = GNUNET_NAMESTORE_create_signature(alice_key,
-                                          GNUNET_TIME_absolute_get_forever(),
-                                          "+",
-                                          &rd, 1);
+                                           GNUNET_TIME_UNIT_FOREVER_ABS,
+                                           "+",
+                                           &rd, 1);
+
+  GNUNET_assert (NULL != sig);
+
+  GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
+                                                                 GNUNET_TIME_UNIT_FOREVER_ABS,
+                                                                 "+",
+                                                                 1,
+                                                                 &rd,
+                                                                 sig));
   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
   nrb = GNUNET_malloc(rd_payload_length + strlen("+") + 1
                       + sizeof(struct GNSNameRecordBlock));
@@ -327,7 +358,7 @@ put_pseu_dht(void *cls, int success)
                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
                   rd_payload_length,
                   (char*)nrb,
-                  rd.expiration,
+                  GNUNET_TIME_UNIT_FOREVER_ABS,
                   DHT_OPERATION_TIMEOUT,
                   &commence_testing,
                   NULL);
@@ -342,9 +373,9 @@ put_www_dht(void *cls, int success)
   struct GNSNameRecordBlock *nrb;
   struct GNUNET_CRYPTO_ShortHashCode name_hash;
   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
-  GNUNET_HashCode xor_hash;
-  GNUNET_HashCode name_hash_double;
-  GNUNET_HashCode zone_hash_double;
+  struct GNUNET_HashCode xor_hash;
+  struct GNUNET_HashCode name_hash_double;
+  struct GNUNET_HashCode zone_hash_double;
   uint32_t rd_payload_length;
   char* nrb_data = NULL;
   struct GNUNET_CRYPTO_RsaSignature *sig;
@@ -352,16 +383,24 @@ put_www_dht(void *cls, int success)
   char* ip = TEST_IP;
   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
   
-  rd.expiration = GNUNET_TIME_absolute_get_forever ();
+  rd.expiration_time = UINT64_MAX;
   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
   rd.data_size = sizeof(struct in_addr);
   rd.data = web;
   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
-
+  rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
+  
   sig = GNUNET_NAMESTORE_create_signature(alice_key,
-                                          GNUNET_TIME_absolute_get_forever(),
+                                          GNUNET_TIME_UNIT_FOREVER_ABS,
                                           TEST_RECORD_NAME,
                                           &rd, 1);
+  
+  GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
+                                                                 GNUNET_TIME_UNIT_FOREVER_ABS,
+                                                                 TEST_RECORD_NAME,
+                                                                 1,
+                                                                 &rd,
+                                                                 sig));
   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
                       + sizeof(struct GNSNameRecordBlock));
@@ -406,7 +445,7 @@ put_www_dht(void *cls, int success)
                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
                   rd_payload_length,
                   (char*)nrb,
-                  rd.expiration,
+                 GNUNET_TIME_UNIT_FOREVER_ABS,
                   DHT_OPERATION_TIMEOUT,
                   &put_pseu_dht,
                   NULL);
@@ -422,21 +461,22 @@ put_pkey_dht(void *cls, int32_t success, const char *emsg)
   struct GNSNameRecordBlock *nrb;
   struct GNUNET_CRYPTO_ShortHashCode name_hash;
   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
-  GNUNET_HashCode xor_hash;
-  GNUNET_HashCode name_hash_double;
-  GNUNET_HashCode zone_hash_double;
+  struct GNUNET_HashCode xor_hash;
+  struct GNUNET_HashCode name_hash_double;
+  struct GNUNET_HashCode zone_hash_double;
   uint32_t rd_payload_length;
   char* nrb_data = NULL;
   struct GNUNET_CRYPTO_RsaSignature *sig;
   struct GNUNET_NAMESTORE_RecordData rd;
   
-  rd.expiration = GNUNET_TIME_absolute_get_forever ();
+  rd.expiration_time = UINT64_MAX;
   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
   rd.data = &alice_hash;
   rd.record_type = GNUNET_GNS_RECORD_PKEY;
-
+  rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
+  
   sig = GNUNET_NAMESTORE_create_signature(bob_key,
-                                          GNUNET_TIME_absolute_get_forever(),
+                                          GNUNET_TIME_UNIT_FOREVER_ABS,
                                           TEST_AUTHORITY_ALICE,
                                           &rd,
                                           1);
@@ -486,21 +526,59 @@ put_pkey_dht(void *cls, int32_t success, const char *emsg)
                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
                   rd_payload_length,
                   (char*)nrb,
-                  rd.expiration,
+                  GNUNET_TIME_UNIT_FOREVER_ABS,
                   DHT_OPERATION_TIMEOUT,
                   &put_www_dht,
                   NULL);
-  GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_NO);
   GNUNET_free (nrb);
 }
 
 static void
-do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
-          const struct GNUNET_CONFIGURATION_Handle *_cfg,
-          struct GNUNET_TESTING_Daemon *d, const char *emsg)
+fin_init_zone (void *cls, int32_t success, const char *emsg)
 {
+  struct GNUNET_NAMESTORE_RecordData rd;
+  rd.expiration_time = UINT64_MAX;
+  rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
+  rd.data = &bob_hash;
+  rd.record_type = GNUNET_GNS_RECORD_PKEY;
+  rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
   
+  GNUNET_NAMESTORE_record_create (namestore_handle,
+                                  our_key,
+                                  TEST_AUTHORITY_BOB,
+                                  &rd,
+                                  &put_pkey_dht,
+                                  NULL);
+
+}
+
+static void
+cont_init_zone (void *cls, int32_t success, const char *emsg)
+{
+
+  struct GNUNET_NAMESTORE_RecordData rd;
+  rd.expiration_time = UINT64_MAX;
+  rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
+  rd.data = &short_zone;
+  rd.record_type = GNUNET_GNS_RECORD_PKEY;
+  rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
+
+  GNUNET_NAMESTORE_record_create (namestore_handle,
+                                  priv_key,
+                                  TEST_SHORTEN_ZONE,
+                                  &rd,
+                                  &fin_init_zone,
+                                  NULL);
+}
+
+static void
+do_lookup (void *cls, const struct GNUNET_PeerIdentity *id,
+           const struct GNUNET_CONFIGURATION_Handle *_cfg,
+           struct GNUNET_TESTING_Daemon *d, const char *emsg)
+{
   
+  char* private_keyfile;
+  char* shorten_keyfile;
   char* our_keyfile;
   
   cfg = _cfg;
@@ -533,33 +611,64 @@ do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
     ok = -1;
     return;
   }
-
+  
+  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
+                                                          "SHORTEN_ZONEKEY",
+                                                          &shorten_keyfile))
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+               "Failed to get shorten zone key from cfg\n");
+    ok = -1;
+    return;
+  }
+  
+  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
+                                                          "PRIVATE_ZONEKEY",
+                                                          &private_keyfile))
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+               "Failed to get private zone key from cfg\n");
+    ok = -1;
+    return;
+  }
   our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
+  priv_key = GNUNET_CRYPTO_rsa_key_create_from_file (private_keyfile);
+  short_key = GNUNET_CRYPTO_rsa_key_create_from_file (shorten_keyfile);
   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_ALICE);
   
   GNUNET_free(our_keyfile);
+  GNUNET_free(shorten_keyfile);
+  GNUNET_free(private_keyfile);
 
   GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
+  GNUNET_CRYPTO_rsa_key_get_public (priv_key, &priv_pkey);
+  GNUNET_CRYPTO_rsa_key_get_public (short_key, &short_pkey);
   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
   GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
   GNUNET_CRYPTO_short_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
-
+  GNUNET_CRYPTO_short_hash(&our_pkey, sizeof(our_pkey), &our_zone);
+  GNUNET_CRYPTO_short_hash(&priv_pkey, sizeof(priv_pkey), &priv_zone);
+  GNUNET_CRYPTO_short_hash(&short_pkey, sizeof(short_pkey), &short_zone);
+  
   struct GNUNET_NAMESTORE_RecordData rd;
-  rd.expiration = GNUNET_TIME_absolute_get_forever ();
+  rd.expiration_time = UINT64_MAX;
   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
-  rd.data = &bob_hash;
+  rd.data = &priv_zone;
   rd.record_type = GNUNET_GNS_RECORD_PKEY;
+  rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
 
   GNUNET_NAMESTORE_record_create (namestore_handle,
                                   our_key,
-                                  TEST_AUTHORITY_BOB,
+                                  TEST_PRIVATE_ZONE,
                                   &rd,
-                                  &put_pkey_dht,
+                                  &cont_init_zone,
                                   NULL);
 
 
+
+
 }
 
 static void