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