2 * This file is part of GNUnet.
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.
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.
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.
21 * @file sensor/test_pow_sign.c
22 * @brief testcase for proof-of-work and signature library functions
26 #include "gnunet_util_lib.h"
27 #include "gnunet_sensor_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_signatures.h"
32 * Number of peers to start for the test
37 * Size of the message exchanged
42 * Number of matching bits to use for generating proof-of-work
44 #define MATCHING_BITS 2
49 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
54 static const char *testname = "test_pow_sign";
57 * Name of GNUNET config file used in this test
59 static const char *cfg_filename = "test_pow_sign.conf";
62 * Status of the test to be returned by main()
67 * Task used to shutdown / expire the test
69 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
72 * Message to be exchanged
74 static char msg[MSG_SIZE];
77 * Private key of sending peer
79 struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
82 * Public key of sending peer
84 struct GNUNET_CRYPTO_EddsaPublicKey *public_key;
90 * @param cls Closure (unused)
91 * @param tc Task context (unused)
94 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 if (NULL != private_key)
98 GNUNET_free (private_key);
101 if (NULL != public_key)
103 GNUNET_free (public_key);
106 GNUNET_SCHEDULER_shutdown ();
111 pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block)
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Received block:\n" "pow: %" PRIu64 ".\n", block->pow);
117 /* Test that the block is valid */
118 GNUNET_assert (MSG_SIZE ==
119 GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
121 GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT,
123 GNUNET_assert (0 == memcmp (msg, response, MSG_SIZE));
124 /* Modify the payload and test that verification returns invalid */
127 GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS,
129 GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT,
132 GNUNET_SCHEDULER_cancel (shutdown_task);
133 GNUNET_SCHEDULER_add_now (do_shutdown, NULL);
138 * Callback to be called when the requested peer information is available
140 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
141 * @param op the operation this callback corresponds to
142 * @param pinfo the result; will be NULL if the operation has failed
143 * @param emsg error message if the operation has failed; will be NULL if the
144 * operation is successfull
147 peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
148 const struct GNUNET_TESTBED_PeerInformation *pinfo,
151 struct GNUNET_TIME_Absolute timestamp;
153 /* generate random data block */
154 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, msg, MSG_SIZE);
155 /* get private and public keys */
157 GNUNET_CRYPTO_eddsa_key_create_from_configuration (pinfo->result.cfg);
158 GNUNET_assert (NULL != private_key);
159 public_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPublicKey);
161 GNUNET_CRYPTO_eddsa_key_get_public (private_key, public_key);
162 /* create pow and sign */
163 timestamp = GNUNET_TIME_absolute_get ();
164 GNUNET_SENSOR_crypto_pow_sign (msg, MSG_SIZE, ×tamp, public_key,
165 private_key, MATCHING_BITS, &pow_cb, NULL);
166 GNUNET_TESTBED_operation_done (op);
171 * Signature of a main function for a testcase.
174 * @param h the run handle
175 * @param num_peers number of peers in 'peers'
176 * @param peers handle to peers run in the testbed. NULL upon timeout (see
177 * GNUNET_TESTBED_test_run()).
178 * @param links_succeeded the number of overlay link connection attempts that
180 * @param links_failed the number of overlay link connection attempts that
182 * @see GNUNET_TESTBED_test_run()
185 test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h,
186 unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
187 unsigned int links_succeeded, unsigned int links_failed)
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 "%d peers started. %d links succeeded. %d links failed.\n",
191 num_peers, links_succeeded, links_failed);
192 GNUNET_assert (NUM_PEERS == num_peers);
193 GNUNET_assert (0 == links_failed);
194 /* Schedule test timeout */
196 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &do_shutdown, NULL);
197 GNUNET_TESTBED_peer_get_information (peers[0],
198 GNUNET_TESTBED_PIT_CONFIGURATION,
199 &peer_info_cb, peers[0]);
204 main (int argc, char *argv[])
206 GNUNET_log_setup (testname, "INFO", NULL);
208 GNUNET_TESTBED_test_run (testname, cfg_filename, NUM_PEERS, 0, NULL, NULL,