test_namestore_api_zone_iteration_stop.c 12 KB

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