Spelling fixes.
[wine/wine-kai.git] / dlls / ntdll / tests / rtl.c
blob443b987c1f2cfed5fbf13f13ca0d987154909c83
1 /* Unit test suite for Rtl* API functions
3 * Copyright 2003 Thomas Mertes
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * NOTES
20 * We use function pointers here as there is no import library for NTDLL on
21 * windows.
24 #include <stdlib.h>
26 #include "ntdll_test.h"
28 #ifndef __WINE_WINTERNL_H
30 typedef struct _RTL_HANDLE
32 struct _RTL_HANDLE * Next;
33 } RTL_HANDLE;
35 typedef struct _RTL_HANDLE_TABLE
37 ULONG MaxHandleCount;
38 ULONG HandleSize;
39 ULONG Unused[2];
40 PVOID NextFree;
41 PVOID FirstHandle;
42 PVOID ReservedMemory;
43 PVOID MaxHandle;
44 } RTL_HANDLE_TABLE;
46 #endif
48 /* Function ptrs for ntdll calls */
49 static HMODULE hntdll = 0;
50 static SIZE_T (WINAPI *pRtlCompareMemory)(LPCVOID,LPCVOID,SIZE_T);
51 static SIZE_T (WINAPI *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);
52 static VOID (WINAPI *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);
53 static VOID (WINAPI *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);
54 static VOID (WINAPI *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);
55 static VOID (WINAPI *pRtlZeroMemory)(LPVOID,SIZE_T);
56 static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);
57 static ULONG (WINAPI *pRtlUniform)(PULONG);
58 static ULONG (WINAPI *pRtlRandom)(PULONG);
59 static BOOLEAN (WINAPI *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
60 static BOOLEAN (WINAPI *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
61 static DWORD (WINAPI *pRtlComputeCrc32)(DWORD,const BYTE*,INT);
62 static void (WINAPI * pRtlInitializeHandleTable)(ULONG, ULONG, RTL_HANDLE_TABLE *);
63 static BOOLEAN (WINAPI * pRtlIsValidIndexHandle)(const RTL_HANDLE_TABLE *, ULONG, RTL_HANDLE **);
64 static NTSTATUS (WINAPI * pRtlDestroyHandleTable)(RTL_HANDLE_TABLE *);
65 static RTL_HANDLE * (WINAPI * pRtlAllocateHandle)(RTL_HANDLE_TABLE *, ULONG *);
66 static BOOLEAN (WINAPI * pRtlFreeHandle)(RTL_HANDLE_TABLE *, RTL_HANDLE *);
67 static NTSTATUS (WINAPI *pRtlAllocateAndInitializeSid)(PSID_IDENTIFIER_AUTHORITY,BYTE,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,PSID*);
68 static NTSTATUS (WINAPI *pRtlFreeSid)(PSID);
69 #define LEN 16
70 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
71 static ULONG src_aligned_block[4];
72 static ULONG dest_aligned_block[32];
73 static const char *src = (const char*)src_aligned_block;
74 static char* dest = (char*)dest_aligned_block;
76 static void InitFunctionPtrs(void)
78 hntdll = LoadLibraryA("ntdll.dll");
79 ok(hntdll != 0, "LoadLibrary failed\n");
80 if (hntdll) {
81 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
82 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
83 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
84 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
85 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
86 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
87 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
88 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
89 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
90 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
91 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
92 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
93 pRtlInitializeHandleTable = (void *)GetProcAddress(hntdll, "RtlInitializeHandleTable");
94 pRtlIsValidIndexHandle = (void *)GetProcAddress(hntdll, "RtlIsValidIndexHandle");
95 pRtlDestroyHandleTable = (void *)GetProcAddress(hntdll, "RtlDestroyHandleTable");
96 pRtlAllocateHandle = (void *)GetProcAddress(hntdll, "RtlAllocateHandle");
97 pRtlFreeHandle = (void *)GetProcAddress(hntdll, "RtlFreeHandle");
98 pRtlAllocateAndInitializeSid = (void *)GetProcAddress(hntdll, "RtlAllocateAndInitializeSid");
99 pRtlFreeSid = (void *)GetProcAddress(hntdll, "RtlFreeSid");
101 strcpy((char*)src_aligned_block, src_src);
102 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
105 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
106 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
108 static void test_RtlCompareMemory(void)
110 SIZE_T size;
112 if (!pRtlCompareMemory)
113 return;
115 strcpy(dest, src);
117 COMP(src,src,0,0);
118 COMP(src,src,LEN,LEN);
119 dest[0] = 'x';
120 COMP(src,dest,LEN,0);
123 static void test_RtlCompareMemoryUlong(void)
125 ULONG a[10];
126 ULONG result;
128 a[0]= 0x0123;
129 a[1]= 0x4567;
130 a[2]= 0x89ab;
131 a[3]= 0xcdef;
132 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
133 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %u, expected 0\n", a, result);
134 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
135 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
136 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
137 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
138 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
139 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
140 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
141 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
142 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
143 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 4\n", a, result);
144 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
145 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 4\n", a, result);
146 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
147 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %u, expected 0\n", a, result);
148 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
149 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %u, expected 0\n", a, result);
150 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
151 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %u, expected 0\n", a, result);
153 a[1]= 0x0123;
154 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
155 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
156 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
157 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
158 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
159 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
160 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
161 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
162 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
163 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 8\n", a, result);
164 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
165 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 8\n", a, result);
168 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
169 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
171 static void test_RtlMoveMemory(void)
173 if (!pRtlMoveMemory)
174 return;
176 /* Length should be in bytes and not rounded. Use strcmp to ensure we
177 * didn't write past the end (it checks for the final NUL left by memset)
179 COPY(0); CMP("");
180 COPY(1); CMP("T");
181 COPY(2); CMP("Th");
182 COPY(3); CMP("Thi");
183 COPY(4); CMP("This");
184 COPY(5); CMP("This ");
185 COPY(6); CMP("This i");
186 COPY(7); CMP("This is");
187 COPY(8); CMP("This is ");
188 COPY(9); CMP("This is a");
190 /* Overlapping */
191 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
192 CMP("his is a test!!");
193 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
194 CMP("TThis is a test!");
197 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
199 static void test_RtlFillMemory(void)
201 if (!pRtlFillMemory)
202 return;
204 /* Length should be in bytes and not rounded. Use strcmp to ensure we
205 * didn't write past the end (the remainder of the string should match)
207 FILL(0); CMP("This is a test!");
208 FILL(1); CMP("xhis is a test!");
209 FILL(2); CMP("xxis is a test!");
210 FILL(3); CMP("xxxs is a test!");
211 FILL(4); CMP("xxxx is a test!");
212 FILL(5); CMP("xxxxxis a test!");
213 FILL(6); CMP("xxxxxxs a test!");
214 FILL(7); CMP("xxxxxxx a test!");
215 FILL(8); CMP("xxxxxxxxa test!");
216 FILL(9); CMP("xxxxxxxxx test!");
219 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
221 static void test_RtlFillMemoryUlong(void)
223 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
224 if (!pRtlFillMemoryUlong)
225 return;
227 /* Length should be in bytes and not rounded. Use strcmp to ensure we
228 * didn't write past the end (the remainder of the string should match)
230 LFILL(0); CMP("This is a test!");
231 LFILL(1); CMP("This is a test!");
232 LFILL(2); CMP("This is a test!");
233 LFILL(3); CMP("This is a test!");
234 LFILL(4); CMP("xxxx is a test!");
235 LFILL(5); CMP("xxxx is a test!");
236 LFILL(6); CMP("xxxx is a test!");
237 LFILL(7); CMP("xxxx is a test!");
238 LFILL(8); CMP("xxxxxxxxa test!");
239 LFILL(9); CMP("xxxxxxxxa test!");
242 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
243 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
245 static void test_RtlZeroMemory(void)
247 if (!pRtlZeroMemory)
248 return;
250 /* Length should be in bytes and not rounded. */
251 ZERO(0); MCMP("This is a test!");
252 ZERO(1); MCMP("\0his is a test!");
253 ZERO(2); MCMP("\0\0is is a test!");
254 ZERO(3); MCMP("\0\0\0s is a test!");
255 ZERO(4); MCMP("\0\0\0\0 is a test!");
256 ZERO(5); MCMP("\0\0\0\0\0is a test!");
257 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
258 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
259 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
260 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
263 static void test_RtlUlonglongByteSwap(void)
265 ULONGLONG result;
267 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
268 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
269 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%x%08x, expected 0x2143658710325476\n",
270 (DWORD)(result >> 32), (DWORD)result);
274 static void test_RtlUniform(void)
276 ULONGLONG num;
277 ULONG seed;
278 ULONG seed_bak;
279 ULONG expected;
280 ULONG result;
283 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
284 * algorithm. This algorithm is:
286 * seed = (seed * const_1 + const_2) % const_3;
288 * According to the documentation the random number is distributed over
289 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
291 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
293 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
294 * algorithm can be expressed without division as:
296 * seed = (seed * const_1 + const_2) & MAXLONG;
298 * To find out const_2 we just call RtlUniform with seed set to 0:
300 seed = 0;
301 expected = 0x7fffffc3;
302 result = pRtlUniform(&seed);
303 ok(result == expected,
304 "RtlUniform(&seed (seed == 0)) returns %x, expected %x\n",
305 result, expected);
307 * The algorithm is now:
309 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
311 * To find out const_1 we can use:
313 * const_1 = RtlUniform(1) - 0x7fffffc3;
315 * If that does not work a search loop can try all possible values of
316 * const_1 and compare to the result to RtlUniform(1).
317 * This way we find out that const_1 is 0xffffffed.
319 * For seed = 1 the const_2 is 0x7fffffc4:
321 seed = 1;
322 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
323 result = pRtlUniform(&seed);
324 ok(result == expected,
325 "RtlUniform(&seed (seed == 1)) returns %x, expected %x\n",
326 result, expected);
328 * For seed = 2 the const_2 is 0x7fffffc3:
330 seed = 2;
331 expected = seed * 0xffffffed + 0x7fffffc3;
332 result = pRtlUniform(&seed);
335 * Windows Vista uses different algorithms, so skip the rest of the tests
336 * until that is figured out. Trace output for the failures is about 10.5 MB!
339 if (result == 0x7fffff9f) {
340 skip("Most likely running on Windows Vista which uses a different algorithm\n");
341 return;
344 ok(result == expected,
345 "RtlUniform(&seed (seed == 2)) returns %x, expected %x\n",
346 result, expected);
349 * More tests show that if seed is odd the result must be incremented by 1:
351 seed = 3;
352 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
353 result = pRtlUniform(&seed);
354 ok(result == expected,
355 "RtlUniform(&seed (seed == 3)) returns %x, expected %x\n",
356 result, expected);
358 seed = 0x6bca1aa;
359 expected = seed * 0xffffffed + 0x7fffffc3;
360 result = pRtlUniform(&seed);
361 ok(result == expected,
362 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %x, expected %x\n",
363 result, expected);
365 seed = 0x6bca1ab;
366 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
367 result = pRtlUniform(&seed);
368 ok(result == expected,
369 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %x, expected %x\n",
370 result, expected);
372 * When seed is 0x6bca1ac there is an exception:
374 seed = 0x6bca1ac;
375 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
376 result = pRtlUniform(&seed);
377 ok(result == expected,
378 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %x, expected %x\n",
379 result, expected);
381 * Note that up to here const_3 is not used
382 * (the highest bit of the result is not set).
384 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
386 seed = 0x6bca1ad;
387 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
388 result = pRtlUniform(&seed);
389 ok(result == expected,
390 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %x, expected %x\n",
391 result, expected);
393 seed = 0x6bca1ae;
394 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
395 result = pRtlUniform(&seed);
396 ok(result == expected,
397 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %x, expected %x\n",
398 result, expected);
400 * There are several ranges where for odd or even seed the result must be
401 * incremented by 1. You can see this ranges in the following test.
403 * For a full test use one of the following loop heads:
405 * for (num = 0; num <= 0xffffffff; num++) {
406 * seed = num;
407 * ...
409 * seed = 0;
410 * for (num = 0; num <= 0xffffffff; num++) {
411 * ...
413 seed = 0;
414 for (num = 0; num <= 100000; num++) {
416 expected = seed * 0xffffffed + 0x7fffffc3;
417 if (seed < 0x6bca1ac) {
418 expected = expected + (seed & 1);
419 } else if (seed == 0x6bca1ac) {
420 expected = (expected + 2) & MAXLONG;
421 } else if (seed < 0xd79435c) {
422 expected = (expected + (~seed & 1)) & MAXLONG;
423 } else if (seed < 0x1435e50b) {
424 expected = expected + (seed & 1);
425 } else if (seed < 0x1af286ba) {
426 expected = (expected + (~seed & 1)) & MAXLONG;
427 } else if (seed < 0x21af2869) {
428 expected = expected + (seed & 1);
429 } else if (seed < 0x286bca18) {
430 expected = (expected + (~seed & 1)) & MAXLONG;
431 } else if (seed < 0x2f286bc7) {
432 expected = expected + (seed & 1);
433 } else if (seed < 0x35e50d77) {
434 expected = (expected + (~seed & 1)) & MAXLONG;
435 } else if (seed < 0x3ca1af26) {
436 expected = expected + (seed & 1);
437 } else if (seed < 0x435e50d5) {
438 expected = (expected + (~seed & 1)) & MAXLONG;
439 } else if (seed < 0x4a1af284) {
440 expected = expected + (seed & 1);
441 } else if (seed < 0x50d79433) {
442 expected = (expected + (~seed & 1)) & MAXLONG;
443 } else if (seed < 0x579435e2) {
444 expected = expected + (seed & 1);
445 } else if (seed < 0x5e50d792) {
446 expected = (expected + (~seed & 1)) & MAXLONG;
447 } else if (seed < 0x650d7941) {
448 expected = expected + (seed & 1);
449 } else if (seed < 0x6bca1af0) {
450 expected = (expected + (~seed & 1)) & MAXLONG;
451 } else if (seed < 0x7286bc9f) {
452 expected = expected + (seed & 1);
453 } else if (seed < 0x79435e4e) {
454 expected = (expected + (~seed & 1)) & MAXLONG;
455 } else if (seed < 0x7ffffffd) {
456 expected = expected + (seed & 1);
457 } else if (seed < 0x86bca1ac) {
458 expected = (expected + (~seed & 1)) & MAXLONG;
459 } else if (seed == 0x86bca1ac) {
460 expected = (expected + 1) & MAXLONG;
461 } else if (seed < 0x8d79435c) {
462 expected = expected + (seed & 1);
463 } else if (seed < 0x9435e50b) {
464 expected = (expected + (~seed & 1)) & MAXLONG;
465 } else if (seed < 0x9af286ba) {
466 expected = expected + (seed & 1);
467 } else if (seed < 0xa1af2869) {
468 expected = (expected + (~seed & 1)) & MAXLONG;
469 } else if (seed < 0xa86bca18) {
470 expected = expected + (seed & 1);
471 } else if (seed < 0xaf286bc7) {
472 expected = (expected + (~seed & 1)) & MAXLONG;
473 } else if (seed == 0xaf286bc7) {
474 expected = (expected + 2) & MAXLONG;
475 } else if (seed < 0xb5e50d77) {
476 expected = expected + (seed & 1);
477 } else if (seed < 0xbca1af26) {
478 expected = (expected + (~seed & 1)) & MAXLONG;
479 } else if (seed < 0xc35e50d5) {
480 expected = expected + (seed & 1);
481 } else if (seed < 0xca1af284) {
482 expected = (expected + (~seed & 1)) & MAXLONG;
483 } else if (seed < 0xd0d79433) {
484 expected = expected + (seed & 1);
485 } else if (seed < 0xd79435e2) {
486 expected = (expected + (~seed & 1)) & MAXLONG;
487 } else if (seed < 0xde50d792) {
488 expected = expected + (seed & 1);
489 } else if (seed < 0xe50d7941) {
490 expected = (expected + (~seed & 1)) & MAXLONG;
491 } else if (seed < 0xebca1af0) {
492 expected = expected + (seed & 1);
493 } else if (seed < 0xf286bc9f) {
494 expected = (expected + (~seed & 1)) & MAXLONG;
495 } else if (seed < 0xf9435e4e) {
496 expected = expected + (seed & 1);
497 } else if (seed < 0xfffffffd) {
498 expected = (expected + (~seed & 1)) & MAXLONG;
499 } else {
500 expected = expected + (seed & 1);
501 } /* if */
502 seed_bak = seed;
503 result = pRtlUniform(&seed);
504 ok(result == expected,
505 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
506 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
507 ok(seed == expected,
508 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
509 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
510 } /* for */
512 * Further investigation shows: In the different regions the highest bit
513 * is set or cleared when even or odd seeds need an increment by 1.
514 * This leads to a simplified algorithm:
516 * seed = seed * 0xffffffed + 0x7fffffc3;
517 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
518 * seed = (seed + 2) & MAXLONG;
519 * } else if (seed == 0x7fffffff) {
520 * seed = 0;
521 * } else if ((seed & 0x80000000) == 0) {
522 * seed = seed + (~seed & 1);
523 * } else {
524 * seed = (seed + (seed & 1)) & MAXLONG;
527 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
529 * Now comes the funny part:
530 * It took me one weekend, to find the complicated algorithm and one day more,
531 * to find the simplified algorithm. Several weeks later I found out: The value
532 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
533 * nor with the simplified algorithm. In reality the native function and our
534 * function return a random number distributed over [0..MAXLONG-1]. Note
535 * that this is different from what native documentation states [0..MAXLONG].
536 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
538 * seed = (seed * const_1 + const_2) % MAXLONG;
540 * Further investigations show that the real algorithm is:
542 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
544 * This is checked with the test below:
546 seed = 0;
547 for (num = 0; num <= 100000; num++) {
548 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
549 seed_bak = seed;
550 result = pRtlUniform(&seed);
551 ok(result == expected,
552 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
553 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
554 ok(seed == expected,
555 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
556 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, expected);
557 } /* for */
559 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
560 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
561 * that there is more than one cycle of generated randon numbers ...
566 static ULONG WINAPI my_RtlRandom(PULONG seed)
568 static ULONG saved_value[128] =
569 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
570 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
571 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
572 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
573 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
574 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
575 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
576 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
577 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
578 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
579 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
580 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
581 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
582 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
583 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
584 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
585 ULONG rand;
586 int pos;
587 ULONG result;
589 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
590 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
591 pos = *seed & 0x7f;
592 result = saved_value[pos];
593 saved_value[pos] = rand;
594 return(result);
598 static void test_RtlRandom(void)
600 ULONGLONG num;
601 ULONG seed;
602 ULONG seed_bak;
603 ULONG seed_expected;
604 ULONG result;
605 ULONG result_expected;
608 * Unlike RtlUniform, RtlRandom is not documented. We guess that for
609 * RtlRandom D.H. Lehmer's 1948 algorithm is used like stated in
610 * the documentation of the RtlUniform function. This algorithm is:
612 * seed = (seed * const_1 + const_2) % const_3;
614 * According to the RtlUniform documentation the random number is
615 * distributed over [0..MAXLONG], but in reality it is distributed
616 * over [0..MAXLONG-1]. Therefore const_3 might be MAXLONG + 1 or
617 * MAXLONG:
619 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
621 * or
623 * seed = (seed * const_1 + const_2) % MAXLONG;
625 * To find out const_2 we just call RtlRandom with seed set to 0:
627 seed = 0;
628 result_expected = 0x320a1743;
629 seed_expected =0x44b;
630 result = pRtlRandom(&seed);
633 * Windows Vista uses different algorithms, so skip the rest of the tests
634 * until that is figured out. Trace output for the failures is about 10.5 MB!
637 if (seed == 0x3fc) {
638 skip("Most likely running on Windows Vista which uses a different algorithm\n");
639 return;
642 ok(result == result_expected,
643 "pRtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
644 result, result_expected);
645 ok(seed == seed_expected,
646 "pRtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
647 seed, seed_expected);
649 * Seed is not equal to result as with RtlUniform. To see more we
650 * call RtlRandom again with seed set to 0:
652 seed = 0;
653 result_expected = 0x7fffffc3;
654 seed_expected =0x44b;
655 result = pRtlRandom(&seed);
656 ok(result == result_expected,
657 "RtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
658 result, result_expected);
659 ok(seed == seed_expected,
660 "RtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
661 seed, seed_expected);
663 * Seed is set to the same value as before but the result is different.
664 * To see more we call RtlRandom again with seed set to 0:
666 seed = 0;
667 result_expected = 0x7fffffc3;
668 seed_expected =0x44b;
669 result = pRtlRandom(&seed);
670 ok(result == result_expected,
671 "RtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
672 result, result_expected);
673 ok(seed == seed_expected,
674 "RtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
675 seed, seed_expected);
677 * Seed is again set to the same value as before. This time we also
678 * have the same result as before. Interestingly the value of the
679 * result is 0x7fffffc3 which is the same value used in RtlUniform
680 * as const_2. If we do
682 * seed = 0;
683 * result = RtlUniform(&seed);
685 * we get the same result (0x7fffffc3) as with
687 * seed = 0;
688 * RtlRandom(&seed);
689 * seed = 0;
690 * result = RtlRandom(&seed);
692 * And there is another interesting thing. If we do
694 * seed = 0;
695 * RtlUniform(&seed);
696 * RtlUniform(&seed);
698 * seed is set to the value 0x44b which ist the same value that
700 * seed = 0;
701 * RtlRandom(&seed);
703 * assigns to seed. Putting these two findings together leads to
704 * the conclusion that RtlRandom saves the value in some variable,
705 * like in the following algorithm:
707 * result = saved_value;
708 * saved_value = RtlUniform(&seed);
709 * RtlUniform(&seed);
710 * return(result);
712 * Now we do further tests with seed set to 1:
714 seed = 1;
715 result_expected = 0x7a50bbc6;
716 seed_expected =0x5a1;
717 result = pRtlRandom(&seed);
718 ok(result == result_expected,
719 "RtlRandom(&seed (seed == 1)) returns %x, expected %x\n",
720 result, result_expected);
721 ok(seed == seed_expected,
722 "RtlRandom(&seed (seed == 1)) sets seed to %x, expected %x\n",
723 seed, seed_expected);
725 * If there is just one saved_value the result now would be
726 * 0x7fffffc3. From this test we can see that there is more than
727 * one saved_value, like with this algorithm:
729 * result = saved_value[pos];
730 * saved_value[pos] = RtlUniform(&seed);
731 * RtlUniform(&seed);
732 * return(result);
734 * But how is the value of pos determined? The calls to RtlUniform
735 * create a sequence of random numbers. Every second random number
736 * is put into the saved_value array and is used in some later call
737 * of RtlRandom as result. The only reasonable source to determine
738 * pos are the random numbers generated by RtlUniform which are not
739 * put into the saved_value array. This are the values of seed
740 * between the two calls of RtlUniform as in this algorithm:
742 * rand = RtlUniform(&seed);
743 * RtlUniform(&seed);
744 * pos = position(seed);
745 * result = saved_value[pos];
746 * saved_value[pos] = rand;
747 * return(result);
749 * What remains to be determined is: The size of the saved_value array,
750 * the initial values of the saved_value array and the function
751 * position(seed). These tests are not shown here.
752 * The result of these tests is: The size of the saved_value array
753 * is 128, the initial values can be seen in the my_RtlRandom
754 * function and the position(seed) function is (seed & 0x7f).
756 * For a full test of RtlRandom use one of the following loop heads:
758 * for (num = 0; num <= 0xffffffff; num++) {
759 * seed = num;
760 * ...
762 * seed = 0;
763 * for (num = 0; num <= 0xffffffff; num++) {
764 * ...
766 seed = 0;
767 for (num = 0; num <= 100000; num++) {
768 seed_bak = seed;
769 seed_expected = seed;
770 result_expected = my_RtlRandom(&seed_expected);
771 /* The following corrections are necessary because the */
772 /* previous tests changed the saved_value array */
773 if (num == 0) {
774 result_expected = 0x7fffffc3;
775 } else if (num == 81) {
776 result_expected = 0x7fffffb1;
777 } /* if */
778 result = pRtlRandom(&seed);
779 ok(result == result_expected,
780 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
781 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, result_expected);
782 ok(seed == seed_expected,
783 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
784 (DWORD)(num >> 32), (DWORD)num, seed_bak, result, seed_expected);
785 } /* for */
789 typedef struct {
790 ACCESS_MASK GrantedAccess;
791 ACCESS_MASK DesiredAccess;
792 BOOLEAN result;
793 } all_accesses_t;
795 static const all_accesses_t all_accesses[] = {
796 {0xFEDCBA76, 0xFEDCBA76, 1},
797 {0x00000000, 0xFEDCBA76, 0},
798 {0xFEDCBA76, 0x00000000, 1},
799 {0x00000000, 0x00000000, 1},
800 {0xFEDCBA76, 0xFEDCBA70, 1},
801 {0xFEDCBA70, 0xFEDCBA76, 0},
802 {0xFEDCBA76, 0xFEDC8A76, 1},
803 {0xFEDC8A76, 0xFEDCBA76, 0},
804 {0xFEDCBA76, 0xC8C4B242, 1},
805 {0xC8C4B242, 0xFEDCBA76, 0},
807 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
810 static void test_RtlAreAllAccessesGranted(void)
812 unsigned int test_num;
813 BOOLEAN result;
815 for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
816 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
817 all_accesses[test_num].DesiredAccess);
818 ok(all_accesses[test_num].result == result,
819 "(test %d): RtlAreAllAccessesGranted(%08x, %08x) returns %d, expected %d\n",
820 test_num, all_accesses[test_num].GrantedAccess,
821 all_accesses[test_num].DesiredAccess,
822 result, all_accesses[test_num].result);
823 } /* for */
827 typedef struct {
828 ACCESS_MASK GrantedAccess;
829 ACCESS_MASK DesiredAccess;
830 BOOLEAN result;
831 } any_accesses_t;
833 static const any_accesses_t any_accesses[] = {
834 {0xFEDCBA76, 0xFEDCBA76, 1},
835 {0x00000000, 0xFEDCBA76, 0},
836 {0xFEDCBA76, 0x00000000, 0},
837 {0x00000000, 0x00000000, 0},
838 {0xFEDCBA76, 0x01234589, 0},
839 {0x00040000, 0xFEDCBA76, 1},
840 {0x00040000, 0xFED8BA76, 0},
841 {0xFEDCBA76, 0x00040000, 1},
842 {0xFED8BA76, 0x00040000, 0},
844 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
847 static void test_RtlAreAnyAccessesGranted(void)
849 unsigned int test_num;
850 BOOLEAN result;
852 for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
853 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
854 any_accesses[test_num].DesiredAccess);
855 ok(any_accesses[test_num].result == result,
856 "(test %d): RtlAreAnyAccessesGranted(%08x, %08x) returns %d, expected %d\n",
857 test_num, any_accesses[test_num].GrantedAccess,
858 any_accesses[test_num].DesiredAccess,
859 result, any_accesses[test_num].result);
860 } /* for */
863 static void test_RtlComputeCrc32(void)
865 DWORD crc = 0;
867 if (!pRtlComputeCrc32)
868 return;
870 crc = pRtlComputeCrc32(crc, (const BYTE *)src, LEN);
871 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8x\n", crc);
875 typedef struct MY_HANDLE
877 RTL_HANDLE RtlHandle;
878 void * MyValue;
879 } MY_HANDLE;
881 static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
883 ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
884 *AllocatedBit = *AllocatedBit | 1;
887 static void test_HandleTables(void)
889 BOOLEAN result;
890 NTSTATUS status;
891 ULONG Index;
892 MY_HANDLE * MyHandle;
893 RTL_HANDLE_TABLE HandleTable;
895 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
896 MyHandle = (MY_HANDLE *)pRtlAllocateHandle(&HandleTable, &Index);
897 ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
898 RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
899 MyHandle = NULL;
900 result = pRtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
901 ok(result, "Handle %p wasn't valid\n", MyHandle);
902 result = pRtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
903 ok(result, "Couldn't free handle %p\n", MyHandle);
904 status = pRtlDestroyHandleTable(&HandleTable);
905 ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08x\n", status);
908 static void test_RtlAllocateAndInitializeSid(void)
910 NTSTATUS ret;
911 SID_IDENTIFIER_AUTHORITY sia = {{ 1, 2, 3, 4, 5, 6 }};
912 PSID psid;
914 ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
915 ok(!ret, "RtlAllocateAndInitializeSid error %08x\n", ret);
916 ret = pRtlFreeSid(psid);
917 ok(!ret, "RtlFreeSid error %08x\n", ret);
919 /* these tests crash on XP
920 ret = pRtlAllocateAndInitializeSid(NULL, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
921 ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL);*/
923 ret = pRtlAllocateAndInitializeSid(&sia, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
924 ok(ret == STATUS_INVALID_SID, "wrong error %08x\n", ret);
927 START_TEST(rtl)
929 InitFunctionPtrs();
931 if (pRtlCompareMemory)
932 test_RtlCompareMemory();
933 if (pRtlCompareMemoryUlong)
934 test_RtlCompareMemoryUlong();
935 if (pRtlMoveMemory)
936 test_RtlMoveMemory();
937 if (pRtlFillMemory)
938 test_RtlFillMemory();
939 if (pRtlFillMemoryUlong)
940 test_RtlFillMemoryUlong();
941 if (pRtlZeroMemory)
942 test_RtlZeroMemory();
943 if (pRtlUlonglongByteSwap)
944 test_RtlUlonglongByteSwap();
945 if (pRtlUniform)
946 test_RtlUniform();
947 if (pRtlRandom)
948 test_RtlRandom();
949 if (pRtlAreAllAccessesGranted)
950 test_RtlAreAllAccessesGranted();
951 if (pRtlAreAnyAccessesGranted)
952 test_RtlAreAnyAccessesGranted();
953 if (pRtlComputeCrc32)
954 test_RtlComputeCrc32();
955 if (pRtlInitializeHandleTable)
956 test_HandleTables();
957 if (pRtlAllocateAndInitializeSid)
958 test_RtlAllocateAndInitializeSid();