1 /*
2  * Unit test suite for dir functions
3  *
4  * Copyright 2006 CodeWeavers, Aric Stewart
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 #include <mbctype.h>
24 
25 static int (__cdecl *p_makepath_s)(char *, size_t, const char *, const char *, const char *, const char *);
26 static int (__cdecl *p_wmakepath_s)(wchar_t *, size_t, const wchar_t *,const wchar_t *, const wchar_t *, const wchar_t *);
27 
28 static void init(void)
29 {
30     HMODULE hmod = GetModuleHandleA("msvcrt.dll");
31 
32     p_makepath_s = (void *)GetProcAddress(hmod, "_makepath_s");
33     p_wmakepath_s = (void *)GetProcAddress(hmod, "_wmakepath_s");
34 }
35 
36 typedef struct
37 {
38     const char* buffer;
39     const char* drive;
40     const char* dir;
41     const char* file;
42     const char* ext;
43     const char* expected;
44 } makepath_case;
45 
46 #define USE_BUFF ((char*)~0ul)
47 static const makepath_case makepath_cases[] =
48 {
49     { NULL, NULL, NULL, NULL, NULL, "" }, /* 0 */
50     { NULL, "c", NULL, NULL, NULL, "c:" },
51     { NULL, "c:", NULL, NULL, NULL, "c:" },
52     { NULL, "c:\\", NULL, NULL, NULL, "c:" },
53     { NULL, NULL, "dir", NULL, NULL, "dir\\" },
54     { NULL, NULL, "dir\\", NULL, NULL, "dir\\" },
55     { NULL, NULL, "\\dir", NULL, NULL, "\\dir\\" },
56     { NULL, NULL, NULL, "file", NULL, "file" },
57     { NULL, NULL, NULL, "\\file", NULL, "\\file" },
58     { NULL, NULL, NULL, "file", NULL, "file" },
59     { NULL, NULL, NULL, NULL, "ext", ".ext" }, /* 10 */
60     { NULL, NULL, NULL, NULL, ".ext", ".ext" },
61     { "foo", NULL, NULL, NULL, NULL, "" },
62     { "foo", USE_BUFF, NULL, NULL, NULL, "f:" },
63     { "foo", NULL, USE_BUFF, NULL, NULL, "foo\\" },
64     { "foo", NULL, NULL, USE_BUFF, NULL, "foo" },
65     { "foo", NULL, USE_BUFF, "file", NULL, "foo\\file" },
66     { "foo", NULL, USE_BUFF, "file", "ext", "foo\\file.ext" },
67     { "foo", NULL, NULL, USE_BUFF, "ext", "foo.ext" },
68     /* remaining combinations of USE_BUFF crash native */
69     { NULL, "c", "dir", "file", "ext", "c:dir\\file.ext" },
70     { NULL, "c:", "dir", "file", "ext", "c:dir\\file.ext" }, /* 20 */
71     { NULL, "c:\\", "dir", "file", "ext", "c:dir\\file.ext" }
72 };
73 
74 static void test_makepath(void)
75 {
76     WCHAR driveW[MAX_PATH];
77     WCHAR dirW[MAX_PATH];
78     WCHAR fileW[MAX_PATH];
79     WCHAR extW[MAX_PATH];
80     WCHAR bufferW[MAX_PATH];
81     char buffer[MAX_PATH];
82 
83     unsigned int i, n;
84 
85     for (i = 0; i < sizeof(makepath_cases)/sizeof(makepath_cases[0]); ++i)
86     {
87         const makepath_case* p = &makepath_cases[i];
88 
89         memset(buffer, 'X', MAX_PATH);
90         if (p->buffer)
91             strcpy(buffer, p->buffer);
92 
93         /* Ascii */
94         _makepath(buffer,
95                   p->drive == USE_BUFF ? buffer : p->drive,
96                   p->dir == USE_BUFF ? buffer : p->dir,
97                   p->file == USE_BUFF? buffer : p->file,
98                   p->ext == USE_BUFF ? buffer : p->ext);
99 
100         buffer[MAX_PATH - 1] = '\0';
101         ok(!strcmp(p->expected, buffer), "got '%s' for case %d\n", buffer, i);
102 
103         /* Unicode */
104         if (p->drive != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->drive, -1, driveW, MAX_PATH);
105         if (p->dir != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->dir, -1, dirW, MAX_PATH);
106         if (p->file != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->file, -1, fileW, MAX_PATH);
107         if (p->ext != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->ext, -1, extW, MAX_PATH);
108 
109         memset(buffer, 0, MAX_PATH);
110         for (n = 0; n < MAX_PATH; ++n)
111             bufferW[n] = 'X';
112         if (p->buffer) MultiByteToWideChar( CP_ACP, 0, p->buffer, -1, bufferW, MAX_PATH);
113 
114         _wmakepath(bufferW,
115                    p->drive == USE_BUFF ? bufferW : p->drive ? driveW : NULL,
116                    p->dir == USE_BUFF ? bufferW : p->dir ? dirW : NULL,
117                    p->file == USE_BUFF? bufferW : p->file ? fileW : NULL,
118                    p->ext == USE_BUFF ? bufferW : p->ext ? extW : NULL);
119 
120         bufferW[MAX_PATH - 1] = '\0';
121         WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
122         ok(!strcmp(p->expected, buffer), "got '%s' for unicode case %d\n", buffer, i);
123     }
124 }
125 
126 static const WCHAR expected0[] = {'\0','X','X','X','X','X','X','X','X','X','X','X','X'};
127 static const WCHAR expected1[] = {'\0','X','X','X','X','X','X','X','X','X','X','X','X'};
128 static const WCHAR expected2[] = {'\0',':','X','X','X','X','X','X','X','X','X','X','X'};
129 static const WCHAR expected3[] = {'\0',':','d','X','X','X','X','X','X','X','X','X','X'};
130 static const WCHAR expected4[] = {'\0',':','d','\\','X','X','X','X','X','X','X','X','X'};
131 static const WCHAR expected5[] = {'\0',':','d','\\','f','X','X','X','X','X','X','X','X'};
132 static const WCHAR expected6[] = {'\0',':','d','\\','f','i','X','X','X','X','X','X','X'};
133 static const WCHAR expected7[] = {'\0',':','d','\\','f','i','l','X','X','X','X','X','X'};
134 static const WCHAR expected8[] = {'\0',':','d','\\','f','i','l','e','X','X','X','X','X'};
135 static const WCHAR expected9[] = {'\0',':','d','\\','f','i','l','e','.','X','X','X','X'};
136 static const WCHAR expected10[] = {'\0',':','d','\\','f','i','l','e','.','e','X','X','X'};
137 static const WCHAR expected11[] = {'\0',':','d','\\','f','i','l','e','.','e','x','X','X'};
138 
139 static const WCHAR expected12[] = {'\0',':','X','X','X','X','X','X','X','X'};
140 static const WCHAR expected13[] = {'\0',':','d','X','X','X','X','X','X','X'};
141 static const WCHAR expected14[] = {'\0',':','d','i','X','X','X','X','X','X'};
142 static const WCHAR expected15[] = {'\0',':','d','i','r','X','X','X','X','X'};
143 static const WCHAR expected16[] = {'\0',':','d','i','r','\\','X','X','X','X'};
144 
145 static const WCHAR expected17[] = {'\0','o','o'};
146 static const WCHAR expected18[] = {'\0','o','o','\0','X'};
147 static const WCHAR expected19[] = {'\0','o','o','\0'};
148 static const WCHAR expected20[] = {'\0','o','o','\0','X','X','X','X','X'};
149 static const WCHAR expected21[] = {'\0','o','o','\\','f','i','l','X','X'};
150 static const WCHAR expected22[] = {'\0','o','o','\0','X','X','X','X','X','X','X','X','X'};
151 static const WCHAR expected23[] = {'\0','o','o','\\','f','i','l','X','X','X','X','X','X'};
152 static const WCHAR expected24[] = {'\0','o','o','\\','f','i','l','e','.','e','x','X','X'};
153 static const WCHAR expected25[] = {'\0','o','o','\0','X','X','X','X'};
154 static const WCHAR expected26[] = {'\0','o','o','.','e','x','X','X'};
155 
156 typedef struct
157 {
158     const char* buffer;
159     size_t length;
160     const char* drive;
161     const char* dir;
162     const char* file;
163     const char* ext;
164     const char* expected;
165     const WCHAR *expected_unicode;
166     size_t expected_length;
167 } makepath_s_case;
168 
169 static const makepath_s_case makepath_s_cases[] =
170 {
171     /* Behavior with directory parameter containing backslash. */
172     {NULL, 1, "c:", "d\\", "file", "ext", "\0XXXXXXXXXXXX", expected0, 13},
173     {NULL, 2, "c:", "d\\", "file", "ext", "\0XXXXXXXXXXXX", expected1, 13},
174     {NULL, 3, "c:", "d\\", "file", "ext", "\0:XXXXXXXXXXX", expected2, 13},
175     {NULL, 4, "c:", "d\\", "file", "ext", "\0:dXXXXXXXXXX", expected3, 13},
176     {NULL, 5, "c:", "d\\", "file", "ext", "\0:d\\XXXXXXXXX", expected4, 13},
177     {NULL, 6, "c:", "d\\", "file", "ext", "\0:d\\fXXXXXXXX", expected5, 13},
178     {NULL, 7, "c:", "d\\", "file", "ext", "\0:d\\fiXXXXXXX", expected6, 13},
179     {NULL, 8, "c:", "d\\", "file", "ext", "\0:d\\filXXXXXX", expected7, 13},
180     {NULL, 9, "c:", "d\\", "file", "ext", "\0:d\\fileXXXXX", expected8, 13},
181     {NULL, 10, "c:", "d\\", "file", "ext", "\0:d\\file.XXXX", expected9, 13},
182     {NULL, 11, "c:", "d\\", "file", "ext", "\0:d\\file.eXXX", expected10, 13},
183     {NULL, 12, "c:", "d\\", "file", "ext", "\0:d\\file.exXX", expected11, 13},
184     /* Behavior with directory parameter lacking backslash. */
185     {NULL, 3, "c:", "dir", "f", "ext", "\0:XXXXXXXX", expected12, 10},
186     {NULL, 4, "c:", "dir", "f", "ext", "\0:dXXXXXXX", expected13, 10},
187     {NULL, 5, "c:", "dir", "f", "ext", "\0:diXXXXXX", expected14, 10},
188     {NULL, 6, "c:", "dir", "f", "ext", "\0:dirXXXXX", expected15, 10},
189     {NULL, 7, "c:", "dir", "f", "ext", "\0:dir\\XXXX", expected16, 10},
190     /* Behavior with overlapped buffer. */
191     {"foo", 2, USE_BUFF, NULL, NULL, NULL, "\0oo", expected17, 3},
192     {"foo", 4, NULL, USE_BUFF, NULL, NULL, "\0oo\0X", expected18, 5},
193     {"foo", 3, NULL, NULL, USE_BUFF, NULL, "\0oo\0", expected19, 4},
194     {"foo", 4, NULL, USE_BUFF, "file", NULL, "\0oo\0XXXXX", expected20, 9},
195     {"foo", 8, NULL, USE_BUFF, "file", NULL, "\0oo\\filXX", expected21, 9},
196     {"foo", 4, NULL, USE_BUFF, "file", "ext", "\0oo\0XXXXXXXXX", expected22, 13},
197     {"foo", 8, NULL, USE_BUFF, "file", "ext", "\0oo\\filXXXXXX", expected23, 13},
198     {"foo", 12, NULL, USE_BUFF, "file", "ext", "\0oo\\file.exXX", expected24, 13},
199     {"foo", 4, NULL, NULL, USE_BUFF, "ext", "\0oo\0XXXX", expected25, 8},
200     {"foo", 7, NULL, NULL, USE_BUFF, "ext", "\0oo.exXX", expected26, 8},
201 };
202 
203 static void test_makepath_s(void)
204 {
205     WCHAR driveW[MAX_PATH];
206     WCHAR dirW[MAX_PATH];
207     WCHAR fileW[MAX_PATH];
208     WCHAR extW[MAX_PATH];
209     WCHAR bufferW[MAX_PATH];
210     char buffer[MAX_PATH];
211     int ret;
212     unsigned int i, n;
213 
214     if (!p_makepath_s || !p_wmakepath_s)
215     {
216         win_skip("Safe makepath functions are not available\n");
217         return;
218     }
219 
220     errno = EBADF;
221     ret = p_makepath_s(NULL, 0, NULL, NULL, NULL, NULL);
222     ok(ret == EINVAL, "Expected _makepath_s to return EINVAL, got %d\n", ret);
223     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
224 
225     errno = EBADF;
226     ret = p_makepath_s(buffer, 0, NULL, NULL, NULL, NULL);
227     ok(ret == EINVAL, "Expected _makepath_s to return EINVAL, got %d\n", ret);
228     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
229 
230     errno = EBADF;
231     ret = p_wmakepath_s(NULL, 0, NULL, NULL, NULL, NULL);
232     ok(ret == EINVAL, "Expected _wmakepath_s to return EINVAL, got %d\n", ret);
233     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
234 
235     errno = EBADF;
236     ret = p_wmakepath_s(bufferW, 0, NULL, NULL, NULL, NULL);
237     ok(ret == EINVAL, "Expected _wmakepath_s to return EINVAL, got %d\n", ret);
238     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
239 
240     /* Test with the normal _makepath cases. */
241     for (i = 0; i < sizeof(makepath_cases)/sizeof(makepath_cases[0]); i++)
242     {
243         const makepath_case *p = makepath_cases + i;
244 
245         memset(buffer, 'X', MAX_PATH);
246         if (p->buffer)
247             strcpy(buffer, p->buffer);
248 
249         /* Ascii */
250         ret = p_makepath_s(buffer, MAX_PATH,
251                            p->drive == USE_BUFF ? buffer : p->drive,
252                            p->dir == USE_BUFF ? buffer : p->dir,
253                            p->file == USE_BUFF? buffer : p->file,
254                            p->ext == USE_BUFF ? buffer : p->ext);
255         ok(ret == 0, "[%d] Expected _makepath_s to return 0, got %d\n", i, ret);
256 
257         buffer[MAX_PATH - 1] = '\0';
258         ok(!strcmp(p->expected, buffer), "got '%s' for case %d\n", buffer, i);
259 
260         /* Unicode */
261         if (p->drive != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->drive, -1, driveW, MAX_PATH);
262         if (p->dir != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->dir, -1, dirW, MAX_PATH);
263         if (p->file != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->file, -1, fileW, MAX_PATH);
264         if (p->ext != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->ext, -1, extW, MAX_PATH);
265 
266         memset(buffer, 0, MAX_PATH);
267         for (n = 0; n < MAX_PATH; ++n)
268             bufferW[n] = 'X';
269         if (p->buffer) MultiByteToWideChar( CP_ACP, 0, p->buffer, -1, bufferW, MAX_PATH);
270 
271         ret = p_wmakepath_s(bufferW, MAX_PATH,
272                             p->drive == USE_BUFF ? bufferW : p->drive ? driveW : NULL,
273                             p->dir == USE_BUFF ? bufferW : p->dir ? dirW : NULL,
274                             p->file == USE_BUFF? bufferW : p->file ? fileW : NULL,
275                             p->ext == USE_BUFF ? bufferW : p->ext ? extW : NULL);
276         ok(ret == 0, "[%d] Expected _wmakepath_s to return 0, got %d\n", i, ret);
277 
278         bufferW[MAX_PATH - 1] = '\0';
279         WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
280         ok(!strcmp(p->expected, buffer), "got '%s' for unicode case %d\n", buffer, i);
281     }
282 
283     /* Try insufficient length cases. */
284     for (i = 0; i < sizeof(makepath_s_cases)/sizeof(makepath_s_cases[0]); i++)
285     {
286         const makepath_s_case *p = makepath_s_cases + i;
287 
288         memset(buffer, 'X', MAX_PATH);
289         if (p->buffer)
290             strcpy(buffer, p->buffer);
291 
292         /* Ascii */
293         errno = EBADF;
294         ret = p_makepath_s(buffer, p->length,
295                            p->drive == USE_BUFF ? buffer : p->drive,
296                            p->dir == USE_BUFF ? buffer : p->dir,
297                            p->file == USE_BUFF? buffer : p->file,
298                            p->ext == USE_BUFF ? buffer : p->ext);
299         ok(ret == ERANGE, "[%d] Expected _makepath_s to return ERANGE, got %d\n", i, ret);
300         ok(errno == ERANGE, "[%d] Expected errno to be ERANGE, got %d\n", i, errno);
301         ok(!memcmp(p->expected, buffer, p->expected_length), "unexpected output for case %d\n", i);
302 
303         /* Unicode */
304         if (p->drive != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->drive, -1, driveW, MAX_PATH);
305         if (p->dir != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->dir, -1, dirW, MAX_PATH);
306         if (p->file != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->file, -1, fileW, MAX_PATH);
307         if (p->ext != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->ext, -1, extW, MAX_PATH);
308 
309         memset(buffer, 0, MAX_PATH);
310         for (n = 0; n < MAX_PATH; ++n)
311             bufferW[n] = 'X';
312         if (p->buffer) MultiByteToWideChar( CP_ACP, 0, p->buffer, -1, bufferW, MAX_PATH);
313 
314         errno = EBADF;
315         ret = p_wmakepath_s(bufferW, p->length,
316                             p->drive == USE_BUFF ? bufferW : p->drive ? driveW : NULL,
317                             p->dir == USE_BUFF ? bufferW : p->dir ? dirW : NULL,
318                             p->file == USE_BUFF? bufferW : p->file ? fileW : NULL,
319                             p->ext == USE_BUFF ? bufferW : p->ext ? extW : NULL);
320         ok(ret == ERANGE, "[%d] Expected _wmakepath_s to return ERANGE, got %d\n", i, ret);
321         ok(errno == ERANGE, "[%d] Expected errno to be ERANGE, got %d\n", i, errno);
322 
323         ok(!memcmp(p->expected_unicode, bufferW, p->expected_length * sizeof(WCHAR)), "unexpected output for case %d\n", i);
324     }
325 }
326 
327 static void test_fullpath(void)
328 {
329     char full[MAX_PATH];
330     char tmppath[MAX_PATH];
331     char prevpath[MAX_PATH];
332     char level1[MAX_PATH];
333     char level2[MAX_PATH];
334     char teststring[MAX_PATH];
335     char *freeme;
336     BOOL rc,free1,free2;
337 
338     free1=free2=TRUE;
339     GetCurrentDirectoryA(MAX_PATH, prevpath);
340     GetTempPathA(MAX_PATH,tmppath);
341     strcpy(level1,tmppath);
342     strcat(level1,"msvcrt-test\\");
343 
344     rc = CreateDirectoryA(level1,NULL);
345     if (!rc && GetLastError()==ERROR_ALREADY_EXISTS)
346         free1=FALSE;
347 
348     strcpy(level2,level1);
349     strcat(level2,"nextlevel\\");
350     rc = CreateDirectoryA(level2,NULL);
351     if (!rc && GetLastError()==ERROR_ALREADY_EXISTS)
352         free2=FALSE;
353     SetCurrentDirectoryA(level2);
354 
355     ok(_fullpath(full,"test", MAX_PATH)!=NULL,"_fullpath failed\n");
356     strcpy(teststring,level2);
357     strcat(teststring,"test");
358     ok(strcmp(full,teststring)==0,"Invalid Path returned %s\n",full);
359     ok(_fullpath(full,"\\test", MAX_PATH)!=NULL,"_fullpath failed\n");
360     memcpy(teststring,level2,3);
361     teststring[3]=0;
362     strcat(teststring,"test");
363     ok(strcmp(full,teststring)==0,"Invalid Path returned %s\n",full);
364     ok(_fullpath(full,"..\\test", MAX_PATH)!=NULL,"_fullpath failed\n");
365     strcpy(teststring,level1);
366     strcat(teststring,"test");
367     ok(strcmp(full,teststring)==0,"Invalid Path returned %s\n",full);
368     ok(_fullpath(full,"..\\test", 10)==NULL,"_fullpath failed to generate error\n");
369 
370     freeme = _fullpath(NULL,"test", 0);
371     ok(freeme!=NULL,"No path returned\n");
372     strcpy(teststring,level2);
373     strcat(teststring,"test");
374     ok(strcmp(freeme,teststring)==0,"Invalid Path returned %s\n",freeme);
375     free(freeme);
376 
377     SetCurrentDirectoryA(prevpath);
378     if (free2)
379         RemoveDirectoryA(level2);
380     if (free1)
381         RemoveDirectoryA(level1);
382 }
383 
384 static void test_splitpath(void)
385 {
386     const char* path = "c:\\\x83\x5c\x83\x74\x83\x67.bin";
387     char drive[3], dir[MAX_PATH], fname[MAX_PATH], ext[MAX_PATH];
388     int prev_cp = _getmbcp();
389 
390     /* SBCS codepage */
391     _setmbcp(1252);
392     _splitpath(path, drive, dir, fname, ext);
393     ok(!strcmp(drive, "c:"), "got %s\n", drive);
394     ok(!strcmp(dir, "\\\x83\x5c"), "got %s\n", dir);
395     ok(!strcmp(fname, "\x83\x74\x83\x67"), "got %s\n", fname);
396     ok(!strcmp(ext, ".bin"), "got %s\n", ext);
397 
398     /* MBCS (Japanese) codepage */
399     _setmbcp(932);
400     _splitpath(path, drive, dir, fname, ext);
401     ok(!strcmp(drive, "c:"), "got %s\n", drive);
402     ok(!strcmp(dir, "\\"), "got %s\n", dir);
403     ok(!strcmp(fname, "\x83\x5c\x83\x74\x83\x67"), "got %s\n", fname);
404     ok(!strcmp(ext, ".bin"), "got %s\n", ext);
405 
406     _setmbcp(prev_cp);
407 }
408 
409 START_TEST(dir)
410 {
411     init();
412 
413     test_fullpath();
414     test_makepath();
415     test_makepath_s();
416     test_splitpath();
417 }
418