test_peerstore_api_sync.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. */
  15. /**
  16. * @file peerstore/test_peerstore_api_sync.c
  17. * @brief testcase for peerstore sync-on-disconnect feature. Stores
  18. * a value just before disconnecting, and then checks that
  19. * this value is actually stored.
  20. * @author Omar Tarabai
  21. * @author Christian Grothoff (minor fix, comments)
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_testing_lib.h"
  26. #include "gnunet_peerstore_service.h"
  27. /**
  28. * Overall result, 0 for success.
  29. */
  30. static int ok = 404;
  31. /**
  32. * Configuration we use.
  33. */
  34. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  35. /**
  36. * handle to talk to the peerstore.
  37. */
  38. static struct GNUNET_PEERSTORE_Handle *h;
  39. /**
  40. * Subsystem we store the value for.
  41. */
  42. static const char *subsystem = "test_peerstore_api_sync";
  43. /**
  44. * Fake PID under which we store the value.
  45. */
  46. static struct GNUNET_PeerIdentity pid;
  47. /**
  48. * Test key we're storing the test value under.
  49. */
  50. static const char *key = "test_peerstore_api_store_key";
  51. /**
  52. * Test value we are storing.
  53. */
  54. static const char *val = "test_peerstore_api_store_val";
  55. /**
  56. * Function that should be called with the result of the
  57. * lookup, and finally once with NULL to signal the end
  58. * of the iteration.
  59. *
  60. * Upon the first call, we set "ok" to success. On the
  61. * second call (end of iteration) we terminate the test.
  62. *
  63. * @param cls NULL
  64. * @param record the information stored in the peerstore
  65. * @param emsg any error message
  66. * @return #GNUNET_YES (all good, continue)
  67. */
  68. static void
  69. iterate_cb (void *cls,
  70. const struct GNUNET_PEERSTORE_Record *record,
  71. const char *emsg)
  72. {
  73. const char *rec_val;
  74. GNUNET_break (NULL == emsg);
  75. if (NULL == record)
  76. {
  77. GNUNET_PEERSTORE_disconnect (h,
  78. GNUNET_NO);
  79. GNUNET_SCHEDULER_shutdown ();
  80. return;
  81. }
  82. rec_val = record->value;
  83. GNUNET_break (0 == strcmp (rec_val, val));
  84. ok = 0;
  85. }
  86. /**
  87. * Run the 2nd stage of the test where we fetch the
  88. * data that should have been stored.
  89. *
  90. * @param cls NULL
  91. */
  92. static void
  93. test_cont (void *cls)
  94. {
  95. h = GNUNET_PEERSTORE_connect (cfg);
  96. GNUNET_PEERSTORE_iterate (h,
  97. subsystem,
  98. &pid, key,
  99. &iterate_cb,
  100. NULL);
  101. }
  102. /**
  103. * Actually run the test.
  104. */
  105. static void
  106. test1 ()
  107. {
  108. h = GNUNET_PEERSTORE_connect (cfg);
  109. GNUNET_PEERSTORE_store (h,
  110. subsystem,
  111. &pid,
  112. key,
  113. val, strlen (val) + 1,
  114. GNUNET_TIME_UNIT_FOREVER_ABS,
  115. GNUNET_PEERSTORE_STOREOPTION_REPLACE,
  116. NULL, NULL);
  117. GNUNET_PEERSTORE_disconnect (h,
  118. GNUNET_YES);
  119. h = NULL;
  120. /* We need to wait a little bit to give the disconnect
  121. a chance to actually finish the operation; otherwise,
  122. the test may fail non-deterministically if the new
  123. connection is faster than the cleanup routine of the
  124. old one. */
  125. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  126. &test_cont,
  127. NULL);
  128. }
  129. /**
  130. * Initialize globals and launch the test.
  131. *
  132. * @param cls NULL
  133. * @param c configuration to use
  134. * @param peer handle to our peer (unused)
  135. */
  136. static void
  137. run (void *cls,
  138. const struct GNUNET_CONFIGURATION_Handle *c,
  139. struct GNUNET_TESTING_Peer *peer)
  140. {
  141. cfg = c;
  142. memset (&pid, 1, sizeof (pid));
  143. test1 ();
  144. }
  145. int
  146. main (int argc, char *argv[])
  147. {
  148. if (0 !=
  149. GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync",
  150. "peerstore",
  151. "test_peerstore_api_data.conf",
  152. &run, NULL))
  153. return 1;
  154. if (0 != ok)
  155. fprintf (stderr,
  156. "Test failed: %d\n",
  157. ok);
  158. return ok;
  159. }
  160. /* end of test_peerstore_api_sync.c */