test_namestore_api_monitoring.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013, 2018 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_monitoring.c
  18. * @brief testcase for zone monitoring functionality: monitor first, then add records
  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. static struct GNUNET_NAMESTORE_Handle * nsh;
  28. static struct GNUNET_SCHEDULER_Task * endbadly_task;
  29. static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
  30. static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
  31. static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
  32. static int res;
  33. static char * s_name_1;
  34. static struct GNUNET_GNSRECORD_Data *s_rd_1;
  35. static char * s_name_2;
  36. static struct GNUNET_GNSRECORD_Data *s_rd_2;
  37. static char * s_name_3;
  38. static struct GNUNET_GNSRECORD_Data *s_rd_3;
  39. struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3];
  40. static void
  41. do_shutdown ()
  42. {
  43. if (NULL != zm)
  44. {
  45. GNUNET_NAMESTORE_zone_monitor_stop (zm);
  46. zm = NULL;
  47. }
  48. if (NULL != ns_ops[0])
  49. {
  50. GNUNET_NAMESTORE_cancel(ns_ops[0]);
  51. ns_ops[0] = NULL;
  52. }
  53. if (NULL != ns_ops[1])
  54. {
  55. GNUNET_NAMESTORE_cancel(ns_ops[1]);
  56. ns_ops[1] = NULL;
  57. }
  58. if (NULL != ns_ops[2])
  59. {
  60. GNUNET_NAMESTORE_cancel(ns_ops[2]);
  61. ns_ops[2] = NULL;
  62. }
  63. if (NULL != nsh)
  64. {
  65. GNUNET_NAMESTORE_disconnect (nsh);
  66. nsh = NULL;
  67. }
  68. GNUNET_free_non_null(s_name_1);
  69. GNUNET_free_non_null(s_name_2);
  70. GNUNET_free_non_null(s_name_3);
  71. if (s_rd_1 != NULL)
  72. {
  73. GNUNET_free ((void *)s_rd_1->data);
  74. GNUNET_free (s_rd_1);
  75. }
  76. if (s_rd_2 != NULL)
  77. {
  78. GNUNET_free ((void *)s_rd_2->data);
  79. GNUNET_free (s_rd_2);
  80. }
  81. if (s_rd_3 != NULL)
  82. {
  83. GNUNET_free ((void *)s_rd_3->data);
  84. GNUNET_free (s_rd_3);
  85. }
  86. if (NULL != privkey)
  87. {
  88. GNUNET_free (privkey);
  89. privkey = NULL;
  90. }
  91. if (NULL != privkey2)
  92. {
  93. GNUNET_free (privkey2);
  94. privkey2 = NULL;
  95. }
  96. }
  97. /**
  98. * Re-establish the connection to the service.
  99. *
  100. * @param cls handle to use to re-connect.
  101. */
  102. static void
  103. endbadly (void *cls)
  104. {
  105. do_shutdown ();
  106. res = 1;
  107. }
  108. static void
  109. end (void *cls)
  110. {
  111. do_shutdown ();
  112. res = 0;
  113. }
  114. static void
  115. zone_proc (void *cls,
  116. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  117. const char *name,
  118. unsigned int rd_count,
  119. const struct GNUNET_GNSRECORD_Data *rd)
  120. {
  121. static int returned_records;
  122. static int fail = GNUNET_NO;
  123. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  124. "Comparing results name %s\n",
  125. name);
  126. if (0 != memcmp (zone_key,
  127. privkey,
  128. sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
  129. {
  130. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  131. "Monitoring returned wrong zone key\n");
  132. GNUNET_break (0);
  133. GNUNET_SCHEDULER_cancel (endbadly_task);
  134. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  135. return;
  136. }
  137. if (0 == strcmp (name, s_name_1))
  138. {
  139. if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
  140. {
  141. GNUNET_break (0);
  142. fail = GNUNET_YES;
  143. }
  144. }
  145. else if (0 == strcmp (name, s_name_2))
  146. {
  147. if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
  148. {
  149. GNUNET_break (0);
  150. fail = GNUNET_YES;
  151. }
  152. }
  153. else
  154. {
  155. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  156. "Invalid name %s\n",
  157. name);
  158. GNUNET_break (0);
  159. fail = GNUNET_YES;
  160. }
  161. GNUNET_NAMESTORE_zone_monitor_next (zm,
  162. 1);
  163. if (2 == ++returned_records)
  164. {
  165. if (endbadly_task != NULL)
  166. {
  167. GNUNET_SCHEDULER_cancel (endbadly_task);
  168. endbadly_task = NULL;
  169. }
  170. if (GNUNET_YES == fail)
  171. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  172. else
  173. GNUNET_SCHEDULER_add_now (&end, NULL);
  174. }
  175. }
  176. static void
  177. put_cont (void *cls,
  178. int32_t success,
  179. const char *emsg)
  180. {
  181. static int c = 0;
  182. char *label = cls;
  183. if (0 == strcmp (label, s_name_1))
  184. ns_ops[0] = NULL;
  185. else if (0 == strcmp (label, s_name_2))
  186. ns_ops[1] = NULL;
  187. else if (0 == strcmp (label, s_name_3))
  188. ns_ops[2] = NULL;
  189. if (success == GNUNET_OK)
  190. {
  191. c++;
  192. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  193. "Created record %u: `%s'\n",
  194. c,
  195. label);
  196. }
  197. else
  198. {
  199. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  200. "Failed to create record `%s'\n",
  201. label);
  202. GNUNET_break (0);
  203. GNUNET_SCHEDULER_cancel (endbadly_task);
  204. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
  205. NULL);
  206. }
  207. }
  208. static struct GNUNET_GNSRECORD_Data *
  209. create_record (unsigned int count)
  210. {
  211. struct GNUNET_GNSRECORD_Data *rd;
  212. rd = GNUNET_new_array (count,
  213. struct GNUNET_GNSRECORD_Data);
  214. for (unsigned int c = 0; c < count; c++)
  215. {
  216. rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
  217. rd[c].record_type = TEST_RECORD_TYPE;
  218. rd[c].data_size = 50;
  219. rd[c].data = GNUNET_malloc(50);
  220. rd[c].flags = 0;
  221. memset ((char *) rd[c].data, 'a', 50);
  222. }
  223. return rd;
  224. }
  225. static void
  226. fail_cb (void *cls)
  227. {
  228. GNUNET_assert (0);
  229. }
  230. static void
  231. sync_cb (void *cls)
  232. {
  233. /* do nothing */
  234. }
  235. static void
  236. run (void *cls,
  237. const struct GNUNET_CONFIGURATION_Handle *cfg,
  238. struct GNUNET_TESTING_Peer *peer)
  239. {
  240. res = 1;
  241. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  242. GNUNET_assert (privkey != NULL);
  243. /* Start monitoring */
  244. zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
  245. privkey,
  246. GNUNET_YES,
  247. &fail_cb,
  248. NULL,
  249. &zone_proc,
  250. NULL,
  251. &sync_cb,
  252. NULL);
  253. if (NULL == zm)
  254. {
  255. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  256. "Failed to create zone monitor\n");
  257. GNUNET_break (0);
  258. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  259. return;
  260. }
  261. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
  262. /* Connect to namestore */
  263. nsh = GNUNET_NAMESTORE_connect (cfg);
  264. if (NULL == nsh)
  265. {
  266. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
  267. GNUNET_break (0);
  268. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  269. return;
  270. }
  271. privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
  272. GNUNET_assert (privkey2 != NULL);
  273. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  274. "Created record 3\n");
  275. /* name in different zone */
  276. GNUNET_asprintf(&s_name_3, "dummy3");
  277. s_rd_3 = create_record(1);
  278. GNUNET_assert (NULL != (ns_ops[2] =
  279. GNUNET_NAMESTORE_records_store (nsh,
  280. privkey2,
  281. s_name_3,
  282. 1,
  283. s_rd_3,
  284. &put_cont,
  285. s_name_3)));
  286. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  287. "Created record 1\n");
  288. GNUNET_asprintf(&s_name_1, "dummy1");
  289. s_rd_1 = create_record(1);
  290. GNUNET_assert (NULL != (ns_ops[0] =
  291. GNUNET_NAMESTORE_records_store (nsh,
  292. privkey,
  293. s_name_1,
  294. 1,
  295. s_rd_1,
  296. &put_cont,
  297. s_name_1)));
  298. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
  299. GNUNET_asprintf(&s_name_2, "dummy2");
  300. s_rd_2 = create_record(1);
  301. GNUNET_assert (NULL != (ns_ops[1] =
  302. GNUNET_NAMESTORE_records_store (nsh,
  303. privkey,
  304. s_name_2,
  305. 1,
  306. s_rd_2,
  307. &put_cont,
  308. s_name_2)));
  309. }
  310. #include "test_common.c"
  311. int
  312. main (int argc,
  313. char *argv[])
  314. {
  315. const char *plugin_name;
  316. char *cfg_name;
  317. SETUP_CFG (plugin_name, cfg_name);
  318. res = 1;
  319. if (0 !=
  320. GNUNET_TESTING_peer_run ("test-namestore-api-monitoring",
  321. cfg_name,
  322. &run,
  323. NULL))
  324. {
  325. res = 1;
  326. }
  327. GNUNET_DISK_purge_cfg_dir (cfg_name,
  328. "GNUNET_TEST_HOME");
  329. GNUNET_free (cfg_name);
  330. return res;
  331. }
  332. /* end of test_namestore_api_monitoring.c */