[ROSTESTS]
[reactos.git] / rostests / winetests / kernel32 / volume.c
1 /*
2 * Unit test suite for volume functions
3 *
4 * Copyright 2006 Stefan Leichter
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include <stdio.h>
24
25 static HINSTANCE hdll;
26 static BOOL (WINAPI * pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
27 static BOOL (WINAPI * pGetVolumeNameForVolumeMountPointW)(LPCWSTR, LPWSTR, DWORD);
28 static HANDLE (WINAPI *pFindFirstVolumeA)(LPSTR,DWORD);
29 static BOOL (WINAPI *pFindNextVolumeA)(HANDLE,LPSTR,DWORD);
30 static BOOL (WINAPI *pFindVolumeClose)(HANDLE);
31 static UINT (WINAPI *pGetLogicalDriveStringsA)(UINT,LPSTR);
32 static UINT (WINAPI *pGetLogicalDriveStringsW)(UINT,LPWSTR);
33 static BOOL (WINAPI *pGetVolumeInformationA)(LPCSTR, LPSTR, DWORD, LPDWORD, LPDWORD, LPDWORD, LPSTR, DWORD);
34
35 /* ############################### */
36
37 static void test_query_dos_deviceA(void)
38 {
39 char drivestr[] = "a:";
40 char *p, *buffer, buffer2[2000];
41 DWORD ret, ret2, buflen=32768;
42 BOOL found = FALSE;
43
44 if (!pFindFirstVolumeA) {
45 win_skip("On win9x, HARDDISK and RAMDISK not present\n");
46 return;
47 }
48
49 buffer = HeapAlloc( GetProcessHeap(), 0, buflen );
50 ret = QueryDosDeviceA( NULL, buffer, buflen );
51 ok(ret && GetLastError() != ERROR_INSUFFICIENT_BUFFER,
52 "QueryDosDevice buffer too small\n");
53 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
54 HeapFree( GetProcessHeap(), 0, buffer );
55 return;
56 }
57 ok(ret, "QueryDosDeviceA failed to return list, last error %u\n", GetLastError());
58 if (ret) {
59 p = buffer;
60 for (;;) {
61 if (!strlen(p)) break;
62 ret2 = QueryDosDeviceA( p, buffer2, sizeof(buffer2) );
63 ok(ret2, "QueryDosDeviceA failed to return current mapping for %s, last error %u\n", p, GetLastError());
64 p += strlen(p) + 1;
65 if (ret <= (p-buffer)) break;
66 }
67 }
68
69 for (;drivestr[0] <= 'z'; drivestr[0]++) {
70 /* Older W2K fails with ERROR_INSUFFICIENT_BUFFER when buflen is > 32767 */
71 ret = QueryDosDeviceA( drivestr, buffer, buflen - 1);
72 if(ret) {
73 for (p = buffer; *p; p++) *p = toupper(*p);
74 if (strstr(buffer, "HARDDISK") || strstr(buffer, "RAMDISK")) found = TRUE;
75 }
76 }
77 ok(found, "expected at least one devicename to contain HARDDISK or RAMDISK\n");
78 HeapFree( GetProcessHeap(), 0, buffer );
79 }
80
81 static void test_FindFirstVolume(void)
82 {
83 char volume[51];
84 HANDLE handle;
85
86 if (!pFindFirstVolumeA) {
87 skip("FindFirstVolumeA not found\n");
88 return;
89 }
90
91 handle = pFindFirstVolumeA( volume, 0 );
92 ok( handle == INVALID_HANDLE_VALUE, "succeeded with short buffer\n" );
93 ok( GetLastError() == ERROR_MORE_DATA || /* XP */
94 GetLastError() == ERROR_FILENAME_EXCED_RANGE, /* Vista */
95 "wrong error %u\n", GetLastError() );
96 handle = pFindFirstVolumeA( volume, 49 );
97 ok( handle == INVALID_HANDLE_VALUE, "succeeded with short buffer\n" );
98 ok( GetLastError() == ERROR_FILENAME_EXCED_RANGE, "wrong error %u\n", GetLastError() );
99 handle = pFindFirstVolumeA( volume, 51 );
100 ok( handle != INVALID_HANDLE_VALUE, "failed err %u\n", GetLastError() );
101 if (handle != INVALID_HANDLE_VALUE)
102 {
103 do
104 {
105 ok( strlen(volume) == 49, "bad volume name %s\n", volume );
106 ok( !memcmp( volume, "\\\\?\\Volume{", 11 ), "bad volume name %s\n", volume );
107 ok( !memcmp( volume + 47, "}\\", 2 ), "bad volume name %s\n", volume );
108 } while (pFindNextVolumeA( handle, volume, MAX_PATH ));
109 ok( GetLastError() == ERROR_NO_MORE_FILES, "wrong error %u\n", GetLastError() );
110 pFindVolumeClose( handle );
111 }
112 }
113
114 static void test_GetVolumeNameForVolumeMountPointA(void)
115 {
116 BOOL ret;
117 char volume[MAX_PATH], path[] = "c:\\";
118 DWORD len = sizeof(volume), reti;
119 char temp_path[MAX_PATH];
120
121 /* not present before w2k */
122 if (!pGetVolumeNameForVolumeMountPointA) {
123 skip("GetVolumeNameForVolumeMountPointA not found\n");
124 return;
125 }
126
127 reti = GetTempPathA(MAX_PATH, temp_path);
128 ok(reti != 0, "GetTempPathA error %d\n", GetLastError());
129 ok(reti < MAX_PATH, "temp path should fit into MAX_PATH\n");
130
131 ret = pGetVolumeNameForVolumeMountPointA(path, volume, 0);
132 ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
133 ok(GetLastError() == ERROR_FILENAME_EXCED_RANGE ||
134 GetLastError() == ERROR_INVALID_PARAMETER, /* Vista */
135 "wrong error, last=%d\n", GetLastError());
136
137 if (0) { /* these crash on XP */
138 ret = pGetVolumeNameForVolumeMountPointA(path, NULL, len);
139 ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
140
141 ret = pGetVolumeNameForVolumeMountPointA(NULL, volume, len);
142 ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
143 }
144
145 ret = pGetVolumeNameForVolumeMountPointA(path, volume, len);
146 ok(ret == TRUE, "GetVolumeNameForVolumeMountPointA failed\n");
147 ok(!strncmp( volume, "\\\\?\\Volume{", 11),
148 "GetVolumeNameForVolumeMountPointA failed to return valid string <%s>\n",
149 volume);
150
151 /* test with too small buffer */
152 ret = pGetVolumeNameForVolumeMountPointA(path, volume, 10);
153 ok(ret == FALSE && GetLastError() == ERROR_FILENAME_EXCED_RANGE,
154 "GetVolumeNameForVolumeMountPointA failed, wrong error returned, was %d, should be ERROR_FILENAME_EXCED_RANGE\n",
155 GetLastError());
156
157 /* Try on a arbitrary directory */
158 /* On FAT filesystems it seems that GetLastError() is set to
159 ERROR_INVALID_FUNCTION. */
160 ret = pGetVolumeNameForVolumeMountPointA(temp_path, volume, len);
161 ok(ret == FALSE && (GetLastError() == ERROR_NOT_A_REPARSE_POINT ||
162 GetLastError() == ERROR_INVALID_FUNCTION),
163 "GetVolumeNameForVolumeMountPointA failed on %s, last=%d\n",
164 temp_path, GetLastError());
165
166 /* Try on a nonexistent dos drive */
167 path[2] = 0;
168 for (;path[0] <= 'z'; path[0]++) {
169 ret = QueryDosDeviceA( path, volume, len);
170 if(!ret) break;
171 }
172 if (path[0] <= 'z')
173 {
174 path[2] = '\\';
175 ret = pGetVolumeNameForVolumeMountPointA(path, volume, len);
176 ok(ret == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
177 "GetVolumeNameForVolumeMountPointA failed on %s, last=%d\n",
178 path, GetLastError());
179
180 /* Try without trailing \ and on a nonexistent dos drive */
181 path[2] = 0;
182 ret = pGetVolumeNameForVolumeMountPointA(path, volume, len);
183 ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
184 "GetVolumeNameForVolumeMountPointA failed on %s, last=%d\n",
185 path, GetLastError());
186 }
187 }
188
189 static void test_GetVolumeNameForVolumeMountPointW(void)
190 {
191 BOOL ret;
192 WCHAR volume[MAX_PATH], path[] = {'c',':','\\',0};
193 DWORD len = sizeof(volume) / sizeof(WCHAR);
194
195 /* not present before w2k */
196 if (!pGetVolumeNameForVolumeMountPointW) {
197 skip("GetVolumeNameForVolumeMountPointW not found\n");
198 return;
199 }
200
201 ret = pGetVolumeNameForVolumeMountPointW(path, volume, 0);
202 ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
203 ok(GetLastError() == ERROR_FILENAME_EXCED_RANGE ||
204 GetLastError() == ERROR_INVALID_PARAMETER, /* Vista */
205 "wrong error, last=%d\n", GetLastError());
206
207 if (0) { /* these crash on XP */
208 ret = pGetVolumeNameForVolumeMountPointW(path, NULL, len);
209 ok(ret == FALSE, "GetVolumeNameForVolumeMountPointW succeeded\n");
210
211 ret = pGetVolumeNameForVolumeMountPointW(NULL, volume, len);
212 ok(ret == FALSE, "GetVolumeNameForVolumeMountPointW succeeded\n");
213 }
214
215 ret = pGetVolumeNameForVolumeMountPointW(path, volume, len);
216 ok(ret == TRUE, "GetVolumeNameForVolumeMountPointW failed\n");
217 }
218
219 static void test_GetLogicalDriveStringsA(void)
220 {
221 UINT size, size2;
222 char *buf, *ptr;
223
224 if(!pGetLogicalDriveStringsA) {
225 win_skip("GetLogicalDriveStringsA not available\n");
226 return;
227 }
228
229 size = pGetLogicalDriveStringsA(0, NULL);
230 ok(size%4 == 1, "size = %d\n", size);
231
232 buf = HeapAlloc(GetProcessHeap(), 0, size);
233
234 *buf = 0;
235 size2 = pGetLogicalDriveStringsA(2, buf);
236 ok(size2 == size, "size2 = %d\n", size2);
237 ok(!*buf, "buf changed\n");
238
239 size2 = pGetLogicalDriveStringsA(size, buf);
240 ok(size2 == size-1, "size2 = %d\n", size2);
241
242 for(ptr = buf; ptr < buf+size2; ptr += 4) {
243 ok(('A' <= *ptr && *ptr <= 'Z') ||
244 (broken('a' <= *ptr && *ptr <= 'z')), /* Win9x and WinMe */
245 "device name '%c' is not uppercase\n", *ptr);
246 ok(ptr[1] == ':', "ptr[1] = %c, expected ':'\n", ptr[1]);
247 ok(ptr[2] == '\\', "ptr[2] = %c expected '\\'\n", ptr[2]);
248 ok(!ptr[3], "ptr[3] = %c expected nullbyte\n", ptr[3]);
249 }
250 ok(!*ptr, "buf[size2] is not nullbyte\n");
251
252 HeapFree(GetProcessHeap(), 0, buf);
253 }
254
255 static void test_GetLogicalDriveStringsW(void)
256 {
257 UINT size, size2;
258 WCHAR *buf, *ptr;
259
260 if(!pGetLogicalDriveStringsW) {
261 win_skip("GetLogicalDriveStringsW not available\n");
262 return;
263 }
264
265 SetLastError(0xdeadbeef);
266 size = pGetLogicalDriveStringsW(0, NULL);
267 if (size == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
268 win_skip("GetLogicalDriveStringsW not implemented\n");
269 return;
270 }
271 ok(size%4 == 1, "size = %d\n", size);
272
273 buf = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
274
275 *buf = 0;
276 size2 = pGetLogicalDriveStringsW(2, buf);
277 ok(size2 == size, "size2 = %d\n", size2);
278 ok(!*buf, "buf changed\n");
279
280 size2 = pGetLogicalDriveStringsW(size, buf);
281 ok(size2 == size-1, "size2 = %d\n", size2);
282
283 for(ptr = buf; ptr < buf+size2; ptr += 4) {
284 ok('A' <= *ptr && *ptr <= 'Z', "device name '%c' is not uppercase\n", *ptr);
285 ok(ptr[1] == ':', "ptr[1] = %c, expected ':'\n", ptr[1]);
286 ok(ptr[2] == '\\', "ptr[2] = %c expected '\\'\n", ptr[2]);
287 ok(!ptr[3], "ptr[3] = %c expected nullbyte\n", ptr[3]);
288 }
289 ok(!*ptr, "buf[size2] is not nullbyte\n");
290
291 HeapFree(GetProcessHeap(), 0, buf);
292 }
293
294 static void test_GetVolumeInformationA(void)
295 {
296 BOOL ret;
297 UINT result;
298 char Root_Dir0[]="C:";
299 char Root_Dir1[]="C:\\";
300 char Root_Dir2[]="\\\\?\\C:\\";
301 char volume[MAX_PATH+1];
302 DWORD vol_name_size=MAX_PATH+1, vol_serial_num=-1, max_comp_len=0, fs_flags=0, fs_name_len=MAX_PATH+1;
303 char vol_name_buf[MAX_PATH+1], fs_name_buf[MAX_PATH+1];
304 char windowsdir[MAX_PATH+10];
305 char currentdir[MAX_PATH+1];
306
307 if (!pGetVolumeInformationA) {
308 win_skip("GetVolumeInformationA not found\n");
309 return;
310 }
311 if (!pGetVolumeNameForVolumeMountPointA) {
312 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
313 return;
314 }
315
316 /* get windows drive letter and update strings for testing */
317 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
318 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
319 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
320 Root_Dir0[0] = windowsdir[0];
321 Root_Dir1[0] = windowsdir[0];
322 Root_Dir2[4] = windowsdir[0];
323
324 /* get the unique volume name for the windows drive */
325 ret = pGetVolumeNameForVolumeMountPointA(Root_Dir1, volume, MAX_PATH);
326 ok(ret == TRUE, "GetVolumeNameForVolumeMountPointA failed\n");
327
328 result = GetCurrentDirectory(MAX_PATH, currentdir);
329 ok(result, "GetCurrentDirectory: error %d\n", GetLastError());
330
331 /* **** now start the tests **** */
332 /* check for error on no trailing \ */
333 if (result > 3)
334 {
335 ret = pGetVolumeInformationA(Root_Dir0, vol_name_buf, vol_name_size, NULL,
336 NULL, NULL, fs_name_buf, fs_name_len);
337 ok(!ret && GetLastError() == ERROR_INVALID_NAME,
338 "GetVolumeInformationA w/o '\\' did not fail, last error %u\n", GetLastError());
339 }
340 else
341 skip("Running on a root directory\n");
342
343 /* check for error on no trailing \ when current dir is root dir */
344 ret = SetCurrentDirectory(Root_Dir1);
345 ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
346 ret = pGetVolumeInformationA(Root_Dir0, vol_name_buf, vol_name_size, NULL,
347 NULL, NULL, fs_name_buf, fs_name_len);
348 todo_wine
349 ok(ret, "GetVolumeInformationA failed, last error %u\n", GetLastError());
350
351 /* check for error on no trailing \ when current dir is windows dir */
352 ret = SetCurrentDirectory(windowsdir);
353 ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
354 ret = pGetVolumeInformationA(Root_Dir0, vol_name_buf, vol_name_size, NULL,
355 NULL, NULL, fs_name_buf, fs_name_len);
356 ok(!ret && GetLastError() == ERROR_INVALID_NAME,
357 "GetVolumeInformationA did not fail, last error %u\n", GetLastError());
358
359 /* reset current directory */
360 ret = SetCurrentDirectory(currentdir);
361 ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
362
363 /* try null root directory to return "root of the current directory" */
364 ret = pGetVolumeInformationA(NULL, vol_name_buf, vol_name_size, NULL,
365 NULL, NULL, fs_name_buf, fs_name_len);
366 ok(ret, "GetVolumeInformationA failed on null root dir, last error %u\n", GetLastError());
367
368 /* Try normal drive letter with trailing \ */
369 ret = pGetVolumeInformationA(Root_Dir1, vol_name_buf, vol_name_size,
370 &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
371 ok(ret, "GetVolumeInformationA failed, root=%s, last error=%u\n", Root_Dir1, GetLastError());
372
373 /* try again with dirve letter and the "disable parsing" prefix */
374 ret = pGetVolumeInformationA(Root_Dir2, vol_name_buf, vol_name_size,
375 &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
376 todo_wine ok(ret, "GetVolumeInformationA failed, root=%s, last error=%u\n", Root_Dir2, GetLastError());
377
378 /* try again with unique voluem name */
379 ret = pGetVolumeInformationA(volume, vol_name_buf, vol_name_size,
380 &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
381 todo_wine ok(ret, "GetVolumeInformationA failed, root=%s, last error=%u\n", volume, GetLastError());
382
383 /* try again with device name space */
384 Root_Dir2[2] = '.';
385 ret = pGetVolumeInformationA(Root_Dir2, vol_name_buf, vol_name_size,
386 &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
387 todo_wine ok(ret, "GetVolumeInformationA failed, root=%s, last error=%u\n", Root_Dir2, GetLastError());
388
389 /* try again with a directory off the root - should generate error */
390 if (windowsdir[strlen(windowsdir)-1] != '\\') strcat(windowsdir, "\\");
391 ret = pGetVolumeInformationA(windowsdir, vol_name_buf, vol_name_size,
392 &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
393 todo_wine ok(!ret && GetLastError()==ERROR_DIR_NOT_ROOT,
394 "GetVolumeInformationA failed, root=%s, last error=%u\n", windowsdir, GetLastError());
395 }
396
397 /* Test to check that unique volume name from windows dir mount point */
398 /* matches at least one of the unique volume names returned from the */
399 /* FindFirstVolumeA/FindNextVolumeA list. */
400 static void test_enum_vols(void)
401 {
402 DWORD ret;
403 HANDLE hFind = INVALID_HANDLE_VALUE;
404 char Volume_1[MAX_PATH] = {0};
405 char Volume_2[MAX_PATH] = {0};
406 char path[] = "c:\\";
407 BOOL found = FALSE;
408 char windowsdir[MAX_PATH];
409
410 if (!pGetVolumeNameForVolumeMountPointA) {
411 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
412 return;
413 }
414
415 /*get windows drive letter and update strings for testing */
416 ret = GetWindowsDirectory( windowsdir, sizeof(windowsdir) );
417 ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
418 ok(ret != 0, "GetWindowsDirecory: error %d\n", GetLastError());
419 path[0] = windowsdir[0];
420
421 /* get the unique volume name for the windows drive */
422 ret = pGetVolumeNameForVolumeMountPointA( path, Volume_1, MAX_PATH );
423 ok(ret == TRUE, "GetVolumeNameForVolumeMountPointA failed\n");
424 ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name %s\n", Volume_1);
425
426 /* get first unique volume name of list */
427 hFind = pFindFirstVolumeA( Volume_2, MAX_PATH );
428 ok(hFind != INVALID_HANDLE_VALUE, "FindFirstVolume failed, err=%u\n",
429 GetLastError());
430 if (hFind != INVALID_HANDLE_VALUE) {
431 do
432 {
433 /* validate correct length of unique volume name */
434 ok(strlen(Volume_2) == 49, "Find[First/Next]Volume returned wrong length name %s\n", Volume_1);
435 if (memcmp(Volume_1, Volume_2, 49) == 0)
436 {
437 found = TRUE;
438 break;
439 }
440 } while (pFindNextVolumeA( hFind, Volume_2, MAX_PATH ));
441 ok(found, "volume name %s not found by Find[First/Next]Volume\n", Volume_1);
442 pFindVolumeClose( hFind );
443 }
444 }
445
446 START_TEST(volume)
447 {
448 hdll = GetModuleHandleA("kernel32.dll");
449 pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hdll, "GetVolumeNameForVolumeMountPointA");
450 pGetVolumeNameForVolumeMountPointW = (void *) GetProcAddress(hdll, "GetVolumeNameForVolumeMountPointW");
451 pFindFirstVolumeA = (void *) GetProcAddress(hdll, "FindFirstVolumeA");
452 pFindNextVolumeA = (void *) GetProcAddress(hdll, "FindNextVolumeA");
453 pFindVolumeClose = (void *) GetProcAddress(hdll, "FindVolumeClose");
454 pGetLogicalDriveStringsA = (void *) GetProcAddress(hdll, "GetLogicalDriveStringsA");
455 pGetLogicalDriveStringsW = (void *) GetProcAddress(hdll, "GetLogicalDriveStringsW");
456 pGetVolumeInformationA = (void *) GetProcAddress(hdll, "GetVolumeInformationA");
457
458 test_query_dos_deviceA();
459 test_FindFirstVolume();
460 test_GetVolumeNameForVolumeMountPointA();
461 test_GetVolumeNameForVolumeMountPointW();
462 test_GetLogicalDriveStringsA();
463 test_GetLogicalDriveStringsW();
464 test_GetVolumeInformationA();
465 test_enum_vols();
466 }