• 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 
18 #include "cert_manager_api.h"
19 #include "cm_cert_data_ecc.h"
20 #include "cm_cert_data_part1_rsa.h"
21 #include "cm_mem.h"
22 #include "cm_test_common.h"
23 
24 using namespace testing::ext;
25 using namespace CertmanagerTest;
26 namespace {
27 struct CredentialResult {
28     struct Credential certificate;
29     bool bExpectResult;
30 };
31 
32 struct CredentialResult g_credentialexpectResult[] = {
33     {
34         { 1, "ak", "keyA", "oh:t=ak;o=keyA;u=0;a=0", 2, 1, { 1961, nullptr } }, true /* cert num is 2, len is 1961 */
35     }
36 };
37 
38 struct CredentialAbstractResult {
39     struct CredentialAbstract credentialAbstract;
40     bool bExpectResult;
41 };
42 
43 struct CredentialAbstractResult g_expectList[] = {
44     {
45         { "ak", "keyA", "oh:t=ak;o=keyA;u=100;a=500" }, false
46     },
47     {
48         { "ak", "keyA", "oh:t=ak;o=keyA;u=0;a=0" }, true
49     },
50     {
51         { "ak", "keyA", "oh:t=ak;o=keyA;u=200;a=0" }, false
52     }
53 };
54 
55 static const struct CmBlob g_appCert = { sizeof(g_rsa2048P12CertInfo), const_cast<uint8_t *>(g_rsa2048P12CertInfo) };
56 static const struct CmBlob g_eccAppCert = { sizeof(g_eccP256P12CertInfo), const_cast<uint8_t *>(g_eccP256P12CertInfo) };
57 static const struct CmBlob g_appCertPwd = { sizeof(g_certPwd), const_cast<uint8_t *>(g_certPwd) };
58 
59 static const uint8_t g_p12AbnormalCertinfo[] = {
60     0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 0x03, 0x30, 0x82, 0x0b, 0x87, 0x06, 0x09, 0x2a, 0x86, 0x48,
61     0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82, 0x0b, 0x78, 0x04, 0x82, 0x0b, 0x74, 0x30, 0x82,
62     0x0b, 0x70, 0x30, 0x82, 0x06, 0x27, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07,
63     0x06, 0xa0, 0x82, 0x06, 0x18, 0x30, 0x82, 0x06, 0x14, 0x02, 0x01, 0x00, 0x30, 0x82, 0x06, 0x0d,
64     0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x1c, 0x06, 0x0a, 0x2a,
65     0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0e, 0x04, 0x08, 0x1a, 0x8f, 0xc1,
66     0xd1, 0xda, 0x6c, 0xd1, 0xa9, 0x02, 0x02, 0x08, 0x00, 0x80, 0x82, 0x05, 0xe0, 0xd0, 0x2f, 0x2d,
67     0x52, 0x09, 0x86, 0x55, 0x53, 0xf0, 0x49, 0x8f, 0x00, 0xa1, 0x4d, 0x21, 0xc8, 0xb4, 0xad, 0x27,
68     0x12, 0x44, 0xab, 0x4d, 0x10, 0x14, 0xe3, 0x3c, 0x9a, 0x05, 0x77, 0x51, 0x90, 0x4a, 0x3a, 0x8a,
69     0x09, 0xa9, 0x4b, 0x36, 0x50, 0x60, 0x22, 0x4b, 0x77, 0x12, 0x5c, 0x2f, 0x60, 0xd3, 0xd9, 0x30,
70     0x94, 0x4d, 0x9e, 0x81, 0xc3, 0xe9, 0x9d, 0xd9, 0x47, 0xb3, 0x54, 0xa2, 0x9a, 0x8f, 0xe7, 0x58,
71     0x95, 0xd7, 0x48, 0x87, 0xc4, 0x40, 0xad, 0x9a, 0x42, 0x1d, 0x36, 0xb7, 0x48, 0xbc, 0x70, 0x8c,
72     0x84, 0xcb, 0x3c, 0x02, 0x25, 0x9f, 0xfe, 0x2c, 0x4a, 0x76, 0xb1, 0x27, 0x94, 0x8f, 0xb0, 0x07,
73     0xf0, 0xc0, 0x00, 0x3a, 0x69, 0x16, 0xe1, 0x63, 0x0c, 0xe5, 0x92, 0xc2, 0x7d, 0x99, 0xd9, 0x11,
74     0x40, 0xd8, 0x64, 0xab, 0x13, 0xda, 0x73, 0x7b, 0x12, 0x53, 0xb1, 0x0b, 0x0c, 0x67, 0x81, 0xe1,
75     0xf5, 0x59, 0x3a, 0xc7, 0xe0, 0xe9, 0xda, 0x12, 0xc7, 0x2b, 0xab, 0x3d, 0xbc, 0x10, 0x3d, 0x1a,
76     0x88, 0xc7, 0x1d, 0x31, 0x5f, 0x39, 0x63, 0x51, 0x8b, 0x11, 0x99, 0x05, 0xf9, 0x40, 0x42, 0x27,
77     0xad, 0x75, 0x6f, 0xe2, 0x2d, 0x66, 0x28, 0x97, 0x7c, 0x6f, 0xf4, 0xfc, 0x95, 0xaa, 0x67, 0x81,
78     0xd8, 0x15, 0x3c, 0xf4, 0x7b, 0x97, 0x08, 0x7b, 0x1b, 0x8c, 0xd3, 0x45, 0x8b, 0x96, 0x54, 0x2c,
79     0xb1, 0x00, 0x87, 0x59, 0x5c, 0x94, 0x78, 0x29, 0xaa, 0x7b, 0x9c, 0x5c, 0x61, 0xff, 0xcc, 0x32,
80     0x14, 0x4e, 0xc3, 0x1b, 0x96
81 };
82 
FindCredentialAbstract(const struct CredentialAbstract * abstract,const struct CredentialList * listCert)83 static bool FindCredentialAbstract(const struct CredentialAbstract *abstract, const struct CredentialList *listCert)
84 {
85     if (abstract == nullptr || listCert == nullptr || listCert->credentialCount == 0) {
86         return false;
87     }
88     for (uint32_t i = 0; i < listCert->credentialCount; ++i) {
89         if (CompareCredentialList(abstract, &(listCert->credentialAbstract[i]))) {
90             return true;
91         }
92     }
93     return false;
94 }
95 
96 class CmAppCertTest : public testing::Test {
97 public:
98     static void SetUpTestCase(void);
99 
100     static void TearDownTestCase(void);
101 
102     void SetUp();
103 
104     void TearDown();
105 };
106 
SetUpTestCase(void)107 void CmAppCertTest::SetUpTestCase(void)
108 {
109     SetATPermission();
110 }
111 
TearDownTestCase(void)112 void CmAppCertTest::TearDownTestCase(void)
113 {
114 }
115 
SetUp()116 void CmAppCertTest::SetUp()
117 {
118 }
119 
TearDown()120 void CmAppCertTest::TearDown()
121 {
122 }
123 
124 /**
125  * @tc.name: AppCertInstallBaseTest001
126  * @tc.desc: Test CertManager Install app cert interface base function
127  * @tc.type: FUNC
128  * @tc.require: AR000H0MI8 /SR000H09N9
129  */
130 HWTEST_F(CmAppCertTest, AppCertInstallBaseTest001, TestSize.Level0)
131 {
132     char retUriBuf[MAX_LEN_URI] = {0};
133     struct CmBlob keyUri = { sizeof(retUriBuf), reinterpret_cast<uint8_t *>(retUriBuf) };
134 
135     uint8_t certAliasBuf[] = "keyA";
136     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
137 
138     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
139     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertInstallBaseTest001 credentail test failed, retcode:" << ret;
140 
141     ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
142     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertInstallBaseTest001 pri_credentail test failed, retcode:" << ret;
143 
144     char uriBuf[] = "oh:t=ak;o=keyA;u=0;a=0";
145     EXPECT_EQ(strcmp(uriBuf, (char *)keyUri.data), 0) << "strcmp failed";
146 }
147 
148 /**
149  * @tc.name: AppCertInstallBaseTest002
150  * @tc.desc: Test CertManager Install app cert interface base function
151  * @tc.type: FUNC
152  * @tc.require: AR000H0MI8 /SR000H09N9
153  */
154 HWTEST_F(CmAppCertTest, AppCertInstallBaseTest002, TestSize.Level0)
155 {
156     uint8_t keyUriBuf[MAX_LEN_URI] = {0};
157     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
158 
159     uint8_t appCertPwdBuf[] = "12345678";
160     struct CmBlob appCertPwd = { sizeof(appCertPwdBuf), appCertPwdBuf };
161 
162     uint8_t certAliasBuf[] = "keyA";
163     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
164 
165     int32_t ret = CmInstallAppCert(&g_appCert, &appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
166     EXPECT_EQ(ret, CM_FAILURE) << "AppCertInstallBaseTest002 credentail test failed, retcode:" << ret;
167 
168     ret = CmInstallAppCert(&g_appCert, &appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
169     EXPECT_EQ(ret, CM_FAILURE) << "AppCertInstallBaseTest002 pri_credentail test failed, retcode:" << ret;
170 }
171 
172 /**
173  * @tc.name: AppCertInstallTest003
174  * @tc.desc: Test CertManager Install app cert interface base function
175  * @tc.type: FUNC
176  * @tc.require: AR000H0MI8 /SR000H09N9
177  */
178 HWTEST_F(CmAppCertTest, AppCertInstallTest003, TestSize.Level0)
179 {
180     uint8_t certAliasBuf[] = "AppCertInstallTest003";
181     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
182 
183     struct CmBlob appCert = { sizeof(g_p12AbnormalCertinfo), const_cast<uint8_t *>(g_p12AbnormalCertinfo) };
184 
185     uint8_t keyUriBuf[MAX_LEN_URI] = {0};
186     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
187 
188     int32_t ret = CmInstallAppCert(&appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
189     EXPECT_EQ(ret, CM_FAILURE) << "AppCertInstallTest003 credentail test failed, retcode:" << ret;
190 
191     ret = CmInstallAppCert(&appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
192     EXPECT_EQ(ret, CM_FAILURE) << "AppCertInstallTest003 pri_credentail test failed, retcode:" << ret;
193 }
194 
195 /**
196  * @tc.name: AppCertInstallAbnormalTest004
197  * @tc.desc: Test CertManager install app cert interface abnormal function
198  * @tc.type: FUNC
199  * @tc.require: AR000H0MI8 /SR000H09N9
200  */
201 HWTEST_F(CmAppCertTest, AppCertInstallAbnormalTest004, TestSize.Level0)
202 {
203     uint8_t certAliasBuf[] = "keyA";
204     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
205 
206     uint8_t keyUriBuf[MAX_LEN_URI] = {0};
207     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
208 
209     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE + 1, &keyUri);
210     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertInstallAbnormalTest004 test failed, retcode:" << ret;
211 
212     ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE + 1, &keyUri);
213     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertInstallAbnormalTest004 privite test failed, retcode:" << ret;
214 }
215 
216 /**
217  * @tc.name: AppCertInstallBaseEccTest005
218  * @tc.desc: Test CertManager Install app cert interface base function
219  * @tc.type: FUNC
220  * @tc.require: AR000H0MI8 /SR000H09N9
221  */
222 HWTEST_F(CmAppCertTest, AppCertInstallBaseEccTest005, TestSize.Level0)
223 {
224     uint8_t keyUriBuf[MAX_LEN_URI] = {0};
225     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
226 
227     uint8_t certAliasBuf[] = "keyB";
228     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
229 
230     int32_t ret = CmInstallAppCert(&g_eccAppCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
231     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertInstallBaseEccTest005 test failed, retcode:" << ret;
232 
233     ret = CmInstallAppCert(&g_eccAppCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
234     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertInstallBaseEccTest005 privite test failed, retcode:" << ret;
235 
236     ret = CmUninstallAllAppCert();
237     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllTest 005 test failed, retcode:" << ret;
238 }
239 
240 /**
241  * @tc.name: CmGetAppCertBaseTest001
242  * @tc.desc: Test CertManager get app cert interface base function
243  * @tc.type: FUNC
244  * @tc.require: AR000H0MI8 /SR000H09N9
245  */
246 HWTEST_F(CmAppCertTest, CmGetAppCertBaseTest001, TestSize.Level0)
247 {
248     uint8_t certAliasBuf[] = "keyA";
249     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
250 
251     uint8_t uriBuf[MAX_LEN_URI] = {0};
252     struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
253 
254     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
255     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertBaseTest001 test failed, retcode:" << ret;
256 
257     struct Credential certificate;
258     (void)memset_s(&certificate, sizeof(Credential), 0, sizeof(Credential));
259     certificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
260     ASSERT_TRUE(certificate.credData.data != nullptr);
261     certificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
262 
263     ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &certificate);
264     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertBaseTest001 test failed, retcode:" << ret;
265 
266     FreeCMBlobData(&certificate.credData);
267 }
268 
269 /**
270  * @tc.name: CmGetAppCertPerformanceTest002
271  * @tc.desc: Test CertManager get app cert interface performance
272  * @tc.type: FUNC
273  * @tc.require: AR000H0MI8 /SR000H09N9
274  */
275 HWTEST_F(CmAppCertTest, CmGetAppCertPerformanceTest002, TestSize.Level0)
276 {
277     uint32_t times = 1;
278     uint8_t certAliasBuf[] = "keyA";
279     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
280 
281     uint8_t uriBuf[MAX_LEN_URI] = {0};
282     struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
283 
284     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
285     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertPerformanceTest002 install failed, retcode:" << ret;
286 
287     struct Credential certificate;
288     for (uint32_t i = 0; i < times; i++) {
289         (void)memset_s(&certificate, sizeof(Credential), 0, sizeof(Credential));
290         certificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
291         ASSERT_TRUE(certificate.credData.data != nullptr);
292         certificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
293 
294         ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &certificate);
295         EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertPerformanceTest002 get failed,retcode:" << ret;
296 
297         EXPECT_EQ(CompareCredential(&certificate, &(g_credentialexpectResult[0].certificate)), true);
298         FreeCMBlobData(&(certificate.credData));
299     }
300 }
301 
302 /**
303  * @tc.name: CmGetAppBaseCertTest003
304  * @tc.desc: Test CertManager get app cert interface base function
305  * @tc.type: FUNC
306  * @tc.require: AR000H0MI8 /SR000H09N9
307  */
308 HWTEST_F(CmAppCertTest, CmGetAppBaseCertTest003, TestSize.Level0)
309 {
310     uint8_t certAliasBuf[] = "keyC";
311     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
312 
313     uint8_t uriBuf[MAX_LEN_URI] = {0};
314     struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
315 
316     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
317     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppBaseCertTest003 install failed, retcode:" << ret;
318 
319     struct Credential firstcertificate;
320     (void)memset_s(&firstcertificate, sizeof(Credential), 0, sizeof(Credential));
321     firstcertificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
322     ASSERT_TRUE(firstcertificate.credData.data != nullptr);
323     firstcertificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
324 
325     ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &firstcertificate);
326     EXPECT_EQ(ret, CM_SUCCESS) << "first CmGetAppCert failed, retcode:" << ret;
327 
328     struct Credential secondcertificate;
329     (void)memset_s(&secondcertificate, sizeof(Credential), 0, sizeof(Credential));
330     secondcertificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
331     ASSERT_TRUE(secondcertificate.credData.data != nullptr);
332     secondcertificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
333 
334     ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &secondcertificate);
335     EXPECT_EQ(ret, CM_SUCCESS) << "second CmGetAppCert failed, retcode:" << ret;
336 
337     EXPECT_EQ(CompareCredential(&firstcertificate, &secondcertificate), true);
338     FreeCMBlobData(&(firstcertificate.credData));
339     FreeCMBlobData(&(secondcertificate.credData));
340 }
341 
342 /**
343  * @tc.name: CmGetAppCertAbnormalTest004
344  * @tc.desc: Test CertManager get app cert interface abnormal function
345  * @tc.type: FUNC
346  * @tc.require: AR000H0MI8 /SR000H09N9
347  */
348 HWTEST_F(CmAppCertTest, CmGetAppCertAbnormalTest004, TestSize.Level0)
349 {
350     uint8_t keyUriBuf[] = "oh:t=ak;o=key004;u=0;a=0";
351     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
352 
353     struct Credential certificate;
354     (void)memset_s(&certificate, sizeof(Credential), 0, sizeof(Credential));
355     certificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
356     ASSERT_TRUE(certificate.credData.data != nullptr);
357     certificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
358 
359     int32_t ret = CmGetAppCert(nullptr, CM_CREDENTIAL_STORE, &certificate);
360     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmGetAppCertAbnormalTest004 01 failed, retcode:" << ret;
361 
362     ret = CmGetAppCert(&keyUri, CM_CREDENTIAL_STORE, nullptr);
363     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmGetAppCertAbnormalTest004 02 failed, retcode:" << ret;
364 
365     ret = CmGetAppCert(&keyUri, CM_PRI_CREDENTIAL_STORE + 1, &certificate);
366     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmGetAppCertAbnormalTest004 03 failed, retcode:" << ret;
367 
368     ret = CmUninstallAllAppCert();
369     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllTest 004 test failed, retcode:" << ret;
370 
371     FreeCMBlobData(&(certificate.credData));
372 }
373 
374 /**
375  * @tc.name: CmGetAppCertListPerformanceTest001
376  * @tc.desc: Test CertManager get app cert list interface performance
377  * @tc.type: FUNC
378  * @tc.require: AR000H0MI8 /SR000H09N9
379  */
380 HWTEST_F(CmAppCertTest, CmGetAppCertListPerformanceTest001, TestSize.Level0)
381 {
382     uint8_t certAliasBuf[] = "keyA";
383     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
384 
385     uint8_t uriBuf[MAX_LEN_URI] = {0};
386     struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
387 
388     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
389     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListPerformanceTest001 install failed, retcode:" << ret;
390 
391     ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
392     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListPerformanceTest001 private install failed, retcode:" << ret;
393 
394     struct CredentialList certificateList = { 0, nullptr };
395     uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract);
396     certificateList.credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
397     ASSERT_TRUE(certificateList.credentialAbstract != nullptr);
398 
399     uint32_t times = 1;
400     for (uint32_t i = 0; i < times; i++) {
401         certificateList.credentialCount = MAX_COUNT_CERTIFICATE;
402         (void)memset_s(certificateList.credentialAbstract, buffSize, 0, buffSize);
403         ret = CmGetAppCertList(CM_PRI_CREDENTIAL_STORE, &certificateList);
404         EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListTest test failed, retcode:" << ret;
405     }
406 
407     if (certificateList.credentialAbstract != nullptr) {
408         CmFree(certificateList.credentialAbstract);
409     }
410 }
411 
412 /**
413  * @tc.name: CmGetAppCertListBaseTest002
414  * @tc.desc: Test CertManager get app cert list interface base function
415  * @tc.type: FUNC
416  * @tc.require: AR000H0MI8 /SR000H09N9
417  */
418 HWTEST_F(CmAppCertTest, CmGetAppCertListBaseTest002, TestSize.Level0)
419 {
420     uint8_t certAliasBuf[] = "keyA";
421     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
422 
423     uint8_t uriBuf[MAX_LEN_URI] = {0};
424     struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
425 
426     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
427     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListBaseTest002 install failed, retcode:" << ret;
428 
429     struct CredentialList certificateList = { 0, nullptr };
430     uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract);
431     certificateList.credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
432     ASSERT_TRUE(certificateList.credentialAbstract != nullptr);
433     certificateList.credentialCount = MAX_COUNT_CERTIFICATE;
434     (void)memset_s(certificateList.credentialAbstract, buffSize, 0, buffSize);
435 
436     ret = CmGetAppCertList(CM_CREDENTIAL_STORE, &certificateList);
437     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListBaseTest002 test failed, retcode:" << ret;
438 
439     uint32_t length = sizeof(g_expectList) / sizeof(g_expectList[0]);
440     for (uint32_t j = 0; j < length; ++j) {
441         bool bFind = FindCredentialAbstract(&(g_expectList[j].credentialAbstract), &certificateList);
442         EXPECT_EQ(bFind, g_expectList[j].bExpectResult);
443     }
444 
445     if (certificateList.credentialAbstract != nullptr) {
446         CmFree(certificateList.credentialAbstract);
447     }
448 }
449 
450 /**
451  * @tc.name: CmGetAppCertListBaseTest003
452  * @tc.desc: Test CertManager get app cert list interface base function
453  * @tc.type: FUNC
454  * @tc.require: AR000H0MI8 /SR000H09N9
455  */
456 HWTEST_F(CmAppCertTest, CmGetAppCertListBaseTest003, TestSize.Level0)
457 {
458     int32_t ret = CmGetAppCertList(CM_CREDENTIAL_STORE, nullptr);
459     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "Abnormal CmGetAppCertListBaseTest003 test failed, retcode:" << ret;
460 }
461 
462 /**
463  * @tc.name: CmGetAppCertListAbnormalTest004
464  * @tc.desc: Test CertManager get app cert list interface abnormal function
465  * @tc.type: FUNC
466  * @tc.require: AR000H0MI8 /SR000H09N9
467  */
468 HWTEST_F(CmAppCertTest, CmGetAppCertListAbnormalTest004, TestSize.Level0)
469 {
470     struct CredentialList certificateList = { 0, nullptr };
471     uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract);
472     certificateList.credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
473     ASSERT_TRUE(certificateList.credentialAbstract != nullptr);
474     certificateList.credentialCount = MAX_COUNT_CERTIFICATE;
475     (void)memset_s(certificateList.credentialAbstract, buffSize, 0, buffSize);
476 
477     int32_t ret = CmGetAppCertList(CM_PRI_CREDENTIAL_STORE + 1, &certificateList);
478     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "Abnormal AppCertInstallTest04 test failed, retcode:" << ret;
479 
480     if (certificateList.credentialAbstract != nullptr) {
481         CmFree(certificateList.credentialAbstract);
482     }
483 }
484 
485 /**
486  * @tc.name: AppCertUnInstallBaseTest001
487  * @tc.desc: Test CertManager unInstall app cert interface base function
488  * @tc.type: FUNC
489  * @tc.require: AR000H0MI8 /SR000H09N9
490  */
491 HWTEST_F(CmAppCertTest, AppCertUnInstallBaseTest001, TestSize.Level0)
492 {
493     uint8_t certAliasBuf[] = "keyA";
494     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
495 
496     uint8_t uriBuf[MAX_LEN_URI] = {0};
497     struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
498 
499     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
500     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallBaseTest001 install failed, retcode:" << ret;
501 
502     ret = CmUninstallAppCert(&retUri, CM_CREDENTIAL_STORE);
503     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallBaseTest001 uninstall failed, retcode:" << ret;
504 }
505 
506 /**
507  * @tc.name: AppCertUnInstallAbnormalTest002
508  * @tc.desc: Test CertManager unInstall app cert interface abnormal function
509  * @tc.type: FUNC
510  * @tc.require: AR000H0MI8 /SR000H09N9
511  */
512 HWTEST_F(CmAppCertTest, AppCertUnInstallAbnormalTest002, TestSize.Level0)
513 {
514     int32_t ret = CmUninstallAppCert(nullptr, CM_CREDENTIAL_STORE);
515     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertUnInstallAbnormalTest002 test failed, retcode:" << ret;
516 }
517 
518 /**
519  * @tc.name: AppCertUnInstallAbnormalTest003
520  * @tc.desc: Test CertManager unInstall app cert interface abnormal function
521  * @tc.type: FUNC
522  * @tc.require: AR000H0MI8 /SR000H09N9
523  */
524 HWTEST_F(CmAppCertTest, AppCertUnInstallAbnormalTest003, TestSize.Level0)
525 {
526     uint8_t keyUriBuf[] = "oh:t=ak;o=keyA;u=0;a=0";
527     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
528 
529     int32_t ret = CmUninstallAppCert(&keyUri, CM_PRI_CREDENTIAL_STORE + 1);
530     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertUnInstallAbnormalTest003 test failed, retcode:" << ret;
531 }
532 
533 /**
534  * @tc.name: AppCertUnInstallAllAppCertBaseTest001
535  * @tc.desc: Test CertManager unInstall all app cert interface base function
536  * @tc.type: FUNC
537  * @tc.require: AR000H0MI8 /SR000H09N9
538  */
539 HWTEST_F(CmAppCertTest, AppCertUnInstallAllAppCertBaseTest001, TestSize.Level0)
540 {
541     uint8_t certAliasBuf[] = "keyB";
542     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
543 
544     uint8_t uriBuf[MAX_LEN_URI] = {0};
545     struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
546 
547     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
548     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertBaseTest001 1 failed, retcode:" << ret;
549 
550     ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
551     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertBaseTest001 2 failed, retcode:" << ret;
552 
553     ret = CmUninstallAllAppCert();
554     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertBaseTest001 test failed, retcode:" << ret;
555 }
556 
557 /**
558  * @tc.name: AppCertUnInstallAllAppCertAbnormalTest002
559  * @tc.desc: Test CertManager unInstall all app cert interface abnormal function
560  * @tc.type: FUNC
561  * @tc.require: AR000H0MI8 /SR000H09N9
562  */
563 HWTEST_F(CmAppCertTest, AppCertUnInstallAllAppCertAbnormalTest002, TestSize.Level0)
564 {
565     int32_t ret = CmUninstallAllAppCert();
566     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertAbnormalTest002 test failed, retcode:" << ret;
567 }
568 } // end of namespace
569 
570