msvcrt: Implement _makepath_s.
[wine.git] / dlls / msvcrt / tests / dir.c
blob98776c3753a72ffd50cb29d17ddf0406fb45652a
1 /*
2 * Unit test suite for dir functions
4 * Copyright 2006 CodeWeavers, Aric Stewart
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.
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.
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
21 #include "wine/test.h"
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <io.h>
28 #include <windef.h>
29 #include <winbase.h>
30 #include <winnls.h>
31 #include <process.h>
32 #include <errno.h>
34 static int (__cdecl *p_makepath_s)(char *, size_t, const char *, const char *, const char *, const char *);
36 static void init(void)
38 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
40 p_makepath_s = (void *)GetProcAddress(hmod, "_makepath_s");
43 typedef struct
45 const char* buffer;
46 const char* drive;
47 const char* dir;
48 const char* file;
49 const char* ext;
50 const char* expected;
51 } makepath_case;
53 #define USE_BUFF ((char*)~0ul)
54 static const makepath_case makepath_cases[] =
56 { NULL, NULL, NULL, NULL, NULL, "" }, /* 0 */
57 { NULL, "c", NULL, NULL, NULL, "c:" },
58 { NULL, "c:", NULL, NULL, NULL, "c:" },
59 { NULL, "c:\\", NULL, NULL, NULL, "c:" },
60 { NULL, NULL, "dir", NULL, NULL, "dir\\" },
61 { NULL, NULL, "dir\\", NULL, NULL, "dir\\" },
62 { NULL, NULL, "\\dir", NULL, NULL, "\\dir\\" },
63 { NULL, NULL, NULL, "file", NULL, "file" },
64 { NULL, NULL, NULL, "\\file", NULL, "\\file" },
65 { NULL, NULL, NULL, "file", NULL, "file" },
66 { NULL, NULL, NULL, NULL, "ext", ".ext" }, /* 10 */
67 { NULL, NULL, NULL, NULL, ".ext", ".ext" },
68 { "foo", NULL, NULL, NULL, NULL, "" },
69 { "foo", USE_BUFF, NULL, NULL, NULL, "f:" },
70 { "foo", NULL, USE_BUFF, NULL, NULL, "foo\\" },
71 { "foo", NULL, NULL, USE_BUFF, NULL, "foo" },
72 { "foo", NULL, USE_BUFF, "file", NULL, "foo\\file" },
73 { "foo", NULL, USE_BUFF, "file", "ext", "foo\\file.ext" },
74 { "foo", NULL, NULL, USE_BUFF, "ext", "foo.ext" },
75 /* remaining combinations of USE_BUFF crash native */
76 { NULL, "c", "dir", "file", "ext", "c:dir\\file.ext" },
77 { NULL, "c:", "dir", "file", "ext", "c:dir\\file.ext" }, /* 20 */
78 { NULL, "c:\\", "dir", "file", "ext", "c:dir\\file.ext" }
81 static void test_makepath(void)
83 WCHAR driveW[MAX_PATH];
84 WCHAR dirW[MAX_PATH];
85 WCHAR fileW[MAX_PATH];
86 WCHAR extW[MAX_PATH];
87 WCHAR bufferW[MAX_PATH];
88 char buffer[MAX_PATH];
90 unsigned int i, n;
92 for (i = 0; i < sizeof(makepath_cases)/sizeof(makepath_cases[0]); ++i)
94 const makepath_case* p = &makepath_cases[i];
96 memset(buffer, 'X', MAX_PATH);
97 if (p->buffer)
98 strcpy(buffer, p->buffer);
100 /* Ascii */
101 _makepath(buffer,
102 p->drive == USE_BUFF ? buffer : p->drive,
103 p->dir == USE_BUFF ? buffer : p->dir,
104 p->file == USE_BUFF? buffer : p->file,
105 p->ext == USE_BUFF ? buffer : p->ext);
107 buffer[MAX_PATH - 1] = '\0';
108 ok(!strcmp(p->expected, buffer), "got '%s' for case %d\n", buffer, i);
110 /* Unicode */
111 if (p->drive != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->drive, -1, driveW, MAX_PATH);
112 if (p->dir != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->dir, -1, dirW, MAX_PATH);
113 if (p->file != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->file, -1, fileW, MAX_PATH);
114 if (p->ext != USE_BUFF) MultiByteToWideChar(CP_ACP, 0, p->ext, -1, extW, MAX_PATH);
116 memset(buffer, 0, MAX_PATH);
117 for (n = 0; n < MAX_PATH; ++n)
118 bufferW[n] = 'X';
119 if (p->buffer) MultiByteToWideChar( CP_ACP, 0, p->buffer, -1, bufferW, MAX_PATH);
121 _wmakepath(bufferW,
122 p->drive == USE_BUFF ? bufferW : p->drive ? driveW : NULL,
123 p->dir == USE_BUFF ? bufferW : p->dir ? dirW : NULL,
124 p->file == USE_BUFF? bufferW : p->file ? fileW : NULL,
125 p->ext == USE_BUFF ? bufferW : p->ext ? extW : NULL);
127 bufferW[MAX_PATH - 1] = '\0';
128 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
129 ok(!strcmp(p->expected, buffer), "got '%s' for unicode case %d\n", buffer, i);
133 typedef struct
135 const char* buffer;
136 size_t length;
137 const char* drive;
138 const char* dir;
139 const char* file;
140 const char* ext;
141 const char* expected;
142 size_t expected_length;
143 } makepath_s_case;
145 static const makepath_s_case makepath_s_cases[] =
147 /* Behavior with directory parameter containing backslash. */
148 {NULL, 1, "c:", "d\\", "file", "ext", "\0XXXXXXXXXXXX", 13},
149 {NULL, 2, "c:", "d\\", "file", "ext", "\0XXXXXXXXXXXX", 13},
150 {NULL, 3, "c:", "d\\", "file", "ext", "\0:XXXXXXXXXXX", 13},
151 {NULL, 4, "c:", "d\\", "file", "ext", "\0:dXXXXXXXXXX", 13},
152 {NULL, 5, "c:", "d\\", "file", "ext", "\0:d\\XXXXXXXXX", 13},
153 {NULL, 6, "c:", "d\\", "file", "ext", "\0:d\\fXXXXXXXX", 13},
154 {NULL, 7, "c:", "d\\", "file", "ext", "\0:d\\fiXXXXXXX", 13},
155 {NULL, 8, "c:", "d\\", "file", "ext", "\0:d\\filXXXXXX", 13},
156 {NULL, 9, "c:", "d\\", "file", "ext", "\0:d\\fileXXXXX", 13},
157 {NULL, 10, "c:", "d\\", "file", "ext", "\0:d\\file.XXXX", 13},
158 {NULL, 11, "c:", "d\\", "file", "ext", "\0:d\\file.eXXX", 13},
159 {NULL, 12, "c:", "d\\", "file", "ext", "\0:d\\file.exXX", 13},
160 /* Behavior with directory parameter lacking backslash. */
161 {NULL, 3, "c:", "dir", "f", "ext", "\0:XXXXXXXX", 10},
162 {NULL, 4, "c:", "dir", "f", "ext", "\0:dXXXXXXX", 10},
163 {NULL, 5, "c:", "dir", "f", "ext", "\0:diXXXXXX", 10},
164 {NULL, 6, "c:", "dir", "f", "ext", "\0:dirXXXXX", 10},
165 {NULL, 7, "c:", "dir", "f", "ext", "\0:dir\\XXXX", 10},
166 /* Behavior with overlapped buffer. */
167 {"foo", 2, USE_BUFF, NULL, NULL, NULL, "\0oo", 3},
168 {"foo", 4, NULL, USE_BUFF, NULL, NULL, "\0oo\0X", 5},
169 {"foo", 3, NULL, NULL, USE_BUFF, NULL, "\0oo\0", 4},
170 {"foo", 4, NULL, USE_BUFF, "file", NULL, "\0oo\0XXXXX", 9},
171 {"foo", 8, NULL, USE_BUFF, "file", NULL, "\0oo\\filXX", 9},
172 {"foo", 4, NULL, USE_BUFF, "file", "ext", "\0oo\0XXXXXXXXX", 13},
173 {"foo", 8, NULL, USE_BUFF, "file", "ext", "\0oo\\filXXXXXX", 13},
174 {"foo", 12, NULL, USE_BUFF, "file", "ext", "\0oo\\file.exXX", 13},
175 {"foo", 4, NULL, NULL, USE_BUFF, "ext", "\0oo\0XXXX", 8},
176 {"foo", 7, NULL, NULL, USE_BUFF, "ext", "\0oo.exXX", 8},
179 static void test_makepath_s(void)
181 char buffer[MAX_PATH];
182 int ret;
183 unsigned int i;
185 if (!p_makepath_s)
187 win_skip("_makepath_s is not available\n");
188 return;
191 errno = EBADF;
192 ret = p_makepath_s(NULL, 0, NULL, NULL, NULL, NULL);
193 ok(ret == EINVAL, "Expected _makepath_s to return EINVAL, got %d\n", ret);
194 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
196 errno = EBADF;
197 ret = p_makepath_s(buffer, 0, NULL, NULL, NULL, NULL);
198 ok(ret == EINVAL, "Expected _makepath_s to return EINVAL, got %d\n", ret);
199 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
201 /* Test with the normal _makepath cases. */
202 for (i = 0; i < sizeof(makepath_cases)/sizeof(makepath_cases[0]); i++)
204 const makepath_case *p = makepath_cases + i;
206 memset(buffer, 'X', MAX_PATH);
207 if (p->buffer)
208 strcpy(buffer, p->buffer);
210 /* Ascii */
211 ret = p_makepath_s(buffer, MAX_PATH,
212 p->drive == USE_BUFF ? buffer : p->drive,
213 p->dir == USE_BUFF ? buffer : p->dir,
214 p->file == USE_BUFF? buffer : p->file,
215 p->ext == USE_BUFF ? buffer : p->ext);
216 ok(ret == 0, "[%d] Expected _makepath_s to return 0, got %d\n", i, ret);
218 buffer[MAX_PATH - 1] = '\0';
219 ok(!strcmp(p->expected, buffer), "got '%s' for case %d\n", buffer, i);
222 /* Try insufficient length cases. */
223 for (i = 0; i < sizeof(makepath_s_cases)/sizeof(makepath_s_cases[0]); i++)
225 const makepath_s_case *p = makepath_s_cases + i;
227 memset(buffer, 'X', MAX_PATH);
228 if (p->buffer)
229 strcpy(buffer, p->buffer);
231 /* Ascii */
232 errno = EBADF;
233 ret = p_makepath_s(buffer, p->length,
234 p->drive == USE_BUFF ? buffer : p->drive,
235 p->dir == USE_BUFF ? buffer : p->dir,
236 p->file == USE_BUFF? buffer : p->file,
237 p->ext == USE_BUFF ? buffer : p->ext);
238 ok(ret == ERANGE, "[%d] Expected _makepath_s to return ERANGE, got %d\n", i, ret);
239 ok(errno == ERANGE, "[%d] Expected errno to be ERANGE, got %d\n", i, errno);
240 ok(!memcmp(p->expected, buffer, p->expected_length), "unexpected output for case %d\n", i);
244 static void test_fullpath(void)
246 char full[MAX_PATH];
247 char tmppath[MAX_PATH];
248 char prevpath[MAX_PATH];
249 char level1[MAX_PATH];
250 char level2[MAX_PATH];
251 char teststring[MAX_PATH];
252 char *freeme;
253 BOOL rc,free1,free2;
255 free1=free2=TRUE;
256 GetCurrentDirectory(MAX_PATH, prevpath);
257 GetTempPath(MAX_PATH,tmppath);
258 strcpy(level1,tmppath);
259 strcat(level1,"msvcrt-test\\");
261 rc = CreateDirectory(level1,NULL);
262 if (!rc && GetLastError()==ERROR_ALREADY_EXISTS)
263 free1=FALSE;
265 strcpy(level2,level1);
266 strcat(level2,"nextlevel\\");
267 rc = CreateDirectory(level2,NULL);
268 if (!rc && GetLastError()==ERROR_ALREADY_EXISTS)
269 free2=FALSE;
270 SetCurrentDirectory(level2);
272 ok(_fullpath(full,"test", MAX_PATH)!=NULL,"_fullpath failed\n");
273 strcpy(teststring,level2);
274 strcat(teststring,"test");
275 ok(strcmp(full,teststring)==0,"Invalid Path returned %s\n",full);
276 ok(_fullpath(full,"\\test", MAX_PATH)!=NULL,"_fullpath failed\n");
277 strncpy(teststring,level2,3);
278 teststring[3]=0;
279 strcat(teststring,"test");
280 ok(strcmp(full,teststring)==0,"Invalid Path returned %s\n",full);
281 ok(_fullpath(full,"..\\test", MAX_PATH)!=NULL,"_fullpath failed\n");
282 strcpy(teststring,level1);
283 strcat(teststring,"test");
284 ok(strcmp(full,teststring)==0,"Invalid Path returned %s\n",full);
285 ok(_fullpath(full,"..\\test", 10)==NULL,"_fullpath failed to generate error\n");
287 freeme = _fullpath(NULL,"test", 0);
288 ok(freeme!=NULL,"No path returned\n");
289 strcpy(teststring,level2);
290 strcat(teststring,"test");
291 ok(strcmp(freeme,teststring)==0,"Invalid Path returned %s\n",freeme);
292 free(freeme);
294 SetCurrentDirectory(prevpath);
295 if (free2)
296 RemoveDirectory(level2);
297 if (free1)
298 RemoveDirectory(level1);
301 START_TEST(dir)
303 init();
305 test_fullpath();
306 test_makepath();
307 test_makepath_s();