blob: 2e2665b89c586bd8016f883729f4bd6b6aac36e9 [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
Elliott Hughesbcaa4542019-03-08 15:20:23 -080046 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070047#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
Elliott Hughesbcaa4542019-03-08 15:20:23 -080056 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070057#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
Elliott Hughesbcaa4542019-03-08 15:20:23 -080069 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070070#endif
71}
72
73TEST(sys_random, getrandom) {
74#if defined(HAVE_SYS_RANDOM)
Elliott Hughes8465e962017-09-27 16:33:35 -070075 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
Elliott Hughesbcaa4542019-03-08 15:20:23 -080082 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070083#endif
84}
85
86TEST(sys_random, getrandom_EFAULT) {
87#if defined(HAVE_SYS_RANDOM)
Elliott Hughes8465e962017-09-27 16:33:35 -070088 errno = 0;
89 ASSERT_EQ(-1, getrandom(nullptr, 256, 0));
90 ASSERT_EQ(EFAULT, errno);
91#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -080092 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070093#endif
94}
95
96TEST(sys_random, getrandom_EINVAL) {
97#if defined(HAVE_SYS_RANDOM)
Elliott Hughes8465e962017-09-27 16:33:35 -070098 errno = 0;
99 char buf[64];
100 ASSERT_EQ(-1, getrandom(buf, sizeof(buf), ~0));
101 ASSERT_EQ(EINVAL, errno);
102#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800103 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -0700104#endif
105}