test_namecache_api_cache_block.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2012 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file namecache/test_namecache_api.c
  19. * @brief testcase for namecache_api.c: store a record and perform a lookup
  20. */
  21. #include "platform.h"
  22. #include "gnunet_namecache_service.h"
  23. #include "gnunet_testing_lib.h"
  24. #define TEST_RECORD_TYPE 1234
  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 GNUNET_SCHEDULER_TaskIdentifier 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. * @param tc scheduler context
  54. */
  55. static void
  56. endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  57. {
  58. if (NULL != nsqe)
  59. {
  60. GNUNET_NAMECACHE_cancel (nsqe);
  61. nsqe = NULL;
  62. }
  63. cleanup ();
  64. res = 1;
  65. }
  66. static void
  67. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  68. {
  69. cleanup ();
  70. res = 0;
  71. }
  72. static void
  73. rd_decrypt_cb (void *cls,
  74. unsigned int rd_count,
  75. const struct GNUNET_GNSRECORD_Data *rd)
  76. {
  77. char rd_cmp_data[TEST_RECORD_DATALEN];
  78. GNUNET_assert (1 == rd_count);
  79. GNUNET_assert (NULL != rd);
  80. memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
  81. GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
  82. GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
  83. GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
  84. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  85. "Block was decrypted successfully \n");
  86. GNUNET_SCHEDULER_add_now (&end, NULL);
  87. }
  88. static void
  89. name_lookup_proc (void *cls,
  90. const struct GNUNET_GNSRECORD_Block *block)
  91. {
  92. const char *name = cls;
  93. nsqe = NULL;
  94. GNUNET_assert (NULL != cls);
  95. if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
  96. {
  97. GNUNET_SCHEDULER_cancel (endbadly_task);
  98. endbadly_task = GNUNET_SCHEDULER_NO_TASK;
  99. }
  100. if (NULL == block)
  101. {
  102. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  103. _("Namecache returned no block\n"));
  104. if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
  105. GNUNET_SCHEDULER_cancel (endbadly_task);
  106. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  107. return;
  108. }
  109. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  110. "Namecache returned block, decrypting \n");
  111. GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block,
  112. &pubkey, name, &rd_decrypt_cb, (void *) name));
  113. }
  114. static void
  115. cache_cont (void *cls, int32_t success, const char *emsg)
  116. {
  117. const char *name = cls;
  118. struct GNUNET_HashCode derived_hash;
  119. GNUNET_assert (NULL != cls);
  120. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  121. "Name store cached record for `%s': %s\n",
  122. name,
  123. (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
  124. /* Create derived hash */
  125. GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
  126. nsqe = GNUNET_NAMECACHE_lookup_block (nsh, &derived_hash,
  127. &name_lookup_proc, (void *) name);
  128. }
  129. static void
  130. run (void *cls,
  131. const struct GNUNET_CONFIGURATION_Handle *cfg,
  132. struct GNUNET_TESTING_Peer *peer)
  133. {
  134. struct GNUNET_GNSRECORD_Data rd;
  135. struct GNUNET_GNSRECORD_Block *block;
  136. char *hostkey_file;
  137. const char * name = "dummy.dummy.gnunet";
  138. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  139. &endbadly, NULL);
  140. GNUNET_asprintf (&hostkey_file,
  141. "zonefiles%s%s",
  142. DIR_SEPARATOR_STR,
  143. "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
  144. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
  145. privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
  146. GNUNET_free (hostkey_file);
  147. GNUNET_assert (privkey != NULL);
  148. GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
  149. rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 10000000000;
  150. rd.record_type = TEST_RECORD_TYPE;
  151. rd.data_size = TEST_RECORD_DATALEN;
  152. rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
  153. rd.flags = 0;
  154. memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
  155. block = GNUNET_GNSRECORD_block_create (privkey,
  156. GNUNET_TIME_UNIT_FOREVER_ABS,
  157. name, &rd, 1);
  158. if (NULL == block)
  159. {
  160. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  161. "Namecache cannot cache no block!\n");
  162. GNUNET_SCHEDULER_shutdown ();
  163. GNUNET_free (block);
  164. return;
  165. }
  166. nsh = GNUNET_NAMECACHE_connect (cfg);
  167. if (NULL == nsh)
  168. {
  169. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  170. _("Namecache cannot connect to namecache\n"));
  171. GNUNET_SCHEDULER_shutdown ();
  172. GNUNET_free (block);
  173. return;
  174. }
  175. GNUNET_break (NULL != nsh);
  176. nsqe = GNUNET_NAMECACHE_block_cache (nsh,
  177. block,
  178. &cache_cont, (void *) name);
  179. if (NULL == nsqe)
  180. {
  181. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  182. _("Namecache cannot cache no block\n"));
  183. }
  184. GNUNET_free (block);
  185. GNUNET_free ((void *)rd.data);
  186. }
  187. int
  188. main (int argc, char *argv[])
  189. {
  190. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namecache/");
  191. res = 1;
  192. if (0 !=
  193. GNUNET_TESTING_service_run ("test-namecache-api",
  194. "namecache",
  195. "test_namecache_api.conf",
  196. &run,
  197. NULL))
  198. return 1;
  199. return res;
  200. }
  201. /* end of test_namecache_api_cache_block.c */