test_transport_blacklisting.c 17 KB

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