00e3c221176e73beb22f33d1b288cfaf07cf97ee
[oweals/gnunet.git] / src / fs / test_fs_file_information.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file fs/test_fs_file_information.c
23  * @brief simple testcase for file_information operations
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - test that metatdata, etc. are all correct (for example,
28  *   there is a known bug with dirname never being set that is
29  *   not detected!)
30  * - need to iterate over file-information structure
31  * - other API functions may not yet be tested (such as
32  *   filedata-from-callback)
33  */
34
35 #include "platform.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_fs_service.h"
38
39 #define VERBOSE GNUNET_NO
40
41 /**
42  * File-size we use for testing.
43  */
44 #define FILESIZE (1024 * 1024 * 2)
45
46 /**
47  * How long should our test-content live?
48  */ 
49 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
50
51
52 static int
53 mycleaner(void *cls,
54           struct GNUNET_FS_FileInformation *fi,
55           uint64_t length,
56           struct GNUNET_CONTAINER_MetaData *meta,
57           struct GNUNET_FS_Uri **uri,
58           struct GNUNET_FS_BlockOptions *bo,
59           int *do_index,
60           void **client_info)
61 {
62   return GNUNET_OK;
63 }
64
65
66 static void
67 run (void *cls,
68      char *const *args,
69      const char *cfgfile,
70      const struct GNUNET_CONFIGURATION_Handle *cfg)
71 {
72   const char *keywords[] = {
73     "down_foo",
74     "down_bar",
75   };
76   char *fn1;
77   char *fn2;
78   char *buf;
79   struct GNUNET_CONTAINER_MetaData *meta;
80   struct GNUNET_FS_Uri *kuri;
81   struct GNUNET_FS_FileInformation *fi1;
82   struct GNUNET_FS_FileInformation *fi2;
83   struct GNUNET_FS_FileInformation *fidir;
84   struct GNUNET_FS_Handle *fs;
85   size_t i;
86   struct GNUNET_FS_BlockOptions bo;
87
88   fs = GNUNET_FS_start (cfg, "test-fs-file-information", NULL, NULL,
89                         GNUNET_FS_FLAGS_NONE,
90                         GNUNET_FS_OPTIONS_END);
91   fn1 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
92   buf = GNUNET_malloc (FILESIZE);
93   for (i = 0; i < FILESIZE; i++)
94     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
95   GNUNET_assert (FILESIZE ==
96                  GNUNET_DISK_fn_write (fn1,
97                                        buf,
98                                        FILESIZE,
99                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
100   GNUNET_free (buf);
101
102   fn2 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
103   buf = GNUNET_malloc (FILESIZE);
104   for (i = 0; i < FILESIZE; i++)
105     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
106   GNUNET_assert (FILESIZE ==
107                  GNUNET_DISK_fn_write (fn2,
108                                        buf,
109                                        FILESIZE,
110                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
111   GNUNET_free (buf);
112
113   meta = GNUNET_CONTAINER_meta_data_create ();
114   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords); 
115   bo.content_priority = 42;
116   bo.anonymity_level = 1;
117   bo.replication_level = 0;
118   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME); 
119   fi1 = GNUNET_FS_file_information_create_from_file (fs,
120                                                      "file_information-context1",
121                                                      fn1,
122                                                      kuri,
123                                                      meta,
124                                                      GNUNET_YES,
125                                                      &bo);
126   GNUNET_assert (fi1 != NULL);
127   fi2 = GNUNET_FS_file_information_create_from_file (fs,
128                                                      "file_information-context2",
129                                                      fn2,
130                                                      kuri,
131                                                      meta,
132                                                      GNUNET_YES,
133                                                      &bo);
134   GNUNET_assert (fi2 != NULL);
135   fidir = GNUNET_FS_file_information_create_empty_directory (fs,
136                                                              "file_information-context-dir",
137                                                              kuri,
138                                                              meta,
139                                                              &bo);
140   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
141   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
142   GNUNET_FS_uri_destroy (kuri);
143   GNUNET_CONTAINER_meta_data_destroy (meta);
144   GNUNET_assert (NULL != fidir);
145   /* FIXME: test more of API! */
146   GNUNET_FS_file_information_destroy (fidir,
147                                       &mycleaner,
148                                       NULL);
149   GNUNET_DISK_directory_remove (fn1);
150   GNUNET_DISK_directory_remove (fn2);
151   GNUNET_free_non_null (fn1);
152   GNUNET_free_non_null (fn2);
153   GNUNET_FS_stop (fs);
154 }
155
156
157 static int
158 testThumbnail ()
159 {
160   struct GNUNET_CONTAINER_MetaData *m;
161   struct GNUNET_CONTAINER_MetaData *d;
162   struct EXTRACTOR_PluginList *ex;
163   unsigned char *thumb;
164   size_t size;
165   char *date;
166
167   ex = EXTRACTOR_plugin_add_config (NULL, "thumbnailgtk", EXTRACTOR_OPTION_DEFAULT_POLICY);
168   if (ex == NULL)
169     {
170       fprintf (stderr,
171                "Test incomplete, have no GTK thumbnail extractor available.\n");
172       return 0;                 /* can not test, no thumbnailer */
173     }
174   ex = EXTRACTOR_plugin_add_config (ex, "mime", EXTRACTOR_OPTION_DEFAULT_POLICY);
175   m = GNUNET_CONTAINER_meta_data_create ();
176   if (3 != GNUNET_FS_meta_data_extract_from_file (m,
177                                                   "test_fs_file_information_meta_data_image.jpg",
178                                                   ex))
179     {
180       GNUNET_break (0);
181       EXTRACTOR_plugin_remove_all (ex);
182       GNUNET_CONTAINER_meta_data_destroy (m);
183       return 1;
184     }
185   EXTRACTOR_plugin_remove_all (ex);
186   d = GNUNET_CONTAINER_meta_data_duplicate (m);
187   GNUNET_CONTAINER_meta_data_destroy (m);
188   thumb = NULL;
189   size = GNUNET_CONTAINER_meta_data_get_thumbnail (d, &thumb);
190   if (size == 0)
191     {
192       GNUNET_break (0);
193       GNUNET_CONTAINER_meta_data_destroy (d);
194       return 1;
195     }
196   GNUNET_free (thumb);
197   GNUNET_CONTAINER_meta_data_add_publication_date (d);
198   date = GNUNET_CONTAINER_meta_data_get_by_type (d,
199                                                  EXTRACTOR_METATYPE_PUBLICATION_DATE);
200   if (date == NULL)
201     {
202       GNUNET_break (0);
203       GNUNET_CONTAINER_meta_data_destroy (d);
204       return 1;
205     }
206   GNUNET_free (date);
207   GNUNET_CONTAINER_meta_data_destroy (d);
208   return 0;
209 }
210
211
212
213 int
214 main (int argc, char *argv[])
215 {
216   char *const argvx[] = { 
217     "test-fs-file_information",
218     "-c",
219     "test_fs_file_information_data.conf",
220 #if VERBOSE
221     "-L", "DEBUG",
222 #endif
223     NULL
224   };
225   struct GNUNET_GETOPT_CommandLineOption options[] = {
226     GNUNET_GETOPT_OPTION_END
227   };
228
229   GNUNET_log_setup ("test_fs_file_information", 
230 #if VERBOSE
231                     "DEBUG",
232 #else
233                     "WARNING",
234 #endif
235                     NULL);
236   if (0 != testThumbnail ())
237     return 1;
238   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
239                       argvx, "test-fs-file_information",
240                       "nohelp", options, &run, NULL);
241   return 0;
242 }
243
244 /* end of test_fs_file_information.c */