test_peerstore_api_iterate.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013-2017 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_iterate.c
  18. * @brief testcase for peerstore iteration operation
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. #include "gnunet_testing_lib.h"
  23. #include "gnunet_peerstore_service.h"
  24. static int ok = 1;
  25. static struct GNUNET_PEERSTORE_Handle *h;
  26. static struct GNUNET_PEERSTORE_IterateContext *ic;
  27. static char *ss = "test_peerstore_api_iterate";
  28. static struct GNUNET_PeerIdentity p1;
  29. static struct GNUNET_PeerIdentity p2;
  30. static char *k1 = "test_peerstore_api_iterate_key1";
  31. static char *k2 = "test_peerstore_api_iterate_key2";
  32. static char *k3 = "test_peerstore_api_iterate_key3";
  33. static char *val = "test_peerstore_api_iterate_val";
  34. static int count = 0;
  35. static void
  36. iter3_cb (void *cls,
  37. const struct GNUNET_PEERSTORE_Record *record,
  38. const char *emsg)
  39. {
  40. if (NULL != emsg)
  41. {
  42. GNUNET_PEERSTORE_iterate_cancel (ic);
  43. return;
  44. }
  45. if (NULL != record)
  46. {
  47. count++;
  48. return;
  49. }
  50. GNUNET_assert (count == 3);
  51. ok = 0;
  52. GNUNET_PEERSTORE_disconnect (h, GNUNET_NO);
  53. GNUNET_SCHEDULER_shutdown ();
  54. }
  55. static void
  56. iter2_cb (void *cls,
  57. const struct GNUNET_PEERSTORE_Record *record,
  58. const char *emsg)
  59. {
  60. if (NULL != emsg)
  61. {
  62. GNUNET_PEERSTORE_iterate_cancel (ic);
  63. return;
  64. }
  65. if (NULL != record)
  66. {
  67. count++;
  68. return;
  69. }
  70. GNUNET_assert (count == 2);
  71. count = 0;
  72. ic = GNUNET_PEERSTORE_iterate (h,
  73. ss,
  74. NULL,
  75. NULL,
  76. &iter3_cb,
  77. NULL);
  78. }
  79. static void
  80. iter1_cb (void *cls,
  81. const struct GNUNET_PEERSTORE_Record *record,
  82. const char *emsg)
  83. {
  84. if (NULL != emsg)
  85. {
  86. GNUNET_PEERSTORE_iterate_cancel (ic);
  87. return;
  88. }
  89. if (NULL != record)
  90. {
  91. count++;
  92. return;
  93. }
  94. GNUNET_assert (count == 1);
  95. count = 0;
  96. ic = GNUNET_PEERSTORE_iterate (h,
  97. ss,
  98. &p1,
  99. NULL,
  100. &iter2_cb,
  101. NULL);
  102. }
  103. static void
  104. run (void *cls,
  105. const struct GNUNET_CONFIGURATION_Handle *cfg,
  106. struct GNUNET_TESTING_Peer *peer)
  107. {
  108. h = GNUNET_PEERSTORE_connect (cfg);
  109. GNUNET_assert (NULL != h);
  110. memset (&p1, 1, sizeof (p1));
  111. memset (&p2, 2, sizeof (p2));
  112. GNUNET_PEERSTORE_store (h,
  113. ss,
  114. &p1,
  115. k1,
  116. val,
  117. strlen (val) + 1,
  118. GNUNET_TIME_UNIT_FOREVER_ABS,
  119. GNUNET_PEERSTORE_STOREOPTION_REPLACE,
  120. NULL,
  121. NULL);
  122. GNUNET_PEERSTORE_store (h,
  123. ss,
  124. &p1,
  125. k2,
  126. val,
  127. strlen (val) + 1,
  128. GNUNET_TIME_UNIT_FOREVER_ABS,
  129. GNUNET_PEERSTORE_STOREOPTION_REPLACE,
  130. NULL,
  131. NULL);
  132. GNUNET_PEERSTORE_store (h,
  133. ss,
  134. &p2,
  135. k3,
  136. val,
  137. strlen (val) + 1,
  138. GNUNET_TIME_UNIT_FOREVER_ABS,
  139. GNUNET_PEERSTORE_STOREOPTION_REPLACE,
  140. NULL,
  141. NULL);
  142. ic = GNUNET_PEERSTORE_iterate (h,
  143. ss,
  144. &p1,
  145. k1,
  146. &iter1_cb, NULL);
  147. }
  148. int
  149. main (int argc, char *argv[])
  150. {
  151. if (0 !=
  152. GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore",
  153. "test_peerstore_api_data.conf", &run, NULL))
  154. return 1;
  155. return ok;
  156. }
  157. /* end of test_peerstore_api_iterate.c */