blob: a25490c6725a353cfd65bd82418e210a0955759e [file] [log] [blame]
Elliott Hughes8465e962017-09-27 16:33:35 -07001/*
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
37TEST(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_LOG_(INFO) << "This test requires a C library with <sys/random.h>.\n";
47#endif
48}
49
50TEST(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_LOG_(INFO) << "This test requires a C library with <sys/random.h>.\n";
57#endif
58}
59
60TEST(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_LOG_(INFO) << "This test requires a C library with <sys/random.h>.\n";
70#endif
71}
72
73TEST(sys_random, getrandom) {
74#if defined(HAVE_SYS_RANDOM)
75 if (getrandom(nullptr, 0, 0) == -1 && errno == ENOSYS) {
76 GTEST_LOG_(INFO) << "This test requires a >= 3.17 kernel with getrandom(2).\n";
77 return;
78 }
79
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_LOG_(INFO) << "This test requires a C library with <sys/random.h>.\n";
88#endif
89}
90
91TEST(sys_random, getrandom_EFAULT) {
92#if defined(HAVE_SYS_RANDOM)
93 if (getrandom(nullptr, 0, 0) == -1 && errno == ENOSYS) {
94 GTEST_LOG_(INFO) << "This test requires a >= 3.17 kernel with getrandom(2).\n";
95 return;
96 }
97
98 errno = 0;
99 ASSERT_EQ(-1, getrandom(nullptr, 256, 0));
100 ASSERT_EQ(EFAULT, errno);
101#else
102 GTEST_LOG_(INFO) << "This test requires a C library with <sys/random.h>.\n";
103#endif
104}
105
106TEST(sys_random, getrandom_EINVAL) {
107#if defined(HAVE_SYS_RANDOM)
108 if (getrandom(nullptr, 0, 0) == -1 && errno == ENOSYS) {
109 GTEST_LOG_(INFO) << "This test requires a >= 3.17 kernel with getrandom(2).\n";
110 return;
111 }
112
113 errno = 0;
114 char buf[64];
115 ASSERT_EQ(-1, getrandom(buf, sizeof(buf), ~0));
116 ASSERT_EQ(EINVAL, errno);
117#else
118 GTEST_LOG_(INFO) << "This test requires a C library with <sys/random.h>.\n";
119#endif
120}