test_namestore_api_lookup_shadow.c 6.9 KB

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