1 /*
2 * Copyright (c) 2023 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
18 #include "cf_log.h"
19 #include "cf_memory.h"
20 #include "cf_result.h"
21 #include "cf_type.h"
22 #include "utils.h"
23
24 using namespace testing::ext;
25 namespace {
26 constexpr uint32_t TEST_DEFAULT_SIZE = 10;
27 constexpr uint32_t TEST_DEFAULT_COUNT = 2;
28 class CfCommonTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31
32 static void TearDownTestCase(void);
33
34 void SetUp();
35
36 void TearDown();
37 };
38
SetUpTestCase(void)39 void CfCommonTest::SetUpTestCase(void)
40 {
41 }
42
TearDownTestCase(void)43 void CfCommonTest::TearDownTestCase(void)
44 {
45 }
46
SetUp()47 void CfCommonTest::SetUp()
48 {
49 }
50
TearDown()51 void CfCommonTest::TearDown()
52 {
53 }
54
55 /**
56 * @tc.name: CfBlobDataFree001
57 * @tc.desc: CfBlobDataFree normal case
58 * @tc.type: FUNC
59 * @tc.require: AR000HS2RB /SR000HS2Q1
60 */
61 HWTEST_F(CfCommonTest, CfBlobDataFree001, TestSize.Level0)
62 {
63 CfBlobDataFree(nullptr);
64 CfBlob blobNull = { 0, nullptr };
65 CfBlobDataFree(&blobNull);
66 CfBlob blob = { TEST_DEFAULT_SIZE, nullptr };
67 blob.data = static_cast<uint8_t *>(CfMalloc(blob.size, 0));
68 ASSERT_NE(blob.data, nullptr);
69 CfBlobDataFree(&blob);
70 }
71
72 /**
73 * @tc.name: CfBlobDataClearAndFree001
74 * @tc.desc: CfBlobDataClearAndFree normal case
75 * @tc.type: FUNC
76 * @tc.require: AR000HS2RB /SR000HS2Q1
77 */
78 HWTEST_F(CfCommonTest, CfBlobDataClearAndFree001, TestSize.Level0)
79 {
80 CfBlobDataClearAndFree(nullptr);
81 CfBlob blobNull = { 0, nullptr };
82 CfBlobDataClearAndFree(&blobNull);
83 CfBlob blob = { TEST_DEFAULT_SIZE, nullptr };
84 blob.data = static_cast<uint8_t *>(CfMalloc(blob.size, 0));
85 ASSERT_NE(blob.data, nullptr);
86 CfBlobDataClearAndFree(&blob);
87 }
88
89 /**
90 * @tc.name: CfEncodingBlobDataFree001
91 * @tc.desc: CfEncodingBlobDataFree normal case
92 * @tc.type: FUNC
93 * @tc.require: AR000HS2RB /SR000HS2Q1
94 */
95 HWTEST_F(CfCommonTest, CfEncodingBlobDataFree001, TestSize.Level0)
96 {
97 CfEncodingBlobDataFree(nullptr);
98 CfEncodingBlob blobNull = { nullptr, 0, CF_FORMAT_DER };
99 CfEncodingBlobDataFree(&blobNull);
100 CfEncodingBlob blob = { nullptr, TEST_DEFAULT_SIZE, CF_FORMAT_DER };
101 blob.data = static_cast<uint8_t *>(CfMalloc(blob.len, 0));
102 ASSERT_NE(blob.data, nullptr);
103 CfEncodingBlobDataFree(&blob);
104 }
105
106 /**
107 * @tc.name: CfArrayDataClearAndFree01
108 * @tc.desc: CfArrayDataClearAndFree normal case
109 * @tc.type: FUNC
110 * @tc.require: AR000HS2RB /SR000HS2Q1
111 */
112 HWTEST_F(CfCommonTest, CfArrayDataClearAndFree001, TestSize.Level0)
113 {
114 CfArrayDataClearAndFree(nullptr);
115 CfArray array = { nullptr, CF_FORMAT_DER, TEST_DEFAULT_COUNT };
116 array.data = static_cast<CfBlob *>(CfMalloc(array.count * sizeof(CfBlob), 0));
117 ASSERT_NE(array.data, nullptr);
118
119 for (uint32_t i = 0; i < array.count; ++i) {
120 array.data[i].size = TEST_DEFAULT_SIZE;
121 array.data[i].data = static_cast<uint8_t *>(CfMalloc(array.data[i].size, 0));
122 ASSERT_NE(array.data[i].data, nullptr);
123 }
124
125 CfArrayDataClearAndFree(&array);
126 }
127
128 /**
129 * @tc.name: FreeCfBlobArray002
130 * @tc.desc: FreeCfBlobArray normal case
131 * @tc.type: FUNC
132 * @tc.require: AR000HS2RB /SR000HS2Q1
133 */
134 HWTEST_F(CfCommonTest, FreeCfBlobArray002, TestSize.Level0)
135 {
136 FreeCfBlobArray(nullptr, 0);
137 CfBlob *array = static_cast<CfBlob *>(CfMalloc(TEST_DEFAULT_COUNT * sizeof(CfBlob), 0));
138 ASSERT_NE(array, nullptr);
139
140 FreeCfBlobArray(array, TEST_DEFAULT_COUNT);
141 }
142
143 /**
144 * @tc.name: FreeCfBlobArray003
145 * @tc.desc: FreeCfBlobArray normal case 2
146 * @tc.type: FUNC
147 * @tc.require: AR000HS2RB /SR000HS2Q1
148 */
149 HWTEST_F(CfCommonTest, FreeCfBlobArray003, TestSize.Level0)
150 {
151 CF_LOG_W("this is test for log Warn");
152 CF_LOG_I("this is test for log Info");
153 CF_LOG_E("this is test for log Error");
154 CF_LOG_D("this is test for log Debug");
155 CF_LOG_W("MoreThan512Bytes................................................"
156 "................................................................"
157 "................................................................"
158 "................................................................"
159 "................................................................"
160 "................................................................"
161 "................................................................"
162 "..................................................................");
163 CfBlob *array = static_cast<CfBlob *>(CfMalloc(TEST_DEFAULT_COUNT * sizeof(CfBlob), 0));
164 ASSERT_NE(array, nullptr);
165
166 for (uint32_t i = 0; i < TEST_DEFAULT_COUNT; ++i) {
167 array[i].size = TEST_DEFAULT_SIZE;
168 array[i].data = static_cast<uint8_t *>(CfMalloc(array[i].size, 0));
169 ASSERT_NE(array[i].data, nullptr);
170 }
171
172 FreeCfBlobArray(array, TEST_DEFAULT_COUNT);
173 }
174
175 /**
176 * @tc.name: CfMemTest001
177 * @tc.desc: malloc and free normal
178 * @tc.type: FUNC
179 * @tc.require: AR000HS2RB /SR000HS2Q1
180 */
181 HWTEST_F(CfCommonTest, CfMemTest001, TestSize.Level0)
182 {
183 CfFree(nullptr);
184 uint8_t *buf = static_cast<uint8_t *>(CfMalloc(TEST_DEFAULT_SIZE, 0));
185 ASSERT_NE(buf, nullptr);
186 CfFree(buf);
187 }
188
189 /**
190 * @tc.name: CfMemTest002
191 * @tc.desc: malloc 0
192 * @tc.type: FUNC
193 * @tc.require: AR000HS2RB /SR000HS2Q1
194 */
195 HWTEST_F(CfCommonTest, CfMemTest002, TestSize.Level0)
196 {
197 uint8_t *buf = static_cast<uint8_t *>(CfMalloc(0, 0));
198 ASSERT_EQ(buf, nullptr);
199 }
200
201 /**
202 * @tc.name: CfMemTest003
203 * @tc.desc: malloc more than MAX_MEMORY_SIZE
204 * @tc.type: FUNC
205 * @tc.require: AR000HS2RB /SR000HS2Q1
206 */
207 HWTEST_F(CfCommonTest, CfMemTest003, TestSize.Level0)
208 {
209 uint8_t *buf = static_cast<uint8_t *>(CfMalloc(MAX_MEMORY_SIZE + 1, 0));
210 ASSERT_EQ(buf, nullptr);
211 }
212
213 /**
214 * @tc.name: IsStrValid001
215 * @tc.desc: str is nullptr
216 * @tc.type: FUNC
217 * @tc.require: AR000HS2RB /SR000HS2Q1
218 */
219 HWTEST_F(CfCommonTest, IsStrValid001, TestSize.Level0)
220 {
221 bool checkRes = CfIsStrValid(nullptr, 0);
222 EXPECT_EQ(checkRes, false);
223 }
224
225 /**
226 * @tc.name: IsStrValid002
227 * @tc.desc: len invalid
228 * @tc.type: FUNC
229 * @tc.require: AR000HS2RB /SR000HS2Q1
230 */
231 HWTEST_F(CfCommonTest, IsStrValid002, TestSize.Level0)
232 {
233 char str[] = "this is test for beyond max length.";
234 bool checkRes = CfIsStrValid(str, TEST_DEFAULT_SIZE);
235 EXPECT_EQ(checkRes, false);
236 }
237
238 /**
239 * @tc.name: IsStrValid003
240 * @tc.desc: normal case
241 * @tc.type: FUNC
242 * @tc.require: AR000HS2RB /SR000HS2Q1
243 */
244 HWTEST_F(CfCommonTest, IsStrValid003, TestSize.Level0)
245 {
246 char str[] = "123456789";
247 bool checkRes = CfIsStrValid(str, TEST_DEFAULT_SIZE);
248 EXPECT_EQ(checkRes, true);
249 }
250
251 /**
252 * @tc.name: IsBlobValid001
253 * @tc.desc: normal case
254 * @tc.type: FUNC
255 * @tc.require: AR000HS2RB /SR000HS2Q1
256 */
257 HWTEST_F(CfCommonTest, IsBlobValid001, TestSize.Level0)
258 {
259 uint8_t blobData[] = "normal case";
260 CfBlob blob = { sizeof(blobData), blobData };
261 bool checkRes = CfIsBlobValid(&blob);
262 EXPECT_EQ(checkRes, true);
263 }
264
265 /**
266 * @tc.name: IsBlobValid002
267 * @tc.desc: blob is nullptr
268 * @tc.type: FUNC
269 * @tc.require: AR000HS2RB /SR000HS2Q1
270 */
271 HWTEST_F(CfCommonTest, IsBlobValid002, TestSize.Level0)
272 {
273 bool checkRes = CfIsBlobValid(nullptr);
274 EXPECT_EQ(checkRes, false);
275 }
276
277 /**
278 * @tc.name: IsBlobValid003
279 * @tc.desc: blob data is nullptr
280 * @tc.type: FUNC
281 * @tc.require: AR000HS2RB /SR000HS2Q1
282 */
283 HWTEST_F(CfCommonTest, IsBlobValid003, TestSize.Level0)
284 {
285 CfBlob blob = { TEST_DEFAULT_SIZE, nullptr };
286 bool checkRes = CfIsBlobValid(&blob);
287 EXPECT_EQ(checkRes, false);
288 }
289
290 /**
291 * @tc.name: IsBlobValid004
292 * @tc.desc: blob size is 0
293 * @tc.type: FUNC
294 * @tc.require: AR000HS2RB /SR000HS2Q1
295 */
296 HWTEST_F(CfCommonTest, IsBlobValid004, TestSize.Level0)
297 {
298 uint8_t blobData[] = "invalid blob size is 0";
299 CfBlob blob = { 0, blobData };
300 bool checkRes = CfIsBlobValid(&blob);
301 EXPECT_EQ(checkRes, false);
302 }
303
GetClass(void)304 static const char *GetClass(void)
305 {
306 return "TEST_FOR_GET_CLASS";
307 }
308
GetClassNull(void)309 static const char *GetClassNull(void)
310 {
311 return nullptr;
312 }
313
314 /**
315 * @tc.name: IsClassMatch001
316 * @tc.desc: obj is nullptr
317 * @tc.type: FUNC
318 * @tc.require: AR000HS2RB /SR000HS2Q1
319 */
320 HWTEST_F(CfCommonTest, IsClassMatch001, TestSize.Level0)
321 {
322 bool checkRes = CfIsClassMatch(nullptr, "TEST_FOR_GET_CLASS");
323 EXPECT_EQ(checkRes, false);
324 }
325
326 /**
327 * @tc.name: IsClassMatch002
328 * @tc.desc: obj->getClass() is nullptr
329 * @tc.type: FUNC
330 * @tc.require: AR000HS2RB /SR000HS2Q1
331 */
332 HWTEST_F(CfCommonTest, IsClassMatch002, TestSize.Level0)
333 {
334 CfObjectBase obj = { GetClassNull, nullptr };
335 bool checkRes = CfIsClassMatch(&obj, "TEST_FOR_GET_CLASS");
336 EXPECT_EQ(checkRes, false);
337 }
338
339 /**
340 * @tc.name: IsClassMatch003
341 * @tc.desc: class is nullptr
342 * @tc.type: FUNC
343 * @tc.require: AR000HS2RB /SR000HS2Q1
344 */
345 HWTEST_F(CfCommonTest, IsClassMatch003, TestSize.Level0)
346 {
347 CfObjectBase obj = { GetClass, nullptr };
348 bool checkRes = CfIsClassMatch(&obj, nullptr);
349 EXPECT_EQ(checkRes, false);
350 }
351
352 /**
353 * @tc.name: IsClassMatch004
354 * @tc.desc: normal case
355 * @tc.type: FUNC
356 * @tc.require: AR000HS2RB /SR000HS2Q1
357 */
358 HWTEST_F(CfCommonTest, IsClassMatch004, TestSize.Level0)
359 {
360 CfObjectBase obj = { GetClass, nullptr };
361 bool checkRes = CfIsClassMatch(&obj, "TEST_FOR_GET_CLASS");
362 EXPECT_EQ(checkRes, true);
363 }
364
365 /**
366 * @tc.name: IsClassMatch005
367 * @tc.desc: class not equal
368 * @tc.type: FUNC
369 * @tc.require: AR000HS2RB /SR000HS2Q1
370 */
371 HWTEST_F(CfCommonTest, IsClassMatch005, TestSize.Level0)
372 {
373 CfObjectBase obj = { GetClass, nullptr };
374 bool checkRes = CfIsClassMatch(&obj, "TEST_FOR_GET_CLASS123");
375 EXPECT_EQ(checkRes, false);
376 }
377
378 /**
379 * @tc.name: IsPubKeyClassMatch001
380 * @tc.desc: normal case
381 * @tc.type: FUNC
382 * @tc.require: AR000HS2RB /SR000HS2Q1
383 */
384 HWTEST_F(CfCommonTest, IsPubKeyClassMatch001, TestSize.Level0)
385 {
386 HcfObjectBase obj = { GetClass, nullptr };
387 bool checkRes = CfIsPubKeyClassMatch(&obj, "TEST_FOR_GET_CLASS");
388 EXPECT_EQ(checkRes, true);
389 }
390
391 /**
392 * @tc.name: IsPubKeyClassMatch002
393 * @tc.desc: class not equal
394 * @tc.type: FUNC
395 * @tc.require: AR000HS2RB /SR000HS2Q1
396 */
397 HWTEST_F(CfCommonTest, IsPubKeyClassMatch002, TestSize.Level0)
398 {
399 HcfObjectBase obj = { GetClass, nullptr };
400 bool checkRes = CfIsPubKeyClassMatch(&obj, "TEST_FOR_GET_CLASS000");
401 EXPECT_EQ(checkRes, false);
402 }
403
404 /**
405 * @tc.name: IsPubKeyClassMatch003
406 * @tc.desc: obj is nullptr
407 * @tc.type: FUNC
408 * @tc.require: AR000HS2RB /SR000HS2Q1
409 */
410 HWTEST_F(CfCommonTest, IsPubKeyClassMatch003, TestSize.Level0)
411 {
412 bool checkRes = CfIsPubKeyClassMatch(nullptr, "TEST_FOR_GET_CLASS");
413 EXPECT_EQ(checkRes, false);
414 }
415
416 /**
417 * @tc.name: IsPubKeyClassMatch004
418 * @tc.desc: obj->getClass() is nullptr
419 * @tc.type: FUNC
420 * @tc.require: AR000HS2RB /SR000HS2Q1
421 */
422 HWTEST_F(CfCommonTest, IsPubKeyClassMatch004, TestSize.Level0)
423 {
424 HcfObjectBase obj = { GetClassNull, nullptr };
425 bool checkRes = CfIsPubKeyClassMatch(&obj, "TEST_FOR_GET_CLASS");
426 EXPECT_EQ(checkRes, false);
427 }
428
429 /**
430 * @tc.name: IsPubKeyClassMatch005
431 * @tc.desc: class is nullptr
432 * @tc.type: FUNC
433 * @tc.require: AR000HS2RB /SR000HS2Q1
434 */
435 HWTEST_F(CfCommonTest, IsPubKeyClassMatch005, TestSize.Level0)
436 {
437 HcfObjectBase obj = { GetClass, nullptr };
438 bool checkRes = CfIsPubKeyClassMatch(&obj, nullptr);
439 EXPECT_EQ(checkRes, false);
440 }
441 } // end of namespace
442