test_peerstore_api_sync.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * Function that should be called with the result of the
  58. * lookup, and finally once with NULL to signal the end
  59. * of the iteration.
  60. *
  61. * Upon the first call, we set "ok" to success. On the
  62. * second call (end of iteration) we terminate the test.
  63. *
  64. * @param cls NULL
  65. * @param record the information stored in the peerstore
  66. * @param emsg any error message
  67. * @return #GNUNET_YES (all good, continue)
  68. */
  69. static void
  70. iterate_cb (void *cls,
  71. const struct GNUNET_PEERSTORE_Record *record,
  72. const char *emsg)
  73. {
  74. const char *rec_val;
  75. GNUNET_break (NULL == emsg);
  76. if (NULL == record)
  77. {
  78. GNUNET_PEERSTORE_disconnect (h,
  79. GNUNET_NO);
  80. GNUNET_SCHEDULER_shutdown ();
  81. return;
  82. }
  83. rec_val = record->value;
  84. GNUNET_break (0 == strcmp (rec_val, val));
  85. ok = 0;
  86. }
  87. /**
  88. * Run the 2nd stage of the test where we fetch the
  89. * data that should have been stored.
  90. *
  91. * @param cls NULL
  92. */
  93. static void
  94. test_cont (void *cls)
  95. {
  96. h = GNUNET_PEERSTORE_connect (cfg);
  97. GNUNET_PEERSTORE_iterate (h,
  98. subsystem,
  99. &pid, key,
  100. &iterate_cb,
  101. NULL);
  102. }
  103. /**
  104. * Actually run the test.
  105. */
  106. static void
  107. test1 ()
  108. {
  109. h = GNUNET_PEERSTORE_connect (cfg);
  110. GNUNET_PEERSTORE_store (h,
  111. subsystem,
  112. &pid,
  113. key,
  114. val, strlen (val) + 1,
  115. GNUNET_TIME_UNIT_FOREVER_ABS,
  116. GNUNET_PEERSTORE_STOREOPTION_REPLACE,
  117. NULL, NULL);
  118. GNUNET_PEERSTORE_disconnect (h,
  119. GNUNET_YES);
  120. h = NULL;
  121. /* We need to wait a little bit to give the disconnect
  122. a chance to actually finish the operation; otherwise,
  123. the test may fail non-deterministically if the new
  124. connection is faster than the cleanup routine of the
  125. old one. */
  126. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  127. &test_cont,
  128. NULL);
  129. }
  130. /**
  131. * Initialize globals and launch the test.
  132. *
  133. * @param cls NULL
  134. * @param c configuration to use
  135. * @param peer handle to our peer (unused)
  136. */
  137. static void
  138. run (void *cls,
  139. const struct GNUNET_CONFIGURATION_Handle *c,
  140. struct GNUNET_TESTING_Peer *peer)
  141. {
  142. cfg = c;
  143. memset (&pid, 1, sizeof (pid));
  144. test1 ();
  145. }
  146. int
  147. main (int argc, char *argv[])
  148. {
  149. if (0 !=
  150. GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync",
  151. "peerstore",
  152. "test_peerstore_api_data.conf",
  153. &run, NULL))
  154. return 1;
  155. if (0 != ok)
  156. fprintf (stderr,
  157. "Test failed: %d\n",
  158. ok);
  159. return ok;
  160. }
  161. /* end of test_peerstore_api_sync.c */