test_namestore_api_zone_iteration_nick.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 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_iteration.c
  18. * @brief testcase for zone iteration functionality: iterate all zones
  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 ZONE_NICK_1 "nick1"
  27. #define ZONE_NICK_2 "nick2"
  28. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
  29. static struct GNUNET_NAMESTORE_Handle * nsh;
  30. static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
  31. static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
  32. static struct GNUNET_NAMESTORE_ZoneIterator *zi;
  33. static int res;
  34. static int returned_records;
  35. static char * s_name_1;
  36. static struct GNUNET_GNSRECORD_Data *s_rd_1;
  37. static char * s_name_2;
  38. static struct GNUNET_GNSRECORD_Data *s_rd_2;
  39. static char * s_name_3;
  40. static struct GNUNET_GNSRECORD_Data *s_rd_3;
  41. static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
  42. /**
  43. * Re-establish the connection to the service.
  44. *
  45. * @param cls handle to use to re-connect.
  46. * @param tc scheduler context
  47. */
  48. static void
  49. end (void *cls)
  50. {
  51. if (NULL != zi)
  52. {
  53. GNUNET_NAMESTORE_zone_iteration_stop (zi);
  54. zi = NULL;
  55. }
  56. if (nsh != NULL)
  57. {
  58. GNUNET_NAMESTORE_disconnect (nsh);
  59. nsh = NULL;
  60. }
  61. GNUNET_free_non_null (s_name_1);
  62. GNUNET_free_non_null (s_name_2);
  63. GNUNET_free_non_null (s_name_3);
  64. if (s_rd_1 != NULL)
  65. {
  66. GNUNET_free ((void *)s_rd_1->data);
  67. GNUNET_free (s_rd_1);
  68. }
  69. if (s_rd_2 != NULL)
  70. {
  71. GNUNET_free ((void *)s_rd_2->data);
  72. GNUNET_free (s_rd_2);
  73. }
  74. if (s_rd_3 != NULL)
  75. {
  76. GNUNET_free ((void *)s_rd_3->data);
  77. GNUNET_free (s_rd_3);
  78. }
  79. if (privkey != NULL)
  80. {
  81. GNUNET_free (privkey);
  82. privkey = NULL;
  83. }
  84. if (privkey2 != NULL)
  85. {
  86. GNUNET_free (privkey2);
  87. privkey2 = NULL;
  88. }
  89. }
  90. static int
  91. check_zone_1 (const char *label, unsigned int rd_count,
  92. const struct GNUNET_GNSRECORD_Data *rd)
  93. {
  94. for (unsigned int c = 0; c< rd_count ; c++)
  95. {
  96. if ( (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK) &&
  97. (0 != strcmp (rd[c].data, ZONE_NICK_1)) )
  98. {
  99. GNUNET_break (0);
  100. return GNUNET_YES;
  101. }
  102. }
  103. return GNUNET_NO;
  104. }
  105. static int
  106. check_zone_2 (const char *label,
  107. unsigned int rd_count,
  108. const struct GNUNET_GNSRECORD_Data *rd)
  109. {
  110. for (unsigned int c = 0; c< rd_count ; c++)
  111. {
  112. if ( (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK) &&
  113. (0 != strcmp (rd[c].data, ZONE_NICK_2)) )
  114. {
  115. GNUNET_break (0);
  116. return GNUNET_YES;
  117. }
  118. }
  119. return GNUNET_NO;
  120. }
  121. static void
  122. zone_proc_end (void *cls)
  123. {
  124. zi = NULL;
  125. res = 0;
  126. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  127. "Received last result, iteration done after receing %u results\n",
  128. returned_records);
  129. GNUNET_SCHEDULER_shutdown ();
  130. }
  131. static void
  132. zone_proc (void *cls,
  133. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  134. const char *label,
  135. unsigned int rd_count,
  136. const struct GNUNET_GNSRECORD_Data *rd)
  137. {
  138. int failed = GNUNET_NO;
  139. GNUNET_assert (NULL != zone);
  140. if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
  141. {
  142. failed = check_zone_1 (label, rd_count, rd);
  143. if (GNUNET_YES == failed)
  144. GNUNET_break (0);
  145. }
  146. else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
  147. {
  148. failed = check_zone_2 (label, rd_count, rd);
  149. if (GNUNET_YES == failed)
  150. GNUNET_break (0);
  151. }
  152. else
  153. {
  154. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  155. "Received invalid zone\n");
  156. failed = GNUNET_YES;
  157. GNUNET_break (0);
  158. }
  159. if (failed == GNUNET_NO)
  160. {
  161. returned_records ++;
  162. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  163. "Telling namestore to send the next result\n");
  164. GNUNET_NAMESTORE_zone_iterator_next (zi,
  165. 1);
  166. }
  167. else
  168. {
  169. GNUNET_break (0);
  170. res = 1;
  171. GNUNET_SCHEDULER_shutdown ();
  172. }
  173. }
  174. static void
  175. fail_cb (void *cls)
  176. {
  177. GNUNET_assert (0);
  178. }
  179. static void
  180. put_cont (void *cls,
  181. int32_t success,
  182. const char *emsg)
  183. {
  184. static int c = 0;
  185. if (success == GNUNET_OK)
  186. {
  187. c++;
  188. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
  189. }
  190. else
  191. {
  192. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
  193. emsg);
  194. GNUNET_break (0);
  195. GNUNET_SCHEDULER_shutdown ();
  196. return;
  197. }
  198. if (c == 3)
  199. {
  200. res = 1;
  201. returned_records = 0;
  202. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
  203. zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
  204. NULL,
  205. &fail_cb,
  206. NULL,
  207. &zone_proc,
  208. NULL,
  209. &zone_proc_end,
  210. NULL);
  211. if (zi == NULL)
  212. {
  213. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
  214. GNUNET_break (0);
  215. GNUNET_SCHEDULER_shutdown ();
  216. return;
  217. }
  218. }
  219. }
  220. static struct GNUNET_GNSRECORD_Data *
  221. create_record (unsigned int count)
  222. {
  223. struct GNUNET_GNSRECORD_Data * rd;
  224. rd = GNUNET_new_array (count,
  225. struct GNUNET_GNSRECORD_Data);
  226. for (unsigned int c = 0; c < count; c++)
  227. {
  228. rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
  229. rd[c].record_type = TEST_RECORD_TYPE;
  230. rd[c].data_size = 50;
  231. rd[c].data = GNUNET_malloc(50);
  232. rd[c].flags = 0;
  233. memset ((char *) rd[c].data, 'a', 50);
  234. }
  235. return rd;
  236. }
  237. static void
  238. nick_2_cont (void *cls,
  239. int32_t success,
  240. const char *emsg)
  241. {
  242. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  243. "Nick added : %s\n",
  244. (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
  245. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
  246. GNUNET_asprintf(&s_name_1, "dummy1");
  247. s_rd_1 = create_record(1);
  248. GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
  249. 1, s_rd_1,
  250. &put_cont, NULL);
  251. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  252. "Created record 2 \n");
  253. GNUNET_asprintf(&s_name_2, "dummy2");
  254. s_rd_2 = create_record(1);
  255. GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2,
  256. 1, s_rd_2, &put_cont, NULL);
  257. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  258. "Created record 3\n");
  259. /* name in different zone */
  260. GNUNET_asprintf(&s_name_3, "dummy3");
  261. s_rd_3 = create_record(1);
  262. GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
  263. 1, s_rd_3,
  264. &put_cont, NULL);
  265. }
  266. static void
  267. nick_1_cont (void *cls, int32_t success, const char *emsg)
  268. {
  269. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  270. "Nick 1 added : %s\n",
  271. (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
  272. nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey2, ZONE_NICK_2, &nick_2_cont, &privkey2);
  273. if (NULL == nsqe)
  274. {
  275. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  276. _("Namestore cannot store no block\n"));
  277. }
  278. }
  279. /**
  280. * Callback called from the zone iterator when we iterate over
  281. * the empty zone. Check that we got no records and then
  282. * start the actual tests by filling the zone.
  283. */
  284. static void
  285. empty_zone_proc (void *cls,
  286. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  287. const char *label,
  288. unsigned int rd_count,
  289. const struct GNUNET_GNSRECORD_Data *rd)
  290. {
  291. GNUNET_assert (nsh == cls);
  292. if (NULL != zone)
  293. {
  294. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  295. _("Expected empty zone but received zone private key\n"));
  296. GNUNET_break (0);
  297. GNUNET_SCHEDULER_shutdown ();
  298. return;
  299. }
  300. if ((NULL != label) || (NULL != rd) || (0 != rd_count))
  301. {
  302. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  303. _("Expected no zone content but received data\n"));
  304. GNUNET_break (0);
  305. GNUNET_SCHEDULER_shutdown ();
  306. return;
  307. }
  308. GNUNET_assert (0);
  309. }
  310. static void
  311. empty_zone_end (void *cls)
  312. {
  313. GNUNET_assert (nsh == cls);
  314. zi = NULL;
  315. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  316. GNUNET_assert (privkey != NULL);
  317. privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
  318. GNUNET_assert (privkey2 != NULL);
  319. nsqe = GNUNET_NAMESTORE_set_nick (nsh,
  320. privkey,
  321. ZONE_NICK_1,
  322. &nick_1_cont,
  323. &privkey);
  324. if (NULL == nsqe)
  325. {
  326. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  327. _("Namestore cannot store no block\n"));
  328. }
  329. }
  330. static void
  331. run (void *cls,
  332. const struct GNUNET_CONFIGURATION_Handle *cfg,
  333. struct GNUNET_TESTING_Peer *peer)
  334. {
  335. nsh = GNUNET_NAMESTORE_connect (cfg);
  336. GNUNET_break (NULL != nsh);
  337. GNUNET_SCHEDULER_add_shutdown (&end,
  338. NULL);
  339. /* first, iterate over empty namestore */
  340. zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
  341. NULL,
  342. &fail_cb,
  343. NULL,
  344. &empty_zone_proc,
  345. nsh,
  346. &empty_zone_end,
  347. nsh);
  348. if (NULL == zi)
  349. {
  350. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  351. "Failed to create zone iterator\n");
  352. GNUNET_break (0);
  353. GNUNET_SCHEDULER_shutdown ();
  354. }
  355. }
  356. #include "test_common.c"
  357. int
  358. main (int argc, char *argv[])
  359. {
  360. const char *plugin_name;
  361. char *cfg_name;
  362. SETUP_CFG (plugin_name, cfg_name);
  363. res = 1;
  364. if (0 !=
  365. GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-nick",
  366. cfg_name,
  367. &run,
  368. NULL))
  369. {
  370. res = 1;
  371. }
  372. GNUNET_DISK_purge_cfg_dir (cfg_name,
  373. "GNUNET_TEST_HOME");
  374. GNUNET_free (cfg_name);
  375. return res;
  376. }
  377. /* end of test_namestore_api_zone_iteration.c */