• 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: AppCertInstallAbnormalTest005
242  * @tc.desc: Test CertManager Install app cert alias not include \0
243  * @tc.type: FUNC
244  * @tc.require: AR000H0MI8 /SR000H09N9
245  */
246 HWTEST_F(CmAppCertTest, AppCertInstallAbnormalTest005, TestSize.Level0)
247 {
248     uint8_t keyUriBuf[MAX_LEN_URI] = {0};
249     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
250 
251     uint8_t aliasBuf[] = "keyB";
252     struct CmBlob certAlias = { sizeof(aliasBuf) - 1, aliasBuf }; /* not include '\0' */
253 
254     int32_t ret = CmInstallAppCert(&g_eccAppCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
255     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertInstallAbnormalTest005 test failed, retcode:" << ret;
256 }
257 
258 /**
259  * @tc.name: AppCertInstallBaseEccTest006
260  * @tc.desc: Test CertManager Install app cert pwd not include \0
261  * @tc.type: FUNC
262  * @tc.require: AR000H0MI8 /SR000H09N9
263  */
264 HWTEST_F(CmAppCertTest, AppCertInstallAbnormalTest006, TestSize.Level0)
265 {
266     uint8_t keyUriBuf[MAX_LEN_URI] = {0};
267     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
268 
269     uint8_t aliasBuf[] = "keyC";
270     struct CmBlob certAlias = { sizeof(aliasBuf), aliasBuf };
271 
272     uint8_t pwdBuf[] = "123";
273     struct CmBlob pwd = { sizeof(pwdBuf) - 1, pwdBuf }; /* not include '\0' */
274 
275     int32_t ret = CmInstallAppCert(&g_eccAppCert, &pwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
276     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertInstallAbnormalTest006 test failed, retcode:" << ret;
277 }
278 
279 /**
280  * @tc.name: CmGetAppCertBaseTest001
281  * @tc.desc: Test CertManager get app cert interface base function
282  * @tc.type: FUNC
283  * @tc.require: AR000H0MI8 /SR000H09N9
284  */
285 HWTEST_F(CmAppCertTest, CmGetAppCertBaseTest001, TestSize.Level0)
286 {
287     uint8_t certAliasBuf[] = "keyA";
288     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
289 
290     uint8_t uriBuf[MAX_LEN_URI] = {0};
291     struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
292 
293     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
294     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertBaseTest001 test failed, retcode:" << ret;
295 
296     struct Credential certificate;
297     (void)memset_s(&certificate, sizeof(Credential), 0, sizeof(Credential));
298     certificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
299     ASSERT_TRUE(certificate.credData.data != nullptr);
300     certificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
301 
302     ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &certificate);
303     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertBaseTest001 test failed, retcode:" << ret;
304 
305     FreeCMBlobData(&certificate.credData);
306 }
307 
308 /**
309  * @tc.name: CmGetAppCertPerformanceTest002
310  * @tc.desc: Test CertManager get app cert interface performance
311  * @tc.type: FUNC
312  * @tc.require: AR000H0MI8 /SR000H09N9
313  */
314 HWTEST_F(CmAppCertTest, CmGetAppCertPerformanceTest002, TestSize.Level0)
315 {
316     uint32_t times = 1;
317     uint8_t certAliasBuf[] = "keyA";
318     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
319 
320     uint8_t uriBuf[MAX_LEN_URI] = {0};
321     struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
322 
323     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
324     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertPerformanceTest002 install failed, retcode:" << ret;
325 
326     struct Credential certificate;
327     for (uint32_t i = 0; i < times; i++) {
328         (void)memset_s(&certificate, sizeof(Credential), 0, sizeof(Credential));
329         certificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
330         ASSERT_TRUE(certificate.credData.data != nullptr);
331         certificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
332 
333         ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &certificate);
334         EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertPerformanceTest002 get failed,retcode:" << ret;
335 
336         EXPECT_EQ(CompareCredential(&certificate, &(g_credentialexpectResult[0].certificate)), true);
337         FreeCMBlobData(&(certificate.credData));
338     }
339 }
340 
341 /**
342  * @tc.name: CmGetAppBaseCertTest003
343  * @tc.desc: Test CertManager get app cert interface base function
344  * @tc.type: FUNC
345  * @tc.require: AR000H0MI8 /SR000H09N9
346  */
347 HWTEST_F(CmAppCertTest, CmGetAppBaseCertTest003, TestSize.Level0)
348 {
349     uint8_t certAliasBuf[] = "keyC";
350     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
351 
352     uint8_t uriBuf[MAX_LEN_URI] = {0};
353     struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
354 
355     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
356     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppBaseCertTest003 install failed, retcode:" << ret;
357 
358     struct Credential firstcertificate;
359     (void)memset_s(&firstcertificate, sizeof(Credential), 0, sizeof(Credential));
360     firstcertificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
361     ASSERT_TRUE(firstcertificate.credData.data != nullptr);
362     firstcertificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
363 
364     ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &firstcertificate);
365     EXPECT_EQ(ret, CM_SUCCESS) << "first CmGetAppCert failed, retcode:" << ret;
366 
367     struct Credential secondcertificate;
368     (void)memset_s(&secondcertificate, sizeof(Credential), 0, sizeof(Credential));
369     secondcertificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
370     ASSERT_TRUE(secondcertificate.credData.data != nullptr);
371     secondcertificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
372 
373     ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &secondcertificate);
374     EXPECT_EQ(ret, CM_SUCCESS) << "second CmGetAppCert failed, retcode:" << ret;
375 
376     EXPECT_EQ(CompareCredential(&firstcertificate, &secondcertificate), true);
377     FreeCMBlobData(&(firstcertificate.credData));
378     FreeCMBlobData(&(secondcertificate.credData));
379 }
380 
381 /**
382  * @tc.name: CmGetAppCertAbnormalTest004
383  * @tc.desc: Test CertManager get app cert interface abnormal function
384  * @tc.type: FUNC
385  * @tc.require: AR000H0MI8 /SR000H09N9
386  */
387 HWTEST_F(CmAppCertTest, CmGetAppCertAbnormalTest004, TestSize.Level0)
388 {
389     uint8_t keyUriBuf[] = "oh:t=ak;o=key004;u=0;a=0";
390     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
391 
392     struct Credential certificate;
393     (void)memset_s(&certificate, sizeof(Credential), 0, sizeof(Credential));
394     certificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
395     ASSERT_TRUE(certificate.credData.data != nullptr);
396     certificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
397 
398     int32_t ret = CmGetAppCert(nullptr, CM_CREDENTIAL_STORE, &certificate);
399     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmGetAppCertAbnormalTest004 01 failed, retcode:" << ret;
400 
401     ret = CmGetAppCert(&keyUri, CM_CREDENTIAL_STORE, nullptr);
402     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmGetAppCertAbnormalTest004 02 failed, retcode:" << ret;
403 
404     ret = CmGetAppCert(&keyUri, CM_PRI_CREDENTIAL_STORE + 1, &certificate);
405     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmGetAppCertAbnormalTest004 03 failed, retcode:" << ret;
406 
407     ret = CmUninstallAllAppCert();
408     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllTest 004 test failed, retcode:" << ret;
409 
410     FreeCMBlobData(&(certificate.credData));
411 }
412 
413 /**
414  * @tc.name: CmGetAppCertListPerformanceTest001
415  * @tc.desc: Test CertManager get app cert list interface performance
416  * @tc.type: FUNC
417  * @tc.require: AR000H0MI8 /SR000H09N9
418  */
419 HWTEST_F(CmAppCertTest, CmGetAppCertListPerformanceTest001, TestSize.Level0)
420 {
421     uint8_t certAliasBuf[] = "keyA";
422     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
423 
424     uint8_t uriBuf[MAX_LEN_URI] = {0};
425     struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
426 
427     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
428     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListPerformanceTest001 install failed, retcode:" << ret;
429 
430     ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
431     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListPerformanceTest001 private install failed, retcode:" << ret;
432 
433     struct CredentialList certificateList = { 0, nullptr };
434     uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract);
435     certificateList.credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
436     ASSERT_TRUE(certificateList.credentialAbstract != nullptr);
437 
438     uint32_t times = 1;
439     for (uint32_t i = 0; i < times; i++) {
440         certificateList.credentialCount = MAX_COUNT_CERTIFICATE;
441         (void)memset_s(certificateList.credentialAbstract, buffSize, 0, buffSize);
442         ret = CmGetAppCertList(CM_PRI_CREDENTIAL_STORE, &certificateList);
443         EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListTest test failed, retcode:" << ret;
444     }
445 
446     if (certificateList.credentialAbstract != nullptr) {
447         CmFree(certificateList.credentialAbstract);
448     }
449 }
450 
451 /**
452  * @tc.name: CmGetAppCertListBaseTest002
453  * @tc.desc: Test CertManager get app cert list interface base function
454  * @tc.type: FUNC
455  * @tc.require: AR000H0MI8 /SR000H09N9
456  */
457 HWTEST_F(CmAppCertTest, CmGetAppCertListBaseTest002, TestSize.Level0)
458 {
459     uint8_t certAliasBuf[] = "keyA";
460     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
461 
462     uint8_t uriBuf[MAX_LEN_URI] = {0};
463     struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
464 
465     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
466     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListBaseTest002 install failed, retcode:" << ret;
467 
468     struct CredentialList certificateList = { 0, nullptr };
469     uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract);
470     certificateList.credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
471     ASSERT_TRUE(certificateList.credentialAbstract != nullptr);
472     certificateList.credentialCount = MAX_COUNT_CERTIFICATE;
473     (void)memset_s(certificateList.credentialAbstract, buffSize, 0, buffSize);
474 
475     ret = CmGetAppCertList(CM_CREDENTIAL_STORE, &certificateList);
476     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListBaseTest002 test failed, retcode:" << ret;
477 
478     uint32_t length = sizeof(g_expectList) / sizeof(g_expectList[0]);
479     for (uint32_t j = 0; j < length; ++j) {
480         bool bFind = FindCredentialAbstract(&(g_expectList[j].credentialAbstract), &certificateList);
481         EXPECT_EQ(bFind, g_expectList[j].bExpectResult);
482     }
483 
484     if (certificateList.credentialAbstract != nullptr) {
485         CmFree(certificateList.credentialAbstract);
486     }
487 }
488 
489 /**
490  * @tc.name: CmGetAppCertListBaseTest003
491  * @tc.desc: Test CertManager get app cert list interface base function
492  * @tc.type: FUNC
493  * @tc.require: AR000H0MI8 /SR000H09N9
494  */
495 HWTEST_F(CmAppCertTest, CmGetAppCertListBaseTest003, TestSize.Level0)
496 {
497     int32_t ret = CmGetAppCertList(CM_CREDENTIAL_STORE, nullptr);
498     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "Abnormal CmGetAppCertListBaseTest003 test failed, retcode:" << ret;
499 }
500 
501 /**
502  * @tc.name: CmGetAppCertListAbnormalTest004
503  * @tc.desc: Test CertManager get app cert list interface abnormal function
504  * @tc.type: FUNC
505  * @tc.require: AR000H0MI8 /SR000H09N9
506  */
507 HWTEST_F(CmAppCertTest, CmGetAppCertListAbnormalTest004, TestSize.Level0)
508 {
509     struct CredentialList certificateList = { 0, nullptr };
510     uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract);
511     certificateList.credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
512     ASSERT_TRUE(certificateList.credentialAbstract != nullptr);
513     certificateList.credentialCount = MAX_COUNT_CERTIFICATE;
514     (void)memset_s(certificateList.credentialAbstract, buffSize, 0, buffSize);
515 
516     int32_t ret = CmGetAppCertList(CM_PRI_CREDENTIAL_STORE + 1, &certificateList);
517     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "Abnormal AppCertInstallTest04 test failed, retcode:" << ret;
518 
519     if (certificateList.credentialAbstract != nullptr) {
520         CmFree(certificateList.credentialAbstract);
521     }
522 }
523 
524 /**
525  * @tc.name: AppCertUnInstallBaseTest001
526  * @tc.desc: Test CertManager unInstall app cert interface base function
527  * @tc.type: FUNC
528  * @tc.require: AR000H0MI8 /SR000H09N9
529  */
530 HWTEST_F(CmAppCertTest, AppCertUnInstallBaseTest001, TestSize.Level0)
531 {
532     uint8_t certAliasBuf[] = "keyA";
533     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
534 
535     uint8_t uriBuf[MAX_LEN_URI] = {0};
536     struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
537 
538     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
539     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallBaseTest001 install failed, retcode:" << ret;
540 
541     ret = CmUninstallAppCert(&retUri, CM_CREDENTIAL_STORE);
542     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallBaseTest001 uninstall failed, retcode:" << ret;
543 }
544 
545 /**
546  * @tc.name: AppCertUnInstallAbnormalTest002
547  * @tc.desc: Test CertManager unInstall app cert interface abnormal function
548  * @tc.type: FUNC
549  * @tc.require: AR000H0MI8 /SR000H09N9
550  */
551 HWTEST_F(CmAppCertTest, AppCertUnInstallAbnormalTest002, TestSize.Level0)
552 {
553     int32_t ret = CmUninstallAppCert(nullptr, CM_CREDENTIAL_STORE);
554     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertUnInstallAbnormalTest002 test failed, retcode:" << ret;
555 }
556 
557 /**
558  * @tc.name: AppCertUnInstallAbnormalTest003
559  * @tc.desc: Test CertManager unInstall app cert interface abnormal function
560  * @tc.type: FUNC
561  * @tc.require: AR000H0MI8 /SR000H09N9
562  */
563 HWTEST_F(CmAppCertTest, AppCertUnInstallAbnormalTest003, TestSize.Level0)
564 {
565     uint8_t keyUriBuf[] = "oh:t=ak;o=keyA;u=0;a=0";
566     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
567 
568     int32_t ret = CmUninstallAppCert(&keyUri, CM_PRI_CREDENTIAL_STORE + 1);
569     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertUnInstallAbnormalTest003 test failed, retcode:" << ret;
570 }
571 
572 /**
573  * @tc.name: AppCertUnInstallAllAppCertBaseTest001
574  * @tc.desc: Test CertManager unInstall all app cert interface base function
575  * @tc.type: FUNC
576  * @tc.require: AR000H0MI8 /SR000H09N9
577  */
578 HWTEST_F(CmAppCertTest, AppCertUnInstallAllAppCertBaseTest001, TestSize.Level0)
579 {
580     uint8_t certAliasBuf[] = "keyB";
581     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
582 
583     uint8_t uriBuf[MAX_LEN_URI] = {0};
584     struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
585 
586     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
587     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertBaseTest001 1 failed, retcode:" << ret;
588 
589     ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
590     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertBaseTest001 2 failed, retcode:" << ret;
591 
592     ret = CmUninstallAllAppCert();
593     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertBaseTest001 test failed, retcode:" << ret;
594 }
595 
596 /**
597  * @tc.name: AppCertUnInstallAllAppCertAbnormalTest002
598  * @tc.desc: Test CertManager unInstall all app cert interface abnormal function
599  * @tc.type: FUNC
600  * @tc.require: AR000H0MI8 /SR000H09N9
601  */
602 HWTEST_F(CmAppCertTest, AppCertUnInstallAllAppCertAbnormalTest002, TestSize.Level0)
603 {
604     int32_t ret = CmUninstallAllAppCert();
605     EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertAbnormalTest002 test failed, retcode:" << ret;
606 }
607 } // end of namespace
608 
609