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
20 * We use function pointers here as there is no import library for NTDLL on
26 #include "ntdll_test.h"
29 #ifndef __WINE_WINTERNL_H
31 typedef struct _RTL_HANDLE
33 struct _RTL_HANDLE
* Next
;
36 typedef struct _RTL_HANDLE_TABLE
49 /* avoid #include <winsock2.h> */
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
*pRtlDecompressFragment
)(USHORT
, PUCHAR
, ULONG
, const UCHAR
*, ULONG
, ULONG
, PULONG
, PVOID
);
98 static NTSTATUS (WINAPI
*pRtlCompressBuffer
)(USHORT
, const UCHAR
*, ULONG
, PUCHAR
, ULONG
, ULONG
, PULONG
, PVOID
);
99 static BOOL (WINAPI
*pRtlIsCriticalSectionLocked
)(RTL_CRITICAL_SECTION
*);
100 static BOOL (WINAPI
*pRtlIsCriticalSectionLockedByThread
)(RTL_CRITICAL_SECTION
*);
102 static HMODULE hkernel32
= 0;
103 static BOOL (WINAPI
*pIsWow64Process
)(HANDLE
, PBOOL
);
107 static const char* src_src
= "This is a test!"; /* 16 bytes long, incl NUL */
108 static ULONG src_aligned_block
[4];
109 static ULONG dest_aligned_block
[32];
110 static const char *src
= (const char*)src_aligned_block
;
111 static char* dest
= (char*)dest_aligned_block
;
113 static void InitFunctionPtrs(void)
115 hntdll
= LoadLibraryA("ntdll.dll");
116 ok(hntdll
!= 0, "LoadLibrary failed\n");
118 pRtlCompareMemory
= (void *)GetProcAddress(hntdll
, "RtlCompareMemory");
119 pRtlCompareMemoryUlong
= (void *)GetProcAddress(hntdll
, "RtlCompareMemoryUlong");
120 pRtlDeleteTimer
= (void *)GetProcAddress(hntdll
, "RtlDeleteTimer");
121 pRtlMoveMemory
= (void *)GetProcAddress(hntdll
, "RtlMoveMemory");
122 pRtlFillMemory
= (void *)GetProcAddress(hntdll
, "RtlFillMemory");
123 pRtlFillMemoryUlong
= (void *)GetProcAddress(hntdll
, "RtlFillMemoryUlong");
124 pRtlZeroMemory
= (void *)GetProcAddress(hntdll
, "RtlZeroMemory");
125 pRtlUlonglongByteSwap
= (void *)GetProcAddress(hntdll
, "RtlUlonglongByteSwap");
126 pRtlUniform
= (void *)GetProcAddress(hntdll
, "RtlUniform");
127 pRtlRandom
= (void *)GetProcAddress(hntdll
, "RtlRandom");
128 pRtlAreAllAccessesGranted
= (void *)GetProcAddress(hntdll
, "RtlAreAllAccessesGranted");
129 pRtlAreAnyAccessesGranted
= (void *)GetProcAddress(hntdll
, "RtlAreAnyAccessesGranted");
130 pRtlComputeCrc32
= (void *)GetProcAddress(hntdll
, "RtlComputeCrc32");
131 pRtlInitializeHandleTable
= (void *)GetProcAddress(hntdll
, "RtlInitializeHandleTable");
132 pRtlIsValidIndexHandle
= (void *)GetProcAddress(hntdll
, "RtlIsValidIndexHandle");
133 pRtlDestroyHandleTable
= (void *)GetProcAddress(hntdll
, "RtlDestroyHandleTable");
134 pRtlAllocateHandle
= (void *)GetProcAddress(hntdll
, "RtlAllocateHandle");
135 pRtlFreeHandle
= (void *)GetProcAddress(hntdll
, "RtlFreeHandle");
136 pRtlAllocateAndInitializeSid
= (void *)GetProcAddress(hntdll
, "RtlAllocateAndInitializeSid");
137 pRtlFreeSid
= (void *)GetProcAddress(hntdll
, "RtlFreeSid");
138 pNtCurrentTeb
= (void *)GetProcAddress(hntdll
, "NtCurrentTeb");
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");
155 hkernel32
= LoadLibraryA("kernel32.dll");
156 ok(hkernel32
!= 0, "LoadLibrary failed\n");
158 pIsWow64Process
= (void *)GetProcAddress(hkernel32
, "IsWow64Process");
160 strcpy((char*)src_aligned_block
, src_src
);
161 ok(strlen(src
) == 15, "Source must be 16 bytes long!\n");
164 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
165 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
167 static void test_RtlCompareMemory(void)
171 if (!pRtlCompareMemory
)
173 win_skip("RtlCompareMemory is not available\n");
180 COMP(src
,src
,LEN
,LEN
);
182 COMP(src
,dest
,LEN
,0);
185 static void test_RtlCompareMemoryUlong(void)
190 if (!pRtlCompareMemoryUlong
)
192 win_skip("RtlCompareMemoryUlong is not available\n");
200 result
= pRtlCompareMemoryUlong(a
, 0, 0x0123);
201 ok(result
== 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %u, expected 0\n", a
, result
);
202 result
= pRtlCompareMemoryUlong(a
, 3, 0x0123);
203 ok(result
== 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a
, result
);
204 result
= pRtlCompareMemoryUlong(a
, 4, 0x0123);
205 ok(result
== 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a
, result
);
206 result
= pRtlCompareMemoryUlong(a
, 5, 0x0123);
207 ok(result
== 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a
, result
);
208 result
= pRtlCompareMemoryUlong(a
, 7, 0x0123);
209 ok(result
== 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a
, result
);
210 result
= pRtlCompareMemoryUlong(a
, 8, 0x0123);
211 ok(result
== 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 4\n", a
, result
);
212 result
= pRtlCompareMemoryUlong(a
, 9, 0x0123);
213 ok(result
== 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 4\n", a
, result
);
214 result
= pRtlCompareMemoryUlong(a
, 4, 0x0127);
215 ok(result
== 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %u, expected 0\n", a
, result
);
216 result
= pRtlCompareMemoryUlong(a
, 4, 0x7123);
217 ok(result
== 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %u, expected 0\n", a
, result
);
218 result
= pRtlCompareMemoryUlong(a
, 16, 0x4567);
219 ok(result
== 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %u, expected 0\n", a
, result
);
222 result
= pRtlCompareMemoryUlong(a
, 3, 0x0123);
223 ok(result
== 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a
, result
);
224 result
= pRtlCompareMemoryUlong(a
, 4, 0x0123);
225 ok(result
== 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a
, result
);
226 result
= pRtlCompareMemoryUlong(a
, 5, 0x0123);
227 ok(result
== 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a
, result
);
228 result
= pRtlCompareMemoryUlong(a
, 7, 0x0123);
229 ok(result
== 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a
, result
);
230 result
= pRtlCompareMemoryUlong(a
, 8, 0x0123);
231 ok(result
== 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 8\n", a
, result
);
232 result
= pRtlCompareMemoryUlong(a
, 9, 0x0123);
233 ok(result
== 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 8\n", a
, result
);
236 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
237 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
239 static void test_RtlMoveMemory(void)
243 win_skip("RtlMoveMemory is not available\n");
247 /* Length should be in bytes and not rounded. Use strcmp to ensure we
248 * didn't write past the end (it checks for the final NUL left by memset)
254 COPY(4); CMP("This");
255 COPY(5); CMP("This ");
256 COPY(6); CMP("This i");
257 COPY(7); CMP("This is");
258 COPY(8); CMP("This is ");
259 COPY(9); CMP("This is a");
262 strcpy(dest
, src
); pRtlMoveMemory(dest
, dest
+ 1, strlen(src
) - 1);
263 CMP("his is a test!!");
264 strcpy(dest
, src
); pRtlMoveMemory(dest
+ 1, dest
, strlen(src
));
265 CMP("TThis is a test!");
268 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
270 static void test_RtlFillMemory(void)
274 win_skip("RtlFillMemory is not available\n");
278 /* Length should be in bytes and not rounded. Use strcmp to ensure we
279 * didn't write past the end (the remainder of the string should match)
281 FILL(0); CMP("This is a test!");
282 FILL(1); CMP("xhis is a test!");
283 FILL(2); CMP("xxis is a test!");
284 FILL(3); CMP("xxxs is a test!");
285 FILL(4); CMP("xxxx is a test!");
286 FILL(5); CMP("xxxxxis a test!");
287 FILL(6); CMP("xxxxxxs a test!");
288 FILL(7); CMP("xxxxxxx a test!");
289 FILL(8); CMP("xxxxxxxxa test!");
290 FILL(9); CMP("xxxxxxxxx test!");
293 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
295 static void test_RtlFillMemoryUlong(void)
297 ULONG val
= ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
298 if (!pRtlFillMemoryUlong
)
300 win_skip("RtlFillMemoryUlong is not available\n");
304 /* Length should be in bytes and not rounded. Use strcmp to ensure we
305 * didn't write past the end (the remainder of the string should match)
307 LFILL(0); CMP("This is a test!");
308 LFILL(1); CMP("This is a test!");
309 LFILL(2); CMP("This is a test!");
310 LFILL(3); CMP("This is a test!");
311 LFILL(4); CMP("xxxx is a test!");
312 LFILL(5); CMP("xxxx is a test!");
313 LFILL(6); CMP("xxxx is a test!");
314 LFILL(7); CMP("xxxx is a test!");
315 LFILL(8); CMP("xxxxxxxxa test!");
316 LFILL(9); CMP("xxxxxxxxa test!");
319 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
320 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
322 static void test_RtlZeroMemory(void)
326 win_skip("RtlZeroMemory is not available\n");
330 /* Length should be in bytes and not rounded. */
331 ZERO(0); MCMP("This is a test!");
332 ZERO(1); MCMP("\0his is a test!");
333 ZERO(2); MCMP("\0\0is is a test!");
334 ZERO(3); MCMP("\0\0\0s is a test!");
335 ZERO(4); MCMP("\0\0\0\0 is a test!");
336 ZERO(5); MCMP("\0\0\0\0\0is a test!");
337 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
338 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
339 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
340 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
343 static void test_RtlUlonglongByteSwap(void)
347 if ( !pRtlUlonglongByteSwap
)
349 win_skip("RtlUlonglongByteSwap is not available\n");
353 if ( pRtlUlonglongByteSwap( 0 ) != 0 )
355 win_skip("Broken RtlUlonglongByteSwap in win2k\n");
359 result
= pRtlUlonglongByteSwap( ((ULONGLONG
)0x76543210 << 32) | 0x87654321 );
360 ok( (((ULONGLONG
)0x21436587 << 32) | 0x10325476) == result
,
361 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%x%08x, expected 0x2143658710325476\n",
362 (DWORD
)(result
>> 32), (DWORD
)result
);
366 static void test_RtlUniform(void)
376 win_skip("RtlUniform is not available\n");
381 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
382 * algorithm. This algorithm is:
384 * seed = (seed * const_1 + const_2) % const_3;
386 * According to the documentation the random number is distributed over
387 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
389 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
391 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
392 * algorithm can be expressed without division as:
394 * seed = (seed * const_1 + const_2) & MAXLONG;
396 * To find out const_2 we just call RtlUniform with seed set to 0:
399 expected
= 0x7fffffc3;
400 result
= pRtlUniform(&seed
);
401 ok(result
== expected
,
402 "RtlUniform(&seed (seed == 0)) returns %x, expected %x\n",
405 * The algorithm is now:
407 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
409 * To find out const_1 we can use:
411 * const_1 = RtlUniform(1) - 0x7fffffc3;
413 * If that does not work a search loop can try all possible values of
414 * const_1 and compare to the result to RtlUniform(1).
415 * This way we find out that const_1 is 0xffffffed.
417 * For seed = 1 the const_2 is 0x7fffffc4:
420 expected
= seed
* 0xffffffed + 0x7fffffc3 + 1;
421 result
= pRtlUniform(&seed
);
422 ok(result
== expected
,
423 "RtlUniform(&seed (seed == 1)) returns %x, expected %x\n",
426 * For seed = 2 the const_2 is 0x7fffffc3:
429 expected
= seed
* 0xffffffed + 0x7fffffc3;
430 result
= pRtlUniform(&seed
);
433 * Windows Vista uses different algorithms, so skip the rest of the tests
434 * until that is figured out. Trace output for the failures is about 10.5 MB!
437 if (result
== 0x7fffff9f) {
438 skip("Most likely running on Windows Vista which uses a different algorithm\n");
442 ok(result
== expected
,
443 "RtlUniform(&seed (seed == 2)) returns %x, expected %x\n",
447 * More tests show that if seed is odd the result must be incremented by 1:
450 expected
= seed
* 0xffffffed + 0x7fffffc3 + (seed
& 1);
451 result
= pRtlUniform(&seed
);
452 ok(result
== expected
,
453 "RtlUniform(&seed (seed == 3)) returns %x, expected %x\n",
457 expected
= seed
* 0xffffffed + 0x7fffffc3;
458 result
= pRtlUniform(&seed
);
459 ok(result
== expected
,
460 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %x, expected %x\n",
464 expected
= seed
* 0xffffffed + 0x7fffffc3 + 1;
465 result
= pRtlUniform(&seed
);
466 ok(result
== expected
,
467 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %x, expected %x\n",
470 * When seed is 0x6bca1ac there is an exception:
473 expected
= seed
* 0xffffffed + 0x7fffffc3 + 2;
474 result
= pRtlUniform(&seed
);
475 ok(result
== expected
,
476 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %x, expected %x\n",
479 * Note that up to here const_3 is not used
480 * (the highest bit of the result is not set).
482 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
485 expected
= (seed
* 0xffffffed + 0x7fffffc3) & MAXLONG
;
486 result
= pRtlUniform(&seed
);
487 ok(result
== expected
,
488 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %x, expected %x\n",
492 expected
= (seed
* 0xffffffed + 0x7fffffc3 + 1) & MAXLONG
;
493 result
= pRtlUniform(&seed
);
494 ok(result
== expected
,
495 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %x, expected %x\n",
498 * There are several ranges where for odd or even seed the result must be
499 * incremented by 1. You can see this ranges in the following test.
501 * For a full test use one of the following loop heads:
503 * for (num = 0; num <= 0xffffffff; num++) {
508 * for (num = 0; num <= 0xffffffff; num++) {
512 for (num
= 0; num
<= 100000; num
++) {
514 expected
= seed
* 0xffffffed + 0x7fffffc3;
515 if (seed
< 0x6bca1ac) {
516 expected
= expected
+ (seed
& 1);
517 } else if (seed
== 0x6bca1ac) {
518 expected
= (expected
+ 2) & MAXLONG
;
519 } else if (seed
< 0xd79435c) {
520 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
521 } else if (seed
< 0x1435e50b) {
522 expected
= expected
+ (seed
& 1);
523 } else if (seed
< 0x1af286ba) {
524 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
525 } else if (seed
< 0x21af2869) {
526 expected
= expected
+ (seed
& 1);
527 } else if (seed
< 0x286bca18) {
528 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
529 } else if (seed
< 0x2f286bc7) {
530 expected
= expected
+ (seed
& 1);
531 } else if (seed
< 0x35e50d77) {
532 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
533 } else if (seed
< 0x3ca1af26) {
534 expected
= expected
+ (seed
& 1);
535 } else if (seed
< 0x435e50d5) {
536 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
537 } else if (seed
< 0x4a1af284) {
538 expected
= expected
+ (seed
& 1);
539 } else if (seed
< 0x50d79433) {
540 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
541 } else if (seed
< 0x579435e2) {
542 expected
= expected
+ (seed
& 1);
543 } else if (seed
< 0x5e50d792) {
544 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
545 } else if (seed
< 0x650d7941) {
546 expected
= expected
+ (seed
& 1);
547 } else if (seed
< 0x6bca1af0) {
548 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
549 } else if (seed
< 0x7286bc9f) {
550 expected
= expected
+ (seed
& 1);
551 } else if (seed
< 0x79435e4e) {
552 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
553 } else if (seed
< 0x7ffffffd) {
554 expected
= expected
+ (seed
& 1);
555 } else if (seed
< 0x86bca1ac) {
556 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
557 } else if (seed
== 0x86bca1ac) {
558 expected
= (expected
+ 1) & MAXLONG
;
559 } else if (seed
< 0x8d79435c) {
560 expected
= expected
+ (seed
& 1);
561 } else if (seed
< 0x9435e50b) {
562 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
563 } else if (seed
< 0x9af286ba) {
564 expected
= expected
+ (seed
& 1);
565 } else if (seed
< 0xa1af2869) {
566 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
567 } else if (seed
< 0xa86bca18) {
568 expected
= expected
+ (seed
& 1);
569 } else if (seed
< 0xaf286bc7) {
570 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
571 } else if (seed
== 0xaf286bc7) {
572 expected
= (expected
+ 2) & MAXLONG
;
573 } else if (seed
< 0xb5e50d77) {
574 expected
= expected
+ (seed
& 1);
575 } else if (seed
< 0xbca1af26) {
576 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
577 } else if (seed
< 0xc35e50d5) {
578 expected
= expected
+ (seed
& 1);
579 } else if (seed
< 0xca1af284) {
580 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
581 } else if (seed
< 0xd0d79433) {
582 expected
= expected
+ (seed
& 1);
583 } else if (seed
< 0xd79435e2) {
584 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
585 } else if (seed
< 0xde50d792) {
586 expected
= expected
+ (seed
& 1);
587 } else if (seed
< 0xe50d7941) {
588 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
589 } else if (seed
< 0xebca1af0) {
590 expected
= expected
+ (seed
& 1);
591 } else if (seed
< 0xf286bc9f) {
592 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
593 } else if (seed
< 0xf9435e4e) {
594 expected
= expected
+ (seed
& 1);
595 } else if (seed
< 0xfffffffd) {
596 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
598 expected
= expected
+ (seed
& 1);
601 result
= pRtlUniform(&seed
);
602 ok(result
== expected
,
603 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
604 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, expected
);
606 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
607 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, expected
);
610 * Further investigation shows: In the different regions the highest bit
611 * is set or cleared when even or odd seeds need an increment by 1.
612 * This leads to a simplified algorithm:
614 * seed = seed * 0xffffffed + 0x7fffffc3;
615 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
616 * seed = (seed + 2) & MAXLONG;
617 * } else if (seed == 0x7fffffff) {
619 * } else if ((seed & 0x80000000) == 0) {
620 * seed = seed + (~seed & 1);
622 * seed = (seed + (seed & 1)) & MAXLONG;
625 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
627 * Now comes the funny part:
628 * It took me one weekend, to find the complicated algorithm and one day more,
629 * to find the simplified algorithm. Several weeks later I found out: The value
630 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
631 * nor with the simplified algorithm. In reality the native function and our
632 * function return a random number distributed over [0..MAXLONG-1]. Note
633 * that this is different from what native documentation states [0..MAXLONG].
634 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
636 * seed = (seed * const_1 + const_2) % MAXLONG;
638 * Further investigations show that the real algorithm is:
640 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
642 * This is checked with the test below:
645 for (num
= 0; num
<= 100000; num
++) {
646 expected
= (seed
* 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
648 result
= pRtlUniform(&seed
);
649 ok(result
== expected
,
650 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
651 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, expected
);
653 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
654 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, expected
);
657 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
658 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
659 * that there is more than one cycle of generated randon numbers ...
664 static void test_RtlRandom(void)
672 win_skip("RtlRandom is not available\n");
677 for (i
= 0; i
< sizeof(res
) / sizeof(res
[0]); i
++)
679 res
[i
] = pRtlRandom(&seed
);
680 ok(seed
!= res
[i
], "%i: seed is same as res %x\n", i
, seed
);
681 for (j
= 0; j
< i
; j
++)
682 ok(res
[i
] != res
[j
], "res[%i] (%x) is same as res[%i] (%x)\n", j
, res
[j
], i
, res
[i
]);
688 ACCESS_MASK GrantedAccess
;
689 ACCESS_MASK DesiredAccess
;
693 static const all_accesses_t all_accesses
[] = {
694 {0xFEDCBA76, 0xFEDCBA76, 1},
695 {0x00000000, 0xFEDCBA76, 0},
696 {0xFEDCBA76, 0x00000000, 1},
697 {0x00000000, 0x00000000, 1},
698 {0xFEDCBA76, 0xFEDCBA70, 1},
699 {0xFEDCBA70, 0xFEDCBA76, 0},
700 {0xFEDCBA76, 0xFEDC8A76, 1},
701 {0xFEDC8A76, 0xFEDCBA76, 0},
702 {0xFEDCBA76, 0xC8C4B242, 1},
703 {0xC8C4B242, 0xFEDCBA76, 0},
705 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
708 static void test_RtlAreAllAccessesGranted(void)
710 unsigned int test_num
;
713 if (!pRtlAreAllAccessesGranted
)
715 win_skip("RtlAreAllAccessesGranted is not available\n");
719 for (test_num
= 0; test_num
< NB_ALL_ACCESSES
; test_num
++) {
720 result
= pRtlAreAllAccessesGranted(all_accesses
[test_num
].GrantedAccess
,
721 all_accesses
[test_num
].DesiredAccess
);
722 ok(all_accesses
[test_num
].result
== result
,
723 "(test %d): RtlAreAllAccessesGranted(%08x, %08x) returns %d, expected %d\n",
724 test_num
, all_accesses
[test_num
].GrantedAccess
,
725 all_accesses
[test_num
].DesiredAccess
,
726 result
, all_accesses
[test_num
].result
);
732 ACCESS_MASK GrantedAccess
;
733 ACCESS_MASK DesiredAccess
;
737 static const any_accesses_t any_accesses
[] = {
738 {0xFEDCBA76, 0xFEDCBA76, 1},
739 {0x00000000, 0xFEDCBA76, 0},
740 {0xFEDCBA76, 0x00000000, 0},
741 {0x00000000, 0x00000000, 0},
742 {0xFEDCBA76, 0x01234589, 0},
743 {0x00040000, 0xFEDCBA76, 1},
744 {0x00040000, 0xFED8BA76, 0},
745 {0xFEDCBA76, 0x00040000, 1},
746 {0xFED8BA76, 0x00040000, 0},
748 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
751 static void test_RtlAreAnyAccessesGranted(void)
753 unsigned int test_num
;
756 if (!pRtlAreAnyAccessesGranted
)
758 win_skip("RtlAreAnyAccessesGranted is not available\n");
762 for (test_num
= 0; test_num
< NB_ANY_ACCESSES
; test_num
++) {
763 result
= pRtlAreAnyAccessesGranted(any_accesses
[test_num
].GrantedAccess
,
764 any_accesses
[test_num
].DesiredAccess
);
765 ok(any_accesses
[test_num
].result
== result
,
766 "(test %d): RtlAreAnyAccessesGranted(%08x, %08x) returns %d, expected %d\n",
767 test_num
, any_accesses
[test_num
].GrantedAccess
,
768 any_accesses
[test_num
].DesiredAccess
,
769 result
, any_accesses
[test_num
].result
);
773 static void test_RtlComputeCrc32(void)
777 if (!pRtlComputeCrc32
)
779 win_skip("RtlComputeCrc32 is not available\n");
783 crc
= pRtlComputeCrc32(crc
, (const BYTE
*)src
, LEN
);
784 ok(crc
== 0x40861dc2,"Expected 0x40861dc2, got %8x\n", crc
);
788 typedef struct MY_HANDLE
790 RTL_HANDLE RtlHandle
;
794 static inline void RtlpMakeHandleAllocated(RTL_HANDLE
* Handle
)
796 ULONG_PTR
*AllocatedBit
= (ULONG_PTR
*)(&Handle
->Next
);
797 *AllocatedBit
= *AllocatedBit
| 1;
800 static void test_HandleTables(void)
805 MY_HANDLE
* MyHandle
;
806 RTL_HANDLE_TABLE HandleTable
;
808 if (!pRtlInitializeHandleTable
)
810 win_skip("RtlInitializeHandleTable is not available\n");
814 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE
), &HandleTable
);
815 MyHandle
= (MY_HANDLE
*)pRtlAllocateHandle(&HandleTable
, &Index
);
816 ok(MyHandle
!= NULL
, "RtlAllocateHandle failed\n");
817 RtlpMakeHandleAllocated(&MyHandle
->RtlHandle
);
819 result
= pRtlIsValidIndexHandle(&HandleTable
, Index
, (RTL_HANDLE
**)&MyHandle
);
820 ok(result
, "Handle %p wasn't valid\n", MyHandle
);
821 result
= pRtlFreeHandle(&HandleTable
, &MyHandle
->RtlHandle
);
822 ok(result
, "Couldn't free handle %p\n", MyHandle
);
823 status
= pRtlDestroyHandleTable(&HandleTable
);
824 ok(status
== STATUS_SUCCESS
, "RtlDestroyHandleTable failed with error 0x%08x\n", status
);
827 static void test_RtlAllocateAndInitializeSid(void)
830 SID_IDENTIFIER_AUTHORITY sia
= {{ 1, 2, 3, 4, 5, 6 }};
833 if (!pRtlAllocateAndInitializeSid
)
835 win_skip("RtlAllocateAndInitializeSid is not available\n");
839 ret
= pRtlAllocateAndInitializeSid(&sia
, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid
);
840 ok(!ret
, "RtlAllocateAndInitializeSid error %08x\n", ret
);
841 ret
= pRtlFreeSid(psid
);
842 ok(!ret
, "RtlFreeSid error %08x\n", ret
);
844 /* these tests crash on XP */
847 pRtlAllocateAndInitializeSid(NULL
, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid
);
848 pRtlAllocateAndInitializeSid(&sia
, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL
);
851 ret
= pRtlAllocateAndInitializeSid(&sia
, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid
);
852 ok(ret
== STATUS_INVALID_SID
, "wrong error %08x\n", ret
);
855 static void test_RtlDeleteTimer(void)
859 if (!pRtlDeleteTimer
)
861 win_skip("RtlDeleteTimer is not available\n");
865 ret
= pRtlDeleteTimer(NULL
, NULL
, NULL
);
866 ok(ret
== STATUS_INVALID_PARAMETER_1
||
867 ret
== STATUS_INVALID_PARAMETER
, /* W2K */
868 "expected STATUS_INVALID_PARAMETER_1 or STATUS_INVALID_PARAMETER, got %x\n", ret
);
871 static void test_RtlThreadErrorMode(void)
878 if (!pRtlGetThreadErrorMode
|| !pRtlSetThreadErrorMode
)
880 win_skip("RtlGetThreadErrorMode and/or RtlSetThreadErrorMode not available\n");
884 if (!pIsWow64Process
|| !pIsWow64Process(GetCurrentProcess(), &is_wow64
))
887 oldmode
= pRtlGetThreadErrorMode();
889 status
= pRtlSetThreadErrorMode(0x70, &mode
);
890 ok(status
== STATUS_SUCCESS
||
891 status
== STATUS_WAIT_1
, /* Vista */
892 "RtlSetThreadErrorMode failed with error 0x%08x\n", status
);
894 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
896 ok(pRtlGetThreadErrorMode() == 0x70,
897 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode
, 0x70);
898 if (!is_wow64
&& pNtCurrentTeb
)
899 ok(pNtCurrentTeb()->HardErrorDisabled
== 0x70,
900 "The TEB contains 0x%x, expected 0x%x\n",
901 pNtCurrentTeb()->HardErrorDisabled
, 0x70);
903 status
= pRtlSetThreadErrorMode(0, &mode
);
904 ok(status
== STATUS_SUCCESS
||
905 status
== STATUS_WAIT_1
, /* Vista */
906 "RtlSetThreadErrorMode failed with error 0x%08x\n", status
);
908 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
910 ok(pRtlGetThreadErrorMode() == 0,
911 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode
, 0);
912 if (!is_wow64
&& pNtCurrentTeb
)
913 ok(pNtCurrentTeb()->HardErrorDisabled
== 0,
914 "The TEB contains 0x%x, expected 0x%x\n",
915 pNtCurrentTeb()->HardErrorDisabled
, 0);
917 for (mode
= 1; mode
; mode
<<= 1)
919 status
= pRtlSetThreadErrorMode(mode
, NULL
);
921 ok(status
== STATUS_SUCCESS
||
922 status
== STATUS_WAIT_1
, /* Vista */
923 "RtlSetThreadErrorMode(%x,NULL) failed with error 0x%08x\n",
926 ok(status
== STATUS_INVALID_PARAMETER_1
,
927 "RtlSetThreadErrorMode(%x,NULL) returns 0x%08x, "
928 "expected STATUS_INVALID_PARAMETER_1\n",
932 pRtlSetThreadErrorMode(oldmode
, NULL
);
935 static void test_LdrProcessRelocationBlock(void)
937 IMAGE_BASE_RELOCATION
*ret
;
942 if(!pLdrProcessRelocationBlock
) {
943 win_skip("LdrProcessRelocationBlock not available\n");
948 reloc
= IMAGE_REL_BASED_HIGHLOW
<<12;
949 ret
= pLdrProcessRelocationBlock(&addr32
, 1, &reloc
, 0x500050);
950 ok((USHORT
*)ret
== &reloc
+1, "ret = %p, expected %p\n", ret
, &reloc
+1);
951 ok(addr32
== 0x550055, "addr32 = %x, expected 0x550055\n", addr32
);
954 reloc
= IMAGE_REL_BASED_HIGH
<<12;
955 ret
= pLdrProcessRelocationBlock(&addr16
, 1, &reloc
, 0x500060);
956 ok((USHORT
*)ret
== &reloc
+1, "ret = %p, expected %p\n", ret
, &reloc
+1);
957 ok(addr16
== 0x555, "addr16 = %x, expected 0x555\n", addr16
);
960 reloc
= IMAGE_REL_BASED_LOW
<<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
== 0x565, "addr16 = %x, expected 0x565\n", addr16
);
966 static void test_RtlIpv4AddressToString(void)
973 if (!pRtlIpv4AddressToStringA
)
975 win_skip("RtlIpv4AddressToStringA not available\n");
979 ip
.S_un
.S_un_b
.s_b1
= 1;
980 ip
.S_un
.S_un_b
.s_b2
= 2;
981 ip
.S_un
.S_un_b
.s_b3
= 3;
982 ip
.S_un
.S_un_b
.s_b4
= 4;
984 memset(buffer
, '#', sizeof(buffer
) - 1);
985 buffer
[sizeof(buffer
) -1] = 0;
986 res
= pRtlIpv4AddressToStringA(&ip
, buffer
);
987 len
= strlen(buffer
);
988 ok(res
== (buffer
+ len
), "got %p with '%s' (expected %p)\n", res
, buffer
, buffer
+ len
);
990 res
= pRtlIpv4AddressToStringA(&ip
, NULL
);
991 ok( (res
== (char *)~0) ||
992 broken(res
== (char *)len
), /* XP and w2003 */
993 "got %p (expected ~0)\n", res
);
996 /* this crashes in windows */
997 memset(buffer
, '#', sizeof(buffer
) - 1);
998 buffer
[sizeof(buffer
) -1] = 0;
999 res
= pRtlIpv4AddressToStringA(NULL
, buffer
);
1000 trace("got %p with '%s'\n", res
, buffer
);
1004 /* this crashes in windows */
1005 res
= pRtlIpv4AddressToStringA(NULL
, NULL
);
1006 trace("got %p\n", res
);
1010 static void test_RtlIpv4AddressToStringEx(void)
1012 CHAR ip_1234
[] = "1.2.3.4";
1013 CHAR ip_1234_80
[] = "1.2.3.4:80";
1022 if (!pRtlIpv4AddressToStringExA
)
1024 win_skip("RtlIpv4AddressToStringExA not available\n");
1028 ip
.S_un
.S_un_b
.s_b1
= 1;
1029 ip
.S_un
.S_un_b
.s_b2
= 2;
1030 ip
.S_un
.S_un_b
.s_b3
= 3;
1031 ip
.S_un
.S_un_b
.s_b4
= 4;
1034 expect
= ip_1234_80
;
1036 size
= sizeof(buffer
);
1037 memset(buffer
, '#', sizeof(buffer
) - 1);
1038 buffer
[sizeof(buffer
) -1] = 0;
1039 res
= pRtlIpv4AddressToStringExA(&ip
, port
, buffer
, &size
);
1040 used
= strlen(buffer
);
1041 ok( (res
== STATUS_SUCCESS
) &&
1042 (size
== strlen(expect
) + 1) && !strcmp(buffer
, expect
),
1043 "got 0x%x and size %d with '%s'\n", res
, size
, buffer
);
1046 memset(buffer
, '#', sizeof(buffer
) - 1);
1047 buffer
[sizeof(buffer
) -1] = 0;
1048 res
= pRtlIpv4AddressToStringExA(&ip
, port
, buffer
, &size
);
1049 ok( (res
== STATUS_SUCCESS
) &&
1050 (size
== strlen(expect
) + 1) && !strcmp(buffer
, expect
),
1051 "got 0x%x and size %d with '%s'\n", res
, size
, buffer
);
1054 memset(buffer
, '#', sizeof(buffer
) - 1);
1055 buffer
[sizeof(buffer
) -1] = 0;
1056 res
= pRtlIpv4AddressToStringExA(&ip
, port
, buffer
, &size
);
1057 ok( (res
== STATUS_INVALID_PARAMETER
) && (size
== used
+ 1),
1058 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1059 res
, size
, buffer
, used
+ 1);
1062 memset(buffer
, '#', sizeof(buffer
) - 1);
1063 buffer
[sizeof(buffer
) -1] = 0;
1064 res
= pRtlIpv4AddressToStringExA(&ip
, port
, buffer
, &size
);
1065 ok( (res
== STATUS_INVALID_PARAMETER
) && (size
== used
+ 1),
1066 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1067 res
, size
, buffer
, used
+ 1);
1070 /* to get only the ip, use 0 as port */
1074 size
= sizeof(buffer
);
1075 memset(buffer
, '#', sizeof(buffer
) - 1);
1076 buffer
[sizeof(buffer
) -1] = 0;
1077 res
= pRtlIpv4AddressToStringExA(&ip
, port
, buffer
, &size
);
1078 used
= strlen(buffer
);
1079 ok( (res
== STATUS_SUCCESS
) &&
1080 (size
== strlen(expect
) + 1) && !strcmp(buffer
, expect
),
1081 "got 0x%x and size %d with '%s'\n", res
, size
, buffer
);
1084 memset(buffer
, '#', sizeof(buffer
) - 1);
1085 buffer
[sizeof(buffer
) -1] = 0;
1086 res
= pRtlIpv4AddressToStringExA(&ip
, port
, buffer
, &size
);
1087 ok( (res
== STATUS_SUCCESS
) &&
1088 (size
== strlen(expect
) + 1) && !strcmp(buffer
, expect
),
1089 "got 0x%x and size %d with '%s'\n", res
, size
, buffer
);
1092 memset(buffer
, '#', sizeof(buffer
) - 1);
1093 buffer
[sizeof(buffer
) -1] = 0;
1094 res
= pRtlIpv4AddressToStringExA(&ip
, port
, buffer
, &size
);
1095 ok( (res
== STATUS_INVALID_PARAMETER
) && (size
== used
+ 1),
1096 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1097 res
, size
, buffer
, used
+ 1);
1100 memset(buffer
, '#', sizeof(buffer
) - 1);
1101 buffer
[sizeof(buffer
) -1] = 0;
1102 res
= pRtlIpv4AddressToStringExA(&ip
, port
, buffer
, &size
);
1103 ok( (res
== STATUS_INVALID_PARAMETER
) && (size
== used
+ 1),
1104 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1105 res
, size
, buffer
, used
+ 1);
1108 /* parameters are checked */
1109 memset(buffer
, '#', sizeof(buffer
) - 1);
1110 buffer
[sizeof(buffer
) -1] = 0;
1111 res
= pRtlIpv4AddressToStringExA(&ip
, 0, buffer
, NULL
);
1112 ok(res
== STATUS_INVALID_PARAMETER
,
1113 "got 0x%x with '%s' (expected STATUS_INVALID_PARAMETER)\n", res
, buffer
);
1115 size
= sizeof(buffer
);
1116 res
= pRtlIpv4AddressToStringExA(&ip
, 0, NULL
, &size
);
1117 ok( res
== STATUS_INVALID_PARAMETER
,
1118 "got 0x%x and size %d (expected STATUS_INVALID_PARAMETER)\n", res
, size
);
1120 size
= sizeof(buffer
);
1121 memset(buffer
, '#', sizeof(buffer
) - 1);
1122 buffer
[sizeof(buffer
) -1] = 0;
1123 res
= pRtlIpv4AddressToStringExA(NULL
, 0, buffer
, &size
);
1124 ok( res
== STATUS_INVALID_PARAMETER
,
1125 "got 0x%x and size %d with '%s' (expected STATUS_INVALID_PARAMETER)\n",
1129 static void test_RtlIpv4StringToAddress(void)
1132 IN_ADDR ip
, expected_ip
;
1139 int terminator_offset
;
1141 BOOL strict_is_different
;
1142 NTSTATUS res_strict
;
1143 int terminator_offset_strict
;
1147 { "", STATUS_INVALID_PARAMETER
, 0, { -1 } },
1148 { " ", STATUS_INVALID_PARAMETER
, 0, { -1 } },
1149 { "1.1.1.1", STATUS_SUCCESS
, 7, { 1, 1, 1, 1 } },
1150 { "0.0.0.0", STATUS_SUCCESS
, 7, { 0, 0, 0, 0 } },
1151 { "255.255.255.255", STATUS_SUCCESS
, 15, { 255, 255, 255, 255 } },
1152 { "255.255.255.255:123",
1153 STATUS_SUCCESS
, 15, { 255, 255, 255, 255 } },
1154 { "255.255.255.256", STATUS_INVALID_PARAMETER
, 15, { -1 } },
1155 { "255.255.255.4294967295",
1156 STATUS_INVALID_PARAMETER
, 22, { -1 } },
1157 { "255.255.255.4294967296",
1158 STATUS_INVALID_PARAMETER
, 21, { -1 } },
1159 { "255.255.255.4294967297",
1160 STATUS_INVALID_PARAMETER
, 21, { -1 } },
1161 { "a", STATUS_INVALID_PARAMETER
, 0, { -1 } },
1162 { "1.1.1.0xaA", STATUS_SUCCESS
, 10, { 1, 1, 1, 170 },
1163 TRUE
, STATUS_INVALID_PARAMETER
, 8, { -1 } },
1164 { "1.1.1.0XaA", STATUS_SUCCESS
, 10, { 1, 1, 1, 170 },
1165 TRUE
, STATUS_INVALID_PARAMETER
, 8, { -1 } },
1166 { "1.1.1.0x", STATUS_INVALID_PARAMETER
, 8, { -1 } },
1167 { "1.1.1.0xff", STATUS_SUCCESS
, 10, { 1, 1, 1, 255 },
1168 TRUE
, STATUS_INVALID_PARAMETER
, 8, { -1 } },
1169 { "1.1.1.0x100", STATUS_INVALID_PARAMETER
, 11, { -1 },
1170 TRUE
, STATUS_INVALID_PARAMETER
, 8, { -1 } },
1171 { "1.1.1.0xffffffff",STATUS_INVALID_PARAMETER
, 16, { -1 },
1172 TRUE
, STATUS_INVALID_PARAMETER
, 8, { -1 } },
1173 { "1.1.1.0x100000000",
1174 STATUS_INVALID_PARAMETER
, 16, { -1, 0, 0, 0 },
1175 TRUE
, STATUS_INVALID_PARAMETER
, 8, { -1 } },
1176 { "1.1.1.010", STATUS_SUCCESS
, 9, { 1, 1, 1, 8 },
1177 TRUE
, STATUS_INVALID_PARAMETER
, 7, { -1 } },
1178 { "1.1.1.00", STATUS_SUCCESS
, 8, { 1, 1, 1, 0 },
1179 TRUE
, STATUS_INVALID_PARAMETER
, 7, { -1 } },
1180 { "1.1.1.007", STATUS_SUCCESS
, 9, { 1, 1, 1, 7 },
1181 TRUE
, STATUS_INVALID_PARAMETER
, 7, { -1 } },
1182 { "1.1.1.08", STATUS_INVALID_PARAMETER
, 7, { -1 } },
1183 { "1.1.1.008", STATUS_SUCCESS
, 8, { 1, 1, 1, 0 },
1184 TRUE
, STATUS_INVALID_PARAMETER
, 7, { -1 } },
1185 { "1.1.1.0a", STATUS_SUCCESS
, 7, { 1, 1, 1, 0 } },
1186 { "1.1.1.0o10", STATUS_SUCCESS
, 7, { 1, 1, 1, 0 } },
1187 { "1.1.1.0b10", STATUS_SUCCESS
, 7, { 1, 1, 1, 0 } },
1188 { "1.1.1.-2", STATUS_INVALID_PARAMETER
, 6, { -1 } },
1189 { "1", STATUS_SUCCESS
, 1, { 0, 0, 0, 1 },
1190 TRUE
, STATUS_INVALID_PARAMETER
, 1, { -1 } },
1191 { "-1", STATUS_INVALID_PARAMETER
, 0, { -1 } },
1192 { "203569230", STATUS_SUCCESS
, 9, { 12, 34, 56, 78 },
1193 TRUE
, STATUS_INVALID_PARAMETER
, 9, { -1 } },
1194 { "1.223756", STATUS_SUCCESS
, 8, { 1, 3, 106, 12 },
1195 TRUE
, STATUS_INVALID_PARAMETER
, 8, { -1 } },
1196 { "3.4.756", STATUS_SUCCESS
, 7, { 3, 4, 2, 244 },
1197 TRUE
, STATUS_INVALID_PARAMETER
, 7, { -1 } },
1198 { "3.4.756.1", STATUS_INVALID_PARAMETER
, 9, { -1 } },
1199 { "3.4.65536", STATUS_INVALID_PARAMETER
, 9, { -1 } },
1200 { "3.4.5.6.7", STATUS_INVALID_PARAMETER
, 7, { -1 } },
1201 { "3.4.5.+6", STATUS_INVALID_PARAMETER
, 6, { -1 } },
1202 { " 3.4.5.6", STATUS_INVALID_PARAMETER
, 0, { -1 } },
1203 { "\t3.4.5.6", STATUS_INVALID_PARAMETER
, 0, { -1 } },
1204 { "3.4.5.6 ", STATUS_SUCCESS
, 7, { 3, 4, 5, 6 } },
1205 { "3. 4.5.6", STATUS_INVALID_PARAMETER
, 2, { -1 } },
1206 { ".", STATUS_INVALID_PARAMETER
, 1, { -1 } },
1207 { "..", STATUS_INVALID_PARAMETER
, 1, { -1 } },
1208 { "1.", STATUS_INVALID_PARAMETER
, 2, { -1 } },
1209 { "1..", STATUS_INVALID_PARAMETER
, 3, { -1 } },
1210 { ".1", STATUS_INVALID_PARAMETER
, 1, { -1 } },
1211 { ".1.", STATUS_INVALID_PARAMETER
, 1, { -1 } },
1212 { ".1.2.3", STATUS_INVALID_PARAMETER
, 1, { -1 } },
1213 { "0.1.2.3", STATUS_SUCCESS
, 7, { 0, 1, 2, 3 } },
1214 { "0.1.2.3.", STATUS_INVALID_PARAMETER
, 7, { -1 } },
1215 { "[0.1.2.3]", STATUS_INVALID_PARAMETER
, 0, { -1 } },
1216 { "::1", STATUS_INVALID_PARAMETER
, 0, { -1 } },
1217 { ":1", STATUS_INVALID_PARAMETER
, 0, { -1 } },
1219 const int testcount
= sizeof(tests
) / sizeof(tests
[0]);
1222 if (!pRtlIpv4StringToAddressA
)
1224 skip("RtlIpv4StringToAddress not available\n");
1230 /* leaving either parameter NULL crashes on Windows */
1231 res
= pRtlIpv4StringToAddressA(NULL
, FALSE
, &terminator
, &ip
);
1232 res
= pRtlIpv4StringToAddressA("1.1.1.1", FALSE
, NULL
, &ip
);
1233 res
= pRtlIpv4StringToAddressA("1.1.1.1", FALSE
, &terminator
, NULL
);
1234 /* same for the wide char version */
1236 res = pRtlIpv4StringToAddressW(NULL, FALSE, &terminatorW, &ip);
1237 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, NULL, &ip);
1238 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, &terminatorW, NULL);
1242 for (i
= 0; i
< testcount
; i
++)
1245 terminator
= &dummy
;
1246 ip
.S_un
.S_addr
= 0xabababab;
1247 res
= pRtlIpv4StringToAddressA(tests
[i
].address
, FALSE
, &terminator
, &ip
);
1248 ok(res
== tests
[i
].res
,
1249 "[%s] res = 0x%08x, expected 0x%08x\n",
1250 tests
[i
].address
, res
, tests
[i
].res
);
1251 ok(terminator
== tests
[i
].address
+ tests
[i
].terminator_offset
,
1252 "[%s] terminator = %p, expected %p\n",
1253 tests
[i
].address
, terminator
, tests
[i
].address
+ tests
[i
].terminator_offset
);
1254 if (tests
[i
].ip
[0] == -1)
1255 expected_ip
.S_un
.S_addr
= 0xabababab;
1258 expected_ip
.S_un
.S_un_b
.s_b1
= tests
[i
].ip
[0];
1259 expected_ip
.S_un
.S_un_b
.s_b2
= tests
[i
].ip
[1];
1260 expected_ip
.S_un
.S_un_b
.s_b3
= tests
[i
].ip
[2];
1261 expected_ip
.S_un
.S_un_b
.s_b4
= tests
[i
].ip
[3];
1263 ok(ip
.S_un
.S_addr
== expected_ip
.S_un
.S_addr
,
1264 "[%s] ip = %08x, expected %08x\n",
1265 tests
[i
].address
, ip
.S_un
.S_addr
, expected_ip
.S_un
.S_addr
);
1267 if (!tests
[i
].strict_is_different
)
1269 tests
[i
].res_strict
= tests
[i
].res
;
1270 tests
[i
].terminator_offset_strict
= tests
[i
].terminator_offset
;
1271 tests
[i
].ip_strict
[0] = tests
[i
].ip
[0];
1272 tests
[i
].ip_strict
[1] = tests
[i
].ip
[1];
1273 tests
[i
].ip_strict
[2] = tests
[i
].ip
[2];
1274 tests
[i
].ip_strict
[3] = tests
[i
].ip
[3];
1277 terminator
= &dummy
;
1278 ip
.S_un
.S_addr
= 0xabababab;
1279 res
= pRtlIpv4StringToAddressA(tests
[i
].address
, TRUE
, &terminator
, &ip
);
1280 ok(res
== tests
[i
].res_strict
,
1281 "[%s] res = 0x%08x, expected 0x%08x\n",
1282 tests
[i
].address
, res
, tests
[i
].res_strict
);
1283 ok(terminator
== tests
[i
].address
+ tests
[i
].terminator_offset_strict
,
1284 "[%s] terminator = %p, expected %p\n",
1285 tests
[i
].address
, terminator
, tests
[i
].address
+ tests
[i
].terminator_offset_strict
);
1286 if (tests
[i
].ip_strict
[0] == -1)
1287 expected_ip
.S_un
.S_addr
= 0xabababab;
1290 expected_ip
.S_un
.S_un_b
.s_b1
= tests
[i
].ip_strict
[0];
1291 expected_ip
.S_un
.S_un_b
.s_b2
= tests
[i
].ip_strict
[1];
1292 expected_ip
.S_un
.S_un_b
.s_b3
= tests
[i
].ip_strict
[2];
1293 expected_ip
.S_un
.S_un_b
.s_b4
= tests
[i
].ip_strict
[3];
1295 ok(ip
.S_un
.S_addr
== expected_ip
.S_un
.S_addr
,
1296 "[%s] ip = %08x, expected %08x\n",
1297 tests
[i
].address
, ip
.S_un
.S_addr
, expected_ip
.S_un
.S_addr
);
1301 static void test_LdrAddRefDll(void)
1309 win_skip( "LdrAddRefDll not supported\n" );
1313 mod
= LoadLibraryA("comctl32.dll");
1314 ok(mod
!= NULL
, "got %p\n", mod
);
1315 ret
= FreeLibrary(mod
);
1316 ok(ret
, "got %d\n", ret
);
1318 mod2
= GetModuleHandleA("comctl32.dll");
1319 ok(mod2
== NULL
, "got %p\n", mod2
);
1321 /* load, addref and release 2 times */
1322 mod
= LoadLibraryA("comctl32.dll");
1323 ok(mod
!= NULL
, "got %p\n", mod
);
1324 status
= pLdrAddRefDll(0, mod
);
1325 ok(status
== STATUS_SUCCESS
, "got 0x%08x\n", status
);
1326 ret
= FreeLibrary(mod
);
1327 ok(ret
, "got %d\n", ret
);
1329 mod2
= GetModuleHandleA("comctl32.dll");
1330 ok(mod2
!= NULL
, "got %p\n", mod2
);
1331 ret
= FreeLibrary(mod
);
1332 ok(ret
, "got %d\n", ret
);
1334 mod2
= GetModuleHandleA("comctl32.dll");
1335 ok(mod2
== NULL
, "got %p\n", mod2
);
1338 mod
= LoadLibraryA("comctl32.dll");
1339 ok(mod
!= NULL
, "got %p\n", mod
);
1340 status
= pLdrAddRefDll(LDR_ADDREF_DLL_PIN
, mod
);
1341 ok(status
== STATUS_SUCCESS
, "got 0x%08x\n", status
);
1343 ret
= FreeLibrary(mod
);
1344 ok(ret
, "got %d\n", ret
);
1345 ret
= FreeLibrary(mod
);
1346 ok(ret
, "got %d\n", ret
);
1347 ret
= FreeLibrary(mod
);
1348 ok(ret
, "got %d\n", ret
);
1349 ret
= FreeLibrary(mod
);
1350 ok(ret
, "got %d\n", ret
);
1352 mod2
= GetModuleHandleA("comctl32.dll");
1353 ok(mod2
!= NULL
, "got %p\n", mod2
);
1356 static void test_LdrLockLoaderLock(void)
1362 if (!pLdrLockLoaderLock
)
1364 win_skip("LdrLockLoaderLock() is not available\n");
1371 status
= pLdrLockLoaderLock(0x10, &result
, &magic
);
1372 ok(status
== STATUS_INVALID_PARAMETER_1
, "got 0x%08x\n", status
);
1373 ok(result
== 0, "got %d\n", result
);
1374 ok(magic
== 0, "got %lx\n", magic
);
1377 status
= pLdrLockLoaderLock(0x10, NULL
, &magic
);
1378 ok(status
== STATUS_INVALID_PARAMETER_1
, "got 0x%08x\n", status
);
1379 ok(magic
== 0, "got %lx\n", magic
);
1382 status
= pLdrLockLoaderLock(0x10, &result
, NULL
);
1383 ok(status
== STATUS_INVALID_PARAMETER_1
, "got 0x%08x\n", status
);
1384 ok(result
== 0, "got %d\n", result
);
1386 /* non-blocking mode, result is null */
1388 status
= pLdrLockLoaderLock(0x2, NULL
, &magic
);
1389 ok(status
== STATUS_INVALID_PARAMETER_2
, "got 0x%08x\n", status
);
1390 ok(magic
== 0, "got %lx\n", magic
);
1392 /* magic pointer is null */
1394 status
= pLdrLockLoaderLock(0, &result
, NULL
);
1395 ok(status
== STATUS_INVALID_PARAMETER_3
, "got 0x%08x\n", status
);
1396 ok(result
== 0, "got %d\n", result
);
1398 /* lock in non-blocking mode */
1401 status
= pLdrLockLoaderLock(0x2, &result
, &magic
);
1402 ok(status
== STATUS_SUCCESS
, "got 0x%08x\n", status
);
1403 ok(result
== 1, "got %d\n", result
);
1404 ok(magic
!= 0, "got %lx\n", magic
);
1405 pLdrUnlockLoaderLock(0, magic
);
1408 static void test_RtlCompressBuffer(void)
1410 ULONG compress_workspace
, decompress_workspace
;
1411 static const UCHAR test_buffer
[] = "WineWineWine";
1412 static UCHAR buf1
[0x1000], buf2
[0x1000];
1413 ULONG final_size
, buf_size
;
1414 UCHAR
*workspace
= NULL
;
1417 if (!pRtlCompressBuffer
|| !pRtlDecompressBuffer
|| !pRtlGetCompressionWorkSpaceSize
)
1419 win_skip("skipping RtlCompressBuffer tests, required functions not available\n");
1423 compress_workspace
= decompress_workspace
= 0xdeadbeef;
1424 status
= pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1
, &compress_workspace
,
1425 &decompress_workspace
);
1426 ok(status
== STATUS_SUCCESS
, "got wrong status 0x%08x\n", status
);
1427 ok(compress_workspace
!= 0, "got wrong compress_workspace %u\n", compress_workspace
);
1428 workspace
= HeapAlloc(GetProcessHeap(), 0, compress_workspace
);
1429 ok(workspace
!= NULL
, "HeapAlloc failed %d\n", GetLastError());
1431 /* test compression format / engine */
1432 final_size
= 0xdeadbeef;
1433 status
= pRtlCompressBuffer(COMPRESSION_FORMAT_NONE
, test_buffer
, sizeof(test_buffer
),
1434 buf1
, sizeof(buf1
) - 1, 4096, &final_size
, workspace
);
1435 ok(status
== STATUS_INVALID_PARAMETER
, "got wrong status 0x%08x\n", status
);
1436 ok(final_size
== 0xdeadbeef, "got wrong final_size %u\n", final_size
);
1438 final_size
= 0xdeadbeef;
1439 status
= pRtlCompressBuffer(COMPRESSION_FORMAT_DEFAULT
, 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(0xFF, test_buffer
, sizeof(test_buffer
),
1446 buf1
, sizeof(buf1
) - 1, 4096, &final_size
, workspace
);
1447 ok(status
== STATUS_UNSUPPORTED_COMPRESSION
, "got wrong status 0x%08x\n", status
);
1448 ok(final_size
== 0xdeadbeef, "got wrong final_size %u\n", final_size
);
1450 /* test compression */
1451 final_size
= 0xdeadbeef;
1452 memset(buf1
, 0x11, sizeof(buf1
));
1453 status
= pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1
, test_buffer
, sizeof(test_buffer
),
1454 buf1
, sizeof(buf1
), 4096, &final_size
, workspace
);
1455 ok(status
== STATUS_SUCCESS
, "got wrong status 0x%08x\n", status
);
1456 ok((*(WORD
*)buf1
& 0x7000) == 0x3000, "no chunk signature found %04x\n", *(WORD
*)buf1
);
1458 ok(final_size
< sizeof(test_buffer
), "got wrong final_size %u\n", final_size
);
1460 /* test decompression */
1461 buf_size
= final_size
;
1462 final_size
= 0xdeadbeef;
1463 memset(buf2
, 0x11, sizeof(buf2
));
1464 status
= pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1
, buf2
, sizeof(buf2
),
1465 buf1
, buf_size
, &final_size
);
1466 ok(status
== STATUS_SUCCESS
, "got wrong status 0x%08x\n", status
);
1467 ok(final_size
== sizeof(test_buffer
), "got wrong final_size %u\n", final_size
);
1468 ok(!memcmp(buf2
, test_buffer
, sizeof(test_buffer
)), "got wrong decoded data\n");
1469 ok(buf2
[sizeof(test_buffer
)] == 0x11, "too many bytes written\n");
1471 /* buffer too small */
1472 final_size
= 0xdeadbeef;
1473 memset(buf1
, 0x11, sizeof(buf1
));
1474 status
= pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1
, test_buffer
, sizeof(test_buffer
),
1475 buf1
, 4, 4096, &final_size
, workspace
);
1476 ok(status
== STATUS_BUFFER_TOO_SMALL
, "got wrong status 0x%08x\n", status
);
1478 HeapFree(GetProcessHeap(), 0, workspace
);
1481 static void test_RtlGetCompressionWorkSpaceSize(void)
1483 ULONG compress_workspace
, decompress_workspace
;
1486 if (!pRtlGetCompressionWorkSpaceSize
)
1488 win_skip("RtlGetCompressionWorkSpaceSize is not available\n");
1492 /* test invalid format / engine */
1493 status
= pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_NONE
, &compress_workspace
,
1494 &decompress_workspace
);
1495 ok(status
== STATUS_INVALID_PARAMETER
, "got wrong status 0x%08x\n", status
);
1497 status
= pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_DEFAULT
, &compress_workspace
,
1498 &decompress_workspace
);
1499 ok(status
== STATUS_INVALID_PARAMETER
, "got wrong status 0x%08x\n", status
);
1501 status
= pRtlGetCompressionWorkSpaceSize(0xFF, &compress_workspace
, &decompress_workspace
);
1502 ok(status
== STATUS_UNSUPPORTED_COMPRESSION
, "got wrong status 0x%08x\n", status
);
1504 /* test LZNT1 with normal and maximum compression */
1505 compress_workspace
= decompress_workspace
= 0xdeadbeef;
1506 status
= pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1
, &compress_workspace
,
1507 &decompress_workspace
);
1508 ok(status
== STATUS_SUCCESS
, "got wrong status 0x%08x\n", status
);
1509 ok(compress_workspace
!= 0, "got wrong compress_workspace %u\n", compress_workspace
);
1510 ok(decompress_workspace
== 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace
);
1512 compress_workspace
= decompress_workspace
= 0xdeadbeef;
1513 status
= pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1
| COMPRESSION_ENGINE_MAXIMUM
,
1514 &compress_workspace
, &decompress_workspace
);
1515 ok(status
== STATUS_SUCCESS
, "got wrong status 0x%08x\n", status
);
1516 ok(compress_workspace
!= 0, "got wrong compress_workspace %u\n", compress_workspace
);
1517 ok(decompress_workspace
== 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace
);
1520 /* helper for test_RtlDecompressBuffer, checks if a chunk is incomplete */
1521 static BOOL
is_incomplete_chunk(const UCHAR
*compressed
, ULONG compressed_size
, BOOL check_all
)
1525 if (compressed_size
<= sizeof(WORD
))
1528 while (compressed_size
>= sizeof(WORD
))
1530 chunk_size
= (*(WORD
*)compressed
& 0xFFF) + 1;
1531 if (compressed_size
< sizeof(WORD
) + chunk_size
)
1535 compressed
+= sizeof(WORD
) + chunk_size
;
1536 compressed_size
-= sizeof(WORD
) + chunk_size
;
1542 #define DECOMPRESS_BROKEN_FRAGMENT 1 /* < Win 7 */
1543 #define DECOMPRESS_BROKEN_TRUNCATED 2 /* broken on all machines */
1545 static void test_RtlDecompressBuffer(void)
1549 UCHAR compressed
[32];
1550 ULONG compressed_size
;
1552 UCHAR uncompressed
[32];
1553 ULONG uncompressed_size
;
1558 /* 4 byte uncompressed chunk */
1560 {0x03, 0x30, 'W', 'i', 'n', 'e'},
1565 DECOMPRESS_BROKEN_FRAGMENT
1567 /* 8 byte uncompressed chunk */
1569 {0x07, 0x30, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
1574 DECOMPRESS_BROKEN_FRAGMENT
1576 /* 4 byte compressed chunk */
1578 {0x04, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
1584 /* 8 byte compressed chunk */
1586 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
1592 /* compressed chunk using backwards reference */
1594 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x01, 0x30},
1599 DECOMPRESS_BROKEN_TRUNCATED
1601 /* compressed chunk using backwards reference with length > bytes_read */
1603 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x30},
1608 DECOMPRESS_BROKEN_TRUNCATED
1610 /* same as above, but unused bits != 0 */
1612 {0x06, 0xB0, 0x30, 'W', 'i', 'n', 'e', 0x01, 0x30},
1617 DECOMPRESS_BROKEN_TRUNCATED
1619 /* compressed chunk without backwards reference and unused bits != 0 */
1621 {0x01, 0xB0, 0x02, 'W'},
1627 /* termination sequence after first chunk */
1629 {0x03, 0x30, 'W', 'i', 'n', 'e', 0x00, 0x00, 0x03, 0x30, 'W', 'i', 'n', 'e'},
1634 DECOMPRESS_BROKEN_FRAGMENT
1636 /* compressed chunk using backwards reference with 4 bit offset, 12 bit length */
1638 {0x14, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
1639 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
1643 "ABCDEFGHIJKLMNOPABCD",
1645 DECOMPRESS_BROKEN_TRUNCATED
1647 /* compressed chunk using backwards reference with 5 bit offset, 11 bit length */
1649 {0x15, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
1650 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
1651 0x02, 'A', 0x00, 0x78},
1654 "ABCDEFGHIJKLMNOPABCD",
1656 DECOMPRESS_BROKEN_TRUNCATED
1658 /* uncompressed chunk with invalid magic */
1660 {0x03, 0x20, 'W', 'i', 'n', 'e'},
1665 DECOMPRESS_BROKEN_FRAGMENT
1667 /* compressed chunk with invalid magic */
1669 {0x04, 0xA0, 0x00, 'W', 'i', 'n', 'e'},
1675 /* garbage byte after end of buffer */
1677 {0x00, 0xB0, 0x02, 0x01},
1683 /* empty compressed chunk */
1691 /* empty compressed chunk with unused bits != 0 */
1699 /* empty input buffer */
1703 STATUS_BAD_COMPRESSION_BUFFER
,
1705 /* incomplete chunk header */
1709 STATUS_BAD_COMPRESSION_BUFFER
1711 /* incomplete chunk header */
1715 STATUS_BAD_COMPRESSION_BUFFER
1717 /* compressed chunk with invalid backwards reference */
1719 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x40},
1721 STATUS_BAD_COMPRESSION_BUFFER
1723 /* compressed chunk with incomplete backwards reference */
1725 {0x05, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05},
1727 STATUS_BAD_COMPRESSION_BUFFER
1729 /* incomplete uncompressed chunk */
1731 {0x07, 0x30, 'W', 'i', 'n', 'e'},
1733 STATUS_BAD_COMPRESSION_BUFFER
1735 /* incomplete compressed chunk */
1737 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
1739 STATUS_BAD_COMPRESSION_BUFFER
1741 /* two compressed chunks, the second one incomplete */
1743 {0x00, 0xB0, 0x02, 0x00, 0xB0},
1745 STATUS_BAD_COMPRESSION_BUFFER
,
1749 static UCHAR buf
[0x2000], workspace
[0x1000];
1750 NTSTATUS status
, expected_status
;
1754 if (!pRtlDecompressBuffer
|| !pRtlDecompressFragment
)
1756 win_skip("RtlDecompressBuffer or RtlDecompressFragment is not available\n");
1760 /* test compression format / engine */
1761 final_size
= 0xdeadbeef;
1762 status
= pRtlDecompressBuffer(COMPRESSION_FORMAT_NONE
, buf
, sizeof(buf
), test_lznt
[0].compressed
,
1763 test_lznt
[0].compressed_size
, &final_size
);
1764 ok(status
== STATUS_INVALID_PARAMETER
, "got wrong status 0x%08x\n", status
);
1765 ok(final_size
== 0xdeadbeef, "got wrong final_size %u\n", final_size
);
1767 final_size
= 0xdeadbeef;
1768 status
= pRtlDecompressBuffer(COMPRESSION_FORMAT_DEFAULT
, 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(0xFF, buf
, sizeof(buf
), test_lznt
[0].compressed
,
1775 test_lznt
[0].compressed_size
, &final_size
);
1776 ok(status
== STATUS_UNSUPPORTED_COMPRESSION
, "got wrong status 0x%08x\n", status
);
1777 ok(final_size
== 0xdeadbeef, "got wrong final_size %u\n", final_size
);
1779 /* regular tests for RtlDecompressBuffer */
1780 for (i
= 0; i
< sizeof(test_lznt
) / sizeof(test_lznt
[0]); i
++)
1782 trace("Running test %d (compressed_size=%u, uncompressed_size=%u, status=0x%08x)\n",
1783 i
, test_lznt
[i
].compressed_size
, test_lznt
[i
].uncompressed_size
, test_lznt
[i
].status
);
1785 /* test with very big buffer */
1786 final_size
= 0xdeadbeef;
1787 memset(buf
, 0x11, sizeof(buf
));
1788 status
= pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1
, buf
, sizeof(buf
), test_lznt
[i
].compressed
,
1789 test_lznt
[i
].compressed_size
, &final_size
);
1790 ok(status
== test_lznt
[i
].status
|| broken(status
== STATUS_BAD_COMPRESSION_BUFFER
&&
1791 (test_lznt
[i
].broken_flags
& DECOMPRESS_BROKEN_FRAGMENT
)), "%d: got wrong status 0x%08x\n", i
, status
);
1794 ok(final_size
== test_lznt
[i
].uncompressed_size
,
1795 "%d: got wrong final_size %u\n", i
, final_size
);
1796 ok(!memcmp(buf
, test_lznt
[i
].uncompressed
, test_lznt
[i
].uncompressed_size
),
1797 "%d: got wrong decoded data\n", i
);
1798 ok(buf
[test_lznt
[i
].uncompressed_size
] == 0x11,
1799 "%d: buf[%u] was modified\n", i
, test_lznt
[i
].uncompressed_size
);
1802 /* test that modifier for compression engine is ignored */
1803 final_size
= 0xdeadbeef;
1804 memset(buf
, 0x11, sizeof(buf
));
1805 status
= pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1
| COMPRESSION_ENGINE_MAXIMUM
, buf
, sizeof(buf
),
1806 test_lznt
[i
].compressed
, test_lznt
[i
].compressed_size
, &final_size
);
1807 ok(status
== test_lznt
[i
].status
|| broken(status
== STATUS_BAD_COMPRESSION_BUFFER
&&
1808 (test_lznt
[i
].broken_flags
& DECOMPRESS_BROKEN_FRAGMENT
)), "%d: got wrong status 0x%08x\n", i
, status
);
1811 ok(final_size
== test_lznt
[i
].uncompressed_size
,
1812 "%d: got wrong final_size %u\n", i
, final_size
);
1813 ok(!memcmp(buf
, test_lznt
[i
].uncompressed
, test_lznt
[i
].uncompressed_size
),
1814 "%d: got wrong decoded data\n", i
);
1815 ok(buf
[test_lznt
[i
].uncompressed_size
] == 0x11,
1816 "%d: buf[%u] was modified\n", i
, test_lznt
[i
].uncompressed_size
);
1819 /* test with expected output size */
1820 if (test_lznt
[i
].uncompressed_size
> 0)
1822 final_size
= 0xdeadbeef;
1823 memset(buf
, 0x11, sizeof(buf
));
1824 status
= pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1
, buf
, test_lznt
[i
].uncompressed_size
,
1825 test_lznt
[i
].compressed
, test_lznt
[i
].compressed_size
, &final_size
);
1826 ok(status
== test_lznt
[i
].status
, "%d: got wrong status 0x%08x\n", i
, status
);
1829 ok(final_size
== test_lznt
[i
].uncompressed_size
,
1830 "%d: got wrong final_size %u\n", i
, final_size
);
1831 ok(!memcmp(buf
, test_lznt
[i
].uncompressed
, test_lznt
[i
].uncompressed_size
),
1832 "%d: got wrong decoded data\n", i
);
1833 ok(buf
[test_lznt
[i
].uncompressed_size
] == 0x11,
1834 "%d: buf[%u] was modified\n", i
, test_lznt
[i
].uncompressed_size
);
1838 /* test with smaller output size */
1839 if (test_lznt
[i
].uncompressed_size
> 1)
1841 final_size
= 0xdeadbeef;
1842 memset(buf
, 0x11, sizeof(buf
));
1843 status
= pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1
, buf
, test_lznt
[i
].uncompressed_size
- 1,
1844 test_lznt
[i
].compressed
, test_lznt
[i
].compressed_size
, &final_size
);
1845 if (test_lznt
[i
].broken_flags
& DECOMPRESS_BROKEN_TRUNCATED
)
1847 ok(status
== STATUS_BAD_COMPRESSION_BUFFER
, "%d: got wrong status 0x%08x\n", i
, status
);
1849 ok(status
== test_lznt
[i
].status
, "%d: got wrong status 0x%08x\n", i
, status
);
1852 ok(final_size
== test_lznt
[i
].uncompressed_size
- 1,
1853 "%d: got wrong final_size %u\n", i
, final_size
);
1854 ok(!memcmp(buf
, test_lznt
[i
].uncompressed
, test_lznt
[i
].uncompressed_size
- 1),
1855 "%d: got wrong decoded data\n", i
);
1856 ok(buf
[test_lznt
[i
].uncompressed_size
- 1] == 0x11,
1857 "%d: buf[%u] was modified\n", i
, test_lznt
[i
].uncompressed_size
- 1);
1861 /* test with zero output size */
1862 final_size
= 0xdeadbeef;
1863 memset(buf
, 0x11, sizeof(buf
));
1864 status
= pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1
, buf
, 0, test_lznt
[i
].compressed
,
1865 test_lznt
[i
].compressed_size
, &final_size
);
1866 if (is_incomplete_chunk(test_lznt
[i
].compressed
, test_lznt
[i
].compressed_size
, FALSE
))
1867 ok(status
== STATUS_BAD_COMPRESSION_BUFFER
, "%d: got wrong status 0x%08x\n", i
, status
);
1870 ok(status
== STATUS_SUCCESS
, "%d: got wrong status 0x%08x\n", i
, status
);
1871 ok(final_size
== 0, "%d: got wrong final_size %u\n", i
, final_size
);
1872 ok(buf
[0] == 0x11, "%d: buf[0] was modified\n", i
);
1875 /* test RtlDecompressFragment with offset = 0 */
1876 final_size
= 0xdeadbeef;
1877 memset(buf
, 0x11, sizeof(buf
));
1878 status
= pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1
, buf
, sizeof(buf
), test_lznt
[i
].compressed
,
1879 test_lznt
[i
].compressed_size
, 0, &final_size
, workspace
);
1880 if (test_lznt
[i
].broken_flags
& DECOMPRESS_BROKEN_FRAGMENT
)
1882 ok(status
== STATUS_BAD_COMPRESSION_BUFFER
, "%d: got wrong status 0x%08x\n", i
, status
);
1884 ok(status
== test_lznt
[i
].status
, "%d: got wrong status 0x%08x\n", i
, status
);
1887 ok(final_size
== test_lznt
[i
].uncompressed_size
,
1888 "%d: got wrong final_size %u\n", i
, final_size
);
1889 ok(!memcmp(buf
, test_lznt
[i
].uncompressed
, test_lznt
[i
].uncompressed_size
),
1890 "%d: got wrong decoded data\n", i
);
1891 ok(buf
[test_lznt
[i
].uncompressed_size
] == 0x11,
1892 "%d: buf[%u] was modified\n", i
, test_lznt
[i
].uncompressed_size
);
1895 /* test RtlDecompressFragment with offset = 1 */
1896 final_size
= 0xdeadbeef;
1897 memset(buf
, 0x11, sizeof(buf
));
1898 status
= pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1
, buf
, sizeof(buf
), test_lznt
[i
].compressed
,
1899 test_lznt
[i
].compressed_size
, 1, &final_size
, workspace
);
1900 if (test_lznt
[i
].broken_flags
& DECOMPRESS_BROKEN_FRAGMENT
)
1902 ok(status
== STATUS_BAD_COMPRESSION_BUFFER
, "%d: got wrong status 0x%08x\n", i
, status
);
1904 ok(status
== test_lznt
[i
].status
, "%d: got wrong status 0x%08x\n", i
, status
);
1907 if (test_lznt
[i
].uncompressed_size
== 0)
1910 ok(final_size
== 4095, "%d: got wrong final_size %u\n", i
, final_size
);
1911 /* Buffer doesn't contain any useful value on Windows */
1912 ok(buf
[4095] == 0x11, "%d: buf[4095] was modified\n", i
);
1916 ok(final_size
== test_lznt
[i
].uncompressed_size
- 1,
1917 "%d: got wrong final_size %u\n", i
, final_size
);
1918 ok(!memcmp(buf
, test_lznt
[i
].uncompressed
+ 1, test_lznt
[i
].uncompressed_size
- 1),
1919 "%d: got wrong decoded data\n", i
);
1920 ok(buf
[test_lznt
[i
].uncompressed_size
- 1] == 0x11,
1921 "%d: buf[%u] was modified\n", i
, test_lznt
[i
].uncompressed_size
- 1);
1925 /* test RtlDecompressFragment with offset = 4095 */
1926 final_size
= 0xdeadbeef;
1927 memset(buf
, 0x11, sizeof(buf
));
1928 status
= pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1
, buf
, sizeof(buf
), test_lznt
[i
].compressed
,
1929 test_lznt
[i
].compressed_size
, 4095, &final_size
, workspace
);
1930 if (test_lznt
[i
].broken_flags
& DECOMPRESS_BROKEN_FRAGMENT
)
1932 ok(status
== STATUS_BAD_COMPRESSION_BUFFER
, "%d: got wrong status 0x%08x\n", i
, status
);
1934 ok(status
== test_lznt
[i
].status
, "%d: got wrong status 0x%08x\n", i
, status
);
1938 ok(final_size
== 1, "%d: got wrong final_size %u\n", i
, final_size
);
1940 ok(buf
[0] == 0, "%d: padding is not zero\n", i
);
1941 ok(buf
[1] == 0x11, "%d: buf[1] was modified\n", i
);
1944 /* test RtlDecompressFragment with offset = 4096 */
1945 final_size
= 0xdeadbeef;
1946 memset(buf
, 0x11, sizeof(buf
));
1947 status
= pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1
, buf
, sizeof(buf
), test_lznt
[i
].compressed
,
1948 test_lznt
[i
].compressed_size
, 4096, &final_size
, workspace
);
1949 expected_status
= is_incomplete_chunk(test_lznt
[i
].compressed
, test_lznt
[i
].compressed_size
, TRUE
) ?
1950 test_lznt
[i
].status
: STATUS_SUCCESS
;
1951 ok(status
== expected_status
, "%d: got wrong status 0x%08x, expected 0x%08x\n", i
, status
, expected_status
);
1954 ok(final_size
== 0, "%d: got wrong final_size %u\n", i
, final_size
);
1955 ok(buf
[0] == 0x11, "%d: buf[4096] was modified\n", i
);
1960 #undef DECOMPRESS_BROKEN_FRAGMENT
1961 #undef DECOMPRESS_BROKEN_TRUNCATED
1963 struct critsect_locked_info
1965 CRITICAL_SECTION crit
;
1966 HANDLE semaphores
[2];
1969 static DWORD WINAPI
critsect_locked_thread(void *param
)
1971 struct critsect_locked_info
*info
= param
;
1974 ret
= pRtlIsCriticalSectionLocked(&info
->crit
);
1975 ok(ret
== TRUE
, "expected TRUE, got %u\n", ret
);
1976 ret
= pRtlIsCriticalSectionLockedByThread(&info
->crit
);
1977 ok(ret
== FALSE
, "expected FALSE, got %u\n", ret
);
1979 ReleaseSemaphore(info
->semaphores
[0], 1, NULL
);
1980 ret
= WaitForSingleObject(info
->semaphores
[1], 1000);
1981 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %u\n", ret
);
1983 ret
= pRtlIsCriticalSectionLocked(&info
->crit
);
1984 ok(ret
== FALSE
, "expected FALSE, got %u\n", ret
);
1985 ret
= pRtlIsCriticalSectionLockedByThread(&info
->crit
);
1986 ok(ret
== FALSE
, "expected FALSE, got %u\n", ret
);
1988 EnterCriticalSection(&info
->crit
);
1990 ret
= pRtlIsCriticalSectionLocked(&info
->crit
);
1991 ok(ret
== TRUE
, "expected TRUE, got %u\n", ret
);
1992 ret
= pRtlIsCriticalSectionLockedByThread(&info
->crit
);
1993 ok(ret
== TRUE
, "expected TRUE, got %u\n", ret
);
1995 ReleaseSemaphore(info
->semaphores
[0], 1, NULL
);
1996 ret
= WaitForSingleObject(info
->semaphores
[1], 1000);
1997 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %u\n", ret
);
1999 LeaveCriticalSection(&info
->crit
);
2003 static void test_RtlIsCriticalSectionLocked(void)
2005 struct critsect_locked_info info
;
2009 if (!pRtlIsCriticalSectionLocked
|| !pRtlIsCriticalSectionLockedByThread
)
2011 win_skip("skipping RtlIsCriticalSectionLocked tests, required functions not available\n");
2015 InitializeCriticalSection(&info
.crit
);
2016 info
.semaphores
[0] = CreateSemaphoreW(NULL
, 0, 1, NULL
);
2017 ok(info
.semaphores
[0] != NULL
, "CreateSemaphore failed with %u\n", GetLastError());
2018 info
.semaphores
[1] = CreateSemaphoreW(NULL
, 0, 1, NULL
);
2019 ok(info
.semaphores
[1] != NULL
, "CreateSemaphore failed with %u\n", GetLastError());
2021 ret
= pRtlIsCriticalSectionLocked(&info
.crit
);
2022 ok(ret
== FALSE
, "expected FALSE, got %u\n", ret
);
2023 ret
= pRtlIsCriticalSectionLockedByThread(&info
.crit
);
2024 ok(ret
== FALSE
, "expected FALSE, got %u\n", ret
);
2026 EnterCriticalSection(&info
.crit
);
2028 ret
= pRtlIsCriticalSectionLocked(&info
.crit
);
2029 ok(ret
== TRUE
, "expected TRUE, got %u\n", ret
);
2030 ret
= pRtlIsCriticalSectionLockedByThread(&info
.crit
);
2031 ok(ret
== TRUE
, "expected TRUE, got %u\n", ret
);
2033 thread
= CreateThread(NULL
, 0, critsect_locked_thread
, &info
, 0, NULL
);
2034 ok(thread
!= NULL
, "CreateThread failed with %u\n", GetLastError());
2035 ret
= WaitForSingleObject(info
.semaphores
[0], 1000);
2036 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %u\n", ret
);
2038 LeaveCriticalSection(&info
.crit
);
2040 ReleaseSemaphore(info
.semaphores
[1], 1, NULL
);
2041 ret
= WaitForSingleObject(info
.semaphores
[0], 1000);
2042 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %u\n", ret
);
2044 ret
= pRtlIsCriticalSectionLocked(&info
.crit
);
2045 ok(ret
== TRUE
, "expected TRUE, got %u\n", ret
);
2046 ret
= pRtlIsCriticalSectionLockedByThread(&info
.crit
);
2047 ok(ret
== FALSE
, "expected FALSE, got %u\n", ret
);
2049 ReleaseSemaphore(info
.semaphores
[1], 1, NULL
);
2050 ret
= WaitForSingleObject(thread
, 1000);
2051 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %u\n", ret
);
2053 CloseHandle(thread
);
2054 CloseHandle(info
.semaphores
[0]);
2055 CloseHandle(info
.semaphores
[1]);
2056 DeleteCriticalSection(&info
.crit
);
2063 test_RtlCompareMemory();
2064 test_RtlCompareMemoryUlong();
2065 test_RtlMoveMemory();
2066 test_RtlFillMemory();
2067 test_RtlFillMemoryUlong();
2068 test_RtlZeroMemory();
2069 test_RtlUlonglongByteSwap();
2072 test_RtlAreAllAccessesGranted();
2073 test_RtlAreAnyAccessesGranted();
2074 test_RtlComputeCrc32();
2075 test_HandleTables();
2076 test_RtlAllocateAndInitializeSid();
2077 test_RtlDeleteTimer();
2078 test_RtlThreadErrorMode();
2079 test_LdrProcessRelocationBlock();
2080 test_RtlIpv4AddressToString();
2081 test_RtlIpv4AddressToStringEx();
2082 test_RtlIpv4StringToAddress();
2083 test_LdrAddRefDll();
2084 test_LdrLockLoaderLock();
2085 test_RtlCompressBuffer();
2086 test_RtlGetCompressionWorkSpaceSize();
2087 test_RtlDecompressBuffer();
2088 test_RtlIsCriticalSectionLocked();