test_pow_sign.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * This file is part of GNUnet.
  3. * (C)
  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 sensor/test_pow_sign.c
  22. * @brief testcase for proof-of-work and signature library functions
  23. */
  24. #include <inttypes.h>
  25. #include "platform.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_sensor_util_lib.h"
  28. #include "gnunet_testbed_service.h"
  29. #include "gnunet_signatures.h"
  30. /**
  31. * Number of peers to start for the test
  32. */
  33. #define NUM_PEERS 1
  34. /**
  35. * Size of the message exchanged
  36. */
  37. #define MSG_SIZE 1024
  38. /**
  39. * Number of matching bits to use for generating proof-of-work
  40. */
  41. #define MATCHING_BITS 5
  42. /**
  43. * Test timeout
  44. */
  45. #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
  46. /**
  47. * Test name
  48. */
  49. static const char *testname = "test_pow_sign";
  50. /**
  51. * Name of GNUNET config file used in this test
  52. */
  53. static const char *cfg_filename = "test_pow_sign.conf";
  54. /**
  55. * Status of the test to be returned by main()
  56. */
  57. static int ok = 1;
  58. /**
  59. * Task used to shutdown / expire the test
  60. */
  61. static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
  62. /**
  63. * Message to be exchanged
  64. */
  65. static char msg[MSG_SIZE];
  66. /**
  67. * Private key of sending peer
  68. */
  69. static struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
  70. /**
  71. * Public key of sending peer
  72. */
  73. static struct GNUNET_CRYPTO_EddsaPublicKey *public_key;
  74. /**
  75. * Shutdown task
  76. *
  77. * @param cls Closure (unused)
  78. * @param tc Task context (unused)
  79. */
  80. static void
  81. do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  82. {
  83. if (NULL != private_key)
  84. {
  85. GNUNET_free (private_key);
  86. private_key = NULL;
  87. }
  88. if (NULL != public_key)
  89. {
  90. GNUNET_free (public_key);
  91. public_key = NULL;
  92. }
  93. GNUNET_SCHEDULER_shutdown ();
  94. }
  95. static void
  96. pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block)
  97. {
  98. void *response;
  99. struct GNUNET_TIME_Absolute end_time;
  100. struct GNUNET_TIME_Relative duration;
  101. end_time = GNUNET_TIME_absolute_get();
  102. duration = GNUNET_TIME_absolute_get_difference (block->timestamp, end_time);
  103. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  104. "Received block:\n" "pow: %" PRIu64 ".\n", block->pow);
  105. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Block generation toke %s.\n",
  106. GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO));
  107. /* Test that the block is valid */
  108. GNUNET_assert (MSG_SIZE ==
  109. GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
  110. public_key, &response));
  111. GNUNET_assert (0 == memcmp (msg, response, MSG_SIZE));
  112. /* Modify the payload and test that verification returns invalid */
  113. block->pow++;
  114. GNUNET_assert (0 ==
  115. GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
  116. public_key, &response));
  117. ok = 0;
  118. GNUNET_SCHEDULER_cancel (shutdown_task);
  119. GNUNET_SCHEDULER_add_now (do_shutdown, NULL);
  120. }
  121. /**
  122. * Callback to be called when the requested peer information is available
  123. *
  124. * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
  125. * @param op the operation this callback corresponds to
  126. * @param pinfo the result; will be NULL if the operation has failed
  127. * @param emsg error message if the operation has failed; will be NULL if the
  128. * operation is successfull
  129. */
  130. static void
  131. peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
  132. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  133. const char *emsg)
  134. {
  135. struct GNUNET_TIME_Absolute timestamp;
  136. /* generate random data block */
  137. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, msg, MSG_SIZE);
  138. /* get private and public keys */
  139. private_key =
  140. GNUNET_CRYPTO_eddsa_key_create_from_configuration (pinfo->result.cfg);
  141. GNUNET_assert (NULL != private_key);
  142. public_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPublicKey);
  143. GNUNET_CRYPTO_eddsa_key_get_public (private_key, public_key);
  144. /* create pow and sign */
  145. timestamp = GNUNET_TIME_absolute_get ();
  146. GNUNET_SENSOR_crypto_pow_sign (msg, MSG_SIZE, &timestamp, public_key,
  147. private_key, MATCHING_BITS, &pow_cb, NULL);
  148. GNUNET_TESTBED_operation_done (op);
  149. }
  150. /**
  151. * Signature of a main function for a testcase.
  152. *
  153. * @param cls closure
  154. * @param h the run handle
  155. * @param num_peers number of peers in 'peers'
  156. * @param peers handle to peers run in the testbed. NULL upon timeout (see
  157. * GNUNET_TESTBED_test_run()).
  158. * @param links_succeeded the number of overlay link connection attempts that
  159. * succeeded
  160. * @param links_failed the number of overlay link connection attempts that
  161. * failed
  162. * @see GNUNET_TESTBED_test_run()
  163. */
  164. static void
  165. test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h,
  166. unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
  167. unsigned int links_succeeded, unsigned int links_failed)
  168. {
  169. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  170. "%d peers started. %d links succeeded. %d links failed.\n",
  171. num_peers, links_succeeded, links_failed);
  172. GNUNET_assert (NUM_PEERS == num_peers);
  173. GNUNET_assert (0 == links_failed);
  174. /* Schedule test timeout */
  175. shutdown_task =
  176. GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &do_shutdown, NULL);
  177. GNUNET_TESTBED_peer_get_information (peers[0],
  178. GNUNET_TESTBED_PIT_CONFIGURATION,
  179. &peer_info_cb, peers[0]);
  180. }
  181. int
  182. main (int argc, char *argv[])
  183. {
  184. GNUNET_log_setup (testname, "INFO", NULL);
  185. if (GNUNET_OK ==
  186. GNUNET_TESTBED_test_run (testname, cfg_filename, NUM_PEERS, 0, NULL, NULL,
  187. &test_master, NULL))
  188. return ok;
  189. return 1;
  190. }