[NTDLL_WINETEST]
[reactos.git] / rostests / winetests / ntdll / rtl.c
1 /* Unit test suite for Rtl* API functions
2 *
3 * Copyright 2003 Thomas Mertes
4 *
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.
9 *
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.
14 *
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
18 *
19 * NOTES
20 * We use function pointers here as there is no import library for NTDLL on
21 * windows.
22 */
23
24 #include <stdlib.h>
25
26 #include "ntdll_test.h"
27 #include "inaddr.h"
28
29 #ifndef __WINE_WINTERNL_H
30
31 typedef struct _RTL_HANDLE
32 {
33 struct _RTL_HANDLE * Next;
34 } RTL_HANDLE;
35
36 typedef struct _RTL_HANDLE_TABLE
37 {
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;
46
47 #endif
48
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)
55 {
56 return (s >> 8) | (s << 8);
57 }
58 #define htons(s) __my_ushort_swap(s)
59 #endif /* WORDS_BIGENDIAN */
60
61
62
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
94 static HMODULE hkernel32 = 0;
95 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
96
97
98 #define LEN 16
99 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
100 static ULONG src_aligned_block[4];
101 static ULONG dest_aligned_block[32];
102 static const char *src = (const char*)src_aligned_block;
103 static char* dest = (char*)dest_aligned_block;
104
105 static void InitFunctionPtrs(void)
106 {
107 hntdll = LoadLibraryA("ntdll.dll");
108 ok(hntdll != 0, "LoadLibrary failed\n");
109 if (hntdll) {
110 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
111 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
112 pRtlDeleteTimer = (void *)GetProcAddress(hntdll, "RtlDeleteTimer");
113 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
114 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
115 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
116 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
117 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
118 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
119 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
120 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
121 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
122 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
123 pRtlInitializeHandleTable = (void *)GetProcAddress(hntdll, "RtlInitializeHandleTable");
124 pRtlIsValidIndexHandle = (void *)GetProcAddress(hntdll, "RtlIsValidIndexHandle");
125 pRtlDestroyHandleTable = (void *)GetProcAddress(hntdll, "RtlDestroyHandleTable");
126 pRtlAllocateHandle = (void *)GetProcAddress(hntdll, "RtlAllocateHandle");
127 pRtlFreeHandle = (void *)GetProcAddress(hntdll, "RtlFreeHandle");
128 pRtlAllocateAndInitializeSid = (void *)GetProcAddress(hntdll, "RtlAllocateAndInitializeSid");
129 pRtlFreeSid = (void *)GetProcAddress(hntdll, "RtlFreeSid");
130 pNtCurrentTeb = (void *)GetProcAddress(hntdll, "NtCurrentTeb");
131 pRtlGetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlGetThreadErrorMode");
132 pRtlSetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlSetThreadErrorMode");
133 pLdrProcessRelocationBlock = (void *)GetProcAddress(hntdll, "LdrProcessRelocationBlock");
134 pRtlIpv4AddressToStringA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringA");
135 pRtlIpv4AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringExA");
136 pRtlIpv4StringToAddressA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressA");
137 pLdrAddRefDll = (void *)GetProcAddress(hntdll, "LdrAddRefDll");
138 }
139 hkernel32 = LoadLibraryA("kernel32.dll");
140 ok(hkernel32 != 0, "LoadLibrary failed\n");
141 if (hkernel32) {
142 pIsWow64Process = (void *)GetProcAddress(hkernel32, "IsWow64Process");
143 }
144 strcpy((char*)src_aligned_block, src_src);
145 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
146 }
147
148 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
149 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
150
151 static void test_RtlCompareMemory(void)
152 {
153 SIZE_T size;
154
155 if (!pRtlCompareMemory)
156 {
157 win_skip("RtlCompareMemory is not available\n");
158 return;
159 }
160
161 strcpy(dest, src);
162
163 COMP(src,src,0,0);
164 COMP(src,src,LEN,LEN);
165 dest[0] = 'x';
166 COMP(src,dest,LEN,0);
167 }
168
169 static void test_RtlCompareMemoryUlong(void)
170 {
171 ULONG a[10];
172 ULONG result;
173
174 if (!pRtlCompareMemoryUlong)
175 {
176 win_skip("RtlCompareMemoryUlong is not available\n");
177 return;
178 }
179
180 a[0]= 0x0123;
181 a[1]= 0x4567;
182 a[2]= 0x89ab;
183 a[3]= 0xcdef;
184 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
185 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %u, expected 0\n", a, result);
186 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
187 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
188 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
189 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
190 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
191 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
192 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
193 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
194 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
195 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 4\n", a, result);
196 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
197 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 4\n", a, result);
198 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
199 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %u, expected 0\n", a, result);
200 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
201 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %u, expected 0\n", a, result);
202 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
203 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %u, expected 0\n", a, result);
204
205 a[1]= 0x0123;
206 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
207 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
208 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
209 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
210 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
211 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
212 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
213 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
214 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
215 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 8\n", a, result);
216 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
217 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 8\n", a, result);
218 }
219
220 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
221 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
222
223 static void test_RtlMoveMemory(void)
224 {
225 if (!pRtlMoveMemory)
226 {
227 win_skip("RtlMoveMemory is not available\n");
228 return;
229 }
230
231 /* Length should be in bytes and not rounded. Use strcmp to ensure we
232 * didn't write past the end (it checks for the final NUL left by memset)
233 */
234 COPY(0); CMP("");
235 COPY(1); CMP("T");
236 COPY(2); CMP("Th");
237 COPY(3); CMP("Thi");
238 COPY(4); CMP("This");
239 COPY(5); CMP("This ");
240 COPY(6); CMP("This i");
241 COPY(7); CMP("This is");
242 COPY(8); CMP("This is ");
243 COPY(9); CMP("This is a");
244
245 /* Overlapping */
246 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
247 CMP("his is a test!!");
248 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
249 CMP("TThis is a test!");
250 }
251
252 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
253
254 static void test_RtlFillMemory(void)
255 {
256 if (!pRtlFillMemory)
257 {
258 win_skip("RtlFillMemory is not available\n");
259 return;
260 }
261
262 /* Length should be in bytes and not rounded. Use strcmp to ensure we
263 * didn't write past the end (the remainder of the string should match)
264 */
265 FILL(0); CMP("This is a test!");
266 FILL(1); CMP("xhis is a test!");
267 FILL(2); CMP("xxis is a test!");
268 FILL(3); CMP("xxxs is a test!");
269 FILL(4); CMP("xxxx is a test!");
270 FILL(5); CMP("xxxxxis a test!");
271 FILL(6); CMP("xxxxxxs a test!");
272 FILL(7); CMP("xxxxxxx a test!");
273 FILL(8); CMP("xxxxxxxxa test!");
274 FILL(9); CMP("xxxxxxxxx test!");
275 }
276
277 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
278
279 static void test_RtlFillMemoryUlong(void)
280 {
281 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
282 if (!pRtlFillMemoryUlong)
283 {
284 win_skip("RtlFillMemoryUlong is not available\n");
285 return;
286 }
287
288 /* Length should be in bytes and not rounded. Use strcmp to ensure we
289 * didn't write past the end (the remainder of the string should match)
290 */
291 LFILL(0); CMP("This is a test!");
292 LFILL(1); CMP("This is a test!");
293 LFILL(2); CMP("This is a test!");
294 LFILL(3); CMP("This is a test!");
295 LFILL(4); CMP("xxxx is a test!");
296 LFILL(5); CMP("xxxx is a test!");
297 LFILL(6); CMP("xxxx is a test!");
298 LFILL(7); CMP("xxxx is a test!");
299 LFILL(8); CMP("xxxxxxxxa test!");
300 LFILL(9); CMP("xxxxxxxxa test!");
301 }
302
303 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
304 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
305
306 static void test_RtlZeroMemory(void)
307 {
308 if (!pRtlZeroMemory)
309 {
310 win_skip("RtlZeroMemory is not available\n");
311 return;
312 }
313
314 /* Length should be in bytes and not rounded. */
315 ZERO(0); MCMP("This is a test!");
316 ZERO(1); MCMP("\0his is a test!");
317 ZERO(2); MCMP("\0\0is is a test!");
318 ZERO(3); MCMP("\0\0\0s is a test!");
319 ZERO(4); MCMP("\0\0\0\0 is a test!");
320 ZERO(5); MCMP("\0\0\0\0\0is a test!");
321 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
322 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
323 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
324 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
325 }
326
327 static void test_RtlUlonglongByteSwap(void)
328 {
329 ULONGLONG result;
330
331 if ( !pRtlUlonglongByteSwap )
332 {
333 win_skip("RtlUlonglongByteSwap is not available\n");
334 return;
335 }
336
337 if ( pRtlUlonglongByteSwap( 0 ) != 0 )
338 {
339 win_skip("Broken RtlUlonglongByteSwap in win2k\n");
340 return;
341 }
342
343 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
344 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
345 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%x%08x, expected 0x2143658710325476\n",
346 (DWORD)(result >> 32), (DWORD)result);
347 }
348
349
350 static void test_RtlUniform(void)
351 {
352 ULONGLONG num;
353 ULONG seed;
354 ULONG seed_bak;
355 ULONG expected;
356 ULONG result;
357
358 if (!pRtlUniform)
359 {
360 win_skip("RtlUniform is not available\n");
361 return;
362 }
363
364 /*
365 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
366 * algorithm. This algorithm is:
367 *
368 * seed = (seed * const_1 + const_2) % const_3;
369 *
370 * According to the documentation the random number is distributed over
371 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
372 *
373 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
374 *
375 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
376 * algorithm can be expressed without division as:
377 *
378 * seed = (seed * const_1 + const_2) & MAXLONG;
379 *
380 * To find out const_2 we just call RtlUniform with seed set to 0:
381 */
382 seed = 0;
383 expected = 0x7fffffc3;
384 result = pRtlUniform(&seed);
385 ok(result == expected,
386 "RtlUniform(&seed (seed == 0)) returns %x, expected %x\n",
387 result, expected);
388 /*
389 * The algorithm is now:
390 *
391 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
392 *
393 * To find out const_1 we can use:
394 *
395 * const_1 = RtlUniform(1) - 0x7fffffc3;
396 *
397 * If that does not work a search loop can try all possible values of
398 * const_1 and compare to the result to RtlUniform(1).
399 * This way we find out that const_1 is 0xffffffed.
400 *
401 * For seed = 1 the const_2 is 0x7fffffc4:
402 */
403 seed = 1;
404 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
405 result = pRtlUniform(&seed);
406 ok(result == expected,
407 "RtlUniform(&seed (seed == 1)) returns %x, expected %x\n",
408 result, expected);
409 /*
410 * For seed = 2 the const_2 is 0x7fffffc3:
411 */
412 seed = 2;
413 expected = seed * 0xffffffed + 0x7fffffc3;
414 result = pRtlUniform(&seed);
415
416 /*
417 * Windows Vista uses different algorithms, so skip the rest of the tests
418 * until that is figured out. Trace output for the failures is about 10.5 MB!
419 */
420
421 if (result == 0x7fffff9f) {
422 skip("Most likely running on Windows Vista which uses a different algorithm\n");
423 return;
424 }
425
426 ok(result == expected,
427 "RtlUniform(&seed (seed == 2)) returns %x, expected %x\n",
428 result, expected);
429
430 /*
431 * More tests show that if seed is odd the result must be incremented by 1:
432 */
433 seed = 3;
434 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
435 result = pRtlUniform(&seed);
436 ok(result == expected,
437 "RtlUniform(&seed (seed == 3)) returns %x, expected %x\n",
438 result, expected);
439
440 seed = 0x6bca1aa;
441 expected = seed * 0xffffffed + 0x7fffffc3;
442 result = pRtlUniform(&seed);
443 ok(result == expected,
444 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %x, expected %x\n",
445 result, expected);
446
447 seed = 0x6bca1ab;
448 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
449 result = pRtlUniform(&seed);
450 ok(result == expected,
451 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %x, expected %x\n",
452 result, expected);
453 /*
454 * When seed is 0x6bca1ac there is an exception:
455 */
456 seed = 0x6bca1ac;
457 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
458 result = pRtlUniform(&seed);
459 ok(result == expected,
460 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %x, expected %x\n",
461 result, expected);
462 /*
463 * Note that up to here const_3 is not used
464 * (the highest bit of the result is not set).
465 *
466 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
467 */
468 seed = 0x6bca1ad;
469 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
470 result = pRtlUniform(&seed);
471 ok(result == expected,
472 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %x, expected %x\n",
473 result, expected);
474
475 seed = 0x6bca1ae;
476 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
477 result = pRtlUniform(&seed);
478 ok(result == expected,
479 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %x, expected %x\n",
480 result, expected);
481 /*
482 * There are several ranges where for odd or even seed the result must be
483 * incremented by 1. You can see this ranges in the following test.
484 *
485 * For a full test use one of the following loop heads:
486 *
487 * for (num = 0; num <= 0xffffffff; num++) {
488 * seed = num;
489 * ...
490 *
491 * seed = 0;
492 * for (num = 0; num <= 0xffffffff; num++) {
493 * ...
494 */
495 seed = 0;
496 for (num = 0; num <= 100000; num++) {
497
498 expected = seed * 0xffffffed + 0x7fffffc3;
499 if (seed < 0x6bca1ac) {
500 expected = expected + (seed & 1);
501 } else if (seed == 0x6bca1ac) {
502 expected = (expected + 2) & MAXLONG;
503 } else if (seed < 0xd79435c) {
504 expected = (expected + (~seed & 1)) & MAXLONG;
505 } else if (seed < 0x1435e50b) {
506 expected = expected + (seed & 1);
507 } else if (seed < 0x1af286ba) {
508 expected = (expected + (~seed & 1)) & MAXLONG;
509 } else if (seed < 0x21af2869) {
510 expected = expected + (seed & 1);
511 } else if (seed < 0x286bca18) {
512 expected = (expected + (~seed & 1)) & MAXLONG;
513 } else if (seed < 0x2f286bc7) {
514 expected = expected + (seed & 1);
515 } else if (seed < 0x35e50d77) {
516 expected = (expected + (~seed & 1)) & MAXLONG;
517 } else if (seed < 0x3ca1af26) {
518 expected = expected + (seed & 1);
519 } else if (seed < 0x435e50d5) {
520 expected = (expected + (~seed & 1)) & MAXLONG;
521 } else if (seed < 0x4a1af284) {
522 expected = expected + (seed & 1);
523 } else if (seed < 0x50d79433) {
524 expected = (expected + (~seed & 1)) & MAXLONG;
525 } else if (seed < 0x579435e2) {
526 expected = expected + (seed & 1);
527 } else if (seed < 0x5e50d792) {
528 expected = (expected + (~seed & 1)) & MAXLONG;
529 } else if (seed < 0x650d7941) {
530 expected = expected + (seed & 1);
531 } else if (seed < 0x6bca1af0) {
532 expected = (expected + (~seed & 1)) & MAXLONG;
533 } else if (seed < 0x7286bc9f) {
534 expected = expected + (seed & 1);
535 } else if (seed < 0x79435e4e) {
536 expected = (expected + (~seed & 1)) & MAXLONG;
537 } else if (seed < 0x7ffffffd) {
538 expected = expected + (seed & 1);
539 } else if (seed < 0x86bca1ac) {
540 expected = (expected + (~seed & 1)) & MAXLONG;
541 } else if (seed == 0x86bca1ac) {
542 expected = (expected + 1) & MAXLONG;
543 } else if (seed < 0x8d79435c) {
544 expected = expected + (seed & 1);
545 } else if (seed < 0x9435e50b) {
546 expected = (expected + (~seed & 1)) & MAXLONG;
547 } else if (seed < 0x9af286ba) {
548 expected = expected + (seed & 1);
549 } else if (seed < 0xa1af2869) {
550 expected = (expected + (~seed & 1)) & MAXLONG;
551 } else if (seed < 0xa86bca18) {
552 expected = expected + (seed & 1);
553 } else if (seed < 0xaf286bc7) {
554 expected = (expected + (~seed & 1)) & MAXLONG;
555 } else if (seed == 0xaf286bc7) {
556 expected = (expected + 2) & MAXLONG;
557 } else if (seed < 0xb5e50d77) {
558 expected = expected + (seed & 1);
559 } else if (seed < 0xbca1af26) {
560 expected = (expected + (~seed & 1)) & MAXLONG;
561 } else if (seed < 0xc35e50d5) {
562 expected = expected + (seed & 1);
563 } else if (seed < 0xca1af284) {
564 expected = (expected + (~seed & 1)) & MAXLONG;
565 } else if (seed < 0xd0d79433) {
566 expected = expected + (seed & 1);
567 } else if (seed < 0xd79435e2) {
568 expected = (expected + (~seed & 1)) & MAXLONG;
569 } else if (seed < 0xde50d792) {
570 expected = expected + (seed & 1);
571 } else if (seed < 0xe50d7941) {
572 expected = (expected + (~seed & 1)) & MAXLONG;
573 } else if (seed < 0xebca1af0) {
574 expected = expected + (seed & 1);
575 } else if (seed < 0xf286bc9f) {
576 expected = (expected + (~seed & 1)) & MAXLONG;
577 } else if (seed < 0xf9435e4e) {
578 expected = expected + (seed & 1);
579 } else if (seed < 0xfffffffd) {
580 expected = (expected + (~seed & 1)) & MAXLONG;
581 } else {
582 expected = expected + (seed & 1);
583 } /* if */
584 seed_bak = seed;
585 result = pRtlUniform(&seed);
586 ok(result == expected,
587 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
588 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
589 ok(seed == expected,
590 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
591 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
592 } /* for */
593 /*
594 * Further investigation shows: In the different regions the highest bit
595 * is set or cleared when even or odd seeds need an increment by 1.
596 * This leads to a simplified algorithm:
597 *
598 * seed = seed * 0xffffffed + 0x7fffffc3;
599 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
600 * seed = (seed + 2) & MAXLONG;
601 * } else if (seed == 0x7fffffff) {
602 * seed = 0;
603 * } else if ((seed & 0x80000000) == 0) {
604 * seed = seed + (~seed & 1);
605 * } else {
606 * seed = (seed + (seed & 1)) & MAXLONG;
607 * }
608 *
609 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
610 *
611 * Now comes the funny part:
612 * It took me one weekend, to find the complicated algorithm and one day more,
613 * to find the simplified algorithm. Several weeks later I found out: The value
614 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
615 * nor with the simplified algorithm. In reality the native function and our
616 * function return a random number distributed over [0..MAXLONG-1]. Note
617 * that this is different from what native documentation states [0..MAXLONG].
618 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
619 *
620 * seed = (seed * const_1 + const_2) % MAXLONG;
621 *
622 * Further investigations show that the real algorithm is:
623 *
624 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
625 *
626 * This is checked with the test below:
627 */
628 seed = 0;
629 for (num = 0; num <= 100000; num++) {
630 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
631 seed_bak = seed;
632 result = pRtlUniform(&seed);
633 ok(result == expected,
634 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
635 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
636 ok(seed == expected,
637 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
638 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
639 } /* for */
640 /*
641 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
642 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
643 * that there is more than one cycle of generated randon numbers ...
644 */
645 }
646
647
648 static ULONG my_RtlRandom(PULONG seed)
649 {
650 static ULONG saved_value[128] =
651 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
652 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
653 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
654 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
655 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
656 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
657 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
658 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
659 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
660 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
661 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
662 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
663 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
664 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
665 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
666 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
667 ULONG rand;
668 int pos;
669 ULONG result;
670
671 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
672 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
673 pos = *seed & 0x7f;
674 result = saved_value[pos];
675 saved_value[pos] = rand;
676 return(result);
677 }
678
679
680 static void test_RtlRandom(void)
681 {
682 ULONGLONG num;
683 ULONG seed;
684 ULONG seed_bak;
685 ULONG seed_expected;
686 ULONG result;
687 ULONG result_expected;
688
689 if (!pRtlRandom)
690 {
691 win_skip("RtlRandom is not available\n");
692 return;
693 }
694
695 /*
696 * Unlike RtlUniform, RtlRandom is not documented. We guess that for
697 * RtlRandom D.H. Lehmer's 1948 algorithm is used like stated in
698 * the documentation of the RtlUniform function. This algorithm is:
699 *
700 * seed = (seed * const_1 + const_2) % const_3;
701 *
702 * According to the RtlUniform documentation the random number is
703 * distributed over [0..MAXLONG], but in reality it is distributed
704 * over [0..MAXLONG-1]. Therefore const_3 might be MAXLONG + 1 or
705 * MAXLONG:
706 *
707 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
708 *
709 * or
710 *
711 * seed = (seed * const_1 + const_2) % MAXLONG;
712 *
713 * To find out const_2 we just call RtlRandom with seed set to 0:
714 */
715 seed = 0;
716 result_expected = 0x320a1743;
717 seed_expected =0x44b;
718 result = pRtlRandom(&seed);
719
720 /*
721 * Windows Vista uses different algorithms, so skip the rest of the tests
722 * until that is figured out. Trace output for the failures is about 10.5 MB!
723 */
724
725 if (seed == 0x3fc) {
726 skip("Most likely running on Windows Vista which uses a different algorithm\n");
727 return;
728 }
729
730 ok(result == result_expected,
731 "pRtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
732 result, result_expected);
733 ok(seed == seed_expected,
734 "pRtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
735 seed, seed_expected);
736 /*
737 * Seed is not equal to result as with RtlUniform. To see more we
738 * call RtlRandom again with seed set to 0:
739 */
740 seed = 0;
741 result_expected = 0x7fffffc3;
742 seed_expected =0x44b;
743 result = pRtlRandom(&seed);
744 ok(result == result_expected,
745 "RtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
746 result, result_expected);
747 ok(seed == seed_expected,
748 "RtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
749 seed, seed_expected);
750 /*
751 * Seed is set to the same value as before but the result is different.
752 * To see more we call RtlRandom again with seed set to 0:
753 */
754 seed = 0;
755 result_expected = 0x7fffffc3;
756 seed_expected =0x44b;
757 result = pRtlRandom(&seed);
758 ok(result == result_expected,
759 "RtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
760 result, result_expected);
761 ok(seed == seed_expected,
762 "RtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
763 seed, seed_expected);
764 /*
765 * Seed is again set to the same value as before. This time we also
766 * have the same result as before. Interestingly the value of the
767 * result is 0x7fffffc3 which is the same value used in RtlUniform
768 * as const_2. If we do
769 *
770 * seed = 0;
771 * result = RtlUniform(&seed);
772 *
773 * we get the same result (0x7fffffc3) as with
774 *
775 * seed = 0;
776 * RtlRandom(&seed);
777 * seed = 0;
778 * result = RtlRandom(&seed);
779 *
780 * And there is another interesting thing. If we do
781 *
782 * seed = 0;
783 * RtlUniform(&seed);
784 * RtlUniform(&seed);
785 *
786 * seed is set to the value 0x44b which ist the same value that
787 *
788 * seed = 0;
789 * RtlRandom(&seed);
790 *
791 * assigns to seed. Putting these two findings together leads to
792 * the conclusion that RtlRandom saves the value in some variable,
793 * like in the following algorithm:
794 *
795 * result = saved_value;
796 * saved_value = RtlUniform(&seed);
797 * RtlUniform(&seed);
798 * return(result);
799 *
800 * Now we do further tests with seed set to 1:
801 */
802 seed = 1;
803 result_expected = 0x7a50bbc6;
804 seed_expected =0x5a1;
805 result = pRtlRandom(&seed);
806 ok(result == result_expected,
807 "RtlRandom(&seed (seed == 1)) returns %x, expected %x\n",
808 result, result_expected);
809 ok(seed == seed_expected,
810 "RtlRandom(&seed (seed == 1)) sets seed to %x, expected %x\n",
811 seed, seed_expected);
812 /*
813 * If there is just one saved_value the result now would be
814 * 0x7fffffc3. From this test we can see that there is more than
815 * one saved_value, like with this algorithm:
816 *
817 * result = saved_value[pos];
818 * saved_value[pos] = RtlUniform(&seed);
819 * RtlUniform(&seed);
820 * return(result);
821 *
822 * But how is the value of pos determined? The calls to RtlUniform
823 * create a sequence of random numbers. Every second random number
824 * is put into the saved_value array and is used in some later call
825 * of RtlRandom as result. The only reasonable source to determine
826 * pos are the random numbers generated by RtlUniform which are not
827 * put into the saved_value array. This are the values of seed
828 * between the two calls of RtlUniform as in this algorithm:
829 *
830 * rand = RtlUniform(&seed);
831 * RtlUniform(&seed);
832 * pos = position(seed);
833 * result = saved_value[pos];
834 * saved_value[pos] = rand;
835 * return(result);
836 *
837 * What remains to be determined is: The size of the saved_value array,
838 * the initial values of the saved_value array and the function
839 * position(seed). These tests are not shown here.
840 * The result of these tests is: The size of the saved_value array
841 * is 128, the initial values can be seen in the my_RtlRandom
842 * function and the position(seed) function is (seed & 0x7f).
843 *
844 * For a full test of RtlRandom use one of the following loop heads:
845 *
846 * for (num = 0; num <= 0xffffffff; num++) {
847 * seed = num;
848 * ...
849 *
850 * seed = 0;
851 * for (num = 0; num <= 0xffffffff; num++) {
852 * ...
853 */
854 seed = 0;
855 for (num = 0; num <= 100000; num++) {
856 seed_bak = seed;
857 seed_expected = seed;
858 result_expected = my_RtlRandom(&seed_expected);
859 /* The following corrections are necessary because the */
860 /* previous tests changed the saved_value array */
861 if (num == 0) {
862 result_expected = 0x7fffffc3;
863 } else if (num == 81) {
864 result_expected = 0x7fffffb1;
865 } /* if */
866 result = pRtlRandom(&seed);
867 ok(result == result_expected,
868 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
869 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, result_expected);
870 ok(seed == seed_expected,
871 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
872 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, seed_expected);
873 } /* for */
874 }
875
876
877 typedef struct {
878 ACCESS_MASK GrantedAccess;
879 ACCESS_MASK DesiredAccess;
880 BOOLEAN result;
881 } all_accesses_t;
882
883 static const all_accesses_t all_accesses[] = {
884 {0xFEDCBA76, 0xFEDCBA76, 1},
885 {0x00000000, 0xFEDCBA76, 0},
886 {0xFEDCBA76, 0x00000000, 1},
887 {0x00000000, 0x00000000, 1},
888 {0xFEDCBA76, 0xFEDCBA70, 1},
889 {0xFEDCBA70, 0xFEDCBA76, 0},
890 {0xFEDCBA76, 0xFEDC8A76, 1},
891 {0xFEDC8A76, 0xFEDCBA76, 0},
892 {0xFEDCBA76, 0xC8C4B242, 1},
893 {0xC8C4B242, 0xFEDCBA76, 0},
894 };
895 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
896
897
898 static void test_RtlAreAllAccessesGranted(void)
899 {
900 unsigned int test_num;
901 BOOLEAN result;
902
903 if (!pRtlAreAllAccessesGranted)
904 {
905 win_skip("RtlAreAllAccessesGranted is not available\n");
906 return;
907 }
908
909 for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
910 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
911 all_accesses[test_num].DesiredAccess);
912 ok(all_accesses[test_num].result == result,
913 "(test %d): RtlAreAllAccessesGranted(%08x, %08x) returns %d, expected %d\n",
914 test_num, all_accesses[test_num].GrantedAccess,
915 all_accesses[test_num].DesiredAccess,
916 result, all_accesses[test_num].result);
917 } /* for */
918 }
919
920
921 typedef struct {
922 ACCESS_MASK GrantedAccess;
923 ACCESS_MASK DesiredAccess;
924 BOOLEAN result;
925 } any_accesses_t;
926
927 static const any_accesses_t any_accesses[] = {
928 {0xFEDCBA76, 0xFEDCBA76, 1},
929 {0x00000000, 0xFEDCBA76, 0},
930 {0xFEDCBA76, 0x00000000, 0},
931 {0x00000000, 0x00000000, 0},
932 {0xFEDCBA76, 0x01234589, 0},
933 {0x00040000, 0xFEDCBA76, 1},
934 {0x00040000, 0xFED8BA76, 0},
935 {0xFEDCBA76, 0x00040000, 1},
936 {0xFED8BA76, 0x00040000, 0},
937 };
938 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
939
940
941 static void test_RtlAreAnyAccessesGranted(void)
942 {
943 unsigned int test_num;
944 BOOLEAN result;
945
946 if (!pRtlAreAnyAccessesGranted)
947 {
948 win_skip("RtlAreAnyAccessesGranted is not available\n");
949 return;
950 }
951
952 for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
953 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
954 any_accesses[test_num].DesiredAccess);
955 ok(any_accesses[test_num].result == result,
956 "(test %d): RtlAreAnyAccessesGranted(%08x, %08x) returns %d, expected %d\n",
957 test_num, any_accesses[test_num].GrantedAccess,
958 any_accesses[test_num].DesiredAccess,
959 result, any_accesses[test_num].result);
960 } /* for */
961 }
962
963 static void test_RtlComputeCrc32(void)
964 {
965 DWORD crc = 0;
966
967 if (!pRtlComputeCrc32)
968 {
969 win_skip("RtlComputeCrc32 is not available\n");
970 return;
971 }
972
973 crc = pRtlComputeCrc32(crc, (const BYTE *)src, LEN);
974 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8x\n", crc);
975 }
976
977
978 typedef struct MY_HANDLE
979 {
980 RTL_HANDLE RtlHandle;
981 void * MyValue;
982 } MY_HANDLE;
983
984 static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
985 {
986 ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
987 *AllocatedBit = *AllocatedBit | 1;
988 }
989
990 static void test_HandleTables(void)
991 {
992 BOOLEAN result;
993 NTSTATUS status;
994 ULONG Index;
995 MY_HANDLE * MyHandle;
996 RTL_HANDLE_TABLE HandleTable;
997
998 if (!pRtlInitializeHandleTable)
999 {
1000 win_skip("RtlInitializeHandleTable is not available\n");
1001 return;
1002 }
1003
1004 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
1005 MyHandle = (MY_HANDLE *)pRtlAllocateHandle(&HandleTable, &Index);
1006 ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
1007 RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
1008 MyHandle = NULL;
1009 result = pRtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
1010 ok(result, "Handle %p wasn't valid\n", MyHandle);
1011 result = pRtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
1012 ok(result, "Couldn't free handle %p\n", MyHandle);
1013 status = pRtlDestroyHandleTable(&HandleTable);
1014 ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08x\n", status);
1015 }
1016
1017 static void test_RtlAllocateAndInitializeSid(void)
1018 {
1019 NTSTATUS ret;
1020 SID_IDENTIFIER_AUTHORITY sia = {{ 1, 2, 3, 4, 5, 6 }};
1021 PSID psid;
1022
1023 if (!pRtlAllocateAndInitializeSid)
1024 {
1025 win_skip("RtlAllocateAndInitializeSid is not available\n");
1026 return;
1027 }
1028
1029 ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
1030 ok(!ret, "RtlAllocateAndInitializeSid error %08x\n", ret);
1031 ret = pRtlFreeSid(psid);
1032 ok(!ret, "RtlFreeSid error %08x\n", ret);
1033
1034 /* these tests crash on XP */
1035 if (0)
1036 {
1037 pRtlAllocateAndInitializeSid(NULL, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
1038 pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL);
1039 }
1040
1041 ret = pRtlAllocateAndInitializeSid(&sia, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
1042 ok(ret == STATUS_INVALID_SID, "wrong error %08x\n", ret);
1043 }
1044
1045 static void test_RtlDeleteTimer(void)
1046 {
1047 NTSTATUS ret;
1048
1049 if (!pRtlDeleteTimer)
1050 {
1051 win_skip("RtlDeleteTimer is not available\n");
1052 return;
1053 }
1054
1055 ret = pRtlDeleteTimer(NULL, NULL, NULL);
1056 ok(ret == STATUS_INVALID_PARAMETER_1 ||
1057 ret == STATUS_INVALID_PARAMETER, /* W2K */
1058 "expected STATUS_INVALID_PARAMETER_1 or STATUS_INVALID_PARAMETER, got %x\n", ret);
1059 }
1060
1061 static void test_RtlThreadErrorMode(void)
1062 {
1063 DWORD oldmode;
1064 BOOL is_wow64;
1065 DWORD mode;
1066 NTSTATUS status;
1067
1068 if (!pRtlGetThreadErrorMode || !pRtlSetThreadErrorMode)
1069 {
1070 win_skip("RtlGetThreadErrorMode and/or RtlSetThreadErrorMode not available\n");
1071 return;
1072 }
1073
1074 if (!pIsWow64Process || !pIsWow64Process(GetCurrentProcess(), &is_wow64))
1075 is_wow64 = FALSE;
1076
1077 oldmode = pRtlGetThreadErrorMode();
1078
1079 status = pRtlSetThreadErrorMode(0x70, &mode);
1080 ok(status == STATUS_SUCCESS ||
1081 status == STATUS_WAIT_1, /* Vista */
1082 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
1083 ok(mode == oldmode,
1084 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
1085 mode, oldmode);
1086 ok(pRtlGetThreadErrorMode() == 0x70,
1087 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0x70);
1088 if (!is_wow64 && pNtCurrentTeb)
1089 ok(pNtCurrentTeb()->HardErrorDisabled == 0x70,
1090 "The TEB contains 0x%x, expected 0x%x\n",
1091 pNtCurrentTeb()->HardErrorDisabled, 0x70);
1092
1093 status = pRtlSetThreadErrorMode(0, &mode);
1094 ok(status == STATUS_SUCCESS ||
1095 status == STATUS_WAIT_1, /* Vista */
1096 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
1097 ok(mode == 0x70,
1098 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
1099 mode, 0x70);
1100 ok(pRtlGetThreadErrorMode() == 0,
1101 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0);
1102 if (!is_wow64 && pNtCurrentTeb)
1103 ok(pNtCurrentTeb()->HardErrorDisabled == 0,
1104 "The TEB contains 0x%x, expected 0x%x\n",
1105 pNtCurrentTeb()->HardErrorDisabled, 0);
1106
1107 for (mode = 1; mode; mode <<= 1)
1108 {
1109 status = pRtlSetThreadErrorMode(mode, NULL);
1110 if (mode & 0x70)
1111 ok(status == STATUS_SUCCESS ||
1112 status == STATUS_WAIT_1, /* Vista */
1113 "RtlSetThreadErrorMode(%x,NULL) failed with error 0x%08x\n",
1114 mode, status);
1115 else
1116 ok(status == STATUS_INVALID_PARAMETER_1,
1117 "RtlSetThreadErrorMode(%x,NULL) returns 0x%08x, "
1118 "expected STATUS_INVALID_PARAMETER_1\n",
1119 mode, status);
1120 }
1121
1122 pRtlSetThreadErrorMode(oldmode, NULL);
1123 }
1124
1125 static void test_LdrProcessRelocationBlock(void)
1126 {
1127 IMAGE_BASE_RELOCATION *ret;
1128 USHORT reloc;
1129 DWORD addr32;
1130 SHORT addr16;
1131
1132 if(!pLdrProcessRelocationBlock) {
1133 win_skip("LdrProcessRelocationBlock not available\n");
1134 return;
1135 }
1136
1137 addr32 = 0x50005;
1138 reloc = IMAGE_REL_BASED_HIGHLOW<<12;
1139 ret = pLdrProcessRelocationBlock(&addr32, 1, &reloc, 0x500050);
1140 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
1141 ok(addr32 == 0x550055, "addr32 = %x, expected 0x550055\n", addr32);
1142
1143 addr16 = 0x505;
1144 reloc = IMAGE_REL_BASED_HIGH<<12;
1145 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
1146 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
1147 ok(addr16 == 0x555, "addr16 = %x, expected 0x555\n", addr16);
1148
1149 addr16 = 0x505;
1150 reloc = IMAGE_REL_BASED_LOW<<12;
1151 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
1152 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
1153 ok(addr16 == 0x565, "addr16 = %x, expected 0x565\n", addr16);
1154 }
1155
1156 static void test_RtlIpv4AddressToString(void)
1157 {
1158 CHAR buffer[20];
1159 CHAR *res;
1160 IN_ADDR ip;
1161 DWORD_PTR len;
1162
1163 if (!pRtlIpv4AddressToStringA)
1164 {
1165 win_skip("RtlIpv4AddressToStringA not available\n");
1166 return;
1167 }
1168
1169 ip.S_un.S_un_b.s_b1 = 1;
1170 ip.S_un.S_un_b.s_b2 = 2;
1171 ip.S_un.S_un_b.s_b3 = 3;
1172 ip.S_un.S_un_b.s_b4 = 4;
1173
1174 memset(buffer, '#', sizeof(buffer) - 1);
1175 buffer[sizeof(buffer) -1] = 0;
1176 res = pRtlIpv4AddressToStringA(&ip, buffer);
1177 len = strlen(buffer);
1178 ok(res == (buffer + len), "got %p with '%s' (expected %p)\n", res, buffer, buffer + len);
1179
1180 res = pRtlIpv4AddressToStringA(&ip, NULL);
1181 ok( (res == (char *)~0) ||
1182 broken(res == (char *)len), /* XP and w2003 */
1183 "got %p (expected ~0)\n", res);
1184
1185 if (0) {
1186 /* this crashes in windows */
1187 memset(buffer, '#', sizeof(buffer) - 1);
1188 buffer[sizeof(buffer) -1] = 0;
1189 res = pRtlIpv4AddressToStringA(NULL, buffer);
1190 trace("got %p with '%s'\n", res, buffer);
1191 }
1192
1193 if (0) {
1194 /* this crashes in windows */
1195 res = pRtlIpv4AddressToStringA(NULL, NULL);
1196 trace("got %p\n", res);
1197 }
1198 }
1199
1200 static void test_RtlIpv4AddressToStringEx(void)
1201 {
1202 CHAR ip_1234[] = "1.2.3.4";
1203 CHAR ip_1234_80[] = "1.2.3.4:80";
1204 LPSTR expect;
1205 CHAR buffer[30];
1206 NTSTATUS res;
1207 IN_ADDR ip;
1208 ULONG size;
1209 DWORD used;
1210 USHORT port;
1211
1212 if (!pRtlIpv4AddressToStringExA)
1213 {
1214 win_skip("RtlIpv4AddressToStringExA not available\n");
1215 return;
1216 }
1217
1218 ip.S_un.S_un_b.s_b1 = 1;
1219 ip.S_un.S_un_b.s_b2 = 2;
1220 ip.S_un.S_un_b.s_b3 = 3;
1221 ip.S_un.S_un_b.s_b4 = 4;
1222
1223 port = htons(80);
1224 expect = ip_1234_80;
1225
1226 size = sizeof(buffer);
1227 memset(buffer, '#', sizeof(buffer) - 1);
1228 buffer[sizeof(buffer) -1] = 0;
1229 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1230 used = strlen(buffer);
1231 ok( (res == STATUS_SUCCESS) &&
1232 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1233 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1234
1235 size = used + 1;
1236 memset(buffer, '#', sizeof(buffer) - 1);
1237 buffer[sizeof(buffer) -1] = 0;
1238 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1239 ok( (res == STATUS_SUCCESS) &&
1240 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1241 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1242
1243 size = used;
1244 memset(buffer, '#', sizeof(buffer) - 1);
1245 buffer[sizeof(buffer) -1] = 0;
1246 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1247 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1248 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1249 res, size, buffer, used + 1);
1250
1251 size = used - 1;
1252 memset(buffer, '#', sizeof(buffer) - 1);
1253 buffer[sizeof(buffer) -1] = 0;
1254 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1255 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1256 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1257 res, size, buffer, used + 1);
1258
1259
1260 /* to get only the ip, use 0 as port */
1261 port = 0;
1262 expect = ip_1234;
1263
1264 size = sizeof(buffer);
1265 memset(buffer, '#', sizeof(buffer) - 1);
1266 buffer[sizeof(buffer) -1] = 0;
1267 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1268 used = strlen(buffer);
1269 ok( (res == STATUS_SUCCESS) &&
1270 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1271 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1272
1273 size = used + 1;
1274 memset(buffer, '#', sizeof(buffer) - 1);
1275 buffer[sizeof(buffer) -1] = 0;
1276 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1277 ok( (res == STATUS_SUCCESS) &&
1278 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1279 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1280
1281 size = used;
1282 memset(buffer, '#', sizeof(buffer) - 1);
1283 buffer[sizeof(buffer) -1] = 0;
1284 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1285 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1286 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1287 res, size, buffer, used + 1);
1288
1289 size = used - 1;
1290 memset(buffer, '#', sizeof(buffer) - 1);
1291 buffer[sizeof(buffer) -1] = 0;
1292 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1293 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1294 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1295 res, size, buffer, used + 1);
1296
1297
1298 /* parameters are checked */
1299 memset(buffer, '#', sizeof(buffer) - 1);
1300 buffer[sizeof(buffer) -1] = 0;
1301 res = pRtlIpv4AddressToStringExA(&ip, 0, buffer, NULL);
1302 ok(res == STATUS_INVALID_PARAMETER,
1303 "got 0x%x with '%s' (expected STATUS_INVALID_PARAMETER)\n", res, buffer);
1304
1305 size = sizeof(buffer);
1306 res = pRtlIpv4AddressToStringExA(&ip, 0, NULL, &size);
1307 ok( res == STATUS_INVALID_PARAMETER,
1308 "got 0x%x and size %d (expected STATUS_INVALID_PARAMETER)\n", res, size);
1309
1310 size = sizeof(buffer);
1311 memset(buffer, '#', sizeof(buffer) - 1);
1312 buffer[sizeof(buffer) -1] = 0;
1313 res = pRtlIpv4AddressToStringExA(NULL, 0, buffer, &size);
1314 ok( res == STATUS_INVALID_PARAMETER,
1315 "got 0x%x and size %d with '%s' (expected STATUS_INVALID_PARAMETER)\n",
1316 res, size, buffer);
1317 }
1318
1319 static void test_RtlIpv4StringToAddress(void)
1320 {
1321 NTSTATUS res;
1322 IN_ADDR ip, expected_ip;
1323 PCSTR terminator;
1324 CHAR dummy;
1325 struct
1326 {
1327 PCSTR address;
1328 NTSTATUS res;
1329 int terminator_offset;
1330 int ip[4];
1331 BOOL strict_is_different;
1332 NTSTATUS res_strict;
1333 int terminator_offset_strict;
1334 int ip_strict[4];
1335 } tests[] =
1336 {
1337 { "", STATUS_INVALID_PARAMETER, 0, { -1 } },
1338 { " ", STATUS_INVALID_PARAMETER, 0, { -1 } },
1339 { "1.1.1.1", STATUS_SUCCESS, 7, { 1, 1, 1, 1 } },
1340 { "0.0.0.0", STATUS_SUCCESS, 7, { 0, 0, 0, 0 } },
1341 { "255.255.255.255", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1342 { "255.255.255.255:123",
1343 STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1344 { "255.255.255.256", STATUS_INVALID_PARAMETER, 15, { -1 } },
1345 { "255.255.255.4294967295",
1346 STATUS_INVALID_PARAMETER, 22, { -1 } },
1347 { "255.255.255.4294967296",
1348 STATUS_INVALID_PARAMETER, 21, { -1 } },
1349 { "255.255.255.4294967297",
1350 STATUS_INVALID_PARAMETER, 21, { -1 } },
1351 { "a", STATUS_INVALID_PARAMETER, 0, { -1 } },
1352 { "1.1.1.0xaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1353 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1354 { "1.1.1.0XaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 },
1355 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1356 { "1.1.1.0x", STATUS_INVALID_PARAMETER, 8, { -1 } },
1357 { "1.1.1.0xff", STATUS_SUCCESS, 10, { 1, 1, 1, 255 },
1358 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1359 { "1.1.1.0x100", STATUS_INVALID_PARAMETER, 11, { -1 },
1360 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1361 { "1.1.1.0xffffffff",STATUS_INVALID_PARAMETER, 16, { -1 },
1362 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1363 { "1.1.1.0x100000000",
1364 STATUS_INVALID_PARAMETER, 16, { -1, 0, 0, 0 },
1365 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1366 { "1.1.1.010", STATUS_SUCCESS, 9, { 1, 1, 1, 8 },
1367 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1368 { "1.1.1.00", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1369 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1370 { "1.1.1.007", STATUS_SUCCESS, 9, { 1, 1, 1, 7 },
1371 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1372 { "1.1.1.08", STATUS_INVALID_PARAMETER, 7, { -1 } },
1373 { "1.1.1.008", STATUS_SUCCESS, 8, { 1, 1, 1, 0 },
1374 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1375 { "1.1.1.0a", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1376 { "1.1.1.0o10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1377 { "1.1.1.0b10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } },
1378 { "1.1.1.-2", STATUS_INVALID_PARAMETER, 6, { -1 } },
1379 { "1", STATUS_SUCCESS, 1, { 0, 0, 0, 1 },
1380 TRUE, STATUS_INVALID_PARAMETER, 1, { -1 } },
1381 { "-1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1382 { "203569230", STATUS_SUCCESS, 9, { 12, 34, 56, 78 },
1383 TRUE, STATUS_INVALID_PARAMETER, 9, { -1 } },
1384 { "1.223756", STATUS_SUCCESS, 8, { 1, 3, 106, 12 },
1385 TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } },
1386 { "3.4.756", STATUS_SUCCESS, 7, { 3, 4, 2, 244 },
1387 TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } },
1388 { "3.4.756.1", STATUS_INVALID_PARAMETER, 9, { -1 } },
1389 { "3.4.65536", STATUS_INVALID_PARAMETER, 9, { -1 } },
1390 { "3.4.5.6.7", STATUS_INVALID_PARAMETER, 7, { -1 } },
1391 { "3.4.5.+6", STATUS_INVALID_PARAMETER, 6, { -1 } },
1392 { " 3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1393 { "\t3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1394 { "3.4.5.6 ", STATUS_SUCCESS, 7, { 3, 4, 5, 6 } },
1395 { "3. 4.5.6", STATUS_INVALID_PARAMETER, 2, { -1 } },
1396 { ".", STATUS_INVALID_PARAMETER, 1, { -1 } },
1397 { "..", STATUS_INVALID_PARAMETER, 1, { -1 } },
1398 { "1.", STATUS_INVALID_PARAMETER, 2, { -1 } },
1399 { "1..", STATUS_INVALID_PARAMETER, 3, { -1 } },
1400 { ".1", STATUS_INVALID_PARAMETER, 1, { -1 } },
1401 { ".1.", STATUS_INVALID_PARAMETER, 1, { -1 } },
1402 { ".1.2.3", STATUS_INVALID_PARAMETER, 1, { -1 } },
1403 { "0.1.2.3", STATUS_SUCCESS, 7, { 0, 1, 2, 3 } },
1404 { "0.1.2.3.", STATUS_INVALID_PARAMETER, 7, { -1 } },
1405 { "[0.1.2.3]", STATUS_INVALID_PARAMETER, 0, { -1 } },
1406 { "::1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1407 { ":1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1408 };
1409 const int testcount = sizeof(tests) / sizeof(tests[0]);
1410 int i;
1411
1412 if (!pRtlIpv4StringToAddressA)
1413 {
1414 skip("RtlIpv4StringToAddress not available\n");
1415 return;
1416 }
1417
1418 if (0)
1419 {
1420 /* leaving either parameter NULL crashes on Windows */
1421 res = pRtlIpv4StringToAddressA(NULL, FALSE, &terminator, &ip);
1422 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, NULL, &ip);
1423 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, &terminator, NULL);
1424 /* same for the wide char version */
1425 /*
1426 res = pRtlIpv4StringToAddressW(NULL, FALSE, &terminatorW, &ip);
1427 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, NULL, &ip);
1428 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, &terminatorW, NULL);
1429 */
1430 }
1431
1432 for (i = 0; i < testcount; i++)
1433 {
1434 /* non-strict */
1435 terminator = &dummy;
1436 ip.S_un.S_addr = 0xabababab;
1437 res = pRtlIpv4StringToAddressA(tests[i].address, FALSE, &terminator, &ip);
1438 ok(res == tests[i].res,
1439 "[%s] res = 0x%08x, expected 0x%08x\n",
1440 tests[i].address, res, tests[i].res);
1441 ok(terminator == tests[i].address + tests[i].terminator_offset,
1442 "[%s] terminator = %p, expected %p\n",
1443 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset);
1444 if (tests[i].ip[0] == -1)
1445 expected_ip.S_un.S_addr = 0xabababab;
1446 else
1447 {
1448 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip[0];
1449 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip[1];
1450 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip[2];
1451 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip[3];
1452 }
1453 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1454 "[%s] ip = %08x, expected %08x\n",
1455 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1456
1457 if (!tests[i].strict_is_different)
1458 {
1459 tests[i].res_strict = tests[i].res;
1460 tests[i].terminator_offset_strict = tests[i].terminator_offset;
1461 tests[i].ip_strict[0] = tests[i].ip[0];
1462 tests[i].ip_strict[1] = tests[i].ip[1];
1463 tests[i].ip_strict[2] = tests[i].ip[2];
1464 tests[i].ip_strict[3] = tests[i].ip[3];
1465 }
1466 /* strict */
1467 terminator = &dummy;
1468 ip.S_un.S_addr = 0xabababab;
1469 res = pRtlIpv4StringToAddressA(tests[i].address, TRUE, &terminator, &ip);
1470 ok(res == tests[i].res_strict,
1471 "[%s] res = 0x%08x, expected 0x%08x\n",
1472 tests[i].address, res, tests[i].res_strict);
1473 ok(terminator == tests[i].address + tests[i].terminator_offset_strict,
1474 "[%s] terminator = %p, expected %p\n",
1475 tests[i].address, terminator, tests[i].address + tests[i].terminator_offset_strict);
1476 if (tests[i].ip_strict[0] == -1)
1477 expected_ip.S_un.S_addr = 0xabababab;
1478 else
1479 {
1480 expected_ip.S_un.S_un_b.s_b1 = tests[i].ip_strict[0];
1481 expected_ip.S_un.S_un_b.s_b2 = tests[i].ip_strict[1];
1482 expected_ip.S_un.S_un_b.s_b3 = tests[i].ip_strict[2];
1483 expected_ip.S_un.S_un_b.s_b4 = tests[i].ip_strict[3];
1484 }
1485 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1486 "[%s] ip = %08x, expected %08x\n",
1487 tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1488 }
1489 }
1490
1491 static void test_LdrAddRefDll(void)
1492 {
1493 HMODULE mod, mod2;
1494 NTSTATUS status;
1495 BOOL ret;
1496
1497 if (!pLdrAddRefDll)
1498 {
1499 win_skip( "LdrAddRefDll not supported\n" );
1500 return;
1501 }
1502
1503 #ifdef __REACTOS__
1504 if (!winetest_interactive)
1505 {
1506 skip("Skipping LdrAddRefDll tests. See CORE-8102\n");
1507 return;
1508 }
1509 #endif
1510
1511 mod = LoadLibraryA("comctl32.dll");
1512 ok(mod != NULL, "got %p\n", mod);
1513 ret = FreeLibrary(mod);
1514 ok(ret, "got %d\n", ret);
1515
1516 mod2 = GetModuleHandleA("comctl32.dll");
1517 ok(mod2 == NULL, "got %p\n", mod2);
1518
1519 /* load, addref and release 2 times */
1520 mod = LoadLibraryA("comctl32.dll");
1521 ok(mod != NULL, "got %p\n", mod);
1522 status = pLdrAddRefDll(0, mod);
1523 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1524 ret = FreeLibrary(mod);
1525 ok(ret, "got %d\n", ret);
1526
1527 mod2 = GetModuleHandleA("comctl32.dll");
1528 ok(mod2 != NULL, "got %p\n", mod2);
1529 ret = FreeLibrary(mod);
1530 ok(ret, "got %d\n", ret);
1531
1532 mod2 = GetModuleHandleA("comctl32.dll");
1533 ok(mod2 == NULL, "got %p\n", mod2);
1534
1535 /* pin refcount */
1536 mod = LoadLibraryA("comctl32.dll");
1537 ok(mod != NULL, "got %p\n", mod);
1538 status = pLdrAddRefDll(LDR_ADDREF_DLL_PIN, mod);
1539 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
1540
1541 ret = FreeLibrary(mod);
1542 ok(ret, "got %d\n", ret);
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
1550 mod2 = GetModuleHandleA("comctl32.dll");
1551 ok(mod2 != NULL, "got %p\n", mod2);
1552 }
1553
1554 START_TEST(rtl)
1555 {
1556 InitFunctionPtrs();
1557
1558 test_RtlCompareMemory();
1559 test_RtlCompareMemoryUlong();
1560 test_RtlMoveMemory();
1561 test_RtlFillMemory();
1562 test_RtlFillMemoryUlong();
1563 test_RtlZeroMemory();
1564 test_RtlUlonglongByteSwap();
1565 test_RtlUniform();
1566 test_RtlRandom();
1567 test_RtlAreAllAccessesGranted();
1568 test_RtlAreAnyAccessesGranted();
1569 test_RtlComputeCrc32();
1570 test_HandleTables();
1571 test_RtlAllocateAndInitializeSid();
1572 test_RtlDeleteTimer();
1573 test_RtlThreadErrorMode();
1574 test_LdrProcessRelocationBlock();
1575 test_RtlIpv4AddressToString();
1576 test_RtlIpv4AddressToStringEx();
1577 test_RtlIpv4StringToAddress();
1578 test_LdrAddRefDll();
1579 }