test_namestore_api_remove.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 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 namestore/test_namestore_api.c
  18. * @brief testcase for namestore_api.c to: remove record
  19. */
  20. #include "platform.h"
  21. #include "gnunet_namestore_service.h"
  22. #include "gnunet_testing_lib.h"
  23. #include "gnunet_dnsparser_lib.h"
  24. #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
  25. #define TEST_RECORD_DATALEN 123
  26. #define TEST_RECORD_DATA 'a'
  27. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
  28. static struct GNUNET_NAMESTORE_Handle *nsh;
  29. static struct GNUNET_SCHEDULER_Task * endbadly_task;
  30. static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
  31. static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  32. static int res;
  33. static int removed;
  34. static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
  35. static void
  36. cleanup ()
  37. {
  38. if (NULL != nsh)
  39. {
  40. GNUNET_NAMESTORE_disconnect (nsh);
  41. nsh = NULL;
  42. }
  43. if (NULL != privkey)
  44. {
  45. GNUNET_free (privkey);
  46. privkey = NULL;
  47. }
  48. GNUNET_SCHEDULER_shutdown ();
  49. }
  50. /**
  51. * Re-establish the connection to the service.
  52. *
  53. * @param cls handle to use to re-connect.
  54. */
  55. static void
  56. endbadly (void *cls)
  57. {
  58. if (NULL != nsqe)
  59. {
  60. GNUNET_NAMESTORE_cancel (nsqe);
  61. nsqe = NULL;
  62. }
  63. cleanup ();
  64. res = 1;
  65. }
  66. static void
  67. end (void *cls)
  68. {
  69. cleanup ();
  70. res = 0;
  71. }
  72. static void
  73. remove_cont (void *cls,
  74. int32_t success,
  75. const char *emsg)
  76. {
  77. nsqe = NULL;
  78. if (GNUNET_YES != success)
  79. {
  80. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  81. _("Records could not be removed: `%s'\n"),
  82. emsg);
  83. if (NULL != endbadly_task)
  84. GNUNET_SCHEDULER_cancel (endbadly_task);
  85. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
  86. NULL);
  87. return;
  88. }
  89. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  90. "Records were removed, perform lookup\n");
  91. removed = GNUNET_YES;
  92. if (NULL != endbadly_task)
  93. GNUNET_SCHEDULER_cancel (endbadly_task);
  94. GNUNET_SCHEDULER_add_now (&end, NULL);
  95. }
  96. static void
  97. put_cont (void *cls,
  98. int32_t success,
  99. const char *emsg)
  100. {
  101. const char *name = cls;
  102. GNUNET_assert (NULL != cls);
  103. nsqe = NULL;
  104. if (GNUNET_SYSERR == success)
  105. {
  106. GNUNET_break (0);
  107. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  108. "Namestore could not store record: `%s'\n",
  109. emsg);
  110. if (endbadly_task != NULL)
  111. GNUNET_SCHEDULER_cancel (endbadly_task);
  112. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  113. return;
  114. }
  115. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  116. "Name store added record for `%s': %s\n",
  117. name,
  118. (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
  119. nsqe = GNUNET_NAMESTORE_records_store (nsh,
  120. privkey,
  121. name,
  122. 0, NULL,
  123. &remove_cont, (void *) name);
  124. }
  125. static void
  126. run (void *cls,
  127. const struct GNUNET_CONFIGURATION_Handle *cfg,
  128. struct GNUNET_TESTING_Peer *peer)
  129. {
  130. struct GNUNET_GNSRECORD_Data rd;
  131. const char * name = "dummy.dummy.gnunet";
  132. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  133. &endbadly,
  134. NULL);
  135. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  136. GNUNET_assert (privkey != NULL);
  137. GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
  138. &pubkey);
  139. removed = GNUNET_NO;
  140. rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
  141. rd.record_type = TEST_RECORD_TYPE;
  142. rd.data_size = TEST_RECORD_DATALEN;
  143. rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
  144. rd.flags = 0;
  145. memset ((char *) rd.data,
  146. 'a',
  147. TEST_RECORD_DATALEN);
  148. nsh = GNUNET_NAMESTORE_connect (cfg);
  149. GNUNET_break (NULL != nsh);
  150. nsqe = GNUNET_NAMESTORE_records_store (nsh,
  151. privkey,
  152. name,
  153. 1,
  154. &rd,
  155. &put_cont,
  156. (void *) name);
  157. if (NULL == nsqe)
  158. {
  159. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  160. _("Namestore cannot store no block\n"));
  161. }
  162. GNUNET_free ((void *)rd.data);
  163. }
  164. #include "test_common.c"
  165. int
  166. main (int argc, char *argv[])
  167. {
  168. const char *plugin_name;
  169. char *cfg_name;
  170. SETUP_CFG (plugin_name, cfg_name);
  171. res = 1;
  172. if (0 !=
  173. GNUNET_TESTING_peer_run ("test-namestore-api-remove",
  174. cfg_name,
  175. &run,
  176. NULL))
  177. {
  178. res = 1;
  179. }
  180. GNUNET_DISK_purge_cfg_dir (cfg_name,
  181. "GNUNET_TEST_HOME");
  182. GNUNET_free (cfg_name);
  183. return res;
  184. }
  185. /* end of test_namestore_api_remove.c */