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