1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "crypto_common.h"
18 #include "crypto_rand.h"
19 #include "log.h"
20 #include "memory.h"
21 #include "memory_mock.h"
22
23 using namespace std;
24 using namespace testing::ext;
25
26 class NativeRandTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp();
31 void TearDown();
32 };
33
SetUpTestCase()34 void NativeRandTest::SetUpTestCase() {}
35
TearDownTestCase()36 void NativeRandTest::TearDownTestCase() {}
37
SetUp()38 void NativeRandTest::SetUp() {}
39
TearDown()40 void NativeRandTest::TearDown() {}
41
42 HWTEST_F(NativeRandTest, NativeRandTest001, TestSize.Level0)
43 {
44 OH_CryptoRand *rand = nullptr;
45 OH_Crypto_ErrCode ret = OH_CryptoRand_Create(&rand);
46 EXPECT_EQ(ret, CRYPTO_SUCCESS);
47 ASSERT_NE(rand, nullptr);
48
49 uint8_t seedData[12] = {0x25, 0x65, 0x58, 0x89, 0x85, 0x55, 0x66, 0x77, 0x88, 0x99, 0x11, 0x22};
50 Crypto_DataBlob seed = {
51 .data = seedData,
52 .len = sizeof(seedData)
53 };
54 ret = OH_CryptoRand_SetSeed(rand, &seed);
55 EXPECT_EQ(ret, CRYPTO_SUCCESS);
56
57 Crypto_DataBlob out = { 0 };
58 ret = OH_CryptoRand_GenerateRandom(rand, 10, &out);
59 EXPECT_EQ(ret, CRYPTO_SUCCESS);
60 ASSERT_NE(out.data, nullptr);
61 ASSERT_EQ(out.len, 10);
62
63 const char *algoName = OH_CryptoRand_GetAlgoName(rand);
64 ASSERT_NE(algoName, nullptr);
65 EXPECT_GT(strlen(algoName), 0);
66
67 OH_Crypto_FreeDataBlob(&out);
68 OH_CryptoRand_Destroy(rand);
69 }
70
71 HWTEST_F(NativeRandTest, NativeRandTest002, TestSize.Level0)
72 {
73 OH_Crypto_ErrCode ret = OH_CryptoRand_Create(nullptr);
74 EXPECT_NE(ret, CRYPTO_SUCCESS);
75 }
76
77 HWTEST_F(NativeRandTest, NativeRandTest003, TestSize.Level0)
78 {
79 OH_CryptoRand *rand = nullptr;
80 OH_Crypto_ErrCode ret = OH_CryptoRand_Create(&rand);
81 EXPECT_EQ(ret, CRYPTO_SUCCESS);
82 ASSERT_NE(rand, nullptr);
83
84 uint8_t seedData[12] = {0x25, 0x65, 0x58, 0x89, 0x85, 0x55, 0x66, 0x77, 0x88, 0x99, 0x11, 0x22};
85 Crypto_DataBlob seed = {
86 .data = seedData,
87 .len = sizeof(seedData)
88 };
89 ret = OH_CryptoRand_SetSeed(nullptr, &seed);
90 EXPECT_NE(ret, CRYPTO_SUCCESS);
91 ret = OH_CryptoRand_SetSeed(rand, nullptr);
92 EXPECT_NE(ret, CRYPTO_SUCCESS);
93 ret = OH_CryptoRand_SetSeed(rand, &seed);
94 EXPECT_EQ(ret, CRYPTO_SUCCESS);
95
96 OH_CryptoRand_Destroy(rand);
97 }
98
99 HWTEST_F(NativeRandTest, NativeRandTest004, TestSize.Level0)
100 {
101 OH_CryptoRand *rand = nullptr;
102 OH_Crypto_ErrCode ret = OH_CryptoRand_Create(&rand);
103 EXPECT_EQ(ret, CRYPTO_SUCCESS);
104 ASSERT_NE(rand, nullptr);
105
106 uint8_t seedData[12] = {0x25, 0x65, 0x58, 0x89, 0x85, 0x55, 0x66, 0x77, 0x88, 0x99, 0x11, 0x22};
107 Crypto_DataBlob seed = {
108 .data = seedData,
109 .len = sizeof(seedData)
110 };
111 ret = OH_CryptoRand_SetSeed(rand, &seed);
112 EXPECT_EQ(ret, CRYPTO_SUCCESS);
113
114 Crypto_DataBlob out = { 0 };
115 ret = OH_CryptoRand_GenerateRandom(nullptr, 10, &out);
116 EXPECT_NE(ret, CRYPTO_SUCCESS);
117 ret = OH_CryptoRand_GenerateRandom(rand, 10, nullptr);
118 EXPECT_NE(ret, CRYPTO_SUCCESS);
119 ret = OH_CryptoRand_GenerateRandom(rand, 10, &out);
120 EXPECT_EQ(ret, CRYPTO_SUCCESS);
121
122 OH_Crypto_FreeDataBlob(&out);
123 OH_CryptoRand_Destroy(rand);
124 }
125
126 HWTEST_F(NativeRandTest, NativeRandTest005, TestSize.Level0)
127 {
128 const char *algoName = OH_CryptoRand_GetAlgoName(nullptr);
129 EXPECT_EQ(algoName, nullptr);
130 }