test_namecache_api_cache_block.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 namecache/test_namecache_api.c
  18. * @brief testcase for namecache_api.c: store a record and perform a lookup
  19. */
  20. #include "platform.h"
  21. #include "gnunet_namecache_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_NAMECACHE_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 struct GNUNET_NAMECACHE_QueueEntry *nsqe;
  34. static void
  35. cleanup ()
  36. {
  37. if (NULL != nsh)
  38. {
  39. GNUNET_NAMECACHE_disconnect (nsh);
  40. nsh = NULL;
  41. }
  42. if (NULL != privkey)
  43. {
  44. GNUNET_free (privkey);
  45. privkey = NULL;
  46. }
  47. GNUNET_SCHEDULER_shutdown ();
  48. }
  49. /**
  50. * Re-establish the connection to the service.
  51. *
  52. * @param cls handle to use to re-connect.
  53. */
  54. static void
  55. endbadly (void *cls)
  56. {
  57. if (NULL != nsqe)
  58. {
  59. GNUNET_NAMECACHE_cancel (nsqe);
  60. nsqe = NULL;
  61. }
  62. cleanup ();
  63. res = 1;
  64. }
  65. static void
  66. end (void *cls)
  67. {
  68. cleanup ();
  69. res = 0;
  70. }
  71. static void
  72. rd_decrypt_cb (void *cls,
  73. unsigned int rd_count,
  74. const struct GNUNET_GNSRECORD_Data *rd)
  75. {
  76. char rd_cmp_data[TEST_RECORD_DATALEN];
  77. GNUNET_assert (1 == rd_count);
  78. GNUNET_assert (NULL != rd);
  79. memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
  80. GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
  81. GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
  82. GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
  83. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  84. "Block was decrypted successfully \n");
  85. GNUNET_SCHEDULER_add_now (&end, NULL);
  86. }
  87. static void
  88. name_lookup_proc (void *cls,
  89. const struct GNUNET_GNSRECORD_Block *block)
  90. {
  91. const char *name = cls;
  92. nsqe = NULL;
  93. GNUNET_assert (NULL != cls);
  94. if (endbadly_task != NULL)
  95. {
  96. GNUNET_SCHEDULER_cancel (endbadly_task);
  97. endbadly_task = NULL;
  98. }
  99. if (NULL == block)
  100. {
  101. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  102. _("Namecache returned no block\n"));
  103. if (NULL != endbadly_task)
  104. GNUNET_SCHEDULER_cancel (endbadly_task);
  105. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  106. return;
  107. }
  108. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  109. "Namecache returned block, decrypting \n");
  110. GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
  111. &pubkey, name, &rd_decrypt_cb, (void *) name));
  112. }
  113. static void
  114. cache_cont (void *cls, int32_t success, const char *emsg)
  115. {
  116. const char *name = cls;
  117. struct GNUNET_HashCode derived_hash;
  118. GNUNET_assert (NULL != cls);
  119. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  120. "Name store cached record for `%s': %s\n",
  121. name,
  122. (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
  123. /* Create derived hash */
  124. GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
  125. nsqe = GNUNET_NAMECACHE_lookup_block (nsh, &derived_hash,
  126. &name_lookup_proc, (void *) name);
  127. }
  128. static void
  129. run (void *cls,
  130. const struct GNUNET_CONFIGURATION_Handle *cfg,
  131. struct GNUNET_TESTING_Peer *peer)
  132. {
  133. struct GNUNET_GNSRECORD_Data rd;
  134. struct GNUNET_GNSRECORD_Block *block;
  135. char *hostkey_file;
  136. const char * name = "dummy.dummy.gnunet";
  137. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  138. &endbadly, NULL);
  139. GNUNET_asprintf (&hostkey_file,
  140. "zonefiles%s%s",
  141. DIR_SEPARATOR_STR,
  142. "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
  143. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
  144. privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
  145. GNUNET_free (hostkey_file);
  146. GNUNET_assert (privkey != NULL);
  147. GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
  148. rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 10000000000;
  149. rd.record_type = TEST_RECORD_TYPE;
  150. rd.data_size = TEST_RECORD_DATALEN;
  151. rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
  152. rd.flags = 0;
  153. memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
  154. block = GNUNET_GNSRECORD_block_create (privkey,
  155. GNUNET_TIME_UNIT_FOREVER_ABS,
  156. name, &rd, 1);
  157. if (NULL == block)
  158. {
  159. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  160. "Namecache cannot cache no block!\n");
  161. GNUNET_SCHEDULER_shutdown ();
  162. GNUNET_free (block);
  163. return;
  164. }
  165. nsh = GNUNET_NAMECACHE_connect (cfg);
  166. if (NULL == nsh)
  167. {
  168. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  169. _("Namecache cannot connect to namecache\n"));
  170. GNUNET_SCHEDULER_shutdown ();
  171. GNUNET_free (block);
  172. return;
  173. }
  174. GNUNET_break (NULL != nsh);
  175. nsqe = GNUNET_NAMECACHE_block_cache (nsh,
  176. block,
  177. &cache_cont, (void *) name);
  178. if (NULL == nsqe)
  179. {
  180. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  181. _("Namecache cannot cache no block\n"));
  182. }
  183. GNUNET_free (block);
  184. GNUNET_free ((void *)rd.data);
  185. }
  186. int
  187. main (int argc, char *argv[])
  188. {
  189. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namecache/");
  190. res = 1;
  191. if (0 !=
  192. GNUNET_TESTING_service_run ("test-namecache-api",
  193. "namecache",
  194. "test_namecache_api.conf",
  195. &run,
  196. NULL))
  197. return 1;
  198. return res;
  199. }
  200. /* end of test_namecache_api_cache_block.c */