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