1 #include <errno.h> 2 #include <gtest/gtest.h> 3 4 using namespace testing::ext; 5 6 class MiscGetentropyTest : public testing::Test { SetUp()7 void SetUp() override {} TearDown()8 void TearDown() override {} 9 }; 10 11 constexpr int BUFFER_SIZE = 1024; 12 constexpr int SIZE = 32; 13 14 /** 15 * @tc.name: getentropy_001 16 * @tc.desc: This test verifies that when calling the getentropy function, the correct buffer and size parameters are 17 * passed in to ensure that the function can successfully generate random data and return a value of 0. 18 * @tc.type: FUNC 19 */ 20 HWTEST_F(MiscGetentropyTest, getentropy_001, TestSize.Level1) 21 { 22 char buffer[SIZE]; 23 int ret = getentropy(buffer, sizeof(buffer)); 24 EXPECT_TRUE(ret == 0); 25 } 26 27 /** 28 * @tc.name: getentropy_002 29 * @tc.desc: The testing viewpoint of this code is to verify whether the getentropy function can correctly return an 30 * error code and set errno to EFAULT when its buf parameter is nullptr. 31 * @tc.type: FUNC 32 */ 33 HWTEST_F(MiscGetentropyTest, getentropy_002, TestSize.Level1) 34 { 35 errno = 0; 36 int ret = getentropy(nullptr, 1); 37 EXPECT_TRUE(ret == -1); 38 EXPECT_TRUE(EFAULT == errno); 39 } 40 41 /** 42 * @tc.name: getentropy_003 43 * @tc.desc: The testing viewpoint of this code is to verify whether the getentropy function can correctly return an 44 * error code and set errno to EIO when it cannot generate enough entropy. 45 * @tc.type: FUNC 46 */ 47 HWTEST_F(MiscGetentropyTest, getentropy_003, TestSize.Level1) 48 { 49 char buffer[BUFFER_SIZE]; 50 errno = 0; 51 int ret = getentropy(buffer, sizeof(buffer)); 52 EXPECT_TRUE(ret == -1); 53 EXPECT_TRUE(EIO == errno); 54 }