ntdll/tests: Tests for RtlIpv6StringToAddressEx.
[wine.git] / dlls / ntdll / tests / rtl.c
blobe13c139202e5c43f75502a823467593516125105
1 /* Unit test suite for Rtl* API functions
3 * Copyright 2003 Thomas Mertes
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * NOTES
20 * We use function pointers here as there is no import library for NTDLL on
21 * windows.
24 #include <stdlib.h>
26 #include "ntdll_test.h"
27 #include "in6addr.h"
28 #include "inaddr.h"
30 #ifndef __WINE_WINTERNL_H
32 typedef struct _RTL_HANDLE
34 struct _RTL_HANDLE * Next;
35 } RTL_HANDLE;
37 typedef struct _RTL_HANDLE_TABLE
39 ULONG MaxHandleCount;
40 ULONG HandleSize;
41 ULONG Unused[2];
42 PVOID NextFree;
43 PVOID FirstHandle;
44 PVOID ReservedMemory;
45 PVOID MaxHandle;
46 } RTL_HANDLE_TABLE;
48 #endif
50 /* avoid #include <winsock2.h> */
51 #undef htons
52 #ifdef WORDS_BIGENDIAN
53 #define htons(s) ((USHORT)(s))
54 #else /* WORDS_BIGENDIAN */
55 static inline USHORT __my_ushort_swap(USHORT s)
57 return (s >> 8) | (s << 8);
59 #define htons(s) __my_ushort_swap(s)
60 #endif /* WORDS_BIGENDIAN */
64 /* Function ptrs for ntdll calls */
65 static HMODULE hntdll = 0;
66 static SIZE_T (WINAPI *pRtlCompareMemory)(LPCVOID,LPCVOID,SIZE_T);
67 static SIZE_T (WINAPI *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);
68 static NTSTATUS (WINAPI *pRtlDeleteTimer)(HANDLE, HANDLE, HANDLE);
69 static VOID (WINAPI *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);
70 static VOID (WINAPI *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);
71 static VOID (WINAPI *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);
72 static VOID (WINAPI *pRtlZeroMemory)(LPVOID,SIZE_T);
73 static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);
74 static ULONG (WINAPI *pRtlUniform)(PULONG);
75 static ULONG (WINAPI *pRtlRandom)(PULONG);
76 static BOOLEAN (WINAPI *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
77 static BOOLEAN (WINAPI *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
78 static DWORD (WINAPI *pRtlComputeCrc32)(DWORD,const BYTE*,INT);
79 static void (WINAPI * pRtlInitializeHandleTable)(ULONG, ULONG, RTL_HANDLE_TABLE *);
80 static BOOLEAN (WINAPI * pRtlIsValidIndexHandle)(const RTL_HANDLE_TABLE *, ULONG, RTL_HANDLE **);
81 static NTSTATUS (WINAPI * pRtlDestroyHandleTable)(RTL_HANDLE_TABLE *);
82 static RTL_HANDLE * (WINAPI * pRtlAllocateHandle)(RTL_HANDLE_TABLE *, ULONG *);
83 static BOOLEAN (WINAPI * pRtlFreeHandle)(RTL_HANDLE_TABLE *, RTL_HANDLE *);
84 static NTSTATUS (WINAPI *pRtlAllocateAndInitializeSid)(PSID_IDENTIFIER_AUTHORITY,BYTE,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,PSID*);
85 static NTSTATUS (WINAPI *pRtlFreeSid)(PSID);
86 static DWORD (WINAPI *pRtlGetThreadErrorMode)(void);
87 static NTSTATUS (WINAPI *pRtlSetThreadErrorMode)(DWORD, LPDWORD);
88 static IMAGE_BASE_RELOCATION *(WINAPI *pLdrProcessRelocationBlock)(void*,UINT,USHORT*,INT_PTR);
89 static CHAR * (WINAPI *pRtlIpv4AddressToStringA)(const IN_ADDR *, LPSTR);
90 static NTSTATUS (WINAPI *pRtlIpv4AddressToStringExA)(const IN_ADDR *, USHORT, LPSTR, PULONG);
91 static NTSTATUS (WINAPI *pRtlIpv4StringToAddressA)(PCSTR, BOOLEAN, PCSTR *, IN_ADDR *);
92 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressA)(PCSTR, PCSTR *, struct in6_addr *);
93 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressW)(PCWSTR, PCWSTR *, struct in6_addr *);
94 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressExA)(PCSTR, struct in6_addr *, PULONG, PUSHORT);
95 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressExW)(PCWSTR, struct in6_addr *, PULONG, PUSHORT);
96 static NTSTATUS (WINAPI *pLdrAddRefDll)(ULONG, HMODULE);
97 static NTSTATUS (WINAPI *pLdrLockLoaderLock)(ULONG, ULONG*, ULONG_PTR*);
98 static NTSTATUS (WINAPI *pLdrUnlockLoaderLock)(ULONG, ULONG_PTR);
99 static NTSTATUS (WINAPI *pRtlMultiByteToUnicodeN)(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD);
100 static NTSTATUS (WINAPI *pRtlGetCompressionWorkSpaceSize)(USHORT, PULONG, PULONG);
101 static NTSTATUS (WINAPI *pRtlDecompressBuffer)(USHORT, PUCHAR, ULONG, const UCHAR*, ULONG, PULONG);
102 static NTSTATUS (WINAPI *pRtlDecompressFragment)(USHORT, PUCHAR, ULONG, const UCHAR*, ULONG, ULONG, PULONG, PVOID);
103 static NTSTATUS (WINAPI *pRtlCompressBuffer)(USHORT, const UCHAR*, ULONG, PUCHAR, ULONG, ULONG, PULONG, PVOID);
104 static BOOL (WINAPI *pRtlIsCriticalSectionLocked)(CRITICAL_SECTION *);
105 static BOOL (WINAPI *pRtlIsCriticalSectionLockedByThread)(CRITICAL_SECTION *);
106 static NTSTATUS (WINAPI *pRtlInitializeCriticalSectionEx)(CRITICAL_SECTION *, ULONG, ULONG);
107 static NTSTATUS (WINAPI *pLdrEnumerateLoadedModules)(void *, void *, void *);
108 static NTSTATUS (WINAPI *pRtlMakeSelfRelativeSD)(PSECURITY_DESCRIPTOR,PSECURITY_DESCRIPTOR,LPDWORD);
109 static NTSTATUS (WINAPI *pRtlAbsoluteToSelfRelativeSD)(PSECURITY_DESCRIPTOR,PSECURITY_DESCRIPTOR,PULONG);
110 static NTSTATUS (WINAPI *pLdrRegisterDllNotification)(ULONG, PLDR_DLL_NOTIFICATION_FUNCTION, void *, void **);
111 static NTSTATUS (WINAPI *pLdrUnregisterDllNotification)(void *);
113 static HMODULE hkernel32 = 0;
114 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
117 #define LEN 16
118 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
119 static WCHAR ws2_32dllW[] = {'w','s','2','_','3','2','.','d','l','l',0};
120 static WCHAR nsidllW[] = {'n','s','i','.','d','l','l',0};
121 static WCHAR wintrustdllW[] = {'w','i','n','t','r','u','s','t','.','d','l','l',0};
122 static WCHAR crypt32dllW[] = {'c','r','y','p','t','3','2','.','d','l','l',0};
123 static ULONG src_aligned_block[4];
124 static ULONG dest_aligned_block[32];
125 static const char *src = (const char*)src_aligned_block;
126 static char* dest = (char*)dest_aligned_block;
127 const WCHAR *expected_dll = nsidllW;
129 static void InitFunctionPtrs(void)
131 hntdll = LoadLibraryA("ntdll.dll");
132 ok(hntdll != 0, "LoadLibrary failed\n");
133 if (hntdll) {
134 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
135 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
136 pRtlDeleteTimer = (void *)GetProcAddress(hntdll, "RtlDeleteTimer");
137 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
138 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
139 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
140 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
141 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
142 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
143 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
144 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
145 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
146 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
147 pRtlInitializeHandleTable = (void *)GetProcAddress(hntdll, "RtlInitializeHandleTable");
148 pRtlIsValidIndexHandle = (void *)GetProcAddress(hntdll, "RtlIsValidIndexHandle");
149 pRtlDestroyHandleTable = (void *)GetProcAddress(hntdll, "RtlDestroyHandleTable");
150 pRtlAllocateHandle = (void *)GetProcAddress(hntdll, "RtlAllocateHandle");
151 pRtlFreeHandle = (void *)GetProcAddress(hntdll, "RtlFreeHandle");
152 pRtlAllocateAndInitializeSid = (void *)GetProcAddress(hntdll, "RtlAllocateAndInitializeSid");
153 pRtlFreeSid = (void *)GetProcAddress(hntdll, "RtlFreeSid");
154 pRtlGetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlGetThreadErrorMode");
155 pRtlSetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlSetThreadErrorMode");
156 pLdrProcessRelocationBlock = (void *)GetProcAddress(hntdll, "LdrProcessRelocationBlock");
157 pRtlIpv4AddressToStringA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringA");
158 pRtlIpv4AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringExA");
159 pRtlIpv4StringToAddressA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressA");
160 pRtlIpv6StringToAddressA = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressA");
161 pRtlIpv6StringToAddressW = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressW");
162 pRtlIpv6StringToAddressExA = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressExA");
163 pRtlIpv6StringToAddressExW = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressExW");
164 pLdrAddRefDll = (void *)GetProcAddress(hntdll, "LdrAddRefDll");
165 pLdrLockLoaderLock = (void *)GetProcAddress(hntdll, "LdrLockLoaderLock");
166 pLdrUnlockLoaderLock = (void *)GetProcAddress(hntdll, "LdrUnlockLoaderLock");
167 pRtlMultiByteToUnicodeN = (void *)GetProcAddress(hntdll, "RtlMultiByteToUnicodeN");
168 pRtlGetCompressionWorkSpaceSize = (void *)GetProcAddress(hntdll, "RtlGetCompressionWorkSpaceSize");
169 pRtlDecompressBuffer = (void *)GetProcAddress(hntdll, "RtlDecompressBuffer");
170 pRtlDecompressFragment = (void *)GetProcAddress(hntdll, "RtlDecompressFragment");
171 pRtlCompressBuffer = (void *)GetProcAddress(hntdll, "RtlCompressBuffer");
172 pRtlIsCriticalSectionLocked = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLocked");
173 pRtlIsCriticalSectionLockedByThread = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLockedByThread");
174 pRtlInitializeCriticalSectionEx = (void *)GetProcAddress(hntdll, "RtlInitializeCriticalSectionEx");
175 pLdrEnumerateLoadedModules = (void *)GetProcAddress(hntdll, "LdrEnumerateLoadedModules");
176 pRtlMakeSelfRelativeSD = (void *)GetProcAddress(hntdll, "RtlMakeSelfRelativeSD");
177 pRtlAbsoluteToSelfRelativeSD = (void *)GetProcAddress(hntdll, "RtlAbsoluteToSelfRelativeSD");
178 pLdrRegisterDllNotification = (void *)GetProcAddress(hntdll, "LdrRegisterDllNotification");
179 pLdrUnregisterDllNotification = (void *)GetProcAddress(hntdll, "LdrUnregisterDllNotification");
181 hkernel32 = LoadLibraryA("kernel32.dll");
182 ok(hkernel32 != 0, "LoadLibrary failed\n");
183 if (hkernel32) {
184 pIsWow64Process = (void *)GetProcAddress(hkernel32, "IsWow64Process");
186 strcpy((char*)src_aligned_block, src_src);
187 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
190 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
191 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
193 static void test_RtlCompareMemory(void)
195 SIZE_T size;
197 if (!pRtlCompareMemory)
199 win_skip("RtlCompareMemory is not available\n");
200 return;
203 strcpy(dest, src);
205 COMP(src,src,0,0);
206 COMP(src,src,LEN,LEN);
207 dest[0] = 'x';
208 COMP(src,dest,LEN,0);
211 static void test_RtlCompareMemoryUlong(void)
213 ULONG a[10];
214 ULONG result;
216 if (!pRtlCompareMemoryUlong)
218 win_skip("RtlCompareMemoryUlong is not available\n");
219 return;
222 a[0]= 0x0123;
223 a[1]= 0x4567;
224 a[2]= 0x89ab;
225 a[3]= 0xcdef;
226 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
227 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %u, expected 0\n", a, result);
228 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
229 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
230 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
231 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
232 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
233 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
234 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
235 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
236 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
237 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 4\n", a, result);
238 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
239 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 4\n", a, result);
240 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
241 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %u, expected 0\n", a, result);
242 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
243 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %u, expected 0\n", a, result);
244 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
245 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %u, expected 0\n", a, result);
247 a[1]= 0x0123;
248 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
249 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
250 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
251 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
252 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
253 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
254 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
255 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
256 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
257 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 8\n", a, result);
258 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
259 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 8\n", a, result);
262 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
263 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
265 static void test_RtlMoveMemory(void)
267 if (!pRtlMoveMemory)
269 win_skip("RtlMoveMemory is not available\n");
270 return;
273 /* Length should be in bytes and not rounded. Use strcmp to ensure we
274 * didn't write past the end (it checks for the final NUL left by memset)
276 COPY(0); CMP("");
277 COPY(1); CMP("T");
278 COPY(2); CMP("Th");
279 COPY(3); CMP("Thi");
280 COPY(4); CMP("This");
281 COPY(5); CMP("This ");
282 COPY(6); CMP("This i");
283 COPY(7); CMP("This is");
284 COPY(8); CMP("This is ");
285 COPY(9); CMP("This is a");
287 /* Overlapping */
288 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
289 CMP("his is a test!!");
290 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
291 CMP("TThis is a test!");
294 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
296 static void test_RtlFillMemory(void)
298 if (!pRtlFillMemory)
300 win_skip("RtlFillMemory is not available\n");
301 return;
304 /* Length should be in bytes and not rounded. Use strcmp to ensure we
305 * didn't write past the end (the remainder of the string should match)
307 FILL(0); CMP("This is a test!");
308 FILL(1); CMP("xhis is a test!");
309 FILL(2); CMP("xxis is a test!");
310 FILL(3); CMP("xxxs is a test!");
311 FILL(4); CMP("xxxx is a test!");
312 FILL(5); CMP("xxxxxis a test!");
313 FILL(6); CMP("xxxxxxs a test!");
314 FILL(7); CMP("xxxxxxx a test!");
315 FILL(8); CMP("xxxxxxxxa test!");
316 FILL(9); CMP("xxxxxxxxx test!");
319 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
321 static void test_RtlFillMemoryUlong(void)
323 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
324 if (!pRtlFillMemoryUlong)
326 win_skip("RtlFillMemoryUlong is not available\n");
327 return;
330 /* Length should be in bytes and not rounded. Use strcmp to ensure we
331 * didn't write past the end (the remainder of the string should match)
333 LFILL(0); CMP("This is a test!");
334 LFILL(1); CMP("This is a test!");
335 LFILL(2); CMP("This is a test!");
336 LFILL(3); CMP("This is a test!");
337 LFILL(4); CMP("xxxx is a test!");
338 LFILL(5); CMP("xxxx is a test!");
339 LFILL(6); CMP("xxxx is a test!");
340 LFILL(7); CMP("xxxx is a test!");
341 LFILL(8); CMP("xxxxxxxxa test!");
342 LFILL(9); CMP("xxxxxxxxa test!");
345 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
346 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
348 static void test_RtlZeroMemory(void)
350 if (!pRtlZeroMemory)
352 win_skip("RtlZeroMemory is not available\n");
353 return;
356 /* Length should be in bytes and not rounded. */
357 ZERO(0); MCMP("This is a test!");
358 ZERO(1); MCMP("\0his is a test!");
359 ZERO(2); MCMP("\0\0is is a test!");
360 ZERO(3); MCMP("\0\0\0s is a test!");
361 ZERO(4); MCMP("\0\0\0\0 is a test!");
362 ZERO(5); MCMP("\0\0\0\0\0is a test!");
363 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
364 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
365 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
366 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
369 static void test_RtlUlonglongByteSwap(void)
371 ULONGLONG result;
373 if ( !pRtlUlonglongByteSwap )
375 win_skip("RtlUlonglongByteSwap is not available\n");
376 return;
379 if ( pRtlUlonglongByteSwap( 0 ) != 0 )
381 win_skip("Broken RtlUlonglongByteSwap in win2k\n");
382 return;
385 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
386 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
387 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%s, expected 0x2143658710325476\n",
388 wine_dbgstr_longlong(result));
392 static void test_RtlUniform(void)
394 ULONGLONG num;
395 ULONG seed;
396 ULONG seed_bak;
397 ULONG expected;
398 ULONG result;
400 if (!pRtlUniform)
402 win_skip("RtlUniform is not available\n");
403 return;
407 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
408 * algorithm. This algorithm is:
410 * seed = (seed * const_1 + const_2) % const_3;
412 * According to the documentation the random number is distributed over
413 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
415 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
417 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
418 * algorithm can be expressed without division as:
420 * seed = (seed * const_1 + const_2) & MAXLONG;
422 * To find out const_2 we just call RtlUniform with seed set to 0:
424 seed = 0;
425 expected = 0x7fffffc3;
426 result = pRtlUniform(&seed);
427 ok(result == expected,
428 "RtlUniform(&seed (seed == 0)) returns %x, expected %x\n",
429 result, expected);
431 * The algorithm is now:
433 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
435 * To find out const_1 we can use:
437 * const_1 = RtlUniform(1) - 0x7fffffc3;
439 * If that does not work a search loop can try all possible values of
440 * const_1 and compare to the result to RtlUniform(1).
441 * This way we find out that const_1 is 0xffffffed.
443 * For seed = 1 the const_2 is 0x7fffffc4:
445 seed = 1;
446 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
447 result = pRtlUniform(&seed);
448 ok(result == expected,
449 "RtlUniform(&seed (seed == 1)) returns %x, expected %x\n",
450 result, expected);
452 * For seed = 2 the const_2 is 0x7fffffc3:
454 seed = 2;
455 expected = seed * 0xffffffed + 0x7fffffc3;
456 result = pRtlUniform(&seed);
459 * Windows Vista uses different algorithms, so skip the rest of the tests
460 * until that is figured out. Trace output for the failures is about 10.5 MB!
463 if (result == 0x7fffff9f) {
464 skip("Most likely running on Windows Vista which uses a different algorithm\n");
465 return;
468 ok(result == expected,
469 "RtlUniform(&seed (seed == 2)) returns %x, expected %x\n",
470 result, expected);
473 * More tests show that if seed is odd the result must be incremented by 1:
475 seed = 3;
476 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
477 result = pRtlUniform(&seed);
478 ok(result == expected,
479 "RtlUniform(&seed (seed == 3)) returns %x, expected %x\n",
480 result, expected);
482 seed = 0x6bca1aa;
483 expected = seed * 0xffffffed + 0x7fffffc3;
484 result = pRtlUniform(&seed);
485 ok(result == expected,
486 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %x, expected %x\n",
487 result, expected);
489 seed = 0x6bca1ab;
490 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
491 result = pRtlUniform(&seed);
492 ok(result == expected,
493 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %x, expected %x\n",
494 result, expected);
496 * When seed is 0x6bca1ac there is an exception:
498 seed = 0x6bca1ac;
499 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
500 result = pRtlUniform(&seed);
501 ok(result == expected,
502 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %x, expected %x\n",
503 result, expected);
505 * Note that up to here const_3 is not used
506 * (the highest bit of the result is not set).
508 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
510 seed = 0x6bca1ad;
511 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
512 result = pRtlUniform(&seed);
513 ok(result == expected,
514 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %x, expected %x\n",
515 result, expected);
517 seed = 0x6bca1ae;
518 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
519 result = pRtlUniform(&seed);
520 ok(result == expected,
521 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %x, expected %x\n",
522 result, expected);
524 * There are several ranges where for odd or even seed the result must be
525 * incremented by 1. You can see this ranges in the following test.
527 * For a full test use one of the following loop heads:
529 * for (num = 0; num <= 0xffffffff; num++) {
530 * seed = num;
531 * ...
533 * seed = 0;
534 * for (num = 0; num <= 0xffffffff; num++) {
535 * ...
537 seed = 0;
538 for (num = 0; num <= 100000; num++) {
540 expected = seed * 0xffffffed + 0x7fffffc3;
541 if (seed < 0x6bca1ac) {
542 expected = expected + (seed & 1);
543 } else if (seed == 0x6bca1ac) {
544 expected = (expected + 2) & MAXLONG;
545 } else if (seed < 0xd79435c) {
546 expected = (expected + (~seed & 1)) & MAXLONG;
547 } else if (seed < 0x1435e50b) {
548 expected = expected + (seed & 1);
549 } else if (seed < 0x1af286ba) {
550 expected = (expected + (~seed & 1)) & MAXLONG;
551 } else if (seed < 0x21af2869) {
552 expected = expected + (seed & 1);
553 } else if (seed < 0x286bca18) {
554 expected = (expected + (~seed & 1)) & MAXLONG;
555 } else if (seed < 0x2f286bc7) {
556 expected = expected + (seed & 1);
557 } else if (seed < 0x35e50d77) {
558 expected = (expected + (~seed & 1)) & MAXLONG;
559 } else if (seed < 0x3ca1af26) {
560 expected = expected + (seed & 1);
561 } else if (seed < 0x435e50d5) {
562 expected = (expected + (~seed & 1)) & MAXLONG;
563 } else if (seed < 0x4a1af284) {
564 expected = expected + (seed & 1);
565 } else if (seed < 0x50d79433) {
566 expected = (expected + (~seed & 1)) & MAXLONG;
567 } else if (seed < 0x579435e2) {
568 expected = expected + (seed & 1);
569 } else if (seed < 0x5e50d792) {
570 expected = (expected + (~seed & 1)) & MAXLONG;
571 } else if (seed < 0x650d7941) {
572 expected = expected + (seed & 1);
573 } else if (seed < 0x6bca1af0) {
574 expected = (expected + (~seed & 1)) & MAXLONG;
575 } else if (seed < 0x7286bc9f) {
576 expected = expected + (seed & 1);
577 } else if (seed < 0x79435e4e) {
578 expected = (expected + (~seed & 1)) & MAXLONG;
579 } else if (seed < 0x7ffffffd) {
580 expected = expected + (seed & 1);
581 } else if (seed < 0x86bca1ac) {
582 expected = (expected + (~seed & 1)) & MAXLONG;
583 } else if (seed == 0x86bca1ac) {
584 expected = (expected + 1) & MAXLONG;
585 } else if (seed < 0x8d79435c) {
586 expected = expected + (seed & 1);
587 } else if (seed < 0x9435e50b) {
588 expected = (expected + (~seed & 1)) & MAXLONG;
589 } else if (seed < 0x9af286ba) {
590 expected = expected + (seed & 1);
591 } else if (seed < 0xa1af2869) {
592 expected = (expected + (~seed & 1)) & MAXLONG;
593 } else if (seed < 0xa86bca18) {
594 expected = expected + (seed & 1);
595 } else if (seed < 0xaf286bc7) {
596 expected = (expected + (~seed & 1)) & MAXLONG;
597 } else if (seed == 0xaf286bc7) {
598 expected = (expected + 2) & MAXLONG;
599 } else if (seed < 0xb5e50d77) {
600 expected = expected + (seed & 1);
601 } else if (seed < 0xbca1af26) {
602 expected = (expected + (~seed & 1)) & MAXLONG;
603 } else if (seed < 0xc35e50d5) {
604 expected = expected + (seed & 1);
605 } else if (seed < 0xca1af284) {
606 expected = (expected + (~seed & 1)) & MAXLONG;
607 } else if (seed < 0xd0d79433) {
608 expected = expected + (seed & 1);
609 } else if (seed < 0xd79435e2) {
610 expected = (expected + (~seed & 1)) & MAXLONG;
611 } else if (seed < 0xde50d792) {
612 expected = expected + (seed & 1);
613 } else if (seed < 0xe50d7941) {
614 expected = (expected + (~seed & 1)) & MAXLONG;
615 } else if (seed < 0xebca1af0) {
616 expected = expected + (seed & 1);
617 } else if (seed < 0xf286bc9f) {
618 expected = (expected + (~seed & 1)) & MAXLONG;
619 } else if (seed < 0xf9435e4e) {
620 expected = expected + (seed & 1);
621 } else if (seed < 0xfffffffd) {
622 expected = (expected + (~seed & 1)) & MAXLONG;
623 } else {
624 expected = expected + (seed & 1);
625 } /* if */
626 seed_bak = seed;
627 result = pRtlUniform(&seed);
628 ok(result == expected,
629 "test: 0x%s RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
630 wine_dbgstr_longlong(num), seed_bak, result, expected);
631 ok(seed == expected,
632 "test: 0x%s RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
633 wine_dbgstr_longlong(num), seed_bak, result, expected);
634 } /* for */
636 * Further investigation shows: In the different regions the highest bit
637 * is set or cleared when even or odd seeds need an increment by 1.
638 * This leads to a simplified algorithm:
640 * seed = seed * 0xffffffed + 0x7fffffc3;
641 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
642 * seed = (seed + 2) & MAXLONG;
643 * } else if (seed == 0x7fffffff) {
644 * seed = 0;
645 * } else if ((seed & 0x80000000) == 0) {
646 * seed = seed + (~seed & 1);
647 * } else {
648 * seed = (seed + (seed & 1)) & MAXLONG;
651 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
653 * Now comes the funny part:
654 * It took me one weekend, to find the complicated algorithm and one day more,
655 * to find the simplified algorithm. Several weeks later I found out: The value
656 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
657 * nor with the simplified algorithm. In reality the native function and our
658 * function return a random number distributed over [0..MAXLONG-1]. Note
659 * that this is different from what native documentation states [0..MAXLONG].
660 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
662 * seed = (seed * const_1 + const_2) % MAXLONG;
664 * Further investigations show that the real algorithm is:
666 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
668 * This is checked with the test below:
670 seed = 0;
671 for (num = 0; num <= 100000; num++) {
672 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
673 seed_bak = seed;
674 result = pRtlUniform(&seed);
675 ok(result == expected,
676 "test: 0x%s RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
677 wine_dbgstr_longlong(num), seed_bak, result, expected);
678 ok(seed == expected,
679 "test: 0x%s RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
680 wine_dbgstr_longlong(num), seed_bak, result, expected);
681 } /* for */
683 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
684 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
685 * that there is more than one cycle of generated randon numbers ...
690 static void test_RtlRandom(void)
692 int i, j;
693 ULONG seed;
694 ULONG res[512];
696 if (!pRtlRandom)
698 win_skip("RtlRandom is not available\n");
699 return;
702 seed = 0;
703 for (i = 0; i < ARRAY_SIZE(res); i++)
705 res[i] = pRtlRandom(&seed);
706 ok(seed != res[i], "%i: seed is same as res %x\n", i, seed);
707 for (j = 0; j < i; j++)
708 ok(res[i] != res[j], "res[%i] (%x) is same as res[%i] (%x)\n", j, res[j], i, res[i]);
713 typedef struct {
714 ACCESS_MASK GrantedAccess;
715 ACCESS_MASK DesiredAccess;
716 BOOLEAN result;
717 } all_accesses_t;
719 static const all_accesses_t all_accesses[] = {
720 {0xFEDCBA76, 0xFEDCBA76, 1},
721 {0x00000000, 0xFEDCBA76, 0},
722 {0xFEDCBA76, 0x00000000, 1},
723 {0x00000000, 0x00000000, 1},
724 {0xFEDCBA76, 0xFEDCBA70, 1},
725 {0xFEDCBA70, 0xFEDCBA76, 0},
726 {0xFEDCBA76, 0xFEDC8A76, 1},
727 {0xFEDC8A76, 0xFEDCBA76, 0},
728 {0xFEDCBA76, 0xC8C4B242, 1},
729 {0xC8C4B242, 0xFEDCBA76, 0},
733 static void test_RtlAreAllAccessesGranted(void)
735 unsigned int test_num;
736 BOOLEAN result;
738 if (!pRtlAreAllAccessesGranted)
740 win_skip("RtlAreAllAccessesGranted is not available\n");
741 return;
744 for (test_num = 0; test_num < ARRAY_SIZE(all_accesses); test_num++) {
745 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
746 all_accesses[test_num].DesiredAccess);
747 ok(all_accesses[test_num].result == result,
748 "(test %d): RtlAreAllAccessesGranted(%08x, %08x) returns %d, expected %d\n",
749 test_num, all_accesses[test_num].GrantedAccess,
750 all_accesses[test_num].DesiredAccess,
751 result, all_accesses[test_num].result);
752 } /* for */
756 typedef struct {
757 ACCESS_MASK GrantedAccess;
758 ACCESS_MASK DesiredAccess;
759 BOOLEAN result;
760 } any_accesses_t;
762 static const any_accesses_t any_accesses[] = {
763 {0xFEDCBA76, 0xFEDCBA76, 1},
764 {0x00000000, 0xFEDCBA76, 0},
765 {0xFEDCBA76, 0x00000000, 0},
766 {0x00000000, 0x00000000, 0},
767 {0xFEDCBA76, 0x01234589, 0},
768 {0x00040000, 0xFEDCBA76, 1},
769 {0x00040000, 0xFED8BA76, 0},
770 {0xFEDCBA76, 0x00040000, 1},
771 {0xFED8BA76, 0x00040000, 0},
775 static void test_RtlAreAnyAccessesGranted(void)
777 unsigned int test_num;
778 BOOLEAN result;
780 if (!pRtlAreAnyAccessesGranted)
782 win_skip("RtlAreAnyAccessesGranted is not available\n");
783 return;
786 for (test_num = 0; test_num < ARRAY_SIZE(any_accesses); test_num++) {
787 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
788 any_accesses[test_num].DesiredAccess);
789 ok(any_accesses[test_num].result == result,
790 "(test %d): RtlAreAnyAccessesGranted(%08x, %08x) returns %d, expected %d\n",
791 test_num, any_accesses[test_num].GrantedAccess,
792 any_accesses[test_num].DesiredAccess,
793 result, any_accesses[test_num].result);
794 } /* for */
797 static void test_RtlComputeCrc32(void)
799 DWORD crc = 0;
801 if (!pRtlComputeCrc32)
803 win_skip("RtlComputeCrc32 is not available\n");
804 return;
807 crc = pRtlComputeCrc32(crc, (const BYTE *)src, LEN);
808 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8x\n", crc);
812 typedef struct MY_HANDLE
814 RTL_HANDLE RtlHandle;
815 void * MyValue;
816 } MY_HANDLE;
818 static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
820 ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
821 *AllocatedBit = *AllocatedBit | 1;
824 static void test_HandleTables(void)
826 BOOLEAN result;
827 NTSTATUS status;
828 ULONG Index;
829 MY_HANDLE * MyHandle;
830 RTL_HANDLE_TABLE HandleTable;
832 if (!pRtlInitializeHandleTable)
834 win_skip("RtlInitializeHandleTable is not available\n");
835 return;
838 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
839 MyHandle = (MY_HANDLE *)pRtlAllocateHandle(&HandleTable, &Index);
840 ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
841 RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
842 MyHandle = NULL;
843 result = pRtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
844 ok(result, "Handle %p wasn't valid\n", MyHandle);
845 result = pRtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
846 ok(result, "Couldn't free handle %p\n", MyHandle);
847 status = pRtlDestroyHandleTable(&HandleTable);
848 ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08x\n", status);
851 static void test_RtlAllocateAndInitializeSid(void)
853 NTSTATUS ret;
854 SID_IDENTIFIER_AUTHORITY sia = {{ 1, 2, 3, 4, 5, 6 }};
855 PSID psid;
857 if (!pRtlAllocateAndInitializeSid)
859 win_skip("RtlAllocateAndInitializeSid is not available\n");
860 return;
863 ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
864 ok(!ret, "RtlAllocateAndInitializeSid error %08x\n", ret);
865 ret = pRtlFreeSid(psid);
866 ok(!ret, "RtlFreeSid error %08x\n", ret);
868 /* these tests crash on XP */
869 if (0)
871 pRtlAllocateAndInitializeSid(NULL, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
872 pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL);
875 ret = pRtlAllocateAndInitializeSid(&sia, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
876 ok(ret == STATUS_INVALID_SID, "wrong error %08x\n", ret);
879 static void test_RtlDeleteTimer(void)
881 NTSTATUS ret;
883 if (!pRtlDeleteTimer)
885 win_skip("RtlDeleteTimer is not available\n");
886 return;
889 ret = pRtlDeleteTimer(NULL, NULL, NULL);
890 ok(ret == STATUS_INVALID_PARAMETER_1 ||
891 ret == STATUS_INVALID_PARAMETER, /* W2K */
892 "expected STATUS_INVALID_PARAMETER_1 or STATUS_INVALID_PARAMETER, got %x\n", ret);
895 static void test_RtlThreadErrorMode(void)
897 DWORD oldmode;
898 BOOL is_wow64;
899 DWORD mode;
900 NTSTATUS status;
902 if (!pRtlGetThreadErrorMode || !pRtlSetThreadErrorMode)
904 win_skip("RtlGetThreadErrorMode and/or RtlSetThreadErrorMode not available\n");
905 return;
908 if (!pIsWow64Process || !pIsWow64Process(GetCurrentProcess(), &is_wow64))
909 is_wow64 = FALSE;
911 oldmode = pRtlGetThreadErrorMode();
913 status = pRtlSetThreadErrorMode(0x70, &mode);
914 ok(status == STATUS_SUCCESS ||
915 status == STATUS_WAIT_1, /* Vista */
916 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
917 ok(mode == oldmode,
918 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
919 mode, oldmode);
920 ok(pRtlGetThreadErrorMode() == 0x70,
921 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0x70);
922 if (!is_wow64)
924 ok(NtCurrentTeb()->HardErrorDisabled == 0x70,
925 "The TEB contains 0x%x, expected 0x%x\n",
926 NtCurrentTeb()->HardErrorDisabled, 0x70);
929 status = pRtlSetThreadErrorMode(0, &mode);
930 ok(status == STATUS_SUCCESS ||
931 status == STATUS_WAIT_1, /* Vista */
932 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
933 ok(mode == 0x70,
934 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
935 mode, 0x70);
936 ok(pRtlGetThreadErrorMode() == 0,
937 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0);
938 if (!is_wow64)
940 ok(NtCurrentTeb()->HardErrorDisabled == 0,
941 "The TEB contains 0x%x, expected 0x%x\n",
942 NtCurrentTeb()->HardErrorDisabled, 0);
945 for (mode = 1; mode; mode <<= 1)
947 status = pRtlSetThreadErrorMode(mode, NULL);
948 if (mode & 0x70)
949 ok(status == STATUS_SUCCESS ||
950 status == STATUS_WAIT_1, /* Vista */
951 "RtlSetThreadErrorMode(%x,NULL) failed with error 0x%08x\n",
952 mode, status);
953 else
954 ok(status == STATUS_INVALID_PARAMETER_1,
955 "RtlSetThreadErrorMode(%x,NULL) returns 0x%08x, "
956 "expected STATUS_INVALID_PARAMETER_1\n",
957 mode, status);
960 pRtlSetThreadErrorMode(oldmode, NULL);
963 static void test_LdrProcessRelocationBlock(void)
965 IMAGE_BASE_RELOCATION *ret;
966 USHORT reloc;
967 DWORD addr32;
968 SHORT addr16;
970 if(!pLdrProcessRelocationBlock) {
971 win_skip("LdrProcessRelocationBlock not available\n");
972 return;
975 addr32 = 0x50005;
976 reloc = IMAGE_REL_BASED_HIGHLOW<<12;
977 ret = pLdrProcessRelocationBlock(&addr32, 1, &reloc, 0x500050);
978 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
979 ok(addr32 == 0x550055, "addr32 = %x, expected 0x550055\n", addr32);
981 addr16 = 0x505;
982 reloc = IMAGE_REL_BASED_HIGH<<12;
983 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
984 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
985 ok(addr16 == 0x555, "addr16 = %x, expected 0x555\n", addr16);
987 addr16 = 0x505;
988 reloc = IMAGE_REL_BASED_LOW<<12;
989 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
990 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
991 ok(addr16 == 0x565, "addr16 = %x, expected 0x565\n", addr16);
994 static void test_RtlIpv4AddressToString(void)
996 CHAR buffer[20];
997 CHAR *res;
998 IN_ADDR ip;
999 DWORD_PTR len;
1001 if (!pRtlIpv4AddressToStringA)
1003 win_skip("RtlIpv4AddressToStringA not available\n");
1004 return;
1007 ip.S_un.S_un_b.s_b1 = 1;
1008 ip.S_un.S_un_b.s_b2 = 2;
1009 ip.S_un.S_un_b.s_b3 = 3;
1010 ip.S_un.S_un_b.s_b4 = 4;
1012 memset(buffer, '#', sizeof(buffer) - 1);
1013 buffer[sizeof(buffer) -1] = 0;
1014 res = pRtlIpv4AddressToStringA(&ip, buffer);
1015 len = strlen(buffer);
1016 ok(res == (buffer + len), "got %p with '%s' (expected %p)\n", res, buffer, buffer + len);
1018 res = pRtlIpv4AddressToStringA(&ip, NULL);
1019 ok( (res == (char *)~0) ||
1020 broken(res == (char *)len), /* XP and w2003 */
1021 "got %p (expected ~0)\n", res);
1023 if (0) {
1024 /* this crashes in windows */
1025 memset(buffer, '#', sizeof(buffer) - 1);
1026 buffer[sizeof(buffer) -1] = 0;
1027 res = pRtlIpv4AddressToStringA(NULL, buffer);
1028 trace("got %p with '%s'\n", res, buffer);
1031 if (0) {
1032 /* this crashes in windows */
1033 res = pRtlIpv4AddressToStringA(NULL, NULL);
1034 trace("got %p\n", res);
1038 static void test_RtlIpv4AddressToStringEx(void)
1040 CHAR ip_1234[] = "1.2.3.4";
1041 CHAR ip_1234_80[] = "1.2.3.4:80";
1042 LPSTR expect;
1043 CHAR buffer[30];
1044 NTSTATUS res;
1045 IN_ADDR ip;
1046 ULONG size;
1047 DWORD used;
1048 USHORT port;
1050 if (!pRtlIpv4AddressToStringExA)
1052 win_skip("RtlIpv4AddressToStringExA not available\n");
1053 return;
1056 ip.S_un.S_un_b.s_b1 = 1;
1057 ip.S_un.S_un_b.s_b2 = 2;
1058 ip.S_un.S_un_b.s_b3 = 3;
1059 ip.S_un.S_un_b.s_b4 = 4;
1061 port = htons(80);
1062 expect = ip_1234_80;
1064 size = sizeof(buffer);
1065 memset(buffer, '#', sizeof(buffer) - 1);
1066 buffer[sizeof(buffer) -1] = 0;
1067 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1068 used = strlen(buffer);
1069 ok( (res == STATUS_SUCCESS) &&
1070 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1071 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1073 size = used + 1;
1074 memset(buffer, '#', sizeof(buffer) - 1);
1075 buffer[sizeof(buffer) -1] = 0;
1076 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1077 ok( (res == STATUS_SUCCESS) &&
1078 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1079 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1081 size = used;
1082 memset(buffer, '#', sizeof(buffer) - 1);
1083 buffer[sizeof(buffer) -1] = 0;
1084 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1085 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1086 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1087 res, size, buffer, used + 1);
1089 size = used - 1;
1090 memset(buffer, '#', sizeof(buffer) - 1);
1091 buffer[sizeof(buffer) -1] = 0;
1092 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1093 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1094 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1095 res, size, buffer, used + 1);
1098 /* to get only the ip, use 0 as port */
1099 port = 0;
1100 expect = ip_1234;
1102 size = sizeof(buffer);
1103 memset(buffer, '#', sizeof(buffer) - 1);
1104 buffer[sizeof(buffer) -1] = 0;
1105 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1106 used = strlen(buffer);
1107 ok( (res == STATUS_SUCCESS) &&
1108 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1109 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1111 size = used + 1;
1112 memset(buffer, '#', sizeof(buffer) - 1);
1113 buffer[sizeof(buffer) -1] = 0;
1114 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1115 ok( (res == STATUS_SUCCESS) &&
1116 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1117 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1119 size = used;
1120 memset(buffer, '#', sizeof(buffer) - 1);
1121 buffer[sizeof(buffer) -1] = 0;
1122 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1123 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1124 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1125 res, size, buffer, used + 1);
1127 size = used - 1;
1128 memset(buffer, '#', sizeof(buffer) - 1);
1129 buffer[sizeof(buffer) -1] = 0;
1130 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1131 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1132 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1133 res, size, buffer, used + 1);
1136 /* parameters are checked */
1137 memset(buffer, '#', sizeof(buffer) - 1);
1138 buffer[sizeof(buffer) -1] = 0;
1139 res = pRtlIpv4AddressToStringExA(&ip, 0, buffer, NULL);
1140 ok(res == STATUS_INVALID_PARAMETER,
1141 "got 0x%x with '%s' (expected STATUS_INVALID_PARAMETER)\n", res, buffer);
1143 size = sizeof(buffer);
1144 res = pRtlIpv4AddressToStringExA(&ip, 0, NULL, &size);
1145 ok( res == STATUS_INVALID_PARAMETER,
1146 "got 0x%x and size %d (expected STATUS_INVALID_PARAMETER)\n", res, size);
1148 size = sizeof(buffer);
1149 memset(buffer, '#', sizeof(buffer) - 1);
1150 buffer[sizeof(buffer) -1] = 0;
1151 res = pRtlIpv4AddressToStringExA(NULL, 0, buffer, &size);
1152 ok( res == STATUS_INVALID_PARAMETER,
1153 "got 0x%x and size %d with '%s' (expected STATUS_INVALID_PARAMETER)\n",
1154 res, size, buffer);
1157 static void test_RtlIpv4StringToAddress(void)
1159 NTSTATUS res;
1160 IN_ADDR ip, expected_ip;
1161 PCSTR terminator;
1162 CHAR dummy;
1163 struct
1165 PCSTR address;
1166 NTSTATUS res;
1167 int terminator_offset;
1168 int ip[4];
1169 BOOL strict_is_different;
1170 NTSTATUS res_strict;
1171 int terminator_offset_strict;
1172 int ip_strict[4];
1173 } tests[] =
1175 { "", STATUS_INVALID_PARAMETER, 0, { -1 } },
1176 { " ", STATUS_INVALID_PARAMETER, 0, { -1 } },
1177 { "1.1.1.1", STATUS_SUCCESS, 7, { 1, 1, 1, 1 } },
1178 { "0.0.0.0", STATUS_SUCCESS, 7, { 0, 0, 0, 0 } },
1179 { "255.255.255.255", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1180 { "255.255.255.255:123",
1181 STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1182 { "255.255.255.256", STATUS_INVALID_PARAMETER, 15, { -1 } },
1183 { "255.255.255.4294967295",
1184 STATUS_INVALID_PARAMETER, 22, { -1 } },
1185 { "255.255.255.4294967296",
1186 STATUS_INVALID_PARAMETER, 21, { -1 } },
1187 { "255.255.255.4294967297",
1188 STATUS_INVALID_PARAMETER, 21, { -1 } },
1189 { "a", STATUS_INVALID_PARAMETER, 0, { -1 } },
1190 { "1.1.1.0xaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1191 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1192 { "1.1.1.0XaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1193 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1194 { "1.1.1.0x", STATUS_INVALID_PARAMETER, 8, { -1 } },
1195 { "1.1.1.0xff", STATUS_SUCCESS, 10, { 1, 1, 1, 255 },
1196 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1197 { "1.1.1.0x100", STATUS_INVALID_PARAMETER, 11, { -1 },
1198 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1199 { "1.1.1.0xffffffff",STATUS_INVALID_PARAMETER, 16, { -1 },
1200 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1201 { "1.1.1.0x100000000",
1202 STATUS_INVALID_PARAMETER, 16, { -1, 0, 0, 0 },
1203 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1204 { "1.1.1.010", STATUS_SUCCESS, 9, { 1, 1, 1, 8 },
1205 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1206 { "1.1.1.00", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1207 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1208 { "1.1.1.007", STATUS_SUCCESS, 9, { 1, 1, 1, 7 },
1209 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1210 { "1.1.1.08", STATUS_INVALID_PARAMETER, 7, { -1 } },
1211 { "1.1.1.008", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1212 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1213 { "1.1.1.0a", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1214 { "1.1.1.0o10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1215 { "1.1.1.0b10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1216 { "1.1.1.-2", STATUS_INVALID_PARAMETER, 6, { -1 } },
1217 { "1", STATUS_SUCCESS, 1, { 0, 0, 0, 1 },
1218 TRUE, STATUS_INVALID_PARAMETER, 1, { -1 } },
1219 { "-1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1220 { "203569230", STATUS_SUCCESS, 9, { 12, 34, 56, 78 },
1221 TRUE, STATUS_INVALID_PARAMETER, 9, { -1 } },
1222 { "1.223756", STATUS_SUCCESS, 8, { 1, 3, 106, 12 },
1223 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1224 { "3.4.756", STATUS_SUCCESS, 7, { 3, 4, 2, 244 },
1225 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1226 { "3.4.756.1", STATUS_INVALID_PARAMETER, 9, { -1 } },
1227 { "3.4.65536", STATUS_INVALID_PARAMETER, 9, { -1 } },
1228 { "3.4.5.6.7", STATUS_INVALID_PARAMETER, 7, { -1 } },
1229 { "3.4.5.+6", STATUS_INVALID_PARAMETER, 6, { -1 } },
1230 { " 3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1231 { "\t3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1232 { "3.4.5.6 ", STATUS_SUCCESS, 7, { 3, 4, 5, 6 } },
1233 { "3. 4.5.6", STATUS_INVALID_PARAMETER, 2, { -1 } },
1234 { ".", STATUS_INVALID_PARAMETER, 1, { -1 } },
1235 { "..", STATUS_INVALID_PARAMETER, 1, { -1 } },
1236 { "1.", STATUS_INVALID_PARAMETER, 2, { -1 } },
1237 { "1..", STATUS_INVALID_PARAMETER, 3, { -1 } },
1238 { ".1", STATUS_INVALID_PARAMETER, 1, { -1 } },
1239 { ".1.", STATUS_INVALID_PARAMETER, 1, { -1 } },
1240 { ".1.2.3", STATUS_INVALID_PARAMETER, 1, { -1 } },
1241 { "0.1.2.3", STATUS_SUCCESS, 7, { 0, 1, 2, 3 } },
1242 { "0.1.2.3.", STATUS_INVALID_PARAMETER, 7, { -1 } },
1243 { "[0.1.2.3]", STATUS_INVALID_PARAMETER, 0, { -1 } },
1244 { "::1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1245 { ":1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1247 const int testcount = ARRAY_SIZE(tests);
1248 int i;
1250 if (!pRtlIpv4StringToAddressA)
1252 skip("RtlIpv4StringToAddress not available\n");
1253 return;
1256 if (0)
1258 /* leaving either parameter NULL crashes on Windows */
1259 res = pRtlIpv4StringToAddressA(NULL, FALSE, &terminator, &ip);
1260 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, NULL, &ip);
1261 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, &terminator, NULL);
1262 /* same for the wide char version */
1264 res = pRtlIpv4StringToAddressW(NULL, FALSE, &terminatorW, &ip);
1265 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, NULL, &ip);
1266 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, &terminatorW, NULL);
1270 for (i = 0; i < testcount; i++)
1272 /* non-strict */
1273 terminator = &dummy;
1274 ip.S_un.S_addr = 0xabababab;
1275 res = pRtlIpv4StringToAddressA(tests[i].address, FALSE, &terminator, &ip);
1276 ok(res == tests[i].res,
1277 "[%s] res = 0x%08x, expected 0x%08x\n",
1278 tests[i].address, res, tests[i].res);
1279 ok(terminator == tests[i].address + tests[i].terminator_offset,
1280 "[%s] terminator = %p, expected %p\n",
1281 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset);
1282 if (tests[i].ip[0] == -1)
1283 expected_ip.S_un.S_addr = 0xabababab;
1284 else
1286 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip[0];
1287 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip[1];
1288 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip[2];
1289 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip[3];
1291 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1292 "[%s] ip = %08x, expected %08x\n",
1293 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1295 if (!tests[i].strict_is_different)
1297 tests[i].res_strict = tests[i].res;
1298 tests[i].terminator_offset_strict = tests[i].terminator_offset;
1299 tests[i].ip_strict[0] = tests[i].ip[0];
1300 tests[i].ip_strict[1] = tests[i].ip[1];
1301 tests[i].ip_strict[2] = tests[i].ip[2];
1302 tests[i].ip_strict[3] = tests[i].ip[3];
1304 /* strict */
1305 terminator = &dummy;
1306 ip.S_un.S_addr = 0xabababab;
1307 res = pRtlIpv4StringToAddressA(tests[i].address, TRUE, &terminator, &ip);
1308 ok(res == tests[i].res_strict,
1309 "[%s] res = 0x%08x, expected 0x%08x\n",
1310 tests[i].address, res, tests[i].res_strict);
1311 ok(terminator == tests[i].address + tests[i].terminator_offset_strict,
1312 "[%s] terminator = %p, expected %p\n",
1313 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset_strict);
1314 if (tests[i].ip_strict[0] == -1)
1315 expected_ip.S_un.S_addr = 0xabababab;
1316 else
1318 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip_strict[0];
1319 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip_strict[1];
1320 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip_strict[2];
1321 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip_strict[3];
1323 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1324 "[%s] ip = %08x, expected %08x\n",
1325 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1330 /* ipv6 addresses based on the set from https://github.com/beaugunderson/javascript-ipv6/tree/master/test/data */
1331 static const struct
1333 PCSTR address;
1334 NTSTATUS res;
1335 int terminator_offset;
1336 int ip[8];
1337 /* win_broken: older versions of windows do not handle this correct
1338 ex_fail: Ex function does need the string to be terminated, non-Ex does not.
1339 ex_skip: test doesnt make sense for Ex (f.e. it's invalid for non-Ex but valid for Ex) */
1340 enum { normal_6, win_broken_6 = 1, ex_fail_6 = 2, ex_skip_6 = 4 } flags;
1341 } ipv6_tests[] =
1343 { "0000:0000:0000:0000:0000:0000:0000:0000", STATUS_SUCCESS, 39,
1344 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1345 { "0000:0000:0000:0000:0000:0000:0000:0001", STATUS_SUCCESS, 39,
1346 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1347 { "0:0:0:0:0:0:0:0", STATUS_SUCCESS, 15,
1348 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1349 { "0:0:0:0:0:0:0:1", STATUS_SUCCESS, 15,
1350 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1351 { "0:0:0:0:0:0:0::", STATUS_SUCCESS, 13,
1352 { 0, 0, 0, 0, 0, 0, 0, 0 }, win_broken_6 },
1353 { "0:0:0:0:0:0:13.1.68.3", STATUS_SUCCESS, 21,
1354 { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1355 { "0:0:0:0:0:0::", STATUS_SUCCESS, 13,
1356 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1357 { "0:0:0:0:0::", STATUS_SUCCESS, 11,
1358 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1359 { "0:0:0:0:0:FFFF:129.144.52.38", STATUS_SUCCESS, 28,
1360 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1361 { "0::", STATUS_SUCCESS, 3,
1362 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1363 { "0:1:2:3:4:5:6:7", STATUS_SUCCESS, 15,
1364 { 0, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700 } },
1365 { "1080:0:0:0:8:800:200c:417a", STATUS_SUCCESS, 26,
1366 { 0x8010, 0, 0, 0, 0x800, 0x8, 0x0c20, 0x7a41 } },
1367 { "0:a:b:c:d:e:f::", STATUS_SUCCESS, 13,
1368 { 0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00, 0 }, win_broken_6 },
1369 { "1111:2222:3333:4444:5555:6666:123.123.123.123", STATUS_SUCCESS, 45,
1370 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1371 { "1111:2222:3333:4444:5555:6666:7777:8888", STATUS_SUCCESS, 39,
1372 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1373 { "1111:2222:3333:4444:0x5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 21,
1374 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1375 { "1111:2222:3333:4444:x555:6666:7777:8888", STATUS_INVALID_PARAMETER, 20,
1376 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1377 { "1111:2222:3333:4444:0r5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 21,
1378 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1379 { "1111:2222:3333:4444:r5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 20,
1380 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1381 { "1111:2222:3333:4444:5555:6666:7777::", STATUS_SUCCESS, 34,
1382 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0 }, win_broken_6 },
1383 { "1111:2222:3333:4444:5555:6666::", STATUS_SUCCESS, 31,
1384 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0 } },
1385 { "1111:2222:3333:4444:5555:6666::8888", STATUS_SUCCESS, 35,
1386 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x8888 } },
1387 { "1111:2222:3333:4444:5555::", STATUS_SUCCESS, 26,
1388 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0 } },
1389 { "1111:2222:3333:4444:5555::123.123.123.123", STATUS_SUCCESS, 41,
1390 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7b7b, 0x7b7b } },
1391 { "1111:2222:3333:4444:5555::0x1.123.123.123", STATUS_SUCCESS, 27,
1392 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x100 }, ex_fail_6 },
1393 { "1111:2222:3333:4444:5555::0x88", STATUS_SUCCESS, 27,
1394 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800 }, ex_fail_6 },
1395 { "1111:2222:3333:4444:5555::0X88", STATUS_SUCCESS, 27,
1396 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800 }, ex_fail_6 },
1397 { "1111:2222:3333:4444:5555::0X", STATUS_SUCCESS, 27,
1398 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0 }, ex_fail_6 },
1399 { "1111:2222:3333:4444:5555::0X88:7777", STATUS_SUCCESS, 27,
1400 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800 }, ex_fail_6 },
1401 { "1111:2222:3333:4444:5555::0x8888", STATUS_SUCCESS, 27,
1402 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888 }, ex_fail_6 },
1403 { "1111:2222:3333:4444:5555::08888", STATUS_INVALID_PARAMETER, 31,
1404 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab } },
1405 { "1111:2222:3333:4444:5555::fffff", STATUS_INVALID_PARAMETER, 31,
1406 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab } },
1407 { "1111:2222:3333:4444::fffff", STATUS_INVALID_PARAMETER, 26,
1408 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1409 { "1111:2222:3333::fffff", STATUS_INVALID_PARAMETER, 21,
1410 { 0x1111, 0x2222, 0x3333, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1411 { "1111:2222:3333:4444:5555::7777:8888", STATUS_SUCCESS, 35,
1412 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7777, 0x8888 } },
1413 { "1111:2222:3333:4444:5555::8888", STATUS_SUCCESS, 30,
1414 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888 } },
1415 { "1111::", STATUS_SUCCESS, 6,
1416 { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1417 { "1111::123.123.123.123", STATUS_SUCCESS, 21,
1418 { 0x1111, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1419 { "1111::3333:4444:5555:6666:123.123.123.123", STATUS_SUCCESS, 41,
1420 { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1421 { "1111::3333:4444:5555:6666:7777:8888", STATUS_SUCCESS, 35,
1422 { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1423 { "1111::4444:5555:6666:123.123.123.123", STATUS_SUCCESS, 36,
1424 { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1425 { "1111::4444:5555:6666:7777:8888", STATUS_SUCCESS, 30,
1426 { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1427 { "1111::5555:6666:123.123.123.123", STATUS_SUCCESS, 31,
1428 { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1429 { "1111::5555:6666:7777:8888", STATUS_SUCCESS, 25,
1430 { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7777, 0x8888 } },
1431 { "1111::6666:123.123.123.123", STATUS_SUCCESS, 26,
1432 { 0x1111, 0, 0, 0, 0, 0x6666, 0x7b7b, 0x7b7b } },
1433 { "1111::6666:7777:8888", STATUS_SUCCESS, 20,
1434 { 0x1111, 0, 0, 0, 0, 0x6666, 0x7777, 0x8888 } },
1435 { "1111::7777:8888", STATUS_SUCCESS, 15,
1436 { 0x1111, 0, 0, 0, 0, 0, 0x7777, 0x8888 } },
1437 { "1111::8888", STATUS_SUCCESS, 10,
1438 { 0x1111, 0, 0, 0, 0, 0, 0, 0x8888 } },
1439 { "1:2:3:4:5:6:1.2.3.4", STATUS_SUCCESS, 19,
1440 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x201, 0x403 } },
1441 { "1:2:3:4:5:6:7:8", STATUS_SUCCESS, 15,
1442 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 0x800 } },
1443 { "1:2:3:4:5:6::", STATUS_SUCCESS, 13,
1444 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0 } },
1445 { "1:2:3:4:5:6::8", STATUS_SUCCESS, 14,
1446 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0x800 } },
1447 { "2001:0000:1234:0000:0000:C1C0:ABCD:0876", STATUS_SUCCESS, 39,
1448 { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 } },
1449 { "2001:0000:4136:e378:8000:63bf:3fff:fdd2", STATUS_SUCCESS, 39,
1450 { 0x120, 0, 0x3641, 0x78e3, 0x80, 0xbf63, 0xff3f, 0xd2fd } },
1451 { "2001:0db8:0:0:0:0:1428:57ab", STATUS_SUCCESS, 27,
1452 { 0x120, 0xb80d, 0, 0, 0, 0, 0x2814, 0xab57 } },
1453 { "2001:0db8:1234:ffff:ffff:ffff:ffff:ffff", STATUS_SUCCESS, 39,
1454 { 0x120, 0xb80d, 0x3412, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1455 { "2001::CE49:7601:2CAD:DFFF:7C94:FFFE", STATUS_SUCCESS, 35,
1456 { 0x120, 0, 0x49ce, 0x176, 0xad2c, 0xffdf, 0x947c, 0xfeff } },
1457 { "2001:db8:85a3::8a2e:370:7334", STATUS_SUCCESS, 28,
1458 { 0x120, 0xb80d, 0xa385, 0, 0, 0x2e8a, 0x7003, 0x3473 } },
1459 { "3ffe:0b00:0000:0000:0001:0000:0000:000a", STATUS_SUCCESS, 39,
1460 { 0xfe3f, 0xb, 0, 0, 0x100, 0, 0, 0xa00 } },
1461 { "::", STATUS_SUCCESS, 2,
1462 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1463 { "::%16", STATUS_SUCCESS, 2,
1464 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1465 { "::/16", STATUS_SUCCESS, 2,
1466 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1467 { "::0", STATUS_SUCCESS, 3,
1468 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1469 { "::0:0", STATUS_SUCCESS, 5,
1470 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1471 { "::0:0:0", STATUS_SUCCESS, 7,
1472 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1473 { "::0:0:0:0", STATUS_SUCCESS, 9,
1474 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1475 { "::0:0:0:0:0", STATUS_SUCCESS, 11,
1476 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1477 { "::0:0:0:0:0:0", STATUS_SUCCESS, 13,
1478 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1479 /* this one and the next one are incorrectly parsed by windows,
1480 it adds one zero too many in front, cutting off the last digit. */
1481 { "::0:0:0:0:0:0:0", STATUS_SUCCESS, 13,
1482 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1483 { "::0:a:b:c:d:e:f", STATUS_SUCCESS, 13,
1484 { 0, 0, 0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00 }, ex_fail_6 },
1485 { "::123.123.123.123", STATUS_SUCCESS, 17,
1486 { 0, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1487 { "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", STATUS_SUCCESS, 39,
1488 { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1490 { "':10.0.0.1", STATUS_INVALID_PARAMETER, 0,
1491 { -1 } },
1492 { "-1", STATUS_INVALID_PARAMETER, 0,
1493 { -1 } },
1494 { "02001:0000:1234:0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, -1,
1495 { -1 } },
1496 { "2001:00000:1234:0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, -1,
1497 { 0x120, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1498 { "2001:0000:01234:0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, -1,
1499 { 0x120, 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1500 { "1.2.3.4", STATUS_INVALID_PARAMETER, 7,
1501 { 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1502 { "1.2.3.4:1111::5555", STATUS_INVALID_PARAMETER, 7,
1503 { 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1504 { "1.2.3.4::5555", STATUS_INVALID_PARAMETER, 7,
1505 { 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1506 { "11112222:3333:4444:5555:6666:1.2.3.4", STATUS_INVALID_PARAMETER, -1,
1507 { -1 } },
1508 { "11112222:3333:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, -1,
1509 { -1 } },
1510 { "1111", STATUS_INVALID_PARAMETER, 4,
1511 { -1 } },
1512 { "1111:22223333:4444:5555:6666:1.2.3.4", STATUS_INVALID_PARAMETER, -1,
1513 { 0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1514 { "1111:22223333:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, -1,
1515 { 0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1516 { "1111:2222:", STATUS_INVALID_PARAMETER, 10,
1517 { 0x1111, 0x2222, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1518 { "1111:2222:1.2.3.4", STATUS_INVALID_PARAMETER, 17,
1519 { 0x1111, 0x2222, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab } },
1520 { "1111:2222:3333", STATUS_INVALID_PARAMETER, 14,
1521 { 0x1111, 0x2222, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1522 { "1111:2222:3333:4444:5555:6666:7777:1.2.3.4", STATUS_SUCCESS, 36,
1523 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x100 }, ex_fail_6 },
1524 { "1111:2222:3333:4444:5555:6666:7777:8888:", STATUS_SUCCESS, 39,
1525 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 }, ex_fail_6 },
1526 { "1111:2222:3333:4444:5555:6666:7777:8888:1.2.3.4",STATUS_SUCCESS, 39,
1527 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 }, ex_fail_6 },
1528 { "1111:2222:3333:4444:5555:6666:7777:8888:9999", STATUS_SUCCESS, 39,
1529 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 }, ex_fail_6 },
1530 { "1111:2222:::", STATUS_SUCCESS, 11,
1531 { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1532 { "1111::5555:", STATUS_INVALID_PARAMETER, 11,
1533 { 0x1111, 0x5555, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1534 { "1111::3333:4444:5555:6666:7777::", STATUS_SUCCESS, 30,
1535 { 0x1111, 0, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777 }, ex_fail_6 },
1536 { "1111:2222:::4444:5555:6666:1.2.3.4", STATUS_SUCCESS, 11,
1537 { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1538 { "1111::3333::5555:6666:1.2.3.4", STATUS_SUCCESS, 10,
1539 { 0x1111, 0, 0, 0, 0, 0, 0, 0x3333 }, ex_fail_6 },
1540 { "12345::6:7:8", STATUS_INVALID_PARAMETER, -1,
1541 { -1 } },
1542 { "1::1.2.256.4", STATUS_INVALID_PARAMETER, -1,
1543 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1544 { "1::1.2.3.256", STATUS_INVALID_PARAMETER, 12,
1545 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1546 { "1::1.2.3.300", STATUS_INVALID_PARAMETER, 12,
1547 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1548 { "1::1.2::1", STATUS_INVALID_PARAMETER, 6,
1549 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1550 { "1::1.2.3.4::1", STATUS_SUCCESS, 10,
1551 { 0x100, 0, 0, 0, 0, 0, 0x201, 0x403 }, ex_fail_6 },
1552 { "1::1.", STATUS_INVALID_PARAMETER, 5,
1553 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1554 { "1::1.2", STATUS_INVALID_PARAMETER, 6,
1555 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1556 { "1::1.2.", STATUS_INVALID_PARAMETER, 7,
1557 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1558 { "1::1.2.3", STATUS_INVALID_PARAMETER, 8,
1559 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1560 { "1::1.2.3.", STATUS_INVALID_PARAMETER, 9,
1561 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1562 { "1::1.2.3.4", STATUS_SUCCESS, 10,
1563 { 0x100, 0, 0, 0, 0, 0, 0x201, 0x403 } },
1564 { "1::1.2.3.900", STATUS_INVALID_PARAMETER, 12,
1565 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1566 { "1::1.2.300.4", STATUS_INVALID_PARAMETER, -1,
1567 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1568 { "1::1.256.3.4", STATUS_INVALID_PARAMETER, -1,
1569 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1570 { "1::256.2.3.4", STATUS_INVALID_PARAMETER, -1,
1571 { 0x100, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1572 { "1::2::3", STATUS_SUCCESS, 4,
1573 { 0x100, 0, 0, 0, 0, 0, 0, 0x200 }, ex_fail_6 },
1574 { "2001:0000:1234: 0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, 15,
1575 { 0x120, 0, 0x3412, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1576 { "2001:0000:1234:0000:0000:C1C0:ABCD:0876 0", STATUS_SUCCESS, 39,
1577 { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 }, ex_fail_6 },
1578 { "2001:1:1:1:1:1:255Z255X255Y255", STATUS_INVALID_PARAMETER, 18,
1579 { 0x120, 0x100, 0x100, 0x100, 0x100, 0x100, 0xabab, 0xabab } },
1580 { "2001::FFD3::57ab", STATUS_SUCCESS, 10,
1581 { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff }, ex_fail_6 },
1582 { ":", STATUS_INVALID_PARAMETER, 0,
1583 { -1 } },
1584 { ":1111:2222:3333:4444:5555:6666:1.2.3.4", STATUS_INVALID_PARAMETER, 0,
1585 { -1 } },
1586 { ":1111:2222:3333:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 0,
1587 { -1 } },
1588 { ":1111::", STATUS_INVALID_PARAMETER, 0,
1589 { -1 } },
1590 { "::-1", STATUS_SUCCESS, 2,
1591 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1592 { "::.", STATUS_SUCCESS, 2,
1593 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1594 { "::..", STATUS_SUCCESS, 2,
1595 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1596 { "::...", STATUS_SUCCESS, 2,
1597 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1598 { "XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:1.2.3.4", STATUS_INVALID_PARAMETER, 0,
1599 { -1 } },
1600 { "[::]", STATUS_INVALID_PARAMETER, 0,
1601 { -1 }, ex_skip_6 },
1603 const unsigned int ipv6_testcount = sizeof(ipv6_tests) / sizeof(ipv6_tests[0]);
1605 static void init_ip6(IN6_ADDR* addr, const int src[8])
1607 unsigned int j;
1608 if (!src || src[0] == -1)
1610 for (j = 0; j < 8; ++j)
1611 addr->s6_words[j] = 0xabab;
1613 else
1615 for (j = 0; j < 8; ++j)
1616 addr->s6_words[j] = src[j];
1620 static void compare_RtlIpv6StringToAddressW(PCSTR name_a, int terminator_offset_a,
1621 const struct in6_addr *addr_a, NTSTATUS res_a)
1623 WCHAR name[512];
1624 NTSTATUS res;
1625 IN6_ADDR ip;
1626 PCWSTR terminator;
1628 if (!pRtlIpv6StringToAddressW)
1629 return;
1631 pRtlMultiByteToUnicodeN(name, sizeof(name), NULL, name_a, strlen(name_a) + 1);
1633 init_ip6(&ip, NULL);
1634 terminator = (void *)0xdeadbeef;
1635 res = pRtlIpv6StringToAddressW(name, &terminator, &ip);
1636 ok(res == res_a, "[W:%s] res = 0x%08x, expected 0x%08x\n", name_a, res, res_a);
1638 if (terminator_offset_a < 0)
1640 ok(terminator == (void *)0xdeadbeef,
1641 "[W:%s] terminator = %p, expected it not to change\n",
1642 name_a, terminator);
1644 else
1646 ok(terminator == name + terminator_offset_a,
1647 "[W:%s] terminator = %p, expected %p\n",
1648 name_a, terminator, name + terminator_offset_a);
1651 ok(!memcmp(&ip, addr_a, sizeof(ip)),
1652 "[W:%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
1653 name_a,
1654 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
1655 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
1656 addr_a->s6_words[0], addr_a->s6_words[1], addr_a->s6_words[2], addr_a->s6_words[3],
1657 addr_a->s6_words[4], addr_a->s6_words[5], addr_a->s6_words[6], addr_a->s6_words[7]);
1660 static void test_RtlIpv6StringToAddress(void)
1662 NTSTATUS res;
1663 IN6_ADDR ip, expected_ip;
1664 PCSTR terminator;
1665 unsigned int i;
1667 if (!pRtlIpv6StringToAddressW)
1669 skip("RtlIpv6StringToAddressW not available\n");
1670 /* we can continue, just not test W */
1673 if (!pRtlIpv6StringToAddressA)
1675 skip("RtlIpv6StringToAddressA not available\n");
1676 return; /* all tests are centered around A, we cannot continue */
1679 res = pRtlIpv6StringToAddressA("::", &terminator, &ip);
1680 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08x, expected STATUS_SUCCESS\n", res);
1681 if (0)
1683 /* any of these crash */
1684 res = pRtlIpv6StringToAddressA(NULL, &terminator, &ip);
1685 ok(res == STATUS_INVALID_PARAMETER, "[null string] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1686 res = pRtlIpv6StringToAddressA("::", NULL, &ip);
1687 ok(res == STATUS_INVALID_PARAMETER, "[null terminator] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1688 res = pRtlIpv6StringToAddressA("::", &terminator, NULL);
1689 ok(res == STATUS_INVALID_PARAMETER, "[null result] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1692 /* sanity check */
1693 ok(sizeof(ip) == sizeof(USHORT)* 8, "sizeof(ip)\n");
1695 for (i = 0; i < ipv6_testcount; i++)
1697 init_ip6(&ip, NULL);
1698 terminator = (void *)0xdeadbeef;
1699 res = pRtlIpv6StringToAddressA(ipv6_tests[i].address, &terminator, &ip);
1700 compare_RtlIpv6StringToAddressW(ipv6_tests[i].address, (terminator != (void *)0xdeadbeef) ?
1701 (terminator - ipv6_tests[i].address) : -1, &ip, res);
1703 if (ipv6_tests[i].flags & win_broken_6)
1705 ok(res == ipv6_tests[i].res || broken(res == STATUS_INVALID_PARAMETER),
1706 "[%s] res = 0x%08x, expected 0x%08x\n",
1707 ipv6_tests[i].address, res, ipv6_tests[i].res);
1709 if (res == STATUS_INVALID_PARAMETER)
1710 continue;
1712 else
1714 ok(res == ipv6_tests[i].res,
1715 "[%s] res = 0x%08x, expected 0x%08x\n",
1716 ipv6_tests[i].address, res, ipv6_tests[i].res);
1719 if (ipv6_tests[i].terminator_offset < 0)
1721 ok(terminator == (void *)0xdeadbeef,
1722 "[%s] terminator = %p, expected it not to change\n",
1723 ipv6_tests[i].address, terminator);
1725 else if (ipv6_tests[i].flags & win_broken_6)
1727 PCSTR expected = ipv6_tests[i].address + ipv6_tests[i].terminator_offset;
1728 ok(terminator == expected || broken(terminator == expected + 2),
1729 "[%s] terminator = %p, expected %p\n",
1730 ipv6_tests[i].address, terminator, expected);
1732 else
1734 ok(terminator == ipv6_tests[i].address + ipv6_tests[i].terminator_offset,
1735 "[%s] terminator = %p, expected %p\n",
1736 ipv6_tests[i].address, terminator, ipv6_tests[i].address + ipv6_tests[i].terminator_offset);
1739 init_ip6(&expected_ip, ipv6_tests[i].ip);
1740 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
1741 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
1742 ipv6_tests[i].address,
1743 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
1744 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
1745 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
1746 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
1750 static void compare_RtlIpv6StringToAddressExW(PCSTR name_a, const struct in6_addr *addr_a, HRESULT res_a, ULONG scope_a, USHORT port_a)
1752 WCHAR name[512];
1753 NTSTATUS res;
1754 IN6_ADDR ip;
1755 ULONG scope = 0xbadf00d;
1756 USHORT port = 0xbeef;
1758 if (!pRtlIpv6StringToAddressExW)
1759 return;
1761 pRtlMultiByteToUnicodeN(name, sizeof(name), NULL, name_a, strlen(name_a) + 1);
1763 init_ip6(&ip, NULL);
1764 res = pRtlIpv6StringToAddressExW(name, &ip, &scope, &port);
1766 ok(res == res_a, "[W:%s] res = 0x%08x, expected 0x%08x\n", name_a, res, res_a);
1767 ok(scope == scope_a, "[W:%s] scope = 0x%08x, expected 0x%08x\n", name_a, scope, scope_a);
1768 ok(port == port_a, "[W:%s] port = 0x%08x, expected 0x%08x\n", name_a, port, port_a);
1770 ok(!memcmp(&ip, addr_a, sizeof(ip)),
1771 "[W:%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
1772 name_a,
1773 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
1774 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
1775 addr_a->s6_words[0], addr_a->s6_words[1], addr_a->s6_words[2], addr_a->s6_words[3],
1776 addr_a->s6_words[4], addr_a->s6_words[5], addr_a->s6_words[6], addr_a->s6_words[7]);
1779 static void test_RtlIpv6StringToAddressEx(void)
1781 NTSTATUS res;
1782 IN6_ADDR ip, expected_ip;
1783 ULONG scope;
1784 USHORT port;
1785 static const struct
1787 PCSTR address;
1788 NTSTATUS res;
1789 ULONG scope;
1790 USHORT port;
1791 int ip[8];
1792 } ipv6_ex_tests[] =
1794 { "[::]", STATUS_SUCCESS, 0, 0,
1795 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1796 { "[::1]:8080", STATUS_SUCCESS, 0, 0x901f,
1797 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1798 { "[::1]:0x80", STATUS_SUCCESS, 0, 0x8000,
1799 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1800 { "[::1]:0X80", STATUS_SUCCESS, 0, 0x8000,
1801 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1802 { "[::1]:080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1803 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1804 { "[::1]:800000000080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1805 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1806 { "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80", STATUS_SUCCESS, 0, 0x5000,
1807 { 0xdcfe, 0x98ba, 0x5476, 0x1032, 0xdcfe, 0x98ba, 0x5476, 0x1032 } },
1808 { "[1080:0:0:0:8:800:200C:417A]:1234", STATUS_SUCCESS, 0, 0xd204,
1809 { 0x8010, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1810 { "[3ffe:2a00:100:7031::1]:8080", STATUS_SUCCESS, 0, 0x901f,
1811 { 0xfe3f, 0x2a, 1, 0x3170, 0, 0, 0, 0x100 } },
1812 { "[ 3ffe:2a00:100:7031::1]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1813 { -1 } },
1814 { "[3ffe:2a00:100:7031::1 ]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1815 { 0xfe3f, 0x2a, 1, 0x3170, 0, 0, 0, 0x100 } },
1816 { "[3ffe:2a00:100:7031::1].8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1817 { 0xfe3f, 0x2a, 1, 0x3170, 0, 0, 0, 0x100 } },
1818 { "[1080::8:800:200C:417A]:8080", STATUS_SUCCESS, 0, 0x901f,
1819 { 0x8010, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1820 { "[1080::8:800:200C:417A]!8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1821 { 0x8010, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1822 { "[::FFFF:129.144.52.38]:80", STATUS_SUCCESS, 0, 0x5000,
1823 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1824 { "[::FFFF:129.144.52.38]:-80", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1825 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1826 { "[::FFFF:129.144.52.38]:999999999999", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1827 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1828 { "[::FFFF:129.144.52.38%-8]:80", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1829 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1830 { "[::FFFF:129.144.52.38]:80", STATUS_SUCCESS, 0, 0x5000,
1831 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1832 { "[12345::6:7:8]:80", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1833 { -1 } },
1834 { "[ff01::8:800:200C:417A%16]:8080", STATUS_SUCCESS, 16, 0x901f,
1835 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1836 { "[ff01::8:800:200C:417A%100]:8080", STATUS_SUCCESS, 100, 0x901f,
1837 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1838 { "[ff01::8:800:200C:417A%1000]:8080", STATUS_SUCCESS, 1000, 0x901f,
1839 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1840 { "[ff01::8:800:200C:417A%10000]:8080", STATUS_SUCCESS, 10000, 0x901f,
1841 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1842 { "[ff01::8:800:200C:417A%1000000]:8080", STATUS_SUCCESS, 1000000, 0x901f,
1843 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1844 { "[ff01::8:800:200C:417A%4294967295]:8080", STATUS_SUCCESS, 0xffffffff, 0x901f,
1845 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1846 { "[ff01::8:800:200C:417A%4294967296]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1847 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1848 { "[ff01::8:800:200C:417A%-1]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1849 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1850 { "[ff01::8:800:200C:417A%0]:8080", STATUS_SUCCESS, 0, 0x901f,
1851 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1852 { "[ff01::8:800:200C:417A%1", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1853 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1854 { "[ff01::8:800:200C:417A%0x1000]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1855 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1856 { "[ff01::8:800:200C:417A/16]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
1857 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
1859 const unsigned int ipv6_ex_testcount = sizeof(ipv6_ex_tests) / sizeof(ipv6_ex_tests[0]);
1860 const char *simple_ip = "::";
1861 unsigned int i;
1863 if (!pRtlIpv6StringToAddressExW)
1865 skip("RtlIpv6StringToAddressExW not available\n");
1866 /* we can continue, just not test W */
1869 if (!pRtlIpv6StringToAddressExA)
1871 skip("RtlIpv6StringToAddressExA not available\n");
1872 return;
1875 res = pRtlIpv6StringToAddressExA(simple_ip, &ip, &scope, &port);
1876 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08x, expected STATUS_SUCCESS\n", res);
1878 init_ip6(&ip, NULL);
1879 init_ip6(&expected_ip, NULL);
1880 scope = 0xbadf00d;
1881 port = 0xbeef;
1882 res = pRtlIpv6StringToAddressExA(NULL, &ip, &scope, &port);
1883 ok(res == STATUS_INVALID_PARAMETER,
1884 "[null string] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1885 ok(scope == 0xbadf00d, "[null string] scope = 0x%08x, expected 0xbadf00d\n", scope);
1886 ok(port == 0xbeef, "[null string] port = 0x%08x, expected 0xbeef\n", port);
1887 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
1888 "[null string] ip is changed, expected it not to change\n");
1891 init_ip6(&ip, NULL);
1892 scope = 0xbadf00d;
1893 port = 0xbeef;
1894 res = pRtlIpv6StringToAddressExA(simple_ip, NULL, &scope, &port);
1895 ok(res == STATUS_INVALID_PARAMETER,
1896 "[null result] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1897 ok(scope == 0xbadf00d, "[null result] scope = 0x%08x, expected 0xbadf00d\n", scope);
1898 ok(port == 0xbeef, "[null result] port = 0x%08x, expected 0xbeef\n", port);
1899 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
1900 "[null result] ip is changed, expected it not to change\n");
1902 init_ip6(&ip, NULL);
1903 scope = 0xbadf00d;
1904 port = 0xbeef;
1905 res = pRtlIpv6StringToAddressExA(simple_ip, &ip, NULL, &port);
1906 ok(res == STATUS_INVALID_PARAMETER,
1907 "[null scope] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1908 ok(scope == 0xbadf00d, "[null scope] scope = 0x%08x, expected 0xbadf00d\n", scope);
1909 ok(port == 0xbeef, "[null scope] port = 0x%08x, expected 0xbeef\n", port);
1910 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
1911 "[null scope] ip is changed, expected it not to change\n");
1913 init_ip6(&ip, NULL);
1914 scope = 0xbadf00d;
1915 port = 0xbeef;
1916 res = pRtlIpv6StringToAddressExA(simple_ip, &ip, &scope, NULL);
1917 ok(res == STATUS_INVALID_PARAMETER,
1918 "[null port] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1919 ok(scope == 0xbadf00d, "[null port] scope = 0x%08x, expected 0xbadf00d\n", scope);
1920 ok(port == 0xbeef, "[null port] port = 0x%08x, expected 0xbeef\n", port);
1921 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
1922 "[null port] ip is changed, expected it not to change\n");
1924 /* sanity check */
1925 ok(sizeof(ip) == sizeof(USHORT)* 8, "sizeof(ip)\n");
1927 /* first we run all ip related tests, to make sure someone didnt accidentally reimplement instead of re-use. */
1928 for (i = 0; i < ipv6_testcount; i++)
1930 ULONG scope = 0xbadf00d;
1931 USHORT port = 0xbeef;
1932 NTSTATUS expect_ret = (ipv6_tests[i].flags & ex_fail_6) ? STATUS_INVALID_PARAMETER : ipv6_tests[i].res;
1934 if (ipv6_tests[i].flags & ex_skip_6)
1935 continue;
1937 init_ip6(&ip, NULL);
1938 res = pRtlIpv6StringToAddressExA(ipv6_tests[i].address, &ip, &scope, &port);
1939 compare_RtlIpv6StringToAddressExW(ipv6_tests[i].address, &ip, res, scope, port);
1941 /* make sure nothing was changed if this function fails. */
1942 if (res == STATUS_INVALID_PARAMETER)
1944 ok(scope == 0xbadf00d, "[%s] scope = 0x%08x, expected 0xbadf00d\n",
1945 ipv6_tests[i].address, scope);
1946 ok(port == 0xbeef, "[%s] port = 0x%08x, expected 0xbeef\n",
1947 ipv6_tests[i].address, port);
1949 else
1951 ok(scope != 0xbadf00d, "[%s] scope = 0x%08x, not expected 0xbadf00d\n",
1952 ipv6_tests[i].address, scope);
1953 ok(port != 0xbeef, "[%s] port = 0x%08x, not expected 0xbeef\n",
1954 ipv6_tests[i].address, port);
1957 if (ipv6_tests[i].flags & win_broken_6)
1959 ok(res == expect_ret || broken(res == STATUS_INVALID_PARAMETER),
1960 "[%s] res = 0x%08x, expected 0x%08x\n", ipv6_tests[i].address, res, expect_ret);
1962 if (res == STATUS_INVALID_PARAMETER)
1963 continue;
1965 else
1967 ok(res == expect_ret, "[%s] res = 0x%08x, expected 0x%08x\n",
1968 ipv6_tests[i].address, res, expect_ret);
1971 /* If ex fails but non-ex does not we cannot check if the part that is converted
1972 before it failed was correct, since there is no data for it in the table. */
1973 if (res == expect_ret)
1975 init_ip6(&expected_ip, ipv6_tests[i].ip);
1976 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
1977 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
1978 ipv6_tests[i].address,
1979 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
1980 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
1981 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
1982 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
1986 /* now we run scope / port related tests */
1987 for (i = 0; i < ipv6_ex_testcount; i++)
1989 scope = 0xbadf00d;
1990 port = 0xbeef;
1991 init_ip6(&ip, NULL);
1992 res = pRtlIpv6StringToAddressExA(ipv6_ex_tests[i].address, &ip, &scope, &port);
1993 compare_RtlIpv6StringToAddressExW(ipv6_ex_tests[i].address, &ip, res, scope, port);
1995 ok(res == ipv6_ex_tests[i].res, "[%s] res = 0x%08x, expected 0x%08x\n",
1996 ipv6_ex_tests[i].address, res, ipv6_ex_tests[i].res);
1997 ok(scope == ipv6_ex_tests[i].scope, "[%s] scope = 0x%08x, expected 0x%08x\n",
1998 ipv6_ex_tests[i].address, scope, ipv6_ex_tests[i].scope);
1999 ok(port == ipv6_ex_tests[i].port, "[%s] port = 0x%08x, expected 0x%08x\n",
2000 ipv6_ex_tests[i].address, port, ipv6_ex_tests[i].port);
2002 init_ip6(&expected_ip, ipv6_ex_tests[i].ip);
2003 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2004 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
2005 ipv6_ex_tests[i].address,
2006 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
2007 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
2008 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
2009 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
2013 static void test_LdrAddRefDll(void)
2015 HMODULE mod, mod2;
2016 NTSTATUS status;
2017 BOOL ret;
2019 if (!pLdrAddRefDll)
2021 win_skip( "LdrAddRefDll not supported\n" );
2022 return;
2025 mod = LoadLibraryA("comctl32.dll");
2026 ok(mod != NULL, "got %p\n", mod);
2027 ret = FreeLibrary(mod);
2028 ok(ret, "got %d\n", ret);
2030 mod2 = GetModuleHandleA("comctl32.dll");
2031 ok(mod2 == NULL, "got %p\n", mod2);
2033 /* load, addref and release 2 times */
2034 mod = LoadLibraryA("comctl32.dll");
2035 ok(mod != NULL, "got %p\n", mod);
2036 status = pLdrAddRefDll(0, mod);
2037 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
2038 ret = FreeLibrary(mod);
2039 ok(ret, "got %d\n", ret);
2041 mod2 = GetModuleHandleA("comctl32.dll");
2042 ok(mod2 != NULL, "got %p\n", mod2);
2043 ret = FreeLibrary(mod);
2044 ok(ret, "got %d\n", ret);
2046 mod2 = GetModuleHandleA("comctl32.dll");
2047 ok(mod2 == NULL, "got %p\n", mod2);
2049 /* pin refcount */
2050 mod = LoadLibraryA("comctl32.dll");
2051 ok(mod != NULL, "got %p\n", mod);
2052 status = pLdrAddRefDll(LDR_ADDREF_DLL_PIN, mod);
2053 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
2055 ret = FreeLibrary(mod);
2056 ok(ret, "got %d\n", ret);
2057 ret = FreeLibrary(mod);
2058 ok(ret, "got %d\n", ret);
2059 ret = FreeLibrary(mod);
2060 ok(ret, "got %d\n", ret);
2061 ret = FreeLibrary(mod);
2062 ok(ret, "got %d\n", ret);
2064 mod2 = GetModuleHandleA("comctl32.dll");
2065 ok(mod2 != NULL, "got %p\n", mod2);
2068 static void test_LdrLockLoaderLock(void)
2070 ULONG_PTR magic;
2071 ULONG result;
2072 NTSTATUS status;
2074 if (!pLdrLockLoaderLock)
2076 win_skip("LdrLockLoaderLock() is not available\n");
2077 return;
2080 /* invalid flags */
2081 result = 10;
2082 magic = 0xdeadbeef;
2083 status = pLdrLockLoaderLock(0x10, &result, &magic);
2084 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
2085 ok(result == 0, "got %d\n", result);
2086 ok(magic == 0, "got %lx\n", magic);
2088 magic = 0xdeadbeef;
2089 status = pLdrLockLoaderLock(0x10, NULL, &magic);
2090 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
2091 ok(magic == 0, "got %lx\n", magic);
2093 result = 10;
2094 status = pLdrLockLoaderLock(0x10, &result, NULL);
2095 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
2096 ok(result == 0, "got %d\n", result);
2098 /* non-blocking mode, result is null */
2099 magic = 0xdeadbeef;
2100 status = pLdrLockLoaderLock(0x2, NULL, &magic);
2101 ok(status == STATUS_INVALID_PARAMETER_2, "got 0x%08x\n", status);
2102 ok(magic == 0, "got %lx\n", magic);
2104 /* magic pointer is null */
2105 result = 10;
2106 status = pLdrLockLoaderLock(0, &result, NULL);
2107 ok(status == STATUS_INVALID_PARAMETER_3, "got 0x%08x\n", status);
2108 ok(result == 0, "got %d\n", result);
2110 /* lock in non-blocking mode */
2111 result = 0;
2112 magic = 0;
2113 status = pLdrLockLoaderLock(0x2, &result, &magic);
2114 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
2115 ok(result == 1, "got %d\n", result);
2116 ok(magic != 0, "got %lx\n", magic);
2117 pLdrUnlockLoaderLock(0, magic);
2120 static void test_RtlCompressBuffer(void)
2122 ULONG compress_workspace, decompress_workspace;
2123 static const UCHAR test_buffer[] = "WineWineWine";
2124 static UCHAR buf1[0x1000], buf2[0x1000];
2125 ULONG final_size, buf_size;
2126 UCHAR *workspace = NULL;
2127 NTSTATUS status;
2129 if (!pRtlCompressBuffer || !pRtlDecompressBuffer || !pRtlGetCompressionWorkSpaceSize)
2131 win_skip("skipping RtlCompressBuffer tests, required functions not available\n");
2132 return;
2135 compress_workspace = decompress_workspace = 0xdeadbeef;
2136 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
2137 &decompress_workspace);
2138 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2139 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
2140 workspace = HeapAlloc(GetProcessHeap(), 0, compress_workspace);
2141 ok(workspace != NULL, "HeapAlloc failed %d\n", GetLastError());
2143 /* test compression format / engine */
2144 final_size = 0xdeadbeef;
2145 status = pRtlCompressBuffer(COMPRESSION_FORMAT_NONE, test_buffer, sizeof(test_buffer),
2146 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
2147 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2148 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2150 final_size = 0xdeadbeef;
2151 status = pRtlCompressBuffer(COMPRESSION_FORMAT_DEFAULT, test_buffer, sizeof(test_buffer),
2152 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
2153 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2154 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2156 final_size = 0xdeadbeef;
2157 status = pRtlCompressBuffer(0xFF, test_buffer, sizeof(test_buffer),
2158 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
2159 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
2160 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2162 /* test compression */
2163 final_size = 0xdeadbeef;
2164 memset(buf1, 0x11, sizeof(buf1));
2165 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
2166 buf1, sizeof(buf1), 4096, &final_size, workspace);
2167 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2168 ok((*(WORD *)buf1 & 0x7000) == 0x3000, "no chunk signature found %04x\n", *(WORD *)buf1);
2169 todo_wine
2170 ok(final_size < sizeof(test_buffer), "got wrong final_size %u\n", final_size);
2172 /* test decompression */
2173 buf_size = final_size;
2174 final_size = 0xdeadbeef;
2175 memset(buf2, 0x11, sizeof(buf2));
2176 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf2, sizeof(buf2),
2177 buf1, buf_size, &final_size);
2178 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2179 ok(final_size == sizeof(test_buffer), "got wrong final_size %u\n", final_size);
2180 ok(!memcmp(buf2, test_buffer, sizeof(test_buffer)), "got wrong decoded data\n");
2181 ok(buf2[sizeof(test_buffer)] == 0x11, "too many bytes written\n");
2183 /* buffer too small */
2184 final_size = 0xdeadbeef;
2185 memset(buf1, 0x11, sizeof(buf1));
2186 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
2187 buf1, 4, 4096, &final_size, workspace);
2188 ok(status == STATUS_BUFFER_TOO_SMALL, "got wrong status 0x%08x\n", status);
2190 HeapFree(GetProcessHeap(), 0, workspace);
2193 static void test_RtlGetCompressionWorkSpaceSize(void)
2195 ULONG compress_workspace, decompress_workspace;
2196 NTSTATUS status;
2198 if (!pRtlGetCompressionWorkSpaceSize)
2200 win_skip("RtlGetCompressionWorkSpaceSize is not available\n");
2201 return;
2204 /* test invalid format / engine */
2205 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_NONE, &compress_workspace,
2206 &decompress_workspace);
2207 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2209 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_DEFAULT, &compress_workspace,
2210 &decompress_workspace);
2211 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2213 status = pRtlGetCompressionWorkSpaceSize(0xFF, &compress_workspace, &decompress_workspace);
2214 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
2216 /* test LZNT1 with normal and maximum compression */
2217 compress_workspace = decompress_workspace = 0xdeadbeef;
2218 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
2219 &decompress_workspace);
2220 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2221 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
2222 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
2224 compress_workspace = decompress_workspace = 0xdeadbeef;
2225 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM,
2226 &compress_workspace, &decompress_workspace);
2227 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2228 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
2229 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
2232 /* helper for test_RtlDecompressBuffer, checks if a chunk is incomplete */
2233 static BOOL is_incomplete_chunk(const UCHAR *compressed, ULONG compressed_size, BOOL check_all)
2235 ULONG chunk_size;
2237 if (compressed_size <= sizeof(WORD))
2238 return TRUE;
2240 while (compressed_size >= sizeof(WORD))
2242 chunk_size = (*(WORD *)compressed & 0xFFF) + 1;
2243 if (compressed_size < sizeof(WORD) + chunk_size)
2244 return TRUE;
2245 if (!check_all)
2246 break;
2247 compressed += sizeof(WORD) + chunk_size;
2248 compressed_size -= sizeof(WORD) + chunk_size;
2251 return FALSE;
2254 #define DECOMPRESS_BROKEN_FRAGMENT 1 /* < Win 7 */
2255 #define DECOMPRESS_BROKEN_TRUNCATED 2 /* broken on all machines */
2257 static void test_RtlDecompressBuffer(void)
2259 static const struct
2261 UCHAR compressed[32];
2262 ULONG compressed_size;
2263 NTSTATUS status;
2264 UCHAR uncompressed[32];
2265 ULONG uncompressed_size;
2266 DWORD broken_flags;
2268 test_lznt[] =
2270 /* 4 byte uncompressed chunk */
2272 {0x03, 0x30, 'W', 'i', 'n', 'e'},
2274 STATUS_SUCCESS,
2275 "Wine",
2277 DECOMPRESS_BROKEN_FRAGMENT
2279 /* 8 byte uncompressed chunk */
2281 {0x07, 0x30, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
2283 STATUS_SUCCESS,
2284 "WineWine",
2286 DECOMPRESS_BROKEN_FRAGMENT
2288 /* 4 byte compressed chunk */
2290 {0x04, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
2292 STATUS_SUCCESS,
2293 "Wine",
2296 /* 8 byte compressed chunk */
2298 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
2300 STATUS_SUCCESS,
2301 "WineWine",
2304 /* compressed chunk using backwards reference */
2306 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x01, 0x30},
2308 STATUS_SUCCESS,
2309 "WineWine",
2311 DECOMPRESS_BROKEN_TRUNCATED
2313 /* compressed chunk using backwards reference with length > bytes_read */
2315 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x30},
2317 STATUS_SUCCESS,
2318 "WineWineWine",
2320 DECOMPRESS_BROKEN_TRUNCATED
2322 /* same as above, but unused bits != 0 */
2324 {0x06, 0xB0, 0x30, 'W', 'i', 'n', 'e', 0x01, 0x30},
2326 STATUS_SUCCESS,
2327 "WineWine",
2329 DECOMPRESS_BROKEN_TRUNCATED
2331 /* compressed chunk without backwards reference and unused bits != 0 */
2333 {0x01, 0xB0, 0x02, 'W'},
2335 STATUS_SUCCESS,
2336 "W",
2339 /* termination sequence after first chunk */
2341 {0x03, 0x30, 'W', 'i', 'n', 'e', 0x00, 0x00, 0x03, 0x30, 'W', 'i', 'n', 'e'},
2343 STATUS_SUCCESS,
2344 "Wine",
2346 DECOMPRESS_BROKEN_FRAGMENT
2348 /* compressed chunk using backwards reference with 4 bit offset, 12 bit length */
2350 {0x14, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
2351 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
2352 0x01, 0x01, 0xF0},
2354 STATUS_SUCCESS,
2355 "ABCDEFGHIJKLMNOPABCD",
2357 DECOMPRESS_BROKEN_TRUNCATED
2359 /* compressed chunk using backwards reference with 5 bit offset, 11 bit length */
2361 {0x15, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
2362 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
2363 0x02, 'A', 0x00, 0x78},
2365 STATUS_SUCCESS,
2366 "ABCDEFGHIJKLMNOPABCD",
2368 DECOMPRESS_BROKEN_TRUNCATED
2370 /* uncompressed chunk with invalid magic */
2372 {0x03, 0x20, 'W', 'i', 'n', 'e'},
2374 STATUS_SUCCESS,
2375 "Wine",
2377 DECOMPRESS_BROKEN_FRAGMENT
2379 /* compressed chunk with invalid magic */
2381 {0x04, 0xA0, 0x00, 'W', 'i', 'n', 'e'},
2383 STATUS_SUCCESS,
2384 "Wine",
2387 /* garbage byte after end of buffer */
2389 {0x00, 0xB0, 0x02, 0x01},
2391 STATUS_SUCCESS,
2395 /* empty compressed chunk */
2397 {0x00, 0xB0, 0x00},
2399 STATUS_SUCCESS,
2403 /* empty compressed chunk with unused bits != 0 */
2405 {0x00, 0xB0, 0x01},
2407 STATUS_SUCCESS,
2411 /* empty input buffer */
2415 STATUS_BAD_COMPRESSION_BUFFER,
2417 /* incomplete chunk header */
2419 {0x01},
2421 STATUS_BAD_COMPRESSION_BUFFER
2423 /* incomplete chunk header */
2425 {0x00, 0x30},
2427 STATUS_BAD_COMPRESSION_BUFFER
2429 /* compressed chunk with invalid backwards reference */
2431 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x40},
2433 STATUS_BAD_COMPRESSION_BUFFER
2435 /* compressed chunk with incomplete backwards reference */
2437 {0x05, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05},
2439 STATUS_BAD_COMPRESSION_BUFFER
2441 /* incomplete uncompressed chunk */
2443 {0x07, 0x30, 'W', 'i', 'n', 'e'},
2445 STATUS_BAD_COMPRESSION_BUFFER
2447 /* incomplete compressed chunk */
2449 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
2451 STATUS_BAD_COMPRESSION_BUFFER
2453 /* two compressed chunks, the second one incomplete */
2455 {0x00, 0xB0, 0x02, 0x00, 0xB0},
2457 STATUS_BAD_COMPRESSION_BUFFER,
2461 static UCHAR buf[0x2000], workspace[0x1000];
2462 NTSTATUS status, expected_status;
2463 ULONG final_size;
2464 int i;
2466 if (!pRtlDecompressBuffer || !pRtlDecompressFragment)
2468 win_skip("RtlDecompressBuffer or RtlDecompressFragment is not available\n");
2469 return;
2472 /* test compression format / engine */
2473 final_size = 0xdeadbeef;
2474 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_NONE, buf, sizeof(buf), test_lznt[0].compressed,
2475 test_lznt[0].compressed_size, &final_size);
2476 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2477 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2479 final_size = 0xdeadbeef;
2480 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_DEFAULT, buf, sizeof(buf), test_lznt[0].compressed,
2481 test_lznt[0].compressed_size, &final_size);
2482 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2483 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2485 final_size = 0xdeadbeef;
2486 status = pRtlDecompressBuffer(0xFF, buf, sizeof(buf), test_lznt[0].compressed,
2487 test_lznt[0].compressed_size, &final_size);
2488 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
2489 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2491 /* regular tests for RtlDecompressBuffer */
2492 for (i = 0; i < ARRAY_SIZE(test_lznt); i++)
2494 trace("Running test %d (compressed_size=%u, uncompressed_size=%u, status=0x%08x)\n",
2495 i, test_lznt[i].compressed_size, test_lznt[i].uncompressed_size, test_lznt[i].status);
2497 /* test with very big buffer */
2498 final_size = 0xdeadbeef;
2499 memset(buf, 0x11, sizeof(buf));
2500 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2501 test_lznt[i].compressed_size, &final_size);
2502 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
2503 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08x\n", i, status);
2504 if (!status)
2506 ok(final_size == test_lznt[i].uncompressed_size,
2507 "%d: got wrong final_size %u\n", i, final_size);
2508 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2509 "%d: got wrong decoded data\n", i);
2510 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2511 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
2514 /* test that modifier for compression engine is ignored */
2515 final_size = 0xdeadbeef;
2516 memset(buf, 0x11, sizeof(buf));
2517 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM, buf, sizeof(buf),
2518 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
2519 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
2520 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08x\n", i, status);
2521 if (!status)
2523 ok(final_size == test_lznt[i].uncompressed_size,
2524 "%d: got wrong final_size %u\n", i, final_size);
2525 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2526 "%d: got wrong decoded data\n", i);
2527 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2528 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
2531 /* test with expected output size */
2532 if (test_lznt[i].uncompressed_size > 0)
2534 final_size = 0xdeadbeef;
2535 memset(buf, 0x11, sizeof(buf));
2536 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size,
2537 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
2538 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
2539 if (!status)
2541 ok(final_size == test_lznt[i].uncompressed_size,
2542 "%d: got wrong final_size %u\n", i, final_size);
2543 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2544 "%d: got wrong decoded data\n", i);
2545 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2546 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
2550 /* test with smaller output size */
2551 if (test_lznt[i].uncompressed_size > 1)
2553 final_size = 0xdeadbeef;
2554 memset(buf, 0x11, sizeof(buf));
2555 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size - 1,
2556 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
2557 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_TRUNCATED)
2558 todo_wine
2559 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
2560 else
2561 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
2562 if (!status)
2564 ok(final_size == test_lznt[i].uncompressed_size - 1,
2565 "%d: got wrong final_size %u\n", i, final_size);
2566 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size - 1),
2567 "%d: got wrong decoded data\n", i);
2568 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
2569 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size - 1);
2573 /* test with zero output size */
2574 final_size = 0xdeadbeef;
2575 memset(buf, 0x11, sizeof(buf));
2576 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, 0, test_lznt[i].compressed,
2577 test_lznt[i].compressed_size, &final_size);
2578 if (is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, FALSE))
2579 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
2580 else
2582 ok(status == STATUS_SUCCESS, "%d: got wrong status 0x%08x\n", i, status);
2583 ok(final_size == 0, "%d: got wrong final_size %u\n", i, final_size);
2584 ok(buf[0] == 0x11, "%d: buf[0] was modified\n", i);
2587 /* test RtlDecompressFragment with offset = 0 */
2588 final_size = 0xdeadbeef;
2589 memset(buf, 0x11, sizeof(buf));
2590 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2591 test_lznt[i].compressed_size, 0, &final_size, workspace);
2592 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
2593 todo_wine
2594 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
2595 else
2596 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
2597 if (!status)
2599 ok(final_size == test_lznt[i].uncompressed_size,
2600 "%d: got wrong final_size %u\n", i, final_size);
2601 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2602 "%d: got wrong decoded data\n", i);
2603 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2604 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
2607 /* test RtlDecompressFragment with offset = 1 */
2608 final_size = 0xdeadbeef;
2609 memset(buf, 0x11, sizeof(buf));
2610 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2611 test_lznt[i].compressed_size, 1, &final_size, workspace);
2612 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
2613 todo_wine
2614 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
2615 else
2616 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
2617 if (!status)
2619 if (test_lznt[i].uncompressed_size == 0)
2621 todo_wine
2622 ok(final_size == 4095, "%d: got wrong final_size %u\n", i, final_size);
2623 /* Buffer doesn't contain any useful value on Windows */
2624 ok(buf[4095] == 0x11, "%d: buf[4095] was modified\n", i);
2626 else
2628 ok(final_size == test_lznt[i].uncompressed_size - 1,
2629 "%d: got wrong final_size %u\n", i, final_size);
2630 ok(!memcmp(buf, test_lznt[i].uncompressed + 1, test_lznt[i].uncompressed_size - 1),
2631 "%d: got wrong decoded data\n", i);
2632 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
2633 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size - 1);
2637 /* test RtlDecompressFragment with offset = 4095 */
2638 final_size = 0xdeadbeef;
2639 memset(buf, 0x11, sizeof(buf));
2640 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2641 test_lznt[i].compressed_size, 4095, &final_size, workspace);
2642 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
2643 todo_wine
2644 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
2645 else
2646 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
2647 if (!status)
2649 todo_wine
2650 ok(final_size == 1, "%d: got wrong final_size %u\n", i, final_size);
2651 todo_wine
2652 ok(buf[0] == 0, "%d: padding is not zero\n", i);
2653 ok(buf[1] == 0x11, "%d: buf[1] was modified\n", i);
2656 /* test RtlDecompressFragment with offset = 4096 */
2657 final_size = 0xdeadbeef;
2658 memset(buf, 0x11, sizeof(buf));
2659 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2660 test_lznt[i].compressed_size, 4096, &final_size, workspace);
2661 expected_status = is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, TRUE) ?
2662 test_lznt[i].status : STATUS_SUCCESS;
2663 ok(status == expected_status, "%d: got wrong status 0x%08x, expected 0x%08x\n", i, status, expected_status);
2664 if (!status)
2666 ok(final_size == 0, "%d: got wrong final_size %u\n", i, final_size);
2667 ok(buf[0] == 0x11, "%d: buf[4096] was modified\n", i);
2672 #undef DECOMPRESS_BROKEN_FRAGMENT
2673 #undef DECOMPRESS_BROKEN_TRUNCATED
2675 struct critsect_locked_info
2677 CRITICAL_SECTION crit;
2678 HANDLE semaphores[2];
2681 static DWORD WINAPI critsect_locked_thread(void *param)
2683 struct critsect_locked_info *info = param;
2684 DWORD ret;
2686 ret = pRtlIsCriticalSectionLocked(&info->crit);
2687 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2688 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
2689 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2691 ReleaseSemaphore(info->semaphores[0], 1, NULL);
2692 ret = WaitForSingleObject(info->semaphores[1], 1000);
2693 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2695 ret = pRtlIsCriticalSectionLocked(&info->crit);
2696 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2697 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
2698 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2700 EnterCriticalSection(&info->crit);
2702 ret = pRtlIsCriticalSectionLocked(&info->crit);
2703 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2704 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
2705 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2707 ReleaseSemaphore(info->semaphores[0], 1, NULL);
2708 ret = WaitForSingleObject(info->semaphores[1], 1000);
2709 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2711 LeaveCriticalSection(&info->crit);
2712 return 0;
2715 static void test_RtlIsCriticalSectionLocked(void)
2717 struct critsect_locked_info info;
2718 HANDLE thread;
2719 BOOL ret;
2721 if (!pRtlIsCriticalSectionLocked || !pRtlIsCriticalSectionLockedByThread)
2723 win_skip("skipping RtlIsCriticalSectionLocked tests, required functions not available\n");
2724 return;
2727 InitializeCriticalSection(&info.crit);
2728 info.semaphores[0] = CreateSemaphoreW(NULL, 0, 1, NULL);
2729 ok(info.semaphores[0] != NULL, "CreateSemaphore failed with %u\n", GetLastError());
2730 info.semaphores[1] = CreateSemaphoreW(NULL, 0, 1, NULL);
2731 ok(info.semaphores[1] != NULL, "CreateSemaphore failed with %u\n", GetLastError());
2733 ret = pRtlIsCriticalSectionLocked(&info.crit);
2734 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2735 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2736 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2738 EnterCriticalSection(&info.crit);
2740 ret = pRtlIsCriticalSectionLocked(&info.crit);
2741 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2742 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2743 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2745 thread = CreateThread(NULL, 0, critsect_locked_thread, &info, 0, NULL);
2746 ok(thread != NULL, "CreateThread failed with %u\n", GetLastError());
2747 ret = WaitForSingleObject(info.semaphores[0], 1000);
2748 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2750 LeaveCriticalSection(&info.crit);
2752 ReleaseSemaphore(info.semaphores[1], 1, NULL);
2753 ret = WaitForSingleObject(info.semaphores[0], 1000);
2754 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2756 ret = pRtlIsCriticalSectionLocked(&info.crit);
2757 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2758 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2759 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2761 ReleaseSemaphore(info.semaphores[1], 1, NULL);
2762 ret = WaitForSingleObject(thread, 1000);
2763 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2765 CloseHandle(thread);
2766 CloseHandle(info.semaphores[0]);
2767 CloseHandle(info.semaphores[1]);
2768 DeleteCriticalSection(&info.crit);
2771 static void test_RtlInitializeCriticalSectionEx(void)
2773 static const CRITICAL_SECTION_DEBUG *no_debug = (void *)~(ULONG_PTR)0;
2774 CRITICAL_SECTION cs;
2776 if (!pRtlInitializeCriticalSectionEx)
2778 win_skip("RtlInitializeCriticalSectionEx is not available\n");
2779 return;
2782 memset(&cs, 0x11, sizeof(cs));
2783 pRtlInitializeCriticalSectionEx(&cs, 0, 0);
2784 ok((cs.DebugInfo != NULL && cs.DebugInfo != no_debug) || broken(cs.DebugInfo == no_debug) /* >= Win 8 */,
2785 "expected DebugInfo != NULL and DebugInfo != ~0, got %p\n", cs.DebugInfo);
2786 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2787 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2788 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
2789 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
2790 "expected SpinCount == 0, got %ld\n", cs.SpinCount);
2791 RtlDeleteCriticalSection(&cs);
2793 memset(&cs, 0x11, sizeof(cs));
2794 pRtlInitializeCriticalSectionEx(&cs, 0, RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO);
2795 todo_wine
2796 ok(cs.DebugInfo == no_debug, "expected DebugInfo == ~0, got %p\n", cs.DebugInfo);
2797 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2798 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2799 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
2800 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
2801 "expected SpinCount == 0, got %ld\n", cs.SpinCount);
2802 RtlDeleteCriticalSection(&cs);
2805 static void test_RtlLeaveCriticalSection(void)
2807 RTL_CRITICAL_SECTION cs;
2808 NTSTATUS status;
2810 if (!pRtlInitializeCriticalSectionEx)
2811 return; /* Skip winxp */
2813 status = RtlInitializeCriticalSection(&cs);
2814 ok(!status, "RtlInitializeCriticalSection failed: %x\n", status);
2816 status = RtlEnterCriticalSection(&cs);
2817 ok(!status, "RtlEnterCriticalSection failed: %x\n", status);
2818 todo_wine
2819 ok(cs.LockCount == -2, "expected LockCount == -2, got %d\n", cs.LockCount);
2820 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %d\n", cs.RecursionCount);
2821 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
2823 status = RtlLeaveCriticalSection(&cs);
2824 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2825 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2826 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2827 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2830 * Trying to leave a section that wasn't acquired modifies RecursionCount to an invalid value,
2831 * but doesn't modify LockCount so that an attempt to enter the section later will work.
2833 status = RtlLeaveCriticalSection(&cs);
2834 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2835 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2836 ok(cs.RecursionCount == -1, "expected RecursionCount == -1, got %d\n", cs.RecursionCount);
2837 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2839 /* and again */
2840 status = RtlLeaveCriticalSection(&cs);
2841 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2842 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2843 ok(cs.RecursionCount == -2, "expected RecursionCount == -2, got %d\n", cs.RecursionCount);
2844 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2846 /* entering section fixes RecursionCount */
2847 status = RtlEnterCriticalSection(&cs);
2848 ok(!status, "RtlEnterCriticalSection failed: %x\n", status);
2849 todo_wine
2850 ok(cs.LockCount == -2, "expected LockCount == -2, got %d\n", cs.LockCount);
2851 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %d\n", cs.RecursionCount);
2852 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
2854 status = RtlLeaveCriticalSection(&cs);
2855 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2856 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2857 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2858 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2860 status = RtlDeleteCriticalSection(&cs);
2861 ok(!status, "RtlDeleteCriticalSection failed: %x\n", status);
2864 struct ldr_enum_context
2866 BOOL abort;
2867 BOOL found;
2868 int count;
2871 static void WINAPI ldr_enum_callback(LDR_MODULE *module, void *context, BOOLEAN *stop)
2873 static const WCHAR ntdllW[] = {'n','t','d','l','l','.','d','l','l',0};
2874 struct ldr_enum_context *ctx = context;
2876 if (!lstrcmpiW(module->BaseDllName.Buffer, ntdllW))
2877 ctx->found = TRUE;
2879 ctx->count++;
2880 *stop = ctx->abort;
2883 static void test_LdrEnumerateLoadedModules(void)
2885 struct ldr_enum_context ctx;
2886 NTSTATUS status;
2888 if (!pLdrEnumerateLoadedModules)
2890 win_skip("LdrEnumerateLoadedModules not available\n");
2891 return;
2894 ctx.abort = FALSE;
2895 ctx.found = FALSE;
2896 ctx.count = 0;
2897 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
2898 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08x\n", status);
2899 ok(ctx.count > 1, "Expected more than one module, got %d\n", ctx.count);
2900 ok(ctx.found, "Could not find ntdll in list of modules\n");
2902 ctx.abort = TRUE;
2903 ctx.count = 0;
2904 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
2905 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08x\n", status);
2906 ok(ctx.count == 1, "Expected exactly one module, got %d\n", ctx.count);
2908 status = pLdrEnumerateLoadedModules((void *)0x1, ldr_enum_callback, (void *)0xdeadbeef);
2909 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
2911 status = pLdrEnumerateLoadedModules((void *)0xdeadbeef, ldr_enum_callback, (void *)0xdeadbeef);
2912 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
2914 status = pLdrEnumerateLoadedModules(NULL, NULL, (void *)0xdeadbeef);
2915 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
2918 static void test_RtlMakeSelfRelativeSD(void)
2920 char buf[sizeof(SECURITY_DESCRIPTOR_RELATIVE) + 4];
2921 SECURITY_DESCRIPTOR_RELATIVE *sd_rel = (SECURITY_DESCRIPTOR_RELATIVE *)buf;
2922 SECURITY_DESCRIPTOR sd;
2923 NTSTATUS status;
2924 DWORD len;
2926 if (!pRtlMakeSelfRelativeSD || !pRtlAbsoluteToSelfRelativeSD)
2928 win_skip( "RtlMakeSelfRelativeSD/RtlAbsoluteToSelfRelativeSD not available\n" );
2929 return;
2932 memset( &sd, 0, sizeof(sd) );
2933 sd.Revision = SECURITY_DESCRIPTOR_REVISION;
2935 len = 0;
2936 status = pRtlMakeSelfRelativeSD( &sd, NULL, &len );
2937 ok( status == STATUS_BUFFER_TOO_SMALL, "got %08x\n", status );
2938 ok( len == sizeof(*sd_rel), "got %u\n", len );
2940 len += 4;
2941 status = pRtlMakeSelfRelativeSD( &sd, sd_rel, &len );
2942 ok( status == STATUS_SUCCESS, "got %08x\n", status );
2943 ok( len == sizeof(*sd_rel) + 4, "got %u\n", len );
2945 len = 0;
2946 status = pRtlAbsoluteToSelfRelativeSD( &sd, NULL, &len );
2947 ok( status == STATUS_BUFFER_TOO_SMALL, "got %08x\n", status );
2948 ok( len == sizeof(*sd_rel), "got %u\n", len );
2950 len += 4;
2951 status = pRtlAbsoluteToSelfRelativeSD( &sd, sd_rel, &len );
2952 ok( status == STATUS_SUCCESS, "got %08x\n", status );
2953 ok( len == sizeof(*sd_rel) + 4, "got %u\n", len );
2955 sd.Control = SE_SELF_RELATIVE;
2956 status = pRtlMakeSelfRelativeSD( &sd, sd_rel, &len );
2957 ok( status == STATUS_SUCCESS, "got %08x\n", status );
2958 ok( len == sizeof(*sd_rel) + 4, "got %u\n", len );
2960 status = pRtlAbsoluteToSelfRelativeSD( &sd, sd_rel, &len );
2961 ok( status == STATUS_BAD_DESCRIPTOR_FORMAT, "got %08x\n", status );
2964 static DWORD (CALLBACK *orig_entry)(HMODULE,DWORD,LPVOID);
2965 static DWORD *dll_main_data;
2967 static inline void *get_rva( HMODULE module, DWORD va )
2969 return (void *)((char *)module + va);
2972 static void CALLBACK ldr_notify_callback1(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
2974 const IMAGE_IMPORT_DESCRIPTOR *imports;
2975 const IMAGE_THUNK_DATA *import_list;
2976 IMAGE_THUNK_DATA *thunk_list;
2977 DWORD *calls = context;
2978 LIST_ENTRY *mark;
2979 LDR_MODULE *mod;
2980 ULONG size;
2981 int i, j;
2983 *calls <<= 4;
2984 *calls |= reason;
2986 if (!lstrcmpiW(data->Loaded.BaseDllName->Buffer, expected_dll))
2987 return;
2989 ok(data->Loaded.Flags == 0, "Expected flags 0, got %x\n", data->Loaded.Flags);
2990 ok(!lstrcmpiW(data->Loaded.BaseDllName->Buffer, expected_dll), "Expected %s, got %s\n",
2991 wine_dbgstr_w(expected_dll), wine_dbgstr_w(data->Loaded.BaseDllName->Buffer));
2992 ok(!!data->Loaded.DllBase, "Expected non zero base address\n");
2993 ok(data->Loaded.SizeOfImage, "Expected non zero image size\n");
2995 /* expect module to be last module listed in LdrData load order list */
2996 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
2997 mod = CONTAINING_RECORD(mark->Blink, LDR_MODULE, InMemoryOrderModuleList);
2998 ok(mod->BaseAddress == data->Loaded.DllBase, "Expected base address %p, got %p\n",
2999 data->Loaded.DllBase, mod->BaseAddress);
3000 ok(!lstrcmpiW(mod->BaseDllName.Buffer, expected_dll), "Expected %s, got %s\n",
3001 wine_dbgstr_w(expected_dll), wine_dbgstr_w(mod->BaseDllName.Buffer));
3003 /* show that imports have already been resolved */
3004 imports = RtlImageDirectoryEntryToData(data->Loaded.DllBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size);
3005 ok(!!imports, "Expected dll to have imports\n");
3007 for (i = 0; imports[i].Name; i++)
3009 thunk_list = get_rva(data->Loaded.DllBase, (DWORD)imports[i].FirstThunk);
3010 if (imports[i].OriginalFirstThunk)
3011 import_list = get_rva(data->Loaded.DllBase, (DWORD)imports[i].OriginalFirstThunk);
3012 else
3013 import_list = thunk_list;
3015 for (j = 0; import_list[j].u1.Ordinal; j++)
3017 ok(thunk_list[j].u1.AddressOfData > data->Loaded.SizeOfImage,
3018 "Import has not been resolved: %p\n", (void*)thunk_list[j].u1.Function);
3023 static void CALLBACK ldr_notify_callback2(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3025 DWORD *calls = context;
3026 *calls <<= 4;
3027 *calls |= reason + 2;
3030 static BOOL WINAPI fake_dll_main(HINSTANCE instance, DWORD reason, void* reserved)
3032 if (reason == DLL_PROCESS_ATTACH)
3034 *dll_main_data <<= 4;
3035 *dll_main_data |= 3;
3037 else if (reason == DLL_PROCESS_DETACH)
3039 *dll_main_data <<= 4;
3040 *dll_main_data |= 4;
3042 return orig_entry(instance, reason, reserved);
3045 static void CALLBACK ldr_notify_callback_dll_main(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3047 DWORD *calls = context;
3048 LIST_ENTRY *mark;
3049 LDR_MODULE *mod;
3051 *calls <<= 4;
3052 *calls |= reason;
3054 if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
3055 return;
3057 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
3058 mod = CONTAINING_RECORD(mark->Blink, LDR_MODULE, InMemoryOrderModuleList);
3059 ok(mod->BaseAddress == data->Loaded.DllBase, "Expected base address %p, got %p\n",
3060 data->Loaded.DllBase, mod->BaseAddress);
3061 if (mod->BaseAddress != data->Loaded.DllBase)
3062 return;
3064 orig_entry = mod->EntryPoint;
3065 mod->EntryPoint = fake_dll_main;
3066 dll_main_data = calls;
3069 static BOOL WINAPI fake_dll_main_fail(HINSTANCE instance, DWORD reason, void* reserved)
3071 if (reason == DLL_PROCESS_ATTACH)
3073 *dll_main_data <<= 4;
3074 *dll_main_data |= 3;
3076 else if (reason == DLL_PROCESS_DETACH)
3078 *dll_main_data <<= 4;
3079 *dll_main_data |= 4;
3081 return FALSE;
3084 static void CALLBACK ldr_notify_callback_fail(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3086 DWORD *calls = context;
3087 LIST_ENTRY *mark;
3088 LDR_MODULE *mod;
3090 *calls <<= 4;
3091 *calls |= reason;
3093 if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
3094 return;
3096 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
3097 mod = CONTAINING_RECORD(mark->Blink, LDR_MODULE, InMemoryOrderModuleList);
3098 ok(mod->BaseAddress == data->Loaded.DllBase, "Expected base address %p, got %p\n",
3099 data->Loaded.DllBase, mod->BaseAddress);
3100 if (mod->BaseAddress != data->Loaded.DllBase)
3101 return;
3103 orig_entry = mod->EntryPoint;
3104 mod->EntryPoint = fake_dll_main_fail;
3105 dll_main_data = calls;
3108 static void CALLBACK ldr_notify_callback_imports(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3110 DWORD *calls = context;
3112 if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
3113 return;
3115 if (!lstrcmpiW(data->Loaded.BaseDllName->Buffer, crypt32dllW))
3117 *calls <<= 4;
3118 *calls |= 1;
3121 if (!lstrcmpiW(data->Loaded.BaseDllName->Buffer, wintrustdllW))
3123 *calls <<= 4;
3124 *calls |= 2;
3128 static void test_LdrRegisterDllNotification(void)
3130 void *cookie, *cookie2;
3131 NTSTATUS status;
3132 HMODULE mod;
3133 DWORD calls;
3135 if (!pLdrRegisterDllNotification || !pLdrUnregisterDllNotification)
3137 win_skip("Ldr(Un)RegisterDllNotification not available\n");
3138 return;
3141 mod = LoadLibraryW(expected_dll);
3142 if(mod)
3143 FreeLibrary(mod);
3144 else
3145 expected_dll = ws2_32dllW; /* XP Default */
3147 /* generic test */
3148 status = pLdrRegisterDllNotification(0, ldr_notify_callback1, &calls, &cookie);
3149 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3151 calls = 0;
3152 mod = LoadLibraryW(expected_dll);
3153 ok(!!mod, "Failed to load library: %d\n", GetLastError());
3154 ok(calls == LDR_DLL_NOTIFICATION_REASON_LOADED, "Expected LDR_DLL_NOTIFICATION_REASON_LOADED, got %x\n", calls);
3156 calls = 0;
3157 FreeLibrary(mod);
3158 ok(calls == LDR_DLL_NOTIFICATION_REASON_UNLOADED, "Expected LDR_DLL_NOTIFICATION_REASON_UNLOADED, got %x\n", calls);
3160 /* test order of callbacks */
3161 status = pLdrRegisterDllNotification(0, ldr_notify_callback2, &calls, &cookie2);
3162 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3164 calls = 0;
3165 mod = LoadLibraryW(expected_dll);
3166 ok(!!mod, "Failed to load library: %d\n", GetLastError());
3167 ok(calls == 0x13, "Expected order 0x13, got %x\n", calls);
3169 calls = 0;
3170 FreeLibrary(mod);
3171 ok(calls == 0x24, "Expected order 0x24, got %x\n", calls);
3173 pLdrUnregisterDllNotification(cookie2);
3174 pLdrUnregisterDllNotification(cookie);
3176 /* test dll main order */
3177 status = pLdrRegisterDllNotification(0, ldr_notify_callback_dll_main, &calls, &cookie);
3178 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3180 calls = 0;
3181 mod = LoadLibraryW(expected_dll);
3182 ok(!!mod, "Failed to load library: %d\n", GetLastError());
3183 ok(calls == 0x13, "Expected order 0x13, got %x\n", calls);
3185 calls = 0;
3186 FreeLibrary(mod);
3187 ok(calls == 0x42, "Expected order 0x42, got %x\n", calls);
3189 pLdrUnregisterDllNotification(cookie);
3191 /* test dll main order */
3192 status = pLdrRegisterDllNotification(0, ldr_notify_callback_fail, &calls, &cookie);
3193 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3195 calls = 0;
3196 mod = LoadLibraryW(expected_dll);
3197 ok(!mod, "Expected library to fail loading\n");
3198 ok(calls == 0x1342, "Expected order 0x1342, got %x\n", calls);
3200 pLdrUnregisterDllNotification(cookie);
3202 /* test dll with dependencies */
3203 status = pLdrRegisterDllNotification(0, ldr_notify_callback_imports, &calls, &cookie);
3204 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3206 calls = 0;
3207 mod = LoadLibraryW(wintrustdllW);
3208 ok(!!mod, "Failed to load library: %d\n", GetLastError());
3209 ok(calls == 0x12 || calls == 0x21, "got %x\n", calls);
3211 FreeLibrary(mod);
3212 pLdrUnregisterDllNotification(cookie);
3215 START_TEST(rtl)
3217 InitFunctionPtrs();
3219 test_RtlCompareMemory();
3220 test_RtlCompareMemoryUlong();
3221 test_RtlMoveMemory();
3222 test_RtlFillMemory();
3223 test_RtlFillMemoryUlong();
3224 test_RtlZeroMemory();
3225 test_RtlUlonglongByteSwap();
3226 test_RtlUniform();
3227 test_RtlRandom();
3228 test_RtlAreAllAccessesGranted();
3229 test_RtlAreAnyAccessesGranted();
3230 test_RtlComputeCrc32();
3231 test_HandleTables();
3232 test_RtlAllocateAndInitializeSid();
3233 test_RtlDeleteTimer();
3234 test_RtlThreadErrorMode();
3235 test_LdrProcessRelocationBlock();
3236 test_RtlIpv4AddressToString();
3237 test_RtlIpv4AddressToStringEx();
3238 test_RtlIpv4StringToAddress();
3239 test_RtlIpv6StringToAddress();
3240 test_RtlIpv6StringToAddressEx();
3241 test_LdrAddRefDll();
3242 test_LdrLockLoaderLock();
3243 test_RtlCompressBuffer();
3244 test_RtlGetCompressionWorkSpaceSize();
3245 test_RtlDecompressBuffer();
3246 test_RtlIsCriticalSectionLocked();
3247 test_RtlInitializeCriticalSectionEx();
3248 test_RtlLeaveCriticalSection();
3249 test_LdrEnumerateLoadedModules();
3250 test_RtlMakeSelfRelativeSD();
3251 test_LdrRegisterDllNotification();