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