fixing block reconstruction start/shutdown code
[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           uint32_t *anonymity,
59           uint32_t *priority,
60           int *do_index,
61           struct GNUNET_TIME_Absolute *expirationTime,
62           void **client_info)
63 {
64   return GNUNET_OK;
65 }
66
67
68 static void
69 run (void *cls,
70      char *const *args,
71      const char *cfgfile,
72      const struct GNUNET_CONFIGURATION_Handle *cfg)
73 {
74   const char *keywords[] = {
75     "down_foo",
76     "down_bar",
77   };
78   char *fn1;
79   char *fn2;
80   char *buf;
81   struct GNUNET_CONTAINER_MetaData *meta;
82   struct GNUNET_FS_Uri *kuri;
83   struct GNUNET_FS_FileInformation *fi1;
84   struct GNUNET_FS_FileInformation *fi2;
85   struct GNUNET_FS_FileInformation *fidir;
86   struct GNUNET_FS_Handle *fs;
87   size_t i;
88
89   fs = GNUNET_FS_start (cfg, "test-fs-file-information", NULL, NULL,
90                         GNUNET_FS_FLAGS_NONE,
91                         GNUNET_FS_OPTIONS_END);
92   fn1 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
93   buf = GNUNET_malloc (FILESIZE);
94   for (i = 0; i < FILESIZE; i++)
95     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
96   GNUNET_assert (FILESIZE ==
97                  GNUNET_DISK_fn_write (fn1,
98                                        buf,
99                                        FILESIZE,
100                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
101   GNUNET_free (buf);
102
103   fn2 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
104   buf = GNUNET_malloc (FILESIZE);
105   for (i = 0; i < FILESIZE; i++)
106     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
107   GNUNET_assert (FILESIZE ==
108                  GNUNET_DISK_fn_write (fn2,
109                                        buf,
110                                        FILESIZE,
111                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
112   GNUNET_free (buf);
113
114   meta = GNUNET_CONTAINER_meta_data_create ();
115   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
116   fi1 = GNUNET_FS_file_information_create_from_file (fs,
117                                                      "file_information-context1",
118                                                      fn1,
119                                                      kuri,
120                                                      meta,
121                                                      GNUNET_YES,
122                                                      1,
123                                                      42,
124                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
125   GNUNET_assert (fi1 != NULL);
126   fi2 = GNUNET_FS_file_information_create_from_file (fs,
127                                                      "file_information-context2",
128                                                      fn2,
129                                                      kuri,
130                                                      meta,
131                                                      GNUNET_YES,
132                                                      1,
133                                                      42,
134                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
135   GNUNET_assert (fi2 != NULL);
136   fidir = GNUNET_FS_file_information_create_empty_directory (fs,
137                                                              "file_information-context-dir",
138                                                              kuri,
139                                                              meta,
140                                                              1,
141                                                              42,
142                                                              GNUNET_TIME_relative_to_absolute (LIFETIME)); 
143   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
144   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
145   GNUNET_FS_uri_destroy (kuri);
146   GNUNET_CONTAINER_meta_data_destroy (meta);
147   GNUNET_assert (NULL != fidir);
148   /* FIXME: test more of API! */
149   GNUNET_FS_file_information_destroy (fidir,
150                                       &mycleaner,
151                                       NULL);
152   GNUNET_DISK_directory_remove (fn1);
153   GNUNET_DISK_directory_remove (fn2);
154   GNUNET_free_non_null (fn1);
155   GNUNET_free_non_null (fn2);
156   GNUNET_FS_stop (fs);
157 }
158
159
160 static int
161 testThumbnail ()
162 {
163   struct GNUNET_CONTAINER_MetaData *m;
164   struct GNUNET_CONTAINER_MetaData *d;
165   struct EXTRACTOR_PluginList *ex;
166   unsigned char *thumb;
167   size_t size;
168   char *date;
169
170   ex = EXTRACTOR_plugin_add_config (NULL, "thumbnailgtk", EXTRACTOR_OPTION_DEFAULT_POLICY);
171   if (ex == NULL)
172     {
173       fprintf (stderr,
174                "Test incomplete, have no GTK thumbnail extractor available.\n");
175       return 0;                 /* can not test, no thumbnailer */
176     }
177   ex = EXTRACTOR_plugin_add_config (ex, "mime", EXTRACTOR_OPTION_DEFAULT_POLICY);
178   m = GNUNET_CONTAINER_meta_data_create ();
179   if (3 != GNUNET_FS_meta_data_extract_from_file (m,
180                                                   "test_fs_file_information_meta_data_image.jpg",
181                                                   ex))
182     {
183       GNUNET_break (0);
184       EXTRACTOR_plugin_remove_all (ex);
185       GNUNET_CONTAINER_meta_data_destroy (m);
186       return 1;
187     }
188   EXTRACTOR_plugin_remove_all (ex);
189   d = GNUNET_CONTAINER_meta_data_duplicate (m);
190   GNUNET_CONTAINER_meta_data_destroy (m);
191   thumb = NULL;
192   size = GNUNET_CONTAINER_meta_data_get_thumbnail (d, &thumb);
193   if (size == 0)
194     {
195       GNUNET_break (0);
196       GNUNET_CONTAINER_meta_data_destroy (d);
197       return 1;
198     }
199   GNUNET_free (thumb);
200   GNUNET_CONTAINER_meta_data_add_publication_date (d);
201   date = GNUNET_CONTAINER_meta_data_get_by_type (d,
202                                                  EXTRACTOR_METATYPE_PUBLICATION_DATE);
203   if (date == NULL)
204     {
205       GNUNET_break (0);
206       GNUNET_CONTAINER_meta_data_destroy (d);
207       return 1;
208     }
209   GNUNET_free (date);
210   GNUNET_CONTAINER_meta_data_destroy (d);
211   return 0;
212 }
213
214
215
216 int
217 main (int argc, char *argv[])
218 {
219   char *const argvx[] = { 
220     "test-fs-file_information",
221     "-c",
222     "test_fs_file_information_data.conf",
223 #if VERBOSE
224     "-L", "DEBUG",
225 #endif
226     NULL
227   };
228   struct GNUNET_GETOPT_CommandLineOption options[] = {
229     GNUNET_GETOPT_OPTION_END
230   };
231
232   GNUNET_log_setup ("test_fs_file_information", 
233 #if VERBOSE
234                     "DEBUG",
235 #else
236                     "WARNING",
237 #endif
238                     NULL);
239   if (0 != testThumbnail ())
240     return 1;
241   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
242                       argvx, "test-fs-file_information",
243                       "nohelp", options, &run, NULL);
244   return 0;
245 }
246
247 /* end of test_fs_file_information.c */