test_fs_uri.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. (
  78. "gnunet://fs/chk/4QZP479A9SKGFNMQ2ZBCYE71YV2QMTVGWTVPB6A10ASVCKXDHB05DKPSC7ZF6E9P9W1VE47394EQY7NXA47Q6R35M7P1MJPGP59D1Z8.D54QD1K5XCG5878T6YZ19AM60MQ6FC0YNVK7QY08KK0KM0FJJ3KQWYG112FN5T07KN7J0X35DF6WVBT9B8ZMZ3X2BXJ22X3KFQ6MV2G.15999",
  79. &emsg);
  80. GNUNET_assert (baseURI != NULL);
  81. GNUNET_assert (emsg == NULL);
  82. pk = GNUNET_CRYPTO_eddsa_key_create ();
  83. uri = GNUNET_FS_uri_loc_create (baseURI,
  84. pk,
  85. GNUNET_TIME_absolute_get ());
  86. GNUNET_free (pk);
  87. if (NULL == uri)
  88. {
  89. GNUNET_break (0);
  90. GNUNET_FS_uri_destroy (baseURI);
  91. return 1;
  92. }
  93. if (! GNUNET_FS_uri_test_loc (uri))
  94. {
  95. GNUNET_break (0);
  96. GNUNET_FS_uri_destroy (uri);
  97. GNUNET_FS_uri_destroy (baseURI);
  98. return 1;
  99. }
  100. uri2 = GNUNET_FS_uri_loc_get_uri (uri);
  101. if (! GNUNET_FS_uri_test_equal (baseURI, uri2))
  102. {
  103. GNUNET_break (0);
  104. GNUNET_FS_uri_destroy (uri);
  105. GNUNET_FS_uri_destroy (uri2);
  106. GNUNET_FS_uri_destroy (baseURI);
  107. return 1;
  108. }
  109. GNUNET_FS_uri_destroy (uri2);
  110. GNUNET_FS_uri_destroy (baseURI);
  111. uric = GNUNET_FS_uri_to_string (uri);
  112. #if 0
  113. /* not for the faint of heart: */
  114. printf ("URI: `%s'\n", uric);
  115. #endif
  116. uri2 = GNUNET_FS_uri_parse (uric, &emsg);
  117. GNUNET_free (uric);
  118. if (uri2 == NULL)
  119. {
  120. fprintf (stderr, "URI parsing failed: %s\n", emsg);
  121. GNUNET_break (0);
  122. GNUNET_FS_uri_destroy (uri);
  123. GNUNET_free (emsg);
  124. return 1;
  125. }
  126. GNUNET_assert (NULL == emsg);
  127. if (GNUNET_YES != GNUNET_FS_uri_test_equal (uri, uri2))
  128. {
  129. GNUNET_break (0);
  130. GNUNET_FS_uri_destroy (uri);
  131. GNUNET_FS_uri_destroy (uri2);
  132. return 1;
  133. }
  134. GNUNET_FS_uri_destroy (uri2);
  135. GNUNET_FS_uri_destroy (uri);
  136. return 0;
  137. }
  138. static int
  139. testNamespace (int i)
  140. {
  141. char *uri;
  142. struct GNUNET_FS_Uri *ret;
  143. char *emsg;
  144. struct GNUNET_CRYPTO_EcdsaPrivateKey *ph;
  145. struct GNUNET_CRYPTO_EcdsaPublicKey id;
  146. char buf[1024];
  147. char ubuf[1024];
  148. char *sret;
  149. if (NULL !=
  150. (ret =
  151. GNUNET_FS_uri_parse (
  152. "gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3VUK",
  153. &emsg)))
  154. {
  155. GNUNET_FS_uri_destroy (ret);
  156. GNUNET_assert (0);
  157. }
  158. GNUNET_free (emsg);
  159. if (NULL !=
  160. (ret =
  161. GNUNET_FS_uri_parse
  162. (
  163. "gnunet://fs/sks/XQHH4R288W26EBV369F6RCE0PJVJTX2Y74Q2FJPMPGA31HJX2JG/this",
  164. &emsg)))
  165. {
  166. GNUNET_FS_uri_destroy (ret);
  167. GNUNET_assert (0);
  168. }
  169. GNUNET_free (emsg);
  170. if (NULL != (ret = GNUNET_FS_uri_parse ("gnunet://fs/sks/test", &emsg)))
  171. {
  172. GNUNET_FS_uri_destroy (ret);
  173. GNUNET_assert (0);
  174. }
  175. GNUNET_free (emsg);
  176. ph = GNUNET_CRYPTO_ecdsa_key_create ();
  177. GNUNET_CRYPTO_ecdsa_key_get_public (ph, &id);
  178. sret = GNUNET_STRINGS_data_to_string (&id, sizeof(id),
  179. ubuf, sizeof(ubuf) - 1);
  180. GNUNET_assert (NULL != sret);
  181. sret[0] = '\0';
  182. GNUNET_snprintf (buf, sizeof(buf),
  183. "gnunet://fs/sks/%s/test",
  184. ubuf);
  185. ret = GNUNET_FS_uri_parse (buf, &emsg);
  186. if (NULL == ret)
  187. {
  188. GNUNET_free (emsg);
  189. GNUNET_assert (0);
  190. }
  191. if (GNUNET_FS_uri_test_ksk (ret))
  192. {
  193. GNUNET_FS_uri_destroy (ret);
  194. GNUNET_assert (0);
  195. }
  196. if (! GNUNET_FS_uri_test_sks (ret))
  197. {
  198. GNUNET_FS_uri_destroy (ret);
  199. GNUNET_assert (0);
  200. }
  201. uri = GNUNET_FS_uri_to_string (ret);
  202. if (0 !=
  203. strcmp (uri,
  204. buf))
  205. {
  206. GNUNET_FS_uri_destroy (ret);
  207. GNUNET_free (uri);
  208. GNUNET_assert (0);
  209. }
  210. GNUNET_free (uri);
  211. GNUNET_FS_uri_destroy (ret);
  212. return 0;
  213. }
  214. static int
  215. testFile (int i)
  216. {
  217. char *uri;
  218. struct GNUNET_FS_Uri *ret;
  219. char *emsg;
  220. if (NULL !=
  221. (ret =
  222. GNUNET_FS_uri_parse
  223. (
  224. "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H00000440000.42",
  225. &emsg)))
  226. {
  227. GNUNET_FS_uri_destroy (ret);
  228. GNUNET_assert (0);
  229. }
  230. GNUNET_free (emsg);
  231. if (NULL !=
  232. (ret =
  233. GNUNET_FS_uri_parse
  234. (
  235. "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000",
  236. &emsg)))
  237. {
  238. GNUNET_FS_uri_destroy (ret);
  239. GNUNET_assert (0);
  240. }
  241. GNUNET_free (emsg);
  242. if (NULL !=
  243. (ret =
  244. GNUNET_FS_uri_parse
  245. (
  246. "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.FGH",
  247. &emsg)))
  248. {
  249. GNUNET_FS_uri_destroy (ret);
  250. GNUNET_assert (0);
  251. }
  252. GNUNET_free (emsg);
  253. ret =
  254. GNUNET_FS_uri_parse
  255. (
  256. "gnunet://fs/chk/4QZP479A9SKGFNMQ2ZBCYE71YV2QMTVGWTVPB6A10ASVCKXDHB05DKPSC7ZF6E9P9W1VE47394EQY7NXA47Q6R35M7P1MJPGP59D1Z8.D54QD1K5XCG5878T6YZ19AM60MQ6FC0YNVK7QY08KK0KM0FJJ3KQWYG112FN5T07KN7J0X35DF6WVBT9B8ZMZ3X2BXJ22X3KFQ6MV2G.42",
  257. &emsg);
  258. if (ret == NULL)
  259. {
  260. GNUNET_free (emsg);
  261. GNUNET_assert (0);
  262. }
  263. if (GNUNET_FS_uri_test_ksk (ret))
  264. {
  265. GNUNET_FS_uri_destroy (ret);
  266. GNUNET_assert (0);
  267. }
  268. if (GNUNET_FS_uri_test_sks (ret))
  269. {
  270. GNUNET_FS_uri_destroy (ret);
  271. GNUNET_assert (0);
  272. }
  273. if (GNUNET_ntohll (ret->data.chk.file_length) != 42)
  274. {
  275. GNUNET_FS_uri_destroy (ret);
  276. GNUNET_assert (0);
  277. }
  278. uri = GNUNET_FS_uri_to_string (ret);
  279. if (0 !=
  280. strcmp (uri,
  281. "gnunet://fs/chk/4QZP479A9SKGFNMQ2ZBCYE71YV2QMTVGWTVPB6A10ASVCKXDHB05DKPSC7ZF6E9P9W1VE47394EQY7NXA47Q6R35M7P1MJPGP59D1Z8.D54QD1K5XCG5878T6YZ19AM60MQ6FC0YNVK7QY08KK0KM0FJJ3KQWYG112FN5T07KN7J0X35DF6WVBT9B8ZMZ3X2BXJ22X3KFQ6MV2G.42"))
  282. {
  283. GNUNET_free (uri);
  284. GNUNET_FS_uri_destroy (ret);
  285. GNUNET_assert (0);
  286. }
  287. GNUNET_free (uri);
  288. GNUNET_FS_uri_destroy (ret);
  289. return 0;
  290. }
  291. int
  292. main (int argc, char *argv[])
  293. {
  294. int failureCount = 0;
  295. int i;
  296. GNUNET_log_setup ("test_fs_uri",
  297. "WARNING",
  298. NULL);
  299. failureCount += testKeyword ();
  300. failureCount += testLocation ();
  301. for (i = 0; i < 255; i++)
  302. {
  303. /* fprintf (stderr, "%s", "."); */
  304. failureCount += testNamespace (i);
  305. failureCount += testFile (i);
  306. }
  307. /* fprintf (stderr, "%s", "\n"); */
  308. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-uri");
  309. if (failureCount != 0)
  310. return 1;
  311. return 0;
  312. }
  313. /* end of test_fs_uri.c */