test_transport_api_monitor_peers.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 2010 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 transport/test_transport_api_monitor_peers.c
  19. * @brief base test case for transport peer monitor API
  20. */
  21. #include "platform.h"
  22. #include "gnunet_transport_service.h"
  23. #include "transport-testing.h"
  24. /**
  25. * How long until we give up on transmitting the message?
  26. */
  27. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
  28. /**
  29. * How long until we give up on transmitting the message?
  30. */
  31. #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  32. #define TEST_MESSAGE_SIZE 2600
  33. #define TEST_MESSAGE_TYPE 12345
  34. static char *test_source;
  35. static char *test_plugin;
  36. static char *test_name;
  37. static int ok;
  38. static int s_started;
  39. static int s_connected;
  40. static int s_sending;
  41. static GNUNET_SCHEDULER_TaskIdentifier die_task;
  42. static GNUNET_SCHEDULER_TaskIdentifier send_task;
  43. static struct PeerContext *p1;
  44. static struct PeerContext *p2;
  45. static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
  46. static struct GNUNET_TRANSPORT_TransmitHandle *th;
  47. static struct GNUNET_TRANSPORT_TESTING_handle *tth;
  48. static char *cfg_file_p1;
  49. static char *cfg_file_p2;
  50. static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p1;
  51. static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p2;
  52. static int p1_c = GNUNET_NO;
  53. static int p2_c = GNUNET_NO;
  54. static int p1_c_notify = GNUNET_NO;
  55. static int p2_c_notify = GNUNET_NO;
  56. static void
  57. end ()
  58. {
  59. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  60. "Stopping peers\n");
  61. if (send_task != GNUNET_SCHEDULER_NO_TASK)
  62. GNUNET_SCHEDULER_cancel (send_task);
  63. if (die_task != GNUNET_SCHEDULER_NO_TASK)
  64. {
  65. GNUNET_SCHEDULER_cancel (die_task);
  66. die_task = GNUNET_SCHEDULER_NO_TASK;
  67. }
  68. if (th != NULL)
  69. GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
  70. th = NULL;
  71. if (NULL != p1)
  72. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
  73. p1 = NULL;
  74. if (NULL != p2)
  75. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
  76. p2 = NULL;
  77. if (NULL != pmc_p1)
  78. {
  79. GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
  80. pmc_p1 = NULL;
  81. }
  82. if (NULL != pmc_p2)
  83. {
  84. GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
  85. pmc_p2 = NULL;
  86. }
  87. ok = 0;
  88. }
  89. static void
  90. end_badly (void *cls,
  91. const struct GNUNET_SCHEDULER_TaskContext *tc)
  92. {
  93. die_task = GNUNET_SCHEDULER_NO_TASK;
  94. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
  95. if (send_task != GNUNET_SCHEDULER_NO_TASK)
  96. GNUNET_SCHEDULER_cancel (send_task);
  97. if (cc != NULL)
  98. {
  99. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
  100. GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
  101. cc = NULL;
  102. }
  103. if (th != NULL)
  104. GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
  105. else
  106. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
  107. if (s_started == GNUNET_NO)
  108. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
  109. else
  110. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
  111. if (s_connected == GNUNET_NO)
  112. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
  113. else
  114. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
  115. if (s_sending == GNUNET_NO)
  116. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
  117. else
  118. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
  119. th = NULL;
  120. if (NULL != pmc_p1)
  121. {
  122. GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
  123. pmc_p1 = NULL;
  124. }
  125. if (NULL != pmc_p2)
  126. {
  127. GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
  128. pmc_p2 = NULL;
  129. }
  130. if (p1 != NULL)
  131. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
  132. else
  133. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
  134. if (p2 != NULL)
  135. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
  136. else
  137. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
  138. ok = GNUNET_SYSERR;
  139. }
  140. static void
  141. notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
  142. const struct GNUNET_MessageHeader *message)
  143. {
  144. struct PeerContext *p = cls;
  145. struct PeerContext *t = NULL;
  146. if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
  147. t = p1;
  148. if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
  149. t = p2;
  150. GNUNET_assert (t != NULL);
  151. char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
  152. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  153. "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
  154. p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
  155. GNUNET_i2s (&t->id));
  156. GNUNET_free (ps);
  157. }
  158. static size_t
  159. notify_ready (void *cls, size_t size, void *buf)
  160. {
  161. struct PeerContext *p = cls;
  162. struct GNUNET_MessageHeader *hdr;
  163. th = NULL;
  164. if (buf == NULL)
  165. {
  166. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  167. "Timeout occurred while waiting for transmit_ready\n");
  168. if (GNUNET_SCHEDULER_NO_TASK != die_task)
  169. GNUNET_SCHEDULER_cancel (die_task);
  170. die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  171. ok = 42;
  172. return 0;
  173. }
  174. GNUNET_assert (size >= TEST_MESSAGE_SIZE);
  175. if (buf != NULL)
  176. {
  177. memset (buf, '\0', TEST_MESSAGE_SIZE);
  178. hdr = buf;
  179. hdr->size = htons (TEST_MESSAGE_SIZE);
  180. hdr->type = htons (TEST_MESSAGE_TYPE);
  181. }
  182. char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
  183. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  184. "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
  185. p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
  186. GNUNET_i2s (&p->id));
  187. GNUNET_free (ps);
  188. return TEST_MESSAGE_SIZE;
  189. }
  190. static void
  191. sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  192. {
  193. send_task = GNUNET_SCHEDULER_NO_TASK;
  194. if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
  195. return;
  196. char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
  197. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  198. "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
  199. p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
  200. GNUNET_free (receiver_s);
  201. s_sending = GNUNET_YES;
  202. th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
  203. TIMEOUT_TRANSMIT, &notify_ready,
  204. p1);
  205. }
  206. static void
  207. done ()
  208. {
  209. if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c) && p1_c_notify && p2_c_notify)
  210. {
  211. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Both peers state to be connected\n");
  212. ok = 0;
  213. end();
  214. }
  215. }
  216. static void
  217. notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
  218. {
  219. static int c;
  220. c++;
  221. struct PeerContext *p = cls;
  222. struct PeerContext *t = NULL;
  223. if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
  224. {
  225. p1_c_notify = GNUNET_YES;
  226. t = p1;
  227. }
  228. if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
  229. {
  230. p2_c_notify = GNUNET_YES;
  231. t = p2;
  232. }
  233. GNUNET_assert (t != NULL);
  234. char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
  235. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  236. "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
  237. t->no, GNUNET_i2s (peer));
  238. if (p1_c_notify && p2_c_notify)
  239. GNUNET_SCHEDULER_add_now(&done, NULL);
  240. GNUNET_free (ps);
  241. }
  242. static void
  243. notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
  244. {
  245. struct PeerContext *p = cls;
  246. char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
  247. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  248. "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
  249. GNUNET_i2s (peer));
  250. GNUNET_free (ps);
  251. if (th != NULL)
  252. GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
  253. th = NULL;
  254. }
  255. static void
  256. testing_connect_cb (struct PeerContext *p1,
  257. struct PeerContext *p2,
  258. void *cls)
  259. {
  260. cc = NULL;
  261. char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
  262. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  263. "Peers connected: %u (%s) <-> %u (%s)\n",
  264. p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
  265. GNUNET_free (p1_c);
  266. s_connected = GNUNET_YES;
  267. send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
  268. }
  269. static void
  270. start_cb (struct PeerContext *p, void *cls)
  271. {
  272. static int started;
  273. started++;
  274. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  275. "Peer %u (`%s') started\n", p->no,
  276. GNUNET_i2s (&p->id));
  277. if (started != 2)
  278. return;
  279. else
  280. s_started = GNUNET_YES;
  281. char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
  282. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  283. "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
  284. p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
  285. GNUNET_free (sender_c);
  286. cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
  287. NULL);
  288. }
  289. static void
  290. monitor1_cb (void *cls,
  291. const struct GNUNET_PeerIdentity *peer,
  292. const struct GNUNET_HELLO_Address *address,
  293. enum GNUNET_TRANSPORT_PeerState state,
  294. struct GNUNET_TIME_Absolute state_timeout)
  295. {
  296. if ((NULL == peer) || (NULL == p1))
  297. return;
  298. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  299. "Monitor 1: %s %s %s\n",
  300. GNUNET_i2s (peer),
  301. GNUNET_TRANSPORT_ps2s (state),
  302. GNUNET_STRINGS_absolute_time_to_string(state_timeout));
  303. if ((0 == memcmp (peer, &p2->id, sizeof (p2->id)) &&
  304. (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
  305. GNUNET_NO == p1_c) )
  306. {
  307. p1_c = GNUNET_YES;
  308. GNUNET_SCHEDULER_add_now (&done, NULL);
  309. }
  310. }
  311. static void
  312. monitor2_cb (void *cls,
  313. const struct GNUNET_PeerIdentity *peer,
  314. const struct GNUNET_HELLO_Address *address,
  315. enum GNUNET_TRANSPORT_PeerState state,
  316. struct GNUNET_TIME_Absolute state_timeout)
  317. {
  318. if ((NULL == peer) || (NULL == p2))
  319. return;
  320. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  321. "Monitor 2: %s %s %s\n",
  322. GNUNET_i2s (peer),
  323. GNUNET_TRANSPORT_ps2s (state),
  324. GNUNET_STRINGS_absolute_time_to_string(state_timeout));
  325. if ((0 == memcmp (peer, &p1->id, sizeof (p1->id)) &&
  326. (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
  327. GNUNET_NO == p2_c) )
  328. {
  329. p2_c = GNUNET_YES;
  330. GNUNET_SCHEDULER_add_now (&done, NULL);
  331. }
  332. }
  333. static void
  334. run (void *cls, char *const *args, const char *cfgfile,
  335. const struct GNUNET_CONFIGURATION_Handle *cfg)
  336. {
  337. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  338. s_started = GNUNET_NO;
  339. s_connected = GNUNET_NO;
  340. s_sending = GNUNET_NO;
  341. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
  342. &notify_receive, &notify_connect,
  343. &notify_disconnect, &start_cb,
  344. NULL);
  345. pmc_p1 = GNUNET_TRANSPORT_monitor_peers (p1->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor1_cb, NULL);
  346. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
  347. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
  348. &notify_receive, &notify_connect,
  349. &notify_disconnect, &start_cb,
  350. NULL);
  351. pmc_p2 = GNUNET_TRANSPORT_monitor_peers (p2->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor2_cb, NULL);
  352. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
  353. if ((p1 == NULL) || (p2 == NULL))
  354. {
  355. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
  356. if (die_task != GNUNET_SCHEDULER_NO_TASK)
  357. GNUNET_SCHEDULER_cancel (die_task);
  358. die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  359. return;
  360. }
  361. }
  362. static int
  363. check ()
  364. {
  365. static char *const argv[] = { "test-transport-api",
  366. "-c",
  367. "test_transport_api_data.conf",
  368. NULL
  369. };
  370. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  371. GNUNET_GETOPT_OPTION_END
  372. };
  373. send_task = GNUNET_SCHEDULER_NO_TASK;
  374. ok = 1;
  375. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
  376. "nohelp", options, &run, &ok);
  377. return ok;
  378. }
  379. int
  380. main (int argc, char *argv[])
  381. {
  382. int ret;
  383. ok = 1;
  384. GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
  385. GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
  386. GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
  387. &test_plugin);
  388. GNUNET_log_setup (test_name,
  389. "WARNING",
  390. NULL);
  391. tth = GNUNET_TRANSPORT_TESTING_init ();
  392. GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
  393. GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
  394. ret = check ();
  395. GNUNET_free (cfg_file_p1);
  396. GNUNET_free (cfg_file_p2);
  397. GNUNET_free (test_source);
  398. GNUNET_free (test_plugin);
  399. GNUNET_free (test_name);
  400. GNUNET_TRANSPORT_TESTING_done (tth);
  401. if (0 != ret)
  402. return ret;
  403. else
  404. return ok;
  405. }
  406. /* end of test_transport_api_monitor_peers.c */