• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "cm_test_log.h"
18 #include "cm_test_common.h"
19 #include "cert_manager_api.h"
20 
21 using namespace testing::ext;
22 using namespace CertmanagerTest;
23 namespace {
24 static uint32_t g_selfTokenId = 0;
25 static MockHapToken* g_MockHap = nullptr;
26 struct CertStatusExpectResult {
27     char uri[MAX_LEN_URI];
28     bool inparamStatus;
29     bool expectStatus;
30 };
31 
32 struct CertStatusExpectResult g_expectList[] = {
33     {
34         {"1d3472b9.0"}, false, true
35     },
36     {
37         {"4bfab552.0"}, false, true
38     },
39     {
40         {"4f316efb.0"}, true, true
41     }
42 };
43 
44 class CmSetCertStatusTest : public testing::Test {
45 public:
46     static void SetUpTestCase(void);
47 
48     static void TearDownTestCase(void);
49 
50     void SetUp();
51 
52     void TearDown();
53 };
54 
SetUpTestCase(void)55 void CmSetCertStatusTest::SetUpTestCase(void)
56 {
57     g_selfTokenId = GetSelfTokenID();
58     CmTestCommon::SetTestEnvironment(g_selfTokenId);
59 }
60 
TearDownTestCase(void)61 void CmSetCertStatusTest::TearDownTestCase(void)
62 {
63     CmTestCommon::ResetTestEnvironment();
64 }
65 
SetUp()66 void CmSetCertStatusTest::SetUp()
67 {
68     g_MockHap = new (std::nothrow) MockHapToken();
69 }
70 
TearDown()71 void CmSetCertStatusTest::TearDown()
72 {
73     if (g_MockHap != nullptr) {
74         delete g_MockHap;
75         g_MockHap = nullptr;
76     }
77 }
78 
79 /**
80  * @tc.name: SimpleSetCertStatus001
81  * @tc.desc: Test CertManager set cert status interface base function
82  * @tc.type: FUNC
83  * @tc.require: AR000H0MJA /SR000H096P
84  */
85 HWTEST_F(CmSetCertStatusTest, SimpleSetCertStatus001, TestSize.Level0)
86 {
87     struct CmBlob uriBlob = {strlen(g_expectList[0].uri) + 1, (uint8_t *)(g_expectList[0].uri)};
88 
89     int32_t ret = CmSetCertStatus(&uriBlob, CM_SYSTEM_TRUSTED_STORE, g_expectList[0].inparamStatus);
90     EXPECT_EQ(ret, CM_SUCCESS) << "SimpleSetCertStatus failed,retcode:" << ret;
91 
92     ret = CmSetCertStatus(&uriBlob, CM_SYSTEM_TRUSTED_STORE, true);
93     EXPECT_EQ(ret, CM_SUCCESS) << "SimpleSetCertStatus true failed,retcode:" << ret;
94 }
95 
96 /**
97  * @tc.name: SetCertStatusAndQueryStatus002
98  * @tc.desc: Test CertManager set cert status and query status interface function
99  * @tc.type: FUNC
100  * @tc.require: AR000H0MJA /SR000H096P
101  */
102 HWTEST_F(CmSetCertStatusTest, SetCertStatusAndQueryStatus002, TestSize.Level0)
103 {
104     uint32_t size = sizeof(g_expectList) / sizeof(g_expectList[0]);
105     for (uint32_t i = 0; i < size; ++i) {
106         struct CmBlob uriBlob = {strlen(g_expectList[i].uri) + 1, (uint8_t *)(g_expectList[i].uri)};
107 
108         int32_t ret = CmSetCertStatus(&uriBlob, CM_SYSTEM_TRUSTED_STORE, g_expectList[i].inparamStatus);
109         EXPECT_EQ(ret, CM_SUCCESS) << " SetCertStatusAndQueryStatus, CmSetCertStatus failed,retcode: " << ret;
110 
111         struct CertInfo certDetailInfo;
112         (void)memset_s(&certDetailInfo, sizeof(struct CertInfo), 0, sizeof(struct CertInfo));
113         ret = InitCertInfo(&certDetailInfo);
114         EXPECT_EQ(ret, CM_SUCCESS) << "CmGetCertInfo malloc faild, retcode:" << ret;
115 
116         ret = CmGetCertInfo(&uriBlob, CM_SYSTEM_TRUSTED_STORE, &certDetailInfo);
117         EXPECT_EQ(ret, CM_SUCCESS) << "SetCertStatusAndQueryStatus,CmGetCertInfo failed,retcode: " << ret;
118         int32_t status = (g_expectList[i].expectStatus == certDetailInfo.status) ? 1 : 0;
119 
120         EXPECT_EQ(status, 1) << "SetCertStatusAndQueryStatus faild, cert info: " << DumpCertInfo(&certDetailInfo);
121         FreeCMBlobData(&(certDetailInfo.certInfo));
122 
123         ret = CmSetCertStatus(&uriBlob, CM_SYSTEM_TRUSTED_STORE, true);
124         EXPECT_EQ(ret, CM_SUCCESS) << " SetCertStatusAndQueryStatus, CmSetCertStatus failed,retcode: " << ret;
125     }
126 }
127 
128 /**
129  * @tc.name: SetAllCertStatus003
130  * @tc.desc: Test CertManager set all cert status interface function
131  * @tc.type: FUNC
132  * @tc.require: AR000H0MJA /SR000H096P
133  */
134 HWTEST_F(CmSetCertStatusTest, SetAllCertStatus003, TestSize.Level0)
135 {
136     struct CertList *certlist = NULL;
137 
138     ASSERT_TRUE(InitCertList(&certlist) == CM_SUCCESS);
139     // CA trusted list
140     int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, certlist);
141 
142     EXPECT_EQ(ret, CM_SUCCESS) << "SetAllCertStatus,CmGetCertList failed,retcode:" << ret;
143 
144     for (uint32_t i = 0; i < certlist->certsCount; ++i) {
145         struct CertAbstract *ptr = &(certlist->certAbstract[i]);
146         ASSERT_TRUE(NULL != ptr);
147         struct CmBlob uriBlob = {strlen(ptr->uri) + 1, (uint8_t *)(ptr->uri)};
148         ret = CmSetCertStatus(&uriBlob, CM_SYSTEM_TRUSTED_STORE, false);
149         EXPECT_EQ(ret, CM_SUCCESS);
150     }
151 
152     for (uint32_t i = 0; i < certlist->certsCount; ++i) {
153         struct CertAbstract *ptr2 = &(certlist->certAbstract[i]);
154         ASSERT_TRUE(NULL != ptr2);
155         struct CmBlob uriBlob2 = {strlen(ptr2->uri) + 1, (uint8_t *)(ptr2->uri)};
156         ret = CmSetCertStatus(&uriBlob2, CM_SYSTEM_TRUSTED_STORE, true);
157         EXPECT_EQ(ret, CM_SUCCESS);
158     }
159     FreeCertList(certlist);
160 }
161 
162 /**
163  * @tc.name: ExceptionSetStatus004
164  * @tc.desc: Test CertManager set cert status interface abnormal function
165  * @tc.type: FUNC
166  * @tc.require: AR000H0MJA /SR000H096P
167  */
168 HWTEST_F(CmSetCertStatusTest, ExceptionSetStatus004, TestSize.Level0)
169 {
170     struct CmBlob uriBlob = {strlen(g_expectList[1].uri) + 1, (uint8_t *)(g_expectList[1].uri)};
171     EXPECT_EQ(CmSetCertStatus(NULL, CM_SYSTEM_TRUSTED_STORE, true),
172         CMR_ERROR_NULL_POINTER);
173 
174     EXPECT_EQ(CmSetCertStatus(&uriBlob, 10, true), CMR_ERROR_INVALID_ARGUMENT);
175 
176     const char *invalidUri = "INVALIDXXXX";
177     struct CmBlob invalidUriBlob = {strlen(invalidUri) + 1, (uint8_t *)invalidUri};
178     EXPECT_EQ(CmSetCertStatus(&invalidUriBlob, CM_SYSTEM_TRUSTED_STORE, true),
179         CM_SUCCESS);
180 }
181 }
182