1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <sys/random.h>
6 
7 #include <errno.h>
8 #include <unittest/unittest.h>
9 
getentropy_valid()10 bool getentropy_valid() {
11     BEGIN_TEST;
12 
13     char buf[16];
14 
15     errno = 0;
16     int result = getentropy(buf, sizeof(buf));
17     int err = errno;
18 
19     EXPECT_EQ(result, 0);
20     EXPECT_EQ(err, 0);
21 
22     END_TEST;
23 }
24 
getentropy_too_big()25 bool getentropy_too_big() {
26     BEGIN_TEST;
27 
28     const size_t size = 1024 * 1024 * 1024;
29 
30     char* buf = static_cast<char*>(malloc(size));
31     EXPECT_NONNULL(buf);
32 
33     errno = 0;
34     int result = getentropy(buf, size);
35     int err = errno;
36 
37     EXPECT_EQ(result, -1);
38     EXPECT_EQ(err, EIO);
39 
40     free(buf);
41 
42     END_TEST;
43 }
44 
45 BEGIN_TEST_CASE(getentropy_tests)
46 RUN_TEST(getentropy_valid);
47 RUN_TEST(getentropy_too_big);
END_TEST_CASE(getentropy_tests)48 END_TEST_CASE(getentropy_tests)
49 
50 int main(int argc, char** argv) {
51     return unittest_run_all_tests(argc, argv) ? 0 : -1;
52 }
53