1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Tests for Tunnel Cache
5  * PROGRAMMER:      Pierre Schweitzer <pierre.schweitzer@reactos.org>
6  */
7 
8 #include "precomp.h"
9 
10 static
11 void
12 Test_VeryLongTests(void)
13 {
14     UCHAR i = 0;
15     HANDLE hFile;
16     CHAR TestDir[] = "XTestDirTunnelCache";
17     CHAR OldDir[MAX_PATH];
18     FILETIME FileTime, File1Time;
19 
20     win_skip("Too long, see: ROSTESTS-177\n");
21     return;
22 
23     /* Create a blank test directory */
24     if (GetCurrentDirectory(MAX_PATH, OldDir) == 0)
25     {
26         win_skip("No test directory available\n");
27         return;
28     }
29 
30     /* Create a blank test directory */
31     for (; i < 10; ++i)
32     {
33         TestDir[0] = '0' + i;
34         if (CreateDirectory(TestDir, NULL))
35         {
36             if (SetCurrentDirectory(TestDir) == 0)
37             {
38                 RemoveDirectory(TestDir);
39                 win_skip("No test directory available\n");
40                 return;
41             }
42 
43             break;
44         }
45     }
46 
47     if (i == 10)
48     {
49         win_skip("No test directory available\n");
50         return;
51     }
52 
53     hFile = CreateFile("file1",
54                        GENERIC_READ | GENERIC_WRITE,
55                        0, NULL,
56                        CREATE_NEW,
57                        FILE_ATTRIBUTE_NORMAL,
58                        NULL);
59     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
60     ok(GetFileTime(hFile, &FileTime, NULL, NULL) != FALSE, "GetFileTime() failed\n");
61     CloseHandle(hFile);
62 
63     /* Wait a least 10ms (resolution of FAT) */
64     /* XXX: Increased to 1s for ReactOS... */
65     Sleep(1000);
66 
67     hFile = CreateFile("file2",
68                        GENERIC_READ | GENERIC_WRITE,
69                        0, NULL,
70                        CREATE_NEW,
71                        FILE_ATTRIBUTE_NORMAL,
72                        NULL);
73     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
74     CloseHandle(hFile);
75 
76     ok(MoveFile("file1", "file") != FALSE, "MoveFile() failed\n");
77     /* Sleep over cache expiry */
78     /* FIXME: Query correct value from registry if it exists:
79      * \HKLM\System\CurrentControlSet\Control\FileSystem\MaximumTunnelEntryAgeInSeconds */
80     Sleep(16000);
81     ok(MoveFile("file2", "file1") != FALSE, "MoveFile() failed\n");
82 
83     hFile = CreateFile("file1",
84                        GENERIC_READ,
85                        0, NULL,
86                        OPEN_EXISTING,
87                        FILE_ATTRIBUTE_NORMAL,
88                        NULL);
89     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
90     ok(GetFileTime(hFile, &File1Time, NULL, NULL) != FALSE, "GetFileTime() failed\n");
91     CloseHandle(hFile);
92 
93     ok(RtlCompareMemory(&FileTime, &File1Time, sizeof(FILETIME)) != sizeof(FILETIME), "Tunnel cache still in action?\n");
94 
95     DeleteFile("file2");
96     DeleteFile("file1");
97     DeleteFile("file");
98 
99     SetCurrentDirectory(OldDir);
100     RemoveDirectory(TestDir);
101 }
102 
103 static
104 void
105 Test_LongTests(void)
106 {
107     UCHAR i = 0;
108     HANDLE hFile;
109     CHAR TestDir[] = "XTestDirTunnelCache";
110     CHAR OldDir[MAX_PATH];
111     FILETIME FileTime, File1Time;
112 
113     /* Create a blank test directory */
114     if (GetCurrentDirectory(MAX_PATH, OldDir) == 0)
115     {
116         win_skip("No test directory available\n");
117         return;
118     }
119 
120     /* Create a blank test directory */
121     for (; i < 10; ++i)
122     {
123         TestDir[0] = '0' + i;
124         if (CreateDirectory(TestDir, NULL))
125         {
126             if (SetCurrentDirectory(TestDir) == 0)
127             {
128                 RemoveDirectory(TestDir);
129                 win_skip("No test directory available\n");
130                 return;
131             }
132 
133             break;
134         }
135     }
136 
137     if (i == 10)
138     {
139         win_skip("No test directory available\n");
140         return;
141     }
142 
143     hFile = CreateFile("file1",
144                        GENERIC_READ | GENERIC_WRITE,
145                        0, NULL,
146                        CREATE_NEW,
147                        FILE_ATTRIBUTE_NORMAL,
148                        NULL);
149     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
150     ok(GetFileTime(hFile, &FileTime, NULL, NULL) != FALSE, "GetFileTime() failed\n");
151     CloseHandle(hFile);
152 
153     /* Wait a least 10ms (resolution of FAT) */
154     /* XXX: Increased to 1s for ReactOS... */
155     Sleep(1000);
156 
157     hFile = CreateFile("file2",
158                        GENERIC_READ | GENERIC_WRITE,
159                        0, NULL,
160                        CREATE_NEW,
161                        FILE_ATTRIBUTE_NORMAL,
162                        NULL);
163     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
164     CloseHandle(hFile);
165 
166     ok(MoveFile("file1", "file") != FALSE, "MoveFile() failed\n");
167     ok(MoveFile("file2", "file1") != FALSE, "MoveFile() failed\n");
168 
169     hFile = CreateFile("file1",
170                        GENERIC_READ,
171                        0, NULL,
172                        OPEN_EXISTING,
173                        FILE_ATTRIBUTE_NORMAL,
174                        NULL);
175     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
176     ok(GetFileTime(hFile, &File1Time, NULL, NULL) != FALSE, "GetFileTime() failed\n");
177     CloseHandle(hFile);
178 
179     ok(RtlCompareMemory(&FileTime, &File1Time, sizeof(FILETIME)) == sizeof(FILETIME), "Tunnel cache failed\n");
180 
181     DeleteFile("file2");
182     DeleteFile("file1");
183     DeleteFile("file");
184 
185     SetCurrentDirectory(OldDir);
186     RemoveDirectory(TestDir);
187 }
188 
189 static
190 void
191 Test_ShortTests(void)
192 {
193     UCHAR i = 0;
194     CHAR ShortName[14];
195     HANDLE hFile, hFind;
196     WIN32_FIND_DATA FileInfo;
197     CHAR TestDir[] = "XTestDirTunnelCache";
198     CHAR OldDir[MAX_PATH];
199 
200     /* Create a blank test directory */
201     if (GetCurrentDirectory(MAX_PATH, OldDir) == 0)
202     {
203         win_skip("No test directory available\n");
204         return;
205     }
206 
207     /* Create a blank test directory */
208     for (; i < 10; ++i)
209     {
210         TestDir[0] = '0' + i;
211         if (CreateDirectory(TestDir, NULL))
212         {
213             if (SetCurrentDirectory(TestDir) == 0)
214             {
215                 RemoveDirectory(TestDir);
216                 win_skip("No test directory available\n");
217                 return;
218             }
219 
220             break;
221         }
222     }
223 
224     if (i == 10)
225     {
226         win_skip("No test directory available\n");
227         return;
228     }
229 
230     hFile = CreateFile("verylongfilename",
231                        GENERIC_READ | GENERIC_WRITE,
232                        0, NULL,
233                        CREATE_NEW,
234                        FILE_ATTRIBUTE_NORMAL,
235                        NULL);
236     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
237     CloseHandle(hFile);
238 
239     hFind = FindFirstFile("verylongfilename", &FileInfo);
240     ok(hFind != INVALID_HANDLE_VALUE, "FindFirstFile() failed\n");
241     FindClose(hFind);
242     RtlCopyMemory(ShortName, FileInfo.cAlternateFileName, sizeof(ShortName));
243 
244     ok(MoveFile("verylongfilename", "verylongfilename2") != FALSE, "MoveFile() failed\n");
245     hFind = FindFirstFile("verylongfilename2", &FileInfo);
246     ok(hFind != INVALID_HANDLE_VALUE, "FindFirstFile() failed\n");
247     FindClose(hFind);
248     ok(strcmp(FileInfo.cAlternateFileName, ShortName) == 0, "strcmp() failed\n");
249 
250     hFile = CreateFile("randomfilename",
251                        GENERIC_READ | GENERIC_WRITE,
252                        0, NULL,
253                        CREATE_NEW,
254                        FILE_ATTRIBUTE_NORMAL,
255                        NULL);
256     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
257     CloseHandle(hFile);
258 
259     ok(MoveFileEx("randomfilename", "verylongfilename2", MOVEFILE_REPLACE_EXISTING) != FALSE, "MoveFile() failed\n");
260     hFind = FindFirstFile("verylongfilename2", &FileInfo);
261     ok(hFind != INVALID_HANDLE_VALUE, "FindFirstFile() failed\n");
262     FindClose(hFind);
263     ok(strcmp(FileInfo.cAlternateFileName, ShortName) == 0, "strcmp() failed\n");
264 
265     DeleteFile("randomfilename");
266     DeleteFile("verylongfilename");
267     DeleteFile("verylongfilename2");
268 
269     SetCurrentDirectory(OldDir);
270     RemoveDirectory(TestDir);
271 }
272 
273 START_TEST(TunnelCache)
274 {
275     Test_ShortTests();
276     Test_LongTests();
277     Test_VeryLongTests();
278 }
279