test_namestore_api_zone_to_name.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009 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_zone_to_name.c
  18. * @brief testcase for zone to name translation
  19. */
  20. #include "platform.h"
  21. #include "gnunet_namestore_service.h"
  22. #include "gnunet_testing_lib.h"
  23. #include "namestore.h"
  24. #include "gnunet_dnsparser_lib.h"
  25. #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
  26. #define RECORDS 5
  27. #define TEST_RECORD_DATALEN 123
  28. #define TEST_RECORD_DATA 'a'
  29. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
  30. static struct GNUNET_NAMESTORE_Handle *nsh;
  31. static struct GNUNET_SCHEDULER_Task *endbadly_task;
  32. static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
  33. static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  34. static struct GNUNET_CRYPTO_EcdsaPublicKey s_zone_value;
  35. static char * s_name;
  36. static int res;
  37. static struct GNUNET_NAMESTORE_QueueEntry *qe;
  38. /**
  39. * Re-establish the connection to the service.
  40. *
  41. * @param cls handle to use to re-connect.
  42. */
  43. static void
  44. endbadly (void *cls)
  45. {
  46. (void) cls;
  47. GNUNET_SCHEDULER_shutdown ();
  48. res = 1;
  49. }
  50. static void
  51. end (void *cls)
  52. {
  53. if (NULL != qe)
  54. {
  55. GNUNET_NAMESTORE_cancel (qe);
  56. qe = NULL;
  57. }
  58. if (NULL != endbadly_task)
  59. {
  60. GNUNET_SCHEDULER_cancel (endbadly_task);
  61. endbadly_task = NULL;
  62. }
  63. if (NULL != privkey)
  64. {
  65. GNUNET_free (privkey);
  66. privkey = NULL;
  67. }
  68. if (NULL != nsh)
  69. {
  70. GNUNET_NAMESTORE_disconnect (nsh);
  71. nsh = NULL;
  72. }
  73. }
  74. static void
  75. zone_to_name_proc (void *cls,
  76. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  77. const char *n,
  78. unsigned int rd_count,
  79. const struct GNUNET_GNSRECORD_Data *rd)
  80. {
  81. int fail = GNUNET_NO;
  82. qe = NULL;
  83. if ( (NULL == zone_key) &&
  84. (NULL == n) &&
  85. (0 == rd_count) &&
  86. (NULL == rd) )
  87. {
  88. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  89. "No result found\n");
  90. res = 1;
  91. }
  92. else
  93. {
  94. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  95. "Result found: `%s'\n",
  96. n);
  97. if ( (NULL == n) ||
  98. (0 != strcmp (n,
  99. s_name)))
  100. {
  101. fail = GNUNET_YES;
  102. GNUNET_break (0);
  103. }
  104. if (1 != rd_count)
  105. {
  106. fail = GNUNET_YES;
  107. GNUNET_break (0);
  108. }
  109. if ( (NULL == zone_key) ||
  110. (0 != GNUNET_memcmp (zone_key,
  111. privkey)))
  112. {
  113. fail = GNUNET_YES;
  114. GNUNET_break (0);
  115. }
  116. if (fail == GNUNET_NO)
  117. res = 0;
  118. else
  119. res = 1;
  120. }
  121. GNUNET_SCHEDULER_add_now (&end,
  122. NULL);
  123. }
  124. static void
  125. error_cb (void *cls)
  126. {
  127. (void) cls;
  128. qe = NULL;
  129. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  130. "Not found!\n");
  131. GNUNET_SCHEDULER_shutdown ();
  132. res = 2;
  133. }
  134. static void
  135. put_cont (void *cls,
  136. int32_t success,
  137. const char *emsg)
  138. {
  139. char *name = cls;
  140. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  141. "Name store added record for `%s': %s\n",
  142. name,
  143. (success == GNUNET_OK) ? "SUCCESS" : emsg);
  144. if (success == GNUNET_OK)
  145. {
  146. res = 0;
  147. qe = GNUNET_NAMESTORE_zone_to_name (nsh,
  148. privkey,
  149. &s_zone_value,
  150. &error_cb,
  151. NULL,
  152. &zone_to_name_proc,
  153. NULL);
  154. }
  155. else
  156. {
  157. res = 1;
  158. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  159. "Failed to put records for name `%s'\n",
  160. name);
  161. GNUNET_SCHEDULER_add_now (&end,
  162. NULL);
  163. }
  164. }
  165. static void
  166. run (void *cls,
  167. const struct GNUNET_CONFIGURATION_Handle *cfg,
  168. struct GNUNET_TESTING_Peer *peer)
  169. {
  170. (void) cls;
  171. (void) peer;
  172. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  173. &endbadly,
  174. NULL);
  175. GNUNET_SCHEDULER_add_shutdown (&end,
  176. NULL);
  177. GNUNET_asprintf (&s_name, "dummy");
  178. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  179. GNUNET_assert (NULL != privkey);
  180. /* get public key */
  181. GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
  182. &pubkey);
  183. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
  184. &s_zone_value,
  185. sizeof (s_zone_value));
  186. {
  187. struct GNUNET_GNSRECORD_Data rd;
  188. rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
  189. rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
  190. rd.data_size = sizeof (s_zone_value);
  191. rd.data = &s_zone_value;
  192. rd.flags = 0;
  193. nsh = GNUNET_NAMESTORE_connect (cfg);
  194. GNUNET_break (NULL != nsh);
  195. GNUNET_NAMESTORE_records_store (nsh,
  196. privkey,
  197. s_name,
  198. 1,
  199. &rd,
  200. &put_cont,
  201. NULL);
  202. }
  203. }
  204. #include "test_common.c"
  205. int
  206. main (int argc,
  207. char *argv[])
  208. {
  209. const char *plugin_name;
  210. char *cfg_name;
  211. (void) argc;
  212. SETUP_CFG (plugin_name, cfg_name);
  213. res = 1;
  214. if (0 !=
  215. GNUNET_TESTING_peer_run ("test-namestore-api-zone-to-name",
  216. cfg_name,
  217. &run,
  218. NULL))
  219. {
  220. res = 1;
  221. }
  222. GNUNET_DISK_purge_cfg_dir (cfg_name,
  223. "GNUNET_TEST_HOME");
  224. GNUNET_free (cfg_name);
  225. return res;
  226. }
  227. /* end of test_namestore_api_zone_to_name.c */