Added version information.
[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 "winbase.h"
27 #include "wine/test.h"
28 #include "winnt.h"
29 #include "winnls.h"
30 #include "winternl.h"
31
32 /* Function ptrs for ntdll calls */
33 static HMODULE hntdll = 0;
34 static SIZE_T    (WINAPI  *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);
35 static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);
36 static ULONG     (WINAPI  *pRtlUniform)(PULONG);
37 static ULONG     (WINAPI  *pRtlRandom)(PULONG);
38 static BOOLEAN   (WINAPI  *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
39 static BOOLEAN   (WINAPI  *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
40
41
42 static void InitFunctionPtrs(void)
43 {
44     hntdll = LoadLibraryA("ntdll.dll");
45     ok(hntdll != 0, "LoadLibrary failed");
46     if (hntdll) {
47         pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
48         pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
49         pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
50         pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
51         pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
52         pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
53     } /* if */
54 }
55
56
57 static void test_RtlCompareMemoryUlong(void)
58 {
59     ULONG a[10];
60     ULONG result;
61
62     a[0]= 0x0123;
63     a[1]= 0x4567;
64     a[2]= 0x89ab;
65     a[3]= 0xcdef;
66     result = pRtlCompareMemoryUlong(a, 0, 0x0123);
67     ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %lu, expected 0\n", a, result);
68     result = pRtlCompareMemoryUlong(a, 3, 0x0123);
69     ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
70     result = pRtlCompareMemoryUlong(a, 4, 0x0123);
71     ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
72     result = pRtlCompareMemoryUlong(a, 5, 0x0123);
73     ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
74     result = pRtlCompareMemoryUlong(a, 7, 0x0123);
75     ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
76     result = pRtlCompareMemoryUlong(a, 8, 0x0123);
77     ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 4\n", a, result);
78     result = pRtlCompareMemoryUlong(a, 9, 0x0123);
79     ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 4\n", a, result);
80     result = pRtlCompareMemoryUlong(a, 4, 0x0127);
81     ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %lu, expected 0\n", a, result);
82     result = pRtlCompareMemoryUlong(a, 4, 0x7123);
83     ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %lu, expected 0\n", a, result);
84     result = pRtlCompareMemoryUlong(a, 16, 0x4567);
85     ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %lu, expected 0\n", a, result);
86
87     a[1]= 0x0123;
88     result = pRtlCompareMemoryUlong(a, 3, 0x0123);
89     ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
90     result = pRtlCompareMemoryUlong(a, 4, 0x0123);
91     ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
92     result = pRtlCompareMemoryUlong(a, 5, 0x0123);
93     ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
94     result = pRtlCompareMemoryUlong(a, 7, 0x0123);
95     ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
96     result = pRtlCompareMemoryUlong(a, 8, 0x0123);
97     ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 8\n", a, result);
98     result = pRtlCompareMemoryUlong(a, 9, 0x0123);
99     ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 8\n", a, result);
100 }
101
102
103 static void test_RtlUlonglongByteSwap(void)
104 {
105     ULONGLONG result;
106
107     result = pRtlUlonglongByteSwap(0x7654321087654321);
108     ok(0x2143658710325476 == result,
109        "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%llx, expected 0x2143658710325476",
110        result);
111 }
112
113
114 static void test_RtlUniform(void)
115 {
116     ULONGLONG num;
117     ULONG seed;
118     ULONG seed_bak;
119     ULONG expected;
120     ULONG result;
121
122 /*
123  * According to the documentation RtlUniform is using D.H. Lehmer's 1948
124  * algorithm. This algorithm is:
125  *
126  * seed = (seed * const_1 + const_2) % const_3;
127  *
128  * According to the documentation the random number is distributed over
129  * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
130  *
131  * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
132  *
133  * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
134  * algorithm can be expressed without division as:
135  *
136  * seed = (seed * const_1 + const_2) & MAXLONG;
137  *
138  * To find out const_2 we just call RtlUniform with seed set to 0:
139  */
140     seed = 0;
141     expected = 0x7fffffc3;
142     result = pRtlUniform(&seed);
143     ok(result == expected,
144         "RtlUniform(&seed (seed == 0)) returns %lx, expected %lx",
145         result, expected);
146 /*
147  * The algorithm is now:
148  *
149  * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
150  *
151  * To find out const_1 we can use:
152  *
153  * const_1 = RtlUniform(1) - 0x7fffffc3;
154  *
155  * If that does not work a search loop can try all possible values of
156  * const_1 and compare to the result to RtlUniform(1).
157  * This way we find out that const_1 is 0xffffffed.
158  *
159  * For seed = 1 the const_2 is 0x7fffffc4:
160  */
161     seed = 1;
162     expected = seed * 0xffffffed + 0x7fffffc3 + 1;
163     result = pRtlUniform(&seed);
164     ok(result == expected,
165         "RtlUniform(&seed (seed == 1)) returns %lx, expected %lx",
166         result, expected);
167 /*
168  * For seed = 2 the const_2 is 0x7fffffc3:
169  */
170     seed = 2;
171     expected = seed * 0xffffffed + 0x7fffffc3;
172     result = pRtlUniform(&seed);
173     ok(result == expected,
174         "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx",
175         result, expected);
176 /*
177  * More tests show that if seed is odd the result must be incremented by 1:
178  */
179     seed = 3;
180     expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
181     result = pRtlUniform(&seed);
182     ok(result == expected,
183         "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx",
184         result, expected);
185
186     seed = 0x6bca1aa;
187     expected = seed * 0xffffffed + 0x7fffffc3;
188     result = pRtlUniform(&seed);
189     ok(result == expected,
190         "RtlUniform(&seed (seed == 0x6bca1aa)) returns %lx, expected %lx",
191         result, expected);
192
193     seed = 0x6bca1ab;
194     expected = seed * 0xffffffed + 0x7fffffc3 + 1;
195     result = pRtlUniform(&seed);
196     ok(result == expected,
197         "RtlUniform(&seed (seed == 0x6bca1ab)) returns %lx, expected %lx",
198         result, expected);
199 /*
200  * When seed is 0x6bca1ac there is an exception:
201  */
202     seed = 0x6bca1ac;
203     expected = seed * 0xffffffed + 0x7fffffc3 + 2;
204     result = pRtlUniform(&seed);
205     ok(result == expected,
206         "RtlUniform(&seed (seed == 0x6bca1ac)) returns %lx, expected %lx",
207         result, expected);
208 /*
209  * Note that up to here const_3 is not used
210  * (the highest bit of the result is not set).
211  *
212  * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
213  */
214     seed = 0x6bca1ad;
215     expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
216     result = pRtlUniform(&seed);
217     ok(result == expected,
218         "RtlUniform(&seed (seed == 0x6bca1ad)) returns %lx, expected %lx",
219         result, expected);
220
221     seed = 0x6bca1ae;
222     expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
223     result = pRtlUniform(&seed);
224     ok(result == expected,
225         "RtlUniform(&seed (seed == 0x6bca1ae)) returns %lx, expected %lx",
226         result, expected);
227 /*
228  * There are several ranges where for odd or even seed the result must be
229  * incremented by 1. You can see this ranges in the following test.
230  *
231  * For a full test use one of the following loop heads:
232  *
233  *  for (num = 0; num <= 0xffffffff; num++) {
234  *      seed = num;
235  *      ...
236  *
237  *  seed = 0;
238  *  for (num = 0; num <= 0xffffffff; num++) {
239  *      ...
240  */
241     seed = 0;
242     for (num = 0; num <= 100000; num++) {
243
244         expected = seed * 0xffffffed + 0x7fffffc3;
245         if (seed < 0x6bca1ac) {
246             expected = expected + (seed & 1);
247         } else if (seed == 0x6bca1ac) {
248             expected = (expected + 2) & MAXLONG;
249         } else if (seed < 0xd79435c) {
250             expected = (expected + (~seed & 1)) & MAXLONG;
251         } else if (seed < 0x1435e50b) {
252             expected = expected + (seed & 1);
253         } else if (seed < 0x1af286ba) { 
254             expected = (expected + (~seed & 1)) & MAXLONG;
255         } else if (seed < 0x21af2869) {
256             expected = expected + (seed & 1);
257         } else if (seed < 0x286bca18) {
258             expected = (expected + (~seed & 1)) & MAXLONG;
259         } else if (seed < 0x2f286bc7) {
260             expected = expected + (seed & 1);
261         } else if (seed < 0x35e50d77) {
262             expected = (expected + (~seed & 1)) & MAXLONG;
263         } else if (seed < 0x3ca1af26) {
264             expected = expected + (seed & 1);
265         } else if (seed < 0x435e50d5) {
266             expected = (expected + (~seed & 1)) & MAXLONG;
267         } else if (seed < 0x4a1af284) {
268             expected = expected + (seed & 1);
269         } else if (seed < 0x50d79433) {
270             expected = (expected + (~seed & 1)) & MAXLONG;
271         } else if (seed < 0x579435e2) {
272             expected = expected + (seed & 1);
273         } else if (seed < 0x5e50d792) {
274             expected = (expected + (~seed & 1)) & MAXLONG;
275         } else if (seed < 0x650d7941) {
276             expected = expected + (seed & 1);
277         } else if (seed < 0x6bca1af0) {
278             expected = (expected + (~seed & 1)) & MAXLONG;
279         } else if (seed < 0x7286bc9f) {
280             expected = expected + (seed & 1);
281         } else if (seed < 0x79435e4e) {
282             expected = (expected + (~seed & 1)) & MAXLONG;
283         } else if (seed < 0x7ffffffd) {
284             expected = expected + (seed & 1);
285         } else if (seed < 0x86bca1ac) {
286             expected = (expected + (~seed & 1)) & MAXLONG;
287         } else if (seed == 0x86bca1ac) {
288             expected = (expected + 1) & MAXLONG;
289         } else if (seed < 0x8d79435c) {
290             expected = expected + (seed & 1);
291         } else if (seed < 0x9435e50b) {
292             expected = (expected + (~seed & 1)) & MAXLONG;
293         } else if (seed < 0x9af286ba) {
294             expected = expected + (seed & 1);
295         } else if (seed < 0xa1af2869) {
296             expected = (expected + (~seed & 1)) & MAXLONG;
297         } else if (seed < 0xa86bca18) {
298             expected = expected + (seed & 1);
299         } else if (seed < 0xaf286bc7) {
300             expected = (expected + (~seed & 1)) & MAXLONG;
301         } else if (seed == 0xaf286bc7) {
302             expected = (expected + 2) & MAXLONG;
303         } else if (seed < 0xb5e50d77) {
304             expected = expected + (seed & 1);
305         } else if (seed < 0xbca1af26) {
306             expected = (expected + (~seed & 1)) & MAXLONG;
307         } else if (seed < 0xc35e50d5) {
308             expected = expected + (seed & 1);
309         } else if (seed < 0xca1af284) {
310             expected = (expected + (~seed & 1)) & MAXLONG;
311         } else if (seed < 0xd0d79433) {
312             expected = expected + (seed & 1);
313         } else if (seed < 0xd79435e2) {
314             expected = (expected + (~seed & 1)) & MAXLONG;
315         } else if (seed < 0xde50d792) {
316             expected = expected + (seed & 1);
317         } else if (seed < 0xe50d7941) {
318             expected = (expected + (~seed & 1)) & MAXLONG;
319         } else if (seed < 0xebca1af0) {
320             expected = expected + (seed & 1);
321         } else if (seed < 0xf286bc9f) {
322             expected = (expected + (~seed & 1)) & MAXLONG;
323         } else if (seed < 0xf9435e4e) {
324             expected = expected + (seed & 1);
325         } else if (seed < 0xfffffffd) {
326             expected = (expected + (~seed & 1)) & MAXLONG;
327         } else {
328             expected = expected + (seed & 1);
329         } /* if */
330         seed_bak = seed;
331         result = pRtlUniform(&seed);
332         ok(result == expected,
333                 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx",
334                 num, seed_bak, result, expected);
335         ok(seed == expected,
336                 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx",
337                 num, seed_bak, seed, expected);
338     } /* for */
339 /*
340  * Further investigation shows: In the different regions the highest bit
341  * is set or cleared when even or odd seeds need an increment by 1.
342  * This leads to a simplified algorithm:
343  *
344  * seed = seed * 0xffffffed + 0x7fffffc3;
345  * if (seed == 0xffffffff || seed == 0x7ffffffe) {
346  *     seed = (seed + 2) & MAXLONG;
347  * } else if (seed == 0x7fffffff) {
348  *     seed = 0;
349  * } else if ((seed & 0x80000000) == 0) {
350  *     seed = seed + (~seed & 1);
351  * } else {
352  *     seed = (seed + (seed & 1)) & MAXLONG;
353  * }
354  *
355  * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
356  *
357  * Now comes the funny part:
358  * It took me one weekend, to find the complicated algorithm and one day more,
359  * to find the simplified algorithm. Several weeks later I found out: The value
360  * MAXLONG (=0x7fffffff) is never returned, neighter with the native function
361  * nor with the simplified algorithm. In reality the native function and our
362  * function return a random number distributed over [0..MAXLONG-1]. Note
363  * that this is different to what native documentation states [0..MAXLONG].
364  * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
365  *
366  * seed = (seed * const_1 + const_2) % MAXLONG;
367  *
368  * Further investigations show that the real algorithm is:
369  *
370  * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
371  *
372  * This is checked with the test below:
373  */
374     seed = 0;
375     for (num = 0; num <= 100000; num++) {
376         expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
377         seed_bak = seed;
378         result = pRtlUniform(&seed);
379         ok(result == expected,
380                 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx",
381                 num, seed_bak, result, expected);
382         ok(seed == expected,
383                 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx",
384                 num, seed_bak, seed, expected);
385     } /* for */
386 /*
387  * More tests show that RtlUniform does not return 0x7ffffffd for seed values
388  * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
389  * that there is more than one cycle of generated randon numbers ...
390  */
391 }
392
393
394 ULONG WINAPI my_RtlRandom(PULONG seed)
395 {
396     static ULONG saved_value[128] =
397     { /*   0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
398       /*   8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
399       /*  16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
400       /*  24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
401       /*  32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
402       /*  40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
403       /*  48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
404       /*  56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
405       /*  64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
406       /*  72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
407       /*  80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
408       /*  88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
409       /*  96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
410       /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
411       /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
412       /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
413     ULONG rand;
414     int pos;
415     ULONG result;
416
417     rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
418     *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
419     pos = *seed & 0x7f;
420     result = saved_value[pos];
421     saved_value[pos] = rand;
422     return(result);
423 }
424
425
426 static void test_RtlRandom(void)
427 {
428     ULONGLONG num;
429     ULONG seed;
430     ULONG seed_bak;
431     ULONG seed_expected;
432     ULONG result;
433     ULONG result_expected;
434
435 /*
436  * Unlike RtlUniform, RtlRandom is not documented. We guess that for
437  * RtlRandom D.H. Lehmer's 1948 algorithm is used like stated in
438  * the documentation of the RtlUniform function. This algorithm is:
439  *
440  * seed = (seed * const_1 + const_2) % const_3;
441  *
442  * According to the RtlUniform documentation the random number is
443  * distributed over [0..MAXLONG], but in reality it is distributed
444  * over [0..MAXLONG-1]. Therefore const_3 might be MAXLONG + 1 or
445  * MAXLONG:
446  *
447  * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
448  *
449  * or
450  *
451  * seed = (seed * const_1 + const_2) % MAXLONG;
452  *
453  * To find out const_2 we just call RtlRandom with seed set to 0:
454  */
455     seed = 0;
456     result_expected = 0x320a1743;
457     seed_expected =0x44b;
458     result = pRtlRandom(&seed);
459     ok(result == result_expected,
460         "pRtlRandom(&seed (seed == 0)) returns %lx, expected %lx",
461         result, result_expected);
462     ok(seed == seed_expected,
463         "pRtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx",
464         seed, seed_expected);
465 /*
466  * Seed is not equal to result as with RtlUniform. To see more we
467  * call RtlRandom aggain with seed set to 0:
468  */
469     seed = 0;
470     result_expected = 0x7fffffc3;
471     seed_expected =0x44b;
472     result = pRtlRandom(&seed);
473     ok(result == result_expected,
474         "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx",
475         result, result_expected);
476     ok(seed == seed_expected,
477         "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx",
478         seed, seed_expected);
479 /*
480  * Seed is set to the same value as before but the result is different.
481  * To see more we call RtlRandom aggain with seed set to 0:
482  */
483     seed = 0;
484     result_expected = 0x7fffffc3;
485     seed_expected =0x44b;
486     result = pRtlRandom(&seed);
487     ok(result == result_expected,
488         "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx",
489         result, result_expected);
490     ok(seed == seed_expected,
491         "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx",
492         seed, seed_expected);
493 /*
494  * Seed is aggain set to the same value as before. This time we also
495  * have the same result as before. Interestingly the value of the
496  * result is 0x7fffffc3 which is the same value used in RtlUniform
497  * as const_2. If we do
498  *
499  * seed = 0;
500  * result = RtlUniform(&seed);
501  *
502  * we get the same result (0x7fffffc3) as with
503  *
504  * seed = 0;
505  * RtlRandom(&seed);
506  * seed = 0;
507  * result = RtlRandom(&seed);
508  *
509  * And there is another interesting thing. If we do
510  *
511  * seed = 0;
512  * RtlUniform(&seed);
513  * RtlUniform(&seed);
514  *
515  * seed is set to the value 0x44b which ist the same value that
516  *
517  * seed = 0;
518  * RtlRandom(&seed);
519  *
520  * assigns to seed. Putting this two findings together leads to
521  * the concluson that RtlRandom saves the value in some variable,
522  * like in the following algorithm:
523  *
524  * result = saved_value;
525  * saved_value = RtlUniform(&seed);
526  * RtlUniform(&seed);
527  * return(result);
528  *
529  * Now we do further tests with seed set to 1:
530  */
531     seed = 1;
532     result_expected = 0x7a50bbc6;
533     seed_expected =0x5a1;
534     result = pRtlRandom(&seed);
535     ok(result == result_expected,
536         "RtlRandom(&seed (seed == 1)) returns %lx, expected %lx",
537         result, result_expected);
538     ok(seed == seed_expected,
539         "RtlRandom(&seed (seed == 1)) sets seed to %lx, expected %lx",
540         seed, seed_expected);
541 /*
542  * If there is just one saved_value the result now would be
543  * 0x7fffffc3. From this test we can see that there is more than
544  * one saved_value, like with this algorithm:
545  *
546  * result = saved_value[pos];
547  * saved_value[pos] = RtlUniform(&seed);
548  * RtlUniform(&seed);
549  * return(result);
550  *
551  * But how the value of pos is determined? The calls to RtlUniform
552  * create a sequence of random numbers. Every second random number
553  * is put into the saved_value array and is used in some later call
554  * of RtlRandom as result. The only reasonable source to determine
555  * pos are the random numbers generated by RtlUniform which are not
556  * put into the saved_value array. This are the values of seed
557  * between the two calls of RtlUniform as in this altorithm:
558  *
559  * rand = RtlUniform(&seed);
560  * RtlUniform(&seed);
561  * pos = position(seed);
562  * result = saved_value[pos];
563  * saved_value[pos] = rand;
564  * return(result);
565  *
566  * What remains to determine is: The size of the saved_value array,
567  * the initial values of the saved_value array and the function
568  * position(seed). This tests are not shown here. 
569  * The result of this tests ist: The size of the saved_value array
570  * is 128, the initial values can be seen in the my_RtlRandom
571  * function and the position(seed) function is (seed & 0x7f).
572  *
573  * For a full test of RtlRandom use one of the following loop heads:
574  *
575  *  for (num = 0; num <= 0xffffffff; num++) {
576  *      seed = num;
577  *      ...
578  *
579  *  seed = 0;
580  *  for (num = 0; num <= 0xffffffff; num++) {
581  *      ...
582  */
583     seed = 0;
584     for (num = 0; num <= 100000; num++) {
585         seed_bak = seed;
586         seed_expected = seed;
587         result_expected = my_RtlRandom(&seed_expected);
588         /* The following corrections are necessary because the */
589         /* previous tests changed the saved_value array */
590         if (num == 0) {
591             result_expected = 0x7fffffc3;
592         } else if (num == 81) {
593             result_expected = 0x7fffffb1;
594         } /* if */
595         result = pRtlRandom(&seed);
596         ok(result == result_expected,
597                 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx",
598                 num, seed_bak, result, result_expected);
599         ok(seed == seed_expected,
600                 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx",
601                 num, seed_bak, seed, seed_expected);
602     } /* for */
603 }
604
605
606 typedef struct {
607     ACCESS_MASK GrantedAccess;
608     ACCESS_MASK DesiredAccess;
609     BOOLEAN result;
610 } all_accesses_t;
611
612 static const all_accesses_t all_accesses[] = {
613     {0xFEDCBA76, 0xFEDCBA76, 1},
614     {0x00000000, 0xFEDCBA76, 0},
615     {0xFEDCBA76, 0x00000000, 1},
616     {0x00000000, 0x00000000, 1},
617     {0xFEDCBA76, 0xFEDCBA70, 1},
618     {0xFEDCBA70, 0xFEDCBA76, 0},
619     {0xFEDCBA76, 0xFEDC8A76, 1},
620     {0xFEDC8A76, 0xFEDCBA76, 0},
621     {0xFEDCBA76, 0xC8C4B242, 1},
622     {0xC8C4B242, 0xFEDCBA76, 0},
623 };
624 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
625
626
627 static void test_RtlAreAllAccessesGranted(void)
628 {
629     int test_num;
630     BOOLEAN result;
631
632     for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
633         result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
634                                            all_accesses[test_num].DesiredAccess);
635         ok(all_accesses[test_num].result == result,
636            "(test %d): RtlAreAllAccessesGranted(%08lx, %08lx) returns %d, expected %d",
637            test_num, all_accesses[test_num].GrantedAccess,
638            all_accesses[test_num].DesiredAccess,
639            result, all_accesses[test_num].result);
640     } /* for */
641 }
642
643
644 typedef struct {
645     ACCESS_MASK GrantedAccess;
646     ACCESS_MASK DesiredAccess;
647     BOOLEAN result;
648 } any_accesses_t;
649
650 static const any_accesses_t any_accesses[] = {
651     {0xFEDCBA76, 0xFEDCBA76, 1},
652     {0x00000000, 0xFEDCBA76, 0},
653     {0xFEDCBA76, 0x00000000, 0},
654     {0x00000000, 0x00000000, 0},
655     {0xFEDCBA76, 0x01234589, 0},
656     {0x00040000, 0xFEDCBA76, 1},
657     {0x00040000, 0xFED8BA76, 0},
658     {0xFEDCBA76, 0x00040000, 1},
659     {0xFED8BA76, 0x00040000, 0},
660 };
661 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
662
663
664 static void test_RtlAreAnyAccessesGranted(void)
665 {
666     int test_num;
667     BOOLEAN result;
668
669     for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
670         result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
671                                            any_accesses[test_num].DesiredAccess);
672         ok(any_accesses[test_num].result == result,
673            "(test %d): RtlAreAnyAccessesGranted(%08lx, %08lx) returns %d, expected %d",
674            test_num, any_accesses[test_num].GrantedAccess,
675            any_accesses[test_num].DesiredAccess,
676            result, any_accesses[test_num].result);
677     } /* for */
678 }
679
680
681 START_TEST(rtl)
682 {
683     InitFunctionPtrs();
684
685     test_RtlCompareMemoryUlong();
686     if (pRtlUlonglongByteSwap) {
687         test_RtlUlonglongByteSwap();
688     } /* if */
689     test_RtlUniform();
690     test_RtlRandom();
691     test_RtlAreAllAccessesGranted();
692     test_RtlAreAnyAccessesGranted();
693 }