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     int32_t ret = RegisterAbility(CF_ABILITY(CF_ABILITY_TYPE_ADAPTER, CF_OBJ_TYPE_CERT), nullptr);
67     EXPECT_EQ(ret, CF_NOT_SUPPORT) << "register extension adapter func again, recode:" << ret;
68 }
69 
70 /**
71  * @tc.name: RegisterAbilityTest002
72  * @tc.desc: Test RegisterAbility Exceeds max
73  * @tc.type: FUNC
74  * @tc.require: AR000HS2RB /SR000HS2Q1
75  */
76 HWTEST_F(CfAbilityTest, RegisterAbilityTest002, TestSize.Level0)
77 {
78     for (uint32_t i = 0; i <= CF_ABILITY_MAX_SIZE; ++i) {
79         (void)RegisterAbility(i, nullptr); /* coverage test */
80     }
81 }
82 
83 /**
84  * @tc.name: GetAbilityTest001
85  * @tc.desc: Test GetAbility interface base function
86  * @tc.type: FUNC
87  * @tc.require: AR000HS2RB /SR000HS2Q1
88  */
89 HWTEST_F(CfAbilityTest, GetAbilityTest001, TestSize.Level0)
90 {
91     int32_t ret = CF_SUCCESS;
92     CfBase *func = GetAbility(CF_ABILITY(CF_ABILITY_TYPE_ADAPTER, CF_OBJ_TYPE_CERT));
93     if (func == nullptr) {
94         printf("get ability failed\n");
95         ret = CF_ERR_CRYPTO_OPERATION;
96         ASSERT_EQ(ret, CF_SUCCESS);
97     }
98 
99     CfCertAdapterAbilityFunc *adapterFunc = (CfCertAdapterAbilityFunc *)func;
100     if (adapterFunc->base.type != CF_MAGIC(CF_MAGIC_TYPE_ADAPTER_FUNC, CF_OBJ_TYPE_CERT)) {
101         printf("func magic id is %lu\n", adapterFunc->base.type);
102         ret = CF_INVALID_PARAMS;
103     } else {
104         CfBase *obj001 = nullptr;
105         CfEncodingBlob cert = { const_cast<uint8_t *>(g_certData01), sizeof(g_certData01), CF_FORMAT_DER };
106         ret = adapterFunc->adapterCreate(&cert, &obj001);
107         EXPECT_EQ(ret, CF_SUCCESS) << "create cert object failed, recode:" << ret;
108 
109         ret = adapterFunc->adapterVerify(nullptr, nullptr);
110         EXPECT_EQ(ret, CF_SUCCESS) << "verify cert object failed, recode:" << ret;
111 
112         CfBlob tbsBlob = { 0, nullptr };
113         ret = adapterFunc->adapterGetItem(obj001, CF_ITEM_TBS, &tbsBlob);
114         EXPECT_EQ(ret, CF_SUCCESS) << "get tbs failed, recode:" << ret;
115         CF_FREE_BLOB(tbsBlob);
116 
117         CfBlob issuerUidBlob = { 0, nullptr };
118         ret = adapterFunc->adapterGetItem(obj001, CF_ITEM_ISSUER_UNIQUE_ID, &issuerUidBlob);
119         EXPECT_EQ(ret, CF_SUCCESS) << "get issuerUid failed, recode:" << ret;
120         CF_FREE_BLOB(issuerUidBlob);
121 
122         CfBlob subjectUidBlob = { 0, nullptr };
123         ret = adapterFunc->adapterGetItem(obj001, CF_ITEM_SUBJECT_UNIQUE_ID, &subjectUidBlob);
124         EXPECT_EQ(ret, CF_SUCCESS) << "get subjectUid failed, recode:" << ret;
125         CF_FREE_BLOB(subjectUidBlob);
126 
127         CfBlob extsBlob = { 0, nullptr };
128         ret = adapterFunc->adapterGetItem(obj001, CF_ITEM_EXTENSIONS, &extsBlob);
129         EXPECT_EQ(ret, CF_SUCCESS) << "get extension failed, recode:" << ret;
130         CF_FREE_BLOB(extsBlob);
131 
132         adapterFunc->adapterDestory(&obj001);
133     }
134     EXPECT_EQ(ret, CF_SUCCESS) << "adapter function magic id is err, retcode:" << ret;
135 }
136 }
137