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     ros_skip_flaky
180     ok(RtlCompareMemory(&FileTime, &File1Time, sizeof(FILETIME)) == sizeof(FILETIME), "Tunnel cache failed\n");
181 
182     DeleteFile("file2");
183     DeleteFile("file1");
184     DeleteFile("file");
185 
186     SetCurrentDirectory(OldDir);
187     RemoveDirectory(TestDir);
188 }
189 
190 static
191 void
192 Test_ShortTests(void)
193 {
194     UCHAR i = 0;
195     CHAR ShortName[14];
196     HANDLE hFile, hFind;
197     WIN32_FIND_DATA FileInfo;
198     CHAR TestDir[] = "XTestDirTunnelCache";
199     CHAR OldDir[MAX_PATH];
200 
201     /* Create a blank test directory */
202     if (GetCurrentDirectory(MAX_PATH, OldDir) == 0)
203     {
204         win_skip("No test directory available\n");
205         return;
206     }
207 
208     /* Create a blank test directory */
209     for (; i < 10; ++i)
210     {
211         TestDir[0] = '0' + i;
212         if (CreateDirectory(TestDir, NULL))
213         {
214             if (SetCurrentDirectory(TestDir) == 0)
215             {
216                 RemoveDirectory(TestDir);
217                 win_skip("No test directory available\n");
218                 return;
219             }
220 
221             break;
222         }
223     }
224 
225     if (i == 10)
226     {
227         win_skip("No test directory available\n");
228         return;
229     }
230 
231     hFile = CreateFile("verylongfilename",
232                        GENERIC_READ | GENERIC_WRITE,
233                        0, NULL,
234                        CREATE_NEW,
235                        FILE_ATTRIBUTE_NORMAL,
236                        NULL);
237     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
238     CloseHandle(hFile);
239 
240     hFind = FindFirstFile("verylongfilename", &FileInfo);
241     ok(hFind != INVALID_HANDLE_VALUE, "FindFirstFile() failed\n");
242     FindClose(hFind);
243     RtlCopyMemory(ShortName, FileInfo.cAlternateFileName, sizeof(ShortName));
244 
245     ok(MoveFile("verylongfilename", "verylongfilename2") != FALSE, "MoveFile() failed\n");
246     hFind = FindFirstFile("verylongfilename2", &FileInfo);
247     ok(hFind != INVALID_HANDLE_VALUE, "FindFirstFile() failed\n");
248     FindClose(hFind);
249     ok(strcmp(FileInfo.cAlternateFileName, ShortName) == 0, "strcmp() failed\n");
250 
251     hFile = CreateFile("randomfilename",
252                        GENERIC_READ | GENERIC_WRITE,
253                        0, NULL,
254                        CREATE_NEW,
255                        FILE_ATTRIBUTE_NORMAL,
256                        NULL);
257     ok(hFile != INVALID_HANDLE_VALUE, "CreateFile() failed\n");
258     CloseHandle(hFile);
259 
260     ok(MoveFileEx("randomfilename", "verylongfilename2", MOVEFILE_REPLACE_EXISTING) != FALSE, "MoveFile() failed\n");
261     hFind = FindFirstFile("verylongfilename2", &FileInfo);
262     ok(hFind != INVALID_HANDLE_VALUE, "FindFirstFile() failed\n");
263     FindClose(hFind);
264     ok(strcmp(FileInfo.cAlternateFileName, ShortName) == 0, "strcmp() failed\n");
265 
266     DeleteFile("randomfilename");
267     DeleteFile("verylongfilename");
268     DeleteFile("verylongfilename2");
269 
270     SetCurrentDirectory(OldDir);
271     RemoveDirectory(TestDir);
272 }
273 
274 START_TEST(TunnelCache)
275 {
276     Test_ShortTests();
277     Test_LongTests();
278     Test_VeryLongTests();
279 }
280