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 blob is nullptr
58 * @tc.type: FUNC
59 * @tc.require: AR000HS2RB /SR000HS2Q1
60 */
61 HWTEST_F(CfCommonTest, CfBlobDataFree001, TestSize.Level0)
62 {
63     CfBlobDataFree(nullptr);
64 }
65 
66 /**
67 * @tc.name: CfBlobDataFree002
68 * @tc.desc: CfBlobDataFree blob.data is nullptr
69 * @tc.type: FUNC
70 * @tc.require: AR000HS2RB /SR000HS2Q1
71 */
72 HWTEST_F(CfCommonTest, CfBlobDataFree002, TestSize.Level0)
73 {
74     CfBlob blob = { 0, nullptr };
75     CfBlobDataFree(&blob);
76 }
77 
78 /**
79 * @tc.name: CfBlobDataFree003
80 * @tc.desc: CfBlobDataFree normal case
81 * @tc.type: FUNC
82 * @tc.require: AR000HS2RB /SR000HS2Q1
83 */
84 HWTEST_F(CfCommonTest, CfBlobDataFree003, TestSize.Level0)
85 {
86     CfBlob blob = { TEST_DEFAULT_SIZE, nullptr };
87     blob.data = static_cast<uint8_t *>(CfMalloc(blob.size, 0));
88     ASSERT_NE(blob.data, nullptr);
89     CfBlobDataFree(&blob);
90 }
91 
92 /**
93 * @tc.name: CfBlobDataClearAndFree001
94 * @tc.desc: CfBlobDataClearAndFree blob is nullptr
95 * @tc.type: FUNC
96 * @tc.require: AR000HS2RB /SR000HS2Q1
97 */
98 HWTEST_F(CfCommonTest, CfBlobDataClearAndFree001, TestSize.Level0)
99 {
100     CfBlobDataClearAndFree(nullptr);
101 }
102 
103 /**
104 * @tc.name: CfBlobDataClearAndFree002
105 * @tc.desc: CfBlobDataClearAndFree blob.data is nullptr
106 * @tc.type: FUNC
107 * @tc.require: AR000HS2RB /SR000HS2Q1
108 */
109 HWTEST_F(CfCommonTest, CfBlobDataClearAndFree002, TestSize.Level0)
110 {
111     CfBlob blob = { 0, nullptr };
112     CfBlobDataClearAndFree(&blob);
113 }
114 
115 /**
116 * @tc.name: CfBlobDataClearAndFree003
117 * @tc.desc: CfBlobDataClearAndFree normal case
118 * @tc.type: FUNC
119 * @tc.require: AR000HS2RB /SR000HS2Q1
120 */
121 HWTEST_F(CfCommonTest, CfBlobDataClearAndFree003, TestSize.Level0)
122 {
123     CfBlob blob = { TEST_DEFAULT_SIZE, nullptr };
124     blob.data = static_cast<uint8_t *>(CfMalloc(blob.size, 0));
125     ASSERT_NE(blob.data, nullptr);
126     CfBlobDataClearAndFree(&blob);
127 }
128 
129 /**
130 * @tc.name: CfEncodingBlobDataFree001
131 * @tc.desc: CfEncodingBlobDataFree encodingBlob is nullptr
132 * @tc.type: FUNC
133 * @tc.require: AR000HS2RB /SR000HS2Q1
134 */
135 HWTEST_F(CfCommonTest, CfEncodingBlobDataFree001, TestSize.Level0)
136 {
137     CfEncodingBlobDataFree(nullptr);
138 }
139 
140 /**
141 * @tc.name: CfEncodingBlobDataFree001
142 * @tc.desc: CfEncodingBlobDataFree encodingBlob.data is nullptr
143 * @tc.type: FUNC
144 * @tc.require: AR000HS2RB /SR000HS2Q1
145 */
146 HWTEST_F(CfCommonTest, CfEncodingBlobDataFree002, TestSize.Level0)
147 {
148     CfEncodingBlob blob = { nullptr, 0, CF_FORMAT_DER };
149     CfEncodingBlobDataFree(&blob);
150 }
151 
152 /**
153 * @tc.name: CfEncodingBlobDataFree003
154 * @tc.desc: CfEncodingBlobDataFree normal case
155 * @tc.type: FUNC
156 * @tc.require: AR000HS2RB /SR000HS2Q1
157 */
158 HWTEST_F(CfCommonTest, CfEncodingBlobDataFree003, TestSize.Level0)
159 {
160     CfEncodingBlob blob = { nullptr, TEST_DEFAULT_SIZE, CF_FORMAT_DER };
161     blob.data = static_cast<uint8_t *>(CfMalloc(blob.len, 0));
162     ASSERT_NE(blob.data, nullptr);
163     CfEncodingBlobDataFree(&blob);
164 }
165 
166 /**
167 * @tc.name: CfArrayDataClearAndFree001
168 * @tc.desc: CfArrayDataClearAndFree array is nullptr
169 * @tc.type: FUNC
170 * @tc.require: AR000HS2RB /SR000HS2Q1
171 */
172 HWTEST_F(CfCommonTest, CfArrayDataClearAndFree001, TestSize.Level0)
173 {
174     CfArrayDataClearAndFree(nullptr);
175 }
176 
177 /**
178 * @tc.name: CfArrayDataClearAndFree002
179 * @tc.desc: CfArrayDataClearAndFree normal case
180 * @tc.type: FUNC
181 * @tc.require: AR000HS2RB /SR000HS2Q1
182 */
183 HWTEST_F(CfCommonTest, CfArrayDataClearAndFree002, TestSize.Level0)
184 {
185     CfArray array = { nullptr, CF_FORMAT_DER, TEST_DEFAULT_COUNT };
186     array.data = static_cast<CfBlob *>(CfMalloc(array.count * sizeof(CfBlob), 0));
187     ASSERT_NE(array.data, nullptr);
188 
189     for (uint32_t i = 0; i < array.count; ++i) {
190         array.data[i].size = TEST_DEFAULT_SIZE;
191         array.data[i].data = static_cast<uint8_t *>(CfMalloc(array.data[i].size, 0));
192         ASSERT_NE(array.data[i].data, nullptr);
193     }
194 
195     CfArrayDataClearAndFree(&array);
196 }
197 
198 /**
199 * @tc.name: FreeCfBlobArray001
200 * @tc.desc: FreeCfBlobArray array is nullptr
201 * @tc.type: FUNC
202 * @tc.require: AR000HS2RB /SR000HS2Q1
203 */
204 HWTEST_F(CfCommonTest, FreeCfBlobArray001, TestSize.Level0)
205 {
206     FreeCfBlobArray(nullptr, 0);
207 }
208 
209 /**
210 * @tc.name: FreeCfBlobArray002
211 * @tc.desc: FreeCfBlobArray normal case
212 * @tc.type: FUNC
213 * @tc.require: AR000HS2RB /SR000HS2Q1
214 */
215 HWTEST_F(CfCommonTest, FreeCfBlobArray002, TestSize.Level0)
216 {
217     CfBlob *array = static_cast<CfBlob *>(CfMalloc(TEST_DEFAULT_COUNT * sizeof(CfBlob), 0));
218     ASSERT_NE(array, nullptr);
219 
220     FreeCfBlobArray(array, TEST_DEFAULT_COUNT);
221 }
222 
223 /**
224 * @tc.name: FreeCfBlobArray003
225 * @tc.desc: FreeCfBlobArray normal case 2
226 * @tc.type: FUNC
227 * @tc.require: AR000HS2RB /SR000HS2Q1
228 */
229 HWTEST_F(CfCommonTest, FreeCfBlobArray003, TestSize.Level0)
230 {
231     CfBlob *array = static_cast<CfBlob *>(CfMalloc(TEST_DEFAULT_COUNT * sizeof(CfBlob), 0));
232     ASSERT_NE(array, nullptr);
233 
234     for (uint32_t i = 0; i < TEST_DEFAULT_COUNT; ++i) {
235         array[i].size = TEST_DEFAULT_SIZE;
236         array[i].data = static_cast<uint8_t *>(CfMalloc(array[i].size, 0));
237         ASSERT_NE(array[i].data, nullptr);
238     }
239 
240     FreeCfBlobArray(array, TEST_DEFAULT_COUNT);
241 }
242 
243 /**
244 * @tc.name: CfLogTest001
245 * @tc.desc: Test Log Warn
246 * @tc.type: FUNC
247 * @tc.require: AR000HS2RB /SR000HS2Q1
248 */
249 HWTEST_F(CfCommonTest, CfLogTest001, TestSize.Level0)
250 {
251     CF_LOG_W("this is test for log Warn");
252 }
253 
254 /**
255 * @tc.name: CfLogTest002
256 * @tc.desc: Test Log Info
257 * @tc.type: FUNC
258 * @tc.require: AR000HS2RB /SR000HS2Q1
259 */
260 HWTEST_F(CfCommonTest, CfLogTest002, TestSize.Level0)
261 {
262     CF_LOG_I("this is test for log Info");
263 }
264 
265 /**
266 * @tc.name: CfLogTest003
267 * @tc.desc: Test Log Error
268 * @tc.type: FUNC
269 * @tc.require: AR000HS2RB /SR000HS2Q1
270 */
271 HWTEST_F(CfCommonTest, CfLogTest003, TestSize.Level0)
272 {
273     CF_LOG_E("this is test for log Error");
274 }
275 
276 /**
277 * @tc.name: CfLogTest004
278 * @tc.desc: Test Log Debug
279 * @tc.type: FUNC
280 * @tc.require: AR000HS2RB /SR000HS2Q1
281 */
282 HWTEST_F(CfCommonTest, CfLogTest004, TestSize.Level0)
283 {
284     CF_LOG_D("this is test for log Debug");
285 }
286 
287 /**
288 * @tc.name: CfLogTest005
289 * @tc.desc: Test Log ID INVALID
290 * @tc.type: FUNC
291 * @tc.require: AR000HS2RB /SR000HS2Q1
292 */
293 HWTEST_F(CfCommonTest, CfLogTest005, TestSize.Level0)
294 {
295     CfLog(CF_LOG_LEVEL_D + 1, __func__, __LINE__, "this is test for default branch");
296 }
297 
298 /**
299 * @tc.name: CfLogTest006
300 * @tc.desc: Test Log info length more than 512
301 * @tc.type: FUNC
302 * @tc.require: AR000HS2RB /SR000HS2Q1
303 */
304 HWTEST_F(CfCommonTest, CfLogTest006, TestSize.Level0)
305 {
306     CF_LOG_W("MoreThan512Bytes................................................"
307         "................................................................"
308         "................................................................"
309         "................................................................"
310         "................................................................"
311         "................................................................"
312         "................................................................"
313         "..................................................................");
314 }
315 
316 /**
317 * @tc.name: CfMemTest001
318 * @tc.desc: malloc and free normal
319 * @tc.type: FUNC
320 * @tc.require: AR000HS2RB /SR000HS2Q1
321 */
322 HWTEST_F(CfCommonTest, CfMemTest001, TestSize.Level0)
323 {
324     uint8_t *buf = static_cast<uint8_t *>(CfMalloc(TEST_DEFAULT_SIZE, 0));
325     ASSERT_NE(buf, nullptr);
326     CfFree(buf);
327 }
328 
329 /**
330 * @tc.name: CfMemTest002
331 * @tc.desc: malloc 0
332 * @tc.type: FUNC
333 * @tc.require: AR000HS2RB /SR000HS2Q1
334 */
335 HWTEST_F(CfCommonTest, CfMemTest002, TestSize.Level0)
336 {
337     uint8_t *buf = static_cast<uint8_t *>(CfMalloc(0, 0));
338     ASSERT_EQ(buf, nullptr);
339 }
340 
341 /**
342 * @tc.name: CfMemTest003
343 * @tc.desc: malloc more than MAX_MEMORY_SIZE
344 * @tc.type: FUNC
345 * @tc.require: AR000HS2RB /SR000HS2Q1
346 */
347 HWTEST_F(CfCommonTest, CfMemTest003, TestSize.Level0)
348 {
349     uint8_t *buf = static_cast<uint8_t *>(CfMalloc(MAX_MEMORY_SIZE + 1, 0));
350     ASSERT_EQ(buf, nullptr);
351 }
352 
353 /**
354 * @tc.name: CfMemTest004
355 * @tc.desc: free nullptr
356 * @tc.type: FUNC
357 * @tc.require: AR000HS2RB /SR000HS2Q1
358 */
359 HWTEST_F(CfCommonTest, CfMemTest004, TestSize.Level0)
360 {
361     CfFree(nullptr);
362 }
363 
364 /**
365 * @tc.name: IsStrValid001
366 * @tc.desc: str is nullptr
367 * @tc.type: FUNC
368 * @tc.require: AR000HS2RB /SR000HS2Q1
369 */
370 HWTEST_F(CfCommonTest, IsStrValid001, TestSize.Level0)
371 {
372     bool checkRes = CfIsStrValid(nullptr, 0);
373     EXPECT_EQ(checkRes, false);
374 }
375 
376 /**
377 * @tc.name: IsStrValid002
378 * @tc.desc: len invalid
379 * @tc.type: FUNC
380 * @tc.require: AR000HS2RB /SR000HS2Q1
381 */
382 HWTEST_F(CfCommonTest, IsStrValid002, TestSize.Level0)
383 {
384     char str[] = "this is test for beyond max length.";
385     bool checkRes = CfIsStrValid(str, TEST_DEFAULT_SIZE);
386     EXPECT_EQ(checkRes, false);
387 }
388 
389 /**
390 * @tc.name: IsStrValid003
391 * @tc.desc: normal case
392 * @tc.type: FUNC
393 * @tc.require: AR000HS2RB /SR000HS2Q1
394 */
395 HWTEST_F(CfCommonTest, IsStrValid003, TestSize.Level0)
396 {
397     char str[] = "123456789";
398     bool checkRes = CfIsStrValid(str, TEST_DEFAULT_SIZE);
399     EXPECT_EQ(checkRes, true);
400 }
401 
402 /**
403 * @tc.name: IsBlobValid001
404 * @tc.desc: normal case
405 * @tc.type: FUNC
406 * @tc.require: AR000HS2RB /SR000HS2Q1
407 */
408 HWTEST_F(CfCommonTest, IsBlobValid001, TestSize.Level0)
409 {
410     uint8_t blobData[] = "normal case";
411     CfBlob blob = { sizeof(blobData), blobData };
412     bool checkRes = CfIsBlobValid(&blob);
413     EXPECT_EQ(checkRes, true);
414 }
415 
416 /**
417 * @tc.name: IsBlobValid002
418 * @tc.desc: blob is nullptr
419 * @tc.type: FUNC
420 * @tc.require: AR000HS2RB /SR000HS2Q1
421 */
422 HWTEST_F(CfCommonTest, IsBlobValid002, TestSize.Level0)
423 {
424     bool checkRes = CfIsBlobValid(nullptr);
425     EXPECT_EQ(checkRes, false);
426 }
427 
428 /**
429 * @tc.name: IsBlobValid003
430 * @tc.desc: blob data is nullptr
431 * @tc.type: FUNC
432 * @tc.require: AR000HS2RB /SR000HS2Q1
433 */
434 HWTEST_F(CfCommonTest, IsBlobValid003, TestSize.Level0)
435 {
436     CfBlob blob = { TEST_DEFAULT_SIZE, nullptr };
437     bool checkRes = CfIsBlobValid(&blob);
438     EXPECT_EQ(checkRes, false);
439 }
440 
441 /**
442 * @tc.name: IsBlobValid004
443 * @tc.desc: blob size is 0
444 * @tc.type: FUNC
445 * @tc.require: AR000HS2RB /SR000HS2Q1
446 */
447 HWTEST_F(CfCommonTest, IsBlobValid004, TestSize.Level0)
448 {
449     uint8_t blobData[] = "invalid blob size is 0";
450     CfBlob blob = { 0, blobData };
451     bool checkRes = CfIsBlobValid(&blob);
452     EXPECT_EQ(checkRes, false);
453 }
454 
GetClass(void)455 static const char *GetClass(void)
456 {
457     return "TEST_FOR_GET_CLASS";
458 }
459 
GetClassNull(void)460 static const char *GetClassNull(void)
461 {
462     return nullptr;
463 }
464 
465 /**
466 * @tc.name: IsClassMatch001
467 * @tc.desc: obj is nullptr
468 * @tc.type: FUNC
469 * @tc.require: AR000HS2RB /SR000HS2Q1
470 */
471 HWTEST_F(CfCommonTest, IsClassMatch001, TestSize.Level0)
472 {
473     bool checkRes = CfIsClassMatch(nullptr, "TEST_FOR_GET_CLASS");
474     EXPECT_EQ(checkRes, false);
475 }
476 
477 /**
478 * @tc.name: IsClassMatch002
479 * @tc.desc: obj->getClass() is nullptr
480 * @tc.type: FUNC
481 * @tc.require: AR000HS2RB /SR000HS2Q1
482 */
483 HWTEST_F(CfCommonTest, IsClassMatch002, TestSize.Level0)
484 {
485     CfObjectBase obj = { GetClassNull, nullptr };
486     bool checkRes = CfIsClassMatch(&obj, "TEST_FOR_GET_CLASS");
487     EXPECT_EQ(checkRes, false);
488 }
489 
490 /**
491 * @tc.name: IsClassMatch003
492 * @tc.desc: class is nullptr
493 * @tc.type: FUNC
494 * @tc.require: AR000HS2RB /SR000HS2Q1
495 */
496 HWTEST_F(CfCommonTest, IsClassMatch003, TestSize.Level0)
497 {
498     CfObjectBase obj = { GetClass, nullptr };
499     bool checkRes = CfIsClassMatch(&obj, nullptr);
500     EXPECT_EQ(checkRes, false);
501 }
502 
503 /**
504 * @tc.name: IsClassMatch004
505 * @tc.desc: normal case
506 * @tc.type: FUNC
507 * @tc.require: AR000HS2RB /SR000HS2Q1
508 */
509 HWTEST_F(CfCommonTest, IsClassMatch004, TestSize.Level0)
510 {
511     CfObjectBase obj = { GetClass, nullptr };
512     bool checkRes = CfIsClassMatch(&obj, "TEST_FOR_GET_CLASS");
513     EXPECT_EQ(checkRes, true);
514 }
515 
516 /**
517 * @tc.name: IsClassMatch005
518 * @tc.desc: class not equal
519 * @tc.type: FUNC
520 * @tc.require: AR000HS2RB /SR000HS2Q1
521 */
522 HWTEST_F(CfCommonTest, IsClassMatch005, TestSize.Level0)
523 {
524     CfObjectBase obj = { GetClass, nullptr };
525     bool checkRes = CfIsClassMatch(&obj, "TEST_FOR_GET_CLASS123");
526     EXPECT_EQ(checkRes, false);
527 }
528 
529 /**
530 * @tc.name: IsPubKeyClassMatch001
531 * @tc.desc: normal case
532 * @tc.type: FUNC
533 * @tc.require: AR000HS2RB /SR000HS2Q1
534 */
535 HWTEST_F(CfCommonTest, IsPubKeyClassMatch001, TestSize.Level0)
536 {
537     HcfObjectBase obj = { GetClass, nullptr };
538     bool checkRes = CfIsPubKeyClassMatch(&obj, "TEST_FOR_GET_CLASS");
539     EXPECT_EQ(checkRes, true);
540 }
541 
542 /**
543 * @tc.name: IsPubKeyClassMatch002
544 * @tc.desc: class not equal
545 * @tc.type: FUNC
546 * @tc.require: AR000HS2RB /SR000HS2Q1
547 */
548 HWTEST_F(CfCommonTest, IsPubKeyClassMatch002, TestSize.Level0)
549 {
550     HcfObjectBase obj = { GetClass, nullptr };
551     bool checkRes = CfIsPubKeyClassMatch(&obj, "TEST_FOR_GET_CLASS000");
552     EXPECT_EQ(checkRes, false);
553 }
554 
555 /**
556 * @tc.name: IsPubKeyClassMatch003
557 * @tc.desc: obj is nullptr
558 * @tc.type: FUNC
559 * @tc.require: AR000HS2RB /SR000HS2Q1
560 */
561 HWTEST_F(CfCommonTest, IsPubKeyClassMatch003, TestSize.Level0)
562 {
563     bool checkRes = CfIsPubKeyClassMatch(nullptr, "TEST_FOR_GET_CLASS");
564     EXPECT_EQ(checkRes, false);
565 }
566 
567 /**
568 * @tc.name: IsPubKeyClassMatch004
569 * @tc.desc: obj->getClass() is nullptr
570 * @tc.type: FUNC
571 * @tc.require: AR000HS2RB /SR000HS2Q1
572 */
573 HWTEST_F(CfCommonTest, IsPubKeyClassMatch004, TestSize.Level0)
574 {
575     HcfObjectBase obj = { GetClassNull, nullptr };
576     bool checkRes = CfIsPubKeyClassMatch(&obj, "TEST_FOR_GET_CLASS");
577     EXPECT_EQ(checkRes, false);
578 }
579 
580 /**
581 * @tc.name: IsPubKeyClassMatch005
582 * @tc.desc: class is nullptr
583 * @tc.type: FUNC
584 * @tc.require: AR000HS2RB /SR000HS2Q1
585 */
586 HWTEST_F(CfCommonTest, IsPubKeyClassMatch005, TestSize.Level0)
587 {
588     HcfObjectBase obj = { GetClass, nullptr };
589     bool checkRes = CfIsPubKeyClassMatch(&obj, nullptr);
590     EXPECT_EQ(checkRes, false);
591 }
592 } // end of namespace
593