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 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   size_t i;
87
88   fn1 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
89   buf = GNUNET_malloc (FILESIZE);
90   for (i = 0; i < FILESIZE; i++)
91     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
92   GNUNET_assert (FILESIZE ==
93                  GNUNET_DISK_fn_write (fn1,
94                                        buf,
95                                        FILESIZE,
96                                        GNUNET_DISK_PERM_USER_READ | 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 | GNUNET_DISK_PERM_USER_WRITE));
108   GNUNET_free (buf);
109
110   meta = GNUNET_CONTAINER_meta_data_create ();
111   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
112   fi1 = GNUNET_FS_file_information_create_from_file ("file_information-context1",
113                                                      fn1,
114                                                      kuri,
115                                                      meta,
116                                                      GNUNET_YES,
117                                                      1,
118                                                      42,
119                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
120   fi2 = GNUNET_FS_file_information_create_from_file ("file_information-context2",
121                                                      fn2,
122                                                      kuri,
123                                                      meta,
124                                                      GNUNET_YES,
125                                                      1,
126                                                      42,
127                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
128   fidir = GNUNET_FS_file_information_create_empty_directory ("file_information-context-dir",
129                                                              kuri,
130                                                              meta,
131                                                              1,
132                                                              42,
133                                                              GNUNET_TIME_relative_to_absolute (LIFETIME)); 
134   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
135   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
136   GNUNET_FS_uri_destroy (kuri);
137   GNUNET_CONTAINER_meta_data_destroy (meta);
138   GNUNET_assert (NULL != fidir);
139   /* FIXME: test more of API! */
140   GNUNET_FS_file_information_destroy (fidir,
141                                       &mycleaner,
142                                       NULL);
143   GNUNET_DISK_directory_remove (fn1);
144   GNUNET_DISK_directory_remove (fn2);
145   GNUNET_free_non_null (fn1);
146   GNUNET_free_non_null (fn2);
147 }
148
149
150 int
151 main (int argc, char *argv[])
152 {
153   char *const argvx[] = { 
154     "test-fs-file_information",
155     "-c",
156     "test_fs_file_information_data.conf",
157 #if VERBOSE
158     "-L", "DEBUG",
159 #endif
160     NULL
161   };
162   struct GNUNET_GETOPT_CommandLineOption options[] = {
163     GNUNET_GETOPT_OPTION_END
164   };
165
166   GNUNET_log_setup ("test_fs_file_information", 
167 #if VERBOSE
168                     "DEBUG",
169 #else
170                     "WARNING",
171 #endif
172                     NULL);
173   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
174                       argvx, "test-fs-file_information",
175                       "nohelp", options, &run, NULL);
176   return 0;
177 }
178
179 /* end of test_fs_file_information.c */