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