kernel32/tests: Add acceptable error codes for Win7 process test.
[wine] / dlls / kernel32 / tests / drive.c
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     DWORD logical_drives;
34     UINT type;
35
36     logical_drives = GetLogicalDrives();
37     ok(logical_drives != 0, "GetLogicalDrives error %d\n", GetLastError());
38
39     for (drive[0] = 'A'; drive[0] <= 'Z'; drive[0]++)
40     {
41         type = GetDriveTypeA(drive);
42         ok(type > DRIVE_UNKNOWN && type <= DRIVE_RAMDISK,
43            "not a valid drive %c: type %u\n", drive[0], type);
44
45         if (!(logical_drives & 1))
46             ok(type == DRIVE_NO_ROOT_DIR,
47                "GetDriveTypeA should return DRIVE_NO_ROOT_DIR for inexistent drive %c: but not %u\n",
48                drive[0], type);
49
50         logical_drives >>= 1;
51     }
52 }
53
54 static void test_GetDriveTypeW(void)
55 {
56     WCHAR drive[] = {'?',':','\\',0};
57     DWORD logical_drives;
58     UINT type;
59
60     logical_drives = GetLogicalDrives();
61     ok(logical_drives != 0, "GetLogicalDrives error %d\n", GetLastError());
62
63     for (drive[0] = 'A'; drive[0] <= 'Z'; drive[0]++)
64     {
65         type = GetDriveTypeW(drive);
66         if (type == DRIVE_UNKNOWN && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
67         {
68             win_skip("GetDriveTypeW is not available on Win9x\n");
69             return;
70         }
71         ok(type > DRIVE_UNKNOWN && type <= DRIVE_RAMDISK,
72            "not a valid drive %c: type %u\n", drive[0], type);
73
74         if (!(logical_drives & 1))
75             ok(type == DRIVE_NO_ROOT_DIR,
76                "GetDriveTypeW should return DRIVE_NO_ROOT_DIR for inexistent drive %c: but not %u\n",
77                drive[0], type);
78
79         logical_drives >>= 1;
80     }
81 }
82
83 static void test_GetDiskFreeSpaceA(void)
84 {
85     BOOL ret;
86     DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
87     char drive[] = "?:\\";
88     DWORD logical_drives;
89
90     ret = GetDiskFreeSpaceA(NULL, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
91     ok(ret, "GetDiskFreeSpaceA error %d\n", GetLastError());
92
93     ret = GetDiskFreeSpaceA("", &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
94     ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME),
95        "GetDiskFreeSpaceA(\"\"): ret=%d GetLastError=%d\n",
96        ret, GetLastError());
97
98     ret = GetDiskFreeSpaceA("\\", &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
99     ok(ret, "GetDiskFreeSpaceA error %d\n", GetLastError());
100
101     ret = GetDiskFreeSpaceA("/", &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
102     ok(ret, "GetDiskFreeSpaceA error %d\n", GetLastError());
103
104     logical_drives = GetLogicalDrives();
105     ok(logical_drives != 0, "GetLogicalDrives error %d\n", GetLastError());
106
107     for (drive[0] = 'A'; drive[0] <= 'Z'; drive[0]++)
108     {
109         UINT drivetype = GetDriveTypeA(drive);
110         /* Skip floppy drives because NT pops up a MessageBox if no
111          * floppy is present
112          */
113         if (drivetype != DRIVE_REMOVABLE && drivetype != DRIVE_NO_ROOT_DIR)
114         {
115             ret = GetDiskFreeSpaceA(drive, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
116             if (!(logical_drives & 1))
117                 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND || GetLastError() == ERROR_INVALID_DRIVE),
118                    "GetDiskFreeSpaceA(%s): ret=%d GetLastError=%d\n",
119                    drive, ret, GetLastError());
120             else
121             {
122                 ok(ret ||
123                    GetLastError() == ERROR_NOT_READY ||
124                    GetLastError() == ERROR_INVALID_FUNCTION ||
125                    GetLastError() == ERROR_INVALID_DRIVE ||
126                    GetLastError() == ERROR_PATH_NOT_FOUND ||
127                    GetLastError() == ERROR_REQUEST_ABORTED ||
128                    GetLastError() == ERROR_NETNAME_DELETED ||
129                    GetLastError() == ERROR_UNRECOGNIZED_VOLUME,
130                    "GetDiskFreeSpaceA(%s): ret=%d GetLastError=%d\n",
131                    drive, ret, GetLastError());
132                 if( GetVersion() & 0x80000000)
133                     /* win3.0 thru winME */
134                     ok( total_clusters <= 65535,
135                             "total clusters is %d > 65535\n", total_clusters);
136                 else if (pGetDiskFreeSpaceExA) {
137                     /* NT, 2k, XP : GetDiskFreeSpace should be accurate */
138                     ULARGE_INTEGER totEx, tot, d;
139
140                     tot.QuadPart = sectors_per_cluster;
141                     tot.QuadPart = (tot.QuadPart * bytes_per_sector) * total_clusters;
142                     ret = pGetDiskFreeSpaceExA( drive, &d, &totEx, NULL);
143                     ok( ret ||
144                         GetLastError() == ERROR_NOT_READY ||
145                         GetLastError() == ERROR_INVALID_FUNCTION ||
146                         GetLastError() == ERROR_PATH_NOT_FOUND ||
147                         GetLastError() == ERROR_REQUEST_ABORTED ||
148                         GetLastError() == ERROR_NETNAME_DELETED ||
149                         GetLastError() == ERROR_UNRECOGNIZED_VOLUME,
150                         "GetDiskFreeSpaceExA( %s ) failed. GetLastError=%d\n", drive, GetLastError());
151                     ok( bytes_per_sector == 0 || /* empty cd rom drive */
152                         totEx.QuadPart <= tot.QuadPart,
153                         "GetDiskFreeSpaceA should report at least as much bytes on disk %s as GetDiskFreeSpaceExA\n", drive);
154                 }
155             }
156         }
157         logical_drives >>= 1;
158     }
159 }
160
161 static void test_GetDiskFreeSpaceW(void)
162 {
163     BOOL ret;
164     DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
165     WCHAR drive[] = {'?',':','\\',0};
166     DWORD logical_drives;
167     static const WCHAR empty_pathW[] = { 0 };
168     static const WCHAR root_pathW[] = { '\\', 0 };
169     static const WCHAR unix_style_root_pathW[] = { '/', 0 };
170
171     ret = GetDiskFreeSpaceW(NULL, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
172     if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
173     {
174         win_skip("GetDiskFreeSpaceW is not available\n");
175         return;
176     }
177     ok(ret, "GetDiskFreeSpaceW error %d\n", GetLastError());
178
179     ret = GetDiskFreeSpaceW(empty_pathW, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
180     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
181        "GetDiskFreeSpaceW(\"\"): ret=%d GetLastError=%d\n",
182        ret, GetLastError());
183
184     ret = GetDiskFreeSpaceW(root_pathW, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
185     ok(ret, "GetDiskFreeSpaceW(\"\") error %d\n", GetLastError());
186
187     ret = GetDiskFreeSpaceW(unix_style_root_pathW, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
188     ok(ret, "GetDiskFreeSpaceW error %d\n", GetLastError());
189
190     logical_drives = GetLogicalDrives();
191     ok(logical_drives != 0, "GetLogicalDrives error %d\n", GetLastError());
192
193     for (drive[0] = 'A'; drive[0] <= 'Z'; drive[0]++)
194     {
195         UINT drivetype = GetDriveTypeW(drive);
196         /* Skip floppy drives because NT4 pops up a MessageBox if no floppy is present */
197         if (drivetype != DRIVE_REMOVABLE && drivetype != DRIVE_NO_ROOT_DIR)
198         {
199             ret = GetDiskFreeSpaceW(drive, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters);
200             if (!(logical_drives & 1))
201                 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
202                    "GetDiskFreeSpaceW(%c): ret=%d GetLastError=%d\n",
203                    drive[0], ret, GetLastError());
204             else
205                 ok( ret ||
206                     GetLastError() == ERROR_NOT_READY ||
207                     GetLastError() == ERROR_INVALID_FUNCTION ||
208                     GetLastError() == ERROR_PATH_NOT_FOUND ||
209                     GetLastError() == ERROR_REQUEST_ABORTED ||
210                     GetLastError() == ERROR_UNRECOGNIZED_VOLUME,
211                    "GetDiskFreeSpaceW(%c): ret=%d GetLastError=%d\n",
212                    drive[0], ret, GetLastError());
213         }
214         logical_drives >>= 1;
215     }
216 }
217
218 START_TEST(drive)
219 {
220     HANDLE hkernel32 = GetModuleHandleA("kernel32");
221     pGetDiskFreeSpaceExA = (void *) GetProcAddress(hkernel32, "GetDiskFreeSpaceExA");
222
223     test_GetDriveTypeA();
224     test_GetDriveTypeW();
225
226     test_GetDiskFreeSpaceA();
227     test_GetDiskFreeSpaceW();
228 }