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