Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 37 | #include "utils.h" |
| 38 | |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 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 |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 48 | GTEST_SKIP() << "<sys/random.h> not available"; |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 49 | #endif |
| 50 | } |
| 51 | |
| 52 | TEST(sys_random, getentropy_EFAULT) { |
zijunzhao | e43d553 | 2023-04-14 01:01:37 +0000 | [diff] [blame] | 53 | #pragma clang diagnostic push |
| 54 | #pragma clang diagnostic ignored "-Wnonnull" |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 55 | #if defined(HAVE_SYS_RANDOM) |
| 56 | errno = 0; |
| 57 | ASSERT_EQ(-1, getentropy(nullptr, 1)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 58 | ASSERT_ERRNO(EFAULT); |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 59 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 60 | GTEST_SKIP() << "<sys/random.h> not available"; |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 61 | #endif |
zijunzhao | e43d553 | 2023-04-14 01:01:37 +0000 | [diff] [blame] | 62 | #pragma clang diagnostic pop |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 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))); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 72 | ASSERT_ERRNO(EIO); |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 73 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 74 | GTEST_SKIP() << "<sys/random.h> not available"; |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 75 | #endif |
| 76 | } |
| 77 | |
| 78 | TEST(sys_random, getrandom) { |
| 79 | #if defined(HAVE_SYS_RANDOM) |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 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 |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 87 | GTEST_SKIP() << "<sys/random.h> not available"; |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 88 | #endif |
| 89 | } |
| 90 | |
| 91 | TEST(sys_random, getrandom_EFAULT) { |
zijunzhao | e43d553 | 2023-04-14 01:01:37 +0000 | [diff] [blame] | 92 | #pragma clang diagnostic push |
| 93 | #pragma clang diagnostic ignored "-Wnonnull" |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 94 | #if defined(HAVE_SYS_RANDOM) |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 95 | errno = 0; |
| 96 | ASSERT_EQ(-1, getrandom(nullptr, 256, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 97 | ASSERT_ERRNO(EFAULT); |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 98 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 99 | GTEST_SKIP() << "<sys/random.h> not available"; |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 100 | #endif |
zijunzhao | e43d553 | 2023-04-14 01:01:37 +0000 | [diff] [blame] | 101 | #pragma clang diagnostic pop |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | TEST(sys_random, getrandom_EINVAL) { |
| 105 | #if defined(HAVE_SYS_RANDOM) |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 106 | errno = 0; |
| 107 | char buf[64]; |
| 108 | ASSERT_EQ(-1, getrandom(buf, sizeof(buf), ~0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 109 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 110 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 111 | GTEST_SKIP() << "<sys/random.h> not available"; |
Elliott Hughes | 8465e96 | 2017-09-27 16:33:35 -0700 | [diff] [blame] | 112 | #endif |
| 113 | } |