ntdll pending some winerror.h fixes
[reactos.git] / reactos / regtests / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
28 /* Function ptrs for ntdll calls */
29 static HMODULE hntdll = 0;
30 static SIZE_T (WINAPI *pRtlCompareMemory)(LPCVOID,LPCVOID,SIZE_T);
31 static SIZE_T (WINAPI *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);
32 static VOID (WINAPI *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);
33 static VOID (WINAPI *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);
34 static VOID (WINAPI *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);
35 static VOID (WINAPI *pRtlZeroMemory)(LPVOID,SIZE_T);
36 static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);
37 static ULONG (WINAPI *pRtlUniform)(PULONG);
38 static ULONG (WINAPI *pRtlRandom)(PULONG);
39 static BOOLEAN (WINAPI *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
40 static BOOLEAN (WINAPI *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
41 static DWORD (WINAPI *pRtlComputeCrc32)(DWORD,const BYTE*,INT);
42 static void (WINAPI * pRtlInitializeHandleTable)(ULONG, ULONG, RTL_HANDLE_TABLE *);
43 static BOOLEAN (WINAPI * pRtlIsValidIndexHandle)(const RTL_HANDLE_TABLE *, ULONG, RTL_HANDLE **);
44 static NTSTATUS (WINAPI * pRtlDestroyHandleTable)(RTL_HANDLE_TABLE *);
45 static RTL_HANDLE * (WINAPI * pRtlAllocateHandle)(RTL_HANDLE_TABLE *, ULONG *);
46 static BOOLEAN (WINAPI * pRtlFreeHandle)(RTL_HANDLE_TABLE *, RTL_HANDLE *);
47 #define LEN 16
48 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
49 static ULONG src_aligned_block[4];
50 static ULONG dest_aligned_block[32];
51 static const char *src = (const char*)src_aligned_block;
52 static char* dest = (char*)dest_aligned_block;
53
54 static void InitFunctionPtrs(void)
55 {
56 hntdll = LoadLibraryA("ntdll.dll");
57 ok(hntdll != 0, "LoadLibrary failed\n");
58 if (hntdll) {
59 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
60 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
61 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
62 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
63 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
64 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
65 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
66 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
67 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
68 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
69 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
70 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
71 pRtlInitializeHandleTable = (void *)GetProcAddress(hntdll, "RtlInitializeHandleTable");
72 pRtlIsValidIndexHandle = (void *)GetProcAddress(hntdll, "RtlIsValidIndexHandle");
73 pRtlDestroyHandleTable = (void *)GetProcAddress(hntdll, "RtlDestroyHandleTable");
74 pRtlAllocateHandle = (void *)GetProcAddress(hntdll, "RtlAllocateHandle");
75 pRtlFreeHandle = (void *)GetProcAddress(hntdll, "RtlFreeHandle");
76 }
77 strcpy((char*)src_aligned_block, src_src);
78 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
79 }
80
81 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
82 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
83
84 static void test_RtlCompareMemory(void)
85 {
86 SIZE_T size;
87
88 if (!pRtlCompareMemory)
89 return;
90
91 strcpy(dest, src);
92
93 COMP(src,src,0,0);
94 COMP(src,src,LEN,LEN);
95 dest[0] = 'x';
96 COMP(src,dest,LEN,0);
97 }
98
99 static void test_RtlCompareMemoryUlong(void)
100 {
101 ULONG a[10];
102 ULONG result;
103
104 a[0]= 0x0123;
105 a[1]= 0x4567;
106 a[2]= 0x89ab;
107 a[3]= 0xcdef;
108 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
109 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %lu, expected 0\n", a, result);
110 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
111 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
112 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
113 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
114 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
115 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
116 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
117 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
118 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
119 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 4\n", a, result);
120 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
121 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 4\n", a, result);
122 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
123 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %lu, expected 0\n", a, result);
124 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
125 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %lu, expected 0\n", a, result);
126 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
127 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %lu, expected 0\n", a, result);
128
129 a[1]= 0x0123;
130 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
131 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
132 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
133 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
134 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
135 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
136 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
137 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
138 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
139 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 8\n", a, result);
140 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
141 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 8\n", a, result);
142 }
143
144 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
145 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
146
147 static void test_RtlMoveMemory(void)
148 {
149 if (!pRtlMoveMemory)
150 return;
151
152 /* Length should be in bytes and not rounded. Use strcmp to ensure we
153 * didn't write past the end (it checks for the final NUL left by memset)
154 */
155 COPY(0); CMP("");
156 COPY(1); CMP("T");
157 COPY(2); CMP("Th");
158 COPY(3); CMP("Thi");
159 COPY(4); CMP("This");
160 COPY(5); CMP("This ");
161 COPY(6); CMP("This i");
162 COPY(7); CMP("This is");
163 COPY(8); CMP("This is ");
164 COPY(9); CMP("This is a");
165
166 /* Overlapping */
167 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
168 CMP("his is a test!!");
169 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
170 CMP("TThis is a test!");
171 }
172
173 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
174
175 static void test_RtlFillMemory(void)
176 {
177 if (!pRtlFillMemory)
178 return;
179
180 /* Length should be in bytes and not rounded. Use strcmp to ensure we
181 * didn't write past the end (the remainder of the string should match)
182 */
183 FILL(0); CMP("This is a test!");
184 FILL(1); CMP("xhis is a test!");
185 FILL(2); CMP("xxis is a test!");
186 FILL(3); CMP("xxxs is a test!");
187 FILL(4); CMP("xxxx is a test!");
188 FILL(5); CMP("xxxxxis a test!");
189 FILL(6); CMP("xxxxxxs a test!");
190 FILL(7); CMP("xxxxxxx a test!");
191 FILL(8); CMP("xxxxxxxxa test!");
192 FILL(9); CMP("xxxxxxxxx test!");
193 }
194
195 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
196
197 static void test_RtlFillMemoryUlong(void)
198 {
199 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
200 if (!pRtlFillMemoryUlong)
201 return;
202
203 /* Length should be in bytes and not rounded. Use strcmp to ensure we
204 * didn't write past the end (the remainder of the string should match)
205 */
206 LFILL(0); CMP("This is a test!");
207 LFILL(1); CMP("This is a test!");
208 LFILL(2); CMP("This is a test!");
209 LFILL(3); CMP("This is a test!");
210 LFILL(4); CMP("xxxx is a test!");
211 LFILL(5); CMP("xxxx is a test!");
212 LFILL(6); CMP("xxxx is a test!");
213 LFILL(7); CMP("xxxx is a test!");
214 LFILL(8); CMP("xxxxxxxxa test!");
215 LFILL(9); CMP("xxxxxxxxa test!");
216 }
217
218 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
219 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
220
221 static void test_RtlZeroMemory(void)
222 {
223 if (!pRtlZeroMemory)
224 return;
225
226 /* Length should be in bytes and not rounded. */
227 ZERO(0); MCMP("This is a test!");
228 ZERO(1); MCMP("\0his is a test!");
229 ZERO(2); MCMP("\0\0is is a test!");
230 ZERO(3); MCMP("\0\0\0s is a test!");
231 ZERO(4); MCMP("\0\0\0\0 is a test!");
232 ZERO(5); MCMP("\0\0\0\0\0is a test!");
233 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
234 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
235 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
236 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
237 }
238
239 static void test_RtlUlonglongByteSwap(void)
240 {
241 ULONGLONG result;
242
243 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
244 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
245 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%llx, expected 0x2143658710325476\n",
246 result);
247 }
248
249
250 static void test_RtlUniform(void)
251 {
252 ULONGLONG num;
253 ULONG seed;
254 ULONG seed_bak;
255 ULONG expected;
256 ULONG result;
257
258 /*
259 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
260 * algorithm. This algorithm is:
261 *
262 * seed = (seed * const_1 + const_2) % const_3;
263 *
264 * According to the documentation the random number is distributed over
265 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
266 *
267 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
268 *
269 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
270 * algorithm can be expressed without division as:
271 *
272 * seed = (seed * const_1 + const_2) & MAXLONG;
273 *
274 * To find out const_2 we just call RtlUniform with seed set to 0:
275 */
276 seed = 0;
277 expected = 0x7fffffc3;
278 result = pRtlUniform(&seed);
279 ok(result == expected,
280 "RtlUniform(&seed (seed == 0)) returns %lx, expected %lx\n",
281 result, expected);
282 /*
283 * The algorithm is now:
284 *
285 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
286 *
287 * To find out const_1 we can use:
288 *
289 * const_1 = RtlUniform(1) - 0x7fffffc3;
290 *
291 * If that does not work a search loop can try all possible values of
292 * const_1 and compare to the result to RtlUniform(1).
293 * This way we find out that const_1 is 0xffffffed.
294 *
295 * For seed = 1 the const_2 is 0x7fffffc4:
296 */
297 seed = 1;
298 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
299 result = pRtlUniform(&seed);
300 ok(result == expected,
301 "RtlUniform(&seed (seed == 1)) returns %lx, expected %lx\n",
302 result, expected);
303 /*
304 * For seed = 2 the const_2 is 0x7fffffc3:
305 */
306 seed = 2;
307 expected = seed * 0xffffffed + 0x7fffffc3;
308 result = pRtlUniform(&seed);
309 ok(result == expected,
310 "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx\n",
311 result, expected);
312 /*
313 * More tests show that if seed is odd the result must be incremented by 1:
314 */
315 seed = 3;
316 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
317 result = pRtlUniform(&seed);
318 ok(result == expected,
319 "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx\n",
320 result, expected);
321
322 seed = 0x6bca1aa;
323 expected = seed * 0xffffffed + 0x7fffffc3;
324 result = pRtlUniform(&seed);
325 ok(result == expected,
326 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %lx, expected %lx\n",
327 result, expected);
328
329 seed = 0x6bca1ab;
330 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
331 result = pRtlUniform(&seed);
332 ok(result == expected,
333 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %lx, expected %lx\n",
334 result, expected);
335 /*
336 * When seed is 0x6bca1ac there is an exception:
337 */
338 seed = 0x6bca1ac;
339 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
340 result = pRtlUniform(&seed);
341 ok(result == expected,
342 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %lx, expected %lx\n",
343 result, expected);
344 /*
345 * Note that up to here const_3 is not used
346 * (the highest bit of the result is not set).
347 *
348 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
349 */
350 seed = 0x6bca1ad;
351 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
352 result = pRtlUniform(&seed);
353 ok(result == expected,
354 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %lx, expected %lx\n",
355 result, expected);
356
357 seed = 0x6bca1ae;
358 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
359 result = pRtlUniform(&seed);
360 ok(result == expected,
361 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %lx, expected %lx\n",
362 result, expected);
363 /*
364 * There are several ranges where for odd or even seed the result must be
365 * incremented by 1. You can see this ranges in the following test.
366 *
367 * For a full test use one of the following loop heads:
368 *
369 * for (num = 0; num <= 0xffffffff; num++) {
370 * seed = num;
371 * ...
372 *
373 * seed = 0;
374 * for (num = 0; num <= 0xffffffff; num++) {
375 * ...
376 */
377 seed = 0;
378 for (num = 0; num <= 100000; num++) {
379
380 expected = seed * 0xffffffed + 0x7fffffc3;
381 if (seed < 0x6bca1ac) {
382 expected = expected + (seed & 1);
383 } else if (seed == 0x6bca1ac) {
384 expected = (expected + 2) & MAXLONG;
385 } else if (seed < 0xd79435c) {
386 expected = (expected + (~seed & 1)) & MAXLONG;
387 } else if (seed < 0x1435e50b) {
388 expected = expected + (seed & 1);
389 } else if (seed < 0x1af286ba) {
390 expected = (expected + (~seed & 1)) & MAXLONG;
391 } else if (seed < 0x21af2869) {
392 expected = expected + (seed & 1);
393 } else if (seed < 0x286bca18) {
394 expected = (expected + (~seed & 1)) & MAXLONG;
395 } else if (seed < 0x2f286bc7) {
396 expected = expected + (seed & 1);
397 } else if (seed < 0x35e50d77) {
398 expected = (expected + (~seed & 1)) & MAXLONG;
399 } else if (seed < 0x3ca1af26) {
400 expected = expected + (seed & 1);
401 } else if (seed < 0x435e50d5) {
402 expected = (expected + (~seed & 1)) & MAXLONG;
403 } else if (seed < 0x4a1af284) {
404 expected = expected + (seed & 1);
405 } else if (seed < 0x50d79433) {
406 expected = (expected + (~seed & 1)) & MAXLONG;
407 } else if (seed < 0x579435e2) {
408 expected = expected + (seed & 1);
409 } else if (seed < 0x5e50d792) {
410 expected = (expected + (~seed & 1)) & MAXLONG;
411 } else if (seed < 0x650d7941) {
412 expected = expected + (seed & 1);
413 } else if (seed < 0x6bca1af0) {
414 expected = (expected + (~seed & 1)) & MAXLONG;
415 } else if (seed < 0x7286bc9f) {
416 expected = expected + (seed & 1);
417 } else if (seed < 0x79435e4e) {
418 expected = (expected + (~seed & 1)) & MAXLONG;
419 } else if (seed < 0x7ffffffd) {
420 expected = expected + (seed & 1);
421 } else if (seed < 0x86bca1ac) {
422 expected = (expected + (~seed & 1)) & MAXLONG;
423 } else if (seed == 0x86bca1ac) {
424 expected = (expected + 1) & MAXLONG;
425 } else if (seed < 0x8d79435c) {
426 expected = expected + (seed & 1);
427 } else if (seed < 0x9435e50b) {
428 expected = (expected + (~seed & 1)) & MAXLONG;
429 } else if (seed < 0x9af286ba) {
430 expected = expected + (seed & 1);
431 } else if (seed < 0xa1af2869) {
432 expected = (expected + (~seed & 1)) & MAXLONG;
433 } else if (seed < 0xa86bca18) {
434 expected = expected + (seed & 1);
435 } else if (seed < 0xaf286bc7) {
436 expected = (expected + (~seed & 1)) & MAXLONG;
437 } else if (seed == 0xaf286bc7) {
438 expected = (expected + 2) & MAXLONG;
439 } else if (seed < 0xb5e50d77) {
440 expected = expected + (seed & 1);
441 } else if (seed < 0xbca1af26) {
442 expected = (expected + (~seed & 1)) & MAXLONG;
443 } else if (seed < 0xc35e50d5) {
444 expected = expected + (seed & 1);
445 } else if (seed < 0xca1af284) {
446 expected = (expected + (~seed & 1)) & MAXLONG;
447 } else if (seed < 0xd0d79433) {
448 expected = expected + (seed & 1);
449 } else if (seed < 0xd79435e2) {
450 expected = (expected + (~seed & 1)) & MAXLONG;
451 } else if (seed < 0xde50d792) {
452 expected = expected + (seed & 1);
453 } else if (seed < 0xe50d7941) {
454 expected = (expected + (~seed & 1)) & MAXLONG;
455 } else if (seed < 0xebca1af0) {
456 expected = expected + (seed & 1);
457 } else if (seed < 0xf286bc9f) {
458 expected = (expected + (~seed & 1)) & MAXLONG;
459 } else if (seed < 0xf9435e4e) {
460 expected = expected + (seed & 1);
461 } else if (seed < 0xfffffffd) {
462 expected = (expected + (~seed & 1)) & MAXLONG;
463 } else {
464 expected = expected + (seed & 1);
465 } /* if */
466 seed_bak = seed;
467 result = pRtlUniform(&seed);
468 ok(result == expected,
469 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx\n",
470 num, seed_bak, result, expected);
471 ok(seed == expected,
472 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx\n",
473 num, seed_bak, seed, expected);
474 } /* for */
475 /*
476 * Further investigation shows: In the different regions the highest bit
477 * is set or cleared when even or odd seeds need an increment by 1.
478 * This leads to a simplified algorithm:
479 *
480 * seed = seed * 0xffffffed + 0x7fffffc3;
481 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
482 * seed = (seed + 2) & MAXLONG;
483 * } else if (seed == 0x7fffffff) {
484 * seed = 0;
485 * } else if ((seed & 0x80000000) == 0) {
486 * seed = seed + (~seed & 1);
487 * } else {
488 * seed = (seed + (seed & 1)) & MAXLONG;
489 * }
490 *
491 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
492 *
493 * Now comes the funny part:
494 * It took me one weekend, to find the complicated algorithm and one day more,
495 * to find the simplified algorithm. Several weeks later I found out: The value
496 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
497 * nor with the simplified algorithm. In reality the native function and our
498 * function return a random number distributed over [0..MAXLONG-1]. Note
499 * that this is different from what native documentation states [0..MAXLONG].
500 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
501 *
502 * seed = (seed * const_1 + const_2) % MAXLONG;
503 *
504 * Further investigations show that the real algorithm is:
505 *
506 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
507 *
508 * This is checked with the test below:
509 */
510 seed = 0;
511 for (num = 0; num <= 100000; num++) {
512 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
513 seed_bak = seed;
514 result = pRtlUniform(&seed);
515 ok(result == expected,
516 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx\n",
517 num, seed_bak, result, expected);
518 ok(seed == expected,
519 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx\n",
520 num, seed_bak, seed, expected);
521 } /* for */
522 /*
523 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
524 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
525 * that there is more than one cycle of generated randon numbers ...
526 */
527 }
528
529
530 static ULONG WINAPI my_RtlRandom(PULONG seed)
531 {
532 static ULONG saved_value[128] =
533 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
534 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
535 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
536 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
537 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
538 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
539 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
540 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
541 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
542 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
543 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
544 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
545 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
546 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
547 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
548 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
549 ULONG rand;
550 int pos;
551 ULONG result;
552
553 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
554 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
555 pos = *seed & 0x7f;
556 result = saved_value[pos];
557 saved_value[pos] = rand;
558 return(result);
559 }
560
561
562 static void test_RtlRandom(void)
563 {
564 ULONGLONG num;
565 ULONG seed;
566 ULONG seed_bak;
567 ULONG seed_expected;
568 ULONG result;
569 ULONG result_expected;
570
571 /*
572 * Unlike RtlUniform, RtlRandom is not documented. We guess that for
573 * RtlRandom D.H. Lehmer's 1948 algorithm is used like stated in
574 * the documentation of the RtlUniform function. This algorithm is:
575 *
576 * seed = (seed * const_1 + const_2) % const_3;
577 *
578 * According to the RtlUniform documentation the random number is
579 * distributed over [0..MAXLONG], but in reality it is distributed
580 * over [0..MAXLONG-1]. Therefore const_3 might be MAXLONG + 1 or
581 * MAXLONG:
582 *
583 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
584 *
585 * or
586 *
587 * seed = (seed * const_1 + const_2) % MAXLONG;
588 *
589 * To find out const_2 we just call RtlRandom with seed set to 0:
590 */
591 seed = 0;
592 result_expected = 0x320a1743;
593 seed_expected =0x44b;
594 result = pRtlRandom(&seed);
595 ok(result == result_expected,
596 "pRtlRandom(&seed (seed == 0)) returns %lx, expected %lx\n",
597 result, result_expected);
598 ok(seed == seed_expected,
599 "pRtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx\n",
600 seed, seed_expected);
601 /*
602 * Seed is not equal to result as with RtlUniform. To see more we
603 * call RtlRandom aggain with seed set to 0:
604 */
605 seed = 0;
606 result_expected = 0x7fffffc3;
607 seed_expected =0x44b;
608 result = pRtlRandom(&seed);
609 ok(result == result_expected,
610 "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx\n",
611 result, result_expected);
612 ok(seed == seed_expected,
613 "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx\n",
614 seed, seed_expected);
615 /*
616 * Seed is set to the same value as before but the result is different.
617 * To see more we call RtlRandom aggain with seed set to 0:
618 */
619 seed = 0;
620 result_expected = 0x7fffffc3;
621 seed_expected =0x44b;
622 result = pRtlRandom(&seed);
623 ok(result == result_expected,
624 "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx\n",
625 result, result_expected);
626 ok(seed == seed_expected,
627 "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx\n",
628 seed, seed_expected);
629 /*
630 * Seed is aggain set to the same value as before. This time we also
631 * have the same result as before. Interestingly the value of the
632 * result is 0x7fffffc3 which is the same value used in RtlUniform
633 * as const_2. If we do
634 *
635 * seed = 0;
636 * result = RtlUniform(&seed);
637 *
638 * we get the same result (0x7fffffc3) as with
639 *
640 * seed = 0;
641 * RtlRandom(&seed);
642 * seed = 0;
643 * result = RtlRandom(&seed);
644 *
645 * And there is another interesting thing. If we do
646 *
647 * seed = 0;
648 * RtlUniform(&seed);
649 * RtlUniform(&seed);
650 *
651 * seed is set to the value 0x44b which ist the same value that
652 *
653 * seed = 0;
654 * RtlRandom(&seed);
655 *
656 * assigns to seed. Putting these two findings together leads to
657 * the concluson that RtlRandom saves the value in some variable,
658 * like in the following algorithm:
659 *
660 * result = saved_value;
661 * saved_value = RtlUniform(&seed);
662 * RtlUniform(&seed);
663 * return(result);
664 *
665 * Now we do further tests with seed set to 1:
666 */
667 seed = 1;
668 result_expected = 0x7a50bbc6;
669 seed_expected =0x5a1;
670 result = pRtlRandom(&seed);
671 ok(result == result_expected,
672 "RtlRandom(&seed (seed == 1)) returns %lx, expected %lx\n",
673 result, result_expected);
674 ok(seed == seed_expected,
675 "RtlRandom(&seed (seed == 1)) sets seed to %lx, expected %lx\n",
676 seed, seed_expected);
677 /*
678 * If there is just one saved_value the result now would be
679 * 0x7fffffc3. From this test we can see that there is more than
680 * one saved_value, like with this algorithm:
681 *
682 * result = saved_value[pos];
683 * saved_value[pos] = RtlUniform(&seed);
684 * RtlUniform(&seed);
685 * return(result);
686 *
687 * But how is the value of pos determined? The calls to RtlUniform
688 * create a sequence of random numbers. Every second random number
689 * is put into the saved_value array and is used in some later call
690 * of RtlRandom as result. The only reasonable source to determine
691 * pos are the random numbers generated by RtlUniform which are not
692 * put into the saved_value array. This are the values of seed
693 * between the two calls of RtlUniform as in this algorithm:
694 *
695 * rand = RtlUniform(&seed);
696 * RtlUniform(&seed);
697 * pos = position(seed);
698 * result = saved_value[pos];
699 * saved_value[pos] = rand;
700 * return(result);
701 *
702 * What remains to be determined is: The size of the saved_value array,
703 * the initial values of the saved_value array and the function
704 * position(seed). These tests are not shown here.
705 * The result of these tests is: The size of the saved_value array
706 * is 128, the initial values can be seen in the my_RtlRandom
707 * function and the position(seed) function is (seed & 0x7f).
708 *
709 * For a full test of RtlRandom use one of the following loop heads:
710 *
711 * for (num = 0; num <= 0xffffffff; num++) {
712 * seed = num;
713 * ...
714 *
715 * seed = 0;
716 * for (num = 0; num <= 0xffffffff; num++) {
717 * ...
718 */
719 seed = 0;
720 for (num = 0; num <= 100000; num++) {
721 seed_bak = seed;
722 seed_expected = seed;
723 result_expected = my_RtlRandom(&seed_expected);
724 /* The following corrections are necessary because the */
725 /* previous tests changed the saved_value array */
726 if (num == 0) {
727 result_expected = 0x7fffffc3;
728 } else if (num == 81) {
729 result_expected = 0x7fffffb1;
730 } /* if */
731 result = pRtlRandom(&seed);
732 ok(result == result_expected,
733 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx\n",
734 num, seed_bak, result, result_expected);
735 ok(seed == seed_expected,
736 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx\n",
737 num, seed_bak, seed, seed_expected);
738 } /* for */
739 }
740
741
742 typedef struct {
743 ACCESS_MASK GrantedAccess;
744 ACCESS_MASK DesiredAccess;
745 BOOLEAN result;
746 } all_accesses_t;
747
748 static const all_accesses_t all_accesses[] = {
749 {0xFEDCBA76, 0xFEDCBA76, 1},
750 {0x00000000, 0xFEDCBA76, 0},
751 {0xFEDCBA76, 0x00000000, 1},
752 {0x00000000, 0x00000000, 1},
753 {0xFEDCBA76, 0xFEDCBA70, 1},
754 {0xFEDCBA70, 0xFEDCBA76, 0},
755 {0xFEDCBA76, 0xFEDC8A76, 1},
756 {0xFEDC8A76, 0xFEDCBA76, 0},
757 {0xFEDCBA76, 0xC8C4B242, 1},
758 {0xC8C4B242, 0xFEDCBA76, 0},
759 };
760 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
761
762
763 static void test_RtlAreAllAccessesGranted(void)
764 {
765 size_t test_num;
766 BOOLEAN result;
767
768 for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
769 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
770 all_accesses[test_num].DesiredAccess);
771 ok(all_accesses[test_num].result == result,
772 "(test %d): RtlAreAllAccessesGranted(%08lx, %08lx) returns %d, expected %d\n",
773 test_num, all_accesses[test_num].GrantedAccess,
774 all_accesses[test_num].DesiredAccess,
775 result, all_accesses[test_num].result);
776 } /* for */
777 }
778
779
780 typedef struct {
781 ACCESS_MASK GrantedAccess;
782 ACCESS_MASK DesiredAccess;
783 BOOLEAN result;
784 } any_accesses_t;
785
786 static const any_accesses_t any_accesses[] = {
787 {0xFEDCBA76, 0xFEDCBA76, 1},
788 {0x00000000, 0xFEDCBA76, 0},
789 {0xFEDCBA76, 0x00000000, 0},
790 {0x00000000, 0x00000000, 0},
791 {0xFEDCBA76, 0x01234589, 0},
792 {0x00040000, 0xFEDCBA76, 1},
793 {0x00040000, 0xFED8BA76, 0},
794 {0xFEDCBA76, 0x00040000, 1},
795 {0xFED8BA76, 0x00040000, 0},
796 };
797 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
798
799
800 static void test_RtlAreAnyAccessesGranted(void)
801 {
802 size_t test_num;
803 BOOLEAN result;
804
805 for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
806 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
807 any_accesses[test_num].DesiredAccess);
808 ok(any_accesses[test_num].result == result,
809 "(test %d): RtlAreAnyAccessesGranted(%08lx, %08lx) returns %d, expected %d\n",
810 test_num, any_accesses[test_num].GrantedAccess,
811 any_accesses[test_num].DesiredAccess,
812 result, any_accesses[test_num].result);
813 } /* for */
814 }
815
816 static void test_RtlComputeCrc32(void)
817 {
818 DWORD crc = 0;
819
820 if (!pRtlComputeCrc32)
821 return;
822
823 crc = pRtlComputeCrc32(crc, src, LEN);
824 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8lx\n", crc);
825 }
826
827
828 typedef struct MY_HANDLE
829 {
830 RTL_HANDLE RtlHandle;
831 void * MyValue;
832 } MY_HANDLE;
833
834 static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
835 {
836 ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
837 *AllocatedBit = *AllocatedBit | 1;
838 }
839
840 static void test_HandleTables(void)
841 {
842 BOOLEAN result;
843 NTSTATUS status;
844 ULONG Index;
845 MY_HANDLE * MyHandle;
846 RTL_HANDLE_TABLE HandleTable;
847
848 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
849 MyHandle = (MY_HANDLE *)pRtlAllocateHandle(&HandleTable, &Index);
850 ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
851 RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
852 MyHandle = NULL;
853 result = pRtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
854 ok(result, "Handle %p wasn't valid\n", MyHandle);
855 result = pRtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
856 ok(result, "Couldn't free handle %p\n", MyHandle);
857 status = pRtlDestroyHandleTable(&HandleTable);
858 ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08lx\n", status);
859 }
860
861 START_TEST(rtl)
862 {
863 InitFunctionPtrs();
864
865 if (pRtlCompareMemory)
866 test_RtlCompareMemory();
867 if (pRtlCompareMemoryUlong)
868 test_RtlCompareMemoryUlong();
869 if (pRtlMoveMemory)
870 test_RtlMoveMemory();
871 if (pRtlFillMemory)
872 test_RtlFillMemory();
873 if (pRtlFillMemoryUlong)
874 test_RtlFillMemoryUlong();
875 if (pRtlZeroMemory)
876 test_RtlZeroMemory();
877 if (pRtlUlonglongByteSwap)
878 test_RtlUlonglongByteSwap();
879 if (pRtlUniform)
880 test_RtlUniform();
881 if (pRtlRandom)
882 test_RtlRandom();
883 if (pRtlAreAllAccessesGranted)
884 test_RtlAreAllAccessesGranted();
885 if (pRtlAreAnyAccessesGranted)
886 test_RtlAreAnyAccessesGranted();
887 if (pRtlComputeCrc32)
888 test_RtlComputeCrc32();
889 if (pRtlInitializeHandleTable)
890 test_HandleTables();
891 }