diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-08-19 20:32:27 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-08-19 20:32:27 +0000 |
commit | 491e6b15d64983d8885067d3ef7bcb354d9555c6 (patch) | |
tree | 968109b149ea6cccb8d3c4123bc59d1aad9e1ec4 | |
parent | 41b260fd0f3db3daf54c5e03ccb07af4b1fc5859 (diff) | |
download | gnunet-491e6b15d64983d8885067d3ef7bcb354d9555c6.tar.gz gnunet-491e6b15d64983d8885067d3ef7bcb354d9555c6.zip |
sensor: test case for proof-of-work + fixes
-rw-r--r-- | src/include/gnunet_sensor_util_lib.h | 41 | ||||
-rw-r--r-- | src/peerstore/gnunet-service-peerstore.c | 4 | ||||
-rw-r--r-- | src/sensor/Makefile.am | 10 | ||||
-rw-r--r-- | src/sensor/sensor_util_lib_crypto.c | 95 | ||||
-rw-r--r-- | src/sensor/test_gnunet-service-sensor_reporting.c | 10 | ||||
-rw-r--r-- | src/sensor/test_pow_sign.c | 199 | ||||
-rw-r--r-- | src/sensor/test_pow_sign.conf | 2 |
7 files changed, 312 insertions, 49 deletions
diff --git a/src/include/gnunet_sensor_util_lib.h b/src/include/gnunet_sensor_util_lib.h index d1b1a925e..55cfc9002 100644 --- a/src/include/gnunet_sensor_util_lib.h +++ b/src/include/gnunet_sensor_util_lib.h | |||
@@ -415,10 +415,26 @@ struct GNUNET_SENSOR_crypto_pow_block | |||
415 | struct GNUNET_CRYPTO_EddsaSignature signature; | 415 | struct GNUNET_CRYPTO_EddsaSignature signature; |
416 | 416 | ||
417 | /** | 417 | /** |
418 | * Purpose of signing, data is allocated after this. | 418 | * Size of the msg component (allocated after this struct) |
419 | */ | ||
420 | size_t msg_size; | ||
421 | |||
422 | /** | ||
423 | * Purpose of signing. | ||
424 | * Data is allocated after this (timestamp, public_key, msg). | ||
419 | */ | 425 | */ |
420 | struct GNUNET_CRYPTO_EccSignaturePurpose purpose; | 426 | struct GNUNET_CRYPTO_EccSignaturePurpose purpose; |
421 | 427 | ||
428 | /** | ||
429 | * First part of data - timestamp | ||
430 | */ | ||
431 | struct GNUNET_TIME_Absolute timestamp; | ||
432 | |||
433 | /** | ||
434 | * Second part of data - Public key | ||
435 | */ | ||
436 | struct GNUNET_CRYPTO_EddsaPublicKey public_key; | ||
437 | |||
422 | }; | 438 | }; |
423 | 439 | ||
424 | 440 | ||
@@ -468,6 +484,29 @@ GNUNET_SENSOR_crypto_pow_sign (void *msg, size_t msg_size, | |||
468 | GNUNET_SENSOR_UTIL_pow_callback callback, | 484 | GNUNET_SENSOR_UTIL_pow_callback callback, |
469 | void *callback_cls); | 485 | void *callback_cls); |
470 | 486 | ||
487 | |||
488 | /** | ||
489 | * Verify that proof-of-work and signature in the given block are valid. | ||
490 | * If all valid, a pointer to the payload within the block is set and the size | ||
491 | * of the payload is returned. | ||
492 | * | ||
493 | * **VERY IMPORTANT** : You will still need to verify the timestamp yourself. | ||
494 | * | ||
495 | * @param block The block received and needs to be verified | ||
496 | * @param matching_bits Number of leading zeros in the hash used to verify pow | ||
497 | * @param public_key Public key of the peer that sent this block | ||
498 | * @param purpose Expected signing purpose | ||
499 | * @param payload Where to store the pointer to the payload | ||
500 | * @return Size of the payload | ||
501 | */ | ||
502 | size_t | ||
503 | GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block * | ||
504 | block, int matching_bits, | ||
505 | struct GNUNET_CRYPTO_EddsaPublicKey * | ||
506 | public_key, uint32_t purpose, | ||
507 | void **payload); | ||
508 | |||
509 | |||
471 | #if 0 /* keep Emacsens' auto-indent happy */ | 510 | #if 0 /* keep Emacsens' auto-indent happy */ |
472 | { | 511 | { |
473 | #endif | 512 | #endif |
diff --git a/src/peerstore/gnunet-service-peerstore.c b/src/peerstore/gnunet-service-peerstore.c index c674d3c11..97c272b18 100644 --- a/src/peerstore/gnunet-service-peerstore.c +++ b/src/peerstore/gnunet-service-peerstore.c | |||
@@ -93,12 +93,12 @@ static struct ClientEntry *client_tail; | |||
93 | /** | 93 | /** |
94 | * Are we in the process of shutting down the service? #GNUNET_YES / #GNUNET_NO | 94 | * Are we in the process of shutting down the service? #GNUNET_YES / #GNUNET_NO |
95 | */ | 95 | */ |
96 | int in_shutdown; | 96 | static int in_shutdown; |
97 | 97 | ||
98 | /** | 98 | /** |
99 | * Perform the actual shutdown operations | 99 | * Perform the actual shutdown operations |
100 | */ | 100 | */ |
101 | void | 101 | static void |
102 | do_shutdown () | 102 | do_shutdown () |
103 | { | 103 | { |
104 | if (NULL != db_lib_name) | 104 | if (NULL != db_lib_name) |
diff --git a/src/sensor/Makefile.am b/src/sensor/Makefile.am index d6577ee63..00c8f8301 100644 --- a/src/sensor/Makefile.am +++ b/src/sensor/Makefile.am | |||
@@ -85,7 +85,8 @@ libgnunet_plugin_sensor_model_gaussian_la_DEPENDENCIES = \ | |||
85 | 85 | ||
86 | check_PROGRAMS = \ | 86 | check_PROGRAMS = \ |
87 | test_sensor_api \ | 87 | test_sensor_api \ |
88 | test_gnunet-service-sensor_reporting | 88 | test_gnunet-service-sensor_reporting \ |
89 | test_pow_sign | ||
89 | 90 | ||
90 | if ENABLE_TEST_RUN | 91 | if ENABLE_TEST_RUN |
91 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH; | 92 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH; |
@@ -106,6 +107,13 @@ test_gnunet_service_sensor_reporting_LDADD = \ | |||
106 | $(top_builddir)/src/testbed/libgnunettestbed.la \ | 107 | $(top_builddir)/src/testbed/libgnunettestbed.la \ |
107 | $(top_builddir)/src/peerstore/libgnunetpeerstore.la | 108 | $(top_builddir)/src/peerstore/libgnunetpeerstore.la |
108 | 109 | ||
110 | test_pow_sign_SOURCES = \ | ||
111 | test_pow_sign.c | ||
112 | test_pow_sign_LDADD = \ | ||
113 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
114 | $(top_builddir)/src/testbed/libgnunettestbed.la \ | ||
115 | libgnunetsensorutil.la | ||
116 | |||
109 | pkgsensordir = sensors | 117 | pkgsensordir = sensors |
110 | 118 | ||
111 | install-data-local: | 119 | install-data-local: |
diff --git a/src/sensor/sensor_util_lib_crypto.c b/src/sensor/sensor_util_lib_crypto.c index bccbe74f9..7cf505186 100644 --- a/src/sensor/sensor_util_lib_crypto.c +++ b/src/sensor/sensor_util_lib_crypto.c | |||
@@ -23,7 +23,7 @@ | |||
23 | * @brief senor utilities - crpyto related functions | 23 | * @brief senor utilities - crpyto related functions |
24 | * @author Omar Tarabai | 24 | * @author Omar Tarabai |
25 | */ | 25 | */ |
26 | 26 | #include <inttypes.h> | |
27 | #include "platform.h" | 27 | #include "platform.h" |
28 | #include "gnunet_util_lib.h" | 28 | #include "gnunet_util_lib.h" |
29 | #include "gnunet_sensor_util_lib.h" | 29 | #include "gnunet_sensor_util_lib.h" |
@@ -38,17 +38,7 @@ struct GNUNET_SENSOR_crypto_pow_context | |||
38 | { | 38 | { |
39 | 39 | ||
40 | /** | 40 | /** |
41 | * Buffer of the complete message to calculate the pow for | 41 | * Proof-of-work value |
42 | */ | ||
43 | void *buf; | ||
44 | |||
45 | /** | ||
46 | * Size of buf | ||
47 | */ | ||
48 | size_t buf_size; | ||
49 | |||
50 | /** | ||
51 | * Proof-of-work number | ||
52 | */ | 42 | */ |
53 | uint64_t pow; | 43 | uint64_t pow; |
54 | 44 | ||
@@ -77,6 +67,21 @@ struct GNUNET_SENSOR_crypto_pow_context | |||
77 | */ | 67 | */ |
78 | GNUNET_SCHEDULER_TaskIdentifier calculate_pow_task; | 68 | GNUNET_SCHEDULER_TaskIdentifier calculate_pow_task; |
79 | 69 | ||
70 | /** | ||
71 | * Size of msg (allocated after this struct) | ||
72 | */ | ||
73 | size_t msg_size; | ||
74 | |||
75 | /** | ||
76 | * Timestamp of the message | ||
77 | */ | ||
78 | struct GNUNET_TIME_Absolute timestamp; | ||
79 | |||
80 | /** | ||
81 | * Public key of the peer sending this message | ||
82 | */ | ||
83 | struct GNUNET_CRYPTO_EddsaPublicKey public_key; | ||
84 | |||
80 | }; | 85 | }; |
81 | 86 | ||
82 | 87 | ||
@@ -123,6 +128,7 @@ check_pow (void *msg, size_t msg_size, uint64_t pow, int matching_bits) | |||
123 | char buf[msg_size + sizeof (pow)] GNUNET_ALIGN; | 128 | char buf[msg_size + sizeof (pow)] GNUNET_ALIGN; |
124 | struct GNUNET_HashCode result; | 129 | struct GNUNET_HashCode result; |
125 | 130 | ||
131 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Msg size: %" PRIu64 ".\n", msg_size); | ||
126 | memcpy (buf, &pow, sizeof (pow)); | 132 | memcpy (buf, &pow, sizeof (pow)); |
127 | memcpy (&buf[sizeof (pow)], msg, msg_size); | 133 | memcpy (&buf[sizeof (pow)], msg, msg_size); |
128 | pow_hash (buf, sizeof (buf), &result); | 134 | pow_hash (buf, sizeof (buf), &result); |
@@ -144,17 +150,26 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
144 | int sign_result; | 150 | int sign_result; |
145 | 151 | ||
146 | if (GNUNET_YES == | 152 | if (GNUNET_YES == |
147 | check_pow (cx->buf, cx->buf_size, cx->pow, cx->matching_bits)) | 153 | check_pow (&cx->timestamp, |
154 | sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + | ||
155 | sizeof (struct GNUNET_TIME_Absolute) + cx->msg_size, cx->pow, | ||
156 | cx->matching_bits)) | ||
148 | { | 157 | { |
149 | cx->calculate_pow_task = GNUNET_SCHEDULER_NO_TASK; | 158 | cx->calculate_pow_task = GNUNET_SCHEDULER_NO_TASK; |
150 | result_block = | 159 | result_block = |
151 | GNUNET_malloc (sizeof (struct GNUNET_SENSOR_crypto_pow_block) + | 160 | GNUNET_malloc (sizeof (struct GNUNET_SENSOR_crypto_pow_block) + |
152 | cx->buf_size); | 161 | cx->msg_size); |
162 | result_block->msg_size = cx->msg_size; | ||
163 | result_block->pow = cx->pow; | ||
164 | result_block->timestamp = cx->timestamp; | ||
165 | result_block->public_key = cx->public_key; | ||
153 | result_block->purpose.purpose = | 166 | result_block->purpose.purpose = |
154 | GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT; | 167 | htonl (GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT); |
155 | result_block->purpose.size = | 168 | result_block->purpose.size = |
156 | sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + cx->buf_size; | 169 | htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + |
157 | memcpy (&result_block[1], cx->buf, cx->buf_size); | 170 | sizeof (struct GNUNET_TIME_Absolute) + |
171 | sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + cx->msg_size); | ||
172 | memcpy (&result_block[1], &cx[1], cx->msg_size); | ||
158 | sign_result = | 173 | sign_result = |
159 | GNUNET_CRYPTO_eddsa_sign (&cx->private_key, &result_block->purpose, | 174 | GNUNET_CRYPTO_eddsa_sign (&cx->private_key, &result_block->purpose, |
160 | &result_block->signature); | 175 | &result_block->signature); |
@@ -178,11 +193,6 @@ void | |||
178 | GNUNET_SENSOR_crypto_pow_sign_cancel (struct GNUNET_SENSOR_crypto_pow_context | 193 | GNUNET_SENSOR_crypto_pow_sign_cancel (struct GNUNET_SENSOR_crypto_pow_context |
179 | *cx) | 194 | *cx) |
180 | { | 195 | { |
181 | if (NULL != cx->buf) | ||
182 | { | ||
183 | GNUNET_free (cx->buf); | ||
184 | cx->buf = NULL; | ||
185 | } | ||
186 | GNUNET_free (cx); | 196 | GNUNET_free (cx); |
187 | } | 197 | } |
188 | 198 | ||
@@ -212,15 +222,14 @@ GNUNET_SENSOR_crypto_pow_sign (void *msg, size_t msg_size, | |||
212 | void *callback_cls) | 222 | void *callback_cls) |
213 | { | 223 | { |
214 | struct GNUNET_SENSOR_crypto_pow_context *cx; | 224 | struct GNUNET_SENSOR_crypto_pow_context *cx; |
215 | void *buf; | ||
216 | size_t buf_size; | ||
217 | 225 | ||
218 | buf_size = msg_size + sizeof (*timestamp) + sizeof (*public_key); | 226 | cx = GNUNET_malloc (sizeof (struct GNUNET_SENSOR_crypto_pow_context) + |
219 | buf = GNUNET_malloc (buf_size); | 227 | msg_size); |
220 | cx = GNUNET_new (struct GNUNET_SENSOR_crypto_pow_context); | ||
221 | 228 | ||
222 | cx->buf = buf; | 229 | cx->timestamp = *timestamp; |
223 | cx->buf_size = buf_size; | 230 | cx->public_key = *public_key; |
231 | cx->msg_size = msg_size; | ||
232 | memcpy (&cx[1], msg, msg_size); | ||
224 | cx->pow = 0; | 233 | cx->pow = 0; |
225 | cx->private_key = *private_key; | 234 | cx->private_key = *private_key; |
226 | cx->matching_bits = matching_bits; | 235 | cx->matching_bits = matching_bits; |
@@ -236,9 +245,12 @@ GNUNET_SENSOR_crypto_pow_sign (void *msg, size_t msg_size, | |||
236 | * If all valid, a pointer to the payload within the block is set and the size | 245 | * If all valid, a pointer to the payload within the block is set and the size |
237 | * of the payload is returned. | 246 | * of the payload is returned. |
238 | * | 247 | * |
248 | * **VERY IMPORTANT** : You will still need to verify the timestamp yourself. | ||
249 | * | ||
239 | * @param block The block received and needs to be verified | 250 | * @param block The block received and needs to be verified |
240 | * @param matching_bits Number of leading zeros in the hash used to verify pow | 251 | * @param matching_bits Number of leading zeros in the hash used to verify pow |
241 | * @param public_key Public key of the peer that sent this block | 252 | * @param public_key Public key of the peer that sent this block |
253 | * @param purpose Expected signing purpose | ||
242 | * @param payload Where to store the pointer to the payload | 254 | * @param payload Where to store the pointer to the payload |
243 | * @return Size of the payload | 255 | * @return Size of the payload |
244 | */ | 256 | */ |
@@ -246,28 +258,31 @@ size_t | |||
246 | GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block * | 258 | GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block * |
247 | block, int matching_bits, | 259 | block, int matching_bits, |
248 | struct GNUNET_CRYPTO_EddsaPublicKey * | 260 | struct GNUNET_CRYPTO_EddsaPublicKey * |
249 | public_key, void **payload) | 261 | public_key, uint32_t purpose, |
262 | void **payload) | ||
250 | { | 263 | { |
251 | void *msg; | 264 | /* Check public key */ |
252 | size_t msg_size; | 265 | if (0 != memcmp (public_key, &block->public_key, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))) |
253 | 266 | { | |
267 | LOG (GNUNET_ERROR_TYPE_WARNING, "Public key mismatch.\n"); | ||
268 | return 0; | ||
269 | } | ||
254 | /* Check signature */ | 270 | /* Check signature */ |
255 | if (GNUNET_OK != | 271 | if (GNUNET_OK != |
256 | GNUNET_CRYPTO_eddsa_verify (block->purpose.purpose, &block->purpose, | 272 | GNUNET_CRYPTO_eddsa_verify (purpose, &block->purpose, |
257 | &block->signature, public_key)) | 273 | &block->signature, public_key)) |
258 | { | 274 | { |
259 | LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid signature.\n"); | 275 | LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid signature.\n"); |
260 | return 0; | 276 | return 0; |
261 | } | 277 | } |
262 | /* Check pow */ | 278 | /* Check pow */ |
263 | msg = &block[1]; | 279 | if (GNUNET_NO == check_pow (&block->timestamp, |
264 | msg_size = | 280 | sizeof (struct GNUNET_TIME_Absolute) + |
265 | block->purpose.size - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose); | 281 | sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + block->msg_size, block->pow, matching_bits)) |
266 | if (GNUNET_NO == check_pow (msg, msg_size, block->pow, matching_bits)) | ||
267 | { | 282 | { |
268 | LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid proof-of-work.\n"); | 283 | LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid proof-of-work.\n"); |
269 | return 0; | 284 | return 0; |
270 | } | 285 | } |
271 | *payload = msg; | 286 | *payload = &block[1]; |
272 | return msg_size; | 287 | return block->msg_size; |
273 | } | 288 | } |
diff --git a/src/sensor/test_gnunet-service-sensor_reporting.c b/src/sensor/test_gnunet-service-sensor_reporting.c index 3905e8876..afe45afea 100644 --- a/src/sensor/test_gnunet-service-sensor_reporting.c +++ b/src/sensor/test_gnunet-service-sensor_reporting.c | |||
@@ -96,27 +96,27 @@ struct TestPeer | |||
96 | /** | 96 | /** |
97 | * Test name | 97 | * Test name |
98 | */ | 98 | */ |
99 | const static char *testname = "test_gnunet-service-sensor_reporting"; | 99 | static const char *testname = "test_gnunet-service-sensor_reporting"; |
100 | 100 | ||
101 | /** | 101 | /** |
102 | * Name of GNUNET config file used in this test | 102 | * Name of GNUNET config file used in this test |
103 | */ | 103 | */ |
104 | const static char *cfg_filename = "test_gnunet-service-sensor_reporting.conf"; | 104 | static const char *cfg_filename = "test_gnunet-service-sensor_reporting.conf"; |
105 | 105 | ||
106 | /** | 106 | /** |
107 | * Test sensor name | 107 | * Test sensor name |
108 | */ | 108 | */ |
109 | const static char *sensor_name = "test-sensor-statistics"; | 109 | static const char *sensor_name = "test-sensor-statistics"; |
110 | 110 | ||
111 | /** | 111 | /** |
112 | * Path to read test sensor from | 112 | * Path to read test sensor from |
113 | */ | 113 | */ |
114 | const static char *sensor_path_src = "test_sensors/test-sensor-statistics"; | 114 | static const char *sensor_path_src = "test_sensors/test-sensor-statistics"; |
115 | 115 | ||
116 | /** | 116 | /** |
117 | * Path to write new test sensor to | 117 | * Path to write new test sensor to |
118 | */ | 118 | */ |
119 | const static char *sensor_path_dest = | 119 | static const char *sensor_path_dest = |
120 | "/tmp/test-gnunet-service-sensor-reporting/test-sensor-statistics"; | 120 | "/tmp/test-gnunet-service-sensor-reporting/test-sensor-statistics"; |
121 | 121 | ||
122 | /** | 122 | /** |
diff --git a/src/sensor/test_pow_sign.c b/src/sensor/test_pow_sign.c new file mode 100644 index 000000000..84e990cff --- /dev/null +++ b/src/sensor/test_pow_sign.c | |||
@@ -0,0 +1,199 @@ | |||
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 | /** | ||
32 | * Number of peers to start for the test | ||
33 | */ | ||
34 | #define NUM_PEERS 1 | ||
35 | |||
36 | /** | ||
37 | * Size of the message exchanged | ||
38 | */ | ||
39 | #define MSG_SIZE 1024 | ||
40 | |||
41 | /** | ||
42 | * Number of matching bits to use for generating proof-of-work | ||
43 | */ | ||
44 | #define MATCHING_BITS 2 | ||
45 | |||
46 | /** | ||
47 | * Test timeout | ||
48 | */ | ||
49 | #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1) | ||
50 | |||
51 | /** | ||
52 | * Test name | ||
53 | */ | ||
54 | static const char *testname = "test_pow_sign"; | ||
55 | |||
56 | /** | ||
57 | * Name of GNUNET config file used in this test | ||
58 | */ | ||
59 | static const char *cfg_filename = "test_pow_sign.conf"; | ||
60 | |||
61 | /** | ||
62 | * Status of the test to be returned by main() | ||
63 | */ | ||
64 | static int ok = 1; | ||
65 | |||
66 | /** | ||
67 | * Task used to shutdown / expire the test | ||
68 | */ | ||
69 | static GNUNET_SCHEDULER_TaskIdentifier shutdown_task; | ||
70 | |||
71 | /** | ||
72 | * Message to be exchanged | ||
73 | */ | ||
74 | static char msg[MSG_SIZE]; | ||
75 | |||
76 | /** | ||
77 | * Private key of sending peer | ||
78 | */ | ||
79 | struct GNUNET_CRYPTO_EddsaPrivateKey *private_key; | ||
80 | |||
81 | /** | ||
82 | * Public key of sending peer | ||
83 | */ | ||
84 | struct GNUNET_CRYPTO_EddsaPublicKey *public_key; | ||
85 | |||
86 | |||
87 | /** | ||
88 | * Shutdown task | ||
89 | * | ||
90 | * @param cls Closure (unused) | ||
91 | * @param tc Task context (unused) | ||
92 | */ | ||
93 | static void | ||
94 | do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
95 | { | ||
96 | if (NULL != private_key) | ||
97 | { | ||
98 | GNUNET_free (private_key); | ||
99 | private_key = NULL; | ||
100 | } | ||
101 | if (NULL != public_key) | ||
102 | { | ||
103 | GNUNET_free (public_key); | ||
104 | public_key = NULL; | ||
105 | } | ||
106 | GNUNET_SCHEDULER_shutdown (); | ||
107 | } | ||
108 | |||
109 | |||
110 | static void pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block) | ||
111 | { | ||
112 | void *response; | ||
113 | |||
114 | printf ("Received block:\n" | ||
115 | "pow: %" PRIu64 ".\n", block->pow); | ||
116 | GNUNET_assert (MSG_SIZE == | ||
117 | GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS, | ||
118 | public_key, GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT, &response)); | ||
119 | GNUNET_assert (0 == memcmp(msg, response, MSG_SIZE)); | ||
120 | ok = 0; | ||
121 | GNUNET_SCHEDULER_cancel(shutdown_task); | ||
122 | GNUNET_SCHEDULER_add_now (do_shutdown, NULL); | ||
123 | } | ||
124 | |||
125 | |||
126 | /** | ||
127 | * Callback to be called when the requested peer information is available | ||
128 | * | ||
129 | * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information() | ||
130 | * @param op the operation this callback corresponds to | ||
131 | * @param pinfo the result; will be NULL if the operation has failed | ||
132 | * @param emsg error message if the operation has failed; will be NULL if the | ||
133 | * operation is successfull | ||
134 | */ | ||
135 | static void | ||
136 | peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op, | ||
137 | const struct GNUNET_TESTBED_PeerInformation *pinfo, | ||
138 | const char *emsg) | ||
139 | { | ||
140 | struct GNUNET_TIME_Absolute timestamp; | ||
141 | |||
142 | /* generate random data block */ | ||
143 | GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, msg, MSG_SIZE); | ||
144 | /* get private and public keys */ | ||
145 | private_key = | ||
146 | GNUNET_CRYPTO_eddsa_key_create_from_configuration (pinfo->result.cfg); | ||
147 | GNUNET_assert (NULL != private_key); | ||
148 | public_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPublicKey); | ||
149 | GNUNET_CRYPTO_eddsa_key_get_public (private_key, public_key); | ||
150 | /* create pow and sign */ | ||
151 | timestamp = GNUNET_TIME_absolute_get(); | ||
152 | GNUNET_SENSOR_crypto_pow_sign (msg, MSG_SIZE, ×tamp, | ||
153 | public_key, private_key, MATCHING_BITS, &pow_cb, NULL); | ||
154 | } | ||
155 | |||
156 | |||
157 | /** | ||
158 | * Signature of a main function for a testcase. | ||
159 | * | ||
160 | * @param cls closure | ||
161 | * @param h the run handle | ||
162 | * @param num_peers number of peers in 'peers' | ||
163 | * @param peers handle to peers run in the testbed. NULL upon timeout (see | ||
164 | * GNUNET_TESTBED_test_run()). | ||
165 | * @param links_succeeded the number of overlay link connection attempts that | ||
166 | * succeeded | ||
167 | * @param links_failed the number of overlay link connection attempts that | ||
168 | * failed | ||
169 | * @see GNUNET_TESTBED_test_run() | ||
170 | */ | ||
171 | static void | ||
172 | test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h, | ||
173 | unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers, | ||
174 | unsigned int links_succeeded, unsigned int links_failed) | ||
175 | { | ||
176 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
177 | "%d peers started. %d links succeeded. %d links failed.\n", | ||
178 | num_peers, links_succeeded, links_failed); | ||
179 | GNUNET_assert (NUM_PEERS == num_peers); | ||
180 | GNUNET_assert (0 == links_failed); | ||
181 | /* Schedule test timeout */ | ||
182 | shutdown_task = | ||
183 | GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &do_shutdown, NULL); | ||
184 | GNUNET_TESTBED_peer_get_information (peers[0], | ||
185 | GNUNET_TESTBED_PIT_CONFIGURATION, | ||
186 | &peer_info_cb, peers[0]); | ||
187 | } | ||
188 | |||
189 | |||
190 | int | ||
191 | main (int argc, char *argv[]) | ||
192 | { | ||
193 | GNUNET_log_setup (testname, "INFO", NULL); | ||
194 | if (GNUNET_OK == | ||
195 | GNUNET_TESTBED_test_run (testname, cfg_filename, NUM_PEERS, 0, NULL, NULL, | ||
196 | &test_master, NULL)) | ||
197 | return ok; | ||
198 | return 1; | ||
199 | } | ||
diff --git a/src/sensor/test_pow_sign.conf b/src/sensor/test_pow_sign.conf new file mode 100644 index 000000000..ae23d4d26 --- /dev/null +++ b/src/sensor/test_pow_sign.conf | |||
@@ -0,0 +1,2 @@ | |||
1 | [arm] | ||
2 | DEFAULTSERVICES = core \ No newline at end of file | ||