• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // <sys/random.h> was only added as of glibc version 2.25.
18 // Don't try to compile this code on older glibc versions.
19 
20 #include <sys/cdefs.h>
21 #if defined(__BIONIC__)
22   #define HAVE_SYS_RANDOM 1
23 #elif defined(__GLIBC_PREREQ)
24   #if __GLIBC_PREREQ(2, 25)
25     #define HAVE_SYS_RANDOM 1
26   #endif
27 #endif
28 
29 
30 #if defined(HAVE_SYS_RANDOM)
31 #include <sys/random.h>
32 #endif
33 
34 #include <errno.h>
35 #include <gtest/gtest.h>
36 
TEST(sys_random,getentropy)37 TEST(sys_random, getentropy) {
38 #if defined(HAVE_SYS_RANDOM)
39   char buf1[64];
40   char buf2[64];
41 
42   ASSERT_EQ(0, getentropy(buf1, sizeof(buf1)));
43   ASSERT_EQ(0, getentropy(buf2, sizeof(buf2)));
44   ASSERT_TRUE(memcmp(buf1, buf2, sizeof(buf1)) != 0);
45 #else
46   GTEST_SKIP() << "<sys/random.h> not available";
47 #endif
48 }
49 
TEST(sys_random,getentropy_EFAULT)50 TEST(sys_random, getentropy_EFAULT) {
51 #if defined(HAVE_SYS_RANDOM)
52   errno = 0;
53   ASSERT_EQ(-1, getentropy(nullptr, 1));
54   ASSERT_EQ(EFAULT, errno);
55 #else
56   GTEST_SKIP() << "<sys/random.h> not available";
57 #endif
58 }
59 
TEST(sys_random,getentropy_EIO)60 TEST(sys_random, getentropy_EIO) {
61 #if defined(HAVE_SYS_RANDOM)
62   char buf[BUFSIZ];
63   static_assert(BUFSIZ > 256, "BUFSIZ <= 256!");
64 
65   errno = 0;
66   ASSERT_EQ(-1, getentropy(buf, sizeof(buf)));
67   ASSERT_EQ(EIO, errno);
68 #else
69   GTEST_SKIP() << "<sys/random.h> not available";
70 #endif
71 }
72 
TEST(sys_random,getrandom)73 TEST(sys_random, getrandom) {
74 #if defined(HAVE_SYS_RANDOM)
75   char buf1[64];
76   char buf2[64];
77 
78   ASSERT_EQ(64, getrandom(buf1, sizeof(buf1), 0));
79   ASSERT_EQ(64, getrandom(buf2, sizeof(buf2), 0));
80   ASSERT_TRUE(memcmp(buf1, buf2, sizeof(buf1)) != 0);
81 #else
82   GTEST_SKIP() << "<sys/random.h> not available";
83 #endif
84 }
85 
TEST(sys_random,getrandom_EFAULT)86 TEST(sys_random, getrandom_EFAULT) {
87 #if defined(HAVE_SYS_RANDOM)
88   errno = 0;
89   ASSERT_EQ(-1, getrandom(nullptr, 256, 0));
90   ASSERT_EQ(EFAULT, errno);
91 #else
92   GTEST_SKIP() << "<sys/random.h> not available";
93 #endif
94 }
95 
TEST(sys_random,getrandom_EINVAL)96 TEST(sys_random, getrandom_EINVAL) {
97 #if defined(HAVE_SYS_RANDOM)
98   errno = 0;
99   char buf[64];
100   ASSERT_EQ(-1, getrandom(buf, sizeof(buf), ~0));
101   ASSERT_EQ(EINVAL, errno);
102 #else
103   GTEST_SKIP() << "<sys/random.h> not available";
104 #endif
105 }
106