test_fs_uri.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2003, 2004, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/test_fs_uri.c
  19. * @brief Test for fs_uri.c
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_fs_service.h"
  25. #include "fs.h"
  26. #define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); return 1; }
  27. static int
  28. testKeyword ()
  29. {
  30. char *uri;
  31. struct GNUNET_FS_Uri *ret;
  32. char *emsg;
  33. if (NULL != (ret = GNUNET_FS_uri_parse ("gnunet://fs/ksk/++", &emsg)))
  34. {
  35. GNUNET_FS_uri_destroy (ret);
  36. ABORT ();
  37. }
  38. GNUNET_free (emsg);
  39. ret = GNUNET_FS_uri_parse ("gnunet://fs/ksk/foo+bar", &emsg);
  40. if (ret == NULL)
  41. {
  42. GNUNET_free (emsg);
  43. ABORT ();
  44. }
  45. if (!GNUNET_FS_uri_test_ksk (ret))
  46. {
  47. GNUNET_FS_uri_destroy (ret);
  48. ABORT ();
  49. }
  50. if ((2 != ret->data.ksk.keywordCount) ||
  51. (0 != strcmp (" foo", ret->data.ksk.keywords[0])) ||
  52. (0 != strcmp (" bar", ret->data.ksk.keywords[1])))
  53. {
  54. GNUNET_FS_uri_destroy (ret);
  55. ABORT ();
  56. }
  57. uri = GNUNET_FS_uri_to_string (ret);
  58. if (0 != strcmp (uri, "gnunet://fs/ksk/foo+bar"))
  59. {
  60. GNUNET_free (uri);
  61. GNUNET_FS_uri_destroy (ret);
  62. ABORT ();
  63. }
  64. GNUNET_free (uri);
  65. GNUNET_FS_uri_destroy (ret);
  66. return 0;
  67. }
  68. static int
  69. testLocation ()
  70. {
  71. struct GNUNET_FS_Uri *uri;
  72. char *uric;
  73. struct GNUNET_FS_Uri *uri2;
  74. struct GNUNET_FS_Uri *baseURI;
  75. char *emsg;
  76. struct GNUNET_CONFIGURATION_Handle *cfg;
  77. baseURI =
  78. GNUNET_FS_uri_parse
  79. ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42",
  80. &emsg);
  81. GNUNET_assert (baseURI != NULL);
  82. GNUNET_assert (emsg == NULL);
  83. cfg = GNUNET_CONFIGURATION_create ();
  84. if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, "test_fs_uri_data.conf"))
  85. {
  86. fprintf (stderr, "Failed to parse configuration file\n");
  87. GNUNET_FS_uri_destroy (baseURI);
  88. GNUNET_CONFIGURATION_destroy (cfg);
  89. return 1;
  90. }
  91. uri = GNUNET_FS_uri_loc_create (baseURI, cfg, GNUNET_TIME_absolute_get ());
  92. if (uri == NULL)
  93. {
  94. GNUNET_break (0);
  95. GNUNET_FS_uri_destroy (baseURI);
  96. GNUNET_CONFIGURATION_destroy (cfg);
  97. return 1;
  98. }
  99. if (!GNUNET_FS_uri_test_loc (uri))
  100. {
  101. GNUNET_break (0);
  102. GNUNET_FS_uri_destroy (uri);
  103. GNUNET_FS_uri_destroy (baseURI);
  104. GNUNET_CONFIGURATION_destroy (cfg);
  105. return 1;
  106. }
  107. uri2 = GNUNET_FS_uri_loc_get_uri (uri);
  108. if (!GNUNET_FS_uri_test_equal (baseURI, uri2))
  109. {
  110. GNUNET_break (0);
  111. GNUNET_FS_uri_destroy (uri);
  112. GNUNET_FS_uri_destroy (uri2);
  113. GNUNET_FS_uri_destroy (baseURI);
  114. GNUNET_CONFIGURATION_destroy (cfg);
  115. return 1;
  116. }
  117. GNUNET_FS_uri_destroy (uri2);
  118. GNUNET_FS_uri_destroy (baseURI);
  119. uric = GNUNET_FS_uri_to_string (uri);
  120. #if 0
  121. /* not for the faint of heart: */
  122. printf ("URI: `%s'\n", uric);
  123. #endif
  124. uri2 = GNUNET_FS_uri_parse (uric, &emsg);
  125. GNUNET_free (uric);
  126. if (uri2 == NULL)
  127. {
  128. GNUNET_break (0);
  129. GNUNET_FS_uri_destroy (uri);
  130. GNUNET_CONFIGURATION_destroy (cfg);
  131. GNUNET_free (emsg);
  132. return 1;
  133. }
  134. GNUNET_assert (NULL == emsg);
  135. if (GNUNET_YES != GNUNET_FS_uri_test_equal (uri, uri2))
  136. {
  137. GNUNET_break (0);
  138. GNUNET_FS_uri_destroy (uri);
  139. GNUNET_FS_uri_destroy (uri2);
  140. GNUNET_CONFIGURATION_destroy (cfg);
  141. return 1;
  142. }
  143. GNUNET_FS_uri_destroy (uri2);
  144. GNUNET_FS_uri_destroy (uri);
  145. GNUNET_CONFIGURATION_destroy (cfg);
  146. return 0;
  147. }
  148. static int
  149. testNamespace (int i)
  150. {
  151. char *uri;
  152. struct GNUNET_FS_Uri *ret;
  153. char *emsg;
  154. if (NULL !=
  155. (ret =
  156. GNUNET_FS_uri_parse ("gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3VUK",
  157. &emsg)))
  158. {
  159. GNUNET_FS_uri_destroy (ret);
  160. ABORT ();
  161. }
  162. GNUNET_free (emsg);
  163. if (NULL !=
  164. (ret =
  165. GNUNET_FS_uri_parse
  166. ("gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3V/test", &emsg)))
  167. {
  168. GNUNET_FS_uri_destroy (ret);
  169. ABORT ();
  170. }
  171. GNUNET_free (emsg);
  172. if (NULL != (ret = GNUNET_FS_uri_parse ("gnunet://fs/sks/test", &emsg)))
  173. {
  174. GNUNET_FS_uri_destroy (ret);
  175. ABORT ();
  176. }
  177. GNUNET_free (emsg);
  178. ret =
  179. GNUNET_FS_uri_parse
  180. ("gnunet://fs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test",
  181. &emsg);
  182. if (ret == NULL)
  183. {
  184. GNUNET_free (emsg);
  185. ABORT ();
  186. }
  187. if (GNUNET_FS_uri_test_ksk (ret))
  188. {
  189. GNUNET_FS_uri_destroy (ret);
  190. ABORT ();
  191. }
  192. if (!GNUNET_FS_uri_test_sks (ret))
  193. {
  194. GNUNET_FS_uri_destroy (ret);
  195. ABORT ();
  196. }
  197. uri = GNUNET_FS_uri_to_string (ret);
  198. if (0 !=
  199. strcmp (uri,
  200. "gnunet://fs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test"))
  201. {
  202. GNUNET_FS_uri_destroy (ret);
  203. GNUNET_free (uri);
  204. ABORT ();
  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. ABORT ();
  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. ABORT ();
  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. ABORT ();
  244. }
  245. GNUNET_free (emsg);
  246. ret =
  247. GNUNET_FS_uri_parse
  248. ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42",
  249. &emsg);
  250. if (ret == NULL)
  251. {
  252. GNUNET_free (emsg);
  253. ABORT ();
  254. }
  255. if (GNUNET_FS_uri_test_ksk (ret))
  256. {
  257. GNUNET_FS_uri_destroy (ret);
  258. ABORT ();
  259. }
  260. if (GNUNET_FS_uri_test_sks (ret))
  261. {
  262. GNUNET_FS_uri_destroy (ret);
  263. ABORT ();
  264. }
  265. if (GNUNET_ntohll (ret->data.chk.file_length) != 42)
  266. {
  267. GNUNET_FS_uri_destroy (ret);
  268. ABORT ();
  269. }
  270. uri = GNUNET_FS_uri_to_string (ret);
  271. if (0 !=
  272. strcmp (uri,
  273. "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42"))
  274. {
  275. GNUNET_free (uri);
  276. GNUNET_FS_uri_destroy (ret);
  277. ABORT ();
  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. #if VERBOSE
  290. "DEBUG",
  291. #else
  292. "WARNING",
  293. #endif
  294. NULL);
  295. GNUNET_CRYPTO_random_disable_entropy_gathering ();
  296. failureCount += testKeyword ();
  297. failureCount += testLocation ();
  298. for (i = 0; i < 255; i++)
  299. {
  300. /* fprintf (stderr, "."); */
  301. failureCount += testNamespace (i);
  302. failureCount += testFile (i);
  303. }
  304. /* fprintf (stderr, "\n"); */
  305. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-uri");
  306. if (failureCount != 0)
  307. return 1;
  308. return 0;
  309. }
  310. /* end of test_fs_uri.c */