test_dht_api.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file dht/test_dht_api.c
  19. * @brief base test case for dht api
  20. *
  21. * This test case tests DHT api to DUMMY DHT service communication.
  22. *
  23. */
  24. #include "platform.h"
  25. #include "gnunet_common.h"
  26. #include "gnunet_hello_lib.h"
  27. #include "gnunet_getopt_lib.h"
  28. #include "gnunet_os_lib.h"
  29. #include "gnunet_program_lib.h"
  30. #include "gnunet_scheduler_lib.h"
  31. #include "gnunet_dht_service.h"
  32. #include "gnunet_hello_lib.h"
  33. #define VERBOSE GNUNET_NO
  34. #define VERBOSE_ARM GNUNET_NO
  35. #define START_ARM GNUNET_YES
  36. /**
  37. * How long until we really give up on a particular testcase portion?
  38. */
  39. #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
  40. /**
  41. * How long until we give up on any particular operation (and retry)?
  42. */
  43. #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
  44. #define MTYPE 12345
  45. struct RetryContext
  46. {
  47. /**
  48. * When to really abort the operation.
  49. */
  50. struct GNUNET_TIME_Absolute real_timeout;
  51. /**
  52. * What timeout to set for the current attempt (increases)
  53. */
  54. struct GNUNET_TIME_Relative next_timeout;
  55. /**
  56. * The context of the peer we are dealing with.
  57. */
  58. struct PeerContext *peer_ctx;
  59. /**
  60. * The task identifier of the retry task, so it can be cancelled.
  61. */
  62. GNUNET_SCHEDULER_TaskIdentifier retry_task;
  63. };
  64. struct PeerContext
  65. {
  66. struct GNUNET_CONFIGURATION_Handle *cfg;
  67. struct GNUNET_DHT_Handle *dht_handle;
  68. struct GNUNET_PeerIdentity id;
  69. struct GNUNET_DHT_GetHandle *get_handle;
  70. struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
  71. #if START_ARM
  72. struct GNUNET_OS_Process *arm_proc;
  73. #endif
  74. };
  75. static struct PeerContext p1;
  76. struct RetryContext retry_context;
  77. static int ok;
  78. GNUNET_SCHEDULER_TaskIdentifier die_task;
  79. #if VERBOSE
  80. #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
  81. #else
  82. #define OKPP do { ok++; } while (0)
  83. #endif
  84. static void
  85. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  86. {
  87. GNUNET_SCHEDULER_cancel (die_task);
  88. die_task = GNUNET_SCHEDULER_NO_TASK;
  89. GNUNET_DHT_disconnect (p1.dht_handle);
  90. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  91. "DHT disconnected, returning success!\n");
  92. ok = 0;
  93. }
  94. static void
  95. stop_arm (struct PeerContext *p)
  96. {
  97. #if START_ARM
  98. if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
  99. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  100. GNUNET_OS_process_wait (p->arm_proc);
  101. GNUNET_OS_process_close (p->arm_proc);
  102. p->arm_proc = NULL;
  103. #endif
  104. GNUNET_CONFIGURATION_destroy (p->cfg);
  105. }
  106. static void
  107. end_badly ()
  108. {
  109. /* do work here */
  110. #if VERBOSE
  111. fprintf (stderr, "Ending on an unhappy note.\n");
  112. #endif
  113. if ((retry_context.peer_ctx != NULL) &&
  114. (retry_context.peer_ctx->find_peer_handle != NULL))
  115. {
  116. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping find peer request!\n");
  117. GNUNET_DHT_find_peer_stop (retry_context.peer_ctx->find_peer_handle);
  118. }
  119. if ((retry_context.peer_ctx != NULL) &&
  120. (retry_context.peer_ctx->get_handle != NULL))
  121. {
  122. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping get request!\n");
  123. GNUNET_DHT_get_stop (retry_context.peer_ctx->get_handle);
  124. }
  125. if (retry_context.retry_task != GNUNET_SCHEDULER_NO_TASK)
  126. GNUNET_SCHEDULER_cancel (retry_context.retry_task);
  127. GNUNET_DHT_disconnect (p1.dht_handle);
  128. ok = 1;
  129. }
  130. /**
  131. * Signature of the main function of a task.
  132. *
  133. * @param cls closure
  134. * @param tc context information (why was this task triggered now)
  135. */
  136. void
  137. test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  138. {
  139. struct PeerContext *peer = cls;
  140. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
  141. if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
  142. {
  143. GNUNET_break (0);
  144. GNUNET_SCHEDULER_cancel (die_task);
  145. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  146. return;
  147. }
  148. GNUNET_assert (peer->dht_handle != NULL);
  149. GNUNET_DHT_find_peer_stop (peer->find_peer_handle);
  150. peer->find_peer_handle = NULL;
  151. #if HAVE_MALICIOUS
  152. GNUNET_DHT_set_malicious_getter (peer->dht_handle, GNUNET_TIME_UNIT_SECONDS,
  153. NULL, NULL);
  154. GNUNET_DHT_set_malicious_putter (peer->dht_handle, GNUNET_TIME_UNIT_SECONDS,
  155. NULL, NULL);
  156. GNUNET_DHT_set_malicious_dropper (peer->dht_handle, NULL, NULL);
  157. #endif
  158. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  159. (GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
  160. }
  161. /**
  162. * Iterator called on each result obtained from a find peer
  163. * operation
  164. *
  165. * @param cls closure (NULL)
  166. * @param peer the peer we learned about
  167. * @param reply response
  168. */
  169. void
  170. test_find_peer_processor (void *cls, const struct GNUNET_HELLO_Message *hello)
  171. {
  172. struct RetryContext *retry_ctx = cls;
  173. struct GNUNET_PeerIdentity peer;
  174. if (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer))
  175. {
  176. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  177. "test_find_peer_processor called (peer `%s'), stopping find peer request!\n",
  178. GNUNET_i2s (&peer));
  179. if (retry_ctx->retry_task != GNUNET_SCHEDULER_NO_TASK)
  180. {
  181. GNUNET_SCHEDULER_cancel (retry_ctx->retry_task);
  182. retry_ctx->retry_task = GNUNET_SCHEDULER_NO_TASK;
  183. }
  184. GNUNET_SCHEDULER_add_continuation (&test_find_peer_stop, &p1,
  185. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  186. }
  187. else
  188. {
  189. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  190. "received find peer request, but hello_get_id failed!\n");
  191. }
  192. }
  193. /**
  194. * Retry the find_peer task on timeout. (Forward declaration)
  195. *
  196. * @param cls closure
  197. * @param tc context information (why was this task triggered now?)
  198. */
  199. void
  200. retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  201. /**
  202. * Retry the find_peer task on timeout.
  203. *
  204. * @param cls closure
  205. * @param tc context information (why was this task triggered now)
  206. */
  207. void
  208. retry_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  209. {
  210. struct RetryContext *retry_ctx = cls;
  211. GNUNET_HashCode hash;
  212. memset (&hash, 42, sizeof (GNUNET_HashCode));
  213. if (GNUNET_TIME_absolute_get_remaining (retry_ctx->real_timeout).rel_value >
  214. 0)
  215. {
  216. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  217. "test_find_peer timed out, retrying!\n");
  218. retry_ctx->next_timeout =
  219. GNUNET_TIME_relative_multiply (retry_ctx->next_timeout, 2);
  220. retry_ctx->peer_ctx->find_peer_handle =
  221. GNUNET_DHT_find_peer_start (retry_ctx->peer_ctx->dht_handle,
  222. retry_ctx->next_timeout, &hash,
  223. GNUNET_DHT_RO_NONE,
  224. &test_find_peer_processor, retry_ctx);
  225. }
  226. else
  227. {
  228. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  229. "test_find_peer timed out for good, failing!\n");
  230. retry_ctx->peer_ctx->find_peer_handle = NULL;
  231. }
  232. if (retry_ctx->peer_ctx->find_peer_handle == NULL)
  233. {
  234. GNUNET_break (0);
  235. GNUNET_SCHEDULER_cancel (die_task);
  236. GNUNET_SCHEDULER_add_now (&end_badly, &p1);
  237. return;
  238. }
  239. retry_ctx->retry_task =
  240. GNUNET_SCHEDULER_add_delayed (retry_ctx->next_timeout,
  241. &retry_find_peer_stop, retry_ctx);
  242. }
  243. /**
  244. * Retry the find_peer task on timeout.
  245. *
  246. * @param cls closure
  247. * @param tc context information (why was this task triggered now?)
  248. */
  249. void
  250. retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  251. {
  252. struct RetryContext *retry_ctx = cls;
  253. GNUNET_HashCode hash;
  254. memset (&hash, 42, sizeof (GNUNET_HashCode));
  255. if (retry_ctx->peer_ctx->find_peer_handle != NULL)
  256. {
  257. GNUNET_DHT_find_peer_stop (retry_ctx->peer_ctx->find_peer_handle);
  258. retry_ctx->peer_ctx->find_peer_handle = NULL;
  259. }
  260. GNUNET_SCHEDULER_add_now (&retry_find_peer, retry_ctx);
  261. }
  262. /**
  263. * Entry point for test of find_peer functionality.
  264. *
  265. * @param cls closure
  266. * @param tc context information (why was this task triggered now)
  267. */
  268. void
  269. test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  270. {
  271. struct PeerContext *peer = cls;
  272. GNUNET_HashCode hash;
  273. memset (&hash, 42, sizeof (GNUNET_HashCode));
  274. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
  275. GNUNET_assert (peer->dht_handle != NULL);
  276. retry_context.real_timeout = GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT);
  277. retry_context.next_timeout = BASE_TIMEOUT;
  278. retry_context.peer_ctx = peer;
  279. peer->find_peer_handle =
  280. GNUNET_DHT_find_peer_start (peer->dht_handle, retry_context.next_timeout,
  281. &hash, GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
  282. &test_find_peer_processor, &retry_context);
  283. if (peer->find_peer_handle == NULL)
  284. {
  285. GNUNET_break (0);
  286. GNUNET_SCHEDULER_cancel (die_task);
  287. GNUNET_SCHEDULER_add_now (&end_badly, &p1);
  288. return;
  289. }
  290. retry_context.retry_task =
  291. GNUNET_SCHEDULER_add_delayed (retry_context.next_timeout,
  292. &retry_find_peer_stop, &retry_context);
  293. }
  294. /**
  295. * Signature of the main function of a task.
  296. *
  297. * @param cls closure
  298. * @param tc context information (why was this task triggered now)
  299. */
  300. void
  301. test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  302. {
  303. struct PeerContext *peer = cls;
  304. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
  305. if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
  306. {
  307. GNUNET_break (0);
  308. GNUNET_SCHEDULER_cancel (die_task);
  309. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  310. return;
  311. }
  312. GNUNET_assert (peer->dht_handle != NULL);
  313. GNUNET_DHT_get_stop (peer->get_handle);
  314. peer->get_handle = NULL;
  315. GNUNET_SCHEDULER_add_now (&test_find_peer, &p1);
  316. }
  317. void
  318. test_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
  319. const GNUNET_HashCode * key,
  320. const struct GNUNET_PeerIdentity *const *get_path,
  321. const struct GNUNET_PeerIdentity *const *put_path,
  322. enum GNUNET_BLOCK_Type type, size_t size, const void *data)
  323. {
  324. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  325. "test_get_iterator called (we got a result), stopping get request!\n");
  326. GNUNET_SCHEDULER_add_continuation (&test_get_stop, &p1,
  327. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  328. }
  329. /**
  330. * Signature of the main function of a task.
  331. *
  332. * @param cls closure
  333. * @param tc context information (why was this task triggered now)
  334. */
  335. void
  336. test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  337. {
  338. struct PeerContext *peer = cls;
  339. GNUNET_HashCode hash;
  340. memset (&hash, 42, sizeof (GNUNET_HashCode));
  341. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
  342. GNUNET_assert (peer->dht_handle != NULL);
  343. retry_context.real_timeout = GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT);
  344. retry_context.next_timeout = BASE_TIMEOUT;
  345. peer->get_handle =
  346. GNUNET_DHT_get_start (peer->dht_handle, TOTAL_TIMEOUT,
  347. GNUNET_BLOCK_TYPE_TEST, &hash,
  348. DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_NONE, NULL,
  349. 0, NULL, 0, &test_get_iterator, NULL);
  350. if (peer->get_handle == NULL)
  351. {
  352. GNUNET_break (0);
  353. GNUNET_SCHEDULER_cancel (die_task);
  354. GNUNET_SCHEDULER_add_now (&end_badly, &p1);
  355. return;
  356. }
  357. retry_context.peer_ctx = peer;
  358. }
  359. /**
  360. * Signature of the main function of a task.
  361. *
  362. * @param cls closure
  363. * @param tc context information (why was this task triggered now)
  364. */
  365. void
  366. test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  367. {
  368. struct PeerContext *peer = cls;
  369. GNUNET_HashCode hash;
  370. char *data;
  371. size_t data_size = 42;
  372. memset (&hash, 42, sizeof (GNUNET_HashCode));
  373. data = GNUNET_malloc (data_size);
  374. memset (data, 43, data_size);
  375. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
  376. peer->dht_handle = GNUNET_DHT_connect (peer->cfg, 100);
  377. GNUNET_assert (peer->dht_handle != NULL);
  378. GNUNET_DHT_put (peer->dht_handle, &hash, DEFAULT_PUT_REPLICATION,
  379. GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_TEST, data_size, data,
  380. GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
  381. TOTAL_TIMEOUT, &test_get, &p1);
  382. GNUNET_free (data);
  383. }
  384. static void
  385. setup_peer (struct PeerContext *p, const char *cfgname)
  386. {
  387. p->cfg = GNUNET_CONFIGURATION_create ();
  388. #if START_ARM
  389. p->arm_proc =
  390. GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
  391. "gnunet-service-arm",
  392. #if VERBOSE_ARM
  393. "-L", "DEBUG",
  394. #endif
  395. "-c", cfgname, NULL);
  396. #endif
  397. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  398. }
  399. static void
  400. run (void *cls, char *const *args, const char *cfgfile,
  401. const struct GNUNET_CONFIGURATION_Handle *cfg)
  402. {
  403. GNUNET_assert (ok == 1);
  404. OKPP;
  405. die_task =
  406. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  407. (GNUNET_TIME_UNIT_MINUTES, 1), &end_badly,
  408. NULL);
  409. setup_peer (&p1, "test_dht_api_peer1.conf");
  410. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  411. (GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
  412. }
  413. static int
  414. check ()
  415. {
  416. char *const argv[] = { "test-dht-api",
  417. "-c",
  418. "test_dht_api_data.conf",
  419. #if VERBOSE
  420. "-L", "DEBUG",
  421. #endif
  422. NULL
  423. };
  424. struct GNUNET_GETOPT_CommandLineOption options[] = {
  425. GNUNET_GETOPT_OPTION_END
  426. };
  427. ok = 1;
  428. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
  429. "test-dht-api", "nohelp", options, &run, &ok);
  430. stop_arm (&p1);
  431. return ok;
  432. }
  433. int
  434. main (int argc, char *argv[])
  435. {
  436. int ret;
  437. GNUNET_log_setup ("test-dht-api",
  438. #if VERBOSE
  439. "DEBUG",
  440. #else
  441. "WARNING",
  442. #endif
  443. NULL);
  444. ret = check ();
  445. GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
  446. return ret;
  447. }
  448. /* end of test_dht_api.c */