ntdll: Implement RtlDecompressFragment.
[wine.git] / dlls / ntdll / tests / rtl.c
blob99fd41301366d8fec42c2a156a1a3559a487dcf9
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 struct _TEB * (WINAPI *pNtCurrentTeb)(void);
86 static DWORD (WINAPI *pRtlGetThreadErrorMode)(void);
87 static NTSTATUS (WINAPI *pRtlSetThreadErrorMode)(DWORD, LPDWORD);
88 static IMAGE_BASE_RELOCATION *(WINAPI *pLdrProcessRelocationBlock)(void*,UINT,USHORT*,INT_PTR);
89 static CHAR * (WINAPI *pRtlIpv4AddressToStringA)(const IN_ADDR *, LPSTR);
90 static NTSTATUS (WINAPI *pRtlIpv4AddressToStringExA)(const IN_ADDR *, USHORT, LPSTR, PULONG);
91 static NTSTATUS (WINAPI *pRtlIpv4StringToAddressA)(PCSTR, BOOLEAN, PCSTR *, IN_ADDR *);
92 static NTSTATUS (WINAPI *pLdrAddRefDll)(ULONG, HMODULE);
93 static NTSTATUS (WINAPI *pLdrLockLoaderLock)(ULONG, ULONG*, ULONG_PTR*);
94 static NTSTATUS (WINAPI *pLdrUnlockLoaderLock)(ULONG, ULONG_PTR);
95 static NTSTATUS (WINAPI *pRtlGetCompressionWorkSpaceSize)(USHORT, PULONG, PULONG);
96 static NTSTATUS (WINAPI *pRtlDecompressBuffer)(USHORT, PUCHAR, ULONG, const UCHAR*, ULONG, PULONG);
97 static NTSTATUS (WINAPI *pRtlCompressBuffer)(USHORT, const UCHAR*, ULONG, PUCHAR, ULONG, ULONG, PULONG, PVOID);
99 static HMODULE hkernel32 = 0;
100 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
103 #define LEN 16
104 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
105 static ULONG src_aligned_block[4];
106 static ULONG dest_aligned_block[32];
107 static const char *src = (const char*)src_aligned_block;
108 static char* dest = (char*)dest_aligned_block;
110 static void InitFunctionPtrs(void)
112 hntdll = LoadLibraryA("ntdll.dll");
113 ok(hntdll != 0, "LoadLibrary failed\n");
114 if (hntdll) {
115 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
116 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
117 pRtlDeleteTimer = (void *)GetProcAddress(hntdll, "RtlDeleteTimer");
118 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
119 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
120 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
121 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
122 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
123 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
124 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
125 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
126 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
127 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
128 pRtlInitializeHandleTable = (void *)GetProcAddress(hntdll, "RtlInitializeHandleTable");
129 pRtlIsValidIndexHandle = (void *)GetProcAddress(hntdll, "RtlIsValidIndexHandle");
130 pRtlDestroyHandleTable = (void *)GetProcAddress(hntdll, "RtlDestroyHandleTable");
131 pRtlAllocateHandle = (void *)GetProcAddress(hntdll, "RtlAllocateHandle");
132 pRtlFreeHandle = (void *)GetProcAddress(hntdll, "RtlFreeHandle");
133 pRtlAllocateAndInitializeSid = (void *)GetProcAddress(hntdll, "RtlAllocateAndInitializeSid");
134 pRtlFreeSid = (void *)GetProcAddress(hntdll, "RtlFreeSid");
135 pNtCurrentTeb = (void *)GetProcAddress(hntdll, "NtCurrentTeb");
136 pRtlGetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlGetThreadErrorMode");
137 pRtlSetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlSetThreadErrorMode");
138 pLdrProcessRelocationBlock = (void *)GetProcAddress(hntdll, "LdrProcessRelocationBlock");
139 pRtlIpv4AddressToStringA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringA");
140 pRtlIpv4AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringExA");
141 pRtlIpv4StringToAddressA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressA");
142 pLdrAddRefDll = (void *)GetProcAddress(hntdll, "LdrAddRefDll");
143 pLdrLockLoaderLock = (void *)GetProcAddress(hntdll, "LdrLockLoaderLock");
144 pLdrUnlockLoaderLock = (void *)GetProcAddress(hntdll, "LdrUnlockLoaderLock");
145 pRtlGetCompressionWorkSpaceSize = (void *)GetProcAddress(hntdll, "RtlGetCompressionWorkSpaceSize");
146 pRtlDecompressBuffer = (void *)GetProcAddress(hntdll, "RtlDecompressBuffer");
147 pRtlCompressBuffer = (void *)GetProcAddress(hntdll, "RtlCompressBuffer");
149 hkernel32 = LoadLibraryA("kernel32.dll");
150 ok(hkernel32 != 0, "LoadLibrary failed\n");
151 if (hkernel32) {
152 pIsWow64Process = (void *)GetProcAddress(hkernel32, "IsWow64Process");
154 strcpy((char*)src_aligned_block, src_src);
155 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
158 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
159 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
161 static void test_RtlCompareMemory(void)
163 SIZE_T size;
165 if (!pRtlCompareMemory)
167 win_skip("RtlCompareMemory is not available\n");
168 return;
171 strcpy(dest, src);
173 COMP(src,src,0,0);
174 COMP(src,src,LEN,LEN);
175 dest[0] = 'x';
176 COMP(src,dest,LEN,0);
179 static void test_RtlCompareMemoryUlong(void)
181 ULONG a[10];
182 ULONG result;
184 if (!pRtlCompareMemoryUlong)
186 win_skip("RtlCompareMemoryUlong is not available\n");
187 return;
190 a[0]= 0x0123;
191 a[1]= 0x4567;
192 a[2]= 0x89ab;
193 a[3]= 0xcdef;
194 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
195 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %u, expected 0\n", a, result);
196 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
197 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
198 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
199 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
200 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
201 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
202 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
203 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
204 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
205 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 4\n", a, result);
206 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
207 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 4\n", a, result);
208 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
209 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %u, expected 0\n", a, result);
210 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
211 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %u, expected 0\n", a, result);
212 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
213 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %u, expected 0\n", a, result);
215 a[1]= 0x0123;
216 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
217 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
218 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
219 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
220 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
221 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
222 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
223 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
224 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
225 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 8\n", a, result);
226 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
227 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 8\n", a, result);
230 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
231 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
233 static void test_RtlMoveMemory(void)
235 if (!pRtlMoveMemory)
237 win_skip("RtlMoveMemory is not available\n");
238 return;
241 /* Length should be in bytes and not rounded. Use strcmp to ensure we
242 * didn't write past the end (it checks for the final NUL left by memset)
244 COPY(0); CMP("");
245 COPY(1); CMP("T");
246 COPY(2); CMP("Th");
247 COPY(3); CMP("Thi");
248 COPY(4); CMP("This");
249 COPY(5); CMP("This ");
250 COPY(6); CMP("This i");
251 COPY(7); CMP("This is");
252 COPY(8); CMP("This is ");
253 COPY(9); CMP("This is a");
255 /* Overlapping */
256 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
257 CMP("his is a test!!");
258 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
259 CMP("TThis is a test!");
262 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
264 static void test_RtlFillMemory(void)
266 if (!pRtlFillMemory)
268 win_skip("RtlFillMemory is not available\n");
269 return;
272 /* Length should be in bytes and not rounded. Use strcmp to ensure we
273 * didn't write past the end (the remainder of the string should match)
275 FILL(0); CMP("This is a test!");
276 FILL(1); CMP("xhis is a test!");
277 FILL(2); CMP("xxis is a test!");
278 FILL(3); CMP("xxxs is a test!");
279 FILL(4); CMP("xxxx is a test!");
280 FILL(5); CMP("xxxxxis a test!");
281 FILL(6); CMP("xxxxxxs a test!");
282 FILL(7); CMP("xxxxxxx a test!");
283 FILL(8); CMP("xxxxxxxxa test!");
284 FILL(9); CMP("xxxxxxxxx test!");
287 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
289 static void test_RtlFillMemoryUlong(void)
291 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
292 if (!pRtlFillMemoryUlong)
294 win_skip("RtlFillMemoryUlong is not available\n");
295 return;
298 /* Length should be in bytes and not rounded. Use strcmp to ensure we
299 * didn't write past the end (the remainder of the string should match)
301 LFILL(0); CMP("This is a test!");
302 LFILL(1); CMP("This is a test!");
303 LFILL(2); CMP("This is a test!");
304 LFILL(3); CMP("This is a test!");
305 LFILL(4); CMP("xxxx is a test!");
306 LFILL(5); CMP("xxxx is a test!");
307 LFILL(6); CMP("xxxx is a test!");
308 LFILL(7); CMP("xxxx is a test!");
309 LFILL(8); CMP("xxxxxxxxa test!");
310 LFILL(9); CMP("xxxxxxxxa test!");
313 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
314 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
316 static void test_RtlZeroMemory(void)
318 if (!pRtlZeroMemory)
320 win_skip("RtlZeroMemory is not available\n");
321 return;
324 /* Length should be in bytes and not rounded. */
325 ZERO(0); MCMP("This is a test!");
326 ZERO(1); MCMP("\0his is a test!");
327 ZERO(2); MCMP("\0\0is is a test!");
328 ZERO(3); MCMP("\0\0\0s is a test!");
329 ZERO(4); MCMP("\0\0\0\0 is a test!");
330 ZERO(5); MCMP("\0\0\0\0\0is a test!");
331 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
332 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
333 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
334 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
337 static void test_RtlUlonglongByteSwap(void)
339 ULONGLONG result;
341 if ( !pRtlUlonglongByteSwap )
343 win_skip("RtlUlonglongByteSwap is not available\n");
344 return;
347 if ( pRtlUlonglongByteSwap( 0 ) != 0 )
349 win_skip("Broken RtlUlonglongByteSwap in win2k\n");
350 return;
353 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
354 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
355 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%x%08x, expected 0x2143658710325476\n",
356 (DWORD)(result >> 32), (DWORD)result);
360 static void test_RtlUniform(void)
362 ULONGLONG num;
363 ULONG seed;
364 ULONG seed_bak;
365 ULONG expected;
366 ULONG result;
368 if (!pRtlUniform)
370 win_skip("RtlUniform is not available\n");
371 return;
375 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
376 * algorithm. This algorithm is:
378 * seed = (seed * const_1 + const_2) % const_3;
380 * According to the documentation the random number is distributed over
381 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
383 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
385 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
386 * algorithm can be expressed without division as:
388 * seed = (seed * const_1 + const_2) & MAXLONG;
390 * To find out const_2 we just call RtlUniform with seed set to 0:
392 seed = 0;
393 expected = 0x7fffffc3;
394 result = pRtlUniform(&seed);
395 ok(result == expected,
396 "RtlUniform(&seed (seed == 0)) returns %x, expected %x\n",
397 result, expected);
399 * The algorithm is now:
401 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
403 * To find out const_1 we can use:
405 * const_1 = RtlUniform(1) - 0x7fffffc3;
407 * If that does not work a search loop can try all possible values of
408 * const_1 and compare to the result to RtlUniform(1).
409 * This way we find out that const_1 is 0xffffffed.
411 * For seed = 1 the const_2 is 0x7fffffc4:
413 seed = 1;
414 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
415 result = pRtlUniform(&seed);
416 ok(result == expected,
417 "RtlUniform(&seed (seed == 1)) returns %x, expected %x\n",
418 result, expected);
420 * For seed = 2 the const_2 is 0x7fffffc3:
422 seed = 2;
423 expected = seed * 0xffffffed + 0x7fffffc3;
424 result = pRtlUniform(&seed);
427 * Windows Vista uses different algorithms, so skip the rest of the tests
428 * until that is figured out. Trace output for the failures is about 10.5 MB!
431 if (result == 0x7fffff9f) {
432 skip("Most likely running on Windows Vista which uses a different algorithm\n");
433 return;
436 ok(result == expected,
437 "RtlUniform(&seed (seed == 2)) returns %x, expected %x\n",
438 result, expected);
441 * More tests show that if seed is odd the result must be incremented by 1:
443 seed = 3;
444 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
445 result = pRtlUniform(&seed);
446 ok(result == expected,
447 "RtlUniform(&seed (seed == 3)) returns %x, expected %x\n",
448 result, expected);
450 seed = 0x6bca1aa;
451 expected = seed * 0xffffffed + 0x7fffffc3;
452 result = pRtlUniform(&seed);
453 ok(result == expected,
454 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %x, expected %x\n",
455 result, expected);
457 seed = 0x6bca1ab;
458 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
459 result = pRtlUniform(&seed);
460 ok(result == expected,
461 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %x, expected %x\n",
462 result, expected);
464 * When seed is 0x6bca1ac there is an exception:
466 seed = 0x6bca1ac;
467 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
468 result = pRtlUniform(&seed);
469 ok(result == expected,
470 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %x, expected %x\n",
471 result, expected);
473 * Note that up to here const_3 is not used
474 * (the highest bit of the result is not set).
476 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
478 seed = 0x6bca1ad;
479 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
480 result = pRtlUniform(&seed);
481 ok(result == expected,
482 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %x, expected %x\n",
483 result, expected);
485 seed = 0x6bca1ae;
486 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
487 result = pRtlUniform(&seed);
488 ok(result == expected,
489 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %x, expected %x\n",
490 result, expected);
492 * There are several ranges where for odd or even seed the result must be
493 * incremented by 1. You can see this ranges in the following test.
495 * For a full test use one of the following loop heads:
497 * for (num = 0; num <= 0xffffffff; num++) {
498 * seed = num;
499 * ...
501 * seed = 0;
502 * for (num = 0; num <= 0xffffffff; num++) {
503 * ...
505 seed = 0;
506 for (num = 0; num <= 100000; num++) {
508 expected = seed * 0xffffffed + 0x7fffffc3;
509 if (seed < 0x6bca1ac) {
510 expected = expected + (seed & 1);
511 } else if (seed == 0x6bca1ac) {
512 expected = (expected + 2) & MAXLONG;
513 } else if (seed < 0xd79435c) {
514 expected = (expected + (~seed & 1)) & MAXLONG;
515 } else if (seed < 0x1435e50b) {
516 expected = expected + (seed & 1);
517 } else if (seed < 0x1af286ba) {
518 expected = (expected + (~seed & 1)) & MAXLONG;
519 } else if (seed < 0x21af2869) {
520 expected = expected + (seed & 1);
521 } else if (seed < 0x286bca18) {
522 expected = (expected + (~seed & 1)) & MAXLONG;
523 } else if (seed < 0x2f286bc7) {
524 expected = expected + (seed & 1);
525 } else if (seed < 0x35e50d77) {
526 expected = (expected + (~seed & 1)) & MAXLONG;
527 } else if (seed < 0x3ca1af26) {
528 expected = expected + (seed & 1);
529 } else if (seed < 0x435e50d5) {
530 expected = (expected + (~seed & 1)) & MAXLONG;
531 } else if (seed < 0x4a1af284) {
532 expected = expected + (seed & 1);
533 } else if (seed < 0x50d79433) {
534 expected = (expected + (~seed & 1)) & MAXLONG;
535 } else if (seed < 0x579435e2) {
536 expected = expected + (seed & 1);
537 } else if (seed < 0x5e50d792) {
538 expected = (expected + (~seed & 1)) & MAXLONG;
539 } else if (seed < 0x650d7941) {
540 expected = expected + (seed & 1);
541 } else if (seed < 0x6bca1af0) {
542 expected = (expected + (~seed & 1)) & MAXLONG;
543 } else if (seed < 0x7286bc9f) {
544 expected = expected + (seed & 1);
545 } else if (seed < 0x79435e4e) {
546 expected = (expected + (~seed & 1)) & MAXLONG;
547 } else if (seed < 0x7ffffffd) {
548 expected = expected + (seed & 1);
549 } else if (seed < 0x86bca1ac) {
550 expected = (expected + (~seed & 1)) & MAXLONG;
551 } else if (seed == 0x86bca1ac) {
552 expected = (expected + 1) & MAXLONG;
553 } else if (seed < 0x8d79435c) {
554 expected = expected + (seed & 1);
555 } else if (seed < 0x9435e50b) {
556 expected = (expected + (~seed & 1)) & MAXLONG;
557 } else if (seed < 0x9af286ba) {
558 expected = expected + (seed & 1);
559 } else if (seed < 0xa1af2869) {
560 expected = (expected + (~seed & 1)) & MAXLONG;
561 } else if (seed < 0xa86bca18) {
562 expected = expected + (seed & 1);
563 } else if (seed < 0xaf286bc7) {
564 expected = (expected + (~seed & 1)) & MAXLONG;
565 } else if (seed == 0xaf286bc7) {
566 expected = (expected + 2) & MAXLONG;
567 } else if (seed < 0xb5e50d77) {
568 expected = expected + (seed & 1);
569 } else if (seed < 0xbca1af26) {
570 expected = (expected + (~seed & 1)) & MAXLONG;
571 } else if (seed < 0xc35e50d5) {
572 expected = expected + (seed & 1);
573 } else if (seed < 0xca1af284) {
574 expected = (expected + (~seed & 1)) & MAXLONG;
575 } else if (seed < 0xd0d79433) {
576 expected = expected + (seed & 1);
577 } else if (seed < 0xd79435e2) {
578 expected = (expected + (~seed & 1)) & MAXLONG;
579 } else if (seed < 0xde50d792) {
580 expected = expected + (seed & 1);
581 } else if (seed < 0xe50d7941) {
582 expected = (expected + (~seed & 1)) & MAXLONG;
583 } else if (seed < 0xebca1af0) {
584 expected = expected + (seed & 1);
585 } else if (seed < 0xf286bc9f) {
586 expected = (expected + (~seed & 1)) & MAXLONG;
587 } else if (seed < 0xf9435e4e) {
588 expected = expected + (seed & 1);
589 } else if (seed < 0xfffffffd) {
590 expected = (expected + (~seed & 1)) & MAXLONG;
591 } else {
592 expected = expected + (seed & 1);
593 } /* if */
594 seed_bak = seed;
595 result = pRtlUniform(&seed);
596 ok(result == expected,
597 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
598 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
599 ok(seed == expected,
600 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
601 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
602 } /* for */
604 * Further investigation shows: In the different regions the highest bit
605 * is set or cleared when even or odd seeds need an increment by 1.
606 * This leads to a simplified algorithm:
608 * seed = seed * 0xffffffed + 0x7fffffc3;
609 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
610 * seed = (seed + 2) & MAXLONG;
611 * } else if (seed == 0x7fffffff) {
612 * seed = 0;
613 * } else if ((seed & 0x80000000) == 0) {
614 * seed = seed + (~seed & 1);
615 * } else {
616 * seed = (seed + (seed & 1)) & MAXLONG;
619 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
621 * Now comes the funny part:
622 * It took me one weekend, to find the complicated algorithm and one day more,
623 * to find the simplified algorithm. Several weeks later I found out: The value
624 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
625 * nor with the simplified algorithm. In reality the native function and our
626 * function return a random number distributed over [0..MAXLONG-1]. Note
627 * that this is different from what native documentation states [0..MAXLONG].
628 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
630 * seed = (seed * const_1 + const_2) % MAXLONG;
632 * Further investigations show that the real algorithm is:
634 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
636 * This is checked with the test below:
638 seed = 0;
639 for (num = 0; num <= 100000; num++) {
640 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
641 seed_bak = seed;
642 result = pRtlUniform(&seed);
643 ok(result == expected,
644 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
645 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
646 ok(seed == expected,
647 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
648 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
649 } /* for */
651 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
652 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
653 * that there is more than one cycle of generated randon numbers ...
658 static ULONG my_RtlRandom(PULONG seed)
660 static ULONG saved_value[128] =
661 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
662 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
663 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
664 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
665 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
666 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
667 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
668 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
669 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
670 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
671 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
672 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
673 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
674 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
675 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
676 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
677 ULONG rand;
678 int pos;
679 ULONG result;
681 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
682 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
683 pos = *seed & 0x7f;
684 result = saved_value[pos];
685 saved_value[pos] = rand;
686 return(result);
690 static void test_RtlRandom(void)
692 ULONGLONG num;
693 ULONG seed;
694 ULONG seed_bak;
695 ULONG seed_expected;
696 ULONG result;
697 ULONG result_expected;
699 if (!pRtlRandom)
701 win_skip("RtlRandom is not available\n");
702 return;
706 * Unlike RtlUniform, RtlRandom is not documented. We guess that for
707 * RtlRandom D.H. Lehmer's 1948 algorithm is used like stated in
708 * the documentation of the RtlUniform function. This algorithm is:
710 * seed = (seed * const_1 + const_2) % const_3;
712 * According to the RtlUniform documentation the random number is
713 * distributed over [0..MAXLONG], but in reality it is distributed
714 * over [0..MAXLONG-1]. Therefore const_3 might be MAXLONG + 1 or
715 * MAXLONG:
717 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
719 * or
721 * seed = (seed * const_1 + const_2) % MAXLONG;
723 * To find out const_2 we just call RtlRandom with seed set to 0:
725 seed = 0;
726 result_expected = 0x320a1743;
727 seed_expected =0x44b;
728 result = pRtlRandom(&seed);
731 * Windows Vista uses different algorithms, so skip the rest of the tests
732 * until that is figured out. Trace output for the failures is about 10.5 MB!
735 if (seed == 0x3fc) {
736 skip("Most likely running on Windows Vista which uses a different algorithm\n");
737 return;
740 ok(result == result_expected,
741 "pRtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
742 result, result_expected);
743 ok(seed == seed_expected,
744 "pRtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
745 seed, seed_expected);
747 * Seed is not equal to result as with RtlUniform. To see more we
748 * call RtlRandom again with seed set to 0:
750 seed = 0;
751 result_expected = 0x7fffffc3;
752 seed_expected =0x44b;
753 result = pRtlRandom(&seed);
754 ok(result == result_expected,
755 "RtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
756 result, result_expected);
757 ok(seed == seed_expected,
758 "RtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
759 seed, seed_expected);
761 * Seed is set to the same value as before but the result is different.
762 * To see more we call RtlRandom again with seed set to 0:
764 seed = 0;
765 result_expected = 0x7fffffc3;
766 seed_expected =0x44b;
767 result = pRtlRandom(&seed);
768 ok(result == result_expected,
769 "RtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
770 result, result_expected);
771 ok(seed == seed_expected,
772 "RtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
773 seed, seed_expected);
775 * Seed is again set to the same value as before. This time we also
776 * have the same result as before. Interestingly the value of the
777 * result is 0x7fffffc3 which is the same value used in RtlUniform
778 * as const_2. If we do
780 * seed = 0;
781 * result = RtlUniform(&seed);
783 * we get the same result (0x7fffffc3) as with
785 * seed = 0;
786 * RtlRandom(&seed);
787 * seed = 0;
788 * result = RtlRandom(&seed);
790 * And there is another interesting thing. If we do
792 * seed = 0;
793 * RtlUniform(&seed);
794 * RtlUniform(&seed);
796 * seed is set to the value 0x44b which ist the same value that
798 * seed = 0;
799 * RtlRandom(&seed);
801 * assigns to seed. Putting these two findings together leads to
802 * the conclusion that RtlRandom saves the value in some variable,
803 * like in the following algorithm:
805 * result = saved_value;
806 * saved_value = RtlUniform(&seed);
807 * RtlUniform(&seed);
808 * return(result);
810 * Now we do further tests with seed set to 1:
812 seed = 1;
813 result_expected = 0x7a50bbc6;
814 seed_expected =0x5a1;
815 result = pRtlRandom(&seed);
816 ok(result == result_expected,
817 "RtlRandom(&seed (seed == 1)) returns %x, expected %x\n",
818 result, result_expected);
819 ok(seed == seed_expected,
820 "RtlRandom(&seed (seed == 1)) sets seed to %x, expected %x\n",
821 seed, seed_expected);
823 * If there is just one saved_value the result now would be
824 * 0x7fffffc3. From this test we can see that there is more than
825 * one saved_value, like with this algorithm:
827 * result = saved_value[pos];
828 * saved_value[pos] = RtlUniform(&seed);
829 * RtlUniform(&seed);
830 * return(result);
832 * But how is the value of pos determined? The calls to RtlUniform
833 * create a sequence of random numbers. Every second random number
834 * is put into the saved_value array and is used in some later call
835 * of RtlRandom as result. The only reasonable source to determine
836 * pos are the random numbers generated by RtlUniform which are not
837 * put into the saved_value array. This are the values of seed
838 * between the two calls of RtlUniform as in this algorithm:
840 * rand = RtlUniform(&seed);
841 * RtlUniform(&seed);
842 * pos = position(seed);
843 * result = saved_value[pos];
844 * saved_value[pos] = rand;
845 * return(result);
847 * What remains to be determined is: The size of the saved_value array,
848 * the initial values of the saved_value array and the function
849 * position(seed). These tests are not shown here.
850 * The result of these tests is: The size of the saved_value array
851 * is 128, the initial values can be seen in the my_RtlRandom
852 * function and the position(seed) function is (seed & 0x7f).
854 * For a full test of RtlRandom use one of the following loop heads:
856 * for (num = 0; num <= 0xffffffff; num++) {
857 * seed = num;
858 * ...
860 * seed = 0;
861 * for (num = 0; num <= 0xffffffff; num++) {
862 * ...
864 seed = 0;
865 for (num = 0; num <= 100000; num++) {
866 seed_bak = seed;
867 seed_expected = seed;
868 result_expected = my_RtlRandom(&seed_expected);
869 /* The following corrections are necessary because the */
870 /* previous tests changed the saved_value array */
871 if (num == 0) {
872 result_expected = 0x7fffffc3;
873 } else if (num == 81) {
874 result_expected = 0x7fffffb1;
875 } /* if */
876 result = pRtlRandom(&seed);
877 ok(result == result_expected,
878 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
879 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, result_expected);
880 ok(seed == seed_expected,
881 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
882 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, seed_expected);
883 } /* for */
887 typedef struct {
888 ACCESS_MASK GrantedAccess;
889 ACCESS_MASK DesiredAccess;
890 BOOLEAN result;
891 } all_accesses_t;
893 static const all_accesses_t all_accesses[] = {
894 {0xFEDCBA76, 0xFEDCBA76, 1},
895 {0x00000000, 0xFEDCBA76, 0},
896 {0xFEDCBA76, 0x00000000, 1},
897 {0x00000000, 0x00000000, 1},
898 {0xFEDCBA76, 0xFEDCBA70, 1},
899 {0xFEDCBA70, 0xFEDCBA76, 0},
900 {0xFEDCBA76, 0xFEDC8A76, 1},
901 {0xFEDC8A76, 0xFEDCBA76, 0},
902 {0xFEDCBA76, 0xC8C4B242, 1},
903 {0xC8C4B242, 0xFEDCBA76, 0},
905 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
908 static void test_RtlAreAllAccessesGranted(void)
910 unsigned int test_num;
911 BOOLEAN result;
913 if (!pRtlAreAllAccessesGranted)
915 win_skip("RtlAreAllAccessesGranted is not available\n");
916 return;
919 for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
920 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
921 all_accesses[test_num].DesiredAccess);
922 ok(all_accesses[test_num].result == result,
923 "(test %d): RtlAreAllAccessesGranted(%08x, %08x) returns %d, expected %d\n",
924 test_num, all_accesses[test_num].GrantedAccess,
925 all_accesses[test_num].DesiredAccess,
926 result, all_accesses[test_num].result);
927 } /* for */
931 typedef struct {
932 ACCESS_MASK GrantedAccess;
933 ACCESS_MASK DesiredAccess;
934 BOOLEAN result;
935 } any_accesses_t;
937 static const any_accesses_t any_accesses[] = {
938 {0xFEDCBA76, 0xFEDCBA76, 1},
939 {0x00000000, 0xFEDCBA76, 0},
940 {0xFEDCBA76, 0x00000000, 0},
941 {0x00000000, 0x00000000, 0},
942 {0xFEDCBA76, 0x01234589, 0},
943 {0x00040000, 0xFEDCBA76, 1},
944 {0x00040000, 0xFED8BA76, 0},
945 {0xFEDCBA76, 0x00040000, 1},
946 {0xFED8BA76, 0x00040000, 0},
948 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
951 static void test_RtlAreAnyAccessesGranted(void)
953 unsigned int test_num;
954 BOOLEAN result;
956 if (!pRtlAreAnyAccessesGranted)
958 win_skip("RtlAreAnyAccessesGranted is not available\n");
959 return;
962 for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
963 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
964 any_accesses[test_num].DesiredAccess);
965 ok(any_accesses[test_num].result == result,
966 "(test %d): RtlAreAnyAccessesGranted(%08x, %08x) returns %d, expected %d\n",
967 test_num, any_accesses[test_num].GrantedAccess,
968 any_accesses[test_num].DesiredAccess,
969 result, any_accesses[test_num].result);
970 } /* for */
973 static void test_RtlComputeCrc32(void)
975 DWORD crc = 0;
977 if (!pRtlComputeCrc32)
979 win_skip("RtlComputeCrc32 is not available\n");
980 return;
983 crc = pRtlComputeCrc32(crc, (const BYTE *)src, LEN);
984 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8x\n", crc);
988 typedef struct MY_HANDLE
990 RTL_HANDLE RtlHandle;
991 void * MyValue;
992 } MY_HANDLE;
994 static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
996 ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
997 *AllocatedBit = *AllocatedBit | 1;
1000 static void test_HandleTables(void)
1002 BOOLEAN result;
1003 NTSTATUS status;
1004 ULONG Index;
1005 MY_HANDLE * MyHandle;
1006 RTL_HANDLE_TABLE HandleTable;
1008 if (!pRtlInitializeHandleTable)
1010 win_skip("RtlInitializeHandleTable is not available\n");
1011 return;
1014 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
1015 MyHandle = (MY_HANDLE *)pRtlAllocateHandle(&HandleTable, &Index);
1016 ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
1017 RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
1018 MyHandle = NULL;
1019 result = pRtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
1020 ok(result, "Handle %p wasn't valid\n", MyHandle);
1021 result = pRtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
1022 ok(result, "Couldn't free handle %p\n", MyHandle);
1023 status = pRtlDestroyHandleTable(&HandleTable);
1024 ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08x\n", status);
1027 static void test_RtlAllocateAndInitializeSid(void)
1029 NTSTATUS ret;
1030 SID_IDENTIFIER_AUTHORITY sia = {{ 1, 2, 3, 4, 5, 6 }};
1031 PSID psid;
1033 if (!pRtlAllocateAndInitializeSid)
1035 win_skip("RtlAllocateAndInitializeSid is not available\n");
1036 return;
1039 ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
1040 ok(!ret, "RtlAllocateAndInitializeSid error %08x\n", ret);
1041 ret = pRtlFreeSid(psid);
1042 ok(!ret, "RtlFreeSid error %08x\n", ret);
1044 /* these tests crash on XP */
1045 if (0)
1047 pRtlAllocateAndInitializeSid(NULL, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
1048 pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL);
1051 ret = pRtlAllocateAndInitializeSid(&sia, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
1052 ok(ret == STATUS_INVALID_SID, "wrong error %08x\n", ret);
1055 static void test_RtlDeleteTimer(void)
1057 NTSTATUS ret;
1059 if (!pRtlDeleteTimer)
1061 win_skip("RtlDeleteTimer is not available\n");
1062 return;
1065 ret = pRtlDeleteTimer(NULL, NULL, NULL);
1066 ok(ret == STATUS_INVALID_PARAMETER_1 ||
1067 ret == STATUS_INVALID_PARAMETER, /* W2K */
1068 "expected STATUS_INVALID_PARAMETER_1 or STATUS_INVALID_PARAMETER, got %x\n", ret);
1071 static void test_RtlThreadErrorMode(void)
1073 DWORD oldmode;
1074 BOOL is_wow64;
1075 DWORD mode;
1076 NTSTATUS status;
1078 if (!pRtlGetThreadErrorMode || !pRtlSetThreadErrorMode)
1080 win_skip("RtlGetThreadErrorMode and/or RtlSetThreadErrorMode not available\n");
1081 return;
1084 if (!pIsWow64Process || !pIsWow64Process(GetCurrentProcess(), &is_wow64))
1085 is_wow64 = FALSE;
1087 oldmode = pRtlGetThreadErrorMode();
1089 status = pRtlSetThreadErrorMode(0x70, &mode);
1090 ok(status == STATUS_SUCCESS ||
1091 status == STATUS_WAIT_1, /* Vista */
1092 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
1093 ok(mode == oldmode,
1094 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
1095 mode, oldmode);
1096 ok(pRtlGetThreadErrorMode() == 0x70,
1097 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0x70);
1098 if (!is_wow64 && pNtCurrentTeb)
1099 ok(pNtCurrentTeb()->HardErrorDisabled == 0x70,
1100 "The TEB contains 0x%x, expected 0x%x\n",
1101 pNtCurrentTeb()->HardErrorDisabled, 0x70);
1103 status = pRtlSetThreadErrorMode(0, &mode);
1104 ok(status == STATUS_SUCCESS ||
1105 status == STATUS_WAIT_1, /* Vista */
1106 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
1107 ok(mode == 0x70,
1108 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
1109 mode, 0x70);
1110 ok(pRtlGetThreadErrorMode() == 0,
1111 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0);
1112 if (!is_wow64 && pNtCurrentTeb)
1113 ok(pNtCurrentTeb()->HardErrorDisabled == 0,
1114 "The TEB contains 0x%x, expected 0x%x\n",
1115 pNtCurrentTeb()->HardErrorDisabled, 0);
1117 for (mode = 1; mode; mode <<= 1)
1119 status = pRtlSetThreadErrorMode(mode, NULL);
1120 if (mode & 0x70)
1121 ok(status == STATUS_SUCCESS ||
1122 status == STATUS_WAIT_1, /* Vista */
1123 "RtlSetThreadErrorMode(%x,NULL) failed with error 0x%08x\n",
1124 mode, status);
1125 else
1126 ok(status == STATUS_INVALID_PARAMETER_1,
1127 "RtlSetThreadErrorMode(%x,NULL) returns 0x%08x, "
1128 "expected STATUS_INVALID_PARAMETER_1\n",
1129 mode, status);
1132 pRtlSetThreadErrorMode(oldmode, NULL);
1135 static void test_LdrProcessRelocationBlock(void)
1137 IMAGE_BASE_RELOCATION *ret;
1138 USHORT reloc;
1139 DWORD addr32;
1140 SHORT addr16;
1142 if(!pLdrProcessRelocationBlock) {
1143 win_skip("LdrProcessRelocationBlock not available\n");
1144 return;
1147 addr32 = 0x50005;
1148 reloc = IMAGE_REL_BASED_HIGHLOW<<12;
1149 ret = pLdrProcessRelocationBlock(&addr32, 1, &reloc, 0x500050);
1150 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
1151 ok(addr32 == 0x550055, "addr32 = %x, expected 0x550055\n", addr32);
1153 addr16 = 0x505;
1154 reloc = IMAGE_REL_BASED_HIGH<<12;
1155 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
1156 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
1157 ok(addr16 == 0x555, "addr16 = %x, expected 0x555\n", addr16);
1159 addr16 = 0x505;
1160 reloc = IMAGE_REL_BASED_LOW<<12;
1161 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
1162 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
1163 ok(addr16 == 0x565, "addr16 = %x, expected 0x565\n", addr16);
1166 static void test_RtlIpv4AddressToString(void)
1168 CHAR buffer[20];
1169 CHAR *res;
1170 IN_ADDR ip;
1171 DWORD_PTR len;
1173 if (!pRtlIpv4AddressToStringA)
1175 win_skip("RtlIpv4AddressToStringA not available\n");
1176 return;
1179 ip.S_un.S_un_b.s_b1 = 1;
1180 ip.S_un.S_un_b.s_b2 = 2;
1181 ip.S_un.S_un_b.s_b3 = 3;
1182 ip.S_un.S_un_b.s_b4 = 4;
1184 memset(buffer, '#', sizeof(buffer) - 1);
1185 buffer[sizeof(buffer) -1] = 0;
1186 res = pRtlIpv4AddressToStringA(&ip, buffer);
1187 len = strlen(buffer);
1188 ok(res == (buffer + len), "got %p with '%s' (expected %p)\n", res, buffer, buffer + len);
1190 res = pRtlIpv4AddressToStringA(&ip, NULL);
1191 ok( (res == (char *)~0) ||
1192 broken(res == (char *)len), /* XP and w2003 */
1193 "got %p (expected ~0)\n", res);
1195 if (0) {
1196 /* this crashes in windows */
1197 memset(buffer, '#', sizeof(buffer) - 1);
1198 buffer[sizeof(buffer) -1] = 0;
1199 res = pRtlIpv4AddressToStringA(NULL, buffer);
1200 trace("got %p with '%s'\n", res, buffer);
1203 if (0) {
1204 /* this crashes in windows */
1205 res = pRtlIpv4AddressToStringA(NULL, NULL);
1206 trace("got %p\n", res);
1210 static void test_RtlIpv4AddressToStringEx(void)
1212 CHAR ip_1234[] = "1.2.3.4";
1213 CHAR ip_1234_80[] = "1.2.3.4:80";
1214 LPSTR expect;
1215 CHAR buffer[30];
1216 NTSTATUS res;
1217 IN_ADDR ip;
1218 ULONG size;
1219 DWORD used;
1220 USHORT port;
1222 if (!pRtlIpv4AddressToStringExA)
1224 win_skip("RtlIpv4AddressToStringExA not available\n");
1225 return;
1228 ip.S_un.S_un_b.s_b1 = 1;
1229 ip.S_un.S_un_b.s_b2 = 2;
1230 ip.S_un.S_un_b.s_b3 = 3;
1231 ip.S_un.S_un_b.s_b4 = 4;
1233 port = htons(80);
1234 expect = ip_1234_80;
1236 size = sizeof(buffer);
1237 memset(buffer, '#', sizeof(buffer) - 1);
1238 buffer[sizeof(buffer) -1] = 0;
1239 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1240 used = strlen(buffer);
1241 ok( (res == STATUS_SUCCESS) &&
1242 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1243 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1245 size = used + 1;
1246 memset(buffer, '#', sizeof(buffer) - 1);
1247 buffer[sizeof(buffer) -1] = 0;
1248 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1249 ok( (res == STATUS_SUCCESS) &&
1250 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1251 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1253 size = used;
1254 memset(buffer, '#', sizeof(buffer) - 1);
1255 buffer[sizeof(buffer) -1] = 0;
1256 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1257 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1258 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1259 res, size, buffer, used + 1);
1261 size = used - 1;
1262 memset(buffer, '#', sizeof(buffer) - 1);
1263 buffer[sizeof(buffer) -1] = 0;
1264 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1265 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1266 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1267 res, size, buffer, used + 1);
1270 /* to get only the ip, use 0 as port */
1271 port = 0;
1272 expect = ip_1234;
1274 size = sizeof(buffer);
1275 memset(buffer, '#', sizeof(buffer) - 1);
1276 buffer[sizeof(buffer) -1] = 0;
1277 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1278 used = strlen(buffer);
1279 ok( (res == STATUS_SUCCESS) &&
1280 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1281 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1283 size = used + 1;
1284 memset(buffer, '#', sizeof(buffer) - 1);
1285 buffer[sizeof(buffer) -1] = 0;
1286 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1287 ok( (res == STATUS_SUCCESS) &&
1288 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1289 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1291 size = used;
1292 memset(buffer, '#', sizeof(buffer) - 1);
1293 buffer[sizeof(buffer) -1] = 0;
1294 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1295 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1296 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1297 res, size, buffer, used + 1);
1299 size = used - 1;
1300 memset(buffer, '#', sizeof(buffer) - 1);
1301 buffer[sizeof(buffer) -1] = 0;
1302 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1303 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1304 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1305 res, size, buffer, used + 1);
1308 /* parameters are checked */
1309 memset(buffer, '#', sizeof(buffer) - 1);
1310 buffer[sizeof(buffer) -1] = 0;
1311 res = pRtlIpv4AddressToStringExA(&ip, 0, buffer, NULL);
1312 ok(res == STATUS_INVALID_PARAMETER,
1313 "got 0x%x with '%s' (expected STATUS_INVALID_PARAMETER)\n", res, buffer);
1315 size = sizeof(buffer);
1316 res = pRtlIpv4AddressToStringExA(&ip, 0, NULL, &size);
1317 ok( res == STATUS_INVALID_PARAMETER,
1318 "got 0x%x and size %d (expected STATUS_INVALID_PARAMETER)\n", res, size);
1320 size = sizeof(buffer);
1321 memset(buffer, '#', sizeof(buffer) - 1);
1322 buffer[sizeof(buffer) -1] = 0;
1323 res = pRtlIpv4AddressToStringExA(NULL, 0, buffer, &size);
1324 ok( res == STATUS_INVALID_PARAMETER,
1325 "got 0x%x and size %d with '%s' (expected STATUS_INVALID_PARAMETER)\n",
1326 res, size, buffer);
1329 static void test_RtlIpv4StringToAddress(void)
1331 NTSTATUS res;
1332 IN_ADDR ip, expected_ip;
1333 PCSTR terminator;
1334 CHAR dummy;
1335 struct
1337 PCSTR address;
1338 NTSTATUS res;
1339 int terminator_offset;
1340 int ip[4];
1341 BOOL strict_is_different;
1342 NTSTATUS res_strict;
1343 int terminator_offset_strict;
1344 int ip_strict[4];
1345 } tests[] =
1347 { "", STATUS_INVALID_PARAMETER, 0, { -1 } },
1348 { " ", STATUS_INVALID_PARAMETER, 0, { -1 } },
1349 { "1.1.1.1", STATUS_SUCCESS, 7, { 1, 1, 1, 1 } },
1350 { "0.0.0.0", STATUS_SUCCESS, 7, { 0, 0, 0, 0 } },
1351 { "255.255.255.255", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1352 { "255.255.255.255:123",
1353 STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1354 { "255.255.255.256", STATUS_INVALID_PARAMETER, 15, { -1 } },
1355 { "255.255.255.4294967295",
1356 STATUS_INVALID_PARAMETER, 22, { -1 } },
1357 { "255.255.255.4294967296",
1358 STATUS_INVALID_PARAMETER, 21, { -1 } },
1359 { "255.255.255.4294967297",
1360 STATUS_INVALID_PARAMETER, 21, { -1 } },
1361 { "a", STATUS_INVALID_PARAMETER, 0, { -1 } },
1362 { "1.1.1.0xaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1363 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1364 { "1.1.1.0XaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1365 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1366 { "1.1.1.0x", STATUS_INVALID_PARAMETER, 8, { -1 } },
1367 { "1.1.1.0xff", STATUS_SUCCESS, 10, { 1, 1, 1, 255 },
1368 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1369 { "1.1.1.0x100", STATUS_INVALID_PARAMETER, 11, { -1 },
1370 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1371 { "1.1.1.0xffffffff",STATUS_INVALID_PARAMETER, 16, { -1 },
1372 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1373 { "1.1.1.0x100000000",
1374 STATUS_INVALID_PARAMETER, 16, { -1, 0, 0, 0 },
1375 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1376 { "1.1.1.010", STATUS_SUCCESS, 9, { 1, 1, 1, 8 },
1377 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1378 { "1.1.1.00", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1379 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1380 { "1.1.1.007", STATUS_SUCCESS, 9, { 1, 1, 1, 7 },
1381 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1382 { "1.1.1.08", STATUS_INVALID_PARAMETER, 7, { -1 } },
1383 { "1.1.1.008", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1384 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1385 { "1.1.1.0a", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1386 { "1.1.1.0o10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1387 { "1.1.1.0b10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1388 { "1.1.1.-2", STATUS_INVALID_PARAMETER, 6, { -1 } },
1389 { "1", STATUS_SUCCESS, 1, { 0, 0, 0, 1 },
1390 TRUE, STATUS_INVALID_PARAMETER, 1, { -1 } },
1391 { "-1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1392 { "203569230", STATUS_SUCCESS, 9, { 12, 34, 56, 78 },
1393 TRUE, STATUS_INVALID_PARAMETER, 9, { -1 } },
1394 { "1.223756", STATUS_SUCCESS, 8, { 1, 3, 106, 12 },
1395 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1396 { "3.4.756", STATUS_SUCCESS, 7, { 3, 4, 2, 244 },
1397 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1398 { "3.4.756.1", STATUS_INVALID_PARAMETER, 9, { -1 } },
1399 { "3.4.65536", STATUS_INVALID_PARAMETER, 9, { -1 } },
1400 { "3.4.5.6.7", STATUS_INVALID_PARAMETER, 7, { -1 } },
1401 { "3.4.5.+6", STATUS_INVALID_PARAMETER, 6, { -1 } },
1402 { " 3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1403 { "\t3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1404 { "3.4.5.6 ", STATUS_SUCCESS, 7, { 3, 4, 5, 6 } },
1405 { "3. 4.5.6", STATUS_INVALID_PARAMETER, 2, { -1 } },
1406 { ".", STATUS_INVALID_PARAMETER, 1, { -1 } },
1407 { "..", STATUS_INVALID_PARAMETER, 1, { -1 } },
1408 { "1.", STATUS_INVALID_PARAMETER, 2, { -1 } },
1409 { "1..", STATUS_INVALID_PARAMETER, 3, { -1 } },
1410 { ".1", STATUS_INVALID_PARAMETER, 1, { -1 } },
1411 { ".1.", STATUS_INVALID_PARAMETER, 1, { -1 } },
1412 { ".1.2.3", STATUS_INVALID_PARAMETER, 1, { -1 } },
1413 { "0.1.2.3", STATUS_SUCCESS, 7, { 0, 1, 2, 3 } },
1414 { "0.1.2.3.", STATUS_INVALID_PARAMETER, 7, { -1 } },
1415 { "[0.1.2.3]", STATUS_INVALID_PARAMETER, 0, { -1 } },
1416 { "::1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1417 { ":1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1419 const int testcount = sizeof(tests) / sizeof(tests[0]);
1420 int i;
1422 if (!pRtlIpv4StringToAddressA)
1424 skip("RtlIpv4StringToAddress not available\n");
1425 return;
1428 if (0)
1430 /* leaving either parameter NULL crashes on Windows */
1431 res = pRtlIpv4StringToAddressA(NULL, FALSE, &terminator, &ip);
1432 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, NULL, &ip);
1433 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, &terminator, NULL);
1434 /* same for the wide char version */
1436 res = pRtlIpv4StringToAddressW(NULL, FALSE, &terminatorW, &ip);
1437 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, NULL, &ip);
1438 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, &terminatorW, NULL);
1442 for (i = 0; i < testcount; i++)
1444 /* non-strict */
1445 terminator = &dummy;
1446 ip.S_un.S_addr = 0xabababab;
1447 res = pRtlIpv4StringToAddressA(tests[i].address, FALSE, &terminator, &ip);
1448 ok(res == tests[i].res,
1449 "[%s] res = 0x%08x, expected 0x%08x\n",
1450 tests[i].address, res, tests[i].res);
1451 ok(terminator == tests[i].address + tests[i].terminator_offset,
1452 "[%s] terminator = %p, expected %p\n",
1453 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset);
1454 if (tests[i].ip[0] == -1)
1455 expected_ip.S_un.S_addr = 0xabababab;
1456 else
1458 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip[0];
1459 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip[1];
1460 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip[2];
1461 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip[3];
1463 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1464 "[%s] ip = %08x, expected %08x\n",
1465 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1467 if (!tests[i].strict_is_different)
1469 tests[i].res_strict = tests[i].res;
1470 tests[i].terminator_offset_strict = tests[i].terminator_offset;
1471 tests[i].ip_strict[0] = tests[i].ip[0];
1472 tests[i].ip_strict[1] = tests[i].ip[1];
1473 tests[i].ip_strict[2] = tests[i].ip[2];
1474 tests[i].ip_strict[3] = tests[i].ip[3];
1476 /* strict */
1477 terminator = &dummy;
1478 ip.S_un.S_addr = 0xabababab;
1479 res = pRtlIpv4StringToAddressA(tests[i].address, TRUE, &terminator, &ip);
1480 ok(res == tests[i].res_strict,
1481 "[%s] res = 0x%08x, expected 0x%08x\n",
1482 tests[i].address, res, tests[i].res_strict);
1483 ok(terminator == tests[i].address + tests[i].terminator_offset_strict,
1484 "[%s] terminator = %p, expected %p\n",
1485 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset_strict);
1486 if (tests[i].ip_strict[0] == -1)
1487 expected_ip.S_un.S_addr = 0xabababab;
1488 else
1490 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip_strict[0];
1491 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip_strict[1];
1492 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip_strict[2];
1493 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip_strict[3];
1495 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1496 "[%s] ip = %08x, expected %08x\n",
1497 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1501 static void test_LdrAddRefDll(void)
1503 HMODULE mod, mod2;
1504 NTSTATUS status;
1505 BOOL ret;
1507 if (!pLdrAddRefDll)
1509 win_skip( "LdrAddRefDll not supported\n" );
1510 return;
1513 mod = LoadLibraryA("comctl32.dll");
1514 ok(mod != NULL, "got %p\n", mod);
1515 ret = FreeLibrary(mod);
1516 ok(ret, "got %d\n", ret);
1518 mod2 = GetModuleHandleA("comctl32.dll");
1519 ok(mod2 == NULL, "got %p\n", mod2);
1521 /* load, addref and release 2 times */
1522 mod = LoadLibraryA("comctl32.dll");
1523 ok(mod != NULL, "got %p\n", mod);
1524 status = pLdrAddRefDll(0, mod);
1525 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1526 ret = FreeLibrary(mod);
1527 ok(ret, "got %d\n", ret);
1529 mod2 = GetModuleHandleA("comctl32.dll");
1530 ok(mod2 != NULL, "got %p\n", mod2);
1531 ret = FreeLibrary(mod);
1532 ok(ret, "got %d\n", ret);
1534 mod2 = GetModuleHandleA("comctl32.dll");
1535 ok(mod2 == NULL, "got %p\n", mod2);
1537 /* pin refcount */
1538 mod = LoadLibraryA("comctl32.dll");
1539 ok(mod != NULL, "got %p\n", mod);
1540 status = pLdrAddRefDll(LDR_ADDREF_DLL_PIN, mod);
1541 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1543 ret = FreeLibrary(mod);
1544 ok(ret, "got %d\n", ret);
1545 ret = FreeLibrary(mod);
1546 ok(ret, "got %d\n", ret);
1547 ret = FreeLibrary(mod);
1548 ok(ret, "got %d\n", ret);
1549 ret = FreeLibrary(mod);
1550 ok(ret, "got %d\n", ret);
1552 mod2 = GetModuleHandleA("comctl32.dll");
1553 ok(mod2 != NULL, "got %p\n", mod2);
1556 static void test_LdrLockLoaderLock(void)
1558 ULONG_PTR magic;
1559 ULONG result;
1560 NTSTATUS status;
1562 if (!pLdrLockLoaderLock)
1564 win_skip("LdrLockLoaderLock() is not available\n");
1565 return;
1568 /* invalid flags */
1569 result = 10;
1570 magic = 0xdeadbeef;
1571 status = pLdrLockLoaderLock(0x10, &result, &magic);
1572 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
1573 ok(result == 0, "got %d\n", result);
1574 ok(magic == 0, "got %lx\n", magic);
1576 magic = 0xdeadbeef;
1577 status = pLdrLockLoaderLock(0x10, NULL, &magic);
1578 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
1579 ok(magic == 0, "got %lx\n", magic);
1581 result = 10;
1582 status = pLdrLockLoaderLock(0x10, &result, NULL);
1583 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
1584 ok(result == 0, "got %d\n", result);
1586 /* non-blocking mode, result is null */
1587 magic = 0xdeadbeef;
1588 status = pLdrLockLoaderLock(0x2, NULL, &magic);
1589 ok(status == STATUS_INVALID_PARAMETER_2, "got 0x%08x\n", status);
1590 ok(magic == 0, "got %lx\n", magic);
1592 /* magic pointer is null */
1593 result = 10;
1594 status = pLdrLockLoaderLock(0, &result, NULL);
1595 ok(status == STATUS_INVALID_PARAMETER_3, "got 0x%08x\n", status);
1596 ok(result == 0, "got %d\n", result);
1598 /* lock in non-blocking mode */
1599 result = 0;
1600 magic = 0;
1601 status = pLdrLockLoaderLock(0x2, &result, &magic);
1602 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1603 ok(result == 1, "got %d\n", result);
1604 ok(magic != 0, "got %lx\n", magic);
1605 pLdrUnlockLoaderLock(0, magic);
1608 static void test_RtlCompressBuffer(void)
1610 ULONG compress_workspace, decompress_workspace;
1611 static const UCHAR test_buffer[] = "WineWineWine";
1612 static UCHAR buf1[0x1000], buf2[0x1000];
1613 ULONG final_size, buf_size;
1614 UCHAR *workspace = NULL;
1615 NTSTATUS status;
1617 if (!pRtlCompressBuffer || !pRtlDecompressBuffer || !pRtlGetCompressionWorkSpaceSize)
1619 win_skip("skipping RtlCompressBuffer tests, required functions not available\n");
1620 return;
1623 compress_workspace = decompress_workspace = 0xdeadbeef;
1624 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
1625 &decompress_workspace);
1626 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1627 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
1628 workspace = HeapAlloc(GetProcessHeap(), 0, compress_workspace);
1629 ok(workspace != NULL, "HeapAlloc failed %d\n", GetLastError());
1631 /* test compression format / engine */
1632 final_size = 0xdeadbeef;
1633 status = pRtlCompressBuffer(COMPRESSION_FORMAT_NONE, test_buffer, sizeof(test_buffer),
1634 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
1635 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1636 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1638 final_size = 0xdeadbeef;
1639 status = pRtlCompressBuffer(COMPRESSION_FORMAT_DEFAULT, test_buffer, sizeof(test_buffer),
1640 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
1641 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1642 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1644 final_size = 0xdeadbeef;
1645 status = pRtlCompressBuffer(0xFF, test_buffer, sizeof(test_buffer),
1646 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
1647 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
1648 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
1650 /* test compression */
1651 final_size = 0xdeadbeef;
1652 memset(buf1, 0x11, sizeof(buf1));
1653 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
1654 buf1, sizeof(buf1), 4096, &final_size, workspace);
1655 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1656 ok((*(WORD *)buf1 & 0x7000) == 0x3000, "no chunk signature found %04x\n", *(WORD *)buf1);
1657 todo_wine
1658 ok(final_size < sizeof(test_buffer), "got wrong final_size %u\n", final_size);
1660 /* test decompression */
1661 buf_size = final_size;
1662 final_size = 0xdeadbeef;
1663 memset(buf2, 0x11, sizeof(buf2));
1664 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf2, sizeof(buf2),
1665 buf1, buf_size, &final_size);
1666 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1667 ok(final_size == sizeof(test_buffer), "got wrong final_size %u\n", final_size);
1668 ok(!memcmp(buf2, test_buffer, sizeof(test_buffer)), "got wrong decoded data\n");
1669 ok(buf2[sizeof(test_buffer)] == 0x11, "too many bytes written\n");
1671 /* buffer too small */
1672 final_size = 0xdeadbeef;
1673 memset(buf1, 0x11, sizeof(buf1));
1674 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
1675 buf1, 4, 4096, &final_size, workspace);
1676 ok(status == STATUS_BUFFER_TOO_SMALL, "got wrong status 0x%08x\n", status);
1678 HeapFree(GetProcessHeap(), 0, workspace);
1681 static void test_RtlGetCompressionWorkSpaceSize(void)
1683 ULONG compress_workspace, decompress_workspace;
1684 NTSTATUS status;
1686 if (!pRtlGetCompressionWorkSpaceSize)
1688 win_skip("RtlGetCompressionWorkSpaceSize is not available\n");
1689 return;
1692 /* test invalid format / engine */
1693 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_NONE, &compress_workspace,
1694 &decompress_workspace);
1695 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1697 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_DEFAULT, &compress_workspace,
1698 &decompress_workspace);
1699 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
1701 status = pRtlGetCompressionWorkSpaceSize(0xFF, &compress_workspace, &decompress_workspace);
1702 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
1704 /* test LZNT1 with normal and maximum compression */
1705 compress_workspace = decompress_workspace = 0xdeadbeef;
1706 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
1707 &decompress_workspace);
1708 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1709 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
1710 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
1712 compress_workspace = decompress_workspace = 0xdeadbeef;
1713 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM,
1714 &compress_workspace, &decompress_workspace);
1715 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
1716 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
1717 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
1720 START_TEST(rtl)
1722 InitFunctionPtrs();
1724 test_RtlCompareMemory();
1725 test_RtlCompareMemoryUlong();
1726 test_RtlMoveMemory();
1727 test_RtlFillMemory();
1728 test_RtlFillMemoryUlong();
1729 test_RtlZeroMemory();
1730 test_RtlUlonglongByteSwap();
1731 test_RtlUniform();
1732 test_RtlRandom();
1733 test_RtlAreAllAccessesGranted();
1734 test_RtlAreAnyAccessesGranted();
1735 test_RtlComputeCrc32();
1736 test_HandleTables();
1737 test_RtlAllocateAndInitializeSid();
1738 test_RtlDeleteTimer();
1739 test_RtlThreadErrorMode();
1740 test_LdrProcessRelocationBlock();
1741 test_RtlIpv4AddressToString();
1742 test_RtlIpv4AddressToStringEx();
1743 test_RtlIpv4StringToAddress();
1744 test_LdrAddRefDll();
1745 test_LdrLockLoaderLock();
1746 test_RtlCompressBuffer();
1747 test_RtlGetCompressionWorkSpaceSize();