• 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 static const int TIMES_PERFORMANCE = 10;
27 static const uint32_t PATH_LEN = 1000;
28 
29 struct CertAbstractResult {
30     struct CertAbstract certAbstract;
31     bool bExpectResult;
32 };
33 
34 struct CertAbstractResult g_listexpectResult[] = {
35     {
36         {
37             "1d3472b9.0",
38             "GlobalSign",
39             true, "CN=GlobalSign,OU=GlobalSign ECC Root CA - R5,O=GlobalSign"
40         },
41         true
42     },
43     {
44         {
45             "4bfab552.0",
46             "Starfield Technologies, Inc.",
47             true,
48             "CN=Starfield Root Certificate Authority - G2,OU=,O=Starfield Technologies, Inc."
49         },
50         true
51     },
52     {
53         {
54             "4f316efb.0",
55             "SwissSign AG",
56             true,
57             "CN=SwissSign Gold CA - G2,OU=,O=SwissSign AG"
58         },
59         true
60     }
61 };
62 
63 class CmGetCertListTest : public testing::Test {
64 public:
65     static void SetUpTestCase(void);
66 
67     static void TearDownTestCase(void);
68 
69     void SetUp();
70 
71     void TearDown();
72 
73     struct CertList *lstCert;
74 };
75 
SetUpTestCase(void)76 void CmGetCertListTest::SetUpTestCase(void)
77 {
78     g_selfTokenId = GetSelfTokenID();
79     CmTestCommon::SetTestEnvironment(g_selfTokenId);
80 }
81 
TearDownTestCase(void)82 void CmGetCertListTest::TearDownTestCase(void)
83 {
84     CmTestCommon::ResetTestEnvironment();
85 }
86 
SetUp()87 void CmGetCertListTest::SetUp()
88 {
89     InitCertList(&lstCert);
90     g_MockHap = new (std::nothrow) MockHapToken();
91 }
92 
TearDown()93 void CmGetCertListTest::TearDown()
94 {
95     FreeCertList(lstCert);
96     if (g_MockHap != nullptr) {
97         delete g_MockHap;
98         g_MockHap = nullptr;
99     }
100 }
101 
102 /**
103  * @tc.name: SimpleGetCertListTest001
104  * @tc.desc: Test CertManager get cert list interface base function
105  * @tc.type: FUNC
106  * @tc.require: AR000H0MJA /SR000H096P
107  */
108 HWTEST_F(CmGetCertListTest, SimpleGetCertListTest001, TestSize.Level0)
109 {
110     int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, lstCert);
111     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetCertList failed,retcode:" << ret;
112 }
113 
114 /**
115  * @tc.name: PerformanceGetCertListTest002
116  * @tc.desc: Test CertManager get cert list interface performance
117  * @tc.type: FUNC
118  * @tc.require: AR000H0MJA /SR000H096P
119  */
120 HWTEST_F(CmGetCertListTest, PerformanceGetCertListTest002, TestSize.Level0)
121 {
122     for (int times = 0; times < TIMES_PERFORMANCE; ++times) {
123         struct CertList *listCert = NULL;
124         ASSERT_TRUE(InitCertList(&listCert) == CM_SUCCESS);
125         int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, listCert);
126         EXPECT_EQ(ret, CM_SUCCESS) << "CmGetCertList Performance failed,retcode:" << ret;
127         FreeCertList(listCert);
128     }
129 }
130 
131 /**
132  * @tc.name: GetCertListContent003
133  * @tc.desc: Test CertManager get cert list content interface function
134  * @tc.type: FUNC
135  * @tc.require: AR000H0MJA /SR000H096P
136  */
137 HWTEST_F(CmGetCertListTest, GetCertListContent003, TestSize.Level0)
138 {
139     int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, lstCert);
140     EXPECT_EQ(ret, CM_SUCCESS) << "firstUserCtx CmGetCertList failed,retcode:" << ret;
141 
142     uint32_t length = sizeof(g_listexpectResult) / sizeof(g_listexpectResult[0]);
143     bool bFind = false;
144     for (uint32_t j = 0; j < length; ++j) {
145         bFind = FindCertAbstract(&(g_listexpectResult[j].certAbstract), lstCert);
146 
147         EXPECT_EQ(bFind, g_listexpectResult[j].bExpectResult) << DumpCertList(lstCert);
148     }
149 }
150 
151 /**
152  * @tc.name: AppGetCertListCompare004
153  * @tc.desc: Test CertManager get cert list compare interface function
154  * @tc.type: FUNC
155  * @tc.require: AR000H0MJA /SR000H096P
156  */
157 HWTEST_F(CmGetCertListTest, AppGetCertListCompare004, TestSize.Level0)
158 {
159     int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, lstCert);
160     EXPECT_EQ(ret, CM_SUCCESS) << "first  CmGetCertList failed,retcode:" << ret;
161 
162     struct CertList *secondListCert = NULL;
163     ASSERT_TRUE(InitCertList(&secondListCert) == CM_SUCCESS);
164     ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, secondListCert);
165     EXPECT_EQ(ret, CM_SUCCESS) << "secondUserCtx CmGetCertList failed,retcode:" << ret;
166 
167     EXPECT_EQ(lstCert->certsCount, secondListCert->certsCount) << "firstUserCtx count:" << lstCert->certsCount
168         << "secondUserCtx count:" << secondListCert->certsCount;
169 
170     FreeCertList(secondListCert);
171 }
172 
173 /**
174  * @tc.name: ExceptionGetCertList005
175  * @tc.desc: Test CertManager get cert list interface abnormal function
176  * @tc.type: FUNC
177  * @tc.require: AR000H0MJA /SR000H096P
178  */
179 HWTEST_F(CmGetCertListTest, ExceptionGetCertList005, TestSize.Level0)
180 {
181     EXPECT_EQ(CmGetCertList(CM_SYSTEM_TRUSTED_STORE, NULL), CMR_ERROR_NULL_POINTER);
182     EXPECT_EQ(CmGetCertList(10, lstCert), CMR_ERROR_INVALID_ARGUMENT);
183 }
184 
185 /**
186  * @tc.name: GetCertStorePath001
187  * @tc.desc: get system ca path
188  * @tc.type: FUNC
189  */
190 HWTEST_F(CmGetCertListTest, GetCertStorePath001, TestSize.Level0)
191 {
192     char path[PATH_LEN] = {0};
193     int32_t ret = CmGetCertStorePath(CM_CA_CERT_SYSTEM, 0, path, sizeof(path));
194     EXPECT_EQ(ret, CM_SUCCESS);
195     EXPECT_EQ(strcmp(path, CA_STORE_PATH_SYSTEM), 0);
196 }
197 
198 /**
199  * @tc.name: GetCertStorePath002
200  * @tc.desc: get user ca path
201  * @tc.type: FUNC
202  */
203 HWTEST_F(CmGetCertListTest, GetCertStorePath002, TestSize.Level0)
204 {
205     char path[PATH_LEN] = {0};
206     uint32_t userId = 101;
207     int32_t ret = CmGetCertStorePath(CM_CA_CERT_USER, userId, path, sizeof(path));
208     EXPECT_EQ(ret, CM_SUCCESS);
209 
210     std::string expectPath = CA_STORE_PATH_USER_SERVICE_BASE + std::to_string(userId);
211     std::cout << "1:" << path << std::endl;
212     std::cout << "2:" << expectPath << std::endl;
213     EXPECT_EQ(strcmp(path, expectPath.c_str()), 0);
214 }
215 
216 /**
217  * @tc.name: GetCertStorePath003
218  * @tc.desc: type invalid
219  * @tc.type: FUNC
220  */
221 HWTEST_F(CmGetCertListTest, GetCertStorePath003, TestSize.Level0)
222 {
223     char path[PATH_LEN] = {0};
224     int32_t type = 3; /* type invalid */
225     int32_t ret = CmGetCertStorePath(static_cast<enum CmCertType>(type), 0, path, sizeof(path));
226     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
227 }
228 
229 /**
230  * @tc.name: GetCertStorePath004
231  * @tc.desc: path is null
232  * @tc.type: FUNC
233  */
234 HWTEST_F(CmGetCertListTest, GetCertStorePath004, TestSize.Level0)
235 {
236     int32_t ret = CmGetCertStorePath(CM_CA_CERT_SYSTEM, 0, nullptr, PATH_LEN);
237     EXPECT_EQ(ret, CMR_ERROR_NULL_POINTER);
238 }
239 
240 /**
241  * @tc.name: GetCertStorePath005
242  * @tc.desc: get system ca path, len too small
243  * @tc.type: FUNC
244  */
245 HWTEST_F(CmGetCertListTest, GetCertStorePath005, TestSize.Level0)
246 {
247     char path[PATH_LEN] = {0};
248     uint32_t length = strlen(CA_STORE_PATH_SYSTEM);
249     int32_t ret = CmGetCertStorePath(CM_CA_CERT_SYSTEM, 0, path, length);
250     EXPECT_EQ(ret, CMR_ERROR_BUFFER_TOO_SMALL);
251 }
252 
253 /**
254  * @tc.name: GetCertStorePath006
255  * @tc.desc: get user ca path, len too small
256  * @tc.type: FUNC
257  */
258 HWTEST_F(CmGetCertListTest, GetCertStorePath006, TestSize.Level0)
259 {
260     char path[PATH_LEN] = {0};
261     uint32_t userId = 100;
262     std::string expectPath = CA_STORE_PATH_USER_SERVICE_BASE + std::to_string(userId);
263     uint32_t length = expectPath.length();
264     int32_t ret = CmGetCertStorePath(CM_CA_CERT_USER, userId, path, length);
265     EXPECT_EQ(ret, CMR_ERROR_BUFFER_TOO_SMALL);
266 }
267 }
268