[NTDLL_APITEST]
[reactos.git] / rostests / apitests / ntdll / RtlDetermineDosPathNameType.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for RtlDetermineDosPathNameType_U/RtlDetermineDosPathNameType_Ustr
5 * PROGRAMMER: Thomas Faber <thfabba@gmx.de>
6 */
7
8 #define WIN32_NO_STATUS
9 #define UNICODE
10 #include <wine/test.h>
11 #include <pseh/pseh2.h>
12 #include <ndk/mmfuncs.h>
13 #include <ndk/rtlfuncs.h>
14
15 /*
16 ULONG
17 NTAPI
18 RtlDetermineDosPathNameType_U(
19 IN PCWSTR Path
20 );
21
22 ULONG
23 NTAPI
24 RtlDetermineDosPathNameType_Ustr(
25 IN PCUNICODE_STRING Path
26 );
27 */
28
29 static
30 ULONG
31 (NTAPI
32 *RtlDetermineDosPathNameType_Ustr)(
33 IN PCUNICODE_STRING Path
34 )
35 //= (PVOID)0x7c830669;
36 ;
37
38 static
39 PVOID
40 AllocateGuarded(
41 SIZE_T SizeRequested)
42 {
43 NTSTATUS Status;
44 SIZE_T Size = PAGE_ROUND_UP(SizeRequested + PAGE_SIZE);
45 PVOID VirtualMemory = NULL;
46 PCHAR StartOfBuffer;
47
48 Status = NtAllocateVirtualMemory(NtCurrentProcess(), &VirtualMemory, 0, &Size, MEM_RESERVE, PAGE_NOACCESS);
49
50 if (!NT_SUCCESS(Status))
51 return NULL;
52
53 Size -= PAGE_SIZE;
54 if (Size)
55 {
56 Status = NtAllocateVirtualMemory(NtCurrentProcess(), &VirtualMemory, 0, &Size, MEM_COMMIT, PAGE_READWRITE);
57 if (!NT_SUCCESS(Status))
58 {
59 Size = 0;
60 Status = NtFreeVirtualMemory(NtCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
61 ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
62 return NULL;
63 }
64 }
65
66 StartOfBuffer = VirtualMemory;
67 StartOfBuffer += Size - SizeRequested;
68
69 return StartOfBuffer;
70 }
71
72 static
73 VOID
74 MakeReadOnly(
75 PVOID Pointer,
76 SIZE_T SizeRequested)
77 {
78 NTSTATUS Status;
79 SIZE_T Size = PAGE_ROUND_UP(SizeRequested);
80 PVOID VirtualMemory = (PVOID)PAGE_ROUND_DOWN((SIZE_T)Pointer);
81
82 if (Size)
83 {
84 Status = NtAllocateVirtualMemory(NtCurrentProcess(), &VirtualMemory, 0, &Size, MEM_COMMIT, PAGE_READWRITE);
85 if (!NT_SUCCESS(Status))
86 {
87 Size = 0;
88 Status = NtFreeVirtualMemory(NtCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
89 ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
90 }
91 }
92 }
93
94 static
95 VOID
96 FreeGuarded(
97 PVOID Pointer)
98 {
99 NTSTATUS Status;
100 PVOID VirtualMemory = (PVOID)PAGE_ROUND_DOWN((SIZE_T)Pointer);
101 SIZE_T Size = 0;
102
103 Status = NtFreeVirtualMemory(NtCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
104 ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
105 }
106
107 #define StartSeh() ExceptionStatus = STATUS_SUCCESS; _SEH2_TRY {
108 #define EndSeh(ExpectedStatus) } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { ExceptionStatus = _SEH2_GetExceptionCode(); } _SEH2_END; ok(ExceptionStatus == ExpectedStatus, "Exception %lx, expected %lx\n", ExceptionStatus, ExpectedStatus)
109
110 START_TEST(RtlDetermineDosPathNameType)
111 {
112 NTSTATUS ExceptionStatus;
113 RTL_PATH_TYPE PathType;
114 struct
115 {
116 PCWSTR FileName;
117 RTL_PATH_TYPE PathType;
118 } Tests[] =
119 {
120 { L"", RtlPathTypeRelative },
121 { L"xyz", RtlPathTypeRelative },
122 { L"CON", RtlPathTypeRelative },
123 { L"NUL", RtlPathTypeRelative },
124 { L":", RtlPathTypeRelative },
125 { L"::", RtlPathTypeDriveRelative },
126 { L":::", RtlPathTypeDriveRelative },
127 { L"::::", RtlPathTypeDriveRelative },
128 { L"::\\", RtlPathTypeDriveAbsolute },
129 { L"\\", RtlPathTypeRooted },
130 { L"\\:", RtlPathTypeRooted },
131 { L"\\C:", RtlPathTypeRooted },
132 { L"\\C:\\", RtlPathTypeRooted },
133 { L"/", RtlPathTypeRooted },
134 { L"/:", RtlPathTypeRooted },
135 { L"/C:", RtlPathTypeRooted },
136 { L"/C:/", RtlPathTypeRooted },
137 { L"C", RtlPathTypeRelative },
138 { L"C:", RtlPathTypeDriveRelative },
139 { L"C:a", RtlPathTypeDriveRelative },
140 { L"C:a\\", RtlPathTypeDriveRelative },
141 { L"C:\\", RtlPathTypeDriveAbsolute },
142 { L"C:/", RtlPathTypeDriveAbsolute },
143 { L"C:\\a", RtlPathTypeDriveAbsolute },
144 { L"C:/a", RtlPathTypeDriveAbsolute },
145 { L"C:\\\\", RtlPathTypeDriveAbsolute },
146 { L"\\\\", RtlPathTypeUncAbsolute },
147 { L"\\\\\\", RtlPathTypeUncAbsolute },
148 { L"\\\\;", RtlPathTypeUncAbsolute },
149 { L"\\??\\", RtlPathTypeRooted },
150 { L"\\??\\UNC", RtlPathTypeRooted },
151 { L"\\??\\UNC\\", RtlPathTypeRooted },
152 { L"\\?", RtlPathTypeRooted },
153 { L"\\?\\", RtlPathTypeRooted },
154 { L"\\?\\UNC", RtlPathTypeRooted },
155 { L"\\?\\UNC\\", RtlPathTypeRooted },
156 { L"\\\\?\\UNC\\", RtlPathTypeLocalDevice },
157 { L"\\\\?", RtlPathTypeRootLocalDevice },
158 { L"\\\\??", RtlPathTypeUncAbsolute },
159 { L"\\\\??\\", RtlPathTypeUncAbsolute },
160 { L"\\\\??\\C:\\", RtlPathTypeUncAbsolute },
161 { L"\\\\.", RtlPathTypeRootLocalDevice },
162 { L"\\\\.\\", RtlPathTypeLocalDevice },
163 { L"\\\\.\\C:\\", RtlPathTypeLocalDevice },
164 { L"\\/", RtlPathTypeUncAbsolute },
165 { L"/\\", RtlPathTypeUncAbsolute },
166 { L"//", RtlPathTypeUncAbsolute },
167 { L"///", RtlPathTypeUncAbsolute },
168 { L"//;", RtlPathTypeUncAbsolute },
169 { L"//?", RtlPathTypeRootLocalDevice },
170 { L"/\\?", RtlPathTypeRootLocalDevice },
171 { L"\\/?", RtlPathTypeRootLocalDevice },
172 { L"//??", RtlPathTypeUncAbsolute },
173 { L"//?" L"?/", RtlPathTypeUncAbsolute },
174 { L"//?" L"?/C:/", RtlPathTypeUncAbsolute },
175 { L"//.", RtlPathTypeRootLocalDevice },
176 { L"\\/.", RtlPathTypeRootLocalDevice },
177 { L"/\\.", RtlPathTypeRootLocalDevice },
178 { L"//./", RtlPathTypeLocalDevice },
179 { L"//./C:/", RtlPathTypeLocalDevice },
180 { L"%SystemRoot%", RtlPathTypeRelative },
181 };
182 ULONG i;
183 PWSTR FileName;
184 USHORT Length;
185
186 if (!RtlDetermineDosPathNameType_Ustr)
187 {
188 RtlDetermineDosPathNameType_Ustr = (PVOID)GetProcAddress(GetModuleHandle(L"ntdll"), "RtlDetermineDosPathNameType_Ustr");
189 if (!RtlDetermineDosPathNameType_Ustr)
190 skip("RtlDetermineDosPathNameType_Ustr unavailable\n");
191 }
192
193 StartSeh() RtlDetermineDosPathNameType_U(NULL); EndSeh(STATUS_ACCESS_VIOLATION);
194
195 if (RtlDetermineDosPathNameType_Ustr)
196 {
197 UNICODE_STRING PathString;
198 StartSeh() RtlDetermineDosPathNameType_Ustr(NULL); EndSeh(STATUS_ACCESS_VIOLATION);
199
200 RtlInitEmptyUnicodeString(&PathString, NULL, MAXUSHORT);
201 PathType = RtlDetermineDosPathNameType_Ustr(&PathString);
202 ok(PathType == RtlPathTypeRelative, "PathType = %d\n", PathType);
203 }
204
205 for (i = 0; i < sizeof(Tests) / sizeof(Tests[0]); i++)
206 {
207 Length = (USHORT)wcslen(Tests[i].FileName) * sizeof(WCHAR);
208 FileName = AllocateGuarded(Length + sizeof(UNICODE_NULL));
209 RtlCopyMemory(FileName, Tests[i].FileName, Length + sizeof(UNICODE_NULL));
210 MakeReadOnly(FileName, Length + sizeof(UNICODE_NULL));
211 StartSeh()
212 PathType = RtlDetermineDosPathNameType_U(FileName);
213 ok(PathType == Tests[i].PathType, "PathType is %d, expected %d for '%S'\n", PathType, Tests[i].PathType, Tests[i].FileName);
214 EndSeh(STATUS_SUCCESS);
215 FreeGuarded(FileName);
216
217 if (RtlDetermineDosPathNameType_Ustr)
218 {
219 UNICODE_STRING PathString;
220
221 FileName = AllocateGuarded(Length);
222 RtlCopyMemory(FileName, Tests[i].FileName, Length);
223 MakeReadOnly(FileName, Length);
224 PathString.Buffer = FileName;
225 PathString.Length = Length;
226 PathString.MaximumLength = MAXUSHORT;
227 StartSeh()
228 PathType = RtlDetermineDosPathNameType_Ustr(&PathString);
229 ok(PathType == Tests[i].PathType, "PathType is %d, expected %d for '%S'\n", PathType, Tests[i].PathType, Tests[i].FileName);
230 EndSeh(STATUS_SUCCESS);
231 FreeGuarded(FileName);
232 }
233 }
234 }