test_transport_blacklisting.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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, \
  59. 10)
  60. static int stage;
  61. static int ok;
  62. static int connected;
  63. static struct GNUNET_SCHEDULER_Task *die_task;
  64. static struct GNUNET_SCHEDULER_Task *timeout_task;
  65. static struct GNUNET_SCHEDULER_Task *stage_task;
  66. #if VERBOSE
  67. #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, \
  68. __FILE__, __LINE__); } while (0)
  69. #else
  70. #define OKPP do { ok++; } while (0)
  71. #endif
  72. static void
  73. run_stage (void *cls);
  74. static void
  75. end (void *cls)
  76. {
  77. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
  78. if (die_task != NULL)
  79. {
  80. GNUNET_SCHEDULER_cancel (die_task);
  81. die_task = NULL;
  82. }
  83. if (timeout_task != NULL)
  84. {
  85. GNUNET_SCHEDULER_cancel (timeout_task);
  86. timeout_task = NULL;
  87. }
  88. if (stage_task != NULL)
  89. {
  90. GNUNET_SCHEDULER_cancel (stage_task);
  91. stage_task = NULL;
  92. }
  93. if (cc != NULL)
  94. {
  95. GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
  96. cc = NULL;
  97. }
  98. if (p1 != NULL)
  99. {
  100. GNUNET_TRANSPORT_TESTING_stop_peer (p1);
  101. p1 = NULL;
  102. }
  103. if (p2 != NULL)
  104. {
  105. GNUNET_TRANSPORT_TESTING_stop_peer (p2);
  106. p2 = NULL;
  107. }
  108. }
  109. static void
  110. end_badly (void *cls)
  111. {
  112. die_task = NULL;
  113. if (timeout_task != NULL)
  114. {
  115. GNUNET_SCHEDULER_cancel (timeout_task);
  116. timeout_task = NULL;
  117. }
  118. if (stage_task != NULL)
  119. {
  120. GNUNET_SCHEDULER_cancel (stage_task);
  121. stage_task = NULL;
  122. }
  123. if (cc != NULL)
  124. {
  125. GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
  126. cc = NULL;
  127. }
  128. if (p1 != NULL)
  129. GNUNET_TRANSPORT_TESTING_stop_peer (p1);
  130. if (p2 != NULL)
  131. GNUNET_TRANSPORT_TESTING_stop_peer (p2);
  132. ok = GNUNET_SYSERR;
  133. }
  134. static void
  135. testing_connect_cb (void *cls)
  136. {
  137. cc = NULL;
  138. char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
  139. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
  140. p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
  141. GNUNET_free (p1_c);
  142. connected = GNUNET_YES;
  143. stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
  144. }
  145. static void
  146. connect_timeout (void *cls)
  147. {
  148. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  149. "Peers not connected, next stage\n");
  150. timeout_task = NULL;
  151. stage_task = GNUNET_SCHEDULER_add_now (&run_stage,
  152. NULL);
  153. }
  154. static int started;
  155. static void
  156. start_cb (void *cls)
  157. {
  158. struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
  159. started++;
  160. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  161. "Peer %u (`%s') started\n",
  162. p->no,
  163. GNUNET_i2s_full (&p->id));
  164. if (started != 2)
  165. return;
  166. char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
  167. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  168. "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
  169. p1->no,
  170. sender_c,
  171. p2->no,
  172. GNUNET_i2s (&p2->id));
  173. GNUNET_free (sender_c);
  174. cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
  175. p2,
  176. &testing_connect_cb,
  177. NULL);
  178. }
  179. static int
  180. check_blacklist_config (const char *cfg_file,
  181. struct GNUNET_PeerIdentity *peer,
  182. struct GNUNET_PeerIdentity *bl_peer)
  183. {
  184. struct GNUNET_CONFIGURATION_Handle *cfg;
  185. char *section;
  186. char *peer_str;
  187. cfg = GNUNET_CONFIGURATION_create ();
  188. if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
  189. {
  190. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n",
  191. cfg_file);
  192. GNUNET_CONFIGURATION_destroy (cfg);
  193. return GNUNET_SYSERR;
  194. }
  195. peer_str = GNUNET_strdup (GNUNET_i2s_full (peer));
  196. GNUNET_asprintf (&section, "transport-blacklist-%s", peer_str);
  197. if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section,
  198. GNUNET_i2s_full (bl_peer)))
  199. {
  200. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  201. "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
  202. cfg_file, peer_str, GNUNET_i2s_full (bl_peer));
  203. GNUNET_CONFIGURATION_destroy (cfg);
  204. GNUNET_free (section);
  205. GNUNET_free (peer_str);
  206. return GNUNET_SYSERR;
  207. }
  208. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  209. "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
  210. cfg_file, peer_str, GNUNET_i2s_full (bl_peer));
  211. GNUNET_CONFIGURATION_destroy (cfg);
  212. GNUNET_free (section);
  213. GNUNET_free (peer_str);
  214. return GNUNET_OK;
  215. }
  216. static void
  217. run_stage (void *cls)
  218. {
  219. stage_task = NULL;
  220. if (NULL != die_task)
  221. GNUNET_SCHEDULER_cancel (die_task);
  222. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  223. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
  224. if (0 == stage)
  225. {
  226. started = GNUNET_NO;
  227. connected = GNUNET_NO;
  228. if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
  229. {
  230. /* Try to connect peers successfully */
  231. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  232. "test_transport_blacklisting_cfg_peer1.conf",
  233. 1,
  234. NULL,
  235. NULL,
  236. NULL,
  237. NULL,
  238. &start_cb,
  239. NULL);
  240. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  241. "test_transport_blacklisting_cfg_peer2.conf",
  242. 2,
  243. NULL,
  244. NULL,
  245. NULL,
  246. NULL,
  247. &start_cb,
  248. NULL);
  249. }
  250. else if (0 == strcmp (test_name,
  251. "test_transport_blacklisting_outbound_bl_full"))
  252. {
  253. const char *cfg_p1 =
  254. "test_transport_blacklisting_cfg_blp_peer1_full.conf";
  255. const char *cfg_p2 =
  256. "test_transport_blacklisting_cfg_blp_peer2_full.conf";
  257. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  258. cfg_p1,
  259. 1, NULL, NULL, NULL,
  260. NULL,
  261. &start_cb, NULL);
  262. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  263. cfg_p2, 2,
  264. NULL, NULL, NULL,
  265. NULL,
  266. &start_cb, NULL);
  267. /* check if configuration contain correct blacklist entries */
  268. if ((GNUNET_SYSERR ==
  269. check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
  270. (GNUNET_SYSERR ==
  271. check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
  272. {
  273. GNUNET_TRANSPORT_TESTING_stop_peer (p1);
  274. p1 = NULL;
  275. GNUNET_TRANSPORT_TESTING_stop_peer (p2);
  276. p2 = NULL;
  277. ok = 1;
  278. GNUNET_SCHEDULER_add_now (&end, NULL);
  279. }
  280. }
  281. else if (0
  282. == strcmp (test_name,
  283. "test_transport_blacklisting_outbound_bl_plugin"))
  284. {
  285. const char *cfg_p1 =
  286. "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
  287. const char *cfg_p2 =
  288. "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
  289. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  290. cfg_p1,
  291. 1,
  292. NULL,
  293. NULL,
  294. NULL,
  295. NULL,
  296. &start_cb,
  297. NULL);
  298. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  299. cfg_p2, 2,
  300. NULL,
  301. NULL,
  302. NULL,
  303. NULL,
  304. &start_cb,
  305. NULL);
  306. /* check if configuration contain correct blacklist entries */
  307. if ((GNUNET_SYSERR ==
  308. check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
  309. (GNUNET_SYSERR ==
  310. check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
  311. {
  312. GNUNET_TRANSPORT_TESTING_stop_peer (p1);
  313. p1 = NULL;
  314. GNUNET_TRANSPORT_TESTING_stop_peer (p2);
  315. p2 = NULL;
  316. ok = 1;
  317. GNUNET_SCHEDULER_add_now (&end, NULL);
  318. }
  319. }
  320. else if (0 == strcmp (test_name,
  321. "test_transport_blacklisting_inbound_bl_full"))
  322. {
  323. const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
  324. const char *cfg_p2 =
  325. "test_transport_blacklisting_cfg_blp_peer2_full.conf";
  326. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  327. cfg_p1, 1,
  328. NULL,
  329. NULL, NULL, NULL,
  330. &start_cb, NULL);
  331. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  332. cfg_p2, 2,
  333. NULL,
  334. NULL, NULL, NULL,
  335. &start_cb, NULL);
  336. /* check if configuration contain correct blacklist entries */
  337. if ((GNUNET_SYSERR ==
  338. check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
  339. {
  340. GNUNET_TRANSPORT_TESTING_stop_peer (p1);
  341. p1 = NULL;
  342. GNUNET_TRANSPORT_TESTING_stop_peer (p2);
  343. p2 = NULL;
  344. ok = 1;
  345. GNUNET_SCHEDULER_add_now (&end, NULL);
  346. }
  347. }
  348. else if (0 == strcmp (test_name,
  349. "test_transport_blacklisting_inbound_bl_plugin"))
  350. {
  351. const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
  352. const char *cfg_p2 =
  353. "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
  354. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  355. cfg_p1, 1,
  356. NULL,
  357. NULL, NULL, NULL,
  358. &start_cb, NULL);
  359. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  360. cfg_p2, 2,
  361. NULL,
  362. NULL, NULL,
  363. NULL,
  364. &start_cb, NULL);
  365. /* check if configuration contain correct blacklist entries */
  366. if ((GNUNET_SYSERR ==
  367. check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
  368. {
  369. GNUNET_TRANSPORT_TESTING_stop_peer (p1);
  370. p1 = NULL;
  371. GNUNET_TRANSPORT_TESTING_stop_peer (p2);
  372. p2 = NULL;
  373. ok = 1;
  374. GNUNET_SCHEDULER_add_now (&end, NULL);
  375. }
  376. }
  377. else if (0 == strcmp (test_name,
  378. "test_transport_blacklisting_multiple_plugins"))
  379. {
  380. const char *cfg_p1 =
  381. "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
  382. const char *cfg_p2 =
  383. "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
  384. p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  385. cfg_p1, 1,
  386. NULL,
  387. NULL, NULL, NULL,
  388. &start_cb, NULL);
  389. p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  390. cfg_p2, 2,
  391. NULL,
  392. NULL, NULL, NULL,
  393. &start_cb, NULL);
  394. /* check if configuration contain correct blacklist entries */
  395. if ((GNUNET_SYSERR ==
  396. check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
  397. (GNUNET_SYSERR ==
  398. check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
  399. {
  400. GNUNET_TRANSPORT_TESTING_stop_peer (p1);
  401. p1 = NULL;
  402. GNUNET_TRANSPORT_TESTING_stop_peer (p2);
  403. p2 = NULL;
  404. ok = 1;
  405. GNUNET_SCHEDULER_add_now (&end, NULL);
  406. }
  407. }
  408. else
  409. {
  410. GNUNET_break (0);
  411. GNUNET_SCHEDULER_add_now (&end, NULL);
  412. }
  413. if ((NULL == p1) || (NULL == p2))
  414. {
  415. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
  416. ok = 1;
  417. GNUNET_SCHEDULER_add_now (&end, NULL);
  418. }
  419. timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
  420. &connect_timeout,
  421. NULL);
  422. stage++;
  423. return;
  424. }
  425. if (cc != NULL)
  426. {
  427. GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
  428. cc = NULL;
  429. }
  430. if (p1 != NULL)
  431. {
  432. GNUNET_TRANSPORT_TESTING_stop_peer (p1);
  433. p1 = NULL;
  434. }
  435. if (p2 != NULL)
  436. {
  437. GNUNET_TRANSPORT_TESTING_stop_peer (p2);
  438. p2 = NULL;
  439. }
  440. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
  441. stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
  442. (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
  443. if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
  444. || (0 == strcmp (test_name,
  445. "test_transport_blacklisting_multiple_plugins")))
  446. {
  447. if ((GNUNET_NO != started) && (GNUNET_YES == connected))
  448. ok = 0;
  449. else
  450. {
  451. GNUNET_break (0);
  452. ok = 1;
  453. }
  454. }
  455. else
  456. {
  457. if ((GNUNET_NO != started) && (GNUNET_YES != connected))
  458. ok = 0;
  459. else
  460. {
  461. ok = 1;
  462. }
  463. }
  464. GNUNET_SCHEDULER_add_now (&end, NULL);
  465. }
  466. static void
  467. run (void *cls, char *const *args, const char *cfgfile,
  468. const struct GNUNET_CONFIGURATION_Handle *cfg)
  469. {
  470. connected = GNUNET_NO;
  471. stage = 0;
  472. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
  473. stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
  474. }
  475. int
  476. main (int argc, char *argv0[])
  477. {
  478. ok = 1;
  479. test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0]);
  480. GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL);
  481. static char *const argv[] =
  482. { "date", "-c", "test_transport_api_data.conf", NULL };
  483. static struct GNUNET_GETOPT_CommandLineOption options[] =
  484. { GNUNET_GETOPT_OPTION_END };
  485. tth = GNUNET_TRANSPORT_TESTING_init ();
  486. GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
  487. "test-transport-api-blacklisting", "nohelp", options,
  488. &run, NULL);
  489. GNUNET_TRANSPORT_TESTING_done (tth);
  490. return ok;
  491. }
  492. /* end of transport_api_blacklisting.c */