test_namestore_api_monitoring_existing.c 9.6 KB

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