]>
git.saurik.com Git - apple/libc.git/blob - tests/arc4random.c
2 * Copyright (c) 2011 David Schultz
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 #include <sys/types.h>
39 #include <darwintest.h>
40 #include <darwintest_utils.h>
42 #define LEN (1024 * 32)
43 static void * stress(void *ptr __unused
)
48 for (int i
= 0; i
< LEN
; i
++){
52 if ((value
& 0x70) == 0)
53 arc4random_uniform(value
);
54 if ((value
& 0x7) == 0)
55 arc4random_buf(buf
, (size_t)i
);
57 T_ASSERT_EQ(buf
[LEN
], 0, NULL
);
62 T_DECL(arc4random_stress
, "arc4random() stress")
64 const int ncpu
= dt_ncpu();
67 for (int i
= 0; i
< ncpu
; i
++){
68 T_ASSERT_POSIX_ZERO(pthread_create(&thr
[i
], NULL
, stress
, NULL
), NULL
);
70 for (int i
= 0; i
< ncpu
; i
++){
71 T_ASSERT_POSIX_ZERO(pthread_join(thr
[i
], NULL
), NULL
);
76 * BUFSIZE is the number of bytes of rc4 output to compare. The probability
77 * that this test fails spuriously is 2**(-BUFSIZE * 8).
81 T_DECL(arc4random_fork
, "arc4random() shouldn't return the same sequence in child post-fork()")
84 char parentbuf
[BUFSIZE
];
85 char childbuf
[BUFSIZE
];
90 page
= mmap(NULL
, sizeof(struct shared_page
), PROT_READ
| PROT_WRITE
,
91 MAP_ANON
| MAP_SHARED
, -1, 0);
92 T_ASSERT_NE(page
, MAP_FAILED
, "mmap()");
94 arc4random_buf(&c
, 1);
98 T_ASSERT_FAIL("fork() failed");
99 } else if (pid
== 0) {
101 arc4random_buf(page
->childbuf
, BUFSIZE
);
106 arc4random_buf(page
->parentbuf
, BUFSIZE
);
107 T_ASSERT_EQ(wait(&status
), pid
, "wait() returns child pid");
109 T_EXPECT_NE(memcmp(page
->parentbuf
, page
->childbuf
, BUFSIZE
), 0, "sequences are distinct");
112 #define ARC4_EXPECTATION ((2u << 31) - 1)/2
113 #define ITER 10000000
114 #define PROB_THRESH sqrt(6*log2(ITER)/(ITER*1.0))
115 /* Simple randomness test using a chernoff bound
116 * If this fails with probability (1 - 2/(ITER)^{4})
117 * arc4random is NOT a uniform distribution between
118 * 0 and 2**32 - 1. Note that this test does not guarantee
119 * that arc4random. Is correct, just that it isn't terribly
122 T_DECL(arc4random_stats
, "ensure arc4random() is random")
126 for (i
= 0; i
< ITER
; i
++) {
127 total
+= arc4random() > ARC4_EXPECTATION
;
129 double prob
= total
/(ITER
*1.0);
130 T_EXPECT_LT(fabs(prob
- 0.5), PROB_THRESH
, "probability is within threshold");