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