test_namestore_api_store_update.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012, 2013, 2018 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_store_update.c
  18. * @brief testcase for namestore_api.c: store a record, update it and perform a lookup
  19. * @author Matthias Wachs
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_namecache_service.h"
  24. #include "gnunet_namestore_service.h"
  25. #include "gnunet_testing_lib.h"
  26. #include "gnunet_dnsparser_lib.h"
  27. #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
  28. #define TEST_RECORD_DATALEN 123
  29. #define TEST_RECORD_DATA 'a'
  30. #define TEST_RECORD_DATALEN2 234
  31. #define TEST_RECORD_DATA2 'b'
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
  33. static struct GNUNET_NAMESTORE_Handle *nsh;
  34. static struct GNUNET_NAMECACHE_Handle *nch;
  35. static struct GNUNET_SCHEDULER_Task * endbadly_task;
  36. static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
  37. static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  38. static int res;
  39. static int update_performed;
  40. static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
  41. static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
  42. static const char *name = "dummy";
  43. /**
  44. * Terminate test with error.
  45. *
  46. * @param cls handle to use to re-connect.
  47. */
  48. static void
  49. endbadly (void *cls)
  50. {
  51. GNUNET_break (0);
  52. endbadly_task = NULL;
  53. GNUNET_SCHEDULER_shutdown ();
  54. res = 1;
  55. }
  56. static void
  57. end (void *cls)
  58. {
  59. if (NULL != endbadly_task)
  60. {
  61. GNUNET_SCHEDULER_cancel (endbadly_task);
  62. endbadly_task = NULL;
  63. }
  64. if (NULL != nsqe)
  65. {
  66. GNUNET_NAMESTORE_cancel (nsqe);
  67. nsqe = NULL;
  68. }
  69. if (NULL != ncqe)
  70. {
  71. GNUNET_NAMECACHE_cancel (ncqe);
  72. ncqe = NULL;
  73. }
  74. if (NULL != nsh)
  75. {
  76. GNUNET_NAMESTORE_disconnect (nsh);
  77. nsh = NULL;
  78. }
  79. if (NULL != nch)
  80. {
  81. GNUNET_NAMECACHE_disconnect (nch);
  82. nch = NULL;
  83. }
  84. if (NULL != privkey)
  85. {
  86. GNUNET_free (privkey);
  87. privkey = NULL;
  88. }
  89. }
  90. static void
  91. put_cont (void *cls,
  92. int32_t success,
  93. const char *emsg);
  94. static void
  95. rd_decrypt_cb (void *cls,
  96. unsigned int rd_count,
  97. const struct GNUNET_GNSRECORD_Data *rd)
  98. {
  99. struct GNUNET_GNSRECORD_Data rd_new;
  100. GNUNET_assert (1 == rd_count);
  101. GNUNET_assert (NULL != rd);
  102. if (GNUNET_NO == update_performed)
  103. {
  104. char rd_cmp_data[TEST_RECORD_DATALEN];
  105. memset (rd_cmp_data,
  106. TEST_RECORD_DATA,
  107. TEST_RECORD_DATALEN);
  108. GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
  109. GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
  110. GNUNET_assert (0 == memcmp (&rd_cmp_data,
  111. rd[0].data,
  112. TEST_RECORD_DATALEN));
  113. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  114. "Block was decrypted successfully, updating record \n");
  115. rd_new.flags = GNUNET_GNSRECORD_RF_NONE;
  116. rd_new.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
  117. rd_new.record_type = TEST_RECORD_TYPE;
  118. rd_new.data_size = TEST_RECORD_DATALEN2;
  119. rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2);
  120. memset ((char *) rd_new.data,
  121. TEST_RECORD_DATA2,
  122. TEST_RECORD_DATALEN2);
  123. nsqe = GNUNET_NAMESTORE_records_store (nsh,
  124. privkey,
  125. name,
  126. 1,
  127. &rd_new,
  128. &put_cont,
  129. (void *) name);
  130. update_performed = GNUNET_YES;
  131. }
  132. else
  133. {
  134. char rd_cmp_data[TEST_RECORD_DATALEN2];
  135. memset (rd_cmp_data,
  136. TEST_RECORD_DATA2,
  137. TEST_RECORD_DATALEN2);
  138. GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
  139. GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size);
  140. GNUNET_assert (0 == memcmp (&rd_cmp_data,
  141. rd[0].data,
  142. TEST_RECORD_DATALEN2));
  143. GNUNET_SCHEDULER_shutdown ();
  144. res = 0;
  145. }
  146. }
  147. static void
  148. name_lookup_proc (void *cls,
  149. const struct GNUNET_GNSRECORD_Block *block)
  150. {
  151. const char *name = cls;
  152. ncqe = NULL;
  153. GNUNET_assert (NULL != cls);
  154. if (NULL == block)
  155. {
  156. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  157. _("Namecache returned no block for `%s'\n"),
  158. name);
  159. GNUNET_SCHEDULER_shutdown ();
  160. return;
  161. }
  162. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  163. "Namecache returned block, decrypting \n");
  164. GNUNET_assert (GNUNET_OK ==
  165. GNUNET_GNSRECORD_block_decrypt (block,
  166. &pubkey,
  167. name,
  168. &rd_decrypt_cb,
  169. (void *) name));
  170. }
  171. static void
  172. put_cont (void *cls,
  173. int32_t success,
  174. const char *emsg)
  175. {
  176. const char *name = cls;
  177. struct GNUNET_HashCode derived_hash;
  178. nsqe = NULL;
  179. GNUNET_assert (NULL != cls);
  180. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  181. "Name store added record for `%s': %s\n",
  182. name,
  183. (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
  184. /* Create derived hash */
  185. GNUNET_GNSRECORD_query_from_private_key (privkey,
  186. name,
  187. &derived_hash);
  188. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  189. "Looking in namecache for `%s'\n",
  190. GNUNET_h2s (&derived_hash));
  191. ncqe = GNUNET_NAMECACHE_lookup_block (nch,
  192. &derived_hash,
  193. &name_lookup_proc, (void *) name);
  194. }
  195. static void
  196. run (void *cls,
  197. const struct GNUNET_CONFIGURATION_Handle *cfg,
  198. struct GNUNET_TESTING_Peer *peer)
  199. {
  200. struct GNUNET_GNSRECORD_Data rd;
  201. update_performed = GNUNET_NO;
  202. GNUNET_SCHEDULER_add_shutdown (&end,
  203. NULL);
  204. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  205. &endbadly,
  206. NULL);
  207. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  208. GNUNET_assert (privkey != NULL);
  209. GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
  210. &pubkey);
  211. rd.flags = GNUNET_GNSRECORD_RF_NONE;
  212. rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
  213. rd.record_type = TEST_RECORD_TYPE;
  214. rd.data_size = TEST_RECORD_DATALEN;
  215. rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
  216. memset ((char *) rd.data,
  217. TEST_RECORD_DATA,
  218. TEST_RECORD_DATALEN);
  219. nsh = GNUNET_NAMESTORE_connect (cfg);
  220. GNUNET_break (NULL != nsh);
  221. nch = GNUNET_NAMECACHE_connect (cfg);
  222. GNUNET_break (NULL != nch);
  223. nsqe = GNUNET_NAMESTORE_records_store (nsh,
  224. privkey,
  225. name,
  226. 1,
  227. &rd,
  228. &put_cont,
  229. (void *) name);
  230. if (NULL == nsqe)
  231. {
  232. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  233. _("Namestore cannot store no block\n"));
  234. }
  235. GNUNET_free ((void *)rd.data);
  236. }
  237. #include "test_common.c"
  238. int
  239. main (int argc,
  240. char *argv[])
  241. {
  242. const char *plugin_name;
  243. char *cfg_name;
  244. SETUP_CFG (plugin_name, cfg_name);
  245. res = 1;
  246. if (0 !=
  247. GNUNET_TESTING_peer_run ("test-namestore-api-store-update",
  248. cfg_name,
  249. &run,
  250. NULL))
  251. {
  252. res = 1;
  253. }
  254. GNUNET_DISK_purge_cfg_dir (cfg_name,
  255. "GNUNET_TEST_HOME");
  256. GNUNET_free (cfg_name);
  257. return res;
  258. }
  259. /* end of test_namestore_api_store_update.c */