test_peerstore_api_store.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 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_store.c
  18. * @brief testcase for peerstore store operation
  19. */
  20. #include "platform.h"
  21. #include "gnunet_peerstore_service.h"
  22. #include "gnunet_testing_lib.h"
  23. static int ok = 1;
  24. static struct GNUNET_PEERSTORE_Handle *h;
  25. static char *subsystem = "test_peerstore_api_store";
  26. static struct GNUNET_PeerIdentity pid;
  27. static char *key = "test_peerstore_api_store_key";
  28. static char *val1 = "test_peerstore_api_store_val1";
  29. static char *val2 = "test_peerstore_api_store_val2-";
  30. static char *val3 = "test_peerstore_api_store_val3--";
  31. static int count = 0;
  32. static void
  33. test3_cont2 (void *cls,
  34. const struct GNUNET_PEERSTORE_Record *record,
  35. const char *emsg)
  36. {
  37. if (NULL != emsg)
  38. return;
  39. if (NULL != record)
  40. {
  41. GNUNET_assert ((strlen (val3) + 1) == record->value_size);
  42. GNUNET_assert (0 == strcmp ((char *) val3,
  43. (char *) record->value));
  44. count++;
  45. return;
  46. }
  47. GNUNET_assert (count == 1);
  48. ok = 0;
  49. GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
  50. GNUNET_SCHEDULER_shutdown ();
  51. }
  52. static void
  53. test3_cont (void *cls,
  54. int success)
  55. {
  56. if (GNUNET_YES != success)
  57. return;
  58. count = 0;
  59. GNUNET_PEERSTORE_iterate (h,
  60. subsystem,
  61. &pid,
  62. key,
  63. &test3_cont2,
  64. NULL);
  65. }
  66. /**
  67. * Replace the previous 2 records
  68. */
  69. static void
  70. test3 ()
  71. {
  72. GNUNET_PEERSTORE_store (h,
  73. subsystem,
  74. &pid,
  75. key,
  76. val3,
  77. strlen (val3) + 1,
  78. GNUNET_TIME_UNIT_FOREVER_ABS,
  79. GNUNET_PEERSTORE_STOREOPTION_REPLACE,
  80. &test3_cont,
  81. NULL);
  82. }
  83. static void
  84. test2_cont2 (void *cls,
  85. const struct GNUNET_PEERSTORE_Record *record,
  86. const char *emsg)
  87. {
  88. if (NULL != emsg)
  89. return;
  90. if (NULL != record)
  91. {
  92. GNUNET_assert (((strlen (val1) + 1) == record->value_size) ||
  93. ((strlen (val2) + 1) == record->value_size));
  94. GNUNET_assert ((0 == strcmp ((char *) val1, (char *) record->value)) ||
  95. (0 == strcmp ((char *) val2, (char *) record->value)));
  96. count++;
  97. return;
  98. }
  99. GNUNET_assert (count == 2);
  100. count = 0;
  101. test3 ();
  102. }
  103. static void
  104. test2_cont (void *cls, int success)
  105. {
  106. if (GNUNET_YES != success)
  107. return;
  108. count = 0;
  109. GNUNET_PEERSTORE_iterate (h,
  110. subsystem,
  111. &pid, key,
  112. &test2_cont2,
  113. NULL);
  114. }
  115. /**
  116. * Test storing a second value with the same key
  117. */
  118. void
  119. test2 ()
  120. {
  121. GNUNET_PEERSTORE_store (h,
  122. subsystem,
  123. &pid,
  124. key,
  125. val2,
  126. strlen (val2) + 1,
  127. GNUNET_TIME_UNIT_FOREVER_ABS,
  128. GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
  129. &test2_cont,
  130. NULL);
  131. }
  132. static void
  133. test1_cont2 (void *cls,
  134. const struct GNUNET_PEERSTORE_Record *record,
  135. const char *emsg)
  136. {
  137. if (NULL != emsg)
  138. return;
  139. if (NULL != record)
  140. {
  141. GNUNET_assert ((strlen (val1) + 1) == record->value_size);
  142. GNUNET_assert (0 == strcmp ((char *) val1, (char *) record->value));
  143. count++;
  144. return;
  145. }
  146. GNUNET_assert (count == 1);
  147. count = 0;
  148. test2 ();
  149. }
  150. static void
  151. test1_cont (void *cls, int success)
  152. {
  153. if (GNUNET_YES != success)
  154. return;
  155. count = 0;
  156. GNUNET_PEERSTORE_iterate (h,
  157. subsystem,
  158. &pid,
  159. key,
  160. &test1_cont2,
  161. NULL);
  162. }
  163. /**
  164. * Store a single record
  165. */
  166. static void
  167. test1 ()
  168. {
  169. GNUNET_PEERSTORE_store (h,
  170. subsystem,
  171. &pid,
  172. key,
  173. val1,
  174. strlen (val1) + 1,
  175. GNUNET_TIME_UNIT_FOREVER_ABS,
  176. GNUNET_PEERSTORE_STOREOPTION_REPLACE,
  177. &test1_cont,
  178. NULL);
  179. }
  180. static void
  181. run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
  182. struct GNUNET_TESTING_Peer *peer)
  183. {
  184. h = GNUNET_PEERSTORE_connect (cfg);
  185. GNUNET_assert (NULL != h);
  186. memset (&pid, 1, sizeof(pid));
  187. test1 ();
  188. }
  189. int
  190. main (int argc, char *argv[])
  191. {
  192. if (0 !=
  193. GNUNET_TESTING_service_run ("test-gnunet-peerstore",
  194. "peerstore",
  195. "test_peerstore_api_data.conf",
  196. &run, NULL))
  197. return 1;
  198. return ok;
  199. }
  200. /* end of test_peerstore_api_store.c */