Implement handle tables and add tests for them.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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         int 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     };
95
96     static const struct test tests[] =
97     {
98         { "\\\\.\\CON",    8, 6 },
99         { "\\\\.\\con",    8, 6 },
100         { "\\\\.\\CON2",   0, 0 },
101         { "",              0, 0 },
102         { "\\\\foo\\nul",  0, 0 },
103         { "c:\\nul:",      6, 6 },
104         { "c:\\nul::",     0, 0 },
105         { "c:prn     ",    4, 6 },
106         { "c:prn.......",  4, 6 },
107         { "c:prn... ...",  4, 6 },
108         { "c:NUL  ....  ", 0, 0 },
109         { "c: . . .",      0, 0 },
110         { "c:",            0, 0 },
111         { " . . . :",      0, 0 },
112         { ":",             0, 0 },
113         { "c:nul. . . :",  4, 6 },
114         { "c:nul . . :",   0, 0 },
115         { "c:nul0",        0, 0 },
116         { "c:prn:aaa",     0, 0 },
117         { "c:PRN:.txt",    4, 6 },
118         { "c:aux:.txt...", 4, 6 },
119         { "c:prn:.txt:",   4, 6 },
120         { "c:nul:aaa",     0, 0 },
121         { "con:",          0, 6 },
122         { "lpt1:",         0, 8 },
123         { "c:com5:",       4, 8 },
124         { "CoM4:",         0, 8 },
125         { "lpt9:",         0, 8 },
126         { "c:\\lpt0.txt",  0, 0 },
127         { "c:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
128           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
129           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
130           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
131           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
132           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
133           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\nul.txt", 1000, 6 },
134         { NULL, 0 }
135     };
136
137     const struct test *test;
138     WCHAR buffer[2000];
139     ULONG ret;
140
141     for (test = tests; test->path; test++)
142     {
143         pRtlMultiByteToUnicodeN( buffer, sizeof(buffer), NULL, test->path, strlen(test->path)+1 );
144         ret = pRtlIsDosDeviceName_U( buffer );
145         ok( ret == MAKELONG( test->len, test->pos ),
146             "Wrong result (%d,%d)/(%d,%d) for %s\n",
147             HIWORD(ret), LOWORD(ret), test->pos, test->len, test->path );
148     }
149 }
150
151 static void test_RtlIsNameLegalDOS8Dot3(void)
152 {
153     struct test
154     {
155         const char *path;
156         BOOLEAN result;
157         BOOLEAN spaces;
158     };
159
160     static const struct test tests[] =
161     {
162         { "12345678",     TRUE,  FALSE },
163         { "123 5678",     TRUE,  TRUE  },
164         { "12345678.",    FALSE, 2 /*not set*/ },
165         { "1234 678.",    FALSE, 2 /*not set*/ },
166         { "12345678.a",   TRUE,  FALSE },
167         { "12345678.a ",  FALSE, 2 /*not set*/ },
168         { "12345678.a c", TRUE,  TRUE  },
169         { " 2345678.a ",  FALSE, 2 /*not set*/ },
170         { "1 345678.abc", TRUE,  TRUE },
171         { "1      8.a c", TRUE,  TRUE },
172         { "1 3 5 7 .abc", FALSE, 2 /*not set*/ },
173         { "12345678.  c", TRUE,  TRUE },
174         { "123456789.a",  FALSE, 2 /*not set*/ },
175         { "12345.abcd",   FALSE, 2 /*not set*/ },
176         { "12345.ab d",   FALSE, 2 /*not set*/ },
177         { ".abc",         FALSE, 2 /*not set*/ },
178         { "12.abc.d",     FALSE, 2 /*not set*/ },
179         { ".",            TRUE,  FALSE },
180         { "..",           TRUE,  FALSE },
181         { "...",          FALSE, 2 /*not set*/ },
182         { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", FALSE, 2 /*not set*/ },
183         { NULL, 0 }
184     };
185
186     const struct test *test;
187     UNICODE_STRING ustr;
188     OEM_STRING oem, oem_ret;
189     WCHAR buffer[200];
190     char buff2[12];
191     BOOLEAN ret, spaces;
192
193     ustr.MaximumLength = sizeof(buffer);
194     ustr.Buffer = buffer;
195     for (test = tests; test->path; test++)
196     {
197         char path[100];
198         strcpy(path, test->path);
199         oem.Buffer = path;
200         oem.Length = strlen(test->path);
201         oem.MaximumLength = oem.Length + 1;
202         pRtlOemStringToUnicodeString( &ustr, &oem, FALSE );
203         spaces = 2;
204         oem_ret.Length = oem_ret.MaximumLength = sizeof(buff2);
205         oem_ret.Buffer = buff2;
206         ret = pRtlIsNameLegalDOS8Dot3( &ustr, &oem_ret, &spaces );
207         ok( ret == test->result, "Wrong result %d/%d for '%s'\n", ret, test->result, test->path );
208         ok( spaces == test->spaces, "Wrong spaces value %d/%d for '%s'\n", spaces, test->spaces, test->path );
209         if (strlen(test->path) <= 12)
210         {
211             char str[13];
212             int i;
213             strcpy( str, test->path );
214             for (i = 0; str[i]; i++) str[i] = toupper(str[i]);
215             ok( oem_ret.Length == strlen(test->path), "Wrong length %d/%d for '%s'\n",
216                 oem_ret.Length, strlen(test->path), test->path );
217             ok( !memcmp( oem_ret.Buffer, str, oem_ret.Length ),
218                 "Wrong string '%.*s'/'%s'\n", oem_ret.Length, oem_ret.Buffer, str );
219         }
220     }
221 }
222 static void test_RtlGetFullPathName_U()
223 {
224     struct test
225     {
226         const char *path;
227         const char *rname;
228         const char *rfile;
229     };
230
231     static const struct test tests[] =
232         {
233             { "c:/test",                     "c:\\test",         "test"},
234             { "c:/test     ",                "c:\\test",         "test"},
235             { "c:/test.",                    "c:\\test",         "test"},
236             { "c:/test  ....   ..   ",       "c:\\test",         "test"},
237             { "c:/test/  ....   ..   ",      "c:\\test\\",       NULL},
238             { "c:/test/..",                  "c:\\",             NULL},
239             { "c:/test/.. ",                 "c:\\test\\",       NULL},
240             { "c:/TEST",                     "c:\\test",         "test"},
241             { "c:/test/file",                "c:\\test\\file",   "file"},
242             { "c:/test/././file",            "c:\\test\\file",   "file"},
243             { "c:/test\\.\\.\\file",         "c:\\test\\file",   "file"},
244             { "c:/test/\\.\\.\\file",        "c:\\test\\file",   "file"},
245             { "c:/test\\\\.\\.\\file",       "c:\\test\\file",   "file"},
246             { "c:/test\\test1\\..\\.\\file", "c:\\test\\file",   "file"},
247             { "c:///test\\.\\.\\file//",     "c:\\test\\file\\", NULL},
248             { "c:///test\\..\\file\\..\\//", "c:\\",             NULL},
249             { NULL, NULL, NULL}
250         };
251
252     const struct test *test;
253     WCHAR pathbufW[2*MAX_PATH], rbufferW[MAX_PATH];
254     CHAR  rbufferA[MAX_PATH], rfileA[MAX_PATH];
255     ULONG ret;
256     WCHAR *file_part;
257     DWORD reslen;
258     int len;
259
260     for (test = tests; test->path; test++)
261     {
262         len= strlen(test->rname) * sizeof(WCHAR);
263         pRtlMultiByteToUnicodeN(pathbufW , sizeof(pathbufW), NULL, test->path, strlen(test->path)+1 );
264         ret = pRtlGetFullPathName_U( pathbufW,MAX_PATH, rbufferW, &file_part);
265         ok( ret == len, "Wrong result %ld/%d for \"%s\"\n", ret, len, test->path );
266         ok(pRtlUnicodeToMultiByteN(rbufferA,MAX_PATH,&reslen,rbufferW,MAX_PATH) == STATUS_SUCCESS,
267            "RtlUnicodeToMultiByteN failed\n");
268         ok(lstrcmpiA(rbufferA,test->rname) == 0, "Got \"%s\" expected \"%s\"\n",rbufferA,test->rname);
269         if (file_part)
270         {
271             ok(pRtlUnicodeToMultiByteN(rfileA,MAX_PATH,&reslen,file_part,MAX_PATH) == STATUS_SUCCESS,
272                "RtlUnicodeToMultiByteN failed\n");
273             ok(test->rfile && !lstrcmpiA(rfileA,test->rfile), "Got \"%s\" expected \"%s\"\n",rfileA,test->rfile);
274         }
275         else
276         {
277             ok( !test->rfile, "Got NULL expected \"%s\"\n", test->rfile );
278         }
279     }
280
281 }
282
283 START_TEST(path)
284 {
285     HMODULE mod = GetModuleHandleA("ntdll.dll");
286     pRtlMultiByteToUnicodeN = (void *)GetProcAddress(mod,"RtlMultiByteToUnicodeN");
287     pRtlUnicodeToMultiByteN = (void *)GetProcAddress(mod,"RtlUnicodeToMultiByteN");
288     pRtlDetermineDosPathNameType_U = (void *)GetProcAddress(mod,"RtlDetermineDosPathNameType_U");
289     pRtlIsDosDeviceName_U = (void *)GetProcAddress(mod,"RtlIsDosDeviceName_U");
290     pRtlOemStringToUnicodeString = (void *)GetProcAddress(mod,"RtlOemStringToUnicodeString");
291     pRtlIsNameLegalDOS8Dot3 = (void *)GetProcAddress(mod,"RtlIsNameLegalDOS8Dot3");
292     pRtlGetFullPathName_U = (void *)GetProcAddress(mod,"RtlGetFullPathName_U");
293     if (pRtlDetermineDosPathNameType_U)
294         test_RtlDetermineDosPathNameType();
295     if (pRtlIsDosDeviceName_U)
296         test_RtlIsDosDeviceName();
297     if (pRtlIsNameLegalDOS8Dot3)
298         test_RtlIsNameLegalDOS8Dot3();
299     if (pRtlGetFullPathName_U && pRtlMultiByteToUnicodeN)
300         test_RtlGetFullPathName_U();
301 }