test_namestore_api_lookup_shadow_filter.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_shadow_filter.c
  18. * @brief testcase for namestore_api.c: store a record with short expiration
  19. * and a shadow record, perform lookup:
  20. * - when active record is valid, expect only active record
  21. * - when active record is expired, expect shadow record only
  22. */
  23. #include "platform.h"
  24. #include "gnunet_namecache_service.h"
  25. #include "gnunet_namestore_service.h"
  26. #include "gnunet_testing_lib.h"
  27. #include "gnunet_dnsparser_lib.h"
  28. #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
  29. #define TEST_NAME "dummy.dummy.gnunet"
  30. #define TEST_RECORD_DATALEN 123
  31. #define TEST_RECORD_DATA 'a'
  32. #define TEST_SHADOW_RECORD_DATA 'b'
  33. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
  34. #define EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  35. static struct GNUNET_NAMESTORE_Handle *nsh;
  36. static struct GNUNET_NAMECACHE_Handle *nch;
  37. static struct GNUNET_SCHEDULER_Task *endbadly_task;
  38. static struct GNUNET_SCHEDULER_Task *delayed_lookup_task;
  39. static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
  40. static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  41. static int res;
  42. static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
  43. static struct GNUNET_NAMECACHE_QueueEntry *ncqe;
  44. static struct GNUNET_NAMECACHE_QueueEntry *ncqe_shadow;
  45. static struct GNUNET_GNSRECORD_Data records[2];
  46. static struct GNUNET_TIME_Absolute record_expiration;
  47. static struct GNUNET_HashCode derived_hash;
  48. static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  49. static void
  50. cleanup ()
  51. {
  52. if (NULL != nsh)
  53. {
  54. GNUNET_NAMESTORE_disconnect (nsh);
  55. nsh = NULL;
  56. }
  57. if (NULL != nch)
  58. {
  59. GNUNET_NAMECACHE_disconnect (nch);
  60. nch = NULL;
  61. }
  62. if (NULL != privkey)
  63. {
  64. GNUNET_free (privkey);
  65. privkey = NULL;
  66. }
  67. GNUNET_SCHEDULER_shutdown ();
  68. }
  69. /**
  70. * Re-establish the connection to the service.
  71. *
  72. * @param cls handle to use to re-connect.
  73. */
  74. static void
  75. endbadly (void *cls)
  76. {
  77. if (NULL != delayed_lookup_task)
  78. {
  79. GNUNET_SCHEDULER_cancel (delayed_lookup_task);
  80. delayed_lookup_task = NULL;
  81. }
  82. if (NULL != nsqe)
  83. {
  84. GNUNET_NAMESTORE_cancel (nsqe);
  85. nsqe = NULL;
  86. }
  87. if (NULL != ncqe)
  88. {
  89. GNUNET_NAMECACHE_cancel (ncqe);
  90. ncqe = NULL;
  91. }
  92. cleanup ();
  93. res = 1;
  94. }
  95. static void
  96. end (void *cls)
  97. {
  98. cleanup ();
  99. res = 0;
  100. }
  101. static void
  102. rd_decrypt_cb (void *cls,
  103. unsigned int rd_count,
  104. const struct GNUNET_GNSRECORD_Data *rd)
  105. {
  106. struct GNUNET_GNSRECORD_Data *expected_rd = cls;
  107. char rd_cmp_data[TEST_RECORD_DATALEN];
  108. if (1 != rd_count)
  109. {
  110. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  111. GNUNET_break (0);
  112. return;
  113. }
  114. if (NULL == rd)
  115. {
  116. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  117. GNUNET_break (0);
  118. return;
  119. }
  120. if (expected_rd == &records[0])
  121. {
  122. /* Expecting active record */
  123. memset (rd_cmp_data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
  124. if (TEST_RECORD_TYPE != rd[0].record_type)
  125. {
  126. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  127. GNUNET_break (0);
  128. return;
  129. }
  130. if (TEST_RECORD_DATALEN != rd[0].data_size)
  131. {
  132. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  133. GNUNET_break (0);
  134. return;
  135. }
  136. if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
  137. {
  138. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  139. GNUNET_break (0);
  140. return;
  141. }
  142. if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
  143. {
  144. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  145. GNUNET_break (0);
  146. return;
  147. }
  148. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  149. "Block was decrypted successfully with active record\n");
  150. }
  151. if (expected_rd == &records[1])
  152. {
  153. /* Expecting shadow record but without shadow flag*/
  154. memset (rd_cmp_data, TEST_SHADOW_RECORD_DATA, TEST_RECORD_DATALEN);
  155. if (TEST_RECORD_TYPE != rd[0].record_type)
  156. {
  157. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  158. GNUNET_break (0);
  159. return;
  160. }
  161. if (TEST_RECORD_DATALEN != rd[0].data_size)
  162. {
  163. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  164. GNUNET_break (0);
  165. return;
  166. }
  167. if (0 != memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN))
  168. {
  169. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  170. GNUNET_break (0);
  171. return;
  172. }
  173. if (0 != (GNUNET_GNSRECORD_RF_SHADOW_RECORD & rd[0].flags))
  174. {
  175. GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  176. GNUNET_break (0);
  177. return;
  178. }
  179. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  180. "Block was decrypted successfully with former shadow record \n");
  181. GNUNET_SCHEDULER_add_now (&end, NULL);
  182. }
  183. }
  184. static void
  185. name_lookup_active_proc (void *cls,
  186. const struct GNUNET_GNSRECORD_Block *block)
  187. {
  188. struct GNUNET_GNSRECORD_Data *expected_rd = cls;
  189. GNUNET_assert (NULL != expected_rd);
  190. ncqe = NULL;
  191. ncqe_shadow = NULL;
  192. if (endbadly_task != NULL)
  193. {
  194. GNUNET_SCHEDULER_cancel (endbadly_task);
  195. endbadly_task = NULL;
  196. }
  197. if (NULL == block)
  198. {
  199. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  200. _ ("Namestore returned no block\n"));
  201. if (endbadly_task != NULL)
  202. GNUNET_SCHEDULER_cancel (endbadly_task);
  203. endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
  204. return;
  205. }
  206. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  207. "Namestore returned block, decrypting \n");
  208. GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt (block,
  209. &pubkey,
  210. TEST_NAME,
  211. &rd_decrypt_cb,
  212. expected_rd));
  213. }
  214. static void
  215. name_lookup_shadow (void *cls)
  216. {
  217. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  218. "Performing lookup for shadow record \n");
  219. delayed_lookup_task = NULL;
  220. ncqe_shadow = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
  221. &name_lookup_active_proc,
  222. &records[1]);
  223. }
  224. static void
  225. put_cont (void *cls, int32_t success, const char *emsg)
  226. {
  227. nsqe = NULL;
  228. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  229. "Name store added record for `%s': %s\n",
  230. TEST_NAME,
  231. (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
  232. /* Create derived hash */
  233. GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
  234. GNUNET_GNSRECORD_query_from_public_key (&pubkey, TEST_NAME, &derived_hash);
  235. if (0 == GNUNET_TIME_absolute_get_remaining (record_expiration).rel_value_us)
  236. {
  237. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  238. "Test to too long to store records, cannot run test!\n");
  239. GNUNET_SCHEDULER_add_now (&end, NULL);
  240. return;
  241. }
  242. /* Lookup active record now */
  243. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  244. "Performing lookup for active record \n");
  245. ncqe = GNUNET_NAMECACHE_lookup_block (nch, &derived_hash,
  246. &name_lookup_active_proc, &records[0]);
  247. delayed_lookup_task = GNUNET_SCHEDULER_add_delayed (
  248. GNUNET_TIME_relative_multiply (EXPIRATION, 2), &name_lookup_shadow, NULL);
  249. }
  250. static void
  251. run (void *cls,
  252. const struct GNUNET_CONFIGURATION_Handle *cfg,
  253. struct GNUNET_TESTING_Peer *peer)
  254. {
  255. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  256. &endbadly,
  257. NULL);
  258. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  259. GNUNET_assert (privkey != NULL);
  260. GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
  261. &pubkey);
  262. record_expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
  263. EXPIRATION);
  264. records[0].expiration_time = record_expiration.abs_value_us;
  265. records[0].record_type = TEST_RECORD_TYPE;
  266. records[0].data_size = TEST_RECORD_DATALEN;
  267. records[0].data = GNUNET_malloc (TEST_RECORD_DATALEN);
  268. records[0].flags = GNUNET_GNSRECORD_RF_NONE;
  269. memset ((char *) records[0].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
  270. records[1].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
  271. + 1000000000;
  272. records[1].record_type = TEST_RECORD_TYPE;
  273. records[1].data_size = TEST_RECORD_DATALEN;
  274. records[1].data = GNUNET_malloc (TEST_RECORD_DATALEN);
  275. records[1].flags = GNUNET_GNSRECORD_RF_SHADOW_RECORD;
  276. memset ((char *) records[1].data, TEST_SHADOW_RECORD_DATA,
  277. TEST_RECORD_DATALEN);
  278. nsh = GNUNET_NAMESTORE_connect (cfg);
  279. nch = GNUNET_NAMECACHE_connect (cfg);
  280. GNUNET_break (NULL != nsh);
  281. GNUNET_break (NULL != nch);
  282. nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, TEST_NAME,
  283. 2, records, &put_cont, NULL);
  284. if (NULL == nsqe)
  285. {
  286. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  287. _ ("Namestore cannot store no block\n"));
  288. }
  289. GNUNET_free ((void *) records[0].data);
  290. GNUNET_free ((void *) records[1].data);
  291. }
  292. #include "test_common.c"
  293. int
  294. main (int argc, char *argv[])
  295. {
  296. const char *plugin_name;
  297. char *cfg_name;
  298. SETUP_CFG (plugin_name, cfg_name);
  299. res = 1;
  300. if (0 !=
  301. GNUNET_TESTING_peer_run ("test-namestore-api-lookup-shadow-filter",
  302. cfg_name,
  303. &run,
  304. NULL))
  305. {
  306. res = 1;
  307. }
  308. GNUNET_DISK_purge_cfg_dir (cfg_name,
  309. "GNUNET_TEST_HOME");
  310. GNUNET_free (cfg_name);
  311. return res;
  312. }
  313. /* end of test_namestore_api_lookup_shadow_filter.c */