test_peerstore_api_sync.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2015 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 peerstore/test_peerstore_api_sync.c
  18. * @brief testcase for peerstore sync-on-disconnect feature. Stores
  19. * a value just before disconnecting, and then checks that
  20. * this value is actually stored.
  21. * @author Omar Tarabai
  22. * @author Christian Grothoff (minor fix, comments)
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_testing_lib.h"
  27. #include "gnunet_peerstore_service.h"
  28. /**
  29. * Overall result, 0 for success.
  30. */
  31. static int ok = 404;
  32. /**
  33. * Configuration we use.
  34. */
  35. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  36. /**
  37. * handle to talk to the peerstore.
  38. */
  39. static struct GNUNET_PEERSTORE_Handle *h;
  40. /**
  41. * Subsystem we store the value for.
  42. */
  43. static const char *subsystem = "test_peerstore_api_sync";
  44. /**
  45. * Fake PID under which we store the value.
  46. */
  47. static struct GNUNET_PeerIdentity pid;
  48. /**
  49. * Test key we're storing the test value under.
  50. */
  51. static const char *key = "test_peerstore_api_store_key";
  52. /**
  53. * Test value we are storing.
  54. */
  55. static const char *val = "test_peerstore_api_store_val";
  56. /**
  57. * Timeout
  58. */
  59. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  60. /**
  61. * Timeout task
  62. */
  63. static struct GNUNET_SCHEDULER_Task *to;
  64. /**
  65. * Iterate handle
  66. */
  67. static struct GNUNET_PEERSTORE_IterateContext *it;
  68. static void
  69. test_cont (void *cls);
  70. /**
  71. * Function that should be called with the result of the
  72. * lookup, and finally once with NULL to signal the end
  73. * of the iteration.
  74. *
  75. * Upon the first call, we set "ok" to success. On the
  76. * second call (end of iteration) we terminate the test.
  77. *
  78. * @param cls NULL
  79. * @param record the information stored in the peerstore
  80. * @param emsg any error message
  81. * @return #GNUNET_YES (all good, continue)
  82. */
  83. static void
  84. iterate_cb (void *cls,
  85. const struct GNUNET_PEERSTORE_Record *record,
  86. const char *emsg)
  87. {
  88. const char *rec_val;
  89. GNUNET_break (NULL == emsg);
  90. if (NULL == record)
  91. {
  92. it = NULL;
  93. if (0 == ok)
  94. {
  95. GNUNET_PEERSTORE_disconnect (h,
  96. GNUNET_NO);
  97. if (NULL != to)
  98. {
  99. GNUNET_SCHEDULER_cancel (to);
  100. to = NULL;
  101. }
  102. GNUNET_SCHEDULER_shutdown ();
  103. return;
  104. }
  105. /**
  106. * Try again
  107. */
  108. GNUNET_SCHEDULER_add_now (&test_cont,
  109. NULL);
  110. return;
  111. }
  112. rec_val = record->value;
  113. GNUNET_break (0 == strcmp (rec_val, val));
  114. ok = 0;
  115. }
  116. static void
  117. timeout_task (void *cls)
  118. {
  119. to = NULL;
  120. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  121. "Timeout reached\n");
  122. if (NULL != it)
  123. GNUNET_PEERSTORE_iterate_cancel (it);
  124. it = NULL;
  125. GNUNET_PEERSTORE_disconnect (h,
  126. GNUNET_NO);
  127. GNUNET_SCHEDULER_shutdown ();
  128. return;
  129. }
  130. /**
  131. * Run the 2nd stage of the test where we fetch the
  132. * data that should have been stored.
  133. *
  134. * @param cls NULL
  135. */
  136. static void
  137. test_cont (void *cls)
  138. {
  139. it = GNUNET_PEERSTORE_iterate (h,
  140. subsystem,
  141. &pid, key,
  142. &iterate_cb,
  143. NULL);
  144. }
  145. static void
  146. disc_cont (void *cls)
  147. {
  148. GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
  149. h = GNUNET_PEERSTORE_connect (cfg);
  150. GNUNET_SCHEDULER_add_now (&test_cont,
  151. NULL);
  152. }
  153. static void
  154. store_cont (void *cls, int success)
  155. {
  156. ok = success;
  157. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  158. "Success: %s\n",
  159. (GNUNET_SYSERR == ok) ? "no" : "yes");
  160. /* We need to wait a little bit to give the disconnect
  161. a chance to actually finish the operation; otherwise,
  162. the test may fail non-deterministically if the new
  163. connection is faster than the cleanup routine of the
  164. old one. */
  165. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  166. &disc_cont,
  167. NULL);
  168. }
  169. /**
  170. * Actually run the test.
  171. */
  172. static void
  173. test1 ()
  174. {
  175. h = GNUNET_PEERSTORE_connect (cfg);
  176. GNUNET_PEERSTORE_store (h,
  177. subsystem,
  178. &pid,
  179. key,
  180. val, strlen (val) + 1,
  181. GNUNET_TIME_UNIT_FOREVER_ABS,
  182. GNUNET_PEERSTORE_STOREOPTION_REPLACE,
  183. &store_cont, NULL);
  184. }
  185. /**
  186. * Initialize globals and launch the test.
  187. *
  188. * @param cls NULL
  189. * @param c configuration to use
  190. * @param peer handle to our peer (unused)
  191. */
  192. static void
  193. run (void *cls,
  194. const struct GNUNET_CONFIGURATION_Handle *c,
  195. struct GNUNET_TESTING_Peer *peer)
  196. {
  197. cfg = c;
  198. memset (&pid, 1, sizeof(pid));
  199. to = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  200. &timeout_task,
  201. NULL);
  202. GNUNET_SCHEDULER_add_now (&test1, NULL);
  203. }
  204. int
  205. main (int argc, char *argv[])
  206. {
  207. if (0 !=
  208. GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync",
  209. "peerstore",
  210. "peerstore.conf",
  211. &run, NULL))
  212. return 1;
  213. if (0 != ok)
  214. fprintf (stderr,
  215. "Test failed: %d\n",
  216. ok);
  217. return ok;
  218. }
  219. /* end of test_peerstore_api_sync.c */