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