1 /*
2  * Unit test suite for virtual substituted drive functions.
3  *
4  * Copyright 2011 Sam Arun Raj
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 "precomp.h"
22 
23 #define SUBST_DRIVE_LETTER 'M'
24 #define SUBST_DRIVE "M:"
25 #define SUBST_DRIVE_NON_EXIST_DIR "M:\\deadbeef"
26 #define SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR "M:\\"
27 #define SUBST_DRIVE_SEARCH "M:\\*"
28 #define SUBST_DRIVE_LOWERCASE "m:"
29 #define SUBST_DRIVE_LOWERCASE_SEARCH "m:\\*"
30 #define SUBST_DRIVE2_LETTER 'N'
31 #define SUBST_DRIVE2 "N:"
32 #define SUBST_DRIVE2_WITH_TRAILING_PATH_SEPERATOR "N:\\"
33 #define SUBST_DRIVE2_SEARCH "N:\\*"
34 
35 static void test_DefineDosDeviceA(void)
36 {
37     CHAR Buffer[MAX_PATH], Target[MAX_PATH];
38     BOOL Result;
39     UINT CharCount;
40     HANDLE hnd;
41     WIN32_FIND_DATAA Data;
42     UINT SystemDriveType, DriveType1, DriveType2;
43     DWORD dwMaskPrev, dwMaskCur;
44     CHAR c;
45 
46     /* Choose the symbolic link target */
47     CharCount = GetSystemWindowsDirectoryA(Target, MAX_PATH);
48     ok(CharCount > 0, "Failed to get windows directory\n");
49     c = Target[3];
50     Target[3] = '\0';
51     SystemDriveType = GetDriveTypeA(Target);
52     Target[3] = c;
53 
54     /* Test with a subst drive pointing to another substed drive */
55     dwMaskPrev = GetLogicalDrives();
56     Result = DefineDosDeviceA(0, SUBST_DRIVE, Target);
57     ok(Result, "Failed to subst drive\n");
58     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
59     Result = DefineDosDeviceA(0, SUBST_DRIVE2, SUBST_DRIVE);
60     ok(Result, "Failed to subst drive\n");
61     DriveType2 = GetDriveTypeA(SUBST_DRIVE2_WITH_TRAILING_PATH_SEPERATOR);
62     dwMaskCur = GetLogicalDrives();
63     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
64     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
65     ok((dwMaskCur & (1 << (SUBST_DRIVE2_LETTER - 'A'))), "Drive bit is not set\n");
66     hnd = FindFirstFileA(SUBST_DRIVE2_SEARCH, &Data);
67     ok(hnd != INVALID_HANDLE_VALUE, "Failed to open subst drive\n");
68     if (hnd) FindClose(hnd);
69     ok(DriveType1 == DriveType2, "subst drive types don't match\n");
70     ok(DriveType1 == SystemDriveType, "subst drive types don't match\n");
71     ok(DriveType2 == SystemDriveType, "subst drive types don't match\n");
72     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, Target);
73     ok(Result, "Failed to remove subst drive using NULL Target name\n");
74     hnd = FindFirstFileA(SUBST_DRIVE2_SEARCH, &Data);
75     ok(hnd == INVALID_HANDLE_VALUE, "Opened subst drive when it should fail, we removed the target\n");
76     if (hnd) FindClose(hnd);
77     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE2, SUBST_DRIVE);
78     ok(Result, "Failed to remove subst drive using NULL Target name\n");
79     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
80     ok(!Result, "Subst drive is present even after remove attempt\n");
81     Result = QueryDosDeviceA(SUBST_DRIVE2, Buffer, MAX_PATH);
82     ok(!Result, "Subst drive is present even after remove attempt\n");
83     dwMaskCur = GetLogicalDrives();
84     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
85 
86     /* Test using lowercase drive letter */
87     dwMaskPrev = GetLogicalDrives();
88     Result = DefineDosDeviceA(0, SUBST_DRIVE_LOWERCASE, Target);
89     ok(Result, "Failed to subst drive using lowercase drive letter\n");
90     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
91     ok(DriveType1 == SystemDriveType, "subst drive types don't match\n");
92     dwMaskCur = GetLogicalDrives();
93     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
94     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
95     hnd = FindFirstFileA(SUBST_DRIVE_SEARCH, &Data);
96     ok(hnd != INVALID_HANDLE_VALUE, "Failed to open subst drive\n");
97     if (hnd) FindClose(hnd);
98     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE_LOWERCASE, Target);
99     ok(Result, "Failed to remove subst drive using lowercase drive letter\n");
100     Result = QueryDosDeviceA(SUBST_DRIVE_LOWERCASE, Buffer, MAX_PATH);
101     ok(!Result, "Subst drive is present even after remove attempt\n");
102     dwMaskCur = GetLogicalDrives();
103     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
104 
105     /* Test remove without using DDD_EXACT_MATCH_ON_REMOVE */
106     dwMaskPrev = GetLogicalDrives();
107     Result = DefineDosDeviceA(0, SUBST_DRIVE, Target);
108     ok(Result, "Failed to subst drive\n");
109     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
110     ok(DriveType1 == SystemDriveType, "subst drive types don't match\n");
111     dwMaskCur = GetLogicalDrives();
112     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
113     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
114     hnd = FindFirstFileA(SUBST_DRIVE_SEARCH, &Data);
115     ok(hnd != INVALID_HANDLE_VALUE, "Failed to open subst drive\n");
116     if (hnd) FindClose(hnd);
117     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION, SUBST_DRIVE, NULL);
118     ok(Result, "Failed to remove subst drive using NULL Target name\n");
119     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
120     ok(!Result, "Subst drive is present even after remove attempt\n");
121     dwMaskCur = GetLogicalDrives();
122     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
123 
124     /* Test multiple adds and multiple removes in add order */
125     dwMaskPrev = GetLogicalDrives();
126     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp1");
127     ok(Result, "Failed to subst drive\n");
128     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp2");
129     ok(Result, "Failed to subst drive\n");
130     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp3");
131     ok(Result, "Failed to subst drive\n");
132     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
133     ok(DriveType1 != SystemDriveType, "subst drive types match when it shouldn't\n");
134     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Wrong last error. Expected %lu, got %lu\n", (DWORD)(ERROR_FILE_NOT_FOUND), GetLastError());
135     dwMaskCur = GetLogicalDrives();
136     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
137     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
138     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp1");
139     ok(Result, "Failed to remove subst drive\n");
140     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
141     ok(Result, "Failed to query subst drive\n");
142     if (Result) ok((_stricmp(Buffer, "\\??\\C:\\temp3") == 0), "Subst drive is not pointing to correct target\n");
143     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp2");
144     ok(Result, "Failed to remove subst drive\n");
145     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
146     ok(Result, "Failed to query subst drive\n");
147     if (Result) ok((_stricmp(Buffer, "\\??\\C:\\temp3") == 0), "Subst drive is not pointing to correct target\n");
148     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp3");
149     ok(Result, "Failed to remove subst drive\n");
150     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
151     ok(!Result, "Subst drive is present even after remove attempt\n");
152     dwMaskCur = GetLogicalDrives();
153     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
154 
155     /* Test multiple adds and multiple removes in reverse order */
156     dwMaskPrev = GetLogicalDrives();
157     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp1");
158     ok(Result, "Failed to subst drive\n");
159     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp2");
160     ok(Result, "Failed to subst drive\n");
161     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp3");
162     ok(Result, "Failed to subst drive\n");
163     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
164     ok(DriveType1 != SystemDriveType, "subst drive types match when it shouldn't\n");
165     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Wrong last error. Expected %lu, got %lu\n", (DWORD)(ERROR_FILE_NOT_FOUND), GetLastError());
166     dwMaskCur = GetLogicalDrives();
167     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
168     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
169     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp3");
170     ok(Result, "Failed to remove subst drive\n");
171     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
172     ok(Result, "Failed to query subst drive\n");
173     if (Result) ok((_stricmp(Buffer, "\\??\\C:\\temp2") == 0), "Subst drive is not pointing to correct target\n");
174     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp2");
175     ok(Result, "Failed to remove subst drive\n");
176     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
177     ok(Result, "Failed to query subst drive\n");
178     if (Result) ok((_stricmp(Buffer, "\\??\\C:\\temp1") == 0), "Subst drive is not pointing to correct target\n");
179     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp1");
180     ok(Result, "Failed to remove subst drive\n");
181     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
182     ok(!Result, "Subst drive is present even after remove attempt\n");
183     dwMaskCur = GetLogicalDrives();
184     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
185 
186     /* Test multiple adds and multiple removes out of order */
187     dwMaskPrev = GetLogicalDrives();
188     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp1");
189     ok(Result, "Failed to subst drive\n");
190     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp2");
191     ok(Result, "Failed to subst drive\n");
192     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp3");
193     ok(Result, "Failed to subst drive\n");
194     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp4");
195     ok(Result, "Failed to subst drive\n");
196     Result = DefineDosDeviceA(0, SUBST_DRIVE, "C:\\temp5");
197     ok(Result, "Failed to subst drive\n");
198     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
199     ok(DriveType1 != SystemDriveType, "subst drive types match when it shouldn't\n");
200     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Wrong last error. Expected %lu, got %lu\n", (DWORD)(ERROR_FILE_NOT_FOUND), GetLastError());
201     dwMaskCur = GetLogicalDrives();
202     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
203     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
204     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp2");
205     ok(Result, "Failed to remove subst drive\n");
206     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
207     ok(Result, "Failed to query subst drive\n");
208     if (Result) ok((_stricmp(Buffer, "\\??\\C:\\temp5") == 0), "Subst drive is not pointing to correct target\n");
209     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp5");
210     ok(Result, "Failed to remove subst drive\n");
211     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
212     ok(Result, "Failed to query subst drive\n");
213     if (Result) ok((_stricmp(Buffer, "\\??\\C:\\temp4") == 0), "Subst drive is not pointing to correct target\n");
214     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp1");
215     ok(Result, "Failed to remove subst drive\n");
216     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
217     ok(Result, "Failed to query subst drive\n");
218     if (Result) ok((_stricmp(Buffer, "\\??\\C:\\temp4") == 0), "Subst drive is not pointing to correct target\n");
219     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp3");
220     ok(Result, "Failed to remove subst drive\n");
221     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
222     ok(Result, "Failed to query subst drive\n");
223     if (Result) ok((_stricmp(Buffer, "\\??\\C:\\temp4") == 0), "Subst drive is not pointing to correct target");
224     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, "C:\\temp4");
225     ok(Result, "Failed to remove subst drive\n");
226     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
227     ok(!Result, "Subst drive is present even after remove attempt\n");
228     dwMaskCur = GetLogicalDrives();
229     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
230 
231     /* Test with trailing '\' appended to TargetPath */
232     dwMaskPrev = GetLogicalDrives();
233     snprintf(Buffer, sizeof(Buffer), "%s\\\\\\", Target);
234     Result = DefineDosDeviceA(0, SUBST_DRIVE, Buffer);
235     ok(Result, "Failed to subst drive\n");
236     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
237     ok(DriveType1 == SystemDriveType, "subst drive types don't match\n");
238     dwMaskCur = GetLogicalDrives();
239     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
240     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
241     hnd = FindFirstFileA(SUBST_DRIVE_SEARCH, &Data);
242     ok(hnd != INVALID_HANDLE_VALUE, "Failed to open subst drive\n");
243     if (hnd) FindClose(hnd);
244     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, Buffer);
245     ok(Result, "Failed to remove subst drive using NULL Target name\n");
246     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
247     ok(!Result, "Subst drive is present even after remove attempt\n");
248     dwMaskCur = GetLogicalDrives();
249     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
250 
251     /* Test with trailing '\' appended to TargetPath and DDD_RAW_TARGET_PATH flag */
252     dwMaskPrev = GetLogicalDrives();
253     snprintf(Buffer, sizeof(Buffer), "\\??\\%s\\\\\\", Target);
254     Result = DefineDosDeviceA(DDD_RAW_TARGET_PATH, SUBST_DRIVE, Buffer);
255     ok(Result, "Failed to subst drive\n");
256     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
257     ok(DriveType1 != SystemDriveType, "subst drive types match when they shouldn't\n");
258     dwMaskCur = GetLogicalDrives();
259     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
260     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
261     hnd = FindFirstFileA(SUBST_DRIVE_SEARCH, &Data);
262     ok(hnd == INVALID_HANDLE_VALUE, "Opened subst drive when it should fail\n");
263     ok(GetLastError() == ERROR_INVALID_NAME, "Wrong last error. Expected %lu, got %lu\n", (DWORD)(ERROR_INVALID_NAME), GetLastError());
264     if (hnd) FindClose(hnd);
265     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION, SUBST_DRIVE, NULL);
266     ok(Result, "Failed to remove subst drive using NULL Target name\n");
267     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
268     ok(!Result, "Subst drive is present even after remove attempt\n");
269     dwMaskCur = GetLogicalDrives();
270     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
271 
272     /* Test using trailing \ against drive letter */
273     dwMaskPrev = GetLogicalDrives();
274     Result = DefineDosDeviceA(0, SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR, Target);
275     ok(!Result, "Subst drive using trailing path seperator, this should not happen\n");
276     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
277     ok(DriveType1 != SystemDriveType, "subst drive types match when it shouldn't\n");
278     dwMaskCur = GetLogicalDrives();
279     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
280     ok(!(dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is set when it shouldn't\n");
281     hnd = FindFirstFileA(SUBST_DRIVE_SEARCH, &Data);
282     ok(hnd == INVALID_HANDLE_VALUE, "Opened subst drive when it should fail\n");
283     if (hnd) FindClose(hnd);
284     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR, Target);
285     ok(!Result, "Removing Subst drive using trailing path seperator passed when it should fail\n");
286     Result = QueryDosDeviceA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR, Buffer, MAX_PATH);
287     ok(!Result, "Subst drive is present when it should not be created in the first place\n");
288     dwMaskCur = GetLogicalDrives();
289     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
290 
291     /* Test using arbitary string, not necessarily a DOS drive letter */
292     dwMaskPrev = GetLogicalDrives();
293     Result = DefineDosDeviceA(0, "!QHello:", Target);
294     ok(Result, "Failed to subst drive using non-DOS drive name\n");
295     dwMaskCur = GetLogicalDrives();
296     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
297     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, "!QHello:", Target);
298     ok(Result, "Failed to subst drive using non-DOS drive name\n");
299     Result = QueryDosDeviceA("!QHello:", Buffer, MAX_PATH);
300     ok(!Result, "Subst drive is present even after remove attempt\n");
301     dwMaskCur = GetLogicalDrives();
302     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
303 
304     /* Test by subst a drive to itself */
305     dwMaskPrev = GetLogicalDrives();
306     Result = DefineDosDeviceA(0, SUBST_DRIVE, SUBST_DRIVE);
307     ok(Result, "Failed to subst drive\n");
308     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
309     ok(DriveType1 != SystemDriveType, "subst drive types match when it shouldn't\n");
310     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Wrong last error. Expected %lu, got %lu\n", (DWORD)(ERROR_FILE_NOT_FOUND), GetLastError());
311     dwMaskCur = GetLogicalDrives();
312     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
313     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
314     hnd = FindFirstFileA(SUBST_DRIVE_SEARCH, &Data);
315     ok(hnd == INVALID_HANDLE_VALUE, "Opened subst drive when it should fail\n");
316     if (hnd) FindClose(hnd);
317     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, SUBST_DRIVE);
318     ok(Result, "Failed to remove subst drive using lowercase drive letter\n");
319     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
320     ok(!Result, "Subst drive is present even after remove attempt\n");
321     dwMaskCur = GetLogicalDrives();
322     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
323 
324     /* Test by subst a drive to an non-existent folder under itself */
325     dwMaskPrev = GetLogicalDrives();
326     Result = DefineDosDeviceA(0, SUBST_DRIVE, SUBST_DRIVE_NON_EXIST_DIR);
327     ok(Result, "Failed to subst drive\n");
328     DriveType1 = GetDriveTypeA(SUBST_DRIVE_WITH_TRAILING_PATH_SEPERATOR);
329     ok(DriveType1 != SystemDriveType, "subst drive types match when it shouldn't\n");
330     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Wrong last error. Expected %lu, got %lu\n", (DWORD)(ERROR_FILE_NOT_FOUND), GetLastError());
331     dwMaskCur = GetLogicalDrives();
332     ok(dwMaskCur != dwMaskPrev, "Drive masks match when it shouldn't\n");
333     ok((dwMaskCur & (1 << (SUBST_DRIVE_LETTER - 'A'))), "Drive bit is not set\n");
334     hnd = FindFirstFileA(SUBST_DRIVE_SEARCH, &Data);
335     ok(hnd == INVALID_HANDLE_VALUE, "Opened subst drive when it should fail\n");
336     if (hnd) FindClose(hnd);
337     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, SUBST_DRIVE_NON_EXIST_DIR);
338     ok(Result, "Failed to remove subst drive using lowercase drive letter\n");
339     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
340     ok(!Result, "Subst drive is present even after remove attempt\n");
341     dwMaskCur = GetLogicalDrives();
342     ok(dwMaskCur == dwMaskPrev, "Drive masks don't match\n");
343 }
344 
345 static void test_QueryDosDeviceA(void)
346 {
347     CHAR Buffer[MAX_PATH], Target[MAX_PATH];
348     BOOL Result;
349     UINT CharCount;
350 
351     /* Choose the symbolic link target */
352     CharCount = GetSystemWindowsDirectoryA(Target, MAX_PATH);
353     ok(CharCount > 0, "Failed to get windows directory\n");
354 
355     Result = DefineDosDeviceA(0, SUBST_DRIVE, Target);
356     ok(Result, "Failed to subst drive\n");
357     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, 0);
358     ok(!Result, "Should fail as the buffer passed is supposed to be small\n");
359     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Wrong last error. Expected %lu, got %lu\n", (DWORD)(ERROR_INSUFFICIENT_BUFFER), GetLastError());
360     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
361     ok(Result, "failed to get target path\n");
362     ok(_strnicmp(Buffer, "\\??\\", 4) == 0, "The target returned does have correct prefix set\n");
363     ok(stricmp(&Buffer[4], Target) == 0, "The target returned does not match the one set\n");
364     Result = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, SUBST_DRIVE, Target);
365     ok(Result, "Failed to remove subst drive using lowercase drive letter\n");
366     Result = QueryDosDeviceA(SUBST_DRIVE, Buffer, MAX_PATH);
367 
368     /* This will try to retrieve all existing MS-DOS device names */
369     Result = QueryDosDeviceA(NULL, Buffer, 0);
370     ok(!Result, "Should fail as the buffer passed is supposed to be small\n");
371     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Wrong last error. Expected %lu, got %lu\n", (DWORD)(ERROR_INSUFFICIENT_BUFFER), GetLastError());
372 }
373 
374 START_TEST(dosdev)
375 {
376     test_DefineDosDeviceA();
377     test_QueryDosDeviceA();
378 }
379