:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / apps / tests / test_old / testfsd.c
1 /* $Id$
2  *
3  * FILE:          testFSD.c
4  * PURPOSE:       A test set for the File System Driver
5  * PROJECT:       ReactOS kernel
6  * COPYRIGHT:     See COPYING in the top level directory
7  * PROGRAMMER:    Rex Jolliff (rex@lvcablemodem.com)
8  *
9  */
10
11 #include <errno.h>
12 #include <stdio.h>
13 #include <windows.h>
14 #include <string.h>
15
16 #include "testsuite.h"
17
18 const char *rootDir = "c:\\";
19 const char *systemRootDir = "c:\\ReactOS";
20 const char *systemDllDir = "c:\\ReactOS\\System32";
21 const char *bogusRootFile = "c:\\bogus";
22 const char *bogusDirAndFile = "c:\\bogus\\bogus";
23 const char *bogusSystemRootFile = "c:\\ReactOS\\bogus";
24 const char *bogusSystemDllFile = "c:\\ReactOS\\System32\\bogus";
25 const char *shortFile = "c:\\ReactOS\\boot.bat";
26 const char *longFile = "c:\\ReactOS\\loadros.com";
27
28 void  testOpenExistant (void)
29 {
30   HANDLE  fileHandle;
31
32   fileHandle  = CreateFile (rootDir, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
33   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
34   CloseHandle (fileHandle);
35   fileHandle  = CreateFile (systemRootDir, GENERIC_READ, 0, 0, 
36                             OPEN_EXISTING, 0, 0);
37   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
38   CloseHandle (fileHandle);
39   fileHandle  = CreateFile (systemDllDir, GENERIC_READ, 0, 0, 
40                             OPEN_EXISTING, 0, 0);
41   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
42   CloseHandle (fileHandle);
43 }
44
45 void  testOpenNonExistant (void)
46 {
47   DWORD  status;
48   HANDLE  fileHandle;
49
50   fileHandle  = CreateFile (bogusRootFile, GENERIC_READ, 0, 0, 
51                             OPEN_EXISTING, 0, 0);
52   ASSERT (fileHandle == INVALID_HANDLE_VALUE);
53   status = GetLastError ();
54   ASSERT (status == ERROR_FILE_NOT_FOUND);
55   CloseHandle (fileHandle);
56   fileHandle  = CreateFile (bogusDirAndFile, GENERIC_READ, 0, 0, 
57                             OPEN_EXISTING, 0, 0);
58   ASSERT (fileHandle == INVALID_HANDLE_VALUE);
59   status = GetLastError ();
60   ASSERT (status == ERROR_PATH_NOT_FOUND);
61   CloseHandle (fileHandle);
62   fileHandle  = CreateFile (bogusSystemRootFile, GENERIC_READ, 0, 0, 
63                             OPEN_EXISTING, 0, 0);
64   ASSERT (fileHandle == INVALID_HANDLE_VALUE);
65   status = GetLastError ();
66   ASSERT (status == ERROR_FILE_NOT_FOUND);
67   CloseHandle (fileHandle);
68   fileHandle  = CreateFile (bogusSystemDllFile, GENERIC_READ, 0, 0, 
69                             OPEN_EXISTING, 0, 0);
70   ASSERT (fileHandle == INVALID_HANDLE_VALUE);
71   status = GetLastError ();
72   ASSERT (status == ERROR_FILE_NOT_FOUND);
73   CloseHandle (fileHandle);
74 }
75
76 void  testCreateExistant (void)
77 {
78   DWORD  status;
79   HANDLE  fileHandle;
80
81   fileHandle  = CreateFile (rootDir, GENERIC_READ, 0, 0, 
82                             CREATE_NEW, 0, 0);
83   ASSERT (fileHandle == INVALID_HANDLE_VALUE);
84   status = GetLastError ();
85   ASSERT (status == ERROR_ALREADY_EXISTS);
86   fileHandle  = CreateFile (systemRootDir, GENERIC_READ, 0, 0, 
87                             CREATE_NEW, 0, 0);
88   ASSERT (fileHandle == INVALID_HANDLE_VALUE);
89   status = GetLastError ();
90   ASSERT (status == ERROR_ALREADY_EXISTS);
91   fileHandle  = CreateFile (systemDllDir, GENERIC_READ, 0, 0, 
92                             CREATE_NEW, 0, 0);
93   ASSERT (fileHandle == INVALID_HANDLE_VALUE);
94   status = GetLastError ();
95   ASSERT (status == ERROR_ALREADY_EXISTS);
96 }
97
98 void  testCreateNonExistant (void)
99 {
100   DWORD  status;
101   HANDLE  fileHandle;
102
103   fileHandle  = CreateFile (bogusRootFile, GENERIC_READ, 0, 0, 
104                             CREATE_NEW, 0, 0);
105   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
106   CloseHandle (fileHandle);
107   fileHandle  = CreateFile (bogusSystemRootFile, GENERIC_READ, 0, 0, 
108                             CREATE_NEW, 0, 0);
109   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
110   CloseHandle (fileHandle);
111   fileHandle  = CreateFile (bogusSystemDllFile, GENERIC_READ, 0, 0, 
112                             CREATE_NEW, 0, 0);
113   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
114   CloseHandle (fileHandle);
115 #if 0
116   fileHandle  = CreateFile (bogusDirAndFile, GENERIC_READ, 0, 0, 
117                             CREATE_NEW, 0, 0);
118   ASSERT (fileHandle == INVALID_HANDLE_VALUE);
119   status = GetLastError ();
120   ASSERT (status == ERROR_PATH_NOT_FOUND);
121   CloseHandle (fileHandle);
122 #endif
123 }
124
125 void testDeleteFiles (void)
126 {
127   BOOL  returnValue;
128   HANDLE  fileHandle;
129
130   fileHandle  = CreateFile ("bogus", GENERIC_READ, 0, 0, 
131                             CREATE_NEW, 0, 0);
132   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
133   CloseHandle (fileHandle);
134   returnValue = DeleteFile ("bogus");
135   ASSERT_MSG (returnValue, 
136               "Delete of bogus failed, error:%d", 
137               GetLastError ());
138
139 #if 0
140   returnValue = DeleteFile (bogusRootFile);
141   ASSERT_MSG (returnValue, 
142               "Delete of %s failed, error:%d", 
143               bogusRootFile, 
144               GetLastError ());
145   returnValue = DeleteFile (bogusRootFile);
146   ASSERT_MSG (!returnValue, 
147               "2nd Delete of %s succeeded but should fail",
148               bogusRootFile);
149   returnValue = DeleteFile (bogusSystemRootFile);
150   ASSERT_MSG (returnValue, 
151               "Delete of %s failed, error:%d", 
152               bogusSystemRootFile,
153               GetLastError ());
154   returnValue = DeleteFile (bogusSystemRootFile);
155   ASSERT_MSG (!returnValue, 
156               "2nd Delete of %s succeeded but should fail",
157               bogusSystemRootFile);
158   returnValue = DeleteFile (bogusSystemDllFile);
159   ASSERT_MSG (returnValue, 
160               "Delete of %s failed, error:%d", 
161               bogusSystemDllFile,
162               GetLastError ());
163   returnValue = DeleteFile (bogusSystemDllFile);
164   ASSERT_MSG (!returnValue, 
165               "2nd Delete of %s succeeded but should fail",
166               bogusSystemDllFile);
167   returnValue = DeleteFile (bogusDirAndFile);
168   ASSERT_MSG (!returnValue, 
169               "Delete of %s succeded but should fail",
170               bogusDirAndFile);
171 #endif
172 }
173
174 void  testOpenWithBlankPathElements (void)
175 {
176   HANDLE  fileHandle;
177
178   fileHandle = CreateFile ("c:", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
179   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
180   CloseHandle (fileHandle);
181   fileHandle = CreateFile ("c:\\\\", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
182   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
183   CloseHandle (fileHandle);
184   fileHandle = CreateFile ("c:\\\\reactos\\", GENERIC_READ, 0, 0, 
185                             OPEN_EXISTING, 0, 0);
186   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
187   CloseHandle (fileHandle);
188   fileHandle = CreateFile ("c:\\reactos\\\\", GENERIC_READ, 0, 0, 
189                             OPEN_EXISTING, 0, 0);
190   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
191   CloseHandle (fileHandle);
192   fileHandle = CreateFile ("c:\\reactos\\\\system32\\", GENERIC_READ, 0, 0, 
193                             OPEN_EXISTING, 0, 0);
194   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
195   CloseHandle (fileHandle);
196   fileHandle = CreateFile ("c:\\reactos\\system32\\\\", GENERIC_READ, 0, 0, 
197                             OPEN_EXISTING, 0, 0);
198   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
199   CloseHandle (fileHandle);
200 }
201
202 void testReadOnInvalidHandle (void)
203 {
204   BOOL  returnValue;
205   char  buffer [256];
206   DWORD  bytesRead;
207
208   returnValue = ReadFile (INVALID_HANDLE_VALUE, 
209                           buffer, 
210                           256, 
211                           &bytesRead, 
212                           NULL);
213   ASSERT_MSG (!returnValue,
214               "Read from invalid handle succeeded but should fail",0);
215   ASSERT (GetLastError () != ENOFILE);
216 }
217
218 void testReadOnZeroLengthFile (void)
219 {
220   BOOL  returnValue;
221   HANDLE  fileHandle;
222   char  buffer [256];
223   DWORD  bytesRead;
224
225   fileHandle  = CreateFile (bogusRootFile, GENERIC_READ, 0, 0, 
226                             CREATE_NEW, 0, 0);
227   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
228   CloseHandle (fileHandle);
229   fileHandle  = CreateFile (bogusRootFile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
230   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
231   returnValue = ReadFile (fileHandle, buffer, 256, &bytesRead, NULL);
232   ASSERT_MSG (!returnValue,
233               "Read from zero length file succeeded but should fail",0);
234   ASSERT (GetLastError () != ERROR_HANDLE_EOF);
235   CloseHandle (fileHandle);
236   returnValue = DeleteFile (bogusRootFile);
237   ASSERT_MSG (returnValue, 
238               "Delete of %s failed, error:%d", 
239               bogusRootFile, 
240               GetLastError ());
241 }
242
243 void testReadOnShortFile (void)
244 {
245   BOOL  returnValue;
246   HANDLE  fileHandle;
247   char  buffer [256];
248   DWORD  bytesRead;
249
250   fileHandle  = CreateFile (shortFile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
251   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
252   returnValue = ReadFile (fileHandle, 
253                           buffer, 
254                           256, 
255                           &bytesRead, 
256                           NULL);
257   ASSERT_MSG (returnValue,
258               "Read from short length file failed, error:%d",
259               GetLastError ());
260   ASSERT (bytesRead > 0 && bytesRead < 256);
261   CloseHandle (fileHandle);
262 }
263
264 void testReadOnLongFile (void)
265 {
266   BOOL  returnValue;
267   HANDLE  fileHandle;
268   char  buffer [256];
269   DWORD  bytesRead;
270   int  count;
271
272   fileHandle  = CreateFile (longFile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
273   ASSERT (fileHandle != INVALID_HANDLE_VALUE);
274   for (count = 0; count < 20; count++)
275   {
276     returnValue = ReadFile (fileHandle, 
277                             buffer, 
278                             256, 
279                             &bytesRead, 
280                             NULL); 
281     ASSERT_MSG (returnValue,
282                 "Read from short length file failed, error:%d",
283                 GetLastError ());
284     ASSERT (bytesRead == 256);
285   }
286   CloseHandle (fileHandle);
287 }
288
289 int main (void)
290 {
291   TEST_RUNNER  testRunner;
292   TEST_SUITE  testsToRun [] =
293   {
294     ADD_TEST (testOpenExistant),
295     ADD_TEST (testOpenNonExistant),
296     ADD_TEST (testCreateExistant),
297     ADD_TEST (testCreateNonExistant),
298     ADD_TEST (testDeleteFiles),
299 /*    ADD_TEST (testOverwriteExistant),*/
300 /*    ADD_TEST (testOverwriteNonExistant),*/
301     ADD_TEST (testOpenWithBlankPathElements),
302     ADD_TEST (testReadOnInvalidHandle),
303     ADD_TEST (testReadOnZeroLengthFile),
304     ADD_TEST (testReadOnShortFile),
305     ADD_TEST (testReadOnLongFile),
306 /*    ADD_TEST (test), */
307     END_TESTS
308   };
309
310   memset (&testRunner, 0, sizeof (TEST_RUNNER));
311   tsRunTests (&testRunner, testsToRun);
312   tsReportResults (&testRunner);
313 }
314
315