test_namestore_api_lookup_nick.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_nick.c
  18. * @brief testcase for namestore_api.c: NICK records
  19. */
  20. #include "platform.h"
  21. #include "gnunet_namestore_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_NICK "gnunettestnick"
  27. #define TEST_RECORD_DATA 'a'
  28. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  29. static struct GNUNET_NAMESTORE_Handle *nsh;
  30. static struct GNUNET_SCHEDULER_Task * endbadly_task;
  31. static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
  32. static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  33. static int res;
  34. static struct GNUNET_GNSRECORD_Data rd_orig;
  35. static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
  36. //static const char * name = "dummy.dummy.gnunet";
  37. static const char * name = "d";
  38. static void
  39. cleanup ()
  40. {
  41. GNUNET_free_non_null ((void *)rd_orig.data);
  42. if (NULL != nsh)
  43. {
  44. GNUNET_NAMESTORE_disconnect (nsh);
  45. nsh = NULL;
  46. }
  47. if (NULL != privkey)
  48. {
  49. GNUNET_free (privkey);
  50. privkey = NULL;
  51. }
  52. GNUNET_SCHEDULER_shutdown ();
  53. }
  54. /**
  55. * Re-establish the connection to the service.
  56. *
  57. * @param cls handle to use to re-connect.
  58. * @param tc scheduler context
  59. */
  60. static void
  61. endbadly (void *cls)
  62. {
  63. if (NULL != nsqe)
  64. {
  65. GNUNET_NAMESTORE_cancel (nsqe);
  66. nsqe = NULL;
  67. }
  68. cleanup ();
  69. res = 1;
  70. }
  71. static void
  72. end (void *cls)
  73. {
  74. cleanup ();
  75. res = 0;
  76. }
  77. static void
  78. lookup_it (void *cls,
  79. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  80. const char *label,
  81. unsigned int rd_count,
  82. const struct GNUNET_GNSRECORD_Data *rd)
  83. {
  84. nsqe = NULL;
  85. int c;
  86. int found_record = GNUNET_NO;
  87. int found_nick = GNUNET_NO;
  88. if (0 != memcmp(privkey, zone, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
  89. {
  90. GNUNET_break(0);
  91. GNUNET_SCHEDULER_cancel (endbadly_task);
  92. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  93. return;
  94. }
  95. if (NULL == label)
  96. {
  97. GNUNET_break(0);
  98. GNUNET_SCHEDULER_cancel (endbadly_task);
  99. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  100. return;
  101. }
  102. if (0 != strcmp (label, name))
  103. {
  104. GNUNET_break(0);
  105. GNUNET_SCHEDULER_cancel (endbadly_task);
  106. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  107. return;
  108. }
  109. if (2 != rd_count)
  110. {
  111. GNUNET_break(0);
  112. GNUNET_SCHEDULER_cancel (endbadly_task);
  113. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  114. return;
  115. }
  116. for (c = 0; c < rd_count; c++)
  117. {
  118. if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
  119. {
  120. if (rd[c].data_size != strlen(TEST_NICK)+1)
  121. {
  122. GNUNET_break(0);
  123. GNUNET_SCHEDULER_cancel (endbadly_task);
  124. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  125. return;
  126. }
  127. if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_PRIVATE))
  128. {
  129. GNUNET_break(0);
  130. GNUNET_SCHEDULER_cancel (endbadly_task);
  131. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  132. return;
  133. }
  134. if (0 != strcmp(rd[c].data, TEST_NICK))
  135. {
  136. GNUNET_SCHEDULER_cancel (endbadly_task);
  137. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  138. return;
  139. }
  140. found_nick = GNUNET_YES;
  141. }
  142. else
  143. {
  144. if (rd[c].record_type != TEST_RECORD_TYPE)
  145. {
  146. GNUNET_break(0);
  147. GNUNET_SCHEDULER_cancel (endbadly_task);
  148. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  149. return;
  150. }
  151. if (rd[c].data_size != TEST_RECORD_DATALEN)
  152. {
  153. GNUNET_break(0);
  154. GNUNET_SCHEDULER_cancel (endbadly_task);
  155. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  156. return;
  157. }
  158. if (0 != memcmp (rd[c].data, rd_orig.data, TEST_RECORD_DATALEN))
  159. {
  160. GNUNET_break(0);
  161. GNUNET_SCHEDULER_cancel (endbadly_task);
  162. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  163. return;
  164. }
  165. if (rd[c].flags != rd->flags)
  166. {
  167. GNUNET_break(0);
  168. GNUNET_SCHEDULER_cancel (endbadly_task);
  169. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  170. return;
  171. }
  172. found_record = GNUNET_YES;
  173. }
  174. }
  175. /* Done */
  176. if ((GNUNET_YES == found_nick) && (GNUNET_YES == found_record))
  177. {
  178. GNUNET_SCHEDULER_cancel (endbadly_task);
  179. endbadly_task = NULL;
  180. GNUNET_SCHEDULER_add_now (&end, NULL );
  181. }
  182. else
  183. {
  184. GNUNET_break (0);
  185. GNUNET_SCHEDULER_cancel (endbadly_task);
  186. endbadly_task = NULL;
  187. GNUNET_SCHEDULER_add_now (&endbadly, NULL );
  188. }
  189. }
  190. static void
  191. fail_cb (void *cls)
  192. {
  193. GNUNET_assert (0);
  194. }
  195. static void
  196. put_cont (void *cls, int32_t success, const char *emsg)
  197. {
  198. const char *name = cls;
  199. nsqe = NULL;
  200. GNUNET_assert (NULL != cls);
  201. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  202. "Name store added record for `%s': %s\n",
  203. name,
  204. (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
  205. if (GNUNET_OK != success)
  206. {
  207. GNUNET_SCHEDULER_cancel (endbadly_task);
  208. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  209. return;
  210. }
  211. /* Lookup */
  212. nsqe = GNUNET_NAMESTORE_records_lookup (nsh,
  213. privkey,
  214. name,
  215. &fail_cb,
  216. NULL,
  217. &lookup_it,
  218. NULL);
  219. }
  220. static void
  221. nick_cont (void *cls, int32_t success, const char *emsg)
  222. {
  223. const char *name = cls;
  224. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  225. "Nick added : %s\n",
  226. (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
  227. rd_orig.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
  228. rd_orig.record_type = TEST_RECORD_TYPE;
  229. rd_orig.data_size = TEST_RECORD_DATALEN;
  230. rd_orig.data = GNUNET_malloc (TEST_RECORD_DATALEN);
  231. rd_orig.flags = 0;
  232. memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
  233. nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
  234. 1, &rd_orig, &put_cont, (void *) name);
  235. }
  236. static void
  237. run (void *cls,
  238. const struct GNUNET_CONFIGURATION_Handle *cfg,
  239. struct GNUNET_TESTING_Peer *peer)
  240. {
  241. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  242. &endbadly,
  243. NULL);
  244. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  245. GNUNET_assert (privkey != NULL);
  246. GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
  247. &pubkey);
  248. nsh = GNUNET_NAMESTORE_connect (cfg);
  249. GNUNET_break (NULL != nsh);
  250. nsqe = GNUNET_NAMESTORE_set_nick (nsh,
  251. privkey,
  252. TEST_NICK,
  253. &nick_cont,
  254. (void *) name);
  255. if (NULL == nsqe)
  256. {
  257. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  258. _("Namestore cannot store no block\n"));
  259. }
  260. }
  261. #include "test_common.c"
  262. int
  263. main (int argc, char *argv[])
  264. {
  265. const char *plugin_name;
  266. char *cfg_name;
  267. SETUP_CFG (plugin_name, cfg_name);
  268. res = 1;
  269. if (0 !=
  270. GNUNET_TESTING_peer_run ("test-namestore-api-lookup-nick",
  271. cfg_name,
  272. &run,
  273. NULL))
  274. {
  275. res = 1;
  276. }
  277. GNUNET_DISK_purge_cfg_dir (cfg_name,
  278. "GNUNET_TEST_HOME");
  279. GNUNET_free (cfg_name);
  280. return res;
  281. }
  282. /* end of test_namestore_api_store.c */