• 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 #pragma clang diagnostic push
52 #pragma clang diagnostic ignored "-Wnonnull"
53 #if defined(HAVE_SYS_RANDOM)
54   errno = 0;
55   ASSERT_EQ(-1, getentropy(nullptr, 1));
56   ASSERT_EQ(EFAULT, errno);
57 #else
58   GTEST_SKIP() << "<sys/random.h> not available";
59 #endif
60 #pragma clang diagnostic pop
61 }
62 
TEST(sys_random,getentropy_EIO)63 TEST(sys_random, getentropy_EIO) {
64 #if defined(HAVE_SYS_RANDOM)
65   char buf[BUFSIZ];
66   static_assert(BUFSIZ > 256, "BUFSIZ <= 256!");
67 
68   errno = 0;
69   ASSERT_EQ(-1, getentropy(buf, sizeof(buf)));
70   ASSERT_EQ(EIO, errno);
71 #else
72   GTEST_SKIP() << "<sys/random.h> not available";
73 #endif
74 }
75 
TEST(sys_random,getrandom)76 TEST(sys_random, getrandom) {
77 #if defined(HAVE_SYS_RANDOM)
78   char buf1[64];
79   char buf2[64];
80 
81   ASSERT_EQ(64, getrandom(buf1, sizeof(buf1), 0));
82   ASSERT_EQ(64, getrandom(buf2, sizeof(buf2), 0));
83   ASSERT_TRUE(memcmp(buf1, buf2, sizeof(buf1)) != 0);
84 #else
85   GTEST_SKIP() << "<sys/random.h> not available";
86 #endif
87 }
88 
TEST(sys_random,getrandom_EFAULT)89 TEST(sys_random, getrandom_EFAULT) {
90 #pragma clang diagnostic push
91 #pragma clang diagnostic ignored "-Wnonnull"
92 #if defined(HAVE_SYS_RANDOM)
93   errno = 0;
94   ASSERT_EQ(-1, getrandom(nullptr, 256, 0));
95   ASSERT_EQ(EFAULT, errno);
96 #else
97   GTEST_SKIP() << "<sys/random.h> not available";
98 #endif
99 #pragma clang diagnostic pop
100 }
101 
TEST(sys_random,getrandom_EINVAL)102 TEST(sys_random, getrandom_EINVAL) {
103 #if defined(HAVE_SYS_RANDOM)
104   errno = 0;
105   char buf[64];
106   ASSERT_EQ(-1, getrandom(buf, sizeof(buf), ~0));
107   ASSERT_EQ(EINVAL, errno);
108 #else
109   GTEST_SKIP() << "<sys/random.h> not available";
110 #endif
111 }
112