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_ability.h"
19 #include "cf_cert_adapter_ability_define.h"
20 #include "cf_magic.h"
21 #include "cf_memory.h"
22 #include "cf_result.h"
23 #include "cf_test_common.h"
24 #include "cf_test_data.h"
25
26 using namespace testing::ext;
27 using namespace CertframeworkTest;
28 using namespace CertframeworkTestData;
29
30 namespace {
31 class CfAbilityTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34
35 static void TearDownTestCase(void);
36
37 void SetUp();
38
39 void TearDown();
40 };
41
SetUpTestCase(void)42 void CfAbilityTest::SetUpTestCase(void)
43 {
44 }
45
TearDownTestCase(void)46 void CfAbilityTest::TearDownTestCase(void)
47 {
48 }
49
SetUp()50 void CfAbilityTest::SetUp()
51 {
52 }
53
TearDown()54 void CfAbilityTest::TearDown()
55 {
56 }
57
58 /**
59 * @tc.name: RegisterAbilityTest001
60 * @tc.desc: Test RegisterAbility interface base function
61 * @tc.type: FUNC
62 * @tc.require: AR000HS2RB /SR000HS2Q1
63 */
64 HWTEST_F(CfAbilityTest, RegisterAbilityTest001, TestSize.Level0)
65 {
66 for (uint32_t i = 0; i <= CF_ABILITY_MAX_SIZE; ++i) {
67 (void)RegisterAbility(i, nullptr); /* coverage test */
68 }
69 int32_t ret = RegisterAbility(CF_ABILITY(CF_ABILITY_TYPE_ADAPTER, CF_OBJ_TYPE_CERT), nullptr);
70 EXPECT_EQ(ret, CF_NOT_SUPPORT) << "register extension adapter func again, recode:" << ret;
71 }
72
73 /**
74 * @tc.name: GetAbilityTest001
75 * @tc.desc: Test GetAbility interface base function
76 * @tc.type: FUNC
77 * @tc.require: AR000HS2RB /SR000HS2Q1
78 */
79 HWTEST_F(CfAbilityTest, GetAbilityTest001, TestSize.Level0)
80 {
81 int32_t ret = CF_SUCCESS;
82 CfBase *func = GetAbility(CF_ABILITY(CF_ABILITY_TYPE_ADAPTER, CF_OBJ_TYPE_CERT));
83 if (func == nullptr) {
84 printf("get ability failed\n");
85 ret = CF_ERR_CRYPTO_OPERATION;
86 ASSERT_EQ(ret, CF_SUCCESS);
87 }
88
89 CfCertAdapterAbilityFunc *adapterFunc = (CfCertAdapterAbilityFunc *)func;
90 if (adapterFunc->base.type != CF_MAGIC(CF_MAGIC_TYPE_ADAPTER_FUNC, CF_OBJ_TYPE_CERT)) {
91 printf("func magic id is %lu\n", adapterFunc->base.type);
92 ret = CF_INVALID_PARAMS;
93 } else {
94 CfBase *obj001 = nullptr;
95 CfEncodingBlob cert = { const_cast<uint8_t *>(g_certData01), sizeof(g_certData01), CF_FORMAT_DER };
96 ret = adapterFunc->adapterCreate(&cert, &obj001);
97 EXPECT_EQ(ret, CF_SUCCESS) << "create cert object failed, recode:" << ret;
98
99 ret = adapterFunc->adapterVerify(nullptr, nullptr);
100 EXPECT_EQ(ret, CF_SUCCESS) << "verify cert object failed, recode:" << ret;
101
102 CfBlob tbsBlob = { 0, nullptr };
103 ret = adapterFunc->adapterGetItem(obj001, CF_ITEM_TBS, &tbsBlob);
104 EXPECT_EQ(ret, CF_SUCCESS) << "get tbs failed, recode:" << ret;
105 CF_FREE_BLOB(tbsBlob);
106
107 CfBlob issuerUidBlob = { 0, nullptr };
108 ret = adapterFunc->adapterGetItem(obj001, CF_ITEM_ISSUER_UNIQUE_ID, &issuerUidBlob);
109 EXPECT_EQ(ret, CF_SUCCESS) << "get issuerUid failed, recode:" << ret;
110 CF_FREE_BLOB(issuerUidBlob);
111
112 CfBlob subjectUidBlob = { 0, nullptr };
113 ret = adapterFunc->adapterGetItem(obj001, CF_ITEM_SUBJECT_UNIQUE_ID, &subjectUidBlob);
114 EXPECT_EQ(ret, CF_SUCCESS) << "get subjectUid failed, recode:" << ret;
115 CF_FREE_BLOB(subjectUidBlob);
116
117 CfBlob extsBlob = { 0, nullptr };
118 ret = adapterFunc->adapterGetItem(obj001, CF_ITEM_EXTENSIONS, &extsBlob);
119 EXPECT_EQ(ret, CF_SUCCESS) << "get extension failed, recode:" << ret;
120 CF_FREE_BLOB(extsBlob);
121
122 adapterFunc->adapterDestory(&obj001);
123 }
124 EXPECT_EQ(ret, CF_SUCCESS) << "adapter function magic id is err, retcode:" << ret;
125 }
126 }
127