aboutsummaryrefslogtreecommitdiff
path: root/src/dht/test_dht_twopeer_put_get.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-09-27 20:19:30 +0000
committerChristian Grothoff <christian@grothoff.org>2011-09-27 20:19:30 +0000
commitdb5bee94c05f667e6b0a0e4f894900a7f9145c6b (patch)
treedc159990da687dd6f7544c38934d4cd06d089b40 /src/dht/test_dht_twopeer_put_get.c
parent8b64acf6f363f5624917d3808a5096577dcd20d5 (diff)
downloadgnunet-db5bee94c05f667e6b0a0e4f894900a7f9145c6b.tar.gz
gnunet-db5bee94c05f667e6b0a0e4f894900a7f9145c6b.zip
fixes
Diffstat (limited to 'src/dht/test_dht_twopeer_put_get.c')
-rw-r--r--src/dht/test_dht_twopeer_put_get.c129
1 files changed, 21 insertions, 108 deletions
diff --git a/src/dht/test_dht_twopeer_put_get.c b/src/dht/test_dht_twopeer_put_get.c
index 82966e944..cf5291346 100644
--- a/src/dht/test_dht_twopeer_put_get.c
+++ b/src/dht/test_dht_twopeer_put_get.c
@@ -39,12 +39,12 @@
39#include "platform.h" 39#include "platform.h"
40#include "gnunet_testing_lib.h" 40#include "gnunet_testing_lib.h"
41#include "gnunet_core_service.h" 41#include "gnunet_core_service.h"
42#include "gnunet_dht_service.h" 42#include "gnunet_dht_service_new.h"
43#include "block_dns.h" 43#include "block_dns.h"
44#include "gnunet_signatures.h" 44#include "gnunet_signatures.h"
45 45
46/* DEFINES */ 46/* DEFINES */
47#define VERBOSE GNUNET_NO 47#define VERBOSE GNUNET_YES
48 48
49/* Timeout for entire testcase */ 49/* Timeout for entire testcase */
50#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5) 50#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
@@ -55,8 +55,6 @@
55/* If number of peers not in config file, use this number */ 55/* If number of peers not in config file, use this number */
56#define DEFAULT_NUM_PEERS 2 56#define DEFAULT_NUM_PEERS 2
57 57
58#define DNS GNUNET_NO
59
60/* Globals */ 58/* Globals */
61 59
62/** 60/**
@@ -105,15 +103,11 @@ static unsigned int total_connections;
105static unsigned int failed_connections; 103static unsigned int failed_connections;
106 104
107/* Task handle to use to schedule test failure */ 105/* Task handle to use to schedule test failure */
108GNUNET_SCHEDULER_TaskIdentifier die_task; 106static GNUNET_SCHEDULER_TaskIdentifier die_task;
109 107
110/* Global return value (0 for success, anything else for failure) */ 108/* Global return value (0 for success, anything else for failure) */
111static int ok; 109static int ok;
112 110
113#if DNS
114struct GNUNET_DNS_Record data;
115#endif
116
117/** 111/**
118 * Peer identity of the first peer started. 112 * Peer identity of the first peer started.
119 */ 113 */
@@ -209,11 +203,13 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
209 * @param size number of bytes in data 203 * @param size number of bytes in data
210 * @param data pointer to the result data 204 * @param data pointer to the result data
211 */ 205 */
212void 206static void
213get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp, 207get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
214 const GNUNET_HashCode * key, 208 const GNUNET_HashCode * key,
215 const struct GNUNET_PeerIdentity *const *get_path, 209 const struct GNUNET_PeerIdentity *get_path,
216 const struct GNUNET_PeerIdentity *const *put_path, 210 unsigned int get_path_size,
211 const struct GNUNET_PeerIdentity *put_path,
212 unsigned int put_path_size,
217 enum GNUNET_BLOCK_Type type, size_t size, 213 enum GNUNET_BLOCK_Type type, size_t size,
218 const void *result_data) 214 const void *result_data)
219{ 215{
@@ -223,19 +219,6 @@ get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
223 memset (&original_key, 42, sizeof (GNUNET_HashCode)); /* Set the key to what it was set to previously */ 219 memset (&original_key, 42, sizeof (GNUNET_HashCode)); /* Set the key to what it was set to previously */
224 memset (original_data, 43, sizeof (original_data)); 220 memset (original_data, 43, sizeof (original_data));
225 221
226#if DNS
227 if ((sizeof (original_data) != size) ||
228 (0 != memcmp (&data.service_descriptor, key, sizeof (GNUNET_HashCode))) ||
229 (0 != memcmp ((char *) &data, result_data, sizeof (original_data))))
230 {
231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
232 "Key or data is not the same as was inserted!\n");
233 GNUNET_SCHEDULER_cancel (die_task);
234 GNUNET_SCHEDULER_add_now (&end_badly,
235 "key or data mismatch in get response!\n");
236 return;
237 }
238#else
239 if ((sizeof (original_data) != size) || 222 if ((sizeof (original_data) != size) ||
240 (0 != memcmp (&original_key, key, sizeof (GNUNET_HashCode))) || 223 (0 != memcmp (&original_key, key, sizeof (GNUNET_HashCode))) ||
241 (0 != memcmp (original_data, result_data, sizeof (original_data)))) 224 (0 != memcmp (original_data, result_data, sizeof (original_data))))
@@ -247,36 +230,13 @@ get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
247 "key or data mismatch in get response!\n"); 230 "key or data mismatch in get response!\n");
248 return; 231 return;
249 } 232 }
250#endif
251 233
252 GNUNET_SCHEDULER_cancel (die_task); 234 GNUNET_SCHEDULER_cancel (die_task);
253 GNUNET_DHT_get_stop (global_get_handle); 235 GNUNET_DHT_get_stop (global_get_handle);
236 global_get_handle = NULL;
254 GNUNET_SCHEDULER_add_now (&finish_testing, NULL); 237 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
255} 238}
256 239
257/**
258 * Start the GET request for the same key/data that was inserted.
259 */
260static void
261do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262{
263 GNUNET_HashCode key; /* Key for data lookup */
264
265#if DNS
266 memcpy (&key, &data.service_descriptor, sizeof (GNUNET_HashCode));
267#else
268 memset (&key, 42, sizeof (GNUNET_HashCode)); /* Set the key to the same thing as when data was inserted */
269#endif
270 global_get_handle =
271 GNUNET_DHT_get_start (peer2dht, GNUNET_TIME_relative_get_forever (),
272#if DNS
273 GNUNET_BLOCK_TYPE_DNS,
274#else
275 GNUNET_BLOCK_TYPE_TEST,
276#endif
277 &key, DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_NONE,
278 NULL, 0, NULL, 0, &get_result_iterator, NULL);
279}
280 240
281/** 241/**
282 * Called when the PUT request has been transmitted to the DHT service. 242 * Called when the PUT request has been transmitted to the DHT service.
@@ -285,16 +245,22 @@ do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
285static void 245static void
286put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 246put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
287{ 247{
248 GNUNET_HashCode key; /* Key for data lookup */
249
288 GNUNET_SCHEDULER_cancel (die_task); 250 GNUNET_SCHEDULER_cancel (die_task);
289 die_task = 251 die_task =
290 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &end_badly, 252 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &end_badly,
291 "waiting for get response (data not found)"); 253 "waiting for get response (data not found)");
292 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 254
293 (GNUNET_TIME_UNIT_SECONDS, 10), &do_get, NULL); 255 memset (&key, 42, sizeof (GNUNET_HashCode)); /* Set the key to the same thing as when data was inserted */
256 global_get_handle =
257 GNUNET_DHT_get_start (peer2dht, GNUNET_TIME_relative_get_forever (),
258 GNUNET_BLOCK_TYPE_TEST,
259 &key, 1, GNUNET_DHT_RO_NONE,
260 NULL, 0, &get_result_iterator, NULL);
294} 261}
295 262
296 263
297#if !DNS
298/** 264/**
299 * Set up some data, and call API PUT function 265 * Set up some data, and call API PUT function
300 */ 266 */
@@ -308,64 +274,12 @@ do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
308 memset (data, 43, sizeof (data)); 274 memset (data, 43, sizeof (data));
309 275
310 /* Insert the data at the first peer */ 276 /* Insert the data at the first peer */
311 GNUNET_DHT_put (peer1dht, &key, DEFAULT_PUT_REPLICATION, GNUNET_DHT_RO_NONE, 277 GNUNET_DHT_put (peer1dht, &key, 1, GNUNET_DHT_RO_NONE,
312 GNUNET_BLOCK_TYPE_TEST, sizeof (data), data, 278 GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
313 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL, 279 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
314 &put_finished, NULL); 280 &put_finished, NULL);
315} 281}
316#else
317
318/**
319 * Set up some data, and call API PUT function
320 */
321static void
322do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
323{
324 char *name = "philipptoelke.gnunet.";
325 size_t size = sizeof (struct GNUNET_DNS_Record);
326
327 memset (&data, 0, size);
328
329 data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
330 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
331 282
332 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
333
334 data.service_type = htonl (GNUNET_DNS_SERVICE_TYPE_UDP);
335 data.ports = htons (69);
336
337 char *keyfile;
338
339 GNUNET_asprintf (&keyfile, "/tmp/test_dns_data_key");
340 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
341 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
342 GNUNET_free (keyfile);
343 GNUNET_assert (my_private_key != NULL);
344
345 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
346
347 data.expiration_time =
348 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
349
350 /* Sign the block */
351 if (GNUNET_OK !=
352 GNUNET_CRYPTO_rsa_sign (my_private_key, &data.purpose, &data.signature))
353 {
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
355 return;
356 }
357 GNUNET_CRYPTO_rsa_key_free (my_private_key);
358
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x\n",
360 *((unsigned int *) &data.service_descriptor));
361
362 GNUNET_DHT_put (peer1dht, &data.service_descriptor, DEFAULT_PUT_REPLICATION,
363 GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size,
364 (char *) &data,
365 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
366 GNUNET_TIME_UNIT_MINUTES, &put_finished, NULL);
367}
368#endif
369 283
370/** 284/**
371 * This function is called whenever a connection attempt is finished between two of 285 * This function is called whenever a connection attempt is finished between two of
@@ -376,7 +290,7 @@ do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
376 * The emsg variable is NULL on success (peers connected), and non-NULL on 290 * The emsg variable is NULL on success (peers connected), and non-NULL on
377 * failure (peers failed to connect). 291 * failure (peers failed to connect).
378 */ 292 */
379void 293static void
380topology_callback (void *cls, const struct GNUNET_PeerIdentity *first, 294topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
381 const struct GNUNET_PeerIdentity *second, uint32_t distance, 295 const struct GNUNET_PeerIdentity *second, uint32_t distance,
382 const struct GNUNET_CONFIGURATION_Handle *first_cfg, 296 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
@@ -415,8 +329,7 @@ topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
415 die_task = 329 die_task =
416 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from test gets"); 330 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from test gets");
417 331
418 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 332 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_put, NULL);
419 (GNUNET_TIME_UNIT_SECONDS, 2), &do_put, NULL);
420 } 333 }
421 else if (total_connections + failed_connections == expected_connections) 334 else if (total_connections + failed_connections == expected_connections)
422 { 335 {