• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = INT_MAX;
102     // define randomBlob and seedBlob
103     struct HcfBlob randomBlob = {0};
104     // test generate random
105     (void)randObj->generateRandom(randObj, randomLen, &randomBlob);
106     // destroy the API obj and blob data
107     HcfBlobDataClearAndFree(&randomBlob);
108     HcfObjDestroy(randObj);
109 }
110 
111 HWTEST_F(CryptoRandTest, CryptoFrameworkSetSeedTest001, TestSize.Level0)
112 {
113     // create a rand obj
114     HcfRand *randObj = nullptr;
115     HcfResult ret = HcfRandCreate(&randObj);
116     ASSERT_EQ(ret, HCF_SUCCESS);
117     // define randomBlob and seedBlob
118     struct HcfBlob *seedBlob = nullptr;
119     // test set seed
120     ret = randObj->setSeed(randObj, seedBlob);
121     EXPECT_NE(ret, HCF_SUCCESS);
122     // destroy the API obj and blob data
123     HcfObjDestroy(randObj);
124 }
125 
126 HWTEST_F(CryptoRandTest, CryptoFrameworkSetSeedTest002, TestSize.Level0)
127 {
128     // create a rand obj
129     HcfRand *randObj = nullptr;
130     HcfResult ret = HcfRandCreate(&randObj);
131     ASSERT_EQ(ret, HCF_SUCCESS);
132     // preset params
133     int32_t seedLen = 32;
134     // define randomBlob and seedBlob
135     struct HcfBlob seedBlob = { .data = nullptr, .len = 0 };
136     // test generate seed
137     ret = randObj->generateRandom(randObj, seedLen, &seedBlob);
138     EXPECT_EQ(ret, HCF_SUCCESS);
139     // test set seed
140     ret = randObj->setSeed(randObj, &seedBlob);
141     EXPECT_EQ(ret, HCF_SUCCESS);
142     // destroy the API obj and blob data
143     HcfBlobDataClearAndFree(&seedBlob);
144     HcfObjDestroy(randObj);
145 }
146 
147 HWTEST_F(CryptoRandTest, CryptoFrameworkSetSeedTest003, TestSize.Level0)
148 {
149     // create a rand obj
150     HcfRand *randObj = nullptr;
151     HcfResult ret = HcfRandCreate(&randObj);
152     ASSERT_EQ(ret, HCF_SUCCESS);
153     // preset params
154     int32_t seedLen = 1000;
155     // define randomBlob and seedBlob
156     struct HcfBlob seedBlob = { .data = nullptr, .len = 0 };
157     // test generate seed
158     ret = randObj->generateRandom(randObj, seedLen, &seedBlob);
159     EXPECT_EQ(ret, HCF_SUCCESS);
160     // test set seed
161     ret = randObj->setSeed(randObj, &seedBlob);
162     EXPECT_EQ(ret, HCF_SUCCESS);
163     // destroy the API obj and blob data
164     HcfBlobDataClearAndFree(&seedBlob);
165     HcfObjDestroy(randObj);
166 }
167 
GetInvalidRandClass(void)168 static const char *GetInvalidRandClass(void)
169 {
170     return "INVALID_RAND_CLASS";
171 }
172 
173 HWTEST_F(CryptoRandTest, NullInputRandTest001, TestSize.Level0)
174 {
175     HcfResult ret = HcfRandSpiCreate(nullptr);
176     EXPECT_NE(ret, HCF_SUCCESS);
177 }
178 
179 HWTEST_F(CryptoRandTest, NullParamRandTest001, TestSize.Level0)
180 {
181     HcfRand *randObj = nullptr;
182     HcfResult ret = HcfRandCreate(&randObj);
183     ASSERT_EQ(ret, HCF_SUCCESS);
184     ret = randObj->generateRandom(nullptr, 0, nullptr);
185     EXPECT_NE(ret, HCF_SUCCESS);
186     ret = randObj->setSeed(nullptr, nullptr);
187     EXPECT_NE(ret, HCF_SUCCESS);
188     randObj->base.destroy(nullptr);
189     HcfObjDestroy(randObj);
190 }
191 
192 HWTEST_F(CryptoRandTest, InvalidFrameworkClassRandTest001, TestSize.Level0)
193 {
194     HcfRand *randObj = nullptr;
195     HcfResult ret = HcfRandCreate(&randObj);
196     ASSERT_EQ(ret, HCF_SUCCESS);
197     HcfRand invalidRandObj = {{0}};
198     invalidRandObj.base.getClass = GetInvalidRandClass;
199     int32_t randomLen = 32;
200     struct HcfBlob randomBlob = { .data = nullptr, .len = 0 };
201     ret = randObj->generateRandom(&invalidRandObj, randomLen, &randomBlob);
202     EXPECT_NE(ret, HCF_SUCCESS);
203     ret = randObj->setSeed(&invalidRandObj, &randomBlob);
204     EXPECT_NE(ret, HCF_SUCCESS);
205     HcfBlobDataClearAndFree(&randomBlob);
206     randObj->base.destroy(&(invalidRandObj.base));
207     HcfObjDestroy(randObj);
208 }
209 
210 HWTEST_F(CryptoRandTest, InvalidSpiClassRandTest001, TestSize.Level0)
211 {
212     HcfRandSpi *spiObj = nullptr;
213     HcfRandSpi invalidSpi = {{0}};
214     invalidSpi.base.getClass = GetInvalidRandClass;
215     HcfResult ret = HcfRandSpiCreate(&spiObj);
216     ASSERT_EQ(ret, HCF_SUCCESS);
217     ASSERT_NE(spiObj, nullptr);
218     (void)spiObj->base.destroy(nullptr);
219     (void)spiObj->base.destroy(&(invalidSpi.base));
220     HcfObjDestroy(spiObj);
221 }
222 
223 HWTEST_F(CryptoRandTest, CryptoFrameworkGetAlgTest001, TestSize.Level0)
224 {
225     HcfRand *randObj = nullptr;
226     HcfResult ret = HcfRandCreate(&randObj);
227     ASSERT_EQ(ret, HCF_SUCCESS);
228 
229     const char *algoName = randObj->getAlgoName(randObj);
230     EXPECT_NE(algoName, nullptr);
231 
232     HcfObjDestroy(randObj);
233 }
234 
235 HWTEST_F(CryptoRandTest, InvalidSpiGetAlgTest001, TestSize.Level0)
236 {
237     HcfRand *randObj = nullptr;
238     HcfResult ret = HcfRandCreate(&randObj);
239     ASSERT_EQ(ret, HCF_SUCCESS);
240 
241     const char *algoName = randObj->getAlgoName(nullptr);
242     EXPECT_EQ(algoName, nullptr);
243 
244     HcfObjDestroy(randObj);
245 }
246 }