diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-05-29 00:46:26 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-05-29 00:46:26 +0000 |
commit | 0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9 (patch) | |
tree | 6b552f40eb089db96409a312a98d9b12bd669102 /src/util/test_disk.c | |
download | gnunet-0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9.tar.gz gnunet-0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9.zip |
ng
Diffstat (limited to 'src/util/test_disk.c')
-rw-r--r-- | src/util/test_disk.c | 274 |
1 files changed, 274 insertions, 0 deletions
diff --git a/src/util/test_disk.c b/src/util/test_disk.c new file mode 100644 index 000000000..0d385afa7 --- /dev/null +++ b/src/util/test_disk.c | |||
@@ -0,0 +1,274 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2001, 2002, 2003, 2005, 2006 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 util/test_disk.c | ||
23 | * @brief testcase for the storage module | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_common.h" | ||
28 | #include "gnunet_disk_lib.h" | ||
29 | #include "gnunet_scheduler_lib.h" | ||
30 | |||
31 | #define TESTSTRING "Hello World\0" | ||
32 | |||
33 | static int | ||
34 | testReadWrite () | ||
35 | { | ||
36 | char tmp[100 + 1]; | ||
37 | int ret; | ||
38 | |||
39 | if (GNUNET_OK != | ||
40 | GNUNET_DISK_file_write (".testfile", TESTSTRING, strlen (TESTSTRING), | ||
41 | "644")) | ||
42 | return 1; | ||
43 | if (GNUNET_OK != GNUNET_DISK_file_test (".testfile")) | ||
44 | return 1; | ||
45 | ret = GNUNET_DISK_file_read (".testfile", sizeof (tmp) - 1, tmp); | ||
46 | if (ret < 0) | ||
47 | { | ||
48 | fprintf (stderr, | ||
49 | "Error reading file `%s' in testReadWrite\n", ".testfile"); | ||
50 | return 1; | ||
51 | } | ||
52 | tmp[ret] = '\0'; | ||
53 | if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) | ||
54 | { | ||
55 | fprintf (stderr, | ||
56 | "Error in testReadWrite: *%s* != *%s* for file %s\n", | ||
57 | tmp, TESTSTRING, ".testfile"); | ||
58 | return 1; | ||
59 | } | ||
60 | GNUNET_DISK_file_copy (".testfile", ".testfile2"); | ||
61 | memset (tmp, 0, sizeof (tmp)); | ||
62 | ret = GNUNET_DISK_file_read (".testfile2", sizeof (tmp) - 1, tmp); | ||
63 | if (ret < 0) | ||
64 | { | ||
65 | fprintf (stderr, | ||
66 | "Error reading file `%s' in testReadWrite\n", ".testfile2"); | ||
67 | return 1; | ||
68 | } | ||
69 | tmp[ret] = '\0'; | ||
70 | if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1)) | ||
71 | { | ||
72 | fprintf (stderr, | ||
73 | "Error in testReadWrite: *%s* != *%s* for file %s\n", | ||
74 | tmp, TESTSTRING, ".testfile2"); | ||
75 | return 1; | ||
76 | } | ||
77 | |||
78 | GNUNET_break (0 == UNLINK (".testfile")); | ||
79 | GNUNET_break (0 == UNLINK (".testfile2")); | ||
80 | if (GNUNET_NO != GNUNET_DISK_file_test (".testfile")) | ||
81 | return 1; | ||
82 | |||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | static int | ||
87 | testOpenClose () | ||
88 | { | ||
89 | int fd; | ||
90 | unsigned long long size; | ||
91 | long avail; | ||
92 | |||
93 | fd = GNUNET_DISK_file_open (".testfile", | ||
94 | O_RDWR | O_CREAT, S_IWUSR | S_IRUSR); | ||
95 | GNUNET_assert (-1 != fd); | ||
96 | GNUNET_break (5 == WRITE (fd, "Hello", 5)); | ||
97 | GNUNET_DISK_file_close (".testfile", fd); | ||
98 | GNUNET_break (GNUNET_OK == | ||
99 | GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO)); | ||
100 | if (size != 5) | ||
101 | return 1; | ||
102 | GNUNET_break (0 == UNLINK (".testfile")); | ||
103 | |||
104 | /* test that avail goes down as we fill the disk... */ | ||
105 | GNUNET_log_skip (1); | ||
106 | avail = GNUNET_DISK_get_blocks_available (".testfile"); | ||
107 | GNUNET_log_skip (0); | ||
108 | fd = GNUNET_DISK_file_open (".testfile", | ||
109 | O_RDWR | O_CREAT, S_IWUSR | S_IRUSR); | ||
110 | GNUNET_assert (-1 != fd); | ||
111 | while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) && | ||
112 | (avail != -1)) | ||
113 | if (16 != WRITE (fd, "HelloWorld123456", 16)) | ||
114 | { | ||
115 | GNUNET_DISK_file_close (".testfile", fd); | ||
116 | GNUNET_break (0 == UNLINK (".testfile")); | ||
117 | return 1; | ||
118 | } | ||
119 | GNUNET_DISK_file_close (".testfile", fd); | ||
120 | GNUNET_break (0 == UNLINK (".testfile")); | ||
121 | |||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | static int ok; | ||
126 | |||
127 | static int | ||
128 | scan_callback (void *want, const char *filename) | ||
129 | { | ||
130 | if (NULL != strstr (filename, want)) | ||
131 | ok++; | ||
132 | return GNUNET_OK; | ||
133 | } | ||
134 | |||
135 | static int | ||
136 | testDirScan () | ||
137 | { | ||
138 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry")) | ||
139 | return 1; | ||
140 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more")) | ||
141 | return 1; | ||
142 | GNUNET_DISK_directory_scan ("test", &scan_callback, "test/entry"); | ||
143 | if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) | ||
144 | return 1; | ||
145 | if (ok < 2) | ||
146 | return 1; | ||
147 | return 0; | ||
148 | } | ||
149 | |||
150 | static void | ||
151 | iter_callback (void *cls, | ||
152 | struct GNUNET_DISK_DirectoryIterator *di, | ||
153 | const char *filename, const char *dirname) | ||
154 | { | ||
155 | int *i = cls; | ||
156 | (*i)++; | ||
157 | GNUNET_DISK_directory_iterator_next (di, GNUNET_NO); | ||
158 | } | ||
159 | |||
160 | static void | ||
161 | iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
162 | { | ||
163 | GNUNET_DISK_directory_iterator_start (tc->sched, | ||
164 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
165 | "test", &iter_callback, cls); | ||
166 | } | ||
167 | |||
168 | static int | ||
169 | testDirIter () | ||
170 | { | ||
171 | int i; | ||
172 | |||
173 | i = 0; | ||
174 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry")) | ||
175 | return 1; | ||
176 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many")) | ||
177 | return 1; | ||
178 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more")) | ||
179 | return 1; | ||
180 | GNUNET_SCHEDULER_run (&iter_task, &i); | ||
181 | if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) | ||
182 | return 1; | ||
183 | if (i < 3) | ||
184 | return 1; | ||
185 | return 0; | ||
186 | } | ||
187 | |||
188 | |||
189 | static int | ||
190 | testGetHome () | ||
191 | { | ||
192 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
193 | char *fn; | ||
194 | int ret; | ||
195 | |||
196 | cfg = GNUNET_CONFIGURATION_create (); | ||
197 | GNUNET_assert (cfg != NULL); | ||
198 | GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME", | ||
199 | "/tmp/a/b/c"); | ||
200 | fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL); | ||
201 | GNUNET_assert (fn != NULL); | ||
202 | GNUNET_CONFIGURATION_destroy (cfg); | ||
203 | ret = strcmp ("/tmp/a/b/c/d/e", fn); | ||
204 | GNUNET_free (fn); | ||
205 | return ret; | ||
206 | } | ||
207 | |||
208 | static int | ||
209 | testCanonicalize () | ||
210 | { | ||
211 | char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\""); | ||
212 | GNUNET_DISK_filename_canonicalize (fn); | ||
213 | if (0 != strcmp (fn, "ab____cd_ef__g_")) | ||
214 | { | ||
215 | GNUNET_free (fn); | ||
216 | return 1; | ||
217 | } | ||
218 | GNUNET_free (fn); | ||
219 | return 0; | ||
220 | } | ||
221 | |||
222 | static int | ||
223 | testChangeOwner () | ||
224 | { | ||
225 | GNUNET_log_skip (1); | ||
226 | if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser")) | ||
227 | return 1; | ||
228 | return 0; | ||
229 | } | ||
230 | |||
231 | static int | ||
232 | testDirMani () | ||
233 | { | ||
234 | if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing")) | ||
235 | return 1; | ||
236 | if (GNUNET_NO != GNUNET_DISK_file_test ("test")) | ||
237 | return 1; | ||
238 | if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing")) | ||
239 | return 1; | ||
240 | if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) | ||
241 | return 1; | ||
242 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test")) | ||
243 | return 1; | ||
244 | if (GNUNET_YES != GNUNET_DISK_directory_test ("test")) | ||
245 | return 1; | ||
246 | if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) | ||
247 | return 1; | ||
248 | |||
249 | |||
250 | return 0; | ||
251 | } | ||
252 | |||
253 | |||
254 | int | ||
255 | main (int argc, char *argv[]) | ||
256 | { | ||
257 | unsigned int failureCount = 0; | ||
258 | |||
259 | GNUNET_log_setup ("test-disk", "WARNING", NULL); | ||
260 | failureCount += testReadWrite (); | ||
261 | failureCount += testOpenClose (); | ||
262 | failureCount += testDirScan (); | ||
263 | failureCount += testDirIter (); | ||
264 | failureCount += testGetHome (); | ||
265 | failureCount += testCanonicalize (); | ||
266 | failureCount += testChangeOwner (); | ||
267 | failureCount += testDirMani (); | ||
268 | if (failureCount != 0) | ||
269 | { | ||
270 | fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount); | ||
271 | return -1; | ||
272 | } | ||
273 | return 0; | ||
274 | } /* end of main */ | ||