-fix #2378
[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, 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, NULL);
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
141
142
143 int
144 main (int argc, char *argv[])
145 {
146   char *const argvx[] = {
147     "test-fs-file_information",
148     "-c",
149     "test_fs_file_information_data.conf",
150 #if VERBOSE
151     "-L", "DEBUG",
152 #endif
153     NULL
154   };
155   struct GNUNET_GETOPT_CommandLineOption options[] = {
156     GNUNET_GETOPT_OPTION_END
157   };
158
159   GNUNET_log_setup ("test_fs_file_information",
160 #if VERBOSE
161                     "DEBUG",
162 #else
163                     "WARNING",
164 #endif
165                     NULL);
166   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
167                       "test-fs-file_information", "nohelp", options, &run,
168                       NULL);
169   return 0;
170 }
171
172 /* end of test_fs_file_information.c */