glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / fs / test_fs_file_information.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2008, 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file fs/test_fs_file_information.c
18  * @brief simple testcase for file_information operations
19  * @author Christian Grothoff
20  *
21  * TODO:
22  * - test that metatdata, etc. are all correct (for example,
23  *   there is a known bug with dirname never being set that is
24  *   not detected!)
25  * - need to iterate over file-information structure
26  * - other API functions may not yet be tested (such as
27  *   filedata-from-callback)
28  */
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fs_service.h"
32
33
34 /**
35  * File-size we use for testing.
36  */
37 #define FILESIZE (1024 * 1024 * 2)
38
39 /**
40  * How long should our test-content live?
41  */
42 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
43
44
45 static int
46 mycleaner (void *cls, struct GNUNET_FS_FileInformation *fi, uint64_t length,
47            struct GNUNET_CONTAINER_MetaData *meta, struct GNUNET_FS_Uri **uri,
48            struct GNUNET_FS_BlockOptions *bo, int *do_index, void **client_info)
49 {
50   return GNUNET_OK;
51 }
52
53
54 static void
55 run (void *cls, char *const *args, const char *cfgfile,
56      const struct GNUNET_CONFIGURATION_Handle *cfg)
57 {
58   const char *keywords[] = {
59     "down_foo",
60     "down_bar",
61   };
62   char *fn1;
63   char *fn2;
64   char *buf;
65   struct GNUNET_CONTAINER_MetaData *meta;
66   struct GNUNET_FS_Uri *kuri;
67   struct GNUNET_FS_FileInformation *fi1;
68   struct GNUNET_FS_FileInformation *fi2;
69   struct GNUNET_FS_FileInformation *fidir;
70   struct GNUNET_FS_Handle *fs;
71   size_t i;
72   struct GNUNET_FS_BlockOptions bo;
73
74   fs = GNUNET_FS_start (cfg, "test-fs-file-information", NULL, NULL,
75                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
76   fn1 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
77   buf = GNUNET_malloc (FILESIZE);
78   for (i = 0; i < FILESIZE; i++)
79     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
80   GNUNET_assert (FILESIZE ==
81                  GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
82                                        GNUNET_DISK_PERM_USER_READ |
83                                        GNUNET_DISK_PERM_USER_WRITE));
84   GNUNET_free (buf);
85
86   fn2 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
87   buf = GNUNET_malloc (FILESIZE);
88   for (i = 0; i < FILESIZE; i++)
89     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
90   GNUNET_assert (FILESIZE ==
91                  GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
92                                        GNUNET_DISK_PERM_USER_READ |
93                                        GNUNET_DISK_PERM_USER_WRITE));
94   GNUNET_free (buf);
95
96   meta = GNUNET_CONTAINER_meta_data_create ();
97   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
98   bo.content_priority = 42;
99   bo.anonymity_level = 1;
100   bo.replication_level = 0;
101   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
102   fi1 =
103       GNUNET_FS_file_information_create_from_file (fs,
104                                                    "file_information-context1",
105                                                    fn1, kuri, meta, GNUNET_YES,
106                                                    &bo);
107   GNUNET_assert (fi1 != NULL);
108   fi2 =
109       GNUNET_FS_file_information_create_from_file (fs,
110                                                    "file_information-context2",
111                                                    fn2, kuri, meta, GNUNET_YES,
112                                                    &bo);
113   GNUNET_assert (fi2 != NULL);
114   fidir =
115       GNUNET_FS_file_information_create_empty_directory (fs,
116                                                          "file_information-context-dir",
117                                                          kuri, meta, &bo, NULL);
118   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
119   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
120   GNUNET_FS_uri_destroy (kuri);
121   GNUNET_CONTAINER_meta_data_destroy (meta);
122   GNUNET_assert (NULL != fidir);
123   /* FIXME: test more of API! */
124   GNUNET_FS_file_information_destroy (fidir, &mycleaner, NULL);
125   GNUNET_DISK_directory_remove (fn1);
126   GNUNET_DISK_directory_remove (fn2);
127   GNUNET_free_non_null (fn1);
128   GNUNET_free_non_null (fn2);
129   GNUNET_FS_stop (fs);
130 }
131
132
133 int
134 main (int argc, char *argv[])
135 {
136   char *const argvx[] = {
137     "test-fs-file_information",
138     "-c",
139     "test_fs_file_information_data.conf",
140     NULL
141   };
142   struct GNUNET_GETOPT_CommandLineOption options[] = {
143     GNUNET_GETOPT_OPTION_END
144   };
145
146   GNUNET_log_setup ("test_fs_file_information",
147                     "WARNING",
148                     NULL);
149   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
150                       "test-fs-file_information", "nohelp", options, &run,
151                       NULL);
152   return 0;
153 }
154
155 /* end of test_fs_file_information.c */