1 /*
2 * Copyright (C) 2022 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 "securec.h"
18
19 #include "rand.h"
20 #include "rand_openssl.h"
21
22 #include "log.h"
23 #include "memory.h"
24
25 using namespace std;
26 using namespace testing::ext;
27
28 namespace {
29 class CryptoRandTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp();
34 void TearDown();
35 };
36
SetUpTestCase()37 void CryptoRandTest::SetUpTestCase() {}
TearDownTestCase()38 void CryptoRandTest::TearDownTestCase() {}
39
SetUp()40 void CryptoRandTest::SetUp() // add init here, this will be called before test.
41 {
42 }
43
TearDown()44 void CryptoRandTest::TearDown() // add destroy here, this will be called when test case done.
45 {
46 }
47
48 /**
49 * @tc.name: CryptoFrameworkRandTest.CryptoFrameworkRandCreateTest001
50 * @tc.desc: Verify that the creation of the random obj is normal.
51 * @tc.type: FUNC
52 * @tc.require: I5QWEN
53 */
54 HWTEST_F(CryptoRandTest, CryptoFrameworkRandCreateTest001, TestSize.Level0)
55 {
56 HcfResult ret = HcfRandCreate(nullptr);
57 EXPECT_NE(ret, HCF_SUCCESS);
58 }
59
60 HWTEST_F(CryptoRandTest, CryptoFrameworkRandGenerateTest001, TestSize.Level0)
61 {
62 // create a rand obj
63 HcfRand *randObj = nullptr;
64 HcfResult ret = HcfRandCreate(&randObj);
65 ASSERT_EQ(ret, HCF_SUCCESS);
66 // preset params
67 int32_t randomLen = 0;
68 // define randomBlob and seedBlob
69 struct HcfBlob randomBlob = {0};
70 // test generate random with length 0
71 ret = randObj->generateRandom(randObj, randomLen, &randomBlob);
72 EXPECT_NE(ret, HCF_SUCCESS);
73 HcfObjDestroy(randObj);
74 }
75
76 HWTEST_F(CryptoRandTest, CryptoFrameworkRandGenerateTest002, TestSize.Level0)
77 {
78 // create a rand obj
79 HcfRand *randObj = nullptr;
80 HcfResult ret = HcfRandCreate(&randObj);
81 ASSERT_EQ(ret, HCF_SUCCESS);
82 // preset params
83 int32_t randomLen = 32;
84 // define randomBlob and seedBlob
85 struct HcfBlob randomBlob = {0};
86 // test generate random
87 ret = randObj->generateRandom(randObj, randomLen, &randomBlob);
88 EXPECT_EQ(ret, HCF_SUCCESS);
89 // destroy the API obj and blob data
90 HcfBlobDataClearAndFree(&randomBlob);
91 HcfObjDestroy(randObj);
92 }
93
94 HWTEST_F(CryptoRandTest, CryptoFrameworkRandGenerateTest003, TestSize.Level0)
95 {
96 // create a rand obj
97 HcfRand *randObj = nullptr;
98 HcfResult ret = HcfRandCreate(&randObj);
99 ASSERT_EQ(ret, HCF_SUCCESS);
100 // preset params
101 int32_t randomLen = 8192;
102 // define randomBlob and seedBlob
103 struct HcfBlob randomBlob = {0};
104 // test generate random
105 ret = randObj->generateRandom(randObj, randomLen, &randomBlob);
106 EXPECT_EQ(ret, HCF_SUCCESS);
107 // destroy the API obj and blob data
108 HcfBlobDataClearAndFree(&randomBlob);
109 HcfObjDestroy(randObj);
110 }
111
112 HWTEST_F(CryptoRandTest, CryptoFrameworkRandGenerateTest004, TestSize.Level0)
113 {
114 // create a rand obj
115 HcfRand *randObj = nullptr;
116 HcfResult ret = HcfRandCreate(&randObj);
117 ASSERT_EQ(ret, HCF_SUCCESS);
118 // preset params
119 int32_t randomLen = 8193;
120 // define randomBlob and seedBlob
121 struct HcfBlob randomBlob = {0};
122 // test generate random
123 ret = randObj->generateRandom(randObj, randomLen, &randomBlob);
124 EXPECT_NE(ret, HCF_SUCCESS);
125 // destroy the API obj and blob data
126 HcfObjDestroy(randObj);
127 }
128
129 HWTEST_F(CryptoRandTest, CryptoFrameworkSetSeedTest001, TestSize.Level0)
130 {
131 // create a rand obj
132 HcfRand *randObj = nullptr;
133 HcfResult ret = HcfRandCreate(&randObj);
134 ASSERT_EQ(ret, HCF_SUCCESS);
135 // define randomBlob and seedBlob
136 struct HcfBlob *seedBlob = nullptr;
137 // test set seed
138 ret = randObj->setSeed(randObj, seedBlob);
139 EXPECT_NE(ret, HCF_SUCCESS);
140 // destroy the API obj and blob data
141 HcfObjDestroy(randObj);
142 }
143
144 HWTEST_F(CryptoRandTest, CryptoFrameworkSetSeedTest002, TestSize.Level0)
145 {
146 // create a rand obj
147 HcfRand *randObj = nullptr;
148 HcfResult ret = HcfRandCreate(&randObj);
149 ASSERT_EQ(ret, HCF_SUCCESS);
150 // preset params
151 int32_t seedLen = 32;
152 // define randomBlob and seedBlob
153 struct HcfBlob seedBlob = { .data = nullptr, .len = 0 };
154 // test generate seed
155 ret = randObj->generateRandom(randObj, seedLen, &seedBlob);
156 EXPECT_EQ(ret, HCF_SUCCESS);
157 // test set seed
158 ret = randObj->setSeed(randObj, &seedBlob);
159 EXPECT_EQ(ret, HCF_SUCCESS);
160 // destroy the API obj and blob data
161 HcfBlobDataClearAndFree(&seedBlob);
162 HcfObjDestroy(randObj);
163 }
164
165 HWTEST_F(CryptoRandTest, CryptoFrameworkSetSeedTest003, TestSize.Level0)
166 {
167 // create a rand obj
168 HcfRand *randObj = nullptr;
169 HcfResult ret = HcfRandCreate(&randObj);
170 ASSERT_EQ(ret, HCF_SUCCESS);
171 // preset params
172 int32_t seedLen = 1000;
173 // define randomBlob and seedBlob
174 struct HcfBlob seedBlob = { .data = nullptr, .len = 0 };
175 // test generate seed
176 ret = randObj->generateRandom(randObj, seedLen, &seedBlob);
177 EXPECT_EQ(ret, HCF_SUCCESS);
178 // test set seed
179 ret = randObj->setSeed(randObj, &seedBlob);
180 EXPECT_EQ(ret, HCF_SUCCESS);
181 // destroy the API obj and blob data
182 HcfBlobDataClearAndFree(&seedBlob);
183 HcfObjDestroy(randObj);
184 }
185
GetInvalidRandClass(void)186 static const char *GetInvalidRandClass(void)
187 {
188 return "INVALID_RAND_CLASS";
189 }
190
191 HWTEST_F(CryptoRandTest, NullInputRandTest001, TestSize.Level0)
192 {
193 HcfResult ret = HcfRandSpiCreate(nullptr);
194 EXPECT_NE(ret, HCF_SUCCESS);
195 }
196
197 HWTEST_F(CryptoRandTest, InvalidSpiClassRandTest001, TestSize.Level0)
198 {
199 HcfRandSpi *spiObj = nullptr;
200 HcfRandSpi invalidSpi = {{0}};
201 invalidSpi.base.getClass = GetInvalidRandClass;
202 HcfResult ret = HcfRandSpiCreate(&spiObj);
203 ASSERT_EQ(ret, HCF_SUCCESS);
204 ASSERT_NE(spiObj, nullptr);
205 (void)spiObj->base.destroy(nullptr);
206 (void)spiObj->base.destroy(&(invalidSpi.base));
207 HcfObjDestroy(spiObj);
208 }
209 }