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_api.h"
19 #include "cf_param.h"
20 #include "cf_result.h"
21 #include "cf_type.h"
22
23 #include "cf_test_common.h"
24 #include "cf_test_data.h"
25 #include "cf_test_sdk_common.h"
26
27 using namespace testing::ext;
28 using namespace CertframeworkTestData;
29 using namespace CertframeworkTest;
30 using namespace CertframeworkSdkTest;
31
32 namespace {
33 constexpr int32_t DER_FORMAT_INDEX = 0;
34 constexpr int32_t PEM_FORMAT_INDEX = 1;
35 class CfCertTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38
39 static void TearDownTestCase(void);
40
41 void SetUp();
42
43 void TearDown();
44 };
45
SetUpTestCase(void)46 void CfCertTest::SetUpTestCase(void)
47 {
48 }
49
TearDownTestCase(void)50 void CfCertTest::TearDownTestCase(void)
51 {
52 }
53
SetUp()54 void CfCertTest::SetUp()
55 {
56 }
57
TearDown()58 void CfCertTest::TearDown()
59 {
60 }
61
62 const static CfEncodingBlob g_cert[] = {
63 { const_cast<uint8_t *>(g_certData01), sizeof(g_certData01), CF_FORMAT_DER },
64 { reinterpret_cast<uint8_t *>(g_certData02), sizeof(g_certData02), CF_FORMAT_PEM }
65 };
66
67 const static CfBlob g_certTbs = { sizeof(g_certData01TBS), const_cast<uint8_t *>(g_certData01TBS) };
68 const static CfBlob g_certPemTbs = { sizeof(g_certData02TBS), const_cast<uint8_t *>(g_certData02TBS) };
69 const static CfBlob g_certIssueUid = { sizeof(g_certData01IssuerUID), const_cast<uint8_t *>(g_certData01IssuerUID) };
70 const static CfBlob g_certSubUid = { sizeof(g_certData01SubjectUID), const_cast<uint8_t *>(g_certData01SubjectUID) };
71 const static CfBlob g_certExt = { sizeof(g_extensionData01), const_cast<uint8_t *>(g_extensionData01) };
72 const static CfBlob g_certPubKey = { sizeof(g_certData01PubKey), const_cast<uint8_t *>(g_certData01PubKey) };
73
CompareResult(CfItemId id,const CfParamSet * out,enum CfEncodingFormat format)74 static bool CompareResult(CfItemId id, const CfParamSet *out, enum CfEncodingFormat format)
75 {
76 CfParam *resultTypeParam = NULL;
77 int32_t ret = CfGetParam(out, CF_TAG_RESULT_TYPE, &resultTypeParam);
78 if (ret != CF_SUCCESS) {
79 printf("get CF_TAG_RESULT_TYPE failed.\n");
80 return false;
81 }
82
83 if (resultTypeParam->int32Param != CF_TAG_TYPE_BYTES) {
84 printf("result type is not CF_TAG_TYPE_BYTES.\n");
85 return false;
86 }
87
88 CfParam *resultParam = NULL;
89 ret = CfGetParam(out, CF_TAG_RESULT_BYTES, &resultParam);
90 if (ret != CF_SUCCESS) {
91 printf("get CF_TAG_RESULT_BYTES from out failed.\n");
92 return false;
93 }
94
95 switch (id) {
96 case CF_ITEM_TBS:
97 if (format == CF_FORMAT_DER) {
98 return CompareBlob(&resultParam->blob, &g_certTbs);
99 }
100 return CompareBlob(&resultParam->blob, &g_certPemTbs);
101 case CF_ITEM_ISSUER_UNIQUE_ID:
102 return CompareBlob(&resultParam->blob, &g_certIssueUid);
103 case CF_ITEM_SUBJECT_UNIQUE_ID:
104 return CompareBlob(&resultParam->blob, &g_certSubUid);
105 case CF_ITEM_EXTENSIONS:
106 return CompareBlob(&resultParam->blob, &g_certExt);
107 case CF_ITEM_PUBLIC_KEY:
108 return CompareBlob(&resultParam->blob, &g_certPubKey);
109 default:
110 return false;
111 }
112 }
113
CertTest(CfItemId id,const CfEncodingBlob * in)114 static void CertTest(CfItemId id, const CfEncodingBlob *in)
115 {
116 CfParamSet *outParamSet = nullptr;
117 CfParam params[] = {
118 { .tag = CF_TAG_GET_TYPE, .int32Param = CF_GET_TYPE_CERT_ITEM },
119 { .tag = CF_TAG_PARAM0_INT32, .int32Param = id },
120 };
121 int32_t ret = CommonTest(CF_OBJ_TYPE_CERT, in, params, sizeof(params) / sizeof(CfParam), &outParamSet);
122 EXPECT_EQ(ret, CF_SUCCESS);
123 #ifdef TEST_PRINT_DATA
124 (void)GetOutValue(outParamSet);
125 #endif
126 EXPECT_EQ(CompareResult(id, outParamSet, in->encodingFormat), true);
127 CfFreeParamSet(&outParamSet);
128 }
129
130 /**
131 * @tc.name: CfCertTest001
132 * @tc.desc: get tbs
133 * @tc.type: FUNC
134 * @tc.require: AR000HS2RB /SR000HS2Q1
135 */
136 HWTEST_F(CfCertTest, CfCertTest001, TestSize.Level0)
137 {
138 CertTest(CF_ITEM_TBS, &g_cert[DER_FORMAT_INDEX]);
139 }
140
141 /**
142 * @tc.name: CfCertTest002
143 * @tc.desc: get issuer unique id
144 * @tc.type: FUNC
145 * @tc.require: AR000HS2RB /SR000HS2Q1
146 */
147 HWTEST_F(CfCertTest, CfCertTest002, TestSize.Level0)
148 {
149 CertTest(CF_ITEM_ISSUER_UNIQUE_ID, &g_cert[DER_FORMAT_INDEX]);
150 }
151
152 /**
153 * @tc.name: CfCertTest003
154 * @tc.desc: get subject unique id
155 * @tc.type: FUNC
156 * @tc.require: AR000HS2RB /SR000HS2Q1
157 */
158 HWTEST_F(CfCertTest, CfCertTest003, TestSize.Level0)
159 {
160 CertTest(CF_ITEM_SUBJECT_UNIQUE_ID, &g_cert[DER_FORMAT_INDEX]);
161 }
162
163 /**
164 * @tc.name: CfCertTest004
165 * @tc.desc: get public key in der format
166 * @tc.type: FUNC
167 * @tc.require: AR000HS2RB /SR000HS2Q1
168 */
169 HWTEST_F(CfCertTest, CfCertTest004, TestSize.Level0)
170 {
171 CertTest(CF_ITEM_PUBLIC_KEY, &g_cert[DER_FORMAT_INDEX]);
172 }
173
174 /**
175 * @tc.name: CfCertTest005
176 * @tc.desc: get extension
177 * @tc.type: FUNC
178 * @tc.require: AR000HS2RB /SR000HS2Q1
179 */
180 HWTEST_F(CfCertTest, CfCertTest005, TestSize.Level0)
181 {
182 CertTest(CF_ITEM_EXTENSIONS, &g_cert[DER_FORMAT_INDEX]);
183 }
184
185 /**
186 * @tc.name: CfCertTest006
187 * @tc.desc: check func
188 * @tc.type: FUNC
189 * @tc.require: AR000HS2RB /SR000HS2Q1
190 */
191 HWTEST_F(CfCertTest, CfCertTest006, TestSize.Level0)
192 {
193 CfParamSet *outParamSet = nullptr;
194 CfParam params[] = {
195 { .tag = CF_TAG_CHECK_TYPE, .int32Param = 0 }, /* reserve test */
196 };
197 CommonTest(CF_OBJ_TYPE_CERT, &g_cert[0], params, sizeof(params) / sizeof(CfParam), &outParamSet);
198 CfFreeParamSet(&outParamSet);
199 }
200
201 /**
202 * @tc.name: CfCertTest007
203 * @tc.desc: create object
204 * @tc.type: FUNC
205 * @tc.require: AR000HS2RB /SR000HS2Q1
206 */
207 HWTEST_F(CfCertTest, CfCertTest007, TestSize.Level0)
208 {
209 CfObject *object = nullptr;
210 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[0], &object);
211 ASSERT_EQ(ret, CF_SUCCESS);
212
213 object->destroy(&object);
214 }
215
216 /**
217 * @tc.name: CfCertTest008
218 * @tc.desc: pem format, get tbs
219 * @tc.type: FUNC
220 * @tc.require: AR000HS2RB /SR000HS2Q1
221 */
222 HWTEST_F(CfCertTest, CfCertTest008, TestSize.Level0)
223 {
224 CertTest(CF_ITEM_TBS, &g_cert[PEM_FORMAT_INDEX]);
225 }
226
227 /**
228 * @tc.name: CfCertTest009
229 * @tc.desc: CfCreate: in is nullptr
230 * @tc.type: FUNC
231 * @tc.require: AR000HS2RB /SR000HS2Q1
232 */
233 HWTEST_F(CfCertTest, CfCertTest009, TestSize.Level0)
234 {
235 CfObject *object = nullptr;
236 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, nullptr, &object); /* in is nullptr */
237 EXPECT_NE(ret, CF_SUCCESS);
238 }
239
240 /**
241 * @tc.name: CfCertTest010
242 * @tc.desc: CfCreate: object is nullptr
243 * @tc.type: FUNC
244 * @tc.require: AR000HS2RB /SR000HS2Q1
245 */
246 HWTEST_F(CfCertTest, CfCertTest010, TestSize.Level0)
247 {
248 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX], nullptr); /* object is nullptr */
249 EXPECT_NE(ret, CF_SUCCESS);
250 }
251
252 /**
253 * @tc.name: CfCertTest011
254 * @tc.desc: CfCreate:objType is invalid
255 * @tc.type: FUNC
256 * @tc.require: AR000HS2RB /SR000HS2Q1
257 */
258 HWTEST_F(CfCertTest, CfCertTest011, TestSize.Level0)
259 {
260 CfObject *object = nullptr;
261 int32_t cfObjType = 0xff; /* objType is invalid */
262 int32_t ret = CfCreate(static_cast<CfObjectType>(cfObjType), &g_cert[DER_FORMAT_INDEX], &object);
263 EXPECT_NE(ret, CF_SUCCESS);
264 }
265
266 /**
267 * @tc.name: CfCertTest012
268 * @tc.desc: CfCreate:in's data is invalid create failed
269 * @tc.type: FUNC
270 * @tc.require: AR000HS2RB /SR000HS2Q1
271 */
272 HWTEST_F(CfCertTest, CfCertTest012, TestSize.Level0)
273 {
274 CfObject *object = nullptr;
275 uint8_t invalidData[] = { 0x30, 0x33, 0x44, 0x55, }; /* in's data is invalid create failed */
276 CfEncodingBlob cert = { invalidData, sizeof(invalidData), CF_FORMAT_DER };
277 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &cert, &object);
278 EXPECT_NE(ret, CF_SUCCESS);
279 }
280
281 /**
282 * @tc.name: CfCertTest013
283 * @tc.desc: CfCreate:in's data is nullptr
284 * @tc.type: FUNC
285 * @tc.require: AR000HS2RB /SR000HS2Q1
286 */
287 HWTEST_F(CfCertTest, CfCertTest013, TestSize.Level0)
288 {
289 CfObject *object = nullptr;
290 uint8_t invalidData[] = { 0x30, 0x11, 0x22, 0x33, };
291 CfEncodingBlob cert = { nullptr, sizeof(invalidData), CF_FORMAT_DER }; /* in's data is nullptr */
292 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &cert, &object);
293 EXPECT_NE(ret, CF_SUCCESS);
294 }
295
296 /**
297 * @tc.name: CfCertTest014
298 * @tc.desc: CfCreate:in's size is 0
299 * @tc.type: FUNC
300 * @tc.require: AR000HS2RB /SR000HS2Q1
301 */
302 HWTEST_F(CfCertTest, CfCertTest014, TestSize.Level0)
303 {
304 CfObject *object = nullptr;
305 uint8_t invalidData[] = { 0x30, 0x01, 0x02, 0x03, };
306 CfEncodingBlob cert = { invalidData, 0, CF_FORMAT_DER }; /* in's size is 0 */
307 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &cert, &object);
308 EXPECT_NE(ret, CF_SUCCESS);
309 }
310
311 /**
312 * @tc.name: CfCertTest015
313 * @tc.desc: CfCreate:in's encodingFormat invalid
314 * @tc.type: FUNC
315 * @tc.require: AR000HS2RB /SR000HS2Q1
316 */
317 HWTEST_F(CfCertTest, CfCertTest015, TestSize.Level0)
318 {
319 CfObject *object = nullptr;
320 int32_t format = 0xff;
321 CfEncodingBlob cert = { reinterpret_cast<uint8_t *>(g_certData02), sizeof(g_certData02),
322 static_cast<enum CfEncodingFormat>(format) };
323 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &cert, &object);
324 EXPECT_NE(ret, CF_SUCCESS);
325 }
326
327 /**
328 * @tc.name: CfCertTest016
329 * @tc.desc: ->destroy: object is nullptr
330 * @tc.type: FUNC
331 * @tc.require: AR000HS2RB /SR000HS2Q1
332 */
333 HWTEST_F(CfCertTest, CfCertTest016, TestSize.Level0)
334 {
335 CfObject *object = nullptr;
336 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX], &object);
337 ASSERT_EQ(ret, CF_SUCCESS);
338
339 object->destroy(nullptr); /* destroy: object is nullptr coverage */
340 object->destroy(&object);
341 }
342
343 /**
344 * @tc.name: CfCertTest017
345 * @tc.desc: ->destroy: *object is nullptr
346 * @tc.type: FUNC
347 * @tc.require: AR000HS2RB /SR000HS2Q1
348 */
349 HWTEST_F(CfCertTest, CfCertTest017, TestSize.Level0)
350 {
351 CfObject *object = nullptr;
352 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX], &object);
353 ASSERT_EQ(ret, CF_SUCCESS);
354
355 CfObject *object1 = nullptr;
356 object->destroy(&object1); /* destroy: *object is nullptr coverage */
357 object->destroy(&object);
358 }
359
360 /**
361 * @tc.name: CfCertTest018
362 * @tc.desc: ->get: object is nullptr
363 * @tc.type: FUNC
364 * @tc.require: AR000HS2RB /SR000HS2Q1
365 */
366 HWTEST_F(CfCertTest, CfCertTest018, TestSize.Level0)
367 {
368 CfObject *object = nullptr;
369 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX], &object);
370 ASSERT_EQ(ret, CF_SUCCESS);
371
372 CfParamSet *inParamSet = nullptr;
373 EXPECT_EQ(CfInitParamSet(&inParamSet), CF_SUCCESS);
374
375 CfParamSet *outParamSet = nullptr;
376 ret = object->get(nullptr, inParamSet, &outParamSet); /* object is nullptr */
377 EXPECT_NE(ret, CF_SUCCESS);
378
379 object->destroy(&object);
380 CfFreeParamSet(&inParamSet);
381 }
382
383 /**
384 * @tc.name: CfCertTest019
385 * @tc.desc: ->get: in is nullptr
386 * @tc.type: FUNC
387 * @tc.require: AR000HS2RB /SR000HS2Q1
388 */
389 HWTEST_F(CfCertTest, CfCertTest019, TestSize.Level0)
390 {
391 CfObject *object = nullptr;
392 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX], &object);
393 ASSERT_EQ(ret, CF_SUCCESS);
394
395 CfParamSet *outParamSet = nullptr;
396 ret = object->get(object, nullptr, &outParamSet); /* inParamSet is nullptr */
397 EXPECT_NE(ret, CF_SUCCESS);
398
399 object->destroy(&object);
400 }
401
402 /**
403 * @tc.name: CfCertTest020
404 * @tc.desc: ->get: out is nullptr
405 * @tc.type: FUNC
406 * @tc.require: AR000HS2RB /SR000HS2Q1
407 */
408 HWTEST_F(CfCertTest, CfCertTest020, TestSize.Level0)
409 {
410 CfObject *object = nullptr;
411 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX], &object);
412 ASSERT_EQ(ret, CF_SUCCESS);
413
414 CfParamSet *inParamSet = nullptr;
415 EXPECT_EQ(CfInitParamSet(&inParamSet), CF_SUCCESS);
416
417 ret = object->get(object, inParamSet, nullptr); /* outParamSet is nullptr */
418 EXPECT_NE(ret, CF_SUCCESS);
419
420 object->destroy(&object);
421 CfFreeParamSet(&inParamSet);
422 }
423
424 /**
425 * @tc.name: CfCertTest021
426 * @tc.desc: ->check: object is nullptr
427 * @tc.type: FUNC
428 * @tc.require: AR000HS2RB /SR000HS2Q1
429 */
430 HWTEST_F(CfCertTest, CfCertTest021, TestSize.Level0)
431 {
432 CfObject *object021 = nullptr;
433 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX], &object021);
434 ASSERT_EQ(ret, CF_SUCCESS);
435
436 CfParamSet *inParamSet = nullptr;
437 EXPECT_EQ(CfInitParamSet(&inParamSet), CF_SUCCESS);
438
439 CfParamSet *outParamSet = nullptr;
440 ret = object021->check(nullptr, inParamSet, &outParamSet); /* check object is nullptr */
441 EXPECT_NE(ret, CF_SUCCESS);
442
443 object021->destroy(&object021);
444 CfFreeParamSet(&inParamSet);
445 }
446
447 /**
448 * @tc.name: CfCertTest022
449 * @tc.desc: ->check: in is nullptr
450 * @tc.type: FUNC
451 * @tc.require: AR000HS2RB /SR000HS2Q1
452 */
453 HWTEST_F(CfCertTest, CfCertTest022, TestSize.Level0)
454 {
455 CfObject *object022 = nullptr;
456 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX], &object022);
457 ASSERT_EQ(ret, CF_SUCCESS);
458
459 CfParamSet *outParamSet = nullptr;
460 ret = object022->check(object022, nullptr, &outParamSet); /* check inParamSet is nullptr */
461 EXPECT_NE(ret, CF_SUCCESS);
462
463 object022->destroy(&object022);
464 }
465
466 /**
467 * @tc.name: CfCertTest023
468 * @tc.desc: ->check: out is nullptr
469 * @tc.type: FUNC
470 * @tc.require: AR000HS2RB /SR000HS2Q1
471 */
472 HWTEST_F(CfCertTest, CfCertTest023, TestSize.Level0)
473 {
474 CfObject *object023 = nullptr;
475 int32_t ret = CfCreate(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX], &object023);
476 ASSERT_EQ(ret, CF_SUCCESS);
477
478 CfParamSet *inParamSet = nullptr;
479 EXPECT_EQ(CfInitParamSet(&inParamSet), CF_SUCCESS);
480
481 ret = object023->check(object023, inParamSet, nullptr); /* check outParamSet is nullptr */
482 EXPECT_NE(ret, CF_SUCCESS);
483
484 object023->destroy(&object023);
485 CfFreeParamSet(&inParamSet);
486 }
487
488 /**
489 * @tc.name: CfCertTest024
490 * @tc.desc: ->get: inParamSet not set CF_TAG_GET_TYPE
491 * @tc.type: FUNC
492 * @tc.require: AR000HS2RB /SR000HS2Q1
493 */
494 HWTEST_F(CfCertTest, CfCertTest024, TestSize.Level0)
495 {
496 CfParam params[] = { /* inParamSet not set CF_TAG_GET_TYPE */
497 { .tag = CF_TAG_CHECK_TYPE, .int32Param = CF_CHECK_TYPE_EXT_CA },
498 };
499
500 int32_t ret = AbnormalTest(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX],
501 params, sizeof(params) / sizeof(CfParam), OP_TYPE_GET);
502 EXPECT_EQ(ret, CF_SUCCESS);
503 }
504
505 /**
506 * @tc.name: CfCertTest025
507 * @tc.desc: ->get: inParamSet's CF_TAG_GET_TYPE is not CF_GET_TYPE_CERT_ITEM
508 * @tc.type: FUNC
509 * @tc.require: AR000HS2RB /SR000HS2Q1
510 */
511 HWTEST_F(CfCertTest, CfCertTest025, TestSize.Level0)
512 {
513 CfParam params[] = { /* CF_TAG_GET_TYPE is not CF_GET_TYPE_CERT_ITEM */
514 { .tag = CF_TAG_GET_TYPE, .int32Param = CF_GET_TYPE_EXT_ITEM },
515 };
516
517 int32_t ret = AbnormalTest(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX],
518 params, sizeof(params) / sizeof(CfParam), OP_TYPE_GET);
519 EXPECT_EQ(ret, CF_SUCCESS);
520 }
521
522 /**
523 * @tc.name: CfCertTest026
524 * @tc.desc: ->get: inParamSet not set CF_TAG_PARAM0_INT32
525 * @tc.type: FUNC
526 * @tc.require: AR000HS2RB /SR000HS2Q1
527 */
528 HWTEST_F(CfCertTest, CfCertTest026, TestSize.Level0)
529 {
530 CfParam params[] = { /* not set CF_TAG_PARAM0_INT32 */
531 { .tag = CF_TAG_GET_TYPE, .int32Param = CF_GET_TYPE_CERT_ITEM },
532 };
533
534 int32_t ret = AbnormalTest(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX],
535 params, sizeof(params) / sizeof(CfParam), OP_TYPE_GET);
536 EXPECT_EQ(ret, CF_SUCCESS);
537 }
538
539 /**
540 * @tc.name: CfCertTest027
541 * @tc.desc: ->get: inParamSet's CF_TAG_PARAM0_INT32 is not valid
542 * @tc.type: FUNC
543 * @tc.require: AR000HS2RB /SR000HS2Q1
544 */
545 HWTEST_F(CfCertTest, CfCertTest027, TestSize.Level0)
546 {
547 CfParam params[] = { /* CF_TAG_PARAM0_INT32 is not valid */
548 { .tag = CF_TAG_GET_TYPE, .int32Param = CF_GET_TYPE_CERT_ITEM },
549 { .tag = CF_TAG_PARAM0_INT32, .int32Param = CF_ITEM_INVALID },
550 };
551
552 int32_t ret = AbnormalTest(CF_OBJ_TYPE_CERT, &g_cert[DER_FORMAT_INDEX],
553 params, sizeof(params) / sizeof(CfParam), OP_TYPE_GET);
554 EXPECT_EQ(ret, CF_SUCCESS);
555 }
556 }
557
558