1 /*
2  * Unit test suite for drive functions.
3  *
4  * Copyright 2002 Dmitry Timoshkov
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 <stdarg.h>
22 
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 
28 static DWORD (WINAPI *pGetDiskFreeSpaceExA)(LPCSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
29 
30 static void test_GetDriveTypeA(void)
31 {
32     char drive[] = "?:\\";
33     char existing_drive_letter = 0;
34     DWORD logical_drives;
35     UINT type;
36 
37     logical_drives = GetLogicalDrives();
38     ok(logical_drives != 0, "GetLogicalDrives error %d\n", GetLastError());
39 
40     for (drive[0] = 'A'; drive[0] <= 'Z'; drive[0]++)
41     {
42         type = GetDriveTypeA(drive);
43         ok(type > DRIVE_UNKNOWN && type <= DRIVE_RAMDISK,
44            "not a valid drive %c: type %u\n", drive[0], type);
45 
46         if (!(logical_drives & 1))
47             ok(type == DRIVE_NO_ROOT_DIR,
48                "GetDriveTypeA should return DRIVE_NO_ROOT_DIR for inexistent drive %c: but not %u\n",
49                drive[0], type);
50         else if (type != DRIVE_NO_ROOT_DIR)
51             existing_drive_letter = drive[0];
52 
53         logical_drives >>= 1;
54     }
55 
56     if (!existing_drive_letter) {
57         skip("No drives found, skipping drive spec format tests.\n");
58         return;
59     }
60 
61     drive[0] = existing_drive_letter;
62     drive[2] = 0; /* C: */
63     type = GetDriveTypeA(drive);
64     ok(type > DRIVE_NO_ROOT_DIR && type <= DRIVE_RAMDISK, "got %u for drive spec '%s'\n", type, drive);
65 
66     drive[1] = '?'; /* C? */
67     type = GetDriveTypeA(drive);
68     ok(type == DRIVE_NO_ROOT_DIR, "got %u for drive spec '%s'\n", type, drive);
69 
70     drive[1] = 0; /* C */
71     type = GetDriveTypeA(drive);
72     ok(type == DRIVE_NO_ROOT_DIR, "got %u for drive spec '%s'\n", type, drive);
73 
74     drive[0] = '?'; /* the string "?" */
75     type = GetDriveTypeA(drive);
76     ok(type == DRIVE_NO_ROOT_DIR, "got %u for drive spec '%s'\n", type, drive);
77 
78     drive[0] = 0; /* the empty string */
79     type = GetDriveTypeA(drive);
80     ok(type == DRIVE_NO_ROOT_DIR, "got %u for drive spec '%s'\n", type, drive);
81 }
82 
83 static void test_GetDriveTypeW(void)
84 {
85     WCHAR drive[] = {'?',':','\\',0};
86     WCHAR existing_drive_letter = 0;
87     DWORD logical_drives;
88     UINT type;
89 
90     logical_drives = GetLogicalDrives();
91     ok(logical_drives != 0, "GetLogicalDrives error %d\n", GetLastError());
92 
93     for (drive[0] = 'A'; drive[0] <= 'Z'; drive[0]++)
94     {
95         type = GetDriveTypeW(drive);
96         ok(type > DRIVE_UNKNOWN && type <= DRIVE_RAMDISK,
97            "not a valid drive %c: type %u\n", drive[0], type);
98 
99         if (!(logical_drives & 1))
100             ok(type == DRIVE_NO_ROOT_DIR,
101                "GetDriveTypeW should return DRIVE_NO_ROOT_DIR for inexistent drive %c: but not %u\n",
102                drive[0], type);
103         else if (type != DRIVE_NO_ROOT_DIR)
104             existing_drive_letter = drive[0];
105 
106         logical_drives >>= 1;
107     }
108 
109     if (!existing_drive_letter) {
110         skip("No drives found, skipping drive spec format tests.\n");
111         return;
112     }
113 
114     drive[0] = existing_drive_letter;
115     drive[2] = 0; /* C: */
116     type = GetDriveTypeW(drive);
117     ok(type > DRIVE_NO_ROOT_DIR && type <= DRIVE_RAMDISK, "got %u for drive spec '%s'\n",
118        type, wine_dbgstr_w(drive));
119 
120     drive[1] = '?'; /* C? */
121     type = GetDriveTypeW(drive);
122     ok(type == DRIVE_NO_ROOT_DIR, "got %u for drive spec '%s'\n", type, wine_dbgstr_w(drive));
123 
124     drive[1] = 0; /* C */
125     type = GetDriveTypeW(drive);
126     ok(type == DRIVE_NO_ROOT_DIR, "got %u for drive spec '%s'\n", type, wine_dbgstr_w(drive));
127 
128     drive[0] = '?'; /* the string "?" */
129     type = GetDriveTypeW(drive);
130     ok(type == DRIVE_NO_ROOT_DIR, "got %u for drive spec '%s'\n", type, wine_dbgstr_w(drive));
131 
132     drive[0] = 0; /* the empty string */
133     type = GetDriveTypeW(drive);
134     ok(type == DRIVE_NO_ROOT_DIR, "got %u for drive spec '%s'\n", type, wine_dbgstr_w(drive));
135 }
136 
137 static void test_GetDiskFreeSpaceA(void)
138 {
139     BOOL ret;
140     DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
141     char drive[] = "?:\\";
142     DWORD logical_drives;
143 
144     ret = GetDiskFreeSpaceA(NULL, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
145     ok(ret, "GetDiskFreeSpaceA error %d\n", GetLastError());
146 
147     ret = GetDiskFreeSpaceA("", &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
148     ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME),
149        "GetDiskFreeSpaceA(\"\"): ret=%d GetLastError=%d\n",
150        ret, GetLastError());
151 
152     ret = GetDiskFreeSpaceA("\\", &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
153     ok(ret, "GetDiskFreeSpaceA error %d\n", GetLastError());
154 
155     ret = GetDiskFreeSpaceA("/", &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
156     ok(ret, "GetDiskFreeSpaceA error %d\n", GetLastError());
157 
158     logical_drives = GetLogicalDrives();
159     ok(logical_drives != 0, "GetLogicalDrives error %d\n", GetLastError());
160 
161     for (drive[0] = 'A'; drive[0] <= 'Z'; drive[0]++)
162     {
163         UINT drivetype = GetDriveTypeA(drive);
164         /* Skip floppy drives because NT pops up a MessageBox if no
165          * floppy is present
166          */
167         if (drivetype != DRIVE_REMOVABLE && drivetype != DRIVE_NO_ROOT_DIR)
168         {
169             ret = GetDiskFreeSpaceA(drive, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
170             if (!(logical_drives & 1))
171                 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND || GetLastError() == ERROR_INVALID_DRIVE),
172                    "GetDiskFreeSpaceA(%s): ret=%d GetLastError=%d\n",
173                    drive, ret, GetLastError());
174             else
175             {
176 
177                 if (!ret)
178                     /* GetDiskFreeSpaceA() should succeed, but it can fail with too many
179                        different GetLastError() results to be usable for an ok() */
180                     trace("GetDiskFreeSpaceA(%s) failed with %d\n", drive, GetLastError());
181 
182                 if( GetVersion() & 0x80000000)
183                     /* win3.0 through winME */
184                     ok( total_clusters <= 65535,
185                             "total clusters is %d > 65535\n", total_clusters);
186                 else if (pGetDiskFreeSpaceExA) {
187                     /* NT, 2k, XP : GetDiskFreeSpace should be accurate */
188                     ULARGE_INTEGER totEx, tot, d;
189 
190                     tot.QuadPart = sectors_per_cluster;
191                     tot.QuadPart = (tot.QuadPart * bytes_per_sector) * total_clusters;
192                     ret = pGetDiskFreeSpaceExA( drive, &d, &totEx, NULL);
193 
194                     if (!ret)
195                         /* GetDiskFreeSpaceExA() should succeed, but it can fail with too many
196                            different GetLastError() results to be usable for an ok() */
197                         trace("GetDiskFreeSpaceExA(%s) failed with %d\n", drive, GetLastError());
198 
199                     ok( bytes_per_sector == 0 || /* empty cd rom drive */
200                         totEx.QuadPart <= tot.QuadPart,
201                         "GetDiskFreeSpaceA should report at least as much bytes on disk %s as GetDiskFreeSpaceExA\n", drive);
202                 }
203             }
204         }
205         logical_drives >>= 1;
206     }
207 }
208 
209 static void test_GetDiskFreeSpaceW(void)
210 {
211     BOOL ret;
212     DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
213     WCHAR drive[] = {'?',':','\\',0};
214     DWORD logical_drives;
215     static const WCHAR empty_pathW[] = { 0 };
216     static const WCHAR root_pathW[] = { '\\', 0 };
217     static const WCHAR unix_style_root_pathW[] = { '/', 0 };
218 
219     ret = GetDiskFreeSpaceW(NULL, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
220     if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
221     {
222         win_skip("GetDiskFreeSpaceW is not available\n");
223         return;
224     }
225     ok(ret, "GetDiskFreeSpaceW error %d\n", GetLastError());
226 
227     ret = GetDiskFreeSpaceW(empty_pathW, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
228     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
229        "GetDiskFreeSpaceW(\"\"): ret=%d GetLastError=%d\n",
230        ret, GetLastError());
231 
232     ret = GetDiskFreeSpaceW(root_pathW, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
233     ok(ret, "GetDiskFreeSpaceW(\"\") error %d\n", GetLastError());
234 
235     ret = GetDiskFreeSpaceW(unix_style_root_pathW, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
236     ok(ret, "GetDiskFreeSpaceW error %d\n", GetLastError());
237 
238     logical_drives = GetLogicalDrives();
239     ok(logical_drives != 0, "GetLogicalDrives error %d\n", GetLastError());
240 
241     for (drive[0] = 'A'; drive[0] <= 'Z'; drive[0]++)
242     {
243 	UINT drivetype = GetDriveTypeW(drive);
244         /* Skip floppy drives because NT4 pops up a MessageBox if no floppy is present */
245         if (drivetype != DRIVE_REMOVABLE && drivetype != DRIVE_NO_ROOT_DIR)
246         {
247             ret = GetDiskFreeSpaceW(drive, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
248             if (!(logical_drives & 1))
249                 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
250                    "GetDiskFreeSpaceW(%c): ret=%d GetLastError=%d\n",
251                    drive[0], ret, GetLastError());
252             else if (!ret)
253                 /* GetDiskFreeSpaceW() should succeed, but it can fail with too many
254                    different GetLastError() results to be usable for an ok() */
255                 trace("GetDiskFreeSpaceW(%c) failed with %d\n", drive[0], GetLastError());
256         }
257         logical_drives >>= 1;
258     }
259 }
260 
261 START_TEST(drive)
262 {
263     HANDLE hkernel32 = GetModuleHandleA("kernel32");
264     pGetDiskFreeSpaceExA = (void *) GetProcAddress(hkernel32, "GetDiskFreeSpaceExA");
265 
266     test_GetDriveTypeA();
267     test_GetDriveTypeW();
268 
269     test_GetDiskFreeSpaceA();
270     test_GetDiskFreeSpaceW();
271 }
272