blob: 4425dbaabd9cc6697f372d0d5107a8d68b2e0183 [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
Elliott Hughes95646e62023-09-21 14:11:19 -070037#include "utils.h"
38
Elliott Hughes8465e962017-09-27 16:33:35 -070039TEST(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
Elliott Hughesbcaa4542019-03-08 15:20:23 -080048 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070049#endif
50}
51
52TEST(sys_random, getentropy_EFAULT) {
zijunzhaoe43d5532023-04-14 01:01:37 +000053#pragma clang diagnostic push
54#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes8465e962017-09-27 16:33:35 -070055#if defined(HAVE_SYS_RANDOM)
56 errno = 0;
57 ASSERT_EQ(-1, getentropy(nullptr, 1));
Elliott Hughes95646e62023-09-21 14:11:19 -070058 ASSERT_ERRNO(EFAULT);
Elliott Hughes8465e962017-09-27 16:33:35 -070059#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -080060 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070061#endif
zijunzhaoe43d5532023-04-14 01:01:37 +000062#pragma clang diagnostic pop
Elliott Hughes8465e962017-09-27 16:33:35 -070063}
64
65TEST(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)));
Elliott Hughes95646e62023-09-21 14:11:19 -070072 ASSERT_ERRNO(EIO);
Elliott Hughes8465e962017-09-27 16:33:35 -070073#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -080074 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070075#endif
76}
77
78TEST(sys_random, getrandom) {
79#if defined(HAVE_SYS_RANDOM)
Elliott Hughes8465e962017-09-27 16:33:35 -070080 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
Elliott Hughesbcaa4542019-03-08 15:20:23 -080087 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -070088#endif
89}
90
91TEST(sys_random, getrandom_EFAULT) {
zijunzhaoe43d5532023-04-14 01:01:37 +000092#pragma clang diagnostic push
93#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes8465e962017-09-27 16:33:35 -070094#if defined(HAVE_SYS_RANDOM)
Elliott Hughes8465e962017-09-27 16:33:35 -070095 errno = 0;
96 ASSERT_EQ(-1, getrandom(nullptr, 256, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -070097 ASSERT_ERRNO(EFAULT);
Elliott Hughes8465e962017-09-27 16:33:35 -070098#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -080099 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -0700100#endif
zijunzhaoe43d5532023-04-14 01:01:37 +0000101#pragma clang diagnostic pop
Elliott Hughes8465e962017-09-27 16:33:35 -0700102}
103
104TEST(sys_random, getrandom_EINVAL) {
105#if defined(HAVE_SYS_RANDOM)
Elliott Hughes8465e962017-09-27 16:33:35 -0700106 errno = 0;
107 char buf[64];
108 ASSERT_EQ(-1, getrandom(buf, sizeof(buf), ~0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700109 ASSERT_ERRNO(EINVAL);
Elliott Hughes8465e962017-09-27 16:33:35 -0700110#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800111 GTEST_SKIP() << "<sys/random.h> not available";
Elliott Hughes8465e962017-09-27 16:33:35 -0700112#endif
113}