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