test_set_union_copy.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2015, 2016 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_union_copy.c
  18. * @brief testcase for lazy copying of union sets
  19. * @author Florian Dold
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_common.h"
  24. #include "gnunet_testing_lib.h"
  25. #include "gnunet_set_service.h"
  26. /**
  27. * Value to return from #main().
  28. */
  29. static int ret;
  30. static struct GNUNET_PeerIdentity local_id;
  31. static struct GNUNET_SET_Handle *set1;
  32. static struct GNUNET_SET_Handle *set2;
  33. static const struct GNUNET_CONFIGURATION_Handle *config;
  34. static struct GNUNET_SCHEDULER_Task *tt;
  35. static void
  36. add_element_str (struct GNUNET_SET_Handle *set,
  37. char *str)
  38. {
  39. struct GNUNET_SET_Element element;
  40. element.element_type = 0;
  41. element.data = str;
  42. element.size = strlen (str);
  43. GNUNET_SET_add_element (set,
  44. &element,
  45. NULL,
  46. NULL);
  47. }
  48. static void
  49. remove_element_str (struct GNUNET_SET_Handle *set,
  50. char *str)
  51. {
  52. struct GNUNET_SET_Element element;
  53. element.element_type = 0;
  54. element.data = str;
  55. element.size = strlen (str);
  56. GNUNET_SET_remove_element (set,
  57. &element,
  58. NULL,
  59. NULL);
  60. }
  61. /**
  62. * Signature of the main function of a task.
  63. *
  64. * @param cls closure
  65. */
  66. static void
  67. timeout_fail (void *cls)
  68. {
  69. tt = NULL;
  70. GNUNET_SCHEDULER_shutdown ();
  71. ret = 1;
  72. }
  73. struct CountIterClosure
  74. {
  75. unsigned int expected_count;
  76. unsigned int ongoing_count;
  77. GNUNET_SCHEDULER_TaskCallback cont;
  78. void *cont_cls;
  79. char *what;
  80. };
  81. static int
  82. check_count_iter (void *cls,
  83. const struct GNUNET_SET_Element *element)
  84. {
  85. struct CountIterClosure *ci_cls = cls;
  86. if (NULL == element)
  87. {
  88. if (ci_cls->expected_count != ci_cls->ongoing_count)
  89. {
  90. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  91. "Expected count (what: %s) to be %u, but it's actually %u\n",
  92. ci_cls->what,
  93. ci_cls->expected_count,
  94. ci_cls->ongoing_count);
  95. ret = 1;
  96. GNUNET_SCHEDULER_shutdown ();
  97. return GNUNET_NO;
  98. }
  99. ci_cls->cont (ci_cls->cont_cls);
  100. GNUNET_free (ci_cls);
  101. return GNUNET_NO;
  102. }
  103. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  104. "Set `%s' has element %.*s\n",
  105. ci_cls->what,
  106. (int) element->size,
  107. (const char *) element->data);
  108. ci_cls->ongoing_count++;
  109. return GNUNET_YES;
  110. }
  111. static void
  112. check_count (struct GNUNET_SET_Handle *set,
  113. char *what,
  114. unsigned int expected_count,
  115. GNUNET_SCHEDULER_TaskCallback cont,
  116. void *cont_cls)
  117. {
  118. struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
  119. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  120. "Checking count of %s\n",
  121. what);
  122. ci_cls->expected_count = expected_count;
  123. ci_cls->ongoing_count = 0;
  124. ci_cls->cont = cont;
  125. ci_cls->cont_cls = cont_cls;
  126. ci_cls->what = what;
  127. GNUNET_assert (GNUNET_YES ==
  128. GNUNET_SET_iterate (set,
  129. &check_count_iter,
  130. ci_cls));
  131. }
  132. static void
  133. test_done (void *cls)
  134. {
  135. GNUNET_SCHEDULER_shutdown ();
  136. }
  137. static void
  138. check_new_set_count (void *cls)
  139. {
  140. check_count (set2,
  141. "new set",
  142. 3,
  143. &test_done,
  144. NULL);
  145. }
  146. static void
  147. copy_done (void *cls,
  148. struct GNUNET_SET_Handle *new_set)
  149. {
  150. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  151. "copy done\n");
  152. set2 = new_set;
  153. remove_element_str (set2,
  154. "k5555");
  155. add_element_str (set2,
  156. "n66666");
  157. add_element_str (set2,
  158. "new2butremoved");
  159. remove_element_str (set2,
  160. "new2butremoved");
  161. remove_element_str (set2,
  162. "new3justremoved");
  163. // Check that set1 didn't change.
  164. check_count (set1,
  165. "old set",
  166. 3,
  167. &check_new_set_count,
  168. NULL);
  169. }
  170. static void
  171. test_copy (void *cls)
  172. {
  173. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  174. "about to copy\n");
  175. GNUNET_SET_copy_lazy (set1,
  176. &copy_done,
  177. NULL);
  178. }
  179. /**
  180. * Function run on shutdown.
  181. *
  182. * @param cls closure
  183. */
  184. static void
  185. do_shutdown (void *cls)
  186. {
  187. if (NULL != tt)
  188. {
  189. GNUNET_SCHEDULER_cancel (tt);
  190. tt = NULL;
  191. }
  192. if (NULL != set1)
  193. {
  194. GNUNET_SET_destroy (set1);
  195. set1 = NULL;
  196. }
  197. if (NULL != set2)
  198. {
  199. GNUNET_SET_destroy (set2);
  200. set2 = NULL;
  201. }
  202. }
  203. /**
  204. * Signature of the 'main' function for a (single-peer) testcase that
  205. * is run using #GNUNET_TESTING_peer_run().
  206. *
  207. * @param cls closure
  208. * @param cfg configuration of the peer that was started
  209. * @param peer identity of the peer that was created
  210. */
  211. static void
  212. run (void *cls,
  213. const struct GNUNET_CONFIGURATION_Handle *cfg,
  214. struct GNUNET_TESTING_Peer *peer)
  215. {
  216. tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
  217. GNUNET_TIME_UNIT_SECONDS, 5),
  218. &timeout_fail,
  219. NULL);
  220. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  221. NULL);
  222. config = cfg;
  223. GNUNET_TESTING_peer_get_identity (peer,
  224. &local_id);
  225. set1 = GNUNET_SET_create (cfg,
  226. GNUNET_SET_OPERATION_UNION);
  227. add_element_str (set1,
  228. "333");
  229. add_element_str (set1,
  230. "k444");
  231. /* duplicate -- ignored */
  232. add_element_str (set1,
  233. "k444");
  234. remove_element_str (set1,
  235. "333");
  236. /* non-existent -- ignored */
  237. remove_element_str (set1,
  238. "999999999");
  239. add_element_str (set1,
  240. "k5555");
  241. /* duplicate -- ignored */
  242. remove_element_str (set1,
  243. "333");
  244. add_element_str (set1,
  245. "k2");
  246. check_count (set1,
  247. "initial test",
  248. 3,
  249. &test_copy,
  250. NULL);
  251. }
  252. int
  253. main (int argc, char **argv)
  254. {
  255. if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
  256. "test_set.conf",
  257. &run, NULL))
  258. {
  259. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  260. "failed to start testing peer\n");
  261. return 1;
  262. }
  263. return ret;
  264. }