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