ntdll: Allow reading from a writable section.
[wine] / dlls / ntdll / tests / path.c
1 /*
2  * Unit test suite for ntdll path functions
3  *
4  * Copyright 2002 Alexandre Julliard
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 "ntdll_test.h"
22
23 static NTSTATUS (WINAPI *pRtlMultiByteToUnicodeN)( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
24                                                    LPCSTR src, DWORD srclen );
25 static NTSTATUS (WINAPI *pRtlUnicodeToMultiByteN)(LPSTR,DWORD,LPDWORD,LPCWSTR,DWORD);
26 static UINT (WINAPI *pRtlDetermineDosPathNameType_U)( PCWSTR path );
27 static ULONG (WINAPI *pRtlIsDosDeviceName_U)( PCWSTR dos_name );
28 static NTSTATUS (WINAPI *pRtlOemStringToUnicodeString)(UNICODE_STRING *, const STRING *, BOOLEAN );
29 static BOOLEAN (WINAPI *pRtlIsNameLegalDOS8Dot3)(const UNICODE_STRING*,POEM_STRING,PBOOLEAN);
30 static DWORD (WINAPI *pRtlGetFullPathName_U)(const WCHAR*,ULONG,WCHAR*,WCHAR**);
31
32
33 static void test_RtlDetermineDosPathNameType(void)
34 {
35     struct test
36     {
37         const char *path;
38         UINT ret;
39     };
40
41     static const struct test tests[] =
42     {
43         { "\\\\foo", 1 },
44         { "//foo", 1 },
45         { "\\/foo", 1 },
46         { "/\\foo", 1 },
47         { "\\\\", 1 },
48         { "//", 1 },
49         { "c:\\foo", 2 },
50         { "c:/foo", 2 },
51         { "c://foo", 2 },
52         { "c:\\", 2 },
53         { "c:/", 2 },
54         { "c:foo", 3 },
55         { "c:f\\oo", 3 },
56         { "c:foo/bar", 3 },
57         { "\\foo", 4 },
58         { "/foo", 4 },
59         { "\\", 4 },
60         { "/", 4 },
61         { "foo", 5 },
62         { "", 5 },
63         { "\0:foo", 5 },
64         { "\\\\.\\foo", 6 },
65         { "//./foo", 6 },
66         { "/\\./foo", 6 },
67         { "\\\\.foo", 1 },
68         { "//.foo", 1 },
69         { "\\\\.", 7 },
70         { "//.", 7 },
71         { NULL, 0 }
72     };
73
74     const struct test *test;
75     WCHAR buffer[MAX_PATH];
76     UINT ret;
77
78     for (test = tests; test->path; test++)
79     {
80         pRtlMultiByteToUnicodeN( buffer, sizeof(buffer), NULL, test->path, strlen(test->path)+1 );
81         ret = pRtlDetermineDosPathNameType_U( buffer );
82         ok( ret == test->ret, "Wrong result %d/%d for %s\n", ret, test->ret, test->path );
83     }
84 }
85
86
87 static void test_RtlIsDosDeviceName(void)
88 {
89     struct test
90     {
91         const char *path;
92         WORD pos;
93         WORD len;
94         BOOL fails;
95     };
96
97     static const struct test tests[] =
98     {
99         { "\\\\.\\CON",    8, 6 },
100         { "\\\\.\\con",    8, 6 },
101         { "\\\\.\\CON2",   0, 0 },
102         { "",              0, 0 },
103         { "\\\\foo\\nul",  0, 0 },
104         { "c:\\nul:",      6, 6 },
105         { "c:\\nul\\",     0, 0 },
106         { "c:\\nul\\foo",  0, 0 },
107         { "c:\\nul::",     6, 6, TRUE },  /* fails on nt4 */
108         { "c:\\nul::::::", 6, 6, TRUE },  /* fails on nt4 */
109         { "c:prn     ",    4, 6 },
110         { "c:prn.......",  4, 6 },
111         { "c:prn... ...",  4, 6 },
112         { "c:NUL  ....  ", 4, 6, TRUE },  /* fails on nt4 */
113         { "c: . . .",      0, 0 },
114         { "c:",            0, 0 },
115         { " . . . :",      0, 0 },
116         { ":",             0, 0 },
117         { "c:nul. . . :",  4, 6 },
118         { "c:nul . . :",   4, 6, TRUE },  /* fails on nt4 */
119         { "c:nul0",        0, 0 },
120         { "c:prn:aaa",     4, 6, TRUE },  /* fails on win9x */
121         { "c:PRN:.txt",    4, 6 },
122         { "c:aux:.txt...", 4, 6 },
123         { "c:prn:.txt:",   4, 6 },
124         { "c:nul:aaa",     4, 6, TRUE },  /* fails on win9x */
125         { "con:",          0, 6 },
126         { "lpt1:",         0, 8 },
127         { "c:com5:",       4, 8 },
128         { "CoM4:",         0, 8 },
129         { "lpt9:",         0, 8 },
130         { "c:\\lpt0.txt",  0, 0 },
131         { "c:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
132           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
133           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
134           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
135           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
136           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
137           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\nul.txt", 1000, 6 },
138         { NULL, 0 }
139     };
140
141     const struct test *test;
142     WCHAR buffer[2000];
143     ULONG ret;
144
145     for (test = tests; test->path; test++)
146     {
147         pRtlMultiByteToUnicodeN( buffer, sizeof(buffer), NULL, test->path, strlen(test->path)+1 );
148         ret = pRtlIsDosDeviceName_U( buffer );
149         ok( ret == MAKELONG( test->len, test->pos ) ||
150             (test->fails && broken( ret == 0 )),
151             "Wrong result (%d,%d)/(%d,%d) for %s\n",
152             HIWORD(ret), LOWORD(ret), test->pos, test->len, test->path );
153     }
154 }
155
156 static void test_RtlIsNameLegalDOS8Dot3(void)
157 {
158     struct test
159     {
160         const char *path;
161         BOOLEAN result;
162         BOOLEAN spaces;
163     };
164
165     static const struct test tests[] =
166     {
167         { "12345678",     TRUE,  FALSE },
168         { "123 5678",     TRUE,  TRUE  },
169         { "12345678.",    FALSE, 2 /*not set*/ },
170         { "1234 678.",    FALSE, 2 /*not set*/ },
171         { "12345678.a",   TRUE,  FALSE },
172         { "12345678.a ",  FALSE, 2 /*not set*/ },
173         { "12345678.a c", TRUE,  TRUE  },
174         { " 2345678.a ",  FALSE, 2 /*not set*/ },
175         { "1 345678.abc", TRUE,  TRUE },
176         { "1      8.a c", TRUE,  TRUE },
177         { "1 3 5 7 .abc", FALSE, 2 /*not set*/ },
178         { "12345678.  c", TRUE,  TRUE },
179         { "123456789.a",  FALSE, 2 /*not set*/ },
180         { "12345.abcd",   FALSE, 2 /*not set*/ },
181         { "12345.ab d",   FALSE, 2 /*not set*/ },
182         { ".abc",         FALSE, 2 /*not set*/ },
183         { "12.abc.d",     FALSE, 2 /*not set*/ },
184         { ".",            TRUE,  FALSE },
185         { "..",           TRUE,  FALSE },
186         { "...",          FALSE, 2 /*not set*/ },
187         { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", FALSE, 2 /*not set*/ },
188         { NULL, 0 }
189     };
190
191     const struct test *test;
192     UNICODE_STRING ustr;
193     OEM_STRING oem, oem_ret;
194     WCHAR buffer[200];
195     char buff2[12];
196     BOOLEAN ret, spaces;
197
198     ustr.MaximumLength = sizeof(buffer);
199     ustr.Buffer = buffer;
200     for (test = tests; test->path; test++)
201     {
202         char path[100];
203         strcpy(path, test->path);
204         oem.Buffer = path;
205         oem.Length = strlen(test->path);
206         oem.MaximumLength = oem.Length + 1;
207         pRtlOemStringToUnicodeString( &ustr, &oem, FALSE );
208         spaces = 2;
209         oem_ret.Length = oem_ret.MaximumLength = sizeof(buff2);
210         oem_ret.Buffer = buff2;
211         ret = pRtlIsNameLegalDOS8Dot3( &ustr, &oem_ret, &spaces );
212         ok( ret == test->result, "Wrong result %d/%d for '%s'\n", ret, test->result, test->path );
213         ok( spaces == test->spaces, "Wrong spaces value %d/%d for '%s'\n", spaces, test->spaces, test->path );
214         if (strlen(test->path) <= 12)
215         {
216             char str[13];
217             int i;
218             strcpy( str, test->path );
219             for (i = 0; str[i]; i++) str[i] = toupper(str[i]);
220             ok( oem_ret.Length == strlen(test->path), "Wrong length %d/%d for '%s'\n",
221                 oem_ret.Length, lstrlenA(test->path), test->path );
222             ok( !memcmp( oem_ret.Buffer, str, oem_ret.Length ),
223                 "Wrong string '%.*s'/'%s'\n", oem_ret.Length, oem_ret.Buffer, str );
224         }
225     }
226 }
227 static void test_RtlGetFullPathName_U(void)
228 {
229     static const WCHAR emptyW[] = {0};
230     static const WCHAR deadbeefW[] = {'d','e','a','d','b','e','e','f',0};
231
232     struct test
233     {
234         const char *path;
235         const char *rname;
236         const char *rfile;
237         const char *alt_rname;
238         const char *alt_rfile;
239     };
240
241     static const struct test tests[] =
242         {
243             { "c:/test",                     "c:\\test",         "test"},
244             { "c:/test     ",                "c:\\test",         "test"},
245             { "c:/test.",                    "c:\\test",         "test"},
246             { "c:/test  ....   ..   ",       "c:\\test",         "test"},
247             { "c:/test/  ....   ..   ",      "c:\\test\\",       NULL},
248             { "c:/test/..",                  "c:\\",             NULL},
249             { "c:/test/.. ",                 "c:\\test\\",       NULL},
250             { "c:/TEST",                     "c:\\test",         "test"},
251             { "c:/test/file",                "c:\\test\\file",   "file"},
252             { "c:/test./file",               "c:\\test\\file",   "file"},
253             { "c:/test.. /file",             "c:\\test.. \\file","file"},
254             { "c:/test/././file",            "c:\\test\\file",   "file"},
255             { "c:/test\\.\\.\\file",         "c:\\test\\file",   "file"},
256             { "c:/test/\\.\\.\\file",        "c:\\test\\file",   "file"},
257             { "c:/test\\\\.\\.\\file",       "c:\\test\\file",   "file"},
258             { "c:/test\\test1\\..\\.\\file", "c:\\test\\file",   "file"},
259             { "c:///test\\.\\.\\file//",     "c:\\test\\file\\", NULL,
260                                              "c:\\test\\file",   "file"},  /* nt4 */
261             { "c:///test\\..\\file\\..\\//", "c:\\",             NULL},
262             { "c:/test../file",              "c:\\test.\\file",  "file",
263                                              "c:\\test..\\file", "file"},  /* vista */
264             { NULL, NULL, NULL}
265         };
266
267     const struct test *test;
268     WCHAR pathbufW[2*MAX_PATH], rbufferW[MAX_PATH];
269     CHAR  rbufferA[MAX_PATH], rfileA[MAX_PATH];
270     ULONG ret;
271     WCHAR *file_part;
272     DWORD reslen;
273     UINT len;
274
275     file_part = (WCHAR *)0xdeadbeef;
276     lstrcpyW(rbufferW, deadbeefW);
277     ret = pRtlGetFullPathName_U(NULL, MAX_PATH, rbufferW, &file_part);
278     ok(!ret, "Expected RtlGetFullPathName_U to return 0, got %u\n", ret);
279     ok(!lstrcmpW(rbufferW, deadbeefW),
280        "Expected the output buffer to be untouched, got %s\n", wine_dbgstr_w(rbufferW));
281     ok(file_part == (WCHAR *)0xdeadbeef ||
282        file_part == NULL, /* Win7 */
283        "Expected file part pointer to be untouched, got %p\n", file_part);
284
285     file_part = (WCHAR *)0xdeadbeef;
286     lstrcpyW(rbufferW, deadbeefW);
287     ret = pRtlGetFullPathName_U(emptyW, MAX_PATH, rbufferW, &file_part);
288     ok(!ret, "Expected RtlGetFullPathName_U to return 0, got %u\n", ret);
289     ok(!lstrcmpW(rbufferW, deadbeefW),
290        "Expected the output buffer to be untouched, got %s\n", wine_dbgstr_w(rbufferW));
291     ok(file_part == (WCHAR *)0xdeadbeef ||
292        file_part == NULL, /* Win7 */
293        "Expected file part pointer to be untouched, got %p\n", file_part);
294
295     for (test = tests; test->path; test++)
296     {
297         len= strlen(test->rname) * sizeof(WCHAR);
298         pRtlMultiByteToUnicodeN(pathbufW , sizeof(pathbufW), NULL, test->path, strlen(test->path)+1 );
299         ret = pRtlGetFullPathName_U( pathbufW,MAX_PATH, rbufferW, &file_part);
300         ok( ret == len || (test->alt_rname && ret == strlen(test->alt_rname)*sizeof(WCHAR)),
301             "Wrong result %d/%d for \"%s\"\n", ret, len, test->path );
302         ok(pRtlUnicodeToMultiByteN(rbufferA,MAX_PATH,&reslen,rbufferW,(lstrlenW(rbufferW) + 1) * sizeof(WCHAR)) == STATUS_SUCCESS,
303            "RtlUnicodeToMultiByteN failed\n");
304         ok(!lstrcmpiA(rbufferA,test->rname) || (test->alt_rname && !lstrcmpiA(rbufferA,test->alt_rname)),
305            "Got \"%s\" expected \"%s\"\n",rbufferA,test->rname);
306         if (file_part)
307         {
308             ok(pRtlUnicodeToMultiByteN(rfileA,MAX_PATH,&reslen,file_part,(lstrlenW(file_part) + 1) * sizeof(WCHAR)) == STATUS_SUCCESS,
309                "RtlUnicodeToMultiByteN failed\n");
310             ok((test->rfile && !lstrcmpiA(rfileA,test->rfile)) ||
311                (test->alt_rfile && !lstrcmpiA(rfileA,test->alt_rfile)),
312                "Got \"%s\" expected \"%s\"\n",rfileA,test->rfile);
313         }
314         else
315         {
316             ok( !test->rfile, "Got NULL expected \"%s\"\n", test->rfile );
317         }
318     }
319 }
320
321 START_TEST(path)
322 {
323     HMODULE mod = GetModuleHandleA("ntdll.dll");
324     if (!mod)
325     {
326         win_skip("Not running on NT, skipping tests\n");
327         return;
328     }
329
330     pRtlMultiByteToUnicodeN = (void *)GetProcAddress(mod,"RtlMultiByteToUnicodeN");
331     pRtlUnicodeToMultiByteN = (void *)GetProcAddress(mod,"RtlUnicodeToMultiByteN");
332     pRtlDetermineDosPathNameType_U = (void *)GetProcAddress(mod,"RtlDetermineDosPathNameType_U");
333     pRtlIsDosDeviceName_U = (void *)GetProcAddress(mod,"RtlIsDosDeviceName_U");
334     pRtlOemStringToUnicodeString = (void *)GetProcAddress(mod,"RtlOemStringToUnicodeString");
335     pRtlIsNameLegalDOS8Dot3 = (void *)GetProcAddress(mod,"RtlIsNameLegalDOS8Dot3");
336     pRtlGetFullPathName_U = (void *)GetProcAddress(mod,"RtlGetFullPathName_U");
337     if (pRtlDetermineDosPathNameType_U)
338         test_RtlDetermineDosPathNameType();
339     if (pRtlIsDosDeviceName_U)
340         test_RtlIsDosDeviceName();
341     if (pRtlIsNameLegalDOS8Dot3)
342         test_RtlIsNameLegalDOS8Dot3();
343     if (pRtlGetFullPathName_U && pRtlMultiByteToUnicodeN)
344         test_RtlGetFullPathName_U();
345 }