blob: e0cbf78572673827cb3f4c0cc603351ddeb930b1 [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) {
zijunzhaoe43d5532023-04-14 01:01:37 +000051#pragma clang diagnostic push
52#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes8465e962017-09-27 16:33:35 -070053#if defined(HAVE_SYS_RANDOM)
54 errno = 0;
55 ASSERT_EQ(-1, getentropy(nullptr, 1));
56 ASSERT_EQ(EFAULT, errno);
57#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -080058 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070059#endif
zijunzhaoe43d5532023-04-14 01:01:37 +000060#pragma clang diagnostic pop
Elliott Hughes8465e962017-09-27 16:33:35 -070061}
62
63TEST(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
Elliott Hughesbcaa4542019-03-08 15:20:23 -080072 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070073#endif
74}
75
76TEST(sys_random, getrandom) {
77#if defined(HAVE_SYS_RANDOM)
Elliott Hughes8465e962017-09-27 16:33:35 -070078 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
Elliott Hughesbcaa4542019-03-08 15:20:23 -080085 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070086#endif
87}
88
89TEST(sys_random, getrandom_EFAULT) {
zijunzhaoe43d5532023-04-14 01:01:37 +000090#pragma clang diagnostic push
91#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes8465e962017-09-27 16:33:35 -070092#if defined(HAVE_SYS_RANDOM)
Elliott Hughes8465e962017-09-27 16:33:35 -070093 errno = 0;
94 ASSERT_EQ(-1, getrandom(nullptr, 256, 0));
95 ASSERT_EQ(EFAULT, errno);
96#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -080097 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070098#endif
zijunzhaoe43d5532023-04-14 01:01:37 +000099#pragma clang diagnostic pop
Elliott Hughes8465e962017-09-27 16:33:35 -0700100}
101
102TEST(sys_random, getrandom_EINVAL) {
103#if defined(HAVE_SYS_RANDOM)
Elliott Hughes8465e962017-09-27 16:33:35 -0700104 errno = 0;
105 char buf[64];
106 ASSERT_EQ(-1, getrandom(buf, sizeof(buf), ~0));
107 ASSERT_EQ(EINVAL, errno);
108#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800109 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -0700110#endif
111}