kernel32/tests: Avoid size_t in traces.
[wine] / dlls / kernel32 / tests / 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 "winioctl.h"
24 #include <stdio.h>
25
26 static HINSTANCE hdll;
27 static BOOL (WINAPI * pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
28 static BOOL (WINAPI * pGetVolumeNameForVolumeMountPointW)(LPCWSTR, LPWSTR, DWORD);
29 static HANDLE (WINAPI *pFindFirstVolumeA)(LPSTR,DWORD);
30 static BOOL (WINAPI *pFindNextVolumeA)(HANDLE,LPSTR,DWORD);
31 static BOOL (WINAPI *pFindVolumeClose)(HANDLE);
32 static UINT (WINAPI *pGetLogicalDriveStringsA)(UINT,LPSTR);
33 static UINT (WINAPI *pGetLogicalDriveStringsW)(UINT,LPWSTR);
34 static BOOL (WINAPI *pGetVolumeInformationA)(LPCSTR, LPSTR, DWORD, LPDWORD, LPDWORD, LPDWORD, LPSTR, DWORD);
35 static BOOL (WINAPI *pGetVolumePathNamesForVolumeNameA)(LPCSTR, LPSTR, DWORD, LPDWORD);
36 static BOOL (WINAPI *pGetVolumePathNamesForVolumeNameW)(LPCWSTR, LPWSTR, DWORD, LPDWORD);
37
38 /* ############################### */
39
40 static void test_query_dos_deviceA(void)
41 {
42     char drivestr[] = "a:";
43     char *p, *buffer, buffer2[2000];
44     DWORD ret, ret2, buflen=32768;
45     BOOL found = FALSE;
46
47     /* callers must guess the buffer size */
48     SetLastError(0xdeadbeef);
49     ret = QueryDosDeviceA( NULL, NULL, 0 );
50     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
51        "QueryDosDeviceA(no buffer): returned %u, le=%u\n", ret, GetLastError());
52
53     buffer = HeapAlloc( GetProcessHeap(), 0, buflen );
54     SetLastError(0xdeadbeef);
55     ret = QueryDosDeviceA( NULL, buffer, buflen );
56     ok((ret && GetLastError() != ERROR_INSUFFICIENT_BUFFER),
57         "QueryDosDeviceA failed to return list, last error %u\n", GetLastError());
58
59     if (ret && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
60         p = buffer;
61         for (;;) {
62             if (!strlen(p)) break;
63             ret2 = QueryDosDeviceA( p, buffer2, sizeof(buffer2) );
64             ok(ret2, "QueryDosDeviceA failed to return current mapping for %s, last error %u\n", p, GetLastError());
65             p += strlen(p) + 1;
66             if (ret <= (p-buffer)) break;
67         }
68     }
69
70     for (;drivestr[0] <= 'z'; drivestr[0]++) {
71         /* Older W2K fails with ERROR_INSUFFICIENT_BUFFER when buflen is > 32767 */
72         ret = QueryDosDeviceA( drivestr, buffer, buflen - 1);
73         ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND,
74             "QueryDosDeviceA failed to return current mapping for %s, last error %u\n", drivestr, GetLastError());
75         if(ret) {
76             for (p = buffer; *p; p++) *p = toupper(*p);
77             if (strstr(buffer, "HARDDISK") || strstr(buffer, "RAMDISK")) found = TRUE;
78         }
79     }
80     ok(found, "expected at least one devicename to contain HARDDISK or RAMDISK\n");
81     HeapFree( GetProcessHeap(), 0, buffer );
82 }
83
84 static void test_define_dos_deviceA(void)
85 {
86     char drivestr[3];
87     char buf[MAX_PATH];
88     DWORD ret;
89
90     /* Find an unused drive letter */
91     drivestr[1] = ':';
92     drivestr[2] = 0;
93     for (drivestr[0] = 'a'; drivestr[0] <= 'z'; drivestr[0]++) {
94         ret = QueryDosDeviceA( drivestr, buf, sizeof(buf));
95         if (!ret) break;
96     }
97     if (drivestr[0] > 'z') {
98         skip("can't test creating a dos drive, none available\n");
99         return;
100     }
101
102     /* Map it to point to the current directory */
103     ret = GetCurrentDirectory(sizeof(buf), buf);
104     ok(ret, "GetCurrentDir\n");
105
106     ret = DefineDosDeviceA(0, drivestr, buf);
107     todo_wine
108     ok(ret, "Could not make drive %s point to %s!\n", drivestr, buf);
109
110     if (!ret) {
111         skip("can't test removing fake drive\n");
112     } else {
113         ret = DefineDosDeviceA(DDD_REMOVE_DEFINITION, drivestr, NULL);
114         ok(ret, "Could not remove fake drive %s!\n", drivestr);
115     }
116 }
117
118 static void test_FindFirstVolume(void)
119 {
120     char volume[51];
121     HANDLE handle;
122
123     /* not present before w2k */
124     if (!pFindFirstVolumeA) {
125         win_skip("FindFirstVolumeA not found\n");
126         return;
127     }
128
129     handle = pFindFirstVolumeA( volume, 0 );
130     ok( handle == INVALID_HANDLE_VALUE, "succeeded with short buffer\n" );
131     ok( GetLastError() == ERROR_MORE_DATA ||  /* XP */
132         GetLastError() == ERROR_FILENAME_EXCED_RANGE,  /* Vista */
133         "wrong error %u\n", GetLastError() );
134     handle = pFindFirstVolumeA( volume, 49 );
135     ok( handle == INVALID_HANDLE_VALUE, "succeeded with short buffer\n" );
136     ok( GetLastError() == ERROR_FILENAME_EXCED_RANGE, "wrong error %u\n", GetLastError() );
137     handle = pFindFirstVolumeA( volume, 51 );
138     ok( handle != INVALID_HANDLE_VALUE, "failed err %u\n", GetLastError() );
139     if (handle != INVALID_HANDLE_VALUE)
140     {
141         do
142         {
143             ok( strlen(volume) == 49, "bad volume name %s\n", volume );
144             ok( !memcmp( volume, "\\\\?\\Volume{", 11 ), "bad volume name %s\n", volume );
145             ok( !memcmp( volume + 47, "}\\", 2 ), "bad volume name %s\n", volume );
146         } while (pFindNextVolumeA( handle, volume, MAX_PATH ));
147         ok( GetLastError() == ERROR_NO_MORE_FILES, "wrong error %u\n", GetLastError() );
148         pFindVolumeClose( handle );
149     }
150 }
151
152 static void test_GetVolumeNameForVolumeMountPointA(void)
153 {
154     BOOL ret;
155     char volume[MAX_PATH], path[] = "c:\\";
156     DWORD len = sizeof(volume), reti;
157     char temp_path[MAX_PATH];
158
159     /* not present before w2k */
160     if (!pGetVolumeNameForVolumeMountPointA) {
161         win_skip("GetVolumeNameForVolumeMountPointA not found\n");
162         return;
163     }
164
165     reti = GetTempPathA(MAX_PATH, temp_path);
166     ok(reti != 0, "GetTempPathA error %d\n", GetLastError());
167     ok(reti < MAX_PATH, "temp path should fit into MAX_PATH\n");
168
169     ret = pGetVolumeNameForVolumeMountPointA(path, volume, 0);
170     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
171     ok(GetLastError() == ERROR_FILENAME_EXCED_RANGE ||
172         GetLastError() == ERROR_INVALID_PARAMETER, /* Vista */
173         "wrong error, last=%d\n", GetLastError());
174
175     if (0) { /* these crash on XP */
176     ret = pGetVolumeNameForVolumeMountPointA(path, NULL, len);
177     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
178
179     ret = pGetVolumeNameForVolumeMountPointA(NULL, volume, len);
180     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
181     }
182
183     ret = pGetVolumeNameForVolumeMountPointA(path, volume, len);
184     ok(ret == TRUE, "GetVolumeNameForVolumeMountPointA failed\n");
185     ok(!strncmp( volume, "\\\\?\\Volume{", 11),
186         "GetVolumeNameForVolumeMountPointA failed to return valid string <%s>\n",
187         volume);
188
189     /* test with too small buffer */
190     ret = pGetVolumeNameForVolumeMountPointA(path, volume, 10);
191     ok(ret == FALSE && GetLastError() == ERROR_FILENAME_EXCED_RANGE,
192             "GetVolumeNameForVolumeMountPointA failed, wrong error returned, was %d, should be ERROR_FILENAME_EXCED_RANGE\n",
193              GetLastError());
194
195     /* Try on a arbitrary directory */
196     /* On FAT filesystems it seems that GetLastError() is set to
197        ERROR_INVALID_FUNCTION. */
198     ret = pGetVolumeNameForVolumeMountPointA(temp_path, volume, len);
199     ok(ret == FALSE && (GetLastError() == ERROR_NOT_A_REPARSE_POINT ||
200         GetLastError() == ERROR_INVALID_FUNCTION),
201         "GetVolumeNameForVolumeMountPointA failed on %s, last=%d\n",
202         temp_path, GetLastError());
203
204     /* Try on a nonexistent dos drive */
205     path[2] = 0;
206     for (;path[0] <= 'z'; path[0]++) {
207         ret = QueryDosDeviceA( path, volume, len);
208         if(!ret) break;
209     }
210     if (path[0] <= 'z')
211     {
212         path[2] = '\\';
213         ret = pGetVolumeNameForVolumeMountPointA(path, volume, len);
214         ok(ret == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
215             "GetVolumeNameForVolumeMountPointA failed on %s, last=%d\n",
216             path, GetLastError());
217
218         /* Try without trailing \ and on a nonexistent dos drive  */
219         path[2] = 0;
220         ret = pGetVolumeNameForVolumeMountPointA(path, volume, len);
221         ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
222             "GetVolumeNameForVolumeMountPointA failed on %s, last=%d\n",
223             path, GetLastError());
224     }
225 }
226
227 static void test_GetVolumeNameForVolumeMountPointW(void)
228 {
229     BOOL ret;
230     WCHAR volume[MAX_PATH], path[] = {'c',':','\\',0};
231     DWORD len = sizeof(volume) / sizeof(WCHAR);
232
233     /* not present before w2k */
234     if (!pGetVolumeNameForVolumeMountPointW) {
235         win_skip("GetVolumeNameForVolumeMountPointW not found\n");
236         return;
237     }
238
239     ret = pGetVolumeNameForVolumeMountPointW(path, volume, 0);
240     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
241     ok(GetLastError() == ERROR_FILENAME_EXCED_RANGE ||
242         GetLastError() == ERROR_INVALID_PARAMETER, /* Vista */
243         "wrong error, last=%d\n", GetLastError());
244
245     if (0) { /* these crash on XP */
246     ret = pGetVolumeNameForVolumeMountPointW(path, NULL, len);
247     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointW succeeded\n");
248
249     ret = pGetVolumeNameForVolumeMountPointW(NULL, volume, len);
250     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointW succeeded\n");
251     }
252
253     ret = pGetVolumeNameForVolumeMountPointW(path, volume, len);
254     ok(ret == TRUE, "GetVolumeNameForVolumeMountPointW failed\n");
255 }
256
257 static void test_GetLogicalDriveStringsA(void)
258 {
259     UINT size, size2;
260     char *buf, *ptr;
261
262     ok( pGetLogicalDriveStringsA != NULL, "GetLogicalDriveStringsA not available\n");
263     if(!pGetLogicalDriveStringsA) {
264         return;
265     }
266
267     size = pGetLogicalDriveStringsA(0, NULL);
268     ok(size%4 == 1, "size = %d\n", size);
269
270     buf = HeapAlloc(GetProcessHeap(), 0, size);
271
272     *buf = 0;
273     size2 = pGetLogicalDriveStringsA(2, buf);
274     ok(size2 == size, "size2 = %d\n", size2);
275     ok(!*buf, "buf changed\n");
276
277     size2 = pGetLogicalDriveStringsA(size, buf);
278     ok(size2 == size-1, "size2 = %d\n", size2);
279
280     for(ptr = buf; ptr < buf+size2; ptr += 4) {
281         ok(('A' <= *ptr && *ptr <= 'Z'), "device name '%c' is not uppercase\n", *ptr);
282         ok(ptr[1] == ':', "ptr[1] = %c, expected ':'\n", ptr[1]);
283         ok(ptr[2] == '\\', "ptr[2] = %c expected '\\'\n", ptr[2]);
284         ok(!ptr[3], "ptr[3] = %c expected nullbyte\n", ptr[3]);
285     }
286     ok(!*ptr, "buf[size2] is not nullbyte\n");
287
288     HeapFree(GetProcessHeap(), 0, buf);
289 }
290
291 static void test_GetLogicalDriveStringsW(void)
292 {
293     UINT size, size2;
294     WCHAR *buf, *ptr;
295
296     ok( pGetLogicalDriveStringsW != NULL, "GetLogicalDriveStringsW not available\n");
297     if(!pGetLogicalDriveStringsW) {
298         return;
299     }
300
301     SetLastError(0xdeadbeef);
302     size = pGetLogicalDriveStringsW(0, NULL);
303     if (size == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
304         win_skip("GetLogicalDriveStringsW not implemented\n");
305         return;
306     }
307     ok(size%4 == 1, "size = %d\n", size);
308
309     buf = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
310
311     *buf = 0;
312     size2 = pGetLogicalDriveStringsW(2, buf);
313     ok(size2 == size, "size2 = %d\n", size2);
314     ok(!*buf, "buf changed\n");
315
316     size2 = pGetLogicalDriveStringsW(size, buf);
317     ok(size2 == size-1, "size2 = %d\n", size2);
318
319     for(ptr = buf; ptr < buf+size2; ptr += 4) {
320         ok('A' <= *ptr && *ptr <= 'Z', "device name '%c' is not uppercase\n", *ptr);
321         ok(ptr[1] == ':', "ptr[1] = %c, expected ':'\n", ptr[1]);
322         ok(ptr[2] == '\\', "ptr[2] = %c expected '\\'\n", ptr[2]);
323         ok(!ptr[3], "ptr[3] = %c expected nullbyte\n", ptr[3]);
324     }
325     ok(!*ptr, "buf[size2] is not nullbyte\n");
326
327     HeapFree(GetProcessHeap(), 0, buf);
328 }
329
330 static void test_GetVolumeInformationA(void)
331 {
332     BOOL ret;
333     UINT result;
334     char Root_Colon[]="C:";
335     char Root_Slash[]="C:\\";
336     char Root_UNC[]="\\\\?\\C:\\";
337     char volume[MAX_PATH+1];
338     DWORD vol_name_size=MAX_PATH+1, vol_serial_num=-1, max_comp_len=0, fs_flags=0, fs_name_len=MAX_PATH+1;
339     char vol_name_buf[MAX_PATH+1], fs_name_buf[MAX_PATH+1];
340     char windowsdir[MAX_PATH+10];
341     char currentdir[MAX_PATH+1];
342
343     ok( pGetVolumeInformationA != NULL, "GetVolumeInformationA not found\n");
344     if(!pGetVolumeInformationA) {
345         return;
346     }
347
348     /* get windows drive letter and update strings for testing */
349     result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
350     ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
351     ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
352     Root_Colon[0] = windowsdir[0];
353     Root_Slash[0] = windowsdir[0];
354     Root_UNC[4] = windowsdir[0];
355
356     result = GetCurrentDirectory(MAX_PATH, currentdir);
357     ok(result, "GetCurrentDirectory: error %d\n", GetLastError());
358     /* Note that GetCurrentDir yields no trailing slash for subdirs */
359
360     /* check for NO error on no trailing \ when current dir is root dir */
361     ret = SetCurrentDirectory(Root_Slash);
362     ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
363     ret = pGetVolumeInformationA(Root_Colon, vol_name_buf, vol_name_size, NULL,
364             NULL, NULL, fs_name_buf, fs_name_len);
365     ok(ret, "GetVolumeInformationA root failed, last error %u\n", GetLastError());
366
367     /* check for error on no trailing \ when current dir is subdir (windows) of queried drive */
368     ret = SetCurrentDirectory(windowsdir);
369     ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
370     SetLastError(0xdeadbeef);
371     ret = pGetVolumeInformationA(Root_Colon, vol_name_buf, vol_name_size, NULL,
372             NULL, NULL, fs_name_buf, fs_name_len);
373     ok(!ret && (GetLastError() == ERROR_INVALID_NAME),
374         "GetVolumeInformationA did%s fail, last error %u\n", ret ? " not":"", GetLastError());
375
376     /* reset current directory */
377     ret = SetCurrentDirectory(currentdir);
378     ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
379
380     if (toupper(currentdir[0]) == toupper(windowsdir[0])) {
381         skip("Please re-run from another device than %c:\n", windowsdir[0]);
382         /* FIXME: Use GetLogicalDrives to find another device to avoid this skip. */
383     } else {
384         char Root_Env[]="=C:"; /* where MS maintains the per volume directory */
385         Root_Env[1] = windowsdir[0];
386
387         /* C:\windows becomes the current directory on drive C: */
388         /* Note that paths to subdirs are stored without trailing slash, like what GetCurrentDir yields. */
389         ret = SetEnvironmentVariable(Root_Env, windowsdir);
390         ok(ret, "SetEnvironmentVariable %s failed\n", Root_Env);
391
392         ret = SetCurrentDirectory(windowsdir);
393         ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
394         ret = SetCurrentDirectory(currentdir);
395         ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
396
397         /* windows dir is current on the root drive, call fails */
398         SetLastError(0xdeadbeef);
399         ret = pGetVolumeInformationA(Root_Colon, vol_name_buf, vol_name_size, NULL,
400                 NULL, NULL, fs_name_buf, fs_name_len);
401         ok(!ret && (GetLastError() == ERROR_INVALID_NAME),
402            "GetVolumeInformationA did%s fail, last error %u\n", ret ? " not":"", GetLastError());
403
404         /* Try normal drive letter with trailing \ */
405         ret = pGetVolumeInformationA(Root_Slash, vol_name_buf, vol_name_size, NULL,
406                 NULL, NULL, fs_name_buf, fs_name_len);
407         ok(ret, "GetVolumeInformationA with \\ failed, last error %u\n", GetLastError());
408
409         ret = SetCurrentDirectory(Root_Slash);
410         ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
411         ret = SetCurrentDirectory(currentdir);
412         ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
413
414         /* windows dir is STILL CURRENT on root drive; the call fails as before,   */
415         /* proving that SetCurrentDir did not remember the other drive's directory */
416         SetLastError(0xdeadbeef);
417         ret = pGetVolumeInformationA(Root_Colon, vol_name_buf, vol_name_size, NULL,
418                 NULL, NULL, fs_name_buf, fs_name_len);
419         ok(!ret && (GetLastError() == ERROR_INVALID_NAME),
420            "GetVolumeInformationA did%s fail, last error %u\n", ret ? " not":"", GetLastError());
421
422         /* Now C:\ becomes the current directory on drive C: */
423         ret = SetEnvironmentVariable(Root_Env, Root_Slash); /* set =C:=C:\ */
424         ok(ret, "SetEnvironmentVariable %s failed\n", Root_Env);
425
426         /* \ is current on root drive, call succeeds */
427         ret = pGetVolumeInformationA(Root_Colon, vol_name_buf, vol_name_size, NULL,
428                 NULL, NULL, fs_name_buf, fs_name_len);
429         ok(ret, "GetVolumeInformationA failed, last error %u\n", GetLastError());
430
431         /* again, SetCurrentDirectory on another drive does not matter */
432         ret = SetCurrentDirectory(Root_Slash);
433         ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
434         ret = SetCurrentDirectory(currentdir);
435         ok(ret, "SetCurrentDirectory: error %d\n", GetLastError());
436
437         /* \ is current on root drive, call succeeds */
438         ret = pGetVolumeInformationA(Root_Colon, vol_name_buf, vol_name_size, NULL,
439                 NULL, NULL, fs_name_buf, fs_name_len);
440         ok(ret, "GetVolumeInformationA failed, last error %u\n", GetLastError());
441     }
442
443     /* try null root directory to return "root of the current directory"  */
444     ret = pGetVolumeInformationA(NULL, vol_name_buf, vol_name_size, NULL,
445             NULL, NULL, fs_name_buf, fs_name_len);
446     ok(ret, "GetVolumeInformationA failed on null root dir, last error %u\n", GetLastError());
447
448     /* Try normal drive letter with trailing \  */
449     ret = pGetVolumeInformationA(Root_Slash, vol_name_buf, vol_name_size,
450             &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
451     ok(ret, "GetVolumeInformationA failed, root=%s, last error=%u\n", Root_Slash, GetLastError());
452
453     /* try again with drive letter and the "disable parsing" prefix */
454     SetLastError(0xdeadbeef);
455     ret = pGetVolumeInformationA(Root_UNC, vol_name_buf, vol_name_size,
456             &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
457     ok(ret, "GetVolumeInformationA did%s fail, root=%s, last error=%u\n", ret ? " not":"", Root_UNC, GetLastError());
458
459     /* try again with device name space  */
460     Root_UNC[2] = '.';
461     SetLastError(0xdeadbeef);
462     ret = pGetVolumeInformationA(Root_UNC, vol_name_buf, vol_name_size,
463             &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
464     ok(ret, "GetVolumeInformationA did%s fail, root=%s, last error=%u\n", ret ? " not":"", Root_UNC, GetLastError());
465
466     /* try again with a directory off the root - should generate error  */
467     if (windowsdir[strlen(windowsdir)-1] != '\\') strcat(windowsdir, "\\");
468     SetLastError(0xdeadbeef);
469     ret = pGetVolumeInformationA(windowsdir, vol_name_buf, vol_name_size,
470             &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
471     ok(!ret && (GetLastError()==ERROR_DIR_NOT_ROOT),
472           "GetVolumeInformationA did%s fail, root=%s, last error=%u\n", ret ? " not":"", windowsdir, GetLastError());
473     /* A subdir with trailing \ yields DIR_NOT_ROOT instead of INVALID_NAME */
474     if (windowsdir[strlen(windowsdir)-1] == '\\') windowsdir[strlen(windowsdir)-1] = 0;
475     SetLastError(0xdeadbeef);
476     ret = pGetVolumeInformationA(windowsdir, vol_name_buf, vol_name_size,
477             &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
478     ok(!ret && (GetLastError()==ERROR_INVALID_NAME),
479           "GetVolumeInformationA did%s fail, root=%s, last error=%u\n", ret ? " not":"", windowsdir, GetLastError());
480
481     if (!pGetVolumeNameForVolumeMountPointA) {
482         win_skip("GetVolumeNameForVolumeMountPointA not found\n");
483         return;
484     }
485     /* get the unique volume name for the windows drive  */
486     ret = pGetVolumeNameForVolumeMountPointA(Root_Slash, volume, MAX_PATH);
487     ok(ret == TRUE, "GetVolumeNameForVolumeMountPointA failed\n");
488
489     /* try again with unique volume name */
490     ret = pGetVolumeInformationA(volume, vol_name_buf, vol_name_size,
491             &vol_serial_num, &max_comp_len, &fs_flags, fs_name_buf, fs_name_len);
492     ok(ret, "GetVolumeInformationA failed, root=%s, last error=%u\n", volume, GetLastError());
493 }
494
495 /* Test to check that unique volume name from windows dir mount point  */
496 /* matches at least one of the unique volume names returned from the   */
497 /* FindFirstVolumeA/FindNextVolumeA list.                              */
498 static void test_enum_vols(void)
499 {
500     DWORD   ret;
501     HANDLE  hFind = INVALID_HANDLE_VALUE;
502     char    Volume_1[MAX_PATH] = {0};
503     char    Volume_2[MAX_PATH] = {0};
504     char    path[] = "c:\\";
505     BOOL    found = FALSE;
506     char    windowsdir[MAX_PATH];
507
508     if (!pGetVolumeNameForVolumeMountPointA) {
509         win_skip("GetVolumeNameForVolumeMountPointA not found\n");
510         return;
511     }
512
513     /*get windows drive letter and update strings for testing  */
514     ret = GetWindowsDirectory( windowsdir, sizeof(windowsdir) );
515     ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
516     ok(ret != 0, "GetWindowsDirecory: error %d\n", GetLastError());
517     path[0] = windowsdir[0];
518
519     /* get the unique volume name for the windows drive  */
520     ret = pGetVolumeNameForVolumeMountPointA( path, Volume_1, MAX_PATH );
521     ok(ret == TRUE, "GetVolumeNameForVolumeMountPointA failed\n");
522     ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name %s\n", Volume_1);
523
524     /* get first unique volume name of list  */
525     hFind = pFindFirstVolumeA( Volume_2, MAX_PATH );
526     ok(hFind != INVALID_HANDLE_VALUE, "FindFirstVolume failed, err=%u\n",
527                 GetLastError());
528
529     do
530     {
531         /* validate correct length of unique volume name  */
532         ok(strlen(Volume_2) == 49, "Find[First/Next]Volume returned wrong length name %s\n", Volume_1);
533         if (memcmp(Volume_1, Volume_2, 49) == 0)
534         {
535             found = TRUE;
536             break;
537         }
538     } while (pFindNextVolumeA( hFind, Volume_2, MAX_PATH ));
539     ok(found, "volume name %s not found by Find[First/Next]Volume\n", Volume_1);
540     pFindVolumeClose( hFind );
541 }
542
543 static void test_disk_extents(void)
544 {
545     BOOL ret;
546     DWORD size;
547     HANDLE handle;
548     static DWORD data[16];
549
550     handle = CreateFileA( "\\\\.\\c:", GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
551     if (handle == INVALID_HANDLE_VALUE)
552     {
553         win_skip("can't open c: drive %u\n", GetLastError());
554         return;
555     }
556     size = 0;
557     ret = DeviceIoControl( handle, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, &data,
558                            sizeof(data), &data, sizeof(data), &size, NULL );
559     if (!ret && GetLastError() == ERROR_INVALID_FUNCTION)
560     {
561         win_skip("IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS not supported\n");
562         CloseHandle( handle );
563         return;
564     }
565     ok(ret, "DeviceIoControl failed %u\n", GetLastError());
566     ok(size == 32, "expected 32, got %u\n", size);
567     CloseHandle( handle );
568 }
569
570 static void test_GetVolumePathNamesForVolumeNameA(void)
571 {
572     BOOL ret;
573     char volume[MAX_PATH], buffer[MAX_PATH];
574     DWORD len, error;
575
576     if (!pGetVolumePathNamesForVolumeNameA || !pGetVolumeNameForVolumeMountPointA)
577     {
578         win_skip("required functions not found\n");
579         return;
580     }
581
582     ret = pGetVolumeNameForVolumeMountPointA( "c:\\", volume, sizeof(volume) );
583     ok(ret, "failed to get volume name %u\n", GetLastError());
584     trace("c:\\ -> %s\n", volume);
585
586     SetLastError( 0xdeadbeef );
587     ret = pGetVolumePathNamesForVolumeNameA( NULL, NULL, 0, NULL );
588     error = GetLastError();
589     ok(!ret, "expected failure\n");
590     ok(error == ERROR_INVALID_NAME, "expected ERROR_INVALID_NAME got %u\n", error);
591
592     SetLastError( 0xdeadbeef );
593     ret = pGetVolumePathNamesForVolumeNameA( "", NULL, 0, NULL );
594     error = GetLastError();
595     ok(!ret, "expected failure\n");
596     ok(error == ERROR_INVALID_NAME, "expected ERROR_INVALID_NAME got %u\n", error);
597
598     SetLastError( 0xdeadbeef );
599     ret = pGetVolumePathNamesForVolumeNameA( volume, NULL, 0, NULL );
600     error = GetLastError();
601     ok(!ret, "expected failure\n");
602     ok(error == ERROR_MORE_DATA, "expected ERROR_MORE_DATA got %u\n", error);
603
604     SetLastError( 0xdeadbeef );
605     ret = pGetVolumePathNamesForVolumeNameA( volume, buffer, 0, NULL );
606     error = GetLastError();
607     ok(!ret, "expected failure\n");
608     ok(error == ERROR_MORE_DATA, "expected ERROR_MORE_DATA got %u\n", error);
609
610     memset( buffer, 0xff, sizeof(buffer) );
611     ret = pGetVolumePathNamesForVolumeNameA( volume, buffer, sizeof(buffer), NULL );
612     ok(ret, "failed to get path names %u\n", GetLastError());
613     ok(!strcmp( "C:\\", buffer ), "expected \"\\C:\" got \"%s\"\n", buffer);
614     ok(!buffer[4], "expected double null-terminated buffer\n");
615
616     len = 0;
617     SetLastError( 0xdeadbeef );
618     ret = pGetVolumePathNamesForVolumeNameA( NULL, NULL, 0, &len );
619     error = GetLastError();
620     ok(!ret, "expected failure\n");
621     ok(error == ERROR_INVALID_NAME, "expected ERROR_INVALID_NAME got %u\n", error);
622
623     len = 0;
624     SetLastError( 0xdeadbeef );
625     ret = pGetVolumePathNamesForVolumeNameA( NULL, NULL, sizeof(buffer), &len );
626     error = GetLastError();
627     ok(!ret, "expected failure\n");
628     ok(error == ERROR_INVALID_NAME, "expected ERROR_INVALID_NAME got %u\n", error);
629
630     len = 0;
631     SetLastError( 0xdeadbeef );
632     ret = pGetVolumePathNamesForVolumeNameA( NULL, buffer, sizeof(buffer), &len );
633     error = GetLastError();
634     ok(!ret, "expected failure\n");
635     ok(error == ERROR_INVALID_NAME, "expected ERROR_INVALID_NAME got %u\n", error);
636
637     len = 0;
638     SetLastError( 0xdeadbeef );
639     ret = pGetVolumePathNamesForVolumeNameA( NULL, buffer, sizeof(buffer), &len );
640     error = GetLastError();
641     ok(!ret, "expected failure\n");
642     ok(error == ERROR_INVALID_NAME, "expected ERROR_INVALID_NAME got %u\n", error);
643
644     len = 0;
645     memset( buffer, 0xff, sizeof(buffer) );
646     ret = pGetVolumePathNamesForVolumeNameA( volume, buffer, sizeof(buffer), &len );
647     ok(ret, "failed to get path names %u\n", GetLastError());
648     ok(len == 5 || broken(len == 2), "expected 5 got %u\n", len);
649     ok(!strcmp( "C:\\", buffer ), "expected \"\\C:\" got \"%s\"\n", buffer);
650     ok(!buffer[4], "expected double null-terminated buffer\n");
651 }
652
653 static void test_GetVolumePathNamesForVolumeNameW(void)
654 {
655     static const WCHAR empty[] = {0};
656     static const WCHAR drive_c[] = {'c',':','\\',0};
657     static const WCHAR volume_null[] = {'\\','\\','?','\\','V','o','l','u','m','e',
658         '{','0','0','0','0','0','0','0','0','-','0','0','0','0','-','0','0','0','0',
659         '-','0','0','0','0','-','0','0','0','0','0','0','0','0','0','0','0','0','}','\\',0};
660     BOOL ret;
661     WCHAR volume[MAX_PATH], buffer[MAX_PATH];
662     DWORD len, error;
663
664     if (!pGetVolumePathNamesForVolumeNameW || !pGetVolumeNameForVolumeMountPointW)
665     {
666         win_skip("required functions not found\n");
667         return;
668     }
669
670     ret = pGetVolumeNameForVolumeMountPointW( drive_c, volume, sizeof(volume)/sizeof(volume[0]) );
671     ok(ret, "failed to get volume name %u\n", GetLastError());
672
673     SetLastError( 0xdeadbeef );
674     ret = pGetVolumePathNamesForVolumeNameW( empty, NULL, 0, NULL );
675     error = GetLastError();
676     ok(!ret, "expected failure\n");
677     ok(error == ERROR_INVALID_NAME, "expected ERROR_INVALID_NAME got %u\n", error);
678
679     SetLastError( 0xdeadbeef );
680     ret = pGetVolumePathNamesForVolumeNameW( volume, NULL, 0, NULL );
681     error = GetLastError();
682     ok(!ret, "expected failure\n");
683     ok(error == ERROR_MORE_DATA, "expected ERROR_MORE_DATA got %u\n", error);
684
685     SetLastError( 0xdeadbeef );
686     ret = pGetVolumePathNamesForVolumeNameW( volume, buffer, 0, NULL );
687     error = GetLastError();
688     ok(!ret, "expected failure\n");
689     ok(error == ERROR_MORE_DATA, "expected ERROR_MORE_DATA got %u\n", error);
690
691     if (0) { /* crash */
692     ret = pGetVolumePathNamesForVolumeNameW( volume, NULL, sizeof(buffer), NULL );
693     ok(ret, "failed to get path names %u\n", GetLastError());
694     }
695
696     ret = pGetVolumePathNamesForVolumeNameW( volume, buffer, sizeof(buffer), NULL );
697     ok(ret, "failed to get path names %u\n", GetLastError());
698
699     len = 0;
700     memset( buffer, 0xff, sizeof(buffer) );
701     ret = pGetVolumePathNamesForVolumeNameW( volume, buffer, sizeof(buffer), &len );
702     ok(ret, "failed to get path names %u\n", GetLastError());
703     ok(len == 5, "expected 5 got %u\n", len);
704     ok(!buffer[4], "expected double null-terminated buffer\n");
705
706     len = 0;
707     volume[1] = '?';
708     volume[lstrlenW( volume ) - 1] = 0;
709     SetLastError( 0xdeadbeef );
710     ret = pGetVolumePathNamesForVolumeNameW( volume, buffer, sizeof(buffer), &len );
711     error = GetLastError();
712     ok(!ret, "expected failure\n");
713     ok(error == ERROR_INVALID_NAME, "expected ERROR_INVALID_NAME got %u\n", error);
714
715     len = 0;
716     volume[0] = '\\';
717     volume[1] = 0;
718     SetLastError( 0xdeadbeef );
719     ret = pGetVolumePathNamesForVolumeNameW( volume, buffer, sizeof(buffer), &len );
720     error = GetLastError();
721     ok(!ret, "expected failure\n");
722     todo_wine ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error);
723
724     len = 0;
725     lstrcpyW( volume, volume_null );
726     SetLastError( 0xdeadbeef );
727     ret = pGetVolumePathNamesForVolumeNameW( volume, buffer, sizeof(buffer), &len );
728     error = GetLastError();
729     ok(!ret, "expected failure\n");
730     ok(error == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND got %u\n", error);
731 }
732
733 START_TEST(volume)
734 {
735     hdll = GetModuleHandleA("kernel32.dll");
736     pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hdll, "GetVolumeNameForVolumeMountPointA");
737     pGetVolumeNameForVolumeMountPointW = (void *) GetProcAddress(hdll, "GetVolumeNameForVolumeMountPointW");
738     pFindFirstVolumeA = (void *) GetProcAddress(hdll, "FindFirstVolumeA");
739     pFindNextVolumeA = (void *) GetProcAddress(hdll, "FindNextVolumeA");
740     pFindVolumeClose = (void *) GetProcAddress(hdll, "FindVolumeClose");
741     pGetLogicalDriveStringsA = (void *) GetProcAddress(hdll, "GetLogicalDriveStringsA");
742     pGetLogicalDriveStringsW = (void *) GetProcAddress(hdll, "GetLogicalDriveStringsW");
743     pGetVolumeInformationA = (void *) GetProcAddress(hdll, "GetVolumeInformationA");
744     pGetVolumePathNamesForVolumeNameA = (void *) GetProcAddress(hdll, "GetVolumePathNamesForVolumeNameA");
745     pGetVolumePathNamesForVolumeNameW = (void *) GetProcAddress(hdll, "GetVolumePathNamesForVolumeNameW");
746
747     test_query_dos_deviceA();
748     test_define_dos_deviceA();
749     test_FindFirstVolume();
750     test_GetVolumeNameForVolumeMountPointA();
751     test_GetVolumeNameForVolumeMountPointW();
752     test_GetLogicalDriveStringsA();
753     test_GetLogicalDriveStringsW();
754     test_GetVolumeInformationA();
755     test_enum_vols();
756     test_disk_extents();
757     test_GetVolumePathNamesForVolumeNameA();
758     test_GetVolumePathNamesForVolumeNameW();
759 }