sensor: fixes for proof-of-work, test passes now
authorOmar Tarabai <tarabai@devegypt.com>
Wed, 20 Aug 2014 10:18:03 +0000 (10:18 +0000)
committerOmar Tarabai <tarabai@devegypt.com>
Wed, 20 Aug 2014 10:18:03 +0000 (10:18 +0000)
src/sensor/sensor_util_lib_crypto.c
src/sensor/test_gnunet-service-sensor_reporting.c
src/sensor/test_pow_sign.c

index 7cf5051867254c9e37cc823f2048f5ed31e9f4b0..e097ed3ae226a87a927e9353faa32c44b4bebd97 100644 (file)
@@ -167,8 +167,8 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
         htonl (GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT);
     result_block->purpose.size =
         htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
-        sizeof (struct GNUNET_TIME_Absolute) +
-        sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + cx->msg_size);
+               sizeof (struct GNUNET_TIME_Absolute) +
+               sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + cx->msg_size);
     memcpy (&result_block[1], &cx[1], cx->msg_size);
     sign_result =
         GNUNET_CRYPTO_eddsa_sign (&cx->private_key, &result_block->purpose,
@@ -178,6 +178,7 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_SENSOR_crypto_pow_sign_cancel (cx);
     if (NULL != callback)
       callback (callback_cls, (GNUNET_OK == sign_result) ? result_block : NULL);
+    return;
   }
   cx->pow++;
   cx->calculate_pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, cx);
@@ -193,7 +194,13 @@ void
 GNUNET_SENSOR_crypto_pow_sign_cancel (struct GNUNET_SENSOR_crypto_pow_context
                                       *cx)
 {
+  if (GNUNET_SCHEDULER_NO_TASK != cx->calculate_pow_task)
+  {
+    GNUNET_SCHEDULER_cancel (cx->calculate_pow_task);
+    cx->calculate_pow_task = GNUNET_SCHEDULER_NO_TASK;
+  }
   GNUNET_free (cx);
+  cx = NULL;
 }
 
 
@@ -262,23 +269,27 @@ GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block *
                                       void **payload)
 {
   /* Check public key */
-  if (0 != memcmp (public_key, &block->public_key, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))
+  if (0 !=
+      memcmp (public_key, &block->public_key,
+              sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING, "Public key mismatch.\n");
     return 0;
   }
   /* Check signature */
   if (GNUNET_OK !=
-      GNUNET_CRYPTO_eddsa_verify (purpose, &block->purpose,
-                                  &block->signature, public_key))
+      GNUNET_CRYPTO_eddsa_verify (purpose, &block->purpose, &block->signature,
+                                  public_key))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid signature.\n");
     return 0;
   }
   /* Check pow */
-  if (GNUNET_NO == check_pow (&block->timestamp,
-      sizeof (struct GNUNET_TIME_Absolute) +
-              sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + block->msg_size, block->pow, matching_bits))
+  if (GNUNET_NO ==
+      check_pow (&block->timestamp,
+                 sizeof (struct GNUNET_TIME_Absolute) +
+                 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + block->msg_size,
+                 block->pow, matching_bits))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid proof-of-work.\n");
     return 0;
index afe45afeafbdfb4e0d538461dbb60d0335625ec7..acb0e7165f14f9009ac1003b28b5aec5b2037df6 100644 (file)
@@ -356,7 +356,11 @@ sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
 {
   struct TestPeer *peer = cls;
 
-  GNUNET_assert (NULL == emsg);
+  if (NULL != emsg)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
+    GNUNET_assert (0);
+  }
   peer->sensor_op =
       GNUNET_TESTBED_service_connect (NULL, peer->testbed_peer, "sensor",
                                       &sensor_connect_cb, peer,
@@ -441,7 +445,11 @@ static void
 dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
                    const char *emsg)
 {
-  GNUNET_assert (NULL == emsg);
+  if (NULL != emsg)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
+    GNUNET_assert (0);
+  }
   GNUNET_TESTBED_operation_done (op);
   /* Connect to peerstore service on first peer */
   peerstore_op =
index 84e990cffecc3609d155d9f272752ff006b2f3c4..e1e78a7219efed832a4b2f728cf5124758dac6f2 100644 (file)
@@ -107,18 +107,29 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
-static void pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block)
+static void
+pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block)
 {
   void *response;
 
-  printf ("Received block:\n"
-      "pow: %" PRIu64 ".\n", block->pow);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Received block:\n" "pow: %" PRIu64 ".\n", block->pow);
+  /* Test that the block is valid */
   GNUNET_assert (MSG_SIZE ==
-  GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
-      public_key, GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT, &response));
-  GNUNET_assert (0 == memcmp(msg, response, MSG_SIZE));
+                 GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
+                                                       public_key,
+                                                       GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT,
+                                                       &response));
+  GNUNET_assert (0 == memcmp (msg, response, MSG_SIZE));
+  /* Modify the payload and test that verification returns invalid */
+  block->pow++;
+  GNUNET_assert (0 ==
+                 GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
+                                                       public_key,
+                                                       GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT,
+                                                       &response));
   ok = 0;
-  GNUNET_SCHEDULER_cancel(shutdown_task);
+  GNUNET_SCHEDULER_cancel (shutdown_task);
   GNUNET_SCHEDULER_add_now (do_shutdown, NULL);
 }
 
@@ -140,17 +151,19 @@ peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
   struct GNUNET_TIME_Absolute timestamp;
 
   /* generate random data block */
-  GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, msg, MSG_SIZE);
+  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, msg, MSG_SIZE);
   /* get private and public keys */
   private_key =
       GNUNET_CRYPTO_eddsa_key_create_from_configuration (pinfo->result.cfg);
   GNUNET_assert (NULL != private_key);
   public_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPublicKey);
+
   GNUNET_CRYPTO_eddsa_key_get_public (private_key, public_key);
   /* create pow and sign */
-  timestamp = GNUNET_TIME_absolute_get();
-  GNUNET_SENSOR_crypto_pow_sign (msg, MSG_SIZE, &timestamp,
-      public_key, private_key, MATCHING_BITS, &pow_cb, NULL);
+  timestamp = GNUNET_TIME_absolute_get ();
+  GNUNET_SENSOR_crypto_pow_sign (msg, MSG_SIZE, &timestamp, public_key,
+                                 private_key, MATCHING_BITS, &pow_cb, NULL);
+  GNUNET_TESTBED_operation_done (op);
 }