test_testing_topology_blacklist.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 testing/test_testing_topology_blacklist.c
  19. * @brief base testcase for testing transport level blacklisting
  20. */
  21. #include "platform.h"
  22. #include "gnunet_testing_lib.h"
  23. #include "gnunet_core_service.h"
  24. #define VERBOSE GNUNET_NO
  25. /**
  26. * How long until we fail the whole testcase?
  27. */
  28. #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
  29. /**
  30. * How long until we give up on starting the peers? (Must be longer than the connect timeout!)
  31. */
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
  33. #define DEFAULT_NUM_PEERS 4
  34. #define MAX_OUTSTANDING_CONNECTIONS 300
  35. static int ok;
  36. struct GNUNET_TIME_Relative connect_timeout;
  37. static unsigned long long connect_attempts;
  38. static unsigned long long num_peers;
  39. static unsigned int total_connections;
  40. static unsigned int failed_connections;
  41. static unsigned int expected_connections;
  42. static unsigned int expected_failed_connections;
  43. static unsigned long long peers_left;
  44. static struct GNUNET_TESTING_PeerGroup *pg;
  45. const struct GNUNET_CONFIGURATION_Handle *main_cfg;
  46. GNUNET_SCHEDULER_TaskIdentifier die_task;
  47. static char *dotOutFileName;
  48. static FILE *dotOutFile;
  49. static char *blacklist_transports;
  50. static enum GNUNET_TESTING_Topology topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Overlay should allow all connections */
  51. static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_RING; /* Blacklist underlay into a ring */
  52. static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
  53. static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Try to connect all possible OVERLAY connections */
  54. static double connect_topology_option_modifier = 0.0;
  55. static char *test_directory;
  56. #define MTYPE 12345
  57. struct GNUNET_TestMessage
  58. {
  59. /**
  60. * Header of the message
  61. */
  62. struct GNUNET_MessageHeader header;
  63. /**
  64. * Unique identifier for this message.
  65. */
  66. uint32_t uid;
  67. };
  68. /**
  69. * Check whether peers successfully shut down.
  70. */
  71. void
  72. shutdown_callback (void *cls, const char *emsg)
  73. {
  74. if (emsg != NULL)
  75. {
  76. #if VERBOSE
  77. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
  78. #endif
  79. if (ok == 0)
  80. ok = 666;
  81. }
  82. else
  83. {
  84. #if VERBOSE
  85. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  86. "All peers successfully shut down!\n");
  87. #endif
  88. }
  89. }
  90. static void
  91. finish_testing ()
  92. {
  93. GNUNET_assert (pg != NULL);
  94. #if VERBOSE
  95. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  96. "Called finish testing, stopping daemons.\n");
  97. #endif
  98. sleep (1);
  99. #if VERBOSE
  100. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling daemons_stop\n");
  101. #endif
  102. GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
  103. #if VERBOSE
  104. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "daemons_stop finished\n");
  105. #endif
  106. if (dotOutFile != NULL)
  107. {
  108. fprintf (dotOutFile, "}");
  109. fclose (dotOutFile);
  110. }
  111. ok = 0;
  112. }
  113. static void
  114. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  115. {
  116. char *msg = cls;
  117. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  118. "End badly was called (%s)... stopping daemons.\n", msg);
  119. if (pg != NULL)
  120. {
  121. GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
  122. ok = 7331; /* Opposite of leet */
  123. }
  124. else
  125. ok = 401; /* Never got peers started */
  126. if (dotOutFile != NULL)
  127. {
  128. fprintf (dotOutFile, "}");
  129. fclose (dotOutFile);
  130. }
  131. }
  132. void
  133. topology_callback (void *cls,
  134. const struct GNUNET_PeerIdentity *first,
  135. const struct GNUNET_PeerIdentity *second,
  136. uint32_t distance,
  137. const struct GNUNET_CONFIGURATION_Handle *first_cfg,
  138. const struct GNUNET_CONFIGURATION_Handle *second_cfg,
  139. struct GNUNET_TESTING_Daemon *first_daemon,
  140. struct GNUNET_TESTING_Daemon *second_daemon,
  141. const char *emsg)
  142. {
  143. if (emsg == NULL)
  144. {
  145. total_connections++;
  146. #if VERBOSE
  147. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
  148. first_daemon->shortname, second_daemon->shortname);
  149. #endif
  150. if (dotOutFile != NULL)
  151. fprintf (dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname,
  152. second_daemon->shortname);
  153. }
  154. else
  155. {
  156. failed_connections++;
  157. #if VERBOSE
  158. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  159. "Failed to connect peer %s to peer %s with error :\n%s\n",
  160. first_daemon->shortname, second_daemon->shortname, emsg);
  161. #endif
  162. }
  163. if (total_connections == expected_connections)
  164. {
  165. #if VERBOSE
  166. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  167. "Created %d total connections, which is our target number (that's bad)!\n",
  168. total_connections);
  169. #endif
  170. GNUNET_SCHEDULER_cancel (die_task);
  171. die_task = GNUNET_SCHEDULER_NO_TASK;
  172. die_task =
  173. GNUNET_SCHEDULER_add_now (&end_badly,
  174. "from topology_callback (too many successful connections)");
  175. }
  176. else if (total_connections + failed_connections == expected_connections)
  177. {
  178. if ((failed_connections == expected_failed_connections)
  179. && (total_connections ==
  180. expected_connections - expected_failed_connections))
  181. {
  182. GNUNET_SCHEDULER_cancel (die_task);
  183. die_task = GNUNET_SCHEDULER_NO_TASK;
  184. die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
  185. }
  186. else
  187. {
  188. GNUNET_SCHEDULER_cancel (die_task);
  189. die_task =
  190. GNUNET_SCHEDULER_add_now (&end_badly,
  191. "from topology_callback (wrong number of failed connections)");
  192. }
  193. }
  194. else
  195. {
  196. #if VERBOSE
  197. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  198. "Have %d total connections, %d failed connections, Want %d (failed) and %d (successful)\n",
  199. total_connections, failed_connections,
  200. expected_failed_connections,
  201. expected_connections - expected_failed_connections);
  202. #endif
  203. }
  204. }
  205. static void
  206. connect_topology ()
  207. {
  208. expected_connections = -1;
  209. if ((pg != NULL) && (peers_left == 0))
  210. {
  211. expected_connections =
  212. GNUNET_TESTING_connect_topology (pg, connection_topology,
  213. connect_topology_option,
  214. connect_topology_option_modifier,
  215. connect_timeout,
  216. connect_attempts,
  217. NULL, NULL);
  218. #if VERBOSE
  219. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  220. "Have %d expected connections\n", expected_connections);
  221. #endif
  222. }
  223. GNUNET_SCHEDULER_cancel (die_task);
  224. if (expected_connections == GNUNET_SYSERR)
  225. {
  226. die_task =
  227. GNUNET_SCHEDULER_add_now (&end_badly,
  228. "from connect topology (bad return)");
  229. }
  230. die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
  231. &end_badly,
  232. "from connect topology (timeout)");
  233. }
  234. static void
  235. create_topology ()
  236. {
  237. peers_left = num_peers; /* Reset counter */
  238. if (GNUNET_TESTING_create_topology
  239. (pg, topology, blacklist_topology,
  240. blacklist_transports) != GNUNET_SYSERR)
  241. {
  242. #if VERBOSE
  243. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  244. "Topology set up, now starting peers!\n");
  245. #endif
  246. GNUNET_TESTING_daemons_continue_startup (pg);
  247. }
  248. else
  249. {
  250. GNUNET_SCHEDULER_cancel (die_task);
  251. die_task =
  252. GNUNET_SCHEDULER_add_now (&end_badly,
  253. "from create topology (bad return)");
  254. }
  255. GNUNET_SCHEDULER_cancel (die_task);
  256. die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
  257. &end_badly,
  258. "from continue startup (timeout)");
  259. }
  260. static void
  261. peers_started_callback (void *cls,
  262. const struct GNUNET_PeerIdentity *id,
  263. const struct GNUNET_CONFIGURATION_Handle *cfg,
  264. struct GNUNET_TESTING_Daemon *d, const char *emsg)
  265. {
  266. if (emsg != NULL)
  267. {
  268. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  269. "Failed to start daemon with error: `%s'\n", emsg);
  270. return;
  271. }
  272. GNUNET_assert (id != NULL);
  273. #if VERBOSE
  274. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
  275. (num_peers - peers_left) + 1, num_peers);
  276. #endif
  277. peers_left--;
  278. if (peers_left == 0)
  279. {
  280. #if VERBOSE
  281. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  282. "All %d daemons started, now creating topology!\n",
  283. num_peers);
  284. #endif
  285. GNUNET_SCHEDULER_cancel (die_task);
  286. /* Set up task in case topology creation doesn't finish
  287. * within a reasonable amount of time */
  288. die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  289. (GNUNET_TIME_UNIT_MINUTES, 5),
  290. &end_badly,
  291. "from peers_started_callback");
  292. connect_topology ();
  293. ok = 0;
  294. }
  295. }
  296. /**
  297. * Callback indicating that the hostkey was created for a peer.
  298. *
  299. * @param cls NULL
  300. * @param id the peer identity
  301. * @param d the daemon handle (pretty useless at this point, remove?)
  302. * @param emsg non-null on failure
  303. */
  304. void
  305. hostkey_callback (void *cls,
  306. const struct GNUNET_PeerIdentity *id,
  307. struct GNUNET_TESTING_Daemon *d, const char *emsg)
  308. {
  309. if (emsg != NULL)
  310. {
  311. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  312. "Hostkey callback received error: %s\n", emsg);
  313. }
  314. #if VERBOSE
  315. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  316. "Hostkey created for peer `%s'\n", GNUNET_i2s (id));
  317. #endif
  318. peers_left--;
  319. if (peers_left == 0)
  320. {
  321. #if VERBOSE
  322. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  323. "All %d hostkeys created, now creating topology!\n",
  324. num_peers);
  325. #endif
  326. GNUNET_SCHEDULER_cancel (die_task);
  327. /* Set up task in case topology creation doesn't finish
  328. * within a reasonable amount of time */
  329. die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  330. (GNUNET_TIME_UNIT_MINUTES, 5),
  331. &end_badly,
  332. "from hostkey_callback");
  333. GNUNET_SCHEDULER_add_now (&create_topology, NULL);
  334. ok = 0;
  335. }
  336. }
  337. static void
  338. run (void *cls,
  339. char *const *args,
  340. const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
  341. {
  342. unsigned long long topology_num;
  343. unsigned long long connect_topology_num;
  344. unsigned long long blacklist_topology_num;
  345. unsigned long long connect_topology_option_num;
  346. unsigned long long temp_connect;
  347. char *connect_topology_option_modifier_string;
  348. ok = 1;
  349. dotOutFile = fopen (dotOutFileName, "w");
  350. if (dotOutFile != NULL)
  351. {
  352. fprintf (dotOutFile, "strict graph G {\n");
  353. }
  354. #if VERBOSE
  355. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  356. "Starting daemons based on config file %s\n", cfgfile);
  357. #endif
  358. if (GNUNET_YES !=
  359. GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
  360. &test_directory))
  361. {
  362. ok = 404;
  363. if (dotOutFile != NULL)
  364. {
  365. fclose (dotOutFile);
  366. }
  367. return;
  368. }
  369. if (GNUNET_YES ==
  370. GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "topology",
  371. &topology_num))
  372. topology = topology_num;
  373. if (GNUNET_YES ==
  374. GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
  375. "connect_topology",
  376. &connect_topology_num))
  377. connection_topology = connect_topology_num;
  378. if (GNUNET_YES ==
  379. GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
  380. "connect_topology_option",
  381. &connect_topology_option_num))
  382. connect_topology_option = connect_topology_option_num;
  383. if (GNUNET_YES ==
  384. GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
  385. "connect_topology_option_modifier",
  386. &connect_topology_option_modifier_string))
  387. {
  388. if (sscanf
  389. (connect_topology_option_modifier_string, "%lf",
  390. &connect_topology_option_modifier) != 1)
  391. {
  392. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  393. _
  394. ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
  395. connect_topology_option_modifier_string,
  396. "connect_topology_option_modifier", "TESTING");
  397. GNUNET_free (connect_topology_option_modifier_string);
  398. ok = 707;
  399. if (dotOutFile != NULL)
  400. {
  401. fclose (dotOutFile);
  402. }
  403. return;
  404. }
  405. GNUNET_free (connect_topology_option_modifier_string);
  406. }
  407. if (GNUNET_OK !=
  408. GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
  409. "blacklist_transports",
  410. &blacklist_transports))
  411. {
  412. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  413. "No transports specified for blacklisting in blacklist testcase (this shouldn't happen!)\n");
  414. ok = 808;
  415. if (dotOutFile != NULL)
  416. {
  417. fclose (dotOutFile);
  418. }
  419. return;
  420. }
  421. if (GNUNET_YES ==
  422. GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
  423. "blacklist_topology",
  424. &blacklist_topology_num))
  425. blacklist_topology = blacklist_topology_num;
  426. if (GNUNET_SYSERR ==
  427. GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
  428. &num_peers))
  429. num_peers = DEFAULT_NUM_PEERS;
  430. if (GNUNET_OK ==
  431. GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_timeout",
  432. &temp_connect))
  433. connect_timeout =
  434. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_connect);
  435. else
  436. {
  437. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_timeout");
  438. return;
  439. }
  440. if (GNUNET_OK !=
  441. GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_attempts",
  442. &connect_attempts))
  443. {
  444. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_attempts");
  445. return;
  446. }
  447. main_cfg = cfg;
  448. GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
  449. peers_left = num_peers;
  450. /* For this specific test we only really want a CLIQUE topology as the
  451. * overlay allowed topology, and a RING topology as the underlying connection
  452. * allowed topology. So we will expect only num_peers * 2 connections to
  453. * work, and (num_peers * (num_peers - 1)) - (num_peers * 2) to fail.
  454. */
  455. expected_connections = num_peers * (num_peers - 1);
  456. expected_failed_connections = expected_connections - (num_peers * 2);
  457. /* Set up a task to end testing if peer start fails */
  458. die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  459. (GNUNET_TIME_UNIT_MINUTES, 5),
  460. &end_badly,
  461. "didn't start all daemons in reasonable amount of time!!!");
  462. pg = GNUNET_TESTING_daemons_start (cfg,
  463. peers_left, peers_left, peers_left,
  464. TIMEOUT, &hostkey_callback,
  465. NULL, &peers_started_callback, NULL,
  466. &topology_callback, NULL, NULL);
  467. }
  468. static int
  469. check ()
  470. {
  471. int ret;
  472. char *const argv[] = { "test-testing-topology-blacklist",
  473. "-c",
  474. "test_testing_data_topology_blacklist.conf",
  475. #if VERBOSE
  476. "-L", "DEBUG",
  477. #endif
  478. NULL
  479. };
  480. struct GNUNET_GETOPT_CommandLineOption options[] = {
  481. GNUNET_GETOPT_OPTION_END
  482. };
  483. ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  484. argv, "test-testing-topology-blacklist", "nohelp",
  485. options, &run, &ok);
  486. if (ret != GNUNET_OK)
  487. {
  488. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  489. "`test-testing-topology-blacklist': Failed with error code %d\n",
  490. ret);
  491. }
  492. return ok;
  493. }
  494. int
  495. main (int argc, char *argv[])
  496. {
  497. int ret;
  498. GNUNET_log_setup ("test_testing_topology_blacklist",
  499. #if VERBOSE
  500. "DEBUG",
  501. #else
  502. "WARNING",
  503. #endif
  504. NULL);
  505. ret = check ();
  506. /**
  507. * Need to remove base directory, subdirectories taken care
  508. * of by the testing framework.
  509. */
  510. if (test_directory != NULL)
  511. {
  512. if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
  513. {
  514. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  515. "Failed to remove testing directory %s\n",
  516. test_directory);
  517. }
  518. }
  519. return ret;
  520. }
  521. /* end of test_testing_topology_blacklist.c */