kernel32/tests: Add some more tests for write watches.
[wine.git] / dlls / ntdll / tests / rtl.c
blob7333ab89358ccff0d1d4cdeca5b2c10854b77e01
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 *);
103 static HMODULE hkernel32 = 0;
104 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
107 #define LEN 16
108 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
109 static ULONG src_aligned_block[4];
110 static ULONG dest_aligned_block[32];
111 static const char *src = (const char*)src_aligned_block;
112 static char* dest = (char*)dest_aligned_block;
114 static void InitFunctionPtrs(void)
116 hntdll = LoadLibraryA("ntdll.dll");
117 ok(hntdll != 0, "LoadLibrary failed\n");
118 if (hntdll) {
119 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
120 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
121 pRtlDeleteTimer = (void *)GetProcAddress(hntdll, "RtlDeleteTimer");
122 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
123 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
124 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
125 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
126 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
127 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
128 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
129 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
130 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
131 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
132 pRtlInitializeHandleTable = (void *)GetProcAddress(hntdll, "RtlInitializeHandleTable");
133 pRtlIsValidIndexHandle = (void *)GetProcAddress(hntdll, "RtlIsValidIndexHandle");
134 pRtlDestroyHandleTable = (void *)GetProcAddress(hntdll, "RtlDestroyHandleTable");
135 pRtlAllocateHandle = (void *)GetProcAddress(hntdll, "RtlAllocateHandle");
136 pRtlFreeHandle = (void *)GetProcAddress(hntdll, "RtlFreeHandle");
137 pRtlAllocateAndInitializeSid = (void *)GetProcAddress(hntdll, "RtlAllocateAndInitializeSid");
138 pRtlFreeSid = (void *)GetProcAddress(hntdll, "RtlFreeSid");
139 pRtlGetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlGetThreadErrorMode");
140 pRtlSetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlSetThreadErrorMode");
141 pLdrProcessRelocationBlock = (void *)GetProcAddress(hntdll, "LdrProcessRelocationBlock");
142 pRtlIpv4AddressToStringA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringA");
143 pRtlIpv4AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringExA");
144 pRtlIpv4StringToAddressA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressA");
145 pLdrAddRefDll = (void *)GetProcAddress(hntdll, "LdrAddRefDll");
146 pLdrLockLoaderLock = (void *)GetProcAddress(hntdll, "LdrLockLoaderLock");
147 pLdrUnlockLoaderLock = (void *)GetProcAddress(hntdll, "LdrUnlockLoaderLock");
148 pRtlGetCompressionWorkSpaceSize = (void *)GetProcAddress(hntdll, "RtlGetCompressionWorkSpaceSize");
149 pRtlDecompressBuffer = (void *)GetProcAddress(hntdll, "RtlDecompressBuffer");
150 pRtlDecompressFragment = (void *)GetProcAddress(hntdll, "RtlDecompressFragment");
151 pRtlCompressBuffer = (void *)GetProcAddress(hntdll, "RtlCompressBuffer");
152 pRtlIsCriticalSectionLocked = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLocked");
153 pRtlIsCriticalSectionLockedByThread = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLockedByThread");
154 pRtlInitializeCriticalSectionEx = (void *)GetProcAddress(hntdll, "RtlInitializeCriticalSectionEx");
155 pLdrEnumerateLoadedModules = (void *)GetProcAddress(hntdll, "LdrEnumerateLoadedModules");
157 hkernel32 = LoadLibraryA("kernel32.dll");
158 ok(hkernel32 != 0, "LoadLibrary failed\n");
159 if (hkernel32) {
160 pIsWow64Process = (void *)GetProcAddress(hkernel32, "IsWow64Process");
162 strcpy((char*)src_aligned_block, src_src);
163 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
166 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
167 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
169 static void test_RtlCompareMemory(void)
171 SIZE_T size;
173 if (!pRtlCompareMemory)
175 win_skip("RtlCompareMemory is not available\n");
176 return;
179 strcpy(dest, src);
181 COMP(src,src,0,0);
182 COMP(src,src,LEN,LEN);
183 dest[0] = 'x';
184 COMP(src,dest,LEN,0);
187 static void test_RtlCompareMemoryUlong(void)
189 ULONG a[10];
190 ULONG result;
192 if (!pRtlCompareMemoryUlong)
194 win_skip("RtlCompareMemoryUlong is not available\n");
195 return;
198 a[0]= 0x0123;
199 a[1]= 0x4567;
200 a[2]= 0x89ab;
201 a[3]= 0xcdef;
202 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
203 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %u, expected 0\n", a, result);
204 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
205 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
206 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
207 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
208 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
209 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
210 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
211 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
212 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
213 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 4\n", a, result);
214 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
215 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 4\n", a, result);
216 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
217 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %u, expected 0\n", a, result);
218 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
219 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %u, expected 0\n", a, result);
220 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
221 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %u, expected 0\n", a, result);
223 a[1]= 0x0123;
224 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
225 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
226 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
227 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
228 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
229 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
230 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
231 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
232 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
233 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 8\n", a, result);
234 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
235 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 8\n", a, result);
238 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
239 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
241 static void test_RtlMoveMemory(void)
243 if (!pRtlMoveMemory)
245 win_skip("RtlMoveMemory is not available\n");
246 return;
249 /* Length should be in bytes and not rounded. Use strcmp to ensure we
250 * didn't write past the end (it checks for the final NUL left by memset)
252 COPY(0); CMP("");
253 COPY(1); CMP("T");
254 COPY(2); CMP("Th");
255 COPY(3); CMP("Thi");
256 COPY(4); CMP("This");
257 COPY(5); CMP("This ");
258 COPY(6); CMP("This i");
259 COPY(7); CMP("This is");
260 COPY(8); CMP("This is ");
261 COPY(9); CMP("This is a");
263 /* Overlapping */
264 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
265 CMP("his is a test!!");
266 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
267 CMP("TThis is a test!");
270 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
272 static void test_RtlFillMemory(void)
274 if (!pRtlFillMemory)
276 win_skip("RtlFillMemory is not available\n");
277 return;
280 /* Length should be in bytes and not rounded. Use strcmp to ensure we
281 * didn't write past the end (the remainder of the string should match)
283 FILL(0); CMP("This is a test!");
284 FILL(1); CMP("xhis is a test!");
285 FILL(2); CMP("xxis is a test!");
286 FILL(3); CMP("xxxs is a test!");
287 FILL(4); CMP("xxxx is a test!");
288 FILL(5); CMP("xxxxxis a test!");
289 FILL(6); CMP("xxxxxxs a test!");
290 FILL(7); CMP("xxxxxxx a test!");
291 FILL(8); CMP("xxxxxxxxa test!");
292 FILL(9); CMP("xxxxxxxxx test!");
295 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
297 static void test_RtlFillMemoryUlong(void)
299 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
300 if (!pRtlFillMemoryUlong)
302 win_skip("RtlFillMemoryUlong is not available\n");
303 return;
306 /* Length should be in bytes and not rounded. Use strcmp to ensure we
307 * didn't write past the end (the remainder of the string should match)
309 LFILL(0); CMP("This is a test!");
310 LFILL(1); CMP("This is a test!");
311 LFILL(2); CMP("This is a test!");
312 LFILL(3); CMP("This is a test!");
313 LFILL(4); CMP("xxxx is a test!");
314 LFILL(5); CMP("xxxx is a test!");
315 LFILL(6); CMP("xxxx is a test!");
316 LFILL(7); CMP("xxxx is a test!");
317 LFILL(8); CMP("xxxxxxxxa test!");
318 LFILL(9); CMP("xxxxxxxxa test!");
321 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
322 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
324 static void test_RtlZeroMemory(void)
326 if (!pRtlZeroMemory)
328 win_skip("RtlZeroMemory is not available\n");
329 return;
332 /* Length should be in bytes and not rounded. */
333 ZERO(0); MCMP("This is a test!");
334 ZERO(1); MCMP("\0his is a test!");
335 ZERO(2); MCMP("\0\0is is a test!");
336 ZERO(3); MCMP("\0\0\0s is a test!");
337 ZERO(4); MCMP("\0\0\0\0 is a test!");
338 ZERO(5); MCMP("\0\0\0\0\0is a test!");
339 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
340 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
341 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
342 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
345 static void test_RtlUlonglongByteSwap(void)
347 ULONGLONG result;
349 if ( !pRtlUlonglongByteSwap )
351 win_skip("RtlUlonglongByteSwap is not available\n");
352 return;
355 if ( pRtlUlonglongByteSwap( 0 ) != 0 )
357 win_skip("Broken RtlUlonglongByteSwap in win2k\n");
358 return;
361 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
362 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
363 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%s, expected 0x2143658710325476\n",
364 wine_dbgstr_longlong(result));
368 static void test_RtlUniform(void)
370 ULONGLONG num;
371 ULONG seed;
372 ULONG seed_bak;
373 ULONG expected;
374 ULONG result;
376 if (!pRtlUniform)
378 win_skip("RtlUniform is not available\n");
379 return;
383 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
384 * algorithm. This algorithm is:
386 * seed = (seed * const_1 + const_2) % const_3;
388 * According to the documentation the random number is distributed over
389 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
391 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
393 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
394 * algorithm can be expressed without division as:
396 * seed = (seed * const_1 + const_2) & MAXLONG;
398 * To find out const_2 we just call RtlUniform with seed set to 0:
400 seed = 0;
401 expected = 0x7fffffc3;
402 result = pRtlUniform(&seed);
403 ok(result == expected,
404 "RtlUniform(&seed (seed == 0)) returns %x, expected %x\n",
405 result, expected);
407 * The algorithm is now:
409 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
411 * To find out const_1 we can use:
413 * const_1 = RtlUniform(1) - 0x7fffffc3;
415 * If that does not work a search loop can try all possible values of
416 * const_1 and compare to the result to RtlUniform(1).
417 * This way we find out that const_1 is 0xffffffed.
419 * For seed = 1 the const_2 is 0x7fffffc4:
421 seed = 1;
422 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
423 result = pRtlUniform(&seed);
424 ok(result == expected,
425 "RtlUniform(&seed (seed == 1)) returns %x, expected %x\n",
426 result, expected);
428 * For seed = 2 the const_2 is 0x7fffffc3:
430 seed = 2;
431 expected = seed * 0xffffffed + 0x7fffffc3;
432 result = pRtlUniform(&seed);
435 * Windows Vista uses different algorithms, so skip the rest of the tests
436 * until that is figured out. Trace output for the failures is about 10.5 MB!
439 if (result == 0x7fffff9f) {
440 skip("Most likely running on Windows Vista which uses a different algorithm\n");
441 return;
444 ok(result == expected,
445 "RtlUniform(&seed (seed == 2)) returns %x, expected %x\n",
446 result, expected);
449 * More tests show that if seed is odd the result must be incremented by 1:
451 seed = 3;
452 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
453 result = pRtlUniform(&seed);
454 ok(result == expected,
455 "RtlUniform(&seed (seed == 3)) returns %x, expected %x\n",
456 result, expected);
458 seed = 0x6bca1aa;
459 expected = seed * 0xffffffed + 0x7fffffc3;
460 result = pRtlUniform(&seed);
461 ok(result == expected,
462 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %x, expected %x\n",
463 result, expected);
465 seed = 0x6bca1ab;
466 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
467 result = pRtlUniform(&seed);
468 ok(result == expected,
469 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %x, expected %x\n",
470 result, expected);
472 * When seed is 0x6bca1ac there is an exception:
474 seed = 0x6bca1ac;
475 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
476 result = pRtlUniform(&seed);
477 ok(result == expected,
478 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %x, expected %x\n",
479 result, expected);
481 * Note that up to here const_3 is not used
482 * (the highest bit of the result is not set).
484 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
486 seed = 0x6bca1ad;
487 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
488 result = pRtlUniform(&seed);
489 ok(result == expected,
490 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %x, expected %x\n",
491 result, expected);
493 seed = 0x6bca1ae;
494 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
495 result = pRtlUniform(&seed);
496 ok(result == expected,
497 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %x, expected %x\n",
498 result, expected);
500 * There are several ranges where for odd or even seed the result must be
501 * incremented by 1. You can see this ranges in the following test.
503 * For a full test use one of the following loop heads:
505 * for (num = 0; num <= 0xffffffff; num++) {
506 * seed = num;
507 * ...
509 * seed = 0;
510 * for (num = 0; num <= 0xffffffff; num++) {
511 * ...
513 seed = 0;
514 for (num = 0; num <= 100000; num++) {
516 expected = seed * 0xffffffed + 0x7fffffc3;
517 if (seed < 0x6bca1ac) {
518 expected = expected + (seed & 1);
519 } else if (seed == 0x6bca1ac) {
520 expected = (expected + 2) & MAXLONG;
521 } else if (seed < 0xd79435c) {
522 expected = (expected + (~seed & 1)) & MAXLONG;
523 } else if (seed < 0x1435e50b) {
524 expected = expected + (seed & 1);
525 } else if (seed < 0x1af286ba) {
526 expected = (expected + (~seed & 1)) & MAXLONG;
527 } else if (seed < 0x21af2869) {
528 expected = expected + (seed & 1);
529 } else if (seed < 0x286bca18) {
530 expected = (expected + (~seed & 1)) & MAXLONG;
531 } else if (seed < 0x2f286bc7) {
532 expected = expected + (seed & 1);
533 } else if (seed < 0x35e50d77) {
534 expected = (expected + (~seed & 1)) & MAXLONG;
535 } else if (seed < 0x3ca1af26) {
536 expected = expected + (seed & 1);
537 } else if (seed < 0x435e50d5) {
538 expected = (expected + (~seed & 1)) & MAXLONG;
539 } else if (seed < 0x4a1af284) {
540 expected = expected + (seed & 1);
541 } else if (seed < 0x50d79433) {
542 expected = (expected + (~seed & 1)) & MAXLONG;
543 } else if (seed < 0x579435e2) {
544 expected = expected + (seed & 1);
545 } else if (seed < 0x5e50d792) {
546 expected = (expected + (~seed & 1)) & MAXLONG;
547 } else if (seed < 0x650d7941) {
548 expected = expected + (seed & 1);
549 } else if (seed < 0x6bca1af0) {
550 expected = (expected + (~seed & 1)) & MAXLONG;
551 } else if (seed < 0x7286bc9f) {
552 expected = expected + (seed & 1);
553 } else if (seed < 0x79435e4e) {
554 expected = (expected + (~seed & 1)) & MAXLONG;
555 } else if (seed < 0x7ffffffd) {
556 expected = expected + (seed & 1);
557 } else if (seed < 0x86bca1ac) {
558 expected = (expected + (~seed & 1)) & MAXLONG;
559 } else if (seed == 0x86bca1ac) {
560 expected = (expected + 1) & MAXLONG;
561 } else if (seed < 0x8d79435c) {
562 expected = expected + (seed & 1);
563 } else if (seed < 0x9435e50b) {
564 expected = (expected + (~seed & 1)) & MAXLONG;
565 } else if (seed < 0x9af286ba) {
566 expected = expected + (seed & 1);
567 } else if (seed < 0xa1af2869) {
568 expected = (expected + (~seed & 1)) & MAXLONG;
569 } else if (seed < 0xa86bca18) {
570 expected = expected + (seed & 1);
571 } else if (seed < 0xaf286bc7) {
572 expected = (expected + (~seed & 1)) & MAXLONG;
573 } else if (seed == 0xaf286bc7) {
574 expected = (expected + 2) & MAXLONG;
575 } else if (seed < 0xb5e50d77) {
576 expected = expected + (seed & 1);
577 } else if (seed < 0xbca1af26) {
578 expected = (expected + (~seed & 1)) & MAXLONG;
579 } else if (seed < 0xc35e50d5) {
580 expected = expected + (seed & 1);
581 } else if (seed < 0xca1af284) {
582 expected = (expected + (~seed & 1)) & MAXLONG;
583 } else if (seed < 0xd0d79433) {
584 expected = expected + (seed & 1);
585 } else if (seed < 0xd79435e2) {
586 expected = (expected + (~seed & 1)) & MAXLONG;
587 } else if (seed < 0xde50d792) {
588 expected = expected + (seed & 1);
589 } else if (seed < 0xe50d7941) {
590 expected = (expected + (~seed & 1)) & MAXLONG;
591 } else if (seed < 0xebca1af0) {
592 expected = expected + (seed & 1);
593 } else if (seed < 0xf286bc9f) {
594 expected = (expected + (~seed & 1)) & MAXLONG;
595 } else if (seed < 0xf9435e4e) {
596 expected = expected + (seed & 1);
597 } else if (seed < 0xfffffffd) {
598 expected = (expected + (~seed & 1)) & MAXLONG;
599 } else {
600 expected = expected + (seed & 1);
601 } /* if */
602 seed_bak = seed;
603 result = pRtlUniform(&seed);
604 ok(result == expected,
605 "test: 0x%s RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
606 wine_dbgstr_longlong(num), seed_bak, result, expected);
607 ok(seed == expected,
608 "test: 0x%s RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
609 wine_dbgstr_longlong(num), seed_bak, result, expected);
610 } /* for */
612 * Further investigation shows: In the different regions the highest bit
613 * is set or cleared when even or odd seeds need an increment by 1.
614 * This leads to a simplified algorithm:
616 * seed = seed * 0xffffffed + 0x7fffffc3;
617 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
618 * seed = (seed + 2) & MAXLONG;
619 * } else if (seed == 0x7fffffff) {
620 * seed = 0;
621 * } else if ((seed & 0x80000000) == 0) {
622 * seed = seed + (~seed & 1);
623 * } else {
624 * seed = (seed + (seed & 1)) & MAXLONG;
627 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
629 * Now comes the funny part:
630 * It took me one weekend, to find the complicated algorithm and one day more,
631 * to find the simplified algorithm. Several weeks later I found out: The value
632 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
633 * nor with the simplified algorithm. In reality the native function and our
634 * function return a random number distributed over [0..MAXLONG-1]. Note
635 * that this is different from what native documentation states [0..MAXLONG].
636 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
638 * seed = (seed * const_1 + const_2) % MAXLONG;
640 * Further investigations show that the real algorithm is:
642 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
644 * This is checked with the test below:
646 seed = 0;
647 for (num = 0; num <= 100000; num++) {
648 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
649 seed_bak = seed;
650 result = pRtlUniform(&seed);
651 ok(result == expected,
652 "test: 0x%s RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
653 wine_dbgstr_longlong(num), seed_bak, result, expected);
654 ok(seed == expected,
655 "test: 0x%s RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
656 wine_dbgstr_longlong(num), seed_bak, result, expected);
657 } /* for */
659 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
660 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
661 * that there is more than one cycle of generated randon numbers ...
666 static void test_RtlRandom(void)
668 int i, j;
669 ULONG seed;
670 ULONG res[512];
672 if (!pRtlRandom)
674 win_skip("RtlRandom is not available\n");
675 return;
678 seed = 0;
679 for (i = 0; i < sizeof(res) / sizeof(res[0]); i++)
681 res[i] = pRtlRandom(&seed);
682 ok(seed != res[i], "%i: seed is same as res %x\n", i, seed);
683 for (j = 0; j < i; j++)
684 ok(res[i] != res[j], "res[%i] (%x) is same as res[%i] (%x)\n", j, res[j], i, res[i]);
689 typedef struct {
690 ACCESS_MASK GrantedAccess;
691 ACCESS_MASK DesiredAccess;
692 BOOLEAN result;
693 } all_accesses_t;
695 static const all_accesses_t all_accesses[] = {
696 {0xFEDCBA76, 0xFEDCBA76, 1},
697 {0x00000000, 0xFEDCBA76, 0},
698 {0xFEDCBA76, 0x00000000, 1},
699 {0x00000000, 0x00000000, 1},
700 {0xFEDCBA76, 0xFEDCBA70, 1},
701 {0xFEDCBA70, 0xFEDCBA76, 0},
702 {0xFEDCBA76, 0xFEDC8A76, 1},
703 {0xFEDC8A76, 0xFEDCBA76, 0},
704 {0xFEDCBA76, 0xC8C4B242, 1},
705 {0xC8C4B242, 0xFEDCBA76, 0},
707 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
710 static void test_RtlAreAllAccessesGranted(void)
712 unsigned int test_num;
713 BOOLEAN result;
715 if (!pRtlAreAllAccessesGranted)
717 win_skip("RtlAreAllAccessesGranted is not available\n");
718 return;
721 for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
722 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
723 all_accesses[test_num].DesiredAccess);
724 ok(all_accesses[test_num].result == result,
725 "(test %d): RtlAreAllAccessesGranted(%08x, %08x) returns %d, expected %d\n",
726 test_num, all_accesses[test_num].GrantedAccess,
727 all_accesses[test_num].DesiredAccess,
728 result, all_accesses[test_num].result);
729 } /* for */
733 typedef struct {
734 ACCESS_MASK GrantedAccess;
735 ACCESS_MASK DesiredAccess;
736 BOOLEAN result;
737 } any_accesses_t;
739 static const any_accesses_t any_accesses[] = {
740 {0xFEDCBA76, 0xFEDCBA76, 1},
741 {0x00000000, 0xFEDCBA76, 0},
742 {0xFEDCBA76, 0x00000000, 0},
743 {0x00000000, 0x00000000, 0},
744 {0xFEDCBA76, 0x01234589, 0},
745 {0x00040000, 0xFEDCBA76, 1},
746 {0x00040000, 0xFED8BA76, 0},
747 {0xFEDCBA76, 0x00040000, 1},
748 {0xFED8BA76, 0x00040000, 0},
750 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
753 static void test_RtlAreAnyAccessesGranted(void)
755 unsigned int test_num;
756 BOOLEAN result;
758 if (!pRtlAreAnyAccessesGranted)
760 win_skip("RtlAreAnyAccessesGranted is not available\n");
761 return;
764 for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
765 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
766 any_accesses[test_num].DesiredAccess);
767 ok(any_accesses[test_num].result == result,
768 "(test %d): RtlAreAnyAccessesGranted(%08x, %08x) returns %d, expected %d\n",
769 test_num, any_accesses[test_num].GrantedAccess,
770 any_accesses[test_num].DesiredAccess,
771 result, any_accesses[test_num].result);
772 } /* for */
775 static void test_RtlComputeCrc32(void)
777 DWORD crc = 0;
779 if (!pRtlComputeCrc32)
781 win_skip("RtlComputeCrc32 is not available\n");
782 return;
785 crc = pRtlComputeCrc32(crc, (const BYTE *)src, LEN);
786 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8x\n", crc);
790 typedef struct MY_HANDLE
792 RTL_HANDLE RtlHandle;
793 void * MyValue;
794 } MY_HANDLE;
796 static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
798 ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
799 *AllocatedBit = *AllocatedBit | 1;
802 static void test_HandleTables(void)
804 BOOLEAN result;
805 NTSTATUS status;
806 ULONG Index;
807 MY_HANDLE * MyHandle;
808 RTL_HANDLE_TABLE HandleTable;
810 if (!pRtlInitializeHandleTable)
812 win_skip("RtlInitializeHandleTable is not available\n");
813 return;
816 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
817 MyHandle = (MY_HANDLE *)pRtlAllocateHandle(&HandleTable, &Index);
818 ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
819 RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
820 MyHandle = NULL;
821 result = pRtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
822 ok(result, "Handle %p wasn't valid\n", MyHandle);
823 result = pRtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
824 ok(result, "Couldn't free handle %p\n", MyHandle);
825 status = pRtlDestroyHandleTable(&HandleTable);
826 ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08x\n", status);
829 static void test_RtlAllocateAndInitializeSid(void)
831 NTSTATUS ret;
832 SID_IDENTIFIER_AUTHORITY sia = {{ 1, 2, 3, 4, 5, 6 }};
833 PSID psid;
835 if (!pRtlAllocateAndInitializeSid)
837 win_skip("RtlAllocateAndInitializeSid is not available\n");
838 return;
841 ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
842 ok(!ret, "RtlAllocateAndInitializeSid error %08x\n", ret);
843 ret = pRtlFreeSid(psid);
844 ok(!ret, "RtlFreeSid error %08x\n", ret);
846 /* these tests crash on XP */
847 if (0)
849 pRtlAllocateAndInitializeSid(NULL, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
850 pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL);
853 ret = pRtlAllocateAndInitializeSid(&sia, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
854 ok(ret == STATUS_INVALID_SID, "wrong error %08x\n", ret);
857 static void test_RtlDeleteTimer(void)
859 NTSTATUS ret;
861 if (!pRtlDeleteTimer)
863 win_skip("RtlDeleteTimer is not available\n");
864 return;
867 ret = pRtlDeleteTimer(NULL, NULL, NULL);
868 ok(ret == STATUS_INVALID_PARAMETER_1 ||
869 ret == STATUS_INVALID_PARAMETER, /* W2K */
870 "expected STATUS_INVALID_PARAMETER_1 or STATUS_INVALID_PARAMETER, got %x\n", ret);
873 static void test_RtlThreadErrorMode(void)
875 DWORD oldmode;
876 BOOL is_wow64;
877 DWORD mode;
878 NTSTATUS status;
880 if (!pRtlGetThreadErrorMode || !pRtlSetThreadErrorMode)
882 win_skip("RtlGetThreadErrorMode and/or RtlSetThreadErrorMode not available\n");
883 return;
886 if (!pIsWow64Process || !pIsWow64Process(GetCurrentProcess(), &is_wow64))
887 is_wow64 = FALSE;
889 oldmode = pRtlGetThreadErrorMode();
891 status = pRtlSetThreadErrorMode(0x70, &mode);
892 ok(status == STATUS_SUCCESS ||
893 status == STATUS_WAIT_1, /* Vista */
894 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
895 ok(mode == oldmode,
896 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
897 mode, oldmode);
898 ok(pRtlGetThreadErrorMode() == 0x70,
899 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0x70);
900 if (!is_wow64)
902 ok(NtCurrentTeb()->HardErrorDisabled == 0x70,
903 "The TEB contains 0x%x, expected 0x%x\n",
904 NtCurrentTeb()->HardErrorDisabled, 0x70);
907 status = pRtlSetThreadErrorMode(0, &mode);
908 ok(status == STATUS_SUCCESS ||
909 status == STATUS_WAIT_1, /* Vista */
910 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
911 ok(mode == 0x70,
912 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
913 mode, 0x70);
914 ok(pRtlGetThreadErrorMode() == 0,
915 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0);
916 if (!is_wow64)
918 ok(NtCurrentTeb()->HardErrorDisabled == 0,
919 "The TEB contains 0x%x, expected 0x%x\n",
920 NtCurrentTeb()->HardErrorDisabled, 0);
923 for (mode = 1; mode; mode <<= 1)
925 status = pRtlSetThreadErrorMode(mode, NULL);
926 if (mode & 0x70)
927 ok(status == STATUS_SUCCESS ||
928 status == STATUS_WAIT_1, /* Vista */
929 "RtlSetThreadErrorMode(%x,NULL) failed with error 0x%08x\n",
930 mode, status);
931 else
932 ok(status == STATUS_INVALID_PARAMETER_1,
933 "RtlSetThreadErrorMode(%x,NULL) returns 0x%08x, "
934 "expected STATUS_INVALID_PARAMETER_1\n",
935 mode, status);
938 pRtlSetThreadErrorMode(oldmode, NULL);
941 static void test_LdrProcessRelocationBlock(void)
943 IMAGE_BASE_RELOCATION *ret;
944 USHORT reloc;
945 DWORD addr32;
946 SHORT addr16;
948 if(!pLdrProcessRelocationBlock) {
949 win_skip("LdrProcessRelocationBlock not available\n");
950 return;
953 addr32 = 0x50005;
954 reloc = IMAGE_REL_BASED_HIGHLOW<<12;
955 ret = pLdrProcessRelocationBlock(&addr32, 1, &reloc, 0x500050);
956 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
957 ok(addr32 == 0x550055, "addr32 = %x, expected 0x550055\n", addr32);
959 addr16 = 0x505;
960 reloc = IMAGE_REL_BASED_HIGH<<12;
961 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
962 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
963 ok(addr16 == 0x555, "addr16 = %x, expected 0x555\n", addr16);
965 addr16 = 0x505;
966 reloc = IMAGE_REL_BASED_LOW<<12;
967 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
968 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
969 ok(addr16 == 0x565, "addr16 = %x, expected 0x565\n", addr16);
972 static void test_RtlIpv4AddressToString(void)
974 CHAR buffer[20];
975 CHAR *res;
976 IN_ADDR ip;
977 DWORD_PTR len;
979 if (!pRtlIpv4AddressToStringA)
981 win_skip("RtlIpv4AddressToStringA not available\n");
982 return;
985 ip.S_un.S_un_b.s_b1 = 1;
986 ip.S_un.S_un_b.s_b2 = 2;
987 ip.S_un.S_un_b.s_b3 = 3;
988 ip.S_un.S_un_b.s_b4 = 4;
990 memset(buffer, '#', sizeof(buffer) - 1);
991 buffer[sizeof(buffer) -1] = 0;
992 res = pRtlIpv4AddressToStringA(&ip, buffer);
993 len = strlen(buffer);
994 ok(res == (buffer + len), "got %p with '%s' (expected %p)\n", res, buffer, buffer + len);
996 res = pRtlIpv4AddressToStringA(&ip, NULL);
997 ok( (res == (char *)~0) ||
998 broken(res == (char *)len), /* XP and w2003 */
999 "got %p (expected ~0)\n", res);
1001 if (0) {
1002 /* this crashes in windows */
1003 memset(buffer, '#', sizeof(buffer) - 1);
1004 buffer[sizeof(buffer) -1] = 0;
1005 res = pRtlIpv4AddressToStringA(NULL, buffer);
1006 trace("got %p with '%s'\n", res, buffer);
1009 if (0) {
1010 /* this crashes in windows */
1011 res = pRtlIpv4AddressToStringA(NULL, NULL);
1012 trace("got %p\n", res);
1016 static void test_RtlIpv4AddressToStringEx(void)
1018 CHAR ip_1234[] = "1.2.3.4";
1019 CHAR ip_1234_80[] = "1.2.3.4:80";
1020 LPSTR expect;
1021 CHAR buffer[30];
1022 NTSTATUS res;
1023 IN_ADDR ip;
1024 ULONG size;
1025 DWORD used;
1026 USHORT port;
1028 if (!pRtlIpv4AddressToStringExA)
1030 win_skip("RtlIpv4AddressToStringExA not available\n");
1031 return;
1034 ip.S_un.S_un_b.s_b1 = 1;
1035 ip.S_un.S_un_b.s_b2 = 2;
1036 ip.S_un.S_un_b.s_b3 = 3;
1037 ip.S_un.S_un_b.s_b4 = 4;
1039 port = htons(80);
1040 expect = ip_1234_80;
1042 size = sizeof(buffer);
1043 memset(buffer, '#', sizeof(buffer) - 1);
1044 buffer[sizeof(buffer) -1] = 0;
1045 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1046 used = strlen(buffer);
1047 ok( (res == STATUS_SUCCESS) &&
1048 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1049 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1051 size = used + 1;
1052 memset(buffer, '#', sizeof(buffer) - 1);
1053 buffer[sizeof(buffer) -1] = 0;
1054 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1055 ok( (res == STATUS_SUCCESS) &&
1056 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1057 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1059 size = used;
1060 memset(buffer, '#', sizeof(buffer) - 1);
1061 buffer[sizeof(buffer) -1] = 0;
1062 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1063 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1064 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1065 res, size, buffer, used + 1);
1067 size = used - 1;
1068 memset(buffer, '#', sizeof(buffer) - 1);
1069 buffer[sizeof(buffer) -1] = 0;
1070 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1071 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1072 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1073 res, size, buffer, used + 1);
1076 /* to get only the ip, use 0 as port */
1077 port = 0;
1078 expect = ip_1234;
1080 size = sizeof(buffer);
1081 memset(buffer, '#', sizeof(buffer) - 1);
1082 buffer[sizeof(buffer) -1] = 0;
1083 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1084 used = strlen(buffer);
1085 ok( (res == STATUS_SUCCESS) &&
1086 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1087 "got 0x%x and size %d with '%s'\n", res, size, buffer);
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_SUCCESS) &&
1094 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1095 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1097 size = used;
1098 memset(buffer, '#', sizeof(buffer) - 1);
1099 buffer[sizeof(buffer) -1] = 0;
1100 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1101 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1102 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1103 res, size, buffer, used + 1);
1105 size = used - 1;
1106 memset(buffer, '#', sizeof(buffer) - 1);
1107 buffer[sizeof(buffer) -1] = 0;
1108 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1109 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1110 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1111 res, size, buffer, used + 1);
1114 /* parameters are checked */
1115 memset(buffer, '#', sizeof(buffer) - 1);
1116 buffer[sizeof(buffer) -1] = 0;
1117 res = pRtlIpv4AddressToStringExA(&ip, 0, buffer, NULL);
1118 ok(res == STATUS_INVALID_PARAMETER,
1119 "got 0x%x with '%s' (expected STATUS_INVALID_PARAMETER)\n", res, buffer);
1121 size = sizeof(buffer);
1122 res = pRtlIpv4AddressToStringExA(&ip, 0, NULL, &size);
1123 ok( res == STATUS_INVALID_PARAMETER,
1124 "got 0x%x and size %d (expected STATUS_INVALID_PARAMETER)\n", res, size);
1126 size = sizeof(buffer);
1127 memset(buffer, '#', sizeof(buffer) - 1);
1128 buffer[sizeof(buffer) -1] = 0;
1129 res = pRtlIpv4AddressToStringExA(NULL, 0, buffer, &size);
1130 ok( res == STATUS_INVALID_PARAMETER,
1131 "got 0x%x and size %d with '%s' (expected STATUS_INVALID_PARAMETER)\n",
1132 res, size, buffer);
1135 static void test_RtlIpv4StringToAddress(void)
1137 NTSTATUS res;
1138 IN_ADDR ip, expected_ip;
1139 PCSTR terminator;
1140 CHAR dummy;
1141 struct
1143 PCSTR address;
1144 NTSTATUS res;
1145 int terminator_offset;
1146 int ip[4];
1147 BOOL strict_is_different;
1148 NTSTATUS res_strict;
1149 int terminator_offset_strict;
1150 int ip_strict[4];
1151 } tests[] =
1153 { "", STATUS_INVALID_PARAMETER, 0, { -1 } },
1154 { " ", STATUS_INVALID_PARAMETER, 0, { -1 } },
1155 { "1.1.1.1", STATUS_SUCCESS, 7, { 1, 1, 1, 1 } },
1156 { "0.0.0.0", STATUS_SUCCESS, 7, { 0, 0, 0, 0 } },
1157 { "255.255.255.255", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1158 { "255.255.255.255:123",
1159 STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1160 { "255.255.255.256", STATUS_INVALID_PARAMETER, 15, { -1 } },
1161 { "255.255.255.4294967295",
1162 STATUS_INVALID_PARAMETER, 22, { -1 } },
1163 { "255.255.255.4294967296",
1164 STATUS_INVALID_PARAMETER, 21, { -1 } },
1165 { "255.255.255.4294967297",
1166 STATUS_INVALID_PARAMETER, 21, { -1 } },
1167 { "a", STATUS_INVALID_PARAMETER, 0, { -1 } },
1168 { "1.1.1.0xaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1169 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1170 { "1.1.1.0XaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1171 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1172 { "1.1.1.0x", STATUS_INVALID_PARAMETER, 8, { -1 } },
1173 { "1.1.1.0xff", STATUS_SUCCESS, 10, { 1, 1, 1, 255 },
1174 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1175 { "1.1.1.0x100", STATUS_INVALID_PARAMETER, 11, { -1 },
1176 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1177 { "1.1.1.0xffffffff",STATUS_INVALID_PARAMETER, 16, { -1 },
1178 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1179 { "1.1.1.0x100000000",
1180 STATUS_INVALID_PARAMETER, 16, { -1, 0, 0, 0 },
1181 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1182 { "1.1.1.010", STATUS_SUCCESS, 9, { 1, 1, 1, 8 },
1183 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1184 { "1.1.1.00", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1185 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1186 { "1.1.1.007", STATUS_SUCCESS, 9, { 1, 1, 1, 7 },
1187 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1188 { "1.1.1.08", STATUS_INVALID_PARAMETER, 7, { -1 } },
1189 { "1.1.1.008", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1190 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1191 { "1.1.1.0a", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1192 { "1.1.1.0o10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1193 { "1.1.1.0b10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1194 { "1.1.1.-2", STATUS_INVALID_PARAMETER, 6, { -1 } },
1195 { "1", STATUS_SUCCESS, 1, { 0, 0, 0, 1 },
1196 TRUE, STATUS_INVALID_PARAMETER, 1, { -1 } },
1197 { "-1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1198 { "203569230", STATUS_SUCCESS, 9, { 12, 34, 56, 78 },
1199 TRUE, STATUS_INVALID_PARAMETER, 9, { -1 } },
1200 { "1.223756", STATUS_SUCCESS, 8, { 1, 3, 106, 12 },
1201 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1202 { "3.4.756", STATUS_SUCCESS, 7, { 3, 4, 2, 244 },
1203 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1204 { "3.4.756.1", STATUS_INVALID_PARAMETER, 9, { -1 } },
1205 { "3.4.65536", STATUS_INVALID_PARAMETER, 9, { -1 } },
1206 { "3.4.5.6.7", STATUS_INVALID_PARAMETER, 7, { -1 } },
1207 { "3.4.5.+6", STATUS_INVALID_PARAMETER, 6, { -1 } },
1208 { " 3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1209 { "\t3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1210 { "3.4.5.6 ", STATUS_SUCCESS, 7, { 3, 4, 5, 6 } },
1211 { "3. 4.5.6", STATUS_INVALID_PARAMETER, 2, { -1 } },
1212 { ".", STATUS_INVALID_PARAMETER, 1, { -1 } },
1213 { "..", STATUS_INVALID_PARAMETER, 1, { -1 } },
1214 { "1.", STATUS_INVALID_PARAMETER, 2, { -1 } },
1215 { "1..", STATUS_INVALID_PARAMETER, 3, { -1 } },
1216 { ".1", STATUS_INVALID_PARAMETER, 1, { -1 } },
1217 { ".1.", STATUS_INVALID_PARAMETER, 1, { -1 } },
1218 { ".1.2.3", STATUS_INVALID_PARAMETER, 1, { -1 } },
1219 { "0.1.2.3", STATUS_SUCCESS, 7, { 0, 1, 2, 3 } },
1220 { "0.1.2.3.", STATUS_INVALID_PARAMETER, 7, { -1 } },
1221 { "[0.1.2.3]", STATUS_INVALID_PARAMETER, 0, { -1 } },
1222 { "::1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1223 { ":1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1225 const int testcount = sizeof(tests) / sizeof(tests[0]);
1226 int i;
1228 if (!pRtlIpv4StringToAddressA)
1230 skip("RtlIpv4StringToAddress not available\n");
1231 return;
1234 if (0)
1236 /* leaving either parameter NULL crashes on Windows */
1237 res = pRtlIpv4StringToAddressA(NULL, FALSE, &terminator, &ip);
1238 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, NULL, &ip);
1239 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, &terminator, NULL);
1240 /* same for the wide char version */
1242 res = pRtlIpv4StringToAddressW(NULL, FALSE, &terminatorW, &ip);
1243 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, NULL, &ip);
1244 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, &terminatorW, NULL);
1248 for (i = 0; i < testcount; i++)
1250 /* non-strict */
1251 terminator = &dummy;
1252 ip.S_un.S_addr = 0xabababab;
1253 res = pRtlIpv4StringToAddressA(tests[i].address, FALSE, &terminator, &ip);
1254 ok(res == tests[i].res,
1255 "[%s] res = 0x%08x, expected 0x%08x\n",
1256 tests[i].address, res, tests[i].res);
1257 ok(terminator == tests[i].address + tests[i].terminator_offset,
1258 "[%s] terminator = %p, expected %p\n",
1259 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset);
1260 if (tests[i].ip[0] == -1)
1261 expected_ip.S_un.S_addr = 0xabababab;
1262 else
1264 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip[0];
1265 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip[1];
1266 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip[2];
1267 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip[3];
1269 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1270 "[%s] ip = %08x, expected %08x\n",
1271 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1273 if (!tests[i].strict_is_different)
1275 tests[i].res_strict = tests[i].res;
1276 tests[i].terminator_offset_strict = tests[i].terminator_offset;
1277 tests[i].ip_strict[0] = tests[i].ip[0];
1278 tests[i].ip_strict[1] = tests[i].ip[1];
1279 tests[i].ip_strict[2] = tests[i].ip[2];
1280 tests[i].ip_strict[3] = tests[i].ip[3];
1282 /* strict */
1283 terminator = &dummy;
1284 ip.S_un.S_addr = 0xabababab;
1285 res = pRtlIpv4StringToAddressA(tests[i].address, TRUE, &terminator, &ip);
1286 ok(res == tests[i].res_strict,
1287 "[%s] res = 0x%08x, expected 0x%08x\n",
1288 tests[i].address, res, tests[i].res_strict);
1289 ok(terminator == tests[i].address + tests[i].terminator_offset_strict,
1290 "[%s] terminator = %p, expected %p\n",
1291 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset_strict);
1292 if (tests[i].ip_strict[0] == -1)
1293 expected_ip.S_un.S_addr = 0xabababab;
1294 else
1296 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip_strict[0];
1297 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip_strict[1];
1298 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip_strict[2];
1299 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip_strict[3];
1301 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1302 "[%s] ip = %08x, expected %08x\n",
1303 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1307 static void test_LdrAddRefDll(void)
1309 HMODULE mod, mod2;
1310 NTSTATUS status;
1311 BOOL ret;
1313 if (!pLdrAddRefDll)
1315 win_skip( "LdrAddRefDll not supported\n" );
1316 return;
1319 mod = LoadLibraryA("comctl32.dll");
1320 ok(mod != NULL, "got %p\n", mod);
1321 ret = FreeLibrary(mod);
1322 ok(ret, "got %d\n", ret);
1324 mod2 = GetModuleHandleA("comctl32.dll");
1325 ok(mod2 == NULL, "got %p\n", mod2);
1327 /* load, addref and release 2 times */
1328 mod = LoadLibraryA("comctl32.dll");
1329 ok(mod != NULL, "got %p\n", mod);
1330 status = pLdrAddRefDll(0, mod);
1331 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1332 ret = FreeLibrary(mod);
1333 ok(ret, "got %d\n", ret);
1335 mod2 = GetModuleHandleA("comctl32.dll");
1336 ok(mod2 != NULL, "got %p\n", mod2);
1337 ret = FreeLibrary(mod);
1338 ok(ret, "got %d\n", ret);
1340 mod2 = GetModuleHandleA("comctl32.dll");
1341 ok(mod2 == NULL, "got %p\n", mod2);
1343 /* pin refcount */
1344 mod = LoadLibraryA("comctl32.dll");
1345 ok(mod != NULL, "got %p\n", mod);
1346 status = pLdrAddRefDll(LDR_ADDREF_DLL_PIN, mod);
1347 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1349 ret = FreeLibrary(mod);
1350 ok(ret, "got %d\n", ret);
1351 ret = FreeLibrary(mod);
1352 ok(ret, "got %d\n", ret);
1353 ret = FreeLibrary(mod);
1354 ok(ret, "got %d\n", ret);
1355 ret = FreeLibrary(mod);
1356 ok(ret, "got %d\n", ret);
1358 mod2 = GetModuleHandleA("comctl32.dll");
1359 ok(mod2 != NULL, "got %p\n", mod2);
1362 static void test_LdrLockLoaderLock(void)
1364 ULONG_PTR magic;
1365 ULONG result;
1366 NTSTATUS status;
1368 if (!pLdrLockLoaderLock)
1370 win_skip("LdrLockLoaderLock() is not available\n");
1371 return;
1374 /* invalid flags */
1375 result = 10;
1376 magic = 0xdeadbeef;
1377 status = pLdrLockLoaderLock(0x10, &result, &magic);
1378 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
1379 ok(result == 0, "got %d\n", result);
1380 ok(magic == 0, "got %lx\n", magic);
1382 magic = 0xdeadbeef;
1383 status = pLdrLockLoaderLock(0x10, NULL, &magic);
1384 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
1385 ok(magic == 0, "got %lx\n", magic);
1387 result = 10;
1388 status = pLdrLockLoaderLock(0x10, &result, NULL);
1389 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
1390 ok(result == 0, "got %d\n", result);
1392 /* non-blocking mode, result is null */
1393 magic = 0xdeadbeef;
1394 status = pLdrLockLoaderLock(0x2, NULL, &magic);
1395 ok(status == STATUS_INVALID_PARAMETER_2, "got 0x%08x\n", status);
1396 ok(magic == 0, "got %lx\n", magic);
1398 /* magic pointer is null */
1399 result = 10;
1400 status = pLdrLockLoaderLock(0, &result, NULL);
1401 ok(status == STATUS_INVALID_PARAMETER_3, "got 0x%08x\n", status);
1402 ok(result == 0, "got %d\n", result);
1404 /* lock in non-blocking mode */
1405 result = 0;
1406 magic = 0;
1407 status = pLdrLockLoaderLock(0x2, &result, &magic);
1408 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1409 ok(result == 1, "got %d\n", result);
1410 ok(magic != 0, "got %lx\n", magic);
1411 pLdrUnlockLoaderLock(0, magic);
1414 static void test_RtlCompressBuffer(void)
1416 ULONG compress_workspace, decompress_workspace;
1417 static const UCHAR test_buffer[] = "WineWineWine";
1418 static UCHAR buf1[0x1000], buf2[0x1000];
1419 ULONG final_size, buf_size;
1420 UCHAR *workspace = NULL;
1421 NTSTATUS status;
1423 if (!pRtlCompressBuffer || !pRtlDecompressBuffer || !pRtlGetCompressionWorkSpaceSize)
1425 win_skip("skipping RtlCompressBuffer tests, required functions not available\n");
1426 return;
1429 compress_workspace = decompress_workspace = 0xdeadbeef;
1430 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
1431 &decompress_workspace);
1432 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1433 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
1434 workspace = HeapAlloc(GetProcessHeap(), 0, compress_workspace);
1435 ok(workspace != NULL, "HeapAlloc failed %d\n", GetLastError());
1437 /* test compression format / engine */
1438 final_size = 0xdeadbeef;
1439 status = pRtlCompressBuffer(COMPRESSION_FORMAT_NONE, test_buffer, sizeof(test_buffer),
1440 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
1441 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1442 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1444 final_size = 0xdeadbeef;
1445 status = pRtlCompressBuffer(COMPRESSION_FORMAT_DEFAULT, test_buffer, sizeof(test_buffer),
1446 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
1447 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1448 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1450 final_size = 0xdeadbeef;
1451 status = pRtlCompressBuffer(0xFF, test_buffer, sizeof(test_buffer),
1452 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
1453 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
1454 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1456 /* test compression */
1457 final_size = 0xdeadbeef;
1458 memset(buf1, 0x11, sizeof(buf1));
1459 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
1460 buf1, sizeof(buf1), 4096, &final_size, workspace);
1461 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1462 ok((*(WORD *)buf1 & 0x7000) == 0x3000, "no chunk signature found %04x\n", *(WORD *)buf1);
1463 todo_wine
1464 ok(final_size < sizeof(test_buffer), "got wrong final_size %u\n", final_size);
1466 /* test decompression */
1467 buf_size = final_size;
1468 final_size = 0xdeadbeef;
1469 memset(buf2, 0x11, sizeof(buf2));
1470 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf2, sizeof(buf2),
1471 buf1, buf_size, &final_size);
1472 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1473 ok(final_size == sizeof(test_buffer), "got wrong final_size %u\n", final_size);
1474 ok(!memcmp(buf2, test_buffer, sizeof(test_buffer)), "got wrong decoded data\n");
1475 ok(buf2[sizeof(test_buffer)] == 0x11, "too many bytes written\n");
1477 /* buffer too small */
1478 final_size = 0xdeadbeef;
1479 memset(buf1, 0x11, sizeof(buf1));
1480 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
1481 buf1, 4, 4096, &final_size, workspace);
1482 ok(status == STATUS_BUFFER_TOO_SMALL, "got wrong status 0x%08x\n", status);
1484 HeapFree(GetProcessHeap(), 0, workspace);
1487 static void test_RtlGetCompressionWorkSpaceSize(void)
1489 ULONG compress_workspace, decompress_workspace;
1490 NTSTATUS status;
1492 if (!pRtlGetCompressionWorkSpaceSize)
1494 win_skip("RtlGetCompressionWorkSpaceSize is not available\n");
1495 return;
1498 /* test invalid format / engine */
1499 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_NONE, &compress_workspace,
1500 &decompress_workspace);
1501 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1503 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_DEFAULT, &compress_workspace,
1504 &decompress_workspace);
1505 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1507 status = pRtlGetCompressionWorkSpaceSize(0xFF, &compress_workspace, &decompress_workspace);
1508 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
1510 /* test LZNT1 with normal and maximum compression */
1511 compress_workspace = decompress_workspace = 0xdeadbeef;
1512 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
1513 &decompress_workspace);
1514 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1515 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
1516 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
1518 compress_workspace = decompress_workspace = 0xdeadbeef;
1519 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM,
1520 &compress_workspace, &decompress_workspace);
1521 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1522 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
1523 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
1526 /* helper for test_RtlDecompressBuffer, checks if a chunk is incomplete */
1527 static BOOL is_incomplete_chunk(const UCHAR *compressed, ULONG compressed_size, BOOL check_all)
1529 ULONG chunk_size;
1531 if (compressed_size <= sizeof(WORD))
1532 return TRUE;
1534 while (compressed_size >= sizeof(WORD))
1536 chunk_size = (*(WORD *)compressed & 0xFFF) + 1;
1537 if (compressed_size < sizeof(WORD) + chunk_size)
1538 return TRUE;
1539 if (!check_all)
1540 break;
1541 compressed += sizeof(WORD) + chunk_size;
1542 compressed_size -= sizeof(WORD) + chunk_size;
1545 return FALSE;
1548 #define DECOMPRESS_BROKEN_FRAGMENT 1 /* < Win 7 */
1549 #define DECOMPRESS_BROKEN_TRUNCATED 2 /* broken on all machines */
1551 static void test_RtlDecompressBuffer(void)
1553 static const struct
1555 UCHAR compressed[32];
1556 ULONG compressed_size;
1557 NTSTATUS status;
1558 UCHAR uncompressed[32];
1559 ULONG uncompressed_size;
1560 DWORD broken_flags;
1562 test_lznt[] =
1564 /* 4 byte uncompressed chunk */
1566 {0x03, 0x30, 'W', 'i', 'n', 'e'},
1568 STATUS_SUCCESS,
1569 "Wine",
1571 DECOMPRESS_BROKEN_FRAGMENT
1573 /* 8 byte uncompressed chunk */
1575 {0x07, 0x30, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
1577 STATUS_SUCCESS,
1578 "WineWine",
1580 DECOMPRESS_BROKEN_FRAGMENT
1582 /* 4 byte compressed chunk */
1584 {0x04, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
1586 STATUS_SUCCESS,
1587 "Wine",
1590 /* 8 byte compressed chunk */
1592 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
1594 STATUS_SUCCESS,
1595 "WineWine",
1598 /* compressed chunk using backwards reference */
1600 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x01, 0x30},
1602 STATUS_SUCCESS,
1603 "WineWine",
1605 DECOMPRESS_BROKEN_TRUNCATED
1607 /* compressed chunk using backwards reference with length > bytes_read */
1609 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x30},
1611 STATUS_SUCCESS,
1612 "WineWineWine",
1614 DECOMPRESS_BROKEN_TRUNCATED
1616 /* same as above, but unused bits != 0 */
1618 {0x06, 0xB0, 0x30, 'W', 'i', 'n', 'e', 0x01, 0x30},
1620 STATUS_SUCCESS,
1621 "WineWine",
1623 DECOMPRESS_BROKEN_TRUNCATED
1625 /* compressed chunk without backwards reference and unused bits != 0 */
1627 {0x01, 0xB0, 0x02, 'W'},
1629 STATUS_SUCCESS,
1630 "W",
1633 /* termination sequence after first chunk */
1635 {0x03, 0x30, 'W', 'i', 'n', 'e', 0x00, 0x00, 0x03, 0x30, 'W', 'i', 'n', 'e'},
1637 STATUS_SUCCESS,
1638 "Wine",
1640 DECOMPRESS_BROKEN_FRAGMENT
1642 /* compressed chunk using backwards reference with 4 bit offset, 12 bit length */
1644 {0x14, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
1645 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
1646 0x01, 0x01, 0xF0},
1648 STATUS_SUCCESS,
1649 "ABCDEFGHIJKLMNOPABCD",
1651 DECOMPRESS_BROKEN_TRUNCATED
1653 /* compressed chunk using backwards reference with 5 bit offset, 11 bit length */
1655 {0x15, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
1656 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
1657 0x02, 'A', 0x00, 0x78},
1659 STATUS_SUCCESS,
1660 "ABCDEFGHIJKLMNOPABCD",
1662 DECOMPRESS_BROKEN_TRUNCATED
1664 /* uncompressed chunk with invalid magic */
1666 {0x03, 0x20, 'W', 'i', 'n', 'e'},
1668 STATUS_SUCCESS,
1669 "Wine",
1671 DECOMPRESS_BROKEN_FRAGMENT
1673 /* compressed chunk with invalid magic */
1675 {0x04, 0xA0, 0x00, 'W', 'i', 'n', 'e'},
1677 STATUS_SUCCESS,
1678 "Wine",
1681 /* garbage byte after end of buffer */
1683 {0x00, 0xB0, 0x02, 0x01},
1685 STATUS_SUCCESS,
1689 /* empty compressed chunk */
1691 {0x00, 0xB0, 0x00},
1693 STATUS_SUCCESS,
1697 /* empty compressed chunk with unused bits != 0 */
1699 {0x00, 0xB0, 0x01},
1701 STATUS_SUCCESS,
1705 /* empty input buffer */
1709 STATUS_BAD_COMPRESSION_BUFFER,
1711 /* incomplete chunk header */
1713 {0x01},
1715 STATUS_BAD_COMPRESSION_BUFFER
1717 /* incomplete chunk header */
1719 {0x00, 0x30},
1721 STATUS_BAD_COMPRESSION_BUFFER
1723 /* compressed chunk with invalid backwards reference */
1725 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x40},
1727 STATUS_BAD_COMPRESSION_BUFFER
1729 /* compressed chunk with incomplete backwards reference */
1731 {0x05, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05},
1733 STATUS_BAD_COMPRESSION_BUFFER
1735 /* incomplete uncompressed chunk */
1737 {0x07, 0x30, 'W', 'i', 'n', 'e'},
1739 STATUS_BAD_COMPRESSION_BUFFER
1741 /* incomplete compressed chunk */
1743 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
1745 STATUS_BAD_COMPRESSION_BUFFER
1747 /* two compressed chunks, the second one incomplete */
1749 {0x00, 0xB0, 0x02, 0x00, 0xB0},
1751 STATUS_BAD_COMPRESSION_BUFFER,
1755 static UCHAR buf[0x2000], workspace[0x1000];
1756 NTSTATUS status, expected_status;
1757 ULONG final_size;
1758 int i;
1760 if (!pRtlDecompressBuffer || !pRtlDecompressFragment)
1762 win_skip("RtlDecompressBuffer or RtlDecompressFragment is not available\n");
1763 return;
1766 /* test compression format / engine */
1767 final_size = 0xdeadbeef;
1768 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_NONE, buf, sizeof(buf), test_lznt[0].compressed,
1769 test_lznt[0].compressed_size, &final_size);
1770 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1771 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1773 final_size = 0xdeadbeef;
1774 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_DEFAULT, buf, sizeof(buf), test_lznt[0].compressed,
1775 test_lznt[0].compressed_size, &final_size);
1776 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1777 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1779 final_size = 0xdeadbeef;
1780 status = pRtlDecompressBuffer(0xFF, buf, sizeof(buf), test_lznt[0].compressed,
1781 test_lznt[0].compressed_size, &final_size);
1782 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
1783 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1785 /* regular tests for RtlDecompressBuffer */
1786 for (i = 0; i < sizeof(test_lznt) / sizeof(test_lznt[0]); i++)
1788 trace("Running test %d (compressed_size=%u, uncompressed_size=%u, status=0x%08x)\n",
1789 i, test_lznt[i].compressed_size, test_lznt[i].uncompressed_size, test_lznt[i].status);
1791 /* test with very big buffer */
1792 final_size = 0xdeadbeef;
1793 memset(buf, 0x11, sizeof(buf));
1794 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1795 test_lznt[i].compressed_size, &final_size);
1796 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
1797 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08x\n", i, status);
1798 if (!status)
1800 ok(final_size == test_lznt[i].uncompressed_size,
1801 "%d: got wrong final_size %u\n", i, final_size);
1802 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
1803 "%d: got wrong decoded data\n", i);
1804 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
1805 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
1808 /* test that modifier for compression engine is ignored */
1809 final_size = 0xdeadbeef;
1810 memset(buf, 0x11, sizeof(buf));
1811 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM, buf, sizeof(buf),
1812 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
1813 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
1814 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08x\n", i, status);
1815 if (!status)
1817 ok(final_size == test_lznt[i].uncompressed_size,
1818 "%d: got wrong final_size %u\n", i, final_size);
1819 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
1820 "%d: got wrong decoded data\n", i);
1821 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
1822 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
1825 /* test with expected output size */
1826 if (test_lznt[i].uncompressed_size > 0)
1828 final_size = 0xdeadbeef;
1829 memset(buf, 0x11, sizeof(buf));
1830 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size,
1831 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
1832 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1833 if (!status)
1835 ok(final_size == test_lznt[i].uncompressed_size,
1836 "%d: got wrong final_size %u\n", i, final_size);
1837 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
1838 "%d: got wrong decoded data\n", i);
1839 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
1840 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
1844 /* test with smaller output size */
1845 if (test_lznt[i].uncompressed_size > 1)
1847 final_size = 0xdeadbeef;
1848 memset(buf, 0x11, sizeof(buf));
1849 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size - 1,
1850 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
1851 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_TRUNCATED)
1852 todo_wine
1853 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1854 else
1855 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1856 if (!status)
1858 ok(final_size == test_lznt[i].uncompressed_size - 1,
1859 "%d: got wrong final_size %u\n", i, final_size);
1860 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size - 1),
1861 "%d: got wrong decoded data\n", i);
1862 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
1863 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size - 1);
1867 /* test with zero output size */
1868 final_size = 0xdeadbeef;
1869 memset(buf, 0x11, sizeof(buf));
1870 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, 0, test_lznt[i].compressed,
1871 test_lznt[i].compressed_size, &final_size);
1872 if (is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, FALSE))
1873 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1874 else
1876 ok(status == STATUS_SUCCESS, "%d: got wrong status 0x%08x\n", i, status);
1877 ok(final_size == 0, "%d: got wrong final_size %u\n", i, final_size);
1878 ok(buf[0] == 0x11, "%d: buf[0] was modified\n", i);
1881 /* test RtlDecompressFragment with offset = 0 */
1882 final_size = 0xdeadbeef;
1883 memset(buf, 0x11, sizeof(buf));
1884 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1885 test_lznt[i].compressed_size, 0, &final_size, workspace);
1886 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
1887 todo_wine
1888 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1889 else
1890 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1891 if (!status)
1893 ok(final_size == test_lznt[i].uncompressed_size,
1894 "%d: got wrong final_size %u\n", i, final_size);
1895 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
1896 "%d: got wrong decoded data\n", i);
1897 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
1898 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
1901 /* test RtlDecompressFragment with offset = 1 */
1902 final_size = 0xdeadbeef;
1903 memset(buf, 0x11, sizeof(buf));
1904 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1905 test_lznt[i].compressed_size, 1, &final_size, workspace);
1906 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
1907 todo_wine
1908 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1909 else
1910 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1911 if (!status)
1913 if (test_lznt[i].uncompressed_size == 0)
1915 todo_wine
1916 ok(final_size == 4095, "%d: got wrong final_size %u\n", i, final_size);
1917 /* Buffer doesn't contain any useful value on Windows */
1918 ok(buf[4095] == 0x11, "%d: buf[4095] was modified\n", i);
1920 else
1922 ok(final_size == test_lznt[i].uncompressed_size - 1,
1923 "%d: got wrong final_size %u\n", i, final_size);
1924 ok(!memcmp(buf, test_lznt[i].uncompressed + 1, test_lznt[i].uncompressed_size - 1),
1925 "%d: got wrong decoded data\n", i);
1926 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
1927 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size - 1);
1931 /* test RtlDecompressFragment with offset = 4095 */
1932 final_size = 0xdeadbeef;
1933 memset(buf, 0x11, sizeof(buf));
1934 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1935 test_lznt[i].compressed_size, 4095, &final_size, workspace);
1936 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
1937 todo_wine
1938 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
1939 else
1940 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
1941 if (!status)
1943 todo_wine
1944 ok(final_size == 1, "%d: got wrong final_size %u\n", i, final_size);
1945 todo_wine
1946 ok(buf[0] == 0, "%d: padding is not zero\n", i);
1947 ok(buf[1] == 0x11, "%d: buf[1] was modified\n", i);
1950 /* test RtlDecompressFragment with offset = 4096 */
1951 final_size = 0xdeadbeef;
1952 memset(buf, 0x11, sizeof(buf));
1953 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
1954 test_lznt[i].compressed_size, 4096, &final_size, workspace);
1955 expected_status = is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, TRUE) ?
1956 test_lznt[i].status : STATUS_SUCCESS;
1957 ok(status == expected_status, "%d: got wrong status 0x%08x, expected 0x%08x\n", i, status, expected_status);
1958 if (!status)
1960 ok(final_size == 0, "%d: got wrong final_size %u\n", i, final_size);
1961 ok(buf[0] == 0x11, "%d: buf[4096] was modified\n", i);
1966 #undef DECOMPRESS_BROKEN_FRAGMENT
1967 #undef DECOMPRESS_BROKEN_TRUNCATED
1969 struct critsect_locked_info
1971 CRITICAL_SECTION crit;
1972 HANDLE semaphores[2];
1975 static DWORD WINAPI critsect_locked_thread(void *param)
1977 struct critsect_locked_info *info = param;
1978 DWORD ret;
1980 ret = pRtlIsCriticalSectionLocked(&info->crit);
1981 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
1982 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
1983 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
1985 ReleaseSemaphore(info->semaphores[0], 1, NULL);
1986 ret = WaitForSingleObject(info->semaphores[1], 1000);
1987 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
1989 ret = pRtlIsCriticalSectionLocked(&info->crit);
1990 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
1991 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
1992 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
1994 EnterCriticalSection(&info->crit);
1996 ret = pRtlIsCriticalSectionLocked(&info->crit);
1997 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
1998 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
1999 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2001 ReleaseSemaphore(info->semaphores[0], 1, NULL);
2002 ret = WaitForSingleObject(info->semaphores[1], 1000);
2003 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2005 LeaveCriticalSection(&info->crit);
2006 return 0;
2009 static void test_RtlIsCriticalSectionLocked(void)
2011 struct critsect_locked_info info;
2012 HANDLE thread;
2013 BOOL ret;
2015 if (!pRtlIsCriticalSectionLocked || !pRtlIsCriticalSectionLockedByThread)
2017 win_skip("skipping RtlIsCriticalSectionLocked tests, required functions not available\n");
2018 return;
2021 InitializeCriticalSection(&info.crit);
2022 info.semaphores[0] = CreateSemaphoreW(NULL, 0, 1, NULL);
2023 ok(info.semaphores[0] != NULL, "CreateSemaphore failed with %u\n", GetLastError());
2024 info.semaphores[1] = CreateSemaphoreW(NULL, 0, 1, NULL);
2025 ok(info.semaphores[1] != NULL, "CreateSemaphore failed with %u\n", GetLastError());
2027 ret = pRtlIsCriticalSectionLocked(&info.crit);
2028 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2029 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2030 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2032 EnterCriticalSection(&info.crit);
2034 ret = pRtlIsCriticalSectionLocked(&info.crit);
2035 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2036 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2037 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2039 thread = CreateThread(NULL, 0, critsect_locked_thread, &info, 0, NULL);
2040 ok(thread != NULL, "CreateThread failed with %u\n", GetLastError());
2041 ret = WaitForSingleObject(info.semaphores[0], 1000);
2042 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2044 LeaveCriticalSection(&info.crit);
2046 ReleaseSemaphore(info.semaphores[1], 1, NULL);
2047 ret = WaitForSingleObject(info.semaphores[0], 1000);
2048 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2050 ret = pRtlIsCriticalSectionLocked(&info.crit);
2051 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2052 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2053 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2055 ReleaseSemaphore(info.semaphores[1], 1, NULL);
2056 ret = WaitForSingleObject(thread, 1000);
2057 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2059 CloseHandle(thread);
2060 CloseHandle(info.semaphores[0]);
2061 CloseHandle(info.semaphores[1]);
2062 DeleteCriticalSection(&info.crit);
2065 static void test_RtlInitializeCriticalSectionEx(void)
2067 static const CRITICAL_SECTION_DEBUG *no_debug = (void *)~(ULONG_PTR)0;
2068 CRITICAL_SECTION cs;
2070 if (!pRtlInitializeCriticalSectionEx)
2072 win_skip("RtlInitializeCriticalSectionEx is not available\n");
2073 return;
2076 memset(&cs, 0x11, sizeof(cs));
2077 pRtlInitializeCriticalSectionEx(&cs, 0, 0);
2078 ok((cs.DebugInfo != NULL && cs.DebugInfo != no_debug) || broken(cs.DebugInfo == no_debug) /* >= Win 8 */,
2079 "expected DebugInfo != NULL and DebugInfo != ~0, got %p\n", cs.DebugInfo);
2080 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2081 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2082 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
2083 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
2084 "expected SpinCount == 0, got %ld\n", cs.SpinCount);
2085 RtlDeleteCriticalSection(&cs);
2087 memset(&cs, 0x11, sizeof(cs));
2088 pRtlInitializeCriticalSectionEx(&cs, 0, RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO);
2089 todo_wine
2090 ok(cs.DebugInfo == no_debug, "expected DebugInfo == ~0, got %p\n", cs.DebugInfo);
2091 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2092 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2093 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
2094 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
2095 "expected SpinCount == 0, got %ld\n", cs.SpinCount);
2096 RtlDeleteCriticalSection(&cs);
2099 static void test_RtlLeaveCriticalSection(void)
2101 RTL_CRITICAL_SECTION cs;
2102 NTSTATUS status;
2104 if (!pRtlInitializeCriticalSectionEx)
2105 return; /* Skip winxp */
2107 status = RtlInitializeCriticalSection(&cs);
2108 ok(!status, "RtlInitializeCriticalSection failed: %x\n", status);
2110 status = RtlEnterCriticalSection(&cs);
2111 ok(!status, "RtlEnterCriticalSection failed: %x\n", status);
2112 todo_wine
2113 ok(cs.LockCount == -2, "expected LockCount == -2, got %d\n", cs.LockCount);
2114 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %d\n", cs.RecursionCount);
2115 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
2117 status = RtlLeaveCriticalSection(&cs);
2118 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2119 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2120 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2121 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2124 * Trying to leave a section that wasn't acquired modifies RecursionCount to an invalid value,
2125 * but doesn't modify LockCount so that an attempt to enter the section later will work.
2127 status = RtlLeaveCriticalSection(&cs);
2128 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2129 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2130 ok(cs.RecursionCount == -1, "expected RecursionCount == -1, got %d\n", cs.RecursionCount);
2131 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2133 /* and again */
2134 status = RtlLeaveCriticalSection(&cs);
2135 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2136 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2137 ok(cs.RecursionCount == -2, "expected RecursionCount == -2, got %d\n", cs.RecursionCount);
2138 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2140 /* entering section fixes RecursionCount */
2141 status = RtlEnterCriticalSection(&cs);
2142 ok(!status, "RtlEnterCriticalSection failed: %x\n", status);
2143 todo_wine
2144 ok(cs.LockCount == -2, "expected LockCount == -2, got %d\n", cs.LockCount);
2145 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %d\n", cs.RecursionCount);
2146 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
2148 status = RtlLeaveCriticalSection(&cs);
2149 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
2150 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
2151 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
2152 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
2154 status = RtlDeleteCriticalSection(&cs);
2155 ok(!status, "RtlDeleteCriticalSection failed: %x\n", status);
2158 struct ldr_enum_context
2160 BOOL abort;
2161 BOOL found;
2162 int count;
2165 static void WINAPI ldr_enum_callback(LDR_MODULE *module, void *context, BOOLEAN *stop)
2167 static const WCHAR ntdllW[] = {'n','t','d','l','l','.','d','l','l',0};
2168 struct ldr_enum_context *ctx = context;
2170 if (!lstrcmpiW(module->BaseDllName.Buffer, ntdllW))
2171 ctx->found = TRUE;
2173 ctx->count++;
2174 *stop = ctx->abort;
2177 static void test_LdrEnumerateLoadedModules(void)
2179 struct ldr_enum_context ctx;
2180 NTSTATUS status;
2182 if (!pLdrEnumerateLoadedModules)
2184 win_skip("LdrEnumerateLoadedModules not available\n");
2185 return;
2188 ctx.abort = FALSE;
2189 ctx.found = FALSE;
2190 ctx.count = 0;
2191 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
2192 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08x\n", status);
2193 ok(ctx.count > 1, "Expected more than one module, got %d\n", ctx.count);
2194 ok(ctx.found, "Could not find ntdll in list of modules\n");
2196 ctx.abort = TRUE;
2197 ctx.count = 0;
2198 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
2199 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08x\n", status);
2200 ok(ctx.count == 1, "Expected exactly one module, got %d\n", ctx.count);
2202 status = pLdrEnumerateLoadedModules((void *)0x1, ldr_enum_callback, (void *)0xdeadbeef);
2203 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
2205 status = pLdrEnumerateLoadedModules((void *)0xdeadbeef, ldr_enum_callback, (void *)0xdeadbeef);
2206 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
2208 status = pLdrEnumerateLoadedModules(NULL, NULL, (void *)0xdeadbeef);
2209 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
2212 START_TEST(rtl)
2214 InitFunctionPtrs();
2216 test_RtlCompareMemory();
2217 test_RtlCompareMemoryUlong();
2218 test_RtlMoveMemory();
2219 test_RtlFillMemory();
2220 test_RtlFillMemoryUlong();
2221 test_RtlZeroMemory();
2222 test_RtlUlonglongByteSwap();
2223 test_RtlUniform();
2224 test_RtlRandom();
2225 test_RtlAreAllAccessesGranted();
2226 test_RtlAreAnyAccessesGranted();
2227 test_RtlComputeCrc32();
2228 test_HandleTables();
2229 test_RtlAllocateAndInitializeSid();
2230 test_RtlDeleteTimer();
2231 test_RtlThreadErrorMode();
2232 test_LdrProcessRelocationBlock();
2233 test_RtlIpv4AddressToString();
2234 test_RtlIpv4AddressToStringEx();
2235 test_RtlIpv4StringToAddress();
2236 test_LdrAddRefDll();
2237 test_LdrLockLoaderLock();
2238 test_RtlCompressBuffer();
2239 test_RtlGetCompressionWorkSpaceSize();
2240 test_RtlDecompressBuffer();
2241 test_RtlIsCriticalSectionLocked();
2242 test_RtlInitializeCriticalSectionEx();
2243 test_RtlLeaveCriticalSection();
2244 test_LdrEnumerateLoadedModules();