test_set_api.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012 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 set/test_set_api.c
  18. * @brief testcase for set_api.c
  19. * @author Florian Dold
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testing_lib.h"
  24. #include "gnunet_set_service.h"
  25. static struct GNUNET_PeerIdentity local_id;
  26. static struct GNUNET_HashCode app_id;
  27. static struct GNUNET_SET_Handle *set1;
  28. static struct GNUNET_SET_Handle *set2;
  29. static struct GNUNET_SET_ListenHandle *listen_handle;
  30. static struct GNUNET_SET_OperationHandle *oh1;
  31. static struct GNUNET_SET_OperationHandle *oh2;
  32. static const struct GNUNET_CONFIGURATION_Handle *config;
  33. static unsigned int iter_count;
  34. static int ret;
  35. static struct GNUNET_SCHEDULER_Task *tt;
  36. static void
  37. result_cb_set1 (void *cls,
  38. const struct GNUNET_SET_Element *element,
  39. uint64_t size,
  40. enum GNUNET_SET_Status status)
  41. {
  42. switch (status)
  43. {
  44. case GNUNET_SET_STATUS_OK:
  45. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 1: got element\n");
  46. break;
  47. case GNUNET_SET_STATUS_FAILURE:
  48. GNUNET_break (0);
  49. oh1 = NULL;
  50. fprintf (stderr, "set 1: received failure status!\n");
  51. ret = 1;
  52. if (NULL != tt)
  53. {
  54. GNUNET_SCHEDULER_cancel (tt);
  55. tt = NULL;
  56. }
  57. GNUNET_SCHEDULER_shutdown ();
  58. break;
  59. case GNUNET_SET_STATUS_DONE:
  60. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 1: done\n");
  61. oh1 = NULL;
  62. if (NULL != set1)
  63. {
  64. GNUNET_SET_destroy (set1);
  65. set1 = NULL;
  66. }
  67. if (NULL == set2)
  68. {
  69. GNUNET_SCHEDULER_cancel (tt);
  70. tt = NULL;
  71. GNUNET_SCHEDULER_shutdown ();
  72. }
  73. break;
  74. default:
  75. GNUNET_assert (0);
  76. }
  77. }
  78. static void
  79. result_cb_set2 (void *cls,
  80. const struct GNUNET_SET_Element *element,
  81. uint64_t size,
  82. enum GNUNET_SET_Status status)
  83. {
  84. switch (status)
  85. {
  86. case GNUNET_SET_STATUS_OK:
  87. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 2: got element\n");
  88. break;
  89. case GNUNET_SET_STATUS_FAILURE:
  90. GNUNET_break (0);
  91. oh2 = NULL;
  92. fprintf (stderr, "set 2: received failure status\n");
  93. GNUNET_SCHEDULER_shutdown ();
  94. ret = 1;
  95. break;
  96. case GNUNET_SET_STATUS_DONE:
  97. oh2 = NULL;
  98. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 2: done\n");
  99. GNUNET_SET_destroy (set2);
  100. set2 = NULL;
  101. if (NULL == set1)
  102. {
  103. GNUNET_SCHEDULER_cancel (tt);
  104. tt = NULL;
  105. GNUNET_SCHEDULER_shutdown ();
  106. }
  107. break;
  108. default:
  109. GNUNET_assert (0);
  110. }
  111. }
  112. static void
  113. listen_cb (void *cls,
  114. const struct GNUNET_PeerIdentity *other_peer,
  115. const struct GNUNET_MessageHeader *context_msg,
  116. struct GNUNET_SET_Request *request)
  117. {
  118. GNUNET_assert (NULL != context_msg);
  119. GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
  120. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "listen cb called\n");
  121. oh2 = GNUNET_SET_accept (request,
  122. GNUNET_SET_RESULT_ADDED,
  123. (struct GNUNET_SET_Option[]){0},
  124. &result_cb_set2,
  125. NULL);
  126. GNUNET_SET_commit (oh2, set2);
  127. }
  128. /**
  129. * Start the set operation.
  130. *
  131. * @param cls closure, unused
  132. */
  133. static void
  134. start (void *cls)
  135. {
  136. struct GNUNET_MessageHeader context_msg;
  137. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting reconciliation\n");
  138. context_msg.size = htons (sizeof context_msg);
  139. context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
  140. listen_handle = GNUNET_SET_listen (config,
  141. GNUNET_SET_OPERATION_UNION,
  142. &app_id,
  143. &listen_cb,
  144. NULL);
  145. oh1 = GNUNET_SET_prepare (&local_id,
  146. &app_id,
  147. &context_msg,
  148. GNUNET_SET_RESULT_ADDED,
  149. (struct GNUNET_SET_Option[]){0},
  150. &result_cb_set1,
  151. NULL);
  152. GNUNET_SET_commit (oh1, set1);
  153. }
  154. /**
  155. * Initialize the second set, continue
  156. *
  157. * @param cls closure, unused
  158. */
  159. static void
  160. init_set2 (void *cls)
  161. {
  162. struct GNUNET_SET_Element element;
  163. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
  164. element.element_type = 0;
  165. element.data = "hello";
  166. element.size = strlen (element.data);
  167. GNUNET_SET_add_element (set2, &element, NULL, NULL);
  168. element.data = "quux";
  169. element.size = strlen (element.data);
  170. GNUNET_SET_add_element (set2, &element, NULL, NULL);
  171. element.data = "baz";
  172. element.size = strlen (element.data);
  173. GNUNET_SET_add_element (set2, &element, &start, NULL);
  174. }
  175. /**
  176. * Initialize the first set, continue.
  177. */
  178. static void
  179. init_set1 (void)
  180. {
  181. struct GNUNET_SET_Element element;
  182. element.element_type = 0;
  183. element.data = "hello";
  184. element.size = strlen (element.data);
  185. GNUNET_SET_add_element (set1, &element, NULL, NULL);
  186. element.data = "bar";
  187. element.size = strlen (element.data);
  188. GNUNET_SET_add_element (set1, &element, &init_set2, NULL);
  189. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
  190. }
  191. static int
  192. iter_cb (void *cls, const struct GNUNET_SET_Element *element)
  193. {
  194. struct GNUNET_SET_Handle *set = cls;
  195. if (NULL == element)
  196. {
  197. GNUNET_assert (3 == iter_count);
  198. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  199. "Iteration finished, destroying set %p\n",
  200. set);
  201. GNUNET_SET_destroy (set);
  202. return GNUNET_YES;
  203. }
  204. iter_count++;
  205. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iter: got element %u\n", iter_count);
  206. return GNUNET_YES;
  207. }
  208. static void
  209. test_iter ()
  210. {
  211. struct GNUNET_SET_Element element;
  212. struct GNUNET_SET_Handle *iter_set;
  213. iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
  214. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  215. "Testing iteration over 3 elements on set %p\n",
  216. iter_set);
  217. element.element_type = 0;
  218. element.data = "hello";
  219. element.size = strlen (element.data);
  220. GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
  221. element.data = "bar";
  222. element.size = strlen (element.data);
  223. GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
  224. element.data = "quux";
  225. element.size = strlen (element.data);
  226. GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
  227. GNUNET_SET_iterate (iter_set, &iter_cb, iter_set);
  228. }
  229. /**
  230. * Function run on timeout.
  231. *
  232. * @param cls closure
  233. */
  234. static void
  235. timeout_fail (void *cls)
  236. {
  237. tt = NULL;
  238. GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Testcase failed with timeout\n");
  239. GNUNET_SCHEDULER_shutdown ();
  240. ret = 1;
  241. }
  242. /**
  243. * Function run on shutdown.
  244. *
  245. * @param cls closure
  246. */
  247. static void
  248. do_shutdown (void *cls)
  249. {
  250. if (NULL != tt)
  251. {
  252. GNUNET_SCHEDULER_cancel (tt);
  253. tt = NULL;
  254. }
  255. if (NULL != oh1)
  256. {
  257. GNUNET_SET_operation_cancel (oh1);
  258. oh1 = NULL;
  259. }
  260. if (NULL != oh2)
  261. {
  262. GNUNET_SET_operation_cancel (oh2);
  263. oh2 = NULL;
  264. }
  265. if (NULL != set1)
  266. {
  267. GNUNET_SET_destroy (set1);
  268. set1 = NULL;
  269. }
  270. if (NULL != set2)
  271. {
  272. GNUNET_SET_destroy (set2);
  273. set2 = NULL;
  274. }
  275. if (NULL != listen_handle)
  276. {
  277. GNUNET_SET_listen_cancel (listen_handle);
  278. listen_handle = NULL;
  279. }
  280. }
  281. /**
  282. * Signature of the 'main' function for a (single-peer) testcase that
  283. * is run using 'GNUNET_TESTING_peer_run'.
  284. *
  285. * @param cls closure
  286. * @param cfg configuration of the peer that was started
  287. * @param peer identity of the peer that was created
  288. */
  289. static void
  290. run (void *cls,
  291. const struct GNUNET_CONFIGURATION_Handle *cfg,
  292. struct GNUNET_TESTING_Peer *peer)
  293. {
  294. struct GNUNET_SET_OperationHandle *my_oh;
  295. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running preparatory tests\n");
  296. tt = GNUNET_SCHEDULER_add_delayed (
  297. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
  298. &timeout_fail,
  299. NULL);
  300. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  301. config = cfg;
  302. GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_get_peer_identity (cfg, &local_id));
  303. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  304. "my id (from CRYPTO): %s\n",
  305. GNUNET_i2s (&local_id));
  306. GNUNET_TESTING_peer_get_identity (peer, &local_id);
  307. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  308. "my id (from TESTING): %s\n",
  309. GNUNET_i2s (&local_id));
  310. test_iter ();
  311. set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
  312. set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
  313. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  314. "Created sets %p and %p for union operation\n",
  315. set1,
  316. set2);
  317. GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
  318. /* test if canceling an uncommited request works! */
  319. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  320. "Launching and instantly stopping set operation\n");
  321. my_oh = GNUNET_SET_prepare (&local_id,
  322. &app_id,
  323. NULL,
  324. GNUNET_SET_RESULT_ADDED,
  325. (struct GNUNET_SET_Option[]){0},
  326. NULL,
  327. NULL);
  328. GNUNET_SET_operation_cancel (my_oh);
  329. /* test the real set reconciliation */
  330. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running real set-reconciliation\n");
  331. init_set1 ();
  332. }
  333. int
  334. main (int argc, char **argv)
  335. {
  336. GNUNET_log_setup ("test_set_api", "WARNING", NULL);
  337. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Launching peer\n");
  338. if (0 !=
  339. GNUNET_TESTING_peer_run ("test_set_api", "test_set.conf", &run, NULL))
  340. {
  341. return 1;
  342. }
  343. return ret;
  344. }