-fix
[oweals/gnunet.git] / src / dht / test_dht_twopeer_put_get.c
index 9db0ca385046bfa8c4c5730cd546945e8d2a59ce..48bf9f8230bf0771d49a7b2bdc75675f556232d6 100644 (file)
@@ -55,8 +55,6 @@
 /* If number of peers not in config file, use this number */
 #define DEFAULT_NUM_PEERS 2
 
-#define DNS GNUNET_NO
-
 /* Globals */
 
 /**
@@ -105,15 +103,11 @@ static unsigned int total_connections;
 static unsigned int failed_connections;
 
 /* Task handle to use to schedule test failure */
-GNUNET_SCHEDULER_TaskIdentifier die_task;
+static GNUNET_SCHEDULER_TaskIdentifier die_task;
 
 /* Global return value (0 for success, anything else for failure) */
 static int ok;
 
-#if DNS
-struct GNUNET_DNS_Record data;
-#endif
-
 /**
  * Peer identity of the first peer started.
  */
@@ -209,13 +203,13 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param size number of bytes in data
  * @param data pointer to the result data
  */
-void
-get_result_iterator (void *cls,
-                     struct GNUNET_TIME_Absolute exp,
+static void
+get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
                      const GNUNET_HashCode * key,
-                     const struct GNUNET_PeerIdentity *const *get_path,
-                     const struct GNUNET_PeerIdentity *const *put_path,
-                     enum GNUNET_BLOCK_Type type,
+                     const struct GNUNET_PeerIdentity *get_path,
+                     unsigned int get_path_size,
+                     const struct GNUNET_PeerIdentity *put_path,
+                     unsigned int put_path_size, enum GNUNET_BLOCK_Type type,
                      size_t size, const void *result_data)
 {
   GNUNET_HashCode original_key; /* Key data was stored data under */
@@ -224,19 +218,8 @@ get_result_iterator (void *cls,
   memset (&original_key, 42, sizeof (GNUNET_HashCode)); /* Set the key to what it was set to previously */
   memset (original_data, 43, sizeof (original_data));
 
-#if DNS
-  if ((0 != memcmp (&data.service_descriptor, key, sizeof (GNUNET_HashCode))) ||
-      (0 != memcmp ((char *) &data, result_data, sizeof (original_data))))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Key or data is not the same as was inserted!\n");
-    GNUNET_SCHEDULER_cancel (die_task);
-    GNUNET_SCHEDULER_add_now (&end_badly,
-                              "key or data mismatch in get response!\n");
-    return;
-  }
-#else
-  if ((0 != memcmp (&original_key, key, sizeof (GNUNET_HashCode))) ||
+  if ((sizeof (original_data) != size) ||
+      (0 != memcmp (&original_key, key, sizeof (GNUNET_HashCode))) ||
       (0 != memcmp (original_data, result_data, sizeof (original_data))))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -246,38 +229,13 @@ get_result_iterator (void *cls,
                               "key or data mismatch in get response!\n");
     return;
   }
-#endif
 
   GNUNET_SCHEDULER_cancel (die_task);
   GNUNET_DHT_get_stop (global_get_handle);
+  global_get_handle = NULL;
   GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
 }
 
-/**
- * Start the GET request for the same key/data that was inserted.
- */
-static void
-do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  GNUNET_HashCode key;          /* Key for data lookup */
-
-#if DNS
-  memcpy (&key, &data.service_descriptor, sizeof (GNUNET_HashCode));
-#else
-  memset (&key, 42, sizeof (GNUNET_HashCode));  /* Set the key to the same thing as when data was inserted */
-#endif
-  global_get_handle =
-      GNUNET_DHT_get_start (peer2dht, GNUNET_TIME_relative_get_forever (),
-#if DNS
-                            GNUNET_BLOCK_TYPE_DNS,
-#else
-                            GNUNET_BLOCK_TYPE_TEST,
-#endif
-                            &key,
-                            DEFAULT_GET_REPLICATION,
-                            GNUNET_DHT_RO_NONE,
-                            NULL, 0, NULL, 0, &get_result_iterator, NULL);
-}
 
 /**
  * Called when the PUT request has been transmitted to the DHT service.
@@ -286,16 +244,21 @@ do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  GNUNET_HashCode key;          /* Key for data lookup */
+
   GNUNET_SCHEDULER_cancel (die_task);
-  die_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
-                                           &end_badly,
-                                           "waiting for get response (data not found)");
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
-                                (GNUNET_TIME_UNIT_SECONDS, 10), &do_get, NULL);
+  die_task =
+      GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &end_badly,
+                                    "waiting for get response (data not found)");
+
+  memset (&key, 42, sizeof (GNUNET_HashCode));  /* Set the key to the same thing as when data was inserted */
+  global_get_handle =
+      GNUNET_DHT_get_start (peer2dht, GNUNET_TIME_UNIT_FOREVER_REL,
+                            GNUNET_BLOCK_TYPE_TEST, &key, 1, GNUNET_DHT_RO_NONE,
+                            NULL, 0, &get_result_iterator, NULL);
 }
 
 
-#if !DNS
 /**
  * Set up some data, and call API PUT function
  */
@@ -309,73 +272,11 @@ do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   memset (data, 43, sizeof (data));
 
   /* Insert the data at the first peer */
-  GNUNET_DHT_put (peer1dht,
-                  &key,
-                  DEFAULT_PUT_REPLICATION,
-                  GNUNET_DHT_RO_NONE,
-                  GNUNET_BLOCK_TYPE_TEST,
-                  sizeof (data), data,
-                  GNUNET_TIME_UNIT_FOREVER_ABS,
+  GNUNET_DHT_put (peer1dht, &key, 1, GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_TEST,
+                  sizeof (data), data, GNUNET_TIME_UNIT_FOREVER_ABS,
                   GNUNET_TIME_UNIT_FOREVER_REL, &put_finished, NULL);
 }
-#else
-
-/**
- * Set up some data, and call API PUT function
- */
-static void
-do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  char *name = "philipptoelke.gnunet.";
-  size_t size = sizeof (struct GNUNET_DNS_Record);
-
-  memset (&data, 0, size);
-
-  data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
-  data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
-
-  GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
 
-  data.service_type = htonl (GNUNET_DNS_SERVICE_TYPE_UDP);
-  data.ports = htons (69);
-
-  char *keyfile;
-
-  GNUNET_asprintf (&keyfile, "/tmp/test_dns_data_key");
-  struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
-      GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
-  GNUNET_free (keyfile);
-  GNUNET_assert (my_private_key != NULL);
-
-  GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
-
-  data.expiration_time =
-      GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
-
-  /* Sign the block */
-  if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
-                                           &data.purpose, &data.signature))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
-    return;
-  }
-  GNUNET_CRYPTO_rsa_key_free (my_private_key);
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Putting with key %08x\n",
-              *((unsigned int *) &data.service_descriptor));
-
-  GNUNET_DHT_put (peer1dht,
-                  &data.service_descriptor,
-                  DEFAULT_PUT_REPLICATION,
-                  GNUNET_DHT_RO_NONE,
-                  GNUNET_BLOCK_TYPE_DNS,
-                  size,
-                  (char *) &data,
-                  GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
-                  GNUNET_TIME_UNIT_MINUTES, &put_finished, NULL);
-}
-#endif
 
 /**
  * This function is called whenever a connection attempt is finished between two of
@@ -386,11 +287,9 @@ do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * The emsg variable is NULL on success (peers connected), and non-NULL on
  * failure (peers failed to connect).
  */
-void
-topology_callback (void *cls,
-                   const struct GNUNET_PeerIdentity *first,
-                   const struct GNUNET_PeerIdentity *second,
-                   uint32_t distance,
+static void
+topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
+                   const struct GNUNET_PeerIdentity *second, uint32_t distance,
                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
                    struct GNUNET_TESTING_Daemon *first_daemon,
@@ -424,11 +323,10 @@ topology_callback (void *cls,
                 total_connections);
 #endif
     GNUNET_SCHEDULER_cancel (die_task);
-    die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
-                                             &end_badly, "from test gets");
+    die_task =
+        GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from test gets");
 
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
-                                  (GNUNET_TIME_UNIT_SECONDS, 2), &do_put, NULL);
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_put, NULL);
   }
   else if (total_connections + failed_connections == expected_connections)
   {
@@ -452,8 +350,7 @@ topology_callback (void *cls,
  * @param emsg NULL if peer started, non-NULL on error
  */
 static void
-peers_started_callback (void *cls,
-                        const struct GNUNET_PeerIdentity *id,
+peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
                         const struct GNUNET_CONFIGURATION_Handle *cfg,
                         struct GNUNET_TESTING_Daemon *d, const char *emsg)
 {
@@ -500,12 +397,10 @@ peers_started_callback (void *cls,
     if ((pg != NULL))           /* Sanity check */
     {
       /* Connect peers in a "straight line" topology, return the number of expected connections */
-      expected_connections = GNUNET_TESTING_connect_topology (pg,
-                                                              GNUNET_TESTING_TOPOLOGY_LINE,
-                                                              GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
-                                                              0.0,
-                                                              TIMEOUT,
-                                                              12, NULL, NULL);
+      expected_connections =
+          GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_LINE,
+                                           GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
+                                           0.0, TIMEOUT, 12, NULL, NULL);
     }
 
     /* Cancel current timeout fail task */
@@ -516,17 +411,16 @@ peers_started_callback (void *cls,
                                     "from connect topology (bad return)");
 
     /* Schedule timeout on failure task */
-    die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
-                                             &end_badly,
-                                             "from connect topology (timeout)");
+    die_task =
+        GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
+                                      "from connect topology (timeout)");
     ok = 0;
   }
 }
 
 static void
-run (void *cls,
-     char *const *args,
-     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
+run (void *cls, char *const *args, const char *cfgfile,
+     const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
 
   /* Get path from configuration file */
@@ -548,21 +442,15 @@ run (void *cls,
   peers_left = num_peers;
 
   /* Set up a task to end testing if peer start fails */
-  die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
-                                           &end_badly,
-                                           "didn't start all daemons in reasonable amount of time!!!");
+  die_task =
+      GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
+                                    "didn't start all daemons in reasonable amount of time!!!");
 
   /* Start num_peers peers, call peers_started_callback on peer start, topology_callback on peer connect */
   /* Read the API documentation for other parameters! */
-  pg = GNUNET_TESTING_daemons_start (cfg,
-                                     num_peers,
-                                     2,
-                                     2,
-                                     TIMEOUT,
-                                     NULL,
-                                     NULL,
-                                     &peers_started_callback,
-                                     NULL, &topology_callback, NULL, NULL);
+  pg = GNUNET_TESTING_daemons_start (cfg, num_peers, 2, 2, TIMEOUT, NULL, NULL,
+                                     &peers_started_callback, NULL,
+                                     &topology_callback, NULL, NULL);
 
 }
 
@@ -584,9 +472,10 @@ check ()
     GNUNET_GETOPT_OPTION_END
   };
   /* Run the run function as a new program */
-  ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
-                            argv, "test-dht-twopeer-put-get", "nohelp",
-                            options, &run, &ok);
+  ret =
+      GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
+                          "test-dht-twopeer-put-get", "nohelp", options, &run,
+                          &ok);
   if (ret != GNUNET_OK)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,