-allow 'years' in time units, indentation and doxygen fixes
[oweals/gnunet.git] / src / util / test_disk.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2003, 2005, 2006, 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 util/test_disk.c
23  * @brief testcase for the storage module
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28
29 #define TESTSTRING "Hello World\0"
30
31 static int
32 testReadWrite ()
33 {
34   char tmp[100 + 1];
35   int ret;
36
37   if (strlen (TESTSTRING) !=
38       GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
39                             GNUNET_DISK_PERM_USER_READ |
40                             GNUNET_DISK_PERM_USER_WRITE))
41     return 1;
42   if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
43     return 1;
44   ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
45   if (ret < 0)
46   {
47     FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
48     return 1;
49   }
50   tmp[ret] = '\0';
51   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
52   {
53     FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
54              TESTSTRING, ".testfile");
55     return 1;
56   }
57   GNUNET_DISK_file_copy (".testfile", ".testfile2");
58   memset (tmp, 0, sizeof (tmp));
59   ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
60   if (ret < 0)
61   {
62     FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n",
63              ".testfile2");
64     return 1;
65   }
66   tmp[ret] = '\0';
67   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
68   {
69     FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
70              TESTSTRING, ".testfile2");
71     return 1;
72   }
73
74   GNUNET_break (0 == UNLINK (".testfile"));
75   GNUNET_break (0 == UNLINK (".testfile2"));
76   if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
77     return 1;
78
79   return 0;
80 }
81
82 static int
83 testOpenClose ()
84 {
85   struct GNUNET_DISK_FileHandle *fh;
86   uint64_t size;
87
88   fh = GNUNET_DISK_file_open (".testfile",
89                               GNUNET_DISK_OPEN_READWRITE |
90                               GNUNET_DISK_OPEN_CREATE,
91                               GNUNET_DISK_PERM_USER_READ |
92                               GNUNET_DISK_PERM_USER_WRITE);
93   GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
94   GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
95   GNUNET_DISK_file_close (fh);
96   GNUNET_break (GNUNET_OK ==
97                 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO, GNUNET_YES));
98   if (size != 5)
99     return 1;
100   GNUNET_break (0 == UNLINK (".testfile"));
101
102   return 0;
103 }
104
105 static int ok;
106
107 static int
108 scan_callback (void *want, const char *filename)
109 {
110   if (NULL != strstr (filename, want))
111     ok++;
112   return GNUNET_OK;
113 }
114
115 static int
116 testDirScan ()
117 {
118   if (GNUNET_OK !=
119       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
120     return 1;
121   if (GNUNET_OK !=
122       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
123     return 1;
124   GNUNET_DISK_directory_scan ("test", &scan_callback,
125                               "test" DIR_SEPARATOR_STR "entry");
126   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
127     return 1;
128   if (ok < 2)
129     return 1;
130   return 0;
131 }
132
133 static void
134 iter_callback (void *cls, struct GNUNET_DISK_DirectoryIterator *di,
135                const char *filename, const char *dirname)
136 {
137   int *i = cls;
138
139   (*i)++;
140   GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
141 }
142
143 static void
144 iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
145 {
146   GNUNET_DISK_directory_iterator_start (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
147                                         "test", &iter_callback, cls);
148 }
149
150 static int
151 testDirIter ()
152 {
153   int i;
154
155   i = 0;
156   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
157     return 1;
158   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
159     return 1;
160   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
161     return 1;
162   GNUNET_SCHEDULER_run (&iter_task, &i);
163   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
164     return 1;
165   if (i < 3)
166     return 1;
167   return 0;
168 }
169
170
171 static int
172 testCanonicalize ()
173 {
174   char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
175
176   GNUNET_DISK_filename_canonicalize (fn);
177   if (0 != strcmp (fn, "ab____cd_ef__g_"))
178   {
179     GNUNET_free (fn);
180     return 1;
181   }
182   GNUNET_free (fn);
183   return 0;
184 }
185
186 static int
187 testChangeOwner ()
188 {
189 #ifndef WINDOWS
190   GNUNET_log_skip (1, GNUNET_NO);
191   if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
192     return 1;
193 #endif
194   return 0;
195 }
196
197 static int
198 testDirMani ()
199 {
200   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
201     return 1;
202   if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
203     return 1;
204   if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
205     return 1;
206   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
207     return 1;
208   if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
209     return 1;
210   if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
211     return 1;
212   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
213     return 1;
214
215
216   return 0;
217 }
218
219
220 int
221 main (int argc, char *argv[])
222 {
223   unsigned int failureCount = 0;
224
225   GNUNET_log_setup ("test-disk", "WARNING", NULL);
226   failureCount += testReadWrite ();
227   failureCount += testOpenClose ();
228   failureCount += testDirScan ();
229   failureCount += testDirIter ();
230   failureCount += testCanonicalize ();
231   failureCount += testChangeOwner ();
232   failureCount += testDirMani ();
233   if (failureCount != 0)
234   {
235     FPRINTF (stderr, "\n%u TESTS FAILED!\n", failureCount);
236     return -1;
237   }
238   return 0;
239 }                               /* end of main */