removing dead code
[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 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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
134 static int
135 testDirIter ()
136 {
137   int i;
138
139   i = 0;
140   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
141     return 1;
142   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
143     return 1;
144   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
145     return 1;
146   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
147     return 1;
148   if (i < 3)
149     return 1;
150   return 0;
151 }
152
153
154 static int
155 testCanonicalize ()
156 {
157   char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
158
159   GNUNET_DISK_filename_canonicalize (fn);
160   if (0 != strcmp (fn, "ab____cd_ef__g_"))
161   {
162     GNUNET_free (fn);
163     return 1;
164   }
165   GNUNET_free (fn);
166   return 0;
167 }
168
169
170 static int
171 testChangeOwner ()
172 {
173 #ifndef WINDOWS
174   GNUNET_log_skip (1, GNUNET_NO);
175   if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
176     return 1;
177 #endif
178   return 0;
179 }
180
181
182 static int
183 testDirMani ()
184 {
185   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
186     return 1;
187   if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
188     return 1;
189   if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
190     return 1;
191   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
192     return 1;
193   if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
194     return 1;
195   if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
196     return 1;
197   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
198     return 1;
199   return 0;
200 }
201
202
203 int
204 main (int argc, char *argv[])
205 {
206   unsigned int failureCount = 0;
207
208   GNUNET_log_setup ("test-disk", "WARNING", NULL);
209   failureCount += testReadWrite ();
210   failureCount += testOpenClose ();
211   failureCount += testDirScan ();
212   failureCount += testDirIter ();
213   failureCount += testCanonicalize ();
214   failureCount += testChangeOwner ();
215   failureCount += testDirMani ();
216   if (failureCount != 0)
217   {
218     FPRINTF (stderr, "\n%u TESTS FAILED!\n", failureCount);
219     return -1;
220   }
221   return 0;
222 }                               /* end of main */