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