kernel32/tests: Use shared Windows directory on TS to find regedit.exe.
[wine] / dlls / kernel32 / tests / volume.c
1 /*
2  * Unit test suite
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
24 static HINSTANCE hdll;
25 static BOOL (WINAPI * pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
26 static BOOL (WINAPI * pGetVolumeNameForVolumeMountPointW)(LPCWSTR, LPWSTR, DWORD);
27 static HANDLE (WINAPI *pFindFirstVolumeA)(LPSTR,DWORD);
28 static BOOL (WINAPI *pFindNextVolumeA)(HANDLE,LPSTR,DWORD);
29 static BOOL (WINAPI *pFindVolumeClose)(HANDLE);
30 static UINT (WINAPI *pGetLogicalDriveStringsA)(UINT,LPSTR);
31 static UINT (WINAPI *pGetLogicalDriveStringsW)(UINT,LPWSTR);
32
33 /* ############################### */
34
35 static void test_query_dos_deviceA(void)
36 {
37     char drivestr[] = "a:";
38     char *p, buffer[2000];
39     DWORD ret;
40     BOOL found = FALSE;
41
42     if (!pFindFirstVolumeA) {
43         skip("On win9x, HARDDISK and RAMDISK not present\n");
44         return;
45     }
46
47     for (;drivestr[0] <= 'z'; drivestr[0]++) {
48         ret = QueryDosDeviceA( drivestr, buffer, sizeof(buffer));
49         if(ret) {
50             for (p = buffer; *p; p++) *p = toupper(*p);
51             if (strstr(buffer, "HARDDISK") || strstr(buffer, "RAMDISK")) found = TRUE;
52         }
53     }
54     ok(found, "expected at least one devicename to contain HARDDISK or RAMDISK\n");
55 }
56
57 static void test_FindFirstVolume(void)
58 {
59     char volume[51];
60     HANDLE handle;
61
62     if (!pFindFirstVolumeA) {
63         skip("FindFirstVolumeA not found\n");
64         return;
65     }
66
67     handle = pFindFirstVolumeA( volume, 0 );
68     ok( handle == INVALID_HANDLE_VALUE, "succeeded with short buffer\n" );
69     ok( GetLastError() == ERROR_MORE_DATA ||  /* XP */
70         GetLastError() == ERROR_FILENAME_EXCED_RANGE,  /* Vista */
71         "wrong error %u\n", GetLastError() );
72     handle = pFindFirstVolumeA( volume, 49 );
73     ok( handle == INVALID_HANDLE_VALUE, "succeeded with short buffer\n" );
74     ok( GetLastError() == ERROR_FILENAME_EXCED_RANGE, "wrong error %u\n", GetLastError() );
75     handle = pFindFirstVolumeA( volume, 51 );
76     ok( handle != INVALID_HANDLE_VALUE, "failed err %u\n", GetLastError() );
77     if (handle != INVALID_HANDLE_VALUE)
78     {
79         do
80         {
81             ok( strlen(volume) == 49, "bad volume name %s\n", volume );
82             ok( !memcmp( volume, "\\\\?\\Volume{", 11 ), "bad volume name %s\n", volume );
83             ok( !memcmp( volume + 47, "}\\", 2 ), "bad volume name %s\n", volume );
84         } while (pFindNextVolumeA( handle, volume, MAX_PATH ));
85         ok( GetLastError() == ERROR_NO_MORE_FILES, "wrong error %u\n", GetLastError() );
86         pFindVolumeClose( handle );
87     }
88 }
89
90 static void test_GetVolumeNameForVolumeMountPointA(void)
91 {
92     BOOL ret;
93     char volume[MAX_PATH], path[] = "c:\\";
94     DWORD len = sizeof(volume);
95
96     /* not present before w2k */
97     if (!pGetVolumeNameForVolumeMountPointA) {
98         skip("GetVolumeNameForVolumeMountPointA not found\n");
99         return;
100     }
101
102     ret = pGetVolumeNameForVolumeMountPointA(path, volume, 0);
103     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
104
105     if (0) { /* these crash on XP */
106     ret = pGetVolumeNameForVolumeMountPointA(path, NULL, len);
107     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
108
109     ret = pGetVolumeNameForVolumeMountPointA(NULL, volume, len);
110     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
111     }
112
113     ret = pGetVolumeNameForVolumeMountPointA(path, volume, len);
114     ok(ret == TRUE, "GetVolumeNameForVolumeMountPointA failed\n");
115 }
116
117 static void test_GetVolumeNameForVolumeMountPointW(void)
118 {
119     BOOL ret;
120     WCHAR volume[MAX_PATH], path[] = {'c',':','\\',0};
121     DWORD len = sizeof(volume) / sizeof(WCHAR);
122
123     /* not present before w2k */
124     if (!pGetVolumeNameForVolumeMountPointW) {
125         skip("GetVolumeNameForVolumeMountPointW not found\n");
126         return;
127     }
128
129     ret = pGetVolumeNameForVolumeMountPointW(path, volume, 0);
130     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointA succeeded\n");
131
132     if (0) { /* these crash on XP */
133     ret = pGetVolumeNameForVolumeMountPointW(path, NULL, len);
134     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointW succeeded\n");
135
136     ret = pGetVolumeNameForVolumeMountPointW(NULL, volume, len);
137     ok(ret == FALSE, "GetVolumeNameForVolumeMountPointW succeeded\n");
138     }
139
140     ret = pGetVolumeNameForVolumeMountPointW(path, volume, len);
141     ok(ret == TRUE, "GetVolumeNameForVolumeMountPointW failed\n");
142 }
143
144 static void test_GetLogicalDriveStringsA(void)
145 {
146     UINT size, size2;
147     char *buf, *ptr;
148
149     if(!pGetLogicalDriveStringsA) {
150         win_skip("GetLogicalDriveStringsA not available\n");
151         return;
152     }
153
154     size = pGetLogicalDriveStringsA(0, NULL);
155     ok(size%4 == 1, "size = %d\n", size);
156
157     buf = HeapAlloc(GetProcessHeap(), 0, size);
158
159     *buf = 0;
160     size2 = pGetLogicalDriveStringsA(2, buf);
161     ok(size2 == size, "size2 = %d\n", size2);
162     ok(!*buf, "buf changed\n");
163
164     size2 = pGetLogicalDriveStringsA(size, buf);
165     ok(size2 == size-1, "size2 = %d\n", size2);
166
167     for(ptr = buf; ptr < buf+size2; ptr += 4) {
168         ok(('A' <= *ptr && *ptr <= 'Z') ||
169            (broken('a' <= *ptr && *ptr <= 'z')), /* Win9x and WinMe */
170            "device name '%c' is not uppercase\n", *ptr);
171         ok(ptr[1] == ':', "ptr[1] = %c, expected ':'\n", ptr[1]);
172         ok(ptr[2] == '\\', "ptr[2] = %c expected '\\'\n", ptr[2]);
173         ok(!ptr[3], "ptr[3] = %c expected nullbyte\n", ptr[3]);
174     }
175     ok(!*ptr, "buf[size2] is not nullbyte\n");
176
177     HeapFree(GetProcessHeap(), 0, buf);
178 }
179
180 static void test_GetLogicalDriveStringsW(void)
181 {
182     UINT size, size2;
183     WCHAR *buf, *ptr;
184
185     if(!pGetLogicalDriveStringsW) {
186         win_skip("GetLogicalDriveStringsW not available\n");
187         return;
188     }
189
190     SetLastError(0xdeadbeef);
191     size = pGetLogicalDriveStringsW(0, NULL);
192     if (size == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
193         win_skip("GetLogicalDriveStringsW not implemented\n");
194         return;
195     }
196     ok(size%4 == 1, "size = %d\n", size);
197
198     buf = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
199
200     *buf = 0;
201     size2 = pGetLogicalDriveStringsW(2, buf);
202     ok(size2 == size, "size2 = %d\n", size2);
203     ok(!*buf, "buf changed\n");
204
205     size2 = pGetLogicalDriveStringsW(size, buf);
206     ok(size2 == size-1, "size2 = %d\n", size2);
207
208     for(ptr = buf; ptr < buf+size2; ptr += 4) {
209         ok('A' <= *ptr && *ptr <= 'Z', "device name '%c' is not uppercase\n", *ptr);
210         ok(ptr[1] == ':', "ptr[1] = %c, expected ':'\n", ptr[1]);
211         ok(ptr[2] == '\\', "ptr[2] = %c expected '\\'\n", ptr[2]);
212         ok(!ptr[3], "ptr[3] = %c expected nullbyte\n", ptr[3]);
213     }
214     ok(!*ptr, "buf[size2] is not nullbyte\n");
215
216     HeapFree(GetProcessHeap(), 0, buf);
217 }
218
219 START_TEST(volume)
220 {
221     hdll = GetModuleHandleA("kernel32.dll");
222     pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hdll, "GetVolumeNameForVolumeMountPointA");
223     pGetVolumeNameForVolumeMountPointW = (void *) GetProcAddress(hdll, "GetVolumeNameForVolumeMountPointW");
224     pFindFirstVolumeA = (void *) GetProcAddress(hdll, "FindFirstVolumeA");
225     pFindNextVolumeA = (void *) GetProcAddress(hdll, "FindNextVolumeA");
226     pFindVolumeClose = (void *) GetProcAddress(hdll, "FindVolumeClose");
227     pGetLogicalDriveStringsA = (void *) GetProcAddress(hdll, "GetLogicalDriveStringsA");
228     pGetLogicalDriveStringsW = (void *) GetProcAddress(hdll, "GetLogicalDriveStringsW");
229
230     test_query_dos_deviceA();
231     test_FindFirstVolume();
232     test_GetVolumeNameForVolumeMountPointA();
233     test_GetVolumeNameForVolumeMountPointW();
234     test_GetLogicalDriveStringsA();
235     test_GetLogicalDriveStringsW();
236 }