test_ats_solver_alternative_after_delete_address.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2013 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 ats/test_ats_solver_alternative_after_delete_address.c
  19. * @brief solver test: add 2 addresses, request address, delete, expect alternative
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_testbed_service.h"
  26. #include "gnunet_ats_service.h"
  27. #include "test_ats_api_common.h"
  28. /**
  29. * Timeout task
  30. */
  31. static struct GNUNET_SCHEDULER_Task * die_task;
  32. /**
  33. * Statistics handle
  34. */
  35. static struct GNUNET_STATISTICS_Handle *stats;
  36. /**
  37. * Scheduling handle
  38. */
  39. static struct GNUNET_ATS_SchedulingHandle *sched_ats;
  40. /**
  41. * Connectivity handle
  42. */
  43. static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
  44. /**
  45. * Return value
  46. */
  47. static int ret;
  48. /**
  49. * Test address
  50. */
  51. static struct Test_Address test_addr;
  52. /**
  53. * Alternative test address
  54. */
  55. static struct Test_Address alt_test_addr;
  56. /**
  57. * Test peer
  58. */
  59. static struct PeerContext p;
  60. /**
  61. * HELLO address
  62. */
  63. static struct GNUNET_HELLO_Address test_hello_address;
  64. /**
  65. * HELLO address
  66. */
  67. static struct GNUNET_HELLO_Address alt_test_hello_address;
  68. /**
  69. * Session
  70. */
  71. static void *test_session;
  72. /**
  73. * Test ats info
  74. */
  75. static struct GNUNET_ATS_Information test_ats_info[2];
  76. /**
  77. * Test ats count
  78. */
  79. static uint32_t test_ats_count;
  80. /**
  81. * Test state
  82. */
  83. static int addresses_added;
  84. static int first_address_suggested;
  85. static int first_address_deleted;
  86. static int second_address_deleted;
  87. static int second_address_suggested = GNUNET_YES;
  88. static struct GNUNET_HELLO_Address *first_suggestion;
  89. static struct GNUNET_HELLO_Address *second_suggestion;
  90. /**
  91. * 1st Address we will destroy.
  92. */
  93. static struct GNUNET_ATS_AddressRecord *ar;
  94. /**
  95. * 2nd Address we will destroy.
  96. */
  97. static struct GNUNET_ATS_AddressRecord *ar2;
  98. static int
  99. stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
  100. int is_persistent);
  101. static void
  102. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  103. {
  104. if (NULL != die_task)
  105. {
  106. GNUNET_SCHEDULER_cancel (die_task);
  107. die_task = NULL;
  108. }
  109. if (NULL != connect_ats)
  110. {
  111. GNUNET_ATS_connectivity_done (connect_ats);
  112. connect_ats = NULL;
  113. }
  114. if (NULL != sched_ats)
  115. {
  116. GNUNET_ATS_scheduling_done (sched_ats);
  117. sched_ats = NULL;
  118. }
  119. GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
  120. if (NULL != stats)
  121. {
  122. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  123. stats = NULL;
  124. }
  125. free_test_address (&test_addr);
  126. GNUNET_free_non_null (first_suggestion);
  127. GNUNET_free_non_null (second_suggestion);
  128. ret = 0;
  129. }
  130. static void
  131. end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  132. {
  133. die_task = NULL;
  134. end ( NULL, NULL);
  135. ret = GNUNET_SYSERR;
  136. }
  137. static void
  138. end_badly_now ()
  139. {
  140. if (NULL != die_task)
  141. {
  142. GNUNET_SCHEDULER_cancel (die_task);
  143. die_task = NULL;
  144. }
  145. GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  146. }
  147. static void
  148. address_suggest_cb (void *cls,
  149. const struct GNUNET_PeerIdentity *peer,
  150. const struct GNUNET_HELLO_Address *address,
  151. struct Session *session,
  152. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  153. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
  154. {
  155. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  156. "Received a sugggestion for peer `%s'\n",
  157. GNUNET_i2s (peer));
  158. if (GNUNET_NO == first_address_suggested)
  159. {
  160. if (NULL == first_suggestion)
  161. {
  162. if ((NULL == address) || (NULL != session))
  163. {
  164. GNUNET_break (0);
  165. end_badly_now ();
  166. return;
  167. }
  168. if ((ntohl(bandwidth_in.value__) == 0) ||
  169. (ntohl(bandwidth_out.value__) == 0))
  170. {
  171. GNUNET_break (0);
  172. end_badly_now ();
  173. return;
  174. }
  175. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  176. "Received 1st sugggestion for peer `%s' : `%s'\n",
  177. GNUNET_i2s (&address->peer), (char *) address->address);
  178. first_suggestion = GNUNET_HELLO_address_copy (address);
  179. first_address_suggested = GNUNET_YES;
  180. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  181. "Deleting 1st address for peer `%s' : `%s'\n",
  182. GNUNET_i2s (&address->peer),
  183. (char *) address->address);
  184. if (0 == (strcmp ((char *) address->address,
  185. "test")))
  186. {
  187. GNUNET_assert (NULL != ar);
  188. GNUNET_ATS_address_destroy (ar);
  189. ar = NULL;
  190. }
  191. else
  192. {
  193. GNUNET_assert (NULL != ar2);
  194. GNUNET_ATS_address_destroy (ar2);
  195. ar2 = NULL;
  196. }
  197. first_address_deleted = GNUNET_YES;
  198. return;
  199. }
  200. }
  201. if (GNUNET_YES == first_address_deleted)
  202. {
  203. if (NULL == second_suggestion)
  204. {
  205. if ((NULL == address) || (NULL != session))
  206. {
  207. GNUNET_break (0);
  208. end_badly_now ();
  209. return;
  210. }
  211. if (0 != memcmp (address->address, first_suggestion->address,
  212. (first_suggestion->address_length < address->address_length) ? first_suggestion->address_length : address->address_length))
  213. {
  214. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  215. "Received 2nd sugggestion for peer `%s' : `%s'\n",
  216. GNUNET_i2s (&address->peer),
  217. (char *) address->address);
  218. second_suggestion = GNUNET_HELLO_address_copy (address);
  219. second_address_suggested = GNUNET_YES;
  220. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  221. "Deleting 2nd address for peer `%s' : `%s'\n",
  222. GNUNET_i2s (&address->peer),
  223. (char *) address->address);
  224. if (NULL != ar)
  225. {
  226. GNUNET_assert (NULL == ar2);
  227. GNUNET_ATS_address_destroy (ar);
  228. ar = NULL;
  229. }
  230. else
  231. {
  232. GNUNET_assert (NULL != ar2);
  233. GNUNET_ATS_address_destroy (ar2);
  234. ar2 = NULL;
  235. }
  236. second_address_deleted = GNUNET_YES;
  237. return;
  238. }
  239. }
  240. }
  241. if (GNUNET_YES == second_address_deleted)
  242. {
  243. /* Expecting disconnect */
  244. if ((ntohl(bandwidth_in.value__) == 0) &&
  245. (ntohl(bandwidth_out.value__) == 0))
  246. {
  247. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  248. "ATS tells me to disconnect\n");
  249. GNUNET_SCHEDULER_add_now (&end, NULL);
  250. return;
  251. }
  252. else
  253. {
  254. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  255. "Expected disconnect but received address `%s' with bandwidth \n",
  256. (char *) address->address);
  257. }
  258. }
  259. }
  260. static int
  261. stat_cb(void *cls, const char *subsystem,
  262. const char *name, uint64_t value,
  263. int is_persistent)
  264. {
  265. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  266. "ATS statistics: `%s' `%s' %llu\n",
  267. subsystem,
  268. name,
  269. value);
  270. if ((GNUNET_NO == addresses_added) && (value == 2))
  271. {
  272. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  273. "All addresses added, requesting....\n");
  274. /* We have 2 addresses, so we can request */
  275. addresses_added = GNUNET_YES;
  276. GNUNET_ATS_connectivity_suggest (connect_ats, &p.id);
  277. }
  278. return GNUNET_OK;
  279. }
  280. static void
  281. run (void *cls,
  282. const struct GNUNET_CONFIGURATION_Handle *mycfg,
  283. struct GNUNET_TESTING_Peer *peer)
  284. {
  285. die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  286. stats = GNUNET_STATISTICS_create ("ats", mycfg);
  287. GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
  288. connect_ats = GNUNET_ATS_connectivity_init (mycfg);
  289. /* Connect to ATS scheduling */
  290. sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
  291. if (sched_ats == NULL)
  292. {
  293. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  294. "Could not connect to ATS scheduling!\n");
  295. end_badly_now ();
  296. return;
  297. }
  298. /* Set up peer */
  299. memset (&p.id, '1', sizeof (p.id));
  300. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
  301. GNUNET_i2s_full(&p.id));
  302. /* Prepare ATS Information */
  303. test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
  304. test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
  305. test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
  306. test_ats_info[1].value = htonl(1);
  307. test_ats_count = 2;
  308. /* Adding address without session */
  309. test_session = NULL;
  310. create_test_address (&test_addr, "test", test_session,
  311. "test", strlen ("test") + 1);
  312. test_hello_address.peer = p.id;
  313. test_hello_address.transport_name = test_addr.plugin;
  314. test_hello_address.address = test_addr.addr;
  315. test_hello_address.address_length = test_addr.addr_len;
  316. /* Adding alternative address without session */
  317. test_session = NULL;
  318. create_test_address (&alt_test_addr, "test", test_session,
  319. "alt_test", strlen ("alt_test") + 1);
  320. alt_test_hello_address.peer = p.id;
  321. alt_test_hello_address.transport_name = alt_test_addr.plugin;
  322. alt_test_hello_address.address = alt_test_addr.addr;
  323. alt_test_hello_address.address_length = alt_test_addr.addr_len;
  324. /* Adding address */
  325. ar = GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL,
  326. test_ats_info, test_ats_count);
  327. /* Adding alternative address */
  328. ar2 = GNUNET_ATS_address_add (sched_ats, &alt_test_hello_address, NULL,
  329. test_ats_info, test_ats_count);
  330. }
  331. int
  332. main (int argc, char *argv[])
  333. {
  334. char *sep;
  335. char *src_filename = GNUNET_strdup (__FILE__);
  336. char *test_filename = GNUNET_strdup (argv[0]);
  337. const char *config_file;
  338. char *solver;
  339. ret = 0;
  340. if (NULL == (sep = (strstr (src_filename, ".c"))))
  341. {
  342. GNUNET_break (0);
  343. return -1;
  344. }
  345. sep[0] = '\0';
  346. if (NULL != (sep = strstr (test_filename, ".exe")))
  347. sep[0] = '\0';
  348. if (NULL == (solver = strstr (test_filename, src_filename)))
  349. {
  350. GNUNET_break (0);
  351. return -1;
  352. }
  353. solver += strlen (src_filename) +1;
  354. if (0 == strcmp(solver, "proportional"))
  355. {
  356. config_file = "test_ats_solver_proportional.conf";
  357. }
  358. else if (0 == strcmp(solver, "mlp"))
  359. {
  360. config_file = "test_ats_solver_mlp.conf";
  361. }
  362. else if ((0 == strcmp(solver, "ril")))
  363. {
  364. config_file = "test_ats_solver_ril.conf";
  365. }
  366. else
  367. {
  368. GNUNET_break (0);
  369. GNUNET_free (src_filename);
  370. GNUNET_free (test_filename);
  371. return 1;
  372. }
  373. GNUNET_free (src_filename);
  374. GNUNET_free (test_filename);
  375. if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
  376. config_file, &run, NULL ))
  377. return GNUNET_SYSERR;
  378. return ret;
  379. }
  380. /* end of file test_ats_solver_alternative_after_delete_address.c */