test_transport_blacklisting.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 2010, 2011 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/transport_api_blacklisting.c
  19. * @brief test for the blacklisting with blacklistings defined in cfg
  20. *
  21. * this file contains multiple tests:
  22. *
  23. * test_transport_blacklisting_no_bl:
  24. * no blacklisting entries
  25. * peers are expected to connect
  26. * test_transport_blacklisting_outbound_bl_full:
  27. * both peers contain bl entries for full peer
  28. * test is expected to not connect
  29. * test_transport_blacklisting_outbound_bl_plugin:
  30. * both peers contain bl entries for plugin
  31. * test is expected to not connect
  32. * test_transport_blacklisting_inbound_bl_plugin:
  33. * peer 1 contains no bl entries
  34. * peer 2 contain bl entries for full peer
  35. * test is expected to not connect
  36. * test_transport_blacklisting_inbound_bl_full:
  37. * peer 1 contains no bl entries
  38. * peer 2 contain bl entries for plugin
  39. * test is expected to not connect
  40. * test_transport_blacklisting_multiple_plugins:
  41. * both peers contain bl entries for plugin
  42. * test is expected to connect with not bl'ed plugin
  43. *
  44. * @author Matthias Wachs
  45. *
  46. */
  47. #include "platform.h"
  48. #include "gnunet_transport_service.h"
  49. #include "transport-testing.h"
  50. char *test_name;
  51. struct PeerContext *p1;
  52. struct PeerContext *p2;
  53. static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
  54. struct GNUNET_TRANSPORT_TESTING_handle *tth;
  55. /**
  56. * How long until we give up on transmitting the message?
  57. */
  58. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
  59. #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  60. static int stage;
  61. static int ok;
  62. static int connected;
  63. static GNUNET_SCHEDULER_TaskIdentifier die_task;
  64. static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
  65. static GNUNET_SCHEDULER_TaskIdentifier stage_task;
  66. #if VERBOSE
  67. #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
  68. #else
  69. #define OKPP do { ok++; } while (0)
  70. #endif
  71. static void
  72. run_stage(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  73. static void
  74. end(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  75. {
  76. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
  77. if (die_task != GNUNET_SCHEDULER_NO_TASK )
  78. {
  79. GNUNET_SCHEDULER_cancel (die_task);
  80. die_task = GNUNET_SCHEDULER_NO_TASK;
  81. }
  82. if (timeout_task != GNUNET_SCHEDULER_NO_TASK )
  83. {
  84. GNUNET_SCHEDULER_cancel (timeout_task);
  85. timeout_task = GNUNET_SCHEDULER_NO_TASK;
  86. }
  87. if (stage_task != GNUNET_SCHEDULER_NO_TASK )
  88. {
  89. GNUNET_SCHEDULER_cancel (stage_task);
  90. stage_task = GNUNET_SCHEDULER_NO_TASK;
  91. }
  92. if (cc != NULL )
  93. {
  94. GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
  95. cc = NULL;
  96. }
  97. if (p1 != NULL )
  98. {
  99. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
  100. p1 = NULL;
  101. }
  102. if (p2 != NULL )
  103. {
  104. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
  105. p2 = NULL;
  106. }
  107. }
  108. static void
  109. end_badly(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  110. {
  111. die_task = GNUNET_SCHEDULER_NO_TASK;
  112. if (timeout_task != GNUNET_SCHEDULER_NO_TASK )
  113. {
  114. GNUNET_SCHEDULER_cancel (timeout_task);
  115. timeout_task = GNUNET_SCHEDULER_NO_TASK;
  116. }
  117. if (stage_task != GNUNET_SCHEDULER_NO_TASK )
  118. {
  119. GNUNET_SCHEDULER_cancel (stage_task);
  120. stage_task = GNUNET_SCHEDULER_NO_TASK;
  121. }
  122. if (cc != NULL )
  123. {
  124. GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
  125. cc = NULL;
  126. }
  127. if (p1 != NULL )
  128. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
  129. if (p2 != NULL )
  130. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
  131. ok = GNUNET_SYSERR;
  132. }
  133. static void
  134. testing_connect_cb(struct PeerContext *p1, struct PeerContext *p2, void *cls)
  135. {
  136. cc = NULL;
  137. char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
  138. GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
  139. p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
  140. GNUNET_free(p1_c);
  141. connected = GNUNET_YES;
  142. stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
  143. }
  144. static void
  145. connect_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  146. {
  147. GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers not connected, next stage\n");
  148. timeout_task = GNUNET_SCHEDULER_NO_TASK;
  149. stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
  150. }
  151. static int started;
  152. void
  153. start_cb(struct PeerContext *p, void *cls)
  154. {
  155. started++;
  156. GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
  157. GNUNET_i2s_full (&p->id));
  158. if (started != 2)
  159. return;
  160. char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
  161. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  162. "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n", p1->no,
  163. sender_c, p2->no, GNUNET_i2s (&p2->id));
  164. GNUNET_free(sender_c);
  165. cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
  166. NULL );
  167. }
  168. static int check_blacklist_config (char *cfg_file,
  169. struct GNUNET_PeerIdentity *peer, struct GNUNET_PeerIdentity *bl_peer)
  170. {
  171. struct GNUNET_CONFIGURATION_Handle *cfg;
  172. char *section;
  173. char *peer_str;
  174. cfg = GNUNET_CONFIGURATION_create ();
  175. if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
  176. {
  177. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n", cfg_file);
  178. GNUNET_CONFIGURATION_destroy (cfg);
  179. return GNUNET_SYSERR;
  180. }
  181. peer_str = GNUNET_strdup (GNUNET_i2s_full(peer));
  182. GNUNET_asprintf (&section, "transport-blacklist-%s", peer_str);
  183. if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section, GNUNET_i2s_full(bl_peer)))
  184. {
  185. GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
  186. "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
  187. cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
  188. GNUNET_CONFIGURATION_destroy (cfg);
  189. GNUNET_free (section);
  190. GNUNET_free (peer_str);
  191. return GNUNET_SYSERR;
  192. }
  193. GNUNET_log(GNUNET_ERROR_TYPE_INFO,
  194. "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
  195. cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
  196. GNUNET_CONFIGURATION_destroy (cfg);
  197. GNUNET_free (section);
  198. GNUNET_free (peer_str);
  199. return GNUNET_OK;
  200. }
  201. static void
  202. run_stage(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  203. {
  204. stage_task = GNUNET_SCHEDULER_NO_TASK;
  205. if (GNUNET_SCHEDULER_NO_TASK != die_task)
  206. GNUNET_SCHEDULER_cancel (die_task);
  207. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
  208. GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
  209. if (0 == stage)
  210. {
  211. started = GNUNET_NO;
  212. connected = GNUNET_NO;
  213. if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
  214. {
  215. /* Try to connect peers successfully */
  216. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  217. "test_transport_blacklisting_cfg_peer1.conf", 1, NULL, NULL, NULL,
  218. &start_cb, NULL );
  219. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  220. "test_transport_blacklisting_cfg_peer2.conf", 2, NULL, NULL, NULL,
  221. &start_cb, NULL );
  222. }
  223. else if (0
  224. == strcmp (test_name, "test_transport_blacklisting_outbound_bl_full"))
  225. {
  226. char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_full.conf";
  227. char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
  228. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  229. cfg_p1 , 1, NULL, NULL, NULL, &start_cb, NULL );
  230. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  231. cfg_p2, 2, NULL, NULL, NULL,
  232. &start_cb, NULL );
  233. /* check if configuration contain correct blacklist entries */
  234. if ((GNUNET_SYSERR == check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
  235. (GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
  236. {
  237. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
  238. p1 = NULL;
  239. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
  240. p2 = NULL;
  241. ok = 1;
  242. GNUNET_SCHEDULER_add_now (&end, NULL );
  243. }
  244. }
  245. else if (0
  246. == strcmp (test_name, "test_transport_blacklisting_outbound_bl_plugin"))
  247. {
  248. char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
  249. char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
  250. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  251. cfg_p1, 1, NULL,
  252. NULL, NULL, &start_cb, NULL );
  253. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  254. cfg_p2, 2, NULL, NULL, NULL,
  255. &start_cb, NULL );
  256. /* check if configuration contain correct blacklist entries */
  257. if ((GNUNET_SYSERR == check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
  258. (GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
  259. {
  260. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
  261. p1 = NULL;
  262. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
  263. p2 = NULL;
  264. ok = 1;
  265. GNUNET_SCHEDULER_add_now (&end, NULL );
  266. }
  267. }
  268. else if (0
  269. == strcmp (test_name, "test_transport_blacklisting_inbound_bl_full"))
  270. {
  271. char * cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
  272. char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
  273. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  274. cfg_p1, 1, NULL, NULL, NULL,
  275. &start_cb, NULL );
  276. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  277. cfg_p2, 2, NULL, NULL,
  278. NULL, &start_cb, NULL );
  279. /* check if configuration contain correct blacklist entries */
  280. if ((GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
  281. {
  282. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
  283. p1 = NULL;
  284. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
  285. p2 = NULL;
  286. ok = 1;
  287. GNUNET_SCHEDULER_add_now (&end, NULL );
  288. }
  289. }
  290. else if (0
  291. == strcmp (test_name, "test_transport_blacklisting_inbound_bl_plugin"))
  292. {
  293. char * cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
  294. char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
  295. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  296. cfg_p1, 1, NULL, NULL, NULL,
  297. &start_cb, NULL );
  298. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  299. cfg_p2, 2, NULL, NULL,
  300. NULL, &start_cb, NULL );
  301. /* check if configuration contain correct blacklist entries */
  302. if ((GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
  303. {
  304. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
  305. p1 = NULL;
  306. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
  307. p2 = NULL;
  308. ok = 1;
  309. GNUNET_SCHEDULER_add_now (&end, NULL );
  310. }
  311. }
  312. else if (0
  313. == strcmp (test_name, "test_transport_blacklisting_multiple_plugins"))
  314. {
  315. char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
  316. char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
  317. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  318. cfg_p1, 1,
  319. NULL, NULL, NULL, &start_cb, NULL );
  320. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  321. cfg_p2, 2,
  322. NULL, NULL, NULL, &start_cb, NULL );
  323. /* check if configuration contain correct blacklist entries */
  324. if ((GNUNET_SYSERR == check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
  325. (GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
  326. {
  327. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
  328. p1 = NULL;
  329. GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
  330. p2 = NULL;
  331. ok = 1;
  332. GNUNET_SCHEDULER_add_now (&end, NULL );
  333. }
  334. }
  335. else
  336. {
  337. GNUNET_break (0);
  338. GNUNET_SCHEDULER_add_now (&end, NULL );
  339. }
  340. if ((NULL == p1) || (NULL == p2))
  341. {
  342. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
  343. ok = 1;
  344. GNUNET_SCHEDULER_add_now (&end, NULL );
  345. }
  346. timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
  347. &connect_timeout, NULL );
  348. stage++;
  349. return;
  350. }
  351. if (cc != NULL )
  352. {
  353. GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
  354. cc = NULL;
  355. }
  356. if (p1 != NULL )
  357. {
  358. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
  359. p1 = NULL;
  360. }
  361. if (p2 != NULL )
  362. {
  363. GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
  364. p2 = NULL;
  365. }
  366. GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
  367. stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
  368. (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
  369. if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
  370. || (0 == strcmp (test_name, "test_transport_blacklisting_multiple_plugins")))
  371. {
  372. if ((GNUNET_NO != started) && (GNUNET_YES == connected))
  373. ok = 0;
  374. else
  375. {
  376. GNUNET_break(0);
  377. ok = 1;
  378. }
  379. }
  380. else
  381. {
  382. if ((GNUNET_NO != started) && (GNUNET_YES != connected))
  383. ok = 0;
  384. else
  385. {
  386. ok = 1;
  387. }
  388. }
  389. GNUNET_SCHEDULER_add_now (&end, NULL );
  390. }
  391. static void
  392. run(void *cls, char * const *args, const char *cfgfile,
  393. const struct GNUNET_CONFIGURATION_Handle *cfg)
  394. {
  395. connected = GNUNET_NO;
  396. stage = 0;
  397. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
  398. stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
  399. }
  400. int
  401. main(int argc, char *argv0[])
  402. {
  403. ok = 1;
  404. GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0], &test_name);
  405. GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL );
  406. static char * const argv[] =
  407. { "date", "-c", "test_transport_api_data.conf", NULL };
  408. static struct GNUNET_GETOPT_CommandLineOption options[] =
  409. { GNUNET_GETOPT_OPTION_END };
  410. tth = GNUNET_TRANSPORT_TESTING_init ();
  411. GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
  412. "test-transport-api-blacklisting", "nohelp", options, &run, NULL );
  413. GNUNET_TRANSPORT_TESTING_done (tth);
  414. return ok;
  415. }
  416. /* end of transport_api_blacklisting.c */