bcrypt: Require macOS version 10.7 or newer for encryption support.
[wine.git] / dlls / ntdll / tests / rtl.c
blob2e3a7de75d24446fc346d595e009d5c5f68ef476
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 "inaddr.h"
29 #ifndef __WINE_WINTERNL_H
31 typedef struct _RTL_HANDLE
33 struct _RTL_HANDLE * Next;
34 } RTL_HANDLE;
36 typedef struct _RTL_HANDLE_TABLE
38 ULONG MaxHandleCount;
39 ULONG HandleSize;
40 ULONG Unused[2];
41 PVOID NextFree;
42 PVOID FirstHandle;
43 PVOID ReservedMemory;
44 PVOID MaxHandle;
45 } RTL_HANDLE_TABLE;
47 #endif
49 /* avoid #include <winsock2.h> */
50 #undef htons
51 #ifdef WORDS_BIGENDIAN
52 #define htons(s) ((USHORT)(s))
53 #else /* WORDS_BIGENDIAN */
54 static inline USHORT __my_ushort_swap(USHORT s)
56 return (s >> 8) | (s << 8);
58 #define htons(s) __my_ushort_swap(s)
59 #endif /* WORDS_BIGENDIAN */
63 /* Function ptrs for ntdll calls */
64 static HMODULE hntdll = 0;
65 static SIZE_T (WINAPI *pRtlCompareMemory)(LPCVOID,LPCVOID,SIZE_T);
66 static SIZE_T (WINAPI *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);
67 static NTSTATUS (WINAPI *pRtlDeleteTimer)(HANDLE, HANDLE, HANDLE);
68 static VOID (WINAPI *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);
69 static VOID (WINAPI *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);
70 static VOID (WINAPI *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);
71 static VOID (WINAPI *pRtlZeroMemory)(LPVOID,SIZE_T);
72 static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);
73 static ULONG (WINAPI *pRtlUniform)(PULONG);
74 static ULONG (WINAPI *pRtlRandom)(PULONG);
75 static BOOLEAN (WINAPI *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
76 static BOOLEAN (WINAPI *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
77 static DWORD (WINAPI *pRtlComputeCrc32)(DWORD,const BYTE*,INT);
78 static void (WINAPI * pRtlInitializeHandleTable)(ULONG, ULONG, RTL_HANDLE_TABLE *);
79 static BOOLEAN (WINAPI * pRtlIsValidIndexHandle)(const RTL_HANDLE_TABLE *, ULONG, RTL_HANDLE **);
80 static NTSTATUS (WINAPI * pRtlDestroyHandleTable)(RTL_HANDLE_TABLE *);
81 static RTL_HANDLE * (WINAPI * pRtlAllocateHandle)(RTL_HANDLE_TABLE *, ULONG *);
82 static BOOLEAN (WINAPI * pRtlFreeHandle)(RTL_HANDLE_TABLE *, RTL_HANDLE *);
83 static NTSTATUS (WINAPI *pRtlAllocateAndInitializeSid)(PSID_IDENTIFIER_AUTHORITY,BYTE,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,PSID*);
84 static NTSTATUS (WINAPI *pRtlFreeSid)(PSID);
85 static DWORD (WINAPI *pRtlGetThreadErrorMode)(void);
86 static NTSTATUS (WINAPI *pRtlSetThreadErrorMode)(DWORD, LPDWORD);
87 static IMAGE_BASE_RELOCATION *(WINAPI *pLdrProcessRelocationBlock)(void*,UINT,USHORT*,INT_PTR);
88 static CHAR * (WINAPI *pRtlIpv4AddressToStringA)(const IN_ADDR *, LPSTR);
89 static NTSTATUS (WINAPI *pRtlIpv4AddressToStringExA)(const IN_ADDR *, USHORT, LPSTR, PULONG);
90 static NTSTATUS (WINAPI *pRtlIpv4StringToAddressA)(PCSTR, BOOLEAN, PCSTR *, IN_ADDR *);
91 static NTSTATUS (WINAPI *pLdrAddRefDll)(ULONG, HMODULE);
92 static NTSTATUS (WINAPI *pLdrLockLoaderLock)(ULONG, ULONG*, ULONG_PTR*);
93 static NTSTATUS (WINAPI *pLdrUnlockLoaderLock)(ULONG, ULONG_PTR);
94 static NTSTATUS (WINAPI *pRtlGetCompressionWorkSpaceSize)(USHORT, PULONG, PULONG);
95 static NTSTATUS (WINAPI *pRtlDecompressBuffer)(USHORT, PUCHAR, ULONG, const UCHAR*, ULONG, PULONG);
96 static NTSTATUS (WINAPI *pRtlDecompressFragment)(USHORT, PUCHAR, ULONG, const UCHAR*, ULONG, ULONG, PULONG, PVOID);
97 static NTSTATUS (WINAPI *pRtlCompressBuffer)(USHORT, const UCHAR*, ULONG, PUCHAR, ULONG, ULONG, PULONG, PVOID);
98 static BOOL (WINAPI *pRtlIsCriticalSectionLocked)(CRITICAL_SECTION *);
99 static BOOL (WINAPI *pRtlIsCriticalSectionLockedByThread)(CRITICAL_SECTION *);
100 static NTSTATUS (WINAPI *pRtlInitializeCriticalSectionEx)(CRITICAL_SECTION *, ULONG, ULONG);
101 static NTSTATUS (WINAPI *pLdrEnumerateLoadedModules)(void *, void *, void *);
102 static NTSTATUS (WINAPI *pRtlMakeSelfRelativeSD)(PSECURITY_DESCRIPTOR,PSECURITY_DESCRIPTOR,LPDWORD);
103 static NTSTATUS (WINAPI *pRtlAbsoluteToSelfRelativeSD)(PSECURITY_DESCRIPTOR,PSECURITY_DESCRIPTOR,PULONG);
105 static HMODULE hkernel32 = 0;
106 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
109 #define LEN 16
110 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
111 static ULONG src_aligned_block[4];
112 static ULONG dest_aligned_block[32];
113 static const char *src = (const char*)src_aligned_block;
114 static char* dest = (char*)dest_aligned_block;
116 static void InitFunctionPtrs(void)
118 hntdll = LoadLibraryA("ntdll.dll");
119 ok(hntdll != 0, "LoadLibrary failed\n");
120 if (hntdll) {
121 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
122 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
123 pRtlDeleteTimer = (void *)GetProcAddress(hntdll, "RtlDeleteTimer");
124 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
125 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
126 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
127 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
128 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
129 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
130 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
131 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
132 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
133 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
134 pRtlInitializeHandleTable = (void *)GetProcAddress(hntdll, "RtlInitializeHandleTable");
135 pRtlIsValidIndexHandle = (void *)GetProcAddress(hntdll, "RtlIsValidIndexHandle");
136 pRtlDestroyHandleTable = (void *)GetProcAddress(hntdll, "RtlDestroyHandleTable");
137 pRtlAllocateHandle = (void *)GetProcAddress(hntdll, "RtlAllocateHandle");
138 pRtlFreeHandle = (void *)GetProcAddress(hntdll, "RtlFreeHandle");
139 pRtlAllocateAndInitializeSid = (void *)GetProcAddress(hntdll, "RtlAllocateAndInitializeSid");
140 pRtlFreeSid = (void *)GetProcAddress(hntdll, "RtlFreeSid");
141 pRtlGetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlGetThreadErrorMode");
142 pRtlSetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlSetThreadErrorMode");
143 pLdrProcessRelocationBlock = (void *)GetProcAddress(hntdll, "LdrProcessRelocationBlock");
144 pRtlIpv4AddressToStringA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringA");
145 pRtlIpv4AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringExA");
146 pRtlIpv4StringToAddressA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressA");
147 pLdrAddRefDll = (void *)GetProcAddress(hntdll, "LdrAddRefDll");
148 pLdrLockLoaderLock = (void *)GetProcAddress(hntdll, "LdrLockLoaderLock");
149 pLdrUnlockLoaderLock = (void *)GetProcAddress(hntdll, "LdrUnlockLoaderLock");
150 pRtlGetCompressionWorkSpaceSize = (void *)GetProcAddress(hntdll, "RtlGetCompressionWorkSpaceSize");
151 pRtlDecompressBuffer = (void *)GetProcAddress(hntdll, "RtlDecompressBuffer");
152 pRtlDecompressFragment = (void *)GetProcAddress(hntdll, "RtlDecompressFragment");
153 pRtlCompressBuffer = (void *)GetProcAddress(hntdll, "RtlCompressBuffer");
154 pRtlIsCriticalSectionLocked = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLocked");
155 pRtlIsCriticalSectionLockedByThread = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLockedByThread");
156 pRtlInitializeCriticalSectionEx = (void *)GetProcAddress(hntdll, "RtlInitializeCriticalSectionEx");
157 pLdrEnumerateLoadedModules = (void *)GetProcAddress(hntdll, "LdrEnumerateLoadedModules");
158 pRtlMakeSelfRelativeSD = (void *)GetProcAddress(hntdll, "RtlMakeSelfRelativeSD");
159 pRtlAbsoluteToSelfRelativeSD = (void *)GetProcAddress(hntdll, "RtlAbsoluteToSelfRelativeSD");
161 hkernel32 = LoadLibraryA("kernel32.dll");
162 ok(hkernel32 != 0, "LoadLibrary failed\n");
163 if (hkernel32) {
164 pIsWow64Process = (void *)GetProcAddress(hkernel32, "IsWow64Process");
166 strcpy((char*)src_aligned_block, src_src);
167 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
170 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
171 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
173 static void test_RtlCompareMemory(void)
175 SIZE_T size;
177 if (!pRtlCompareMemory)
179 win_skip("RtlCompareMemory is not available\n");
180 return;
183 strcpy(dest, src);
185 COMP(src,src,0,0);
186 COMP(src,src,LEN,LEN);
187 dest[0] = 'x';
188 COMP(src,dest,LEN,0);
191 static void test_RtlCompareMemoryUlong(void)
193 ULONG a[10];
194 ULONG result;
196 if (!pRtlCompareMemoryUlong)
198 win_skip("RtlCompareMemoryUlong is not available\n");
199 return;
202 a[0]= 0x0123;
203 a[1]= 0x4567;
204 a[2]= 0x89ab;
205 a[3]= 0xcdef;
206 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
207 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %u, expected 0\n", a, result);
208 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
209 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
210 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
211 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
212 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
213 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
214 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
215 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
216 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
217 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 4\n", a, result);
218 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
219 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 4\n", a, result);
220 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
221 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %u, expected 0\n", a, result);
222 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
223 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %u, expected 0\n", a, result);
224 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
225 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %u, expected 0\n", a, result);
227 a[1]= 0x0123;
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 == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 8\n", a, result);
238 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
239 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 8\n", a, result);
242 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
243 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
245 static void test_RtlMoveMemory(void)
247 if (!pRtlMoveMemory)
249 win_skip("RtlMoveMemory is not available\n");
250 return;
253 /* Length should be in bytes and not rounded. Use strcmp to ensure we
254 * didn't write past the end (it checks for the final NUL left by memset)
256 COPY(0); CMP("");
257 COPY(1); CMP("T");
258 COPY(2); CMP("Th");
259 COPY(3); CMP("Thi");
260 COPY(4); CMP("This");
261 COPY(5); CMP("This ");
262 COPY(6); CMP("This i");
263 COPY(7); CMP("This is");
264 COPY(8); CMP("This is ");
265 COPY(9); CMP("This is a");
267 /* Overlapping */
268 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
269 CMP("his is a test!!");
270 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
271 CMP("TThis is a test!");
274 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
276 static void test_RtlFillMemory(void)
278 if (!pRtlFillMemory)
280 win_skip("RtlFillMemory is not available\n");
281 return;
284 /* Length should be in bytes and not rounded. Use strcmp to ensure we
285 * didn't write past the end (the remainder of the string should match)
287 FILL(0); CMP("This is a test!");
288 FILL(1); CMP("xhis is a test!");
289 FILL(2); CMP("xxis is a test!");
290 FILL(3); CMP("xxxs is a test!");
291 FILL(4); CMP("xxxx is a test!");
292 FILL(5); CMP("xxxxxis a test!");
293 FILL(6); CMP("xxxxxxs a test!");
294 FILL(7); CMP("xxxxxxx a test!");
295 FILL(8); CMP("xxxxxxxxa test!");
296 FILL(9); CMP("xxxxxxxxx test!");
299 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
301 static void test_RtlFillMemoryUlong(void)
303 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
304 if (!pRtlFillMemoryUlong)
306 win_skip("RtlFillMemoryUlong is not available\n");
307 return;
310 /* Length should be in bytes and not rounded. Use strcmp to ensure we
311 * didn't write past the end (the remainder of the string should match)
313 LFILL(0); CMP("This is a test!");
314 LFILL(1); CMP("This is a test!");
315 LFILL(2); CMP("This is a test!");
316 LFILL(3); CMP("This is a test!");
317 LFILL(4); CMP("xxxx is a test!");
318 LFILL(5); CMP("xxxx is a test!");
319 LFILL(6); CMP("xxxx is a test!");
320 LFILL(7); CMP("xxxx is a test!");
321 LFILL(8); CMP("xxxxxxxxa test!");
322 LFILL(9); CMP("xxxxxxxxa test!");
325 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
326 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
328 static void test_RtlZeroMemory(void)
330 if (!pRtlZeroMemory)
332 win_skip("RtlZeroMemory is not available\n");
333 return;
336 /* Length should be in bytes and not rounded. */
337 ZERO(0); MCMP("This is a test!");
338 ZERO(1); MCMP("\0his is a test!");
339 ZERO(2); MCMP("\0\0is is a test!");
340 ZERO(3); MCMP("\0\0\0s is a test!");
341 ZERO(4); MCMP("\0\0\0\0 is a test!");
342 ZERO(5); MCMP("\0\0\0\0\0is a test!");
343 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
344 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
345 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
346 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
349 static void test_RtlUlonglongByteSwap(void)
351 ULONGLONG result;
353 if ( !pRtlUlonglongByteSwap )
355 win_skip("RtlUlonglongByteSwap is not available\n");
356 return;
359 if ( pRtlUlonglongByteSwap( 0 ) != 0 )
361 win_skip("Broken RtlUlonglongByteSwap in win2k\n");
362 return;
365 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
366 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
367 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%s, expected 0x2143658710325476\n",
368 wine_dbgstr_longlong(result));
372 static void test_RtlUniform(void)
374 ULONGLONG num;
375 ULONG seed;
376 ULONG seed_bak;
377 ULONG expected;
378 ULONG result;
380 if (!pRtlUniform)
382 win_skip("RtlUniform is not available\n");
383 return;
387 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
388 * algorithm. This algorithm is:
390 * seed = (seed * const_1 + const_2) % const_3;
392 * According to the documentation the random number is distributed over
393 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
395 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
397 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
398 * algorithm can be expressed without division as:
400 * seed = (seed * const_1 + const_2) & MAXLONG;
402 * To find out const_2 we just call RtlUniform with seed set to 0:
404 seed = 0;
405 expected = 0x7fffffc3;
406 result = pRtlUniform(&seed);
407 ok(result == expected,
408 "RtlUniform(&seed (seed == 0)) returns %x, expected %x\n",
409 result, expected);
411 * The algorithm is now:
413 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
415 * To find out const_1 we can use:
417 * const_1 = RtlUniform(1) - 0x7fffffc3;
419 * If that does not work a search loop can try all possible values of
420 * const_1 and compare to the result to RtlUniform(1).
421 * This way we find out that const_1 is 0xffffffed.
423 * For seed = 1 the const_2 is 0x7fffffc4:
425 seed = 1;
426 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
427 result = pRtlUniform(&seed);
428 ok(result == expected,
429 "RtlUniform(&seed (seed == 1)) returns %x, expected %x\n",
430 result, expected);
432 * For seed = 2 the const_2 is 0x7fffffc3:
434 seed = 2;
435 expected = seed * 0xffffffed + 0x7fffffc3;
436 result = pRtlUniform(&seed);
439 * Windows Vista uses different algorithms, so skip the rest of the tests
440 * until that is figured out. Trace output for the failures is about 10.5 MB!
443 if (result == 0x7fffff9f) {
444 skip("Most likely running on Windows Vista which uses a different algorithm\n");
445 return;
448 ok(result == expected,
449 "RtlUniform(&seed (seed == 2)) returns %x, expected %x\n",
450 result, expected);
453 * More tests show that if seed is odd the result must be incremented by 1:
455 seed = 3;
456 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
457 result = pRtlUniform(&seed);
458 ok(result == expected,
459 "RtlUniform(&seed (seed == 3)) returns %x, expected %x\n",
460 result, expected);
462 seed = 0x6bca1aa;
463 expected = seed * 0xffffffed + 0x7fffffc3;
464 result = pRtlUniform(&seed);
465 ok(result == expected,
466 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %x, expected %x\n",
467 result, expected);
469 seed = 0x6bca1ab;
470 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
471 result = pRtlUniform(&seed);
472 ok(result == expected,
473 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %x, expected %x\n",
474 result, expected);
476 * When seed is 0x6bca1ac there is an exception:
478 seed = 0x6bca1ac;
479 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
480 result = pRtlUniform(&seed);
481 ok(result == expected,
482 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %x, expected %x\n",
483 result, expected);
485 * Note that up to here const_3 is not used
486 * (the highest bit of the result is not set).
488 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
490 seed = 0x6bca1ad;
491 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
492 result = pRtlUniform(&seed);
493 ok(result == expected,
494 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %x, expected %x\n",
495 result, expected);
497 seed = 0x6bca1ae;
498 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
499 result = pRtlUniform(&seed);
500 ok(result == expected,
501 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %x, expected %x\n",
502 result, expected);
504 * There are several ranges where for odd or even seed the result must be
505 * incremented by 1. You can see this ranges in the following test.
507 * For a full test use one of the following loop heads:
509 * for (num = 0; num <= 0xffffffff; num++) {
510 * seed = num;
511 * ...
513 * seed = 0;
514 * for (num = 0; num <= 0xffffffff; num++) {
515 * ...
517 seed = 0;
518 for (num = 0; num <= 100000; num++) {
520 expected = seed * 0xffffffed + 0x7fffffc3;
521 if (seed < 0x6bca1ac) {
522 expected = expected + (seed & 1);
523 } else if (seed == 0x6bca1ac) {
524 expected = (expected + 2) & MAXLONG;
525 } else if (seed < 0xd79435c) {
526 expected = (expected + (~seed & 1)) & MAXLONG;
527 } else if (seed < 0x1435e50b) {
528 expected = expected + (seed & 1);
529 } else if (seed < 0x1af286ba) {
530 expected = (expected + (~seed & 1)) & MAXLONG;
531 } else if (seed < 0x21af2869) {
532 expected = expected + (seed & 1);
533 } else if (seed < 0x286bca18) {
534 expected = (expected + (~seed & 1)) & MAXLONG;
535 } else if (seed < 0x2f286bc7) {
536 expected = expected + (seed & 1);
537 } else if (seed < 0x35e50d77) {
538 expected = (expected + (~seed & 1)) & MAXLONG;
539 } else if (seed < 0x3ca1af26) {
540 expected = expected + (seed & 1);
541 } else if (seed < 0x435e50d5) {
542 expected = (expected + (~seed & 1)) & MAXLONG;
543 } else if (seed < 0x4a1af284) {
544 expected = expected + (seed & 1);
545 } else if (seed < 0x50d79433) {
546 expected = (expected + (~seed & 1)) & MAXLONG;
547 } else if (seed < 0x579435e2) {
548 expected = expected + (seed & 1);
549 } else if (seed < 0x5e50d792) {
550 expected = (expected + (~seed & 1)) & MAXLONG;
551 } else if (seed < 0x650d7941) {
552 expected = expected + (seed & 1);
553 } else if (seed < 0x6bca1af0) {
554 expected = (expected + (~seed & 1)) & MAXLONG;
555 } else if (seed < 0x7286bc9f) {
556 expected = expected + (seed & 1);
557 } else if (seed < 0x79435e4e) {
558 expected = (expected + (~seed & 1)) & MAXLONG;
559 } else if (seed < 0x7ffffffd) {
560 expected = expected + (seed & 1);
561 } else if (seed < 0x86bca1ac) {
562 expected = (expected + (~seed & 1)) & MAXLONG;
563 } else if (seed == 0x86bca1ac) {
564 expected = (expected + 1) & MAXLONG;
565 } else if (seed < 0x8d79435c) {
566 expected = expected + (seed & 1);
567 } else if (seed < 0x9435e50b) {
568 expected = (expected + (~seed & 1)) & MAXLONG;
569 } else if (seed < 0x9af286ba) {
570 expected = expected + (seed & 1);
571 } else if (seed < 0xa1af2869) {
572 expected = (expected + (~seed & 1)) & MAXLONG;
573 } else if (seed < 0xa86bca18) {
574 expected = expected + (seed & 1);
575 } else if (seed < 0xaf286bc7) {
576 expected = (expected + (~seed & 1)) & MAXLONG;
577 } else if (seed == 0xaf286bc7) {
578 expected = (expected + 2) & MAXLONG;
579 } else if (seed < 0xb5e50d77) {
580 expected = expected + (seed & 1);
581 } else if (seed < 0xbca1af26) {
582 expected = (expected + (~seed & 1)) & MAXLONG;
583 } else if (seed < 0xc35e50d5) {
584 expected = expected + (seed & 1);
585 } else if (seed < 0xca1af284) {
586 expected = (expected + (~seed & 1)) & MAXLONG;
587 } else if (seed < 0xd0d79433) {
588 expected = expected + (seed & 1);
589 } else if (seed < 0xd79435e2) {
590 expected = (expected + (~seed & 1)) & MAXLONG;
591 } else if (seed < 0xde50d792) {
592 expected = expected + (seed & 1);
593 } else if (seed < 0xe50d7941) {
594 expected = (expected + (~seed & 1)) & MAXLONG;
595 } else if (seed < 0xebca1af0) {
596 expected = expected + (seed & 1);
597 } else if (seed < 0xf286bc9f) {
598 expected = (expected + (~seed & 1)) & MAXLONG;
599 } else if (seed < 0xf9435e4e) {
600 expected = expected + (seed & 1);
601 } else if (seed < 0xfffffffd) {
602 expected = (expected + (~seed & 1)) & MAXLONG;
603 } else {
604 expected = expected + (seed & 1);
605 } /* if */
606 seed_bak = seed;
607 result = pRtlUniform(&seed);
608 ok(result == expected,
609 "test: 0x%s RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
610 wine_dbgstr_longlong(num), seed_bak, result, expected);
611 ok(seed == expected,
612 "test: 0x%s RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
613 wine_dbgstr_longlong(num), seed_bak, result, expected);
614 } /* for */
616 * Further investigation shows: In the different regions the highest bit
617 * is set or cleared when even or odd seeds need an increment by 1.
618 * This leads to a simplified algorithm:
620 * seed = seed * 0xffffffed + 0x7fffffc3;
621 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
622 * seed = (seed + 2) & MAXLONG;
623 * } else if (seed == 0x7fffffff) {
624 * seed = 0;
625 * } else if ((seed & 0x80000000) == 0) {
626 * seed = seed + (~seed & 1);
627 * } else {
628 * seed = (seed + (seed & 1)) & MAXLONG;
631 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
633 * Now comes the funny part:
634 * It took me one weekend, to find the complicated algorithm and one day more,
635 * to find the simplified algorithm. Several weeks later I found out: The value
636 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
637 * nor with the simplified algorithm. In reality the native function and our
638 * function return a random number distributed over [0..MAXLONG-1]. Note
639 * that this is different from what native documentation states [0..MAXLONG].
640 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
642 * seed = (seed * const_1 + const_2) % MAXLONG;
644 * Further investigations show that the real algorithm is:
646 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
648 * This is checked with the test below:
650 seed = 0;
651 for (num = 0; num <= 100000; num++) {
652 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
653 seed_bak = seed;
654 result = pRtlUniform(&seed);
655 ok(result == expected,
656 "test: 0x%s RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
657 wine_dbgstr_longlong(num), seed_bak, result, expected);
658 ok(seed == expected,
659 "test: 0x%s RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
660 wine_dbgstr_longlong(num), seed_bak, result, expected);
661 } /* for */
663 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
664 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
665 * that there is more than one cycle of generated randon numbers ...
670 static void test_RtlRandom(void)
672 int i, j;
673 ULONG seed;
674 ULONG res[512];
676 if (!pRtlRandom)
678 win_skip("RtlRandom is not available\n");
679 return;
682 seed = 0;
683 for (i = 0; i < sizeof(res) / sizeof(res[0]); i++)
685 res[i] = pRtlRandom(&seed);
686 ok(seed != res[i], "%i: seed is same as res %x\n", i, seed);
687 for (j = 0; j < i; j++)
688 ok(res[i] != res[j], "res[%i] (%x) is same as res[%i] (%x)\n", j, res[j], i, res[i]);
693 typedef struct {
694 ACCESS_MASK GrantedAccess;
695 ACCESS_MASK DesiredAccess;
696 BOOLEAN result;
697 } all_accesses_t;
699 static const all_accesses_t all_accesses[] = {
700 {0xFEDCBA76, 0xFEDCBA76, 1},
701 {0x00000000, 0xFEDCBA76, 0},
702 {0xFEDCBA76, 0x00000000, 1},
703 {0x00000000, 0x00000000, 1},
704 {0xFEDCBA76, 0xFEDCBA70, 1},
705 {0xFEDCBA70, 0xFEDCBA76, 0},
706 {0xFEDCBA76, 0xFEDC8A76, 1},
707 {0xFEDC8A76, 0xFEDCBA76, 0},
708 {0xFEDCBA76, 0xC8C4B242, 1},
709 {0xC8C4B242, 0xFEDCBA76, 0},
711 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
714 static void test_RtlAreAllAccessesGranted(void)
716 unsigned int test_num;
717 BOOLEAN result;
719 if (!pRtlAreAllAccessesGranted)
721 win_skip("RtlAreAllAccessesGranted is not available\n");
722 return;
725 for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
726 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
727 all_accesses[test_num].DesiredAccess);
728 ok(all_accesses[test_num].result == result,
729 "(test %d): RtlAreAllAccessesGranted(%08x, %08x) returns %d, expected %d\n",
730 test_num, all_accesses[test_num].GrantedAccess,
731 all_accesses[test_num].DesiredAccess,
732 result, all_accesses[test_num].result);
733 } /* for */
737 typedef struct {
738 ACCESS_MASK GrantedAccess;
739 ACCESS_MASK DesiredAccess;
740 BOOLEAN result;
741 } any_accesses_t;
743 static const any_accesses_t any_accesses[] = {
744 {0xFEDCBA76, 0xFEDCBA76, 1},
745 {0x00000000, 0xFEDCBA76, 0},
746 {0xFEDCBA76, 0x00000000, 0},
747 {0x00000000, 0x00000000, 0},
748 {0xFEDCBA76, 0x01234589, 0},
749 {0x00040000, 0xFEDCBA76, 1},
750 {0x00040000, 0xFED8BA76, 0},
751 {0xFEDCBA76, 0x00040000, 1},
752 {0xFED8BA76, 0x00040000, 0},
754 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
757 static void test_RtlAreAnyAccessesGranted(void)
759 unsigned int test_num;
760 BOOLEAN result;
762 if (!pRtlAreAnyAccessesGranted)
764 win_skip("RtlAreAnyAccessesGranted is not available\n");
765 return;
768 for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
769 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
770 any_accesses[test_num].DesiredAccess);
771 ok(any_accesses[test_num].result == result,
772 "(test %d): RtlAreAnyAccessesGranted(%08x, %08x) returns %d, expected %d\n",
773 test_num, any_accesses[test_num].GrantedAccess,
774 any_accesses[test_num].DesiredAccess,
775 result, any_accesses[test_num].result);
776 } /* for */
779 static void test_RtlComputeCrc32(void)
781 DWORD crc = 0;
783 if (!pRtlComputeCrc32)
785 win_skip("RtlComputeCrc32 is not available\n");
786 return;
789 crc = pRtlComputeCrc32(crc, (const BYTE *)src, LEN);
790 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8x\n", crc);
794 typedef struct MY_HANDLE
796 RTL_HANDLE RtlHandle;
797 void * MyValue;
798 } MY_HANDLE;
800 static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
802 ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
803 *AllocatedBit = *AllocatedBit | 1;
806 static void test_HandleTables(void)
808 BOOLEAN result;
809 NTSTATUS status;
810 ULONG Index;
811 MY_HANDLE * MyHandle;
812 RTL_HANDLE_TABLE HandleTable;
814 if (!pRtlInitializeHandleTable)
816 win_skip("RtlInitializeHandleTable is not available\n");
817 return;
820 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
821 MyHandle = (MY_HANDLE *)pRtlAllocateHandle(&HandleTable, &Index);
822 ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
823 RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
824 MyHandle = NULL;
825 result = pRtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
826 ok(result, "Handle %p wasn't valid\n", MyHandle);
827 result = pRtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
828 ok(result, "Couldn't free handle %p\n", MyHandle);
829 status = pRtlDestroyHandleTable(&HandleTable);
830 ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08x\n", status);
833 static void test_RtlAllocateAndInitializeSid(void)
835 NTSTATUS ret;
836 SID_IDENTIFIER_AUTHORITY sia = {{ 1, 2, 3, 4, 5, 6 }};
837 PSID psid;
839 if (!pRtlAllocateAndInitializeSid)
841 win_skip("RtlAllocateAndInitializeSid is not available\n");
842 return;
845 ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
846 ok(!ret, "RtlAllocateAndInitializeSid error %08x\n", ret);
847 ret = pRtlFreeSid(psid);
848 ok(!ret, "RtlFreeSid error %08x\n", ret);
850 /* these tests crash on XP */
851 if (0)
853 pRtlAllocateAndInitializeSid(NULL, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
854 pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL);
857 ret = pRtlAllocateAndInitializeSid(&sia, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
858 ok(ret == STATUS_INVALID_SID, "wrong error %08x\n", ret);
861 static void test_RtlDeleteTimer(void)
863 NTSTATUS ret;
865 if (!pRtlDeleteTimer)
867 win_skip("RtlDeleteTimer is not available\n");
868 return;
871 ret = pRtlDeleteTimer(NULL, NULL, NULL);
872 ok(ret == STATUS_INVALID_PARAMETER_1 ||
873 ret == STATUS_INVALID_PARAMETER, /* W2K */
874 "expected STATUS_INVALID_PARAMETER_1 or STATUS_INVALID_PARAMETER, got %x\n", ret);
877 static void test_RtlThreadErrorMode(void)
879 DWORD oldmode;
880 BOOL is_wow64;
881 DWORD mode;
882 NTSTATUS status;
884 if (!pRtlGetThreadErrorMode || !pRtlSetThreadErrorMode)
886 win_skip("RtlGetThreadErrorMode and/or RtlSetThreadErrorMode not available\n");
887 return;
890 if (!pIsWow64Process || !pIsWow64Process(GetCurrentProcess(), &is_wow64))
891 is_wow64 = FALSE;
893 oldmode = pRtlGetThreadErrorMode();
895 status = pRtlSetThreadErrorMode(0x70, &mode);
896 ok(status == STATUS_SUCCESS ||
897 status == STATUS_WAIT_1, /* Vista */
898 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
899 ok(mode == oldmode,
900 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
901 mode, oldmode);
902 ok(pRtlGetThreadErrorMode() == 0x70,
903 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0x70);
904 if (!is_wow64)
906 ok(NtCurrentTeb()->HardErrorDisabled == 0x70,
907 "The TEB contains 0x%x, expected 0x%x\n",
908 NtCurrentTeb()->HardErrorDisabled, 0x70);
911 status = pRtlSetThreadErrorMode(0, &mode);
912 ok(status == STATUS_SUCCESS ||
913 status == STATUS_WAIT_1, /* Vista */
914 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
915 ok(mode == 0x70,
916 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
917 mode, 0x70);
918 ok(pRtlGetThreadErrorMode() == 0,
919 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0);
920 if (!is_wow64)
922 ok(NtCurrentTeb()->HardErrorDisabled == 0,
923 "The TEB contains 0x%x, expected 0x%x\n",
924 NtCurrentTeb()->HardErrorDisabled, 0);
927 for (mode = 1; mode; mode <<= 1)
929 status = pRtlSetThreadErrorMode(mode, NULL);
930 if (mode & 0x70)
931 ok(status == STATUS_SUCCESS ||
932 status == STATUS_WAIT_1, /* Vista */
933 "RtlSetThreadErrorMode(%x,NULL) failed with error 0x%08x\n",
934 mode, status);
935 else
936 ok(status == STATUS_INVALID_PARAMETER_1,
937 "RtlSetThreadErrorMode(%x,NULL) returns 0x%08x, "
938 "expected STATUS_INVALID_PARAMETER_1\n",
939 mode, status);
942 pRtlSetThreadErrorMode(oldmode, NULL);
945 static void test_LdrProcessRelocationBlock(void)
947 IMAGE_BASE_RELOCATION *ret;
948 USHORT reloc;
949 DWORD addr32;
950 SHORT addr16;
952 if(!pLdrProcessRelocationBlock) {
953 win_skip("LdrProcessRelocationBlock not available\n");
954 return;
957 addr32 = 0x50005;
958 reloc = IMAGE_REL_BASED_HIGHLOW<<12;
959 ret = pLdrProcessRelocationBlock(&addr32, 1, &reloc, 0x500050);
960 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
961 ok(addr32 == 0x550055, "addr32 = %x, expected 0x550055\n", addr32);
963 addr16 = 0x505;
964 reloc = IMAGE_REL_BASED_HIGH<<12;
965 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
966 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
967 ok(addr16 == 0x555, "addr16 = %x, expected 0x555\n", addr16);
969 addr16 = 0x505;
970 reloc = IMAGE_REL_BASED_LOW<<12;
971 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
972 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
973 ok(addr16 == 0x565, "addr16 = %x, expected 0x565\n", addr16);
976 static void test_RtlIpv4AddressToString(void)
978 CHAR buffer[20];
979 CHAR *res;
980 IN_ADDR ip;
981 DWORD_PTR len;
983 if (!pRtlIpv4AddressToStringA)
985 win_skip("RtlIpv4AddressToStringA not available\n");
986 return;
989 ip.S_un.S_un_b.s_b1 = 1;
990 ip.S_un.S_un_b.s_b2 = 2;
991 ip.S_un.S_un_b.s_b3 = 3;
992 ip.S_un.S_un_b.s_b4 = 4;
994 memset(buffer, '#', sizeof(buffer) - 1);
995 buffer[sizeof(buffer) -1] = 0;
996 res = pRtlIpv4AddressToStringA(&ip, buffer);
997 len = strlen(buffer);
998 ok(res == (buffer + len), "got %p with '%s' (expected %p)\n", res, buffer, buffer + len);
1000 res = pRtlIpv4AddressToStringA(&ip, NULL);
1001 ok( (res == (char *)~0) ||
1002 broken(res == (char *)len), /* XP and w2003 */
1003 "got %p (expected ~0)\n", res);
1005 if (0) {
1006 /* this crashes in windows */
1007 memset(buffer, '#', sizeof(buffer) - 1);
1008 buffer[sizeof(buffer) -1] = 0;
1009 res = pRtlIpv4AddressToStringA(NULL, buffer);
1010 trace("got %p with '%s'\n", res, buffer);
1013 if (0) {
1014 /* this crashes in windows */
1015 res = pRtlIpv4AddressToStringA(NULL, NULL);
1016 trace("got %p\n", res);
1020 static void test_RtlIpv4AddressToStringEx(void)
1022 CHAR ip_1234[] = "1.2.3.4";
1023 CHAR ip_1234_80[] = "1.2.3.4:80";
1024 LPSTR expect;
1025 CHAR buffer[30];
1026 NTSTATUS res;
1027 IN_ADDR ip;
1028 ULONG size;
1029 DWORD used;
1030 USHORT port;
1032 if (!pRtlIpv4AddressToStringExA)
1034 win_skip("RtlIpv4AddressToStringExA not available\n");
1035 return;
1038 ip.S_un.S_un_b.s_b1 = 1;
1039 ip.S_un.S_un_b.s_b2 = 2;
1040 ip.S_un.S_un_b.s_b3 = 3;
1041 ip.S_un.S_un_b.s_b4 = 4;
1043 port = htons(80);
1044 expect = ip_1234_80;
1046 size = sizeof(buffer);
1047 memset(buffer, '#', sizeof(buffer) - 1);
1048 buffer[sizeof(buffer) -1] = 0;
1049 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1050 used = strlen(buffer);
1051 ok( (res == STATUS_SUCCESS) &&
1052 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1053 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1055 size = used + 1;
1056 memset(buffer, '#', sizeof(buffer) - 1);
1057 buffer[sizeof(buffer) -1] = 0;
1058 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1059 ok( (res == STATUS_SUCCESS) &&
1060 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1061 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1063 size = used;
1064 memset(buffer, '#', sizeof(buffer) - 1);
1065 buffer[sizeof(buffer) -1] = 0;
1066 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1067 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1068 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1069 res, size, buffer, used + 1);
1071 size = used - 1;
1072 memset(buffer, '#', sizeof(buffer) - 1);
1073 buffer[sizeof(buffer) -1] = 0;
1074 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1075 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1076 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1077 res, size, buffer, used + 1);
1080 /* to get only the ip, use 0 as port */
1081 port = 0;
1082 expect = ip_1234;
1084 size = sizeof(buffer);
1085 memset(buffer, '#', sizeof(buffer) - 1);
1086 buffer[sizeof(buffer) -1] = 0;
1087 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1088 used = strlen(buffer);
1089 ok( (res == STATUS_SUCCESS) &&
1090 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1091 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1093 size = used + 1;
1094 memset(buffer, '#', sizeof(buffer) - 1);
1095 buffer[sizeof(buffer) -1] = 0;
1096 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1097 ok( (res == STATUS_SUCCESS) &&
1098 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1099 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1101 size = used;
1102 memset(buffer, '#', sizeof(buffer) - 1);
1103 buffer[sizeof(buffer) -1] = 0;
1104 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1105 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1106 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1107 res, size, buffer, used + 1);
1109 size = used - 1;
1110 memset(buffer, '#', sizeof(buffer) - 1);
1111 buffer[sizeof(buffer) -1] = 0;
1112 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1113 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1114 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1115 res, size, buffer, used + 1);
1118 /* parameters are checked */
1119 memset(buffer, '#', sizeof(buffer) - 1);
1120 buffer[sizeof(buffer) -1] = 0;
1121 res = pRtlIpv4AddressToStringExA(&ip, 0, buffer, NULL);
1122 ok(res == STATUS_INVALID_PARAMETER,
1123 "got 0x%x with '%s' (expected STATUS_INVALID_PARAMETER)\n", res, buffer);
1125 size = sizeof(buffer);
1126 res = pRtlIpv4AddressToStringExA(&ip, 0, NULL, &size);
1127 ok( res == STATUS_INVALID_PARAMETER,
1128 "got 0x%x and size %d (expected STATUS_INVALID_PARAMETER)\n", res, size);
1130 size = sizeof(buffer);
1131 memset(buffer, '#', sizeof(buffer) - 1);
1132 buffer[sizeof(buffer) -1] = 0;
1133 res = pRtlIpv4AddressToStringExA(NULL, 0, buffer, &size);
1134 ok( res == STATUS_INVALID_PARAMETER,
1135 "got 0x%x and size %d with '%s' (expected STATUS_INVALID_PARAMETER)\n",
1136 res, size, buffer);
1139 static void test_RtlIpv4StringToAddress(void)
1141 NTSTATUS res;
1142 IN_ADDR ip, expected_ip;
1143 PCSTR terminator;
1144 CHAR dummy;
1145 struct
1147 PCSTR address;
1148 NTSTATUS res;
1149 int terminator_offset;
1150 int ip[4];
1151 BOOL strict_is_different;
1152 NTSTATUS res_strict;
1153 int terminator_offset_strict;
1154 int ip_strict[4];
1155 } tests[] =
1157 { "", STATUS_INVALID_PARAMETER, 0, { -1 } },
1158 { " ", STATUS_INVALID_PARAMETER, 0, { -1 } },
1159 { "1.1.1.1", STATUS_SUCCESS, 7, { 1, 1, 1, 1 } },
1160 { "0.0.0.0", STATUS_SUCCESS, 7, { 0, 0, 0, 0 } },
1161 { "255.255.255.255", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1162 { "255.255.255.255:123",
1163 STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1164 { "255.255.255.256", STATUS_INVALID_PARAMETER, 15, { -1 } },
1165 { "255.255.255.4294967295",
1166 STATUS_INVALID_PARAMETER, 22, { -1 } },
1167 { "255.255.255.4294967296",
1168 STATUS_INVALID_PARAMETER, 21, { -1 } },
1169 { "255.255.255.4294967297",
1170 STATUS_INVALID_PARAMETER, 21, { -1 } },
1171 { "a", STATUS_INVALID_PARAMETER, 0, { -1 } },
1172 { "1.1.1.0xaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1173 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1174 { "1.1.1.0XaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1175 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1176 { "1.1.1.0x", STATUS_INVALID_PARAMETER, 8, { -1 } },
1177 { "1.1.1.0xff", STATUS_SUCCESS, 10, { 1, 1, 1, 255 },
1178 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1179 { "1.1.1.0x100", STATUS_INVALID_PARAMETER, 11, { -1 },
1180 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1181 { "1.1.1.0xffffffff",STATUS_INVALID_PARAMETER, 16, { -1 },
1182 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1183 { "1.1.1.0x100000000",
1184 STATUS_INVALID_PARAMETER, 16, { -1, 0, 0, 0 },
1185 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1186 { "1.1.1.010", STATUS_SUCCESS, 9, { 1, 1, 1, 8 },
1187 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1188 { "1.1.1.00", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1189 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1190 { "1.1.1.007", STATUS_SUCCESS, 9, { 1, 1, 1, 7 },
1191 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1192 { "1.1.1.08", STATUS_INVALID_PARAMETER, 7, { -1 } },
1193 { "1.1.1.008", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1194 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1195 { "1.1.1.0a", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1196 { "1.1.1.0o10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1197 { "1.1.1.0b10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1198 { "1.1.1.-2", STATUS_INVALID_PARAMETER, 6, { -1 } },
1199 { "1", STATUS_SUCCESS, 1, { 0, 0, 0, 1 },
1200 TRUE, STATUS_INVALID_PARAMETER, 1, { -1 } },
1201 { "-1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1202 { "203569230", STATUS_SUCCESS, 9, { 12, 34, 56, 78 },
1203 TRUE, STATUS_INVALID_PARAMETER, 9, { -1 } },
1204 { "1.223756", STATUS_SUCCESS, 8, { 1, 3, 106, 12 },
1205 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1206 { "3.4.756", STATUS_SUCCESS, 7, { 3, 4, 2, 244 },
1207 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1208 { "3.4.756.1", STATUS_INVALID_PARAMETER, 9, { -1 } },
1209 { "3.4.65536", STATUS_INVALID_PARAMETER, 9, { -1 } },
1210 { "3.4.5.6.7", STATUS_INVALID_PARAMETER, 7, { -1 } },
1211 { "3.4.5.+6", STATUS_INVALID_PARAMETER, 6, { -1 } },
1212 { " 3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1213 { "\t3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1214 { "3.4.5.6 ", STATUS_SUCCESS, 7, { 3, 4, 5, 6 } },
1215 { "3. 4.5.6", STATUS_INVALID_PARAMETER, 2, { -1 } },
1216 { ".", STATUS_INVALID_PARAMETER, 1, { -1 } },
1217 { "..", STATUS_INVALID_PARAMETER, 1, { -1 } },
1218 { "1.", STATUS_INVALID_PARAMETER, 2, { -1 } },
1219 { "1..", STATUS_INVALID_PARAMETER, 3, { -1 } },
1220 { ".1", STATUS_INVALID_PARAMETER, 1, { -1 } },
1221 { ".1.", STATUS_INVALID_PARAMETER, 1, { -1 } },
1222 { ".1.2.3", STATUS_INVALID_PARAMETER, 1, { -1 } },
1223 { "0.1.2.3", STATUS_SUCCESS, 7, { 0, 1, 2, 3 } },
1224 { "0.1.2.3.", STATUS_INVALID_PARAMETER, 7, { -1 } },
1225 { "[0.1.2.3]", STATUS_INVALID_PARAMETER, 0, { -1 } },
1226 { "::1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1227 { ":1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1229 const int testcount = sizeof(tests) / sizeof(tests[0]);
1230 int i;
1232 if (!pRtlIpv4StringToAddressA)
1234 skip("RtlIpv4StringToAddress not available\n");
1235 return;
1238 if (0)
1240 /* leaving either parameter NULL crashes on Windows */
1241 res = pRtlIpv4StringToAddressA(NULL, FALSE, &terminator, &ip);
1242 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, NULL, &ip);
1243 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, &terminator, NULL);
1244 /* same for the wide char version */
1246 res = pRtlIpv4StringToAddressW(NULL, FALSE, &terminatorW, &ip);
1247 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, NULL, &ip);
1248 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, &terminatorW, NULL);
1252 for (i = 0; i < testcount; i++)
1254 /* non-strict */
1255 terminator = &dummy;
1256 ip.S_un.S_addr = 0xabababab;
1257 res = pRtlIpv4StringToAddressA(tests[i].address, FALSE, &terminator, &ip);
1258 ok(res == tests[i].res,
1259 "[%s] res = 0x%08x, expected 0x%08x\n",
1260 tests[i].address, res, tests[i].res);
1261 ok(terminator == tests[i].address + tests[i].terminator_offset,
1262 "[%s] terminator = %p, expected %p\n",
1263 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset);
1264 if (tests[i].ip[0] == -1)
1265 expected_ip.S_un.S_addr = 0xabababab;
1266 else
1268 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip[0];
1269 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip[1];
1270 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip[2];
1271 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip[3];
1273 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1274 "[%s] ip = %08x, expected %08x\n",
1275 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1277 if (!tests[i].strict_is_different)
1279 tests[i].res_strict = tests[i].res;
1280 tests[i].terminator_offset_strict = tests[i].terminator_offset;
1281 tests[i].ip_strict[0] = tests[i].ip[0];
1282 tests[i].ip_strict[1] = tests[i].ip[1];
1283 tests[i].ip_strict[2] = tests[i].ip[2];
1284 tests[i].ip_strict[3] = tests[i].ip[3];
1286 /* strict */
1287 terminator = &dummy;
1288 ip.S_un.S_addr = 0xabababab;
1289 res = pRtlIpv4StringToAddressA(tests[i].address, TRUE, &terminator, &ip);
1290 ok(res == tests[i].res_strict,
1291 "[%s] res = 0x%08x, expected 0x%08x\n",
1292 tests[i].address, res, tests[i].res_strict);
1293 ok(terminator == tests[i].address + tests[i].terminator_offset_strict,
1294 "[%s] terminator = %p, expected %p\n",
1295 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset_strict);
1296 if (tests[i].ip_strict[0] == -1)
1297 expected_ip.S_un.S_addr = 0xabababab;
1298 else
1300 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip_strict[0];
1301 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip_strict[1];
1302 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip_strict[2];
1303 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip_strict[3];
1305 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1306 "[%s] ip = %08x, expected %08x\n",
1307 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1311 static void test_LdrAddRefDll(void)
1313 HMODULE mod, mod2;
1314 NTSTATUS status;
1315 BOOL ret;
1317 if (!pLdrAddRefDll)
1319 win_skip( "LdrAddRefDll not supported\n" );
1320 return;
1323 mod = LoadLibraryA("comctl32.dll");
1324 ok(mod != NULL, "got %p\n", mod);
1325 ret = FreeLibrary(mod);
1326 ok(ret, "got %d\n", ret);
1328 mod2 = GetModuleHandleA("comctl32.dll");
1329 ok(mod2 == NULL, "got %p\n", mod2);
1331 /* load, addref and release 2 times */
1332 mod = LoadLibraryA("comctl32.dll");
1333 ok(mod != NULL, "got %p\n", mod);
1334 status = pLdrAddRefDll(0, mod);
1335 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1336 ret = FreeLibrary(mod);
1337 ok(ret, "got %d\n", ret);
1339 mod2 = GetModuleHandleA("comctl32.dll");
1340 ok(mod2 != NULL, "got %p\n", mod2);
1341 ret = FreeLibrary(mod);
1342 ok(ret, "got %d\n", ret);
1344 mod2 = GetModuleHandleA("comctl32.dll");
1345 ok(mod2 == NULL, "got %p\n", mod2);
1347 /* pin refcount */
1348 mod = LoadLibraryA("comctl32.dll");
1349 ok(mod != NULL, "got %p\n", mod);
1350 status = pLdrAddRefDll(LDR_ADDREF_DLL_PIN, mod);
1351 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1353 ret = FreeLibrary(mod);
1354 ok(ret, "got %d\n", ret);
1355 ret = FreeLibrary(mod);
1356 ok(ret, "got %d\n", ret);
1357 ret = FreeLibrary(mod);
1358 ok(ret, "got %d\n", ret);
1359 ret = FreeLibrary(mod);
1360 ok(ret, "got %d\n", ret);
1362 mod2 = GetModuleHandleA("comctl32.dll");
1363 ok(mod2 != NULL, "got %p\n", mod2);
1366 static void test_LdrLockLoaderLock(void)
1368 ULONG_PTR magic;
1369 ULONG result;
1370 NTSTATUS status;
1372 if (!pLdrLockLoaderLock)
1374 win_skip("LdrLockLoaderLock() is not available\n");
1375 return;
1378 /* invalid flags */
1379 result = 10;
1380 magic = 0xdeadbeef;
1381 status = pLdrLockLoaderLock(0x10, &result, &magic);
1382 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
1383 ok(result == 0, "got %d\n", result);
1384 ok(magic == 0, "got %lx\n", magic);
1386 magic = 0xdeadbeef;
1387 status = pLdrLockLoaderLock(0x10, NULL, &magic);
1388 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
1389 ok(magic == 0, "got %lx\n", magic);
1391 result = 10;
1392 status = pLdrLockLoaderLock(0x10, &result, NULL);
1393 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
1394 ok(result == 0, "got %d\n", result);
1396 /* non-blocking mode, result is null */
1397 magic = 0xdeadbeef;
1398 status = pLdrLockLoaderLock(0x2, NULL, &magic);
1399 ok(status == STATUS_INVALID_PARAMETER_2, "got 0x%08x\n", status);
1400 ok(magic == 0, "got %lx\n", magic);
1402 /* magic pointer is null */
1403 result = 10;
1404 status = pLdrLockLoaderLock(0, &result, NULL);
1405 ok(status == STATUS_INVALID_PARAMETER_3, "got 0x%08x\n", status);
1406 ok(result == 0, "got %d\n", result);
1408 /* lock in non-blocking mode */
1409 result = 0;
1410 magic = 0;
1411 status = pLdrLockLoaderLock(0x2, &result, &magic);
1412 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1413 ok(result == 1, "got %d\n", result);
1414 ok(magic != 0, "got %lx\n", magic);
1415 pLdrUnlockLoaderLock(0, magic);
1418 static void test_RtlCompressBuffer(void)
1420 ULONG compress_workspace, decompress_workspace;
1421 static const UCHAR test_buffer[] = "WineWineWine";
1422 static UCHAR buf1[0x1000], buf2[0x1000];
1423 ULONG final_size, buf_size;
1424 UCHAR *workspace = NULL;
1425 NTSTATUS status;
1427 if (!pRtlCompressBuffer || !pRtlDecompressBuffer || !pRtlGetCompressionWorkSpaceSize)
1429 win_skip("skipping RtlCompressBuffer tests, required functions not available\n");
1430 return;
1433 compress_workspace = decompress_workspace = 0xdeadbeef;
1434 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
1435 &decompress_workspace);
1436 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1437 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
1438 workspace = HeapAlloc(GetProcessHeap(), 0, compress_workspace);
1439 ok(workspace != NULL, "HeapAlloc failed %d\n", GetLastError());
1441 /* test compression format / engine */
1442 final_size = 0xdeadbeef;
1443 status = pRtlCompressBuffer(COMPRESSION_FORMAT_NONE, test_buffer, sizeof(test_buffer),
1444 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
1445 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1446 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1448 final_size = 0xdeadbeef;
1449 status = pRtlCompressBuffer(COMPRESSION_FORMAT_DEFAULT, test_buffer, sizeof(test_buffer),
1450 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
1451 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1452 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1454 final_size = 0xdeadbeef;
1455 status = pRtlCompressBuffer(0xFF, test_buffer, sizeof(test_buffer),
1456 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
1457 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
1458 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1460 /* test compression */
1461 final_size = 0xdeadbeef;
1462 memset(buf1, 0x11, sizeof(buf1));
1463 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
1464 buf1, sizeof(buf1), 4096, &final_size, workspace);
1465 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1466 ok((*(WORD *)buf1 & 0x7000) == 0x3000, "no chunk signature found %04x\n", *(WORD *)buf1);
1467 todo_wine
1468 ok(final_size < sizeof(test_buffer), "got wrong final_size %u\n", final_size);
1470 /* test decompression */
1471 buf_size = final_size;
1472 final_size = 0xdeadbeef;
1473 memset(buf2, 0x11, sizeof(buf2));
1474 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf2, sizeof(buf2),
1475 buf1, buf_size, &final_size);
1476 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1477 ok(final_size == sizeof(test_buffer), "got wrong final_size %u\n", final_size);
1478 ok(!memcmp(buf2, test_buffer, sizeof(test_buffer)), "got wrong decoded data\n");
1479 ok(buf2[sizeof(test_buffer)] == 0x11, "too many bytes written\n");
1481 /* buffer too small */
1482 final_size = 0xdeadbeef;
1483 memset(buf1, 0x11, sizeof(buf1));
1484 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
1485 buf1, 4, 4096, &final_size, workspace);
1486 ok(status == STATUS_BUFFER_TOO_SMALL, "got wrong status 0x%08x\n", status);
1488 HeapFree(GetProcessHeap(), 0, workspace);
1491 static void test_RtlGetCompressionWorkSpaceSize(void)
1493 ULONG compress_workspace, decompress_workspace;
1494 NTSTATUS status;
1496 if (!pRtlGetCompressionWorkSpaceSize)
1498 win_skip("RtlGetCompressionWorkSpaceSize is not available\n");
1499 return;
1502 /* test invalid format / engine */
1503 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_NONE, &compress_workspace,
1504 &decompress_workspace);
1505 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1507 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_DEFAULT, &compress_workspace,
1508 &decompress_workspace);
1509 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1511 status = pRtlGetCompressionWorkSpaceSize(0xFF, &compress_workspace, &decompress_workspace);
1512 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
1514 /* test LZNT1 with normal and maximum compression */
1515 compress_workspace = decompress_workspace = 0xdeadbeef;
1516 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
1517 &decompress_workspace);
1518 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1519 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
1520 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
1522 compress_workspace = decompress_workspace = 0xdeadbeef;
1523 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM,
1524 &compress_workspace, &decompress_workspace);
1525 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1526 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
1527 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
1530 /* helper for test_RtlDecompressBuffer, checks if a chunk is incomplete */
1531 static BOOL is_incomplete_chunk(const UCHAR *compressed, ULONG compressed_size, BOOL check_all)
1533 ULONG chunk_size;
1535 if (compressed_size <= sizeof(WORD))
1536 return TRUE;
1538 while (compressed_size >= sizeof(WORD))
1540 chunk_size = (*(WORD *)compressed & 0xFFF) + 1;
1541 if (compressed_size < sizeof(WORD) + chunk_size)
1542 return TRUE;
1543 if (!check_all)
1544 break;
1545 compressed += sizeof(WORD) + chunk_size;
1546 compressed_size -= sizeof(WORD) + chunk_size;
1549 return FALSE;
1552 #define DECOMPRESS_BROKEN_FRAGMENT 1 /* < Win 7 */
1553 #define DECOMPRESS_BROKEN_TRUNCATED 2 /* broken on all machines */
1555 static void test_RtlDecompressBuffer(void)
1557 static const struct
1559 UCHAR compressed[32];
1560 ULONG compressed_size;
1561 NTSTATUS status;
1562 UCHAR uncompressed[32];
1563 ULONG uncompressed_size;
1564 DWORD broken_flags;
1566 test_lznt[] =
1568 /* 4 byte uncompressed chunk */
1570 {0x03, 0x30, 'W', 'i', 'n', 'e'},
1572 STATUS_SUCCESS,
1573 "Wine",
1575 DECOMPRESS_BROKEN_FRAGMENT
1577 /* 8 byte uncompressed chunk */
1579 {0x07, 0x30, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
1581 STATUS_SUCCESS,
1582 "WineWine",
1584 DECOMPRESS_BROKEN_FRAGMENT
1586 /* 4 byte compressed chunk */
1588 {0x04, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
1590 STATUS_SUCCESS,
1591 "Wine",
1594 /* 8 byte compressed chunk */
1596 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
1598 STATUS_SUCCESS,
1599 "WineWine",
1602 /* compressed chunk using backwards reference */
1604 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x01, 0x30},
1606 STATUS_SUCCESS,
1607 "WineWine",
1609 DECOMPRESS_BROKEN_TRUNCATED
1611 /* compressed chunk using backwards reference with length > bytes_read */
1613 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x30},
1615 STATUS_SUCCESS,
1616 "WineWineWine",
1618 DECOMPRESS_BROKEN_TRUNCATED
1620 /* same as above, but unused bits != 0 */
1622 {0x06, 0xB0, 0x30, 'W', 'i', 'n', 'e', 0x01, 0x30},
1624 STATUS_SUCCESS,
1625 "WineWine",
1627 DECOMPRESS_BROKEN_TRUNCATED
1629 /* compressed chunk without backwards reference and unused bits != 0 */
1631 {0x01, 0xB0, 0x02, 'W'},
1633 STATUS_SUCCESS,
1634 "W",
1637 /* termination sequence after first chunk */
1639 {0x03, 0x30, 'W', 'i', 'n', 'e', 0x00, 0x00, 0x03, 0x30, 'W', 'i', 'n', 'e'},
1641 STATUS_SUCCESS,
1642 "Wine",
1644 DECOMPRESS_BROKEN_FRAGMENT
1646 /* compressed chunk using backwards reference with 4 bit offset, 12 bit length */
1648 {0x14, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
1649 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
1650 0x01, 0x01, 0xF0},
1652 STATUS_SUCCESS,
1653 "ABCDEFGHIJKLMNOPABCD",
1655 DECOMPRESS_BROKEN_TRUNCATED
1657 /* compressed chunk using backwards reference with 5 bit offset, 11 bit length */
1659 {0x15, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
1660 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
1661 0x02, 'A', 0x00, 0x78},
1663 STATUS_SUCCESS,
1664 "ABCDEFGHIJKLMNOPABCD",
1666 DECOMPRESS_BROKEN_TRUNCATED
1668 /* uncompressed chunk with invalid magic */
1670 {0x03, 0x20, 'W', 'i', 'n', 'e'},
1672 STATUS_SUCCESS,
1673 "Wine",
1675 DECOMPRESS_BROKEN_FRAGMENT
1677 /* compressed chunk with invalid magic */
1679 {0x04, 0xA0, 0x00, 'W', 'i', 'n', 'e'},
1681 STATUS_SUCCESS,
1682 "Wine",
1685 /* garbage byte after end of buffer */
1687 {0x00, 0xB0, 0x02, 0x01},
1689 STATUS_SUCCESS,
1693 /* empty compressed chunk */
1695 {0x00, 0xB0, 0x00},
1697 STATUS_SUCCESS,
1701 /* empty compressed chunk with unused bits != 0 */
1703 {0x00, 0xB0, 0x01},
1705 STATUS_SUCCESS,
1709 /* empty input buffer */
1713 STATUS_BAD_COMPRESSION_BUFFER,
1715 /* incomplete chunk header */
1717 {0x01},
1719 STATUS_BAD_COMPRESSION_BUFFER
1721 /* incomplete chunk header */
1723 {0x00, 0x30},
1725 STATUS_BAD_COMPRESSION_BUFFER
1727 /* compressed chunk with invalid backwards reference */
1729 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x40},
1731 STATUS_BAD_COMPRESSION_BUFFER
1733 /* compressed chunk with incomplete backwards reference */
1735 {0x05, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05},
1737 STATUS_BAD_COMPRESSION_BUFFER
1739 /* incomplete uncompressed chunk */
1741 {0x07, 0x30, 'W', 'i', 'n', 'e'},
1743 STATUS_BAD_COMPRESSION_BUFFER
1745 /* incomplete compressed chunk */
1747 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
1749 STATUS_BAD_COMPRESSION_BUFFER
1751 /* two compressed chunks, the second one incomplete */
1753 {0x00, 0xB0, 0x02, 0x00, 0xB0},
1755 STATUS_BAD_COMPRESSION_BUFFER,
1759 static UCHAR buf[0x2000], workspace[0x1000];
1760 NTSTATUS status, expected_status;
1761 ULONG final_size;
1762 int i;
1764 if (!pRtlDecompressBuffer || !pRtlDecompressFragment)
1766 win_skip("RtlDecompressBuffer or RtlDecompressFragment is not available\n");
1767 return;
1770 /* test compression format / engine */
1771 final_size = 0xdeadbeef;
1772 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_NONE, buf, sizeof(buf), test_lznt[0].compressed,
1773 test_lznt[0].compressed_size, &final_size);
1774 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1775 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1777 final_size = 0xdeadbeef;
1778 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_DEFAULT, buf, sizeof(buf), test_lznt[0].compressed,
1779 test_lznt[0].compressed_size, &final_size);
1780 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1781 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1783 final_size = 0xdeadbeef;
1784 status = pRtlDecompressBuffer(0xFF, buf, sizeof(buf), test_lznt[0].compressed,
1785 test_lznt[0].compressed_size, &final_size);
1786 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
1787 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1789 /* regular tests for RtlDecompressBuffer */
1790 for (i = 0; i < sizeof(test_lznt) / sizeof(test_lznt[0]); i++)
1792 trace("Running test %d (compressed_size=%u, uncompressed_size=%u, status=0x%08x)\n",
1793 i, test_lznt[i].compressed_size, test_lznt[i].uncompressed_size, test_lznt[i].status);
1795 /* test with very big buffer */
1796 final_size = 0xdeadbeef;
1797 memset(buf, 0x11, sizeof(buf));
1798 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1799 test_lznt[i].compressed_size, &final_size);
1800 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
1801 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08x\n", i, status);
1802 if (!status)
1804 ok(final_size == test_lznt[i].uncompressed_size,
1805 "%d: got wrong final_size %u\n", i, final_size);
1806 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
1807 "%d: got wrong decoded data\n", i);
1808 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
1809 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
1812 /* test that modifier for compression engine is ignored */
1813 final_size = 0xdeadbeef;
1814 memset(buf, 0x11, sizeof(buf));
1815 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM, buf, sizeof(buf),
1816 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
1817 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
1818 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08x\n", i, status);
1819 if (!status)
1821 ok(final_size == test_lznt[i].uncompressed_size,
1822 "%d: got wrong final_size %u\n", i, final_size);
1823 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
1824 "%d: got wrong decoded data\n", i);
1825 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
1826 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
1829 /* test with expected output size */
1830 if (test_lznt[i].uncompressed_size > 0)
1832 final_size = 0xdeadbeef;
1833 memset(buf, 0x11, sizeof(buf));
1834 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size,
1835 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
1836 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1837 if (!status)
1839 ok(final_size == test_lznt[i].uncompressed_size,
1840 "%d: got wrong final_size %u\n", i, final_size);
1841 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
1842 "%d: got wrong decoded data\n", i);
1843 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
1844 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
1848 /* test with smaller output size */
1849 if (test_lznt[i].uncompressed_size > 1)
1851 final_size = 0xdeadbeef;
1852 memset(buf, 0x11, sizeof(buf));
1853 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size - 1,
1854 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
1855 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_TRUNCATED)
1856 todo_wine
1857 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1858 else
1859 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1860 if (!status)
1862 ok(final_size == test_lznt[i].uncompressed_size - 1,
1863 "%d: got wrong final_size %u\n", i, final_size);
1864 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size - 1),
1865 "%d: got wrong decoded data\n", i);
1866 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
1867 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size - 1);
1871 /* test with zero output size */
1872 final_size = 0xdeadbeef;
1873 memset(buf, 0x11, sizeof(buf));
1874 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, 0, test_lznt[i].compressed,
1875 test_lznt[i].compressed_size, &final_size);
1876 if (is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, FALSE))
1877 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1878 else
1880 ok(status == STATUS_SUCCESS, "%d: got wrong status 0x%08x\n", i, status);
1881 ok(final_size == 0, "%d: got wrong final_size %u\n", i, final_size);
1882 ok(buf[0] == 0x11, "%d: buf[0] was modified\n", i);
1885 /* test RtlDecompressFragment with offset = 0 */
1886 final_size = 0xdeadbeef;
1887 memset(buf, 0x11, sizeof(buf));
1888 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1889 test_lznt[i].compressed_size, 0, &final_size, workspace);
1890 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
1891 todo_wine
1892 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1893 else
1894 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1895 if (!status)
1897 ok(final_size == test_lznt[i].uncompressed_size,
1898 "%d: got wrong final_size %u\n", i, final_size);
1899 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
1900 "%d: got wrong decoded data\n", i);
1901 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
1902 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
1905 /* test RtlDecompressFragment with offset = 1 */
1906 final_size = 0xdeadbeef;
1907 memset(buf, 0x11, sizeof(buf));
1908 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1909 test_lznt[i].compressed_size, 1, &final_size, workspace);
1910 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
1911 todo_wine
1912 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1913 else
1914 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1915 if (!status)
1917 if (test_lznt[i].uncompressed_size == 0)
1919 todo_wine
1920 ok(final_size == 4095, "%d: got wrong final_size %u\n", i, final_size);
1921 /* Buffer doesn't contain any useful value on Windows */
1922 ok(buf[4095] == 0x11, "%d: buf[4095] was modified\n", i);
1924 else
1926 ok(final_size == test_lznt[i].uncompressed_size - 1,
1927 "%d: got wrong final_size %u\n", i, final_size);
1928 ok(!memcmp(buf, test_lznt[i].uncompressed + 1, test_lznt[i].uncompressed_size - 1),
1929 "%d: got wrong decoded data\n", i);
1930 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
1931 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size - 1);
1935 /* test RtlDecompressFragment with offset = 4095 */
1936 final_size = 0xdeadbeef;
1937 memset(buf, 0x11, sizeof(buf));
1938 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1939 test_lznt[i].compressed_size, 4095, &final_size, workspace);
1940 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
1941 todo_wine
1942 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1943 else
1944 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1945 if (!status)
1947 todo_wine
1948 ok(final_size == 1, "%d: got wrong final_size %u\n", i, final_size);
1949 todo_wine
1950 ok(buf[0] == 0, "%d: padding is not zero\n", i);
1951 ok(buf[1] == 0x11, "%d: buf[1] was modified\n", i);
1954 /* test RtlDecompressFragment with offset = 4096 */
1955 final_size = 0xdeadbeef;
1956 memset(buf, 0x11, sizeof(buf));
1957 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1958 test_lznt[i].compressed_size, 4096, &final_size, workspace);
1959 expected_status = is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, TRUE) ?
1960 test_lznt[i].status : STATUS_SUCCESS;
1961 ok(status == expected_status, "%d: got wrong status 0x%08x, expected 0x%08x\n", i, status, expected_status);
1962 if (!status)
1964 ok(final_size == 0, "%d: got wrong final_size %u\n", i, final_size);
1965 ok(buf[0] == 0x11, "%d: buf[4096] was modified\n", i);
1970 #undef DECOMPRESS_BROKEN_FRAGMENT
1971 #undef DECOMPRESS_BROKEN_TRUNCATED
1973 struct critsect_locked_info
1975 CRITICAL_SECTION crit;
1976 HANDLE semaphores[2];
1979 static DWORD WINAPI critsect_locked_thread(void *param)
1981 struct critsect_locked_info *info = param;
1982 DWORD ret;
1984 ret = pRtlIsCriticalSectionLocked(&info->crit);
1985 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
1986 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
1987 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
1989 ReleaseSemaphore(info->semaphores[0], 1, NULL);
1990 ret = WaitForSingleObject(info->semaphores[1], 1000);
1991 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
1993 ret = pRtlIsCriticalSectionLocked(&info->crit);
1994 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
1995 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
1996 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
1998 EnterCriticalSection(&info->crit);
2000 ret = pRtlIsCriticalSectionLocked(&info->crit);
2001 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2002 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
2003 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2005 ReleaseSemaphore(info->semaphores[0], 1, NULL);
2006 ret = WaitForSingleObject(info->semaphores[1], 1000);
2007 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2009 LeaveCriticalSection(&info->crit);
2010 return 0;
2013 static void test_RtlIsCriticalSectionLocked(void)
2015 struct critsect_locked_info info;
2016 HANDLE thread;
2017 BOOL ret;
2019 if (!pRtlIsCriticalSectionLocked || !pRtlIsCriticalSectionLockedByThread)
2021 win_skip("skipping RtlIsCriticalSectionLocked tests, required functions not available\n");
2022 return;
2025 InitializeCriticalSection(&info.crit);
2026 info.semaphores[0] = CreateSemaphoreW(NULL, 0, 1, NULL);
2027 ok(info.semaphores[0] != NULL, "CreateSemaphore failed with %u\n", GetLastError());
2028 info.semaphores[1] = CreateSemaphoreW(NULL, 0, 1, NULL);
2029 ok(info.semaphores[1] != NULL, "CreateSemaphore failed with %u\n", GetLastError());
2031 ret = pRtlIsCriticalSectionLocked(&info.crit);
2032 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2033 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2034 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2036 EnterCriticalSection(&info.crit);
2038 ret = pRtlIsCriticalSectionLocked(&info.crit);
2039 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2040 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2041 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2043 thread = CreateThread(NULL, 0, critsect_locked_thread, &info, 0, NULL);
2044 ok(thread != NULL, "CreateThread failed with %u\n", GetLastError());
2045 ret = WaitForSingleObject(info.semaphores[0], 1000);
2046 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2048 LeaveCriticalSection(&info.crit);
2050 ReleaseSemaphore(info.semaphores[1], 1, NULL);
2051 ret = WaitForSingleObject(info.semaphores[0], 1000);
2052 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2054 ret = pRtlIsCriticalSectionLocked(&info.crit);
2055 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2056 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2057 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2059 ReleaseSemaphore(info.semaphores[1], 1, NULL);
2060 ret = WaitForSingleObject(thread, 1000);
2061 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2063 CloseHandle(thread);
2064 CloseHandle(info.semaphores[0]);
2065 CloseHandle(info.semaphores[1]);
2066 DeleteCriticalSection(&info.crit);
2069 static void test_RtlInitializeCriticalSectionEx(void)
2071 static const CRITICAL_SECTION_DEBUG *no_debug = (void *)~(ULONG_PTR)0;
2072 CRITICAL_SECTION cs;
2074 if (!pRtlInitializeCriticalSectionEx)
2076 win_skip("RtlInitializeCriticalSectionEx is not available\n");
2077 return;
2080 memset(&cs, 0x11, sizeof(cs));
2081 pRtlInitializeCriticalSectionEx(&cs, 0, 0);
2082 ok((cs.DebugInfo != NULL && cs.DebugInfo != no_debug) || broken(cs.DebugInfo == no_debug) /* >= Win 8 */,
2083 "expected DebugInfo != NULL and DebugInfo != ~0, got %p\n", cs.DebugInfo);
2084 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2085 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2086 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
2087 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
2088 "expected SpinCount == 0, got %ld\n", cs.SpinCount);
2089 RtlDeleteCriticalSection(&cs);
2091 memset(&cs, 0x11, sizeof(cs));
2092 pRtlInitializeCriticalSectionEx(&cs, 0, RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO);
2093 todo_wine
2094 ok(cs.DebugInfo == no_debug, "expected DebugInfo == ~0, got %p\n", cs.DebugInfo);
2095 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2096 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2097 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
2098 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
2099 "expected SpinCount == 0, got %ld\n", cs.SpinCount);
2100 RtlDeleteCriticalSection(&cs);
2103 static void test_RtlLeaveCriticalSection(void)
2105 RTL_CRITICAL_SECTION cs;
2106 NTSTATUS status;
2108 if (!pRtlInitializeCriticalSectionEx)
2109 return; /* Skip winxp */
2111 status = RtlInitializeCriticalSection(&cs);
2112 ok(!status, "RtlInitializeCriticalSection failed: %x\n", status);
2114 status = RtlEnterCriticalSection(&cs);
2115 ok(!status, "RtlEnterCriticalSection failed: %x\n", status);
2116 todo_wine
2117 ok(cs.LockCount == -2, "expected LockCount == -2, got %d\n", cs.LockCount);
2118 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %d\n", cs.RecursionCount);
2119 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
2121 status = RtlLeaveCriticalSection(&cs);
2122 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2123 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2124 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2125 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2128 * Trying to leave a section that wasn't acquired modifies RecursionCount to an invalid value,
2129 * but doesn't modify LockCount so that an attempt to enter the section later will work.
2131 status = RtlLeaveCriticalSection(&cs);
2132 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2133 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2134 ok(cs.RecursionCount == -1, "expected RecursionCount == -1, got %d\n", cs.RecursionCount);
2135 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2137 /* and again */
2138 status = RtlLeaveCriticalSection(&cs);
2139 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2140 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2141 ok(cs.RecursionCount == -2, "expected RecursionCount == -2, got %d\n", cs.RecursionCount);
2142 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2144 /* entering section fixes RecursionCount */
2145 status = RtlEnterCriticalSection(&cs);
2146 ok(!status, "RtlEnterCriticalSection failed: %x\n", status);
2147 todo_wine
2148 ok(cs.LockCount == -2, "expected LockCount == -2, got %d\n", cs.LockCount);
2149 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %d\n", cs.RecursionCount);
2150 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
2152 status = RtlLeaveCriticalSection(&cs);
2153 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2154 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2155 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2156 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2158 status = RtlDeleteCriticalSection(&cs);
2159 ok(!status, "RtlDeleteCriticalSection failed: %x\n", status);
2162 struct ldr_enum_context
2164 BOOL abort;
2165 BOOL found;
2166 int count;
2169 static void WINAPI ldr_enum_callback(LDR_MODULE *module, void *context, BOOLEAN *stop)
2171 static const WCHAR ntdllW[] = {'n','t','d','l','l','.','d','l','l',0};
2172 struct ldr_enum_context *ctx = context;
2174 if (!lstrcmpiW(module->BaseDllName.Buffer, ntdllW))
2175 ctx->found = TRUE;
2177 ctx->count++;
2178 *stop = ctx->abort;
2181 static void test_LdrEnumerateLoadedModules(void)
2183 struct ldr_enum_context ctx;
2184 NTSTATUS status;
2186 if (!pLdrEnumerateLoadedModules)
2188 win_skip("LdrEnumerateLoadedModules not available\n");
2189 return;
2192 ctx.abort = FALSE;
2193 ctx.found = FALSE;
2194 ctx.count = 0;
2195 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
2196 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08x\n", status);
2197 ok(ctx.count > 1, "Expected more than one module, got %d\n", ctx.count);
2198 ok(ctx.found, "Could not find ntdll in list of modules\n");
2200 ctx.abort = TRUE;
2201 ctx.count = 0;
2202 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
2203 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08x\n", status);
2204 ok(ctx.count == 1, "Expected exactly one module, got %d\n", ctx.count);
2206 status = pLdrEnumerateLoadedModules((void *)0x1, ldr_enum_callback, (void *)0xdeadbeef);
2207 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
2209 status = pLdrEnumerateLoadedModules((void *)0xdeadbeef, ldr_enum_callback, (void *)0xdeadbeef);
2210 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
2212 status = pLdrEnumerateLoadedModules(NULL, NULL, (void *)0xdeadbeef);
2213 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
2216 static void test_RtlMakeSelfRelativeSD(void)
2218 char buf[sizeof(SECURITY_DESCRIPTOR_RELATIVE) + 4];
2219 SECURITY_DESCRIPTOR_RELATIVE *sd_rel = (SECURITY_DESCRIPTOR_RELATIVE *)buf;
2220 SECURITY_DESCRIPTOR sd;
2221 NTSTATUS status;
2222 DWORD len;
2224 if (!pRtlMakeSelfRelativeSD || !pRtlAbsoluteToSelfRelativeSD)
2226 win_skip( "RtlMakeSelfRelativeSD/RtlAbsoluteToSelfRelativeSD not available\n" );
2227 return;
2230 memset( &sd, 0, sizeof(sd) );
2231 sd.Revision = SECURITY_DESCRIPTOR_REVISION;
2233 len = 0;
2234 status = pRtlMakeSelfRelativeSD( &sd, NULL, &len );
2235 ok( status == STATUS_BUFFER_TOO_SMALL, "got %08x\n", status );
2236 ok( len == sizeof(*sd_rel), "got %u\n", len );
2238 len += 4;
2239 status = pRtlMakeSelfRelativeSD( &sd, sd_rel, &len );
2240 ok( status == STATUS_SUCCESS, "got %08x\n", status );
2241 ok( len == sizeof(*sd_rel) + 4, "got %u\n", len );
2243 len = 0;
2244 status = pRtlAbsoluteToSelfRelativeSD( &sd, NULL, &len );
2245 ok( status == STATUS_BUFFER_TOO_SMALL, "got %08x\n", status );
2246 ok( len == sizeof(*sd_rel), "got %u\n", len );
2248 len += 4;
2249 status = pRtlAbsoluteToSelfRelativeSD( &sd, sd_rel, &len );
2250 ok( status == STATUS_SUCCESS, "got %08x\n", status );
2251 ok( len == sizeof(*sd_rel) + 4, "got %u\n", len );
2253 sd.Control = SE_SELF_RELATIVE;
2254 status = pRtlMakeSelfRelativeSD( &sd, sd_rel, &len );
2255 ok( status == STATUS_SUCCESS, "got %08x\n", status );
2256 ok( len == sizeof(*sd_rel) + 4, "got %u\n", len );
2258 status = pRtlAbsoluteToSelfRelativeSD( &sd, sd_rel, &len );
2259 ok( status == STATUS_BAD_DESCRIPTOR_FORMAT, "got %08x\n", status );
2262 START_TEST(rtl)
2264 InitFunctionPtrs();
2266 test_RtlCompareMemory();
2267 test_RtlCompareMemoryUlong();
2268 test_RtlMoveMemory();
2269 test_RtlFillMemory();
2270 test_RtlFillMemoryUlong();
2271 test_RtlZeroMemory();
2272 test_RtlUlonglongByteSwap();
2273 test_RtlUniform();
2274 test_RtlRandom();
2275 test_RtlAreAllAccessesGranted();
2276 test_RtlAreAnyAccessesGranted();
2277 test_RtlComputeCrc32();
2278 test_HandleTables();
2279 test_RtlAllocateAndInitializeSid();
2280 test_RtlDeleteTimer();
2281 test_RtlThreadErrorMode();
2282 test_LdrProcessRelocationBlock();
2283 test_RtlIpv4AddressToString();
2284 test_RtlIpv4AddressToStringEx();
2285 test_RtlIpv4StringToAddress();
2286 test_LdrAddRefDll();
2287 test_LdrLockLoaderLock();
2288 test_RtlCompressBuffer();
2289 test_RtlGetCompressionWorkSpaceSize();
2290 test_RtlDecompressBuffer();
2291 test_RtlIsCriticalSectionLocked();
2292 test_RtlInitializeCriticalSectionEx();
2293 test_RtlLeaveCriticalSection();
2294 test_LdrEnumerateLoadedModules();
2295 test_RtlMakeSelfRelativeSD();