fix
[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 2, 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           struct GNUNET_TIME_Absolute *expirationTime,
61           void **client_info)
62 {
63   return GNUNET_OK;
64 }
65
66
67 static void
68 run (void *cls,
69      struct GNUNET_SCHEDULER_Handle *s,
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 (s, 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   fi2 = GNUNET_FS_file_information_create_from_file (fs,
126                                                      "file_information-context2",
127                                                      fn2,
128                                                      kuri,
129                                                      meta,
130                                                      GNUNET_YES,
131                                                      1,
132                                                      42,
133                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
134   fidir = GNUNET_FS_file_information_create_empty_directory (fs,
135                                                              "file_information-context-dir",
136                                                              kuri,
137                                                              meta,
138                                                              1,
139                                                              42,
140                                                              GNUNET_TIME_relative_to_absolute (LIFETIME)); 
141   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
142   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
143   GNUNET_FS_uri_destroy (kuri);
144   GNUNET_CONTAINER_meta_data_destroy (meta);
145   GNUNET_assert (NULL != fidir);
146   /* FIXME: test more of API! */
147   GNUNET_FS_file_information_destroy (fidir,
148                                       &mycleaner,
149                                       NULL);
150   GNUNET_DISK_directory_remove (fn1);
151   GNUNET_DISK_directory_remove (fn2);
152   GNUNET_free_non_null (fn1);
153   GNUNET_free_non_null (fn2);
154   GNUNET_FS_stop (fs);
155 }
156
157
158 int
159 main (int argc, char *argv[])
160 {
161   char *const argvx[] = { 
162     "test-fs-file_information",
163     "-c",
164     "test_fs_file_information_data.conf",
165 #if VERBOSE
166     "-L", "DEBUG",
167 #endif
168     NULL
169   };
170   struct GNUNET_GETOPT_CommandLineOption options[] = {
171     GNUNET_GETOPT_OPTION_END
172   };
173
174   GNUNET_log_setup ("test_fs_file_information", 
175 #if VERBOSE
176                     "DEBUG",
177 #else
178                     "WARNING",
179 #endif
180                     NULL);
181   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
182                       argvx, "test-fs-file_information",
183                       "nohelp", options, &run, NULL);
184   return 0;
185 }
186
187 /* end of test_fs_file_information.c */