test_fs_uri.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2003-2014 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 fs/test_fs_uri.c
  18. * @brief Test for fs_uri.c
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_fs_service.h"
  24. #include "fs_api.h"
  25. static int
  26. testKeyword ()
  27. {
  28. char *uri;
  29. struct GNUNET_FS_Uri *ret;
  30. char *emsg;
  31. if (NULL != (ret = GNUNET_FS_uri_parse ("gnunet://fs/ksk/++", &emsg)))
  32. {
  33. GNUNET_FS_uri_destroy (ret);
  34. GNUNET_assert (0);
  35. }
  36. GNUNET_free (emsg);
  37. ret = GNUNET_FS_uri_parse ("gnunet://fs/ksk/foo+bar", &emsg);
  38. if (NULL == ret)
  39. {
  40. GNUNET_free (emsg);
  41. GNUNET_assert (0);
  42. }
  43. if (! GNUNET_FS_uri_test_ksk (ret))
  44. {
  45. GNUNET_FS_uri_destroy (ret);
  46. GNUNET_assert (0);
  47. }
  48. if ((2 != ret->data.ksk.keywordCount) ||
  49. (0 != strcmp (" foo", ret->data.ksk.keywords[0])) ||
  50. (0 != strcmp (" bar", ret->data.ksk.keywords[1])))
  51. {
  52. GNUNET_FS_uri_destroy (ret);
  53. GNUNET_assert (0);
  54. }
  55. uri = GNUNET_FS_uri_to_string (ret);
  56. if (0 != strcmp (uri, "gnunet://fs/ksk/foo+bar"))
  57. {
  58. GNUNET_free (uri);
  59. GNUNET_FS_uri_destroy (ret);
  60. GNUNET_assert (0);
  61. }
  62. GNUNET_free (uri);
  63. GNUNET_FS_uri_destroy (ret);
  64. return 0;
  65. }
  66. static int
  67. testLocation ()
  68. {
  69. struct GNUNET_FS_Uri *uri;
  70. char *uric;
  71. struct GNUNET_FS_Uri *uri2;
  72. struct GNUNET_FS_Uri *baseURI;
  73. char *emsg;
  74. struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
  75. baseURI =
  76. GNUNET_FS_uri_parse
  77. ("gnunet://fs/chk/4QZP479A9SKGFNMQ2ZBCYE71YV2QMTVGWTVPB6A10ASVCKXDHB05DKPSC7ZF6E9P9W1VE47394EQY7NXA47Q6R35M7P1MJPGP59D1Z8.D54QD1K5XCG5878T6YZ19AM60MQ6FC0YNVK7QY08KK0KM0FJJ3KQWYG112FN5T07KN7J0X35DF6WVBT9B8ZMZ3X2BXJ22X3KFQ6MV2G.15999",
  78. &emsg);
  79. GNUNET_assert (baseURI != NULL);
  80. GNUNET_assert (emsg == NULL);
  81. pk = GNUNET_CRYPTO_eddsa_key_create ();
  82. uri = GNUNET_FS_uri_loc_create (baseURI,
  83. pk,
  84. GNUNET_TIME_absolute_get ());
  85. GNUNET_free (pk);
  86. if (NULL == uri)
  87. {
  88. GNUNET_break (0);
  89. GNUNET_FS_uri_destroy (baseURI);
  90. return 1;
  91. }
  92. if (! GNUNET_FS_uri_test_loc (uri))
  93. {
  94. GNUNET_break (0);
  95. GNUNET_FS_uri_destroy (uri);
  96. GNUNET_FS_uri_destroy (baseURI);
  97. return 1;
  98. }
  99. uri2 = GNUNET_FS_uri_loc_get_uri (uri);
  100. if (! GNUNET_FS_uri_test_equal (baseURI, uri2))
  101. {
  102. GNUNET_break (0);
  103. GNUNET_FS_uri_destroy (uri);
  104. GNUNET_FS_uri_destroy (uri2);
  105. GNUNET_FS_uri_destroy (baseURI);
  106. return 1;
  107. }
  108. GNUNET_FS_uri_destroy (uri2);
  109. GNUNET_FS_uri_destroy (baseURI);
  110. uric = GNUNET_FS_uri_to_string (uri);
  111. #if 0
  112. /* not for the faint of heart: */
  113. printf ("URI: `%s'\n", uric);
  114. #endif
  115. uri2 = GNUNET_FS_uri_parse (uric, &emsg);
  116. GNUNET_free (uric);
  117. if (uri2 == NULL)
  118. {
  119. fprintf (stderr, "URI parsing failed: %s\n", emsg);
  120. GNUNET_break (0);
  121. GNUNET_FS_uri_destroy (uri);
  122. GNUNET_free (emsg);
  123. return 1;
  124. }
  125. GNUNET_assert (NULL == emsg);
  126. if (GNUNET_YES != GNUNET_FS_uri_test_equal (uri, uri2))
  127. {
  128. GNUNET_break (0);
  129. GNUNET_FS_uri_destroy (uri);
  130. GNUNET_FS_uri_destroy (uri2);
  131. return 1;
  132. }
  133. GNUNET_FS_uri_destroy (uri2);
  134. GNUNET_FS_uri_destroy (uri);
  135. return 0;
  136. }
  137. static int
  138. testNamespace (int i)
  139. {
  140. char *uri;
  141. struct GNUNET_FS_Uri *ret;
  142. char *emsg;
  143. struct GNUNET_CRYPTO_EcdsaPrivateKey *ph;
  144. struct GNUNET_CRYPTO_EcdsaPublicKey id;
  145. char buf[1024];
  146. char ubuf[1024];
  147. char *sret;
  148. if (NULL !=
  149. (ret =
  150. GNUNET_FS_uri_parse ("gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3VUK",
  151. &emsg)))
  152. {
  153. GNUNET_FS_uri_destroy (ret);
  154. GNUNET_assert (0);
  155. }
  156. GNUNET_free (emsg);
  157. if (NULL !=
  158. (ret =
  159. GNUNET_FS_uri_parse
  160. ("gnunet://fs/sks/XQHH4R288W26EBV369F6RCE0PJVJTX2Y74Q2FJPMPGA31HJX2JG/this", &emsg)))
  161. {
  162. GNUNET_FS_uri_destroy (ret);
  163. GNUNET_assert (0);
  164. }
  165. GNUNET_free (emsg);
  166. if (NULL != (ret = GNUNET_FS_uri_parse ("gnunet://fs/sks/test", &emsg)))
  167. {
  168. GNUNET_FS_uri_destroy (ret);
  169. GNUNET_assert (0);
  170. }
  171. GNUNET_free (emsg);
  172. ph = GNUNET_CRYPTO_ecdsa_key_create ();
  173. GNUNET_CRYPTO_ecdsa_key_get_public (ph, &id);
  174. sret = GNUNET_STRINGS_data_to_string (&id, sizeof (id),
  175. ubuf, sizeof (ubuf) - 1);
  176. GNUNET_assert (NULL != sret);
  177. sret[0] = '\0';
  178. GNUNET_snprintf (buf, sizeof (buf),
  179. "gnunet://fs/sks/%s/test",
  180. ubuf);
  181. ret = GNUNET_FS_uri_parse (buf, &emsg);
  182. if (NULL == ret)
  183. {
  184. GNUNET_free (emsg);
  185. GNUNET_assert (0);
  186. }
  187. if (GNUNET_FS_uri_test_ksk (ret))
  188. {
  189. GNUNET_FS_uri_destroy (ret);
  190. GNUNET_assert (0);
  191. }
  192. if (!GNUNET_FS_uri_test_sks (ret))
  193. {
  194. GNUNET_FS_uri_destroy (ret);
  195. GNUNET_assert (0);
  196. }
  197. uri = GNUNET_FS_uri_to_string (ret);
  198. if (0 !=
  199. strcmp (uri,
  200. buf))
  201. {
  202. GNUNET_FS_uri_destroy (ret);
  203. GNUNET_free (uri);
  204. GNUNET_assert (0);
  205. }
  206. GNUNET_free (uri);
  207. GNUNET_FS_uri_destroy (ret);
  208. return 0;
  209. }
  210. static int
  211. testFile (int i)
  212. {
  213. char *uri;
  214. struct GNUNET_FS_Uri *ret;
  215. char *emsg;
  216. if (NULL !=
  217. (ret =
  218. GNUNET_FS_uri_parse
  219. ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H00000440000.42",
  220. &emsg)))
  221. {
  222. GNUNET_FS_uri_destroy (ret);
  223. GNUNET_assert (0);
  224. }
  225. GNUNET_free (emsg);
  226. if (NULL !=
  227. (ret =
  228. GNUNET_FS_uri_parse
  229. ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000",
  230. &emsg)))
  231. {
  232. GNUNET_FS_uri_destroy (ret);
  233. GNUNET_assert (0);
  234. }
  235. GNUNET_free (emsg);
  236. if (NULL !=
  237. (ret =
  238. GNUNET_FS_uri_parse
  239. ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.FGH",
  240. &emsg)))
  241. {
  242. GNUNET_FS_uri_destroy (ret);
  243. GNUNET_assert (0);
  244. }
  245. GNUNET_free (emsg);
  246. ret =
  247. GNUNET_FS_uri_parse
  248. ("gnunet://fs/chk/4QZP479A9SKGFNMQ2ZBCYE71YV2QMTVGWTVPB6A10ASVCKXDHB05DKPSC7ZF6E9P9W1VE47394EQY7NXA47Q6R35M7P1MJPGP59D1Z8.D54QD1K5XCG5878T6YZ19AM60MQ6FC0YNVK7QY08KK0KM0FJJ3KQWYG112FN5T07KN7J0X35DF6WVBT9B8ZMZ3X2BXJ22X3KFQ6MV2G.42",
  249. &emsg);
  250. if (ret == NULL)
  251. {
  252. GNUNET_free (emsg);
  253. GNUNET_assert (0);
  254. }
  255. if (GNUNET_FS_uri_test_ksk (ret))
  256. {
  257. GNUNET_FS_uri_destroy (ret);
  258. GNUNET_assert (0);
  259. }
  260. if (GNUNET_FS_uri_test_sks (ret))
  261. {
  262. GNUNET_FS_uri_destroy (ret);
  263. GNUNET_assert (0);
  264. }
  265. if (GNUNET_ntohll (ret->data.chk.file_length) != 42)
  266. {
  267. GNUNET_FS_uri_destroy (ret);
  268. GNUNET_assert (0);
  269. }
  270. uri = GNUNET_FS_uri_to_string (ret);
  271. if (0 !=
  272. strcmp (uri,
  273. "gnunet://fs/chk/4QZP479A9SKGFNMQ2ZBCYE71YV2QMTVGWTVPB6A10ASVCKXDHB05DKPSC7ZF6E9P9W1VE47394EQY7NXA47Q6R35M7P1MJPGP59D1Z8.D54QD1K5XCG5878T6YZ19AM60MQ6FC0YNVK7QY08KK0KM0FJJ3KQWYG112FN5T07KN7J0X35DF6WVBT9B8ZMZ3X2BXJ22X3KFQ6MV2G.42"))
  274. {
  275. GNUNET_free (uri);
  276. GNUNET_FS_uri_destroy (ret);
  277. GNUNET_assert (0);
  278. }
  279. GNUNET_free (uri);
  280. GNUNET_FS_uri_destroy (ret);
  281. return 0;
  282. }
  283. int
  284. main (int argc, char *argv[])
  285. {
  286. int failureCount = 0;
  287. int i;
  288. GNUNET_log_setup ("test_fs_uri",
  289. "WARNING",
  290. NULL);
  291. failureCount += testKeyword ();
  292. failureCount += testLocation ();
  293. for (i = 0; i < 255; i++)
  294. {
  295. /* FPRINTF (stderr, "%s", "."); */
  296. failureCount += testNamespace (i);
  297. failureCount += testFile (i);
  298. }
  299. /* FPRINTF (stderr, "%s", "\n"); */
  300. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-uri");
  301. if (failureCount != 0)
  302. return 1;
  303. return 0;
  304. }
  305. /* end of test_fs_uri.c */