test_fs_file_information.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2004, 2005, 2006, 2008, 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_file_information.c
  19. * @brief simple testcase for file_information operations
  20. * @author Christian Grothoff
  21. *
  22. * TODO:
  23. * - test that metatdata, etc. are all correct (for example,
  24. * there is a known bug with dirname never being set that is
  25. * not detected!)
  26. * - need to iterate over file-information structure
  27. * - other API functions may not yet be tested (such as
  28. * filedata-from-callback)
  29. */
  30. #include "platform.h"
  31. #include "gnunet_util_lib.h"
  32. #include "gnunet_fs_service.h"
  33. #define VERBOSE GNUNET_NO
  34. /**
  35. * File-size we use for testing.
  36. */
  37. #define FILESIZE (1024 * 1024 * 2)
  38. /**
  39. * How long should our test-content live?
  40. */
  41. #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  42. static int
  43. mycleaner (void *cls, struct GNUNET_FS_FileInformation *fi, uint64_t length,
  44. struct GNUNET_CONTAINER_MetaData *meta, struct GNUNET_FS_Uri **uri,
  45. struct GNUNET_FS_BlockOptions *bo, int *do_index, void **client_info)
  46. {
  47. return GNUNET_OK;
  48. }
  49. static void
  50. run (void *cls, char *const *args, const char *cfgfile,
  51. const struct GNUNET_CONFIGURATION_Handle *cfg)
  52. {
  53. const char *keywords[] = {
  54. "down_foo",
  55. "down_bar",
  56. };
  57. char *fn1;
  58. char *fn2;
  59. char *buf;
  60. struct GNUNET_CONTAINER_MetaData *meta;
  61. struct GNUNET_FS_Uri *kuri;
  62. struct GNUNET_FS_FileInformation *fi1;
  63. struct GNUNET_FS_FileInformation *fi2;
  64. struct GNUNET_FS_FileInformation *fidir;
  65. struct GNUNET_FS_Handle *fs;
  66. size_t i;
  67. struct GNUNET_FS_BlockOptions bo;
  68. fs = GNUNET_FS_start (cfg, "test-fs-file-information", NULL, NULL,
  69. GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
  70. fn1 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
  71. buf = GNUNET_malloc (FILESIZE);
  72. for (i = 0; i < FILESIZE; i++)
  73. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  74. GNUNET_assert (FILESIZE ==
  75. GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
  76. GNUNET_DISK_PERM_USER_READ |
  77. GNUNET_DISK_PERM_USER_WRITE));
  78. GNUNET_free (buf);
  79. fn2 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
  80. buf = GNUNET_malloc (FILESIZE);
  81. for (i = 0; i < FILESIZE; i++)
  82. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  83. GNUNET_assert (FILESIZE ==
  84. GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
  85. GNUNET_DISK_PERM_USER_READ |
  86. GNUNET_DISK_PERM_USER_WRITE));
  87. GNUNET_free (buf);
  88. meta = GNUNET_CONTAINER_meta_data_create ();
  89. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  90. bo.content_priority = 42;
  91. bo.anonymity_level = 1;
  92. bo.replication_level = 0;
  93. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  94. fi1 =
  95. GNUNET_FS_file_information_create_from_file (fs,
  96. "file_information-context1",
  97. fn1, kuri, meta, GNUNET_YES,
  98. &bo);
  99. GNUNET_assert (fi1 != NULL);
  100. fi2 =
  101. GNUNET_FS_file_information_create_from_file (fs,
  102. "file_information-context2",
  103. fn2, kuri, meta, GNUNET_YES,
  104. &bo);
  105. GNUNET_assert (fi2 != NULL);
  106. fidir =
  107. GNUNET_FS_file_information_create_empty_directory (fs,
  108. "file_information-context-dir",
  109. kuri, meta, &bo);
  110. GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
  111. GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
  112. GNUNET_FS_uri_destroy (kuri);
  113. GNUNET_CONTAINER_meta_data_destroy (meta);
  114. GNUNET_assert (NULL != fidir);
  115. /* FIXME: test more of API! */
  116. GNUNET_FS_file_information_destroy (fidir, &mycleaner, NULL);
  117. GNUNET_DISK_directory_remove (fn1);
  118. GNUNET_DISK_directory_remove (fn2);
  119. GNUNET_free_non_null (fn1);
  120. GNUNET_free_non_null (fn2);
  121. GNUNET_FS_stop (fs);
  122. }
  123. static int
  124. testThumbnail ()
  125. {
  126. struct GNUNET_CONTAINER_MetaData *m;
  127. struct GNUNET_CONTAINER_MetaData *d;
  128. struct EXTRACTOR_PluginList *ex;
  129. unsigned char *thumb;
  130. size_t size;
  131. char *date;
  132. ex = EXTRACTOR_plugin_add_config (NULL, "thumbnailgtk",
  133. EXTRACTOR_OPTION_DEFAULT_POLICY);
  134. if (ex == NULL)
  135. {
  136. fprintf (stderr,
  137. "Test incomplete, have no GTK thumbnail extractor available.\n");
  138. return 0; /* can not test, no thumbnailer */
  139. }
  140. ex = EXTRACTOR_plugin_add_config (ex, "mime",
  141. EXTRACTOR_OPTION_DEFAULT_POLICY);
  142. m = GNUNET_CONTAINER_meta_data_create ();
  143. if (3 !=
  144. GNUNET_FS_meta_data_extract_from_file (m,
  145. "test_fs_file_information_meta_data_image.jpg",
  146. ex))
  147. {
  148. GNUNET_break (0);
  149. EXTRACTOR_plugin_remove_all (ex);
  150. GNUNET_CONTAINER_meta_data_destroy (m);
  151. return 1;
  152. }
  153. EXTRACTOR_plugin_remove_all (ex);
  154. d = GNUNET_CONTAINER_meta_data_duplicate (m);
  155. GNUNET_CONTAINER_meta_data_destroy (m);
  156. thumb = NULL;
  157. size = GNUNET_CONTAINER_meta_data_get_thumbnail (d, &thumb);
  158. if (size == 0)
  159. {
  160. GNUNET_break (0);
  161. GNUNET_CONTAINER_meta_data_destroy (d);
  162. return 1;
  163. }
  164. GNUNET_free (thumb);
  165. GNUNET_CONTAINER_meta_data_add_publication_date (d);
  166. date =
  167. GNUNET_CONTAINER_meta_data_get_by_type (d,
  168. EXTRACTOR_METATYPE_PUBLICATION_DATE);
  169. if (date == NULL)
  170. {
  171. GNUNET_break (0);
  172. GNUNET_CONTAINER_meta_data_destroy (d);
  173. return 1;
  174. }
  175. GNUNET_free (date);
  176. GNUNET_CONTAINER_meta_data_destroy (d);
  177. return 0;
  178. }
  179. int
  180. main (int argc, char *argv[])
  181. {
  182. char *const argvx[] = {
  183. "test-fs-file_information",
  184. "-c",
  185. "test_fs_file_information_data.conf",
  186. #if VERBOSE
  187. "-L", "DEBUG",
  188. #endif
  189. NULL
  190. };
  191. struct GNUNET_GETOPT_CommandLineOption options[] = {
  192. GNUNET_GETOPT_OPTION_END
  193. };
  194. GNUNET_log_setup ("test_fs_file_information",
  195. #if VERBOSE
  196. "DEBUG",
  197. #else
  198. "WARNING",
  199. #endif
  200. NULL);
  201. if (0 != testThumbnail ())
  202. return 1;
  203. GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
  204. "test-fs-file_information", "nohelp", options, &run,
  205. NULL);
  206. return 0;
  207. }
  208. /* end of test_fs_file_information.c */