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