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, CM_FAILURE) << "AppCertInstallBaseTest002 credentail test failed, retcode:" << ret;
168
169 ret = CmInstallAppCert(&g_appCert, &appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
170 EXPECT_EQ(ret, CM_FAILURE) << "AppCertInstallBaseTest002 pri_credentail test failed, retcode:" << ret;
171 }
172
173 /**
174 * @tc.name: AppCertInstallTest003
175 * @tc.desc: Test CertManager Install app cert interface base function
176 * @tc.type: FUNC
177 * @tc.require: AR000H0MI8 /SR000H09N9
178 */
179 HWTEST_F(CmAppCertTest, AppCertInstallTest003, TestSize.Level0)
180 {
181 uint8_t certAliasBuf[] = "AppCertInstallTest003";
182 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
183
184 struct CmBlob appCert = { sizeof(g_p12AbnormalCertinfo), const_cast<uint8_t *>(g_p12AbnormalCertinfo) };
185
186 uint8_t keyUriBuf[MAX_LEN_URI] = {0};
187 struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
188
189 int32_t ret = CmInstallAppCert(&appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
190 EXPECT_EQ(ret, CM_FAILURE) << "AppCertInstallTest003 credentail test failed, retcode:" << ret;
191
192 ret = CmInstallAppCert(&appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
193 EXPECT_EQ(ret, CM_FAILURE) << "AppCertInstallTest003 pri_credentail test failed, retcode:" << ret;
194 }
195
196 /**
197 * @tc.name: AppCertInstallAbnormalTest004
198 * @tc.desc: Test CertManager install app cert interface abnormal function
199 * @tc.type: FUNC
200 * @tc.require: AR000H0MI8 /SR000H09N9
201 */
202 HWTEST_F(CmAppCertTest, AppCertInstallAbnormalTest004, TestSize.Level0)
203 {
204 uint8_t certAliasBuf[] = "keyA";
205 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
206
207 uint8_t keyUriBuf[MAX_LEN_URI] = {0};
208 struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
209
210 int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE + 1, &keyUri);
211 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertInstallAbnormalTest004 test failed, retcode:" << ret;
212
213 ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE + 1, &keyUri);
214 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertInstallAbnormalTest004 privite test failed, retcode:" << ret;
215 }
216
217 /**
218 * @tc.name: AppCertInstallBaseEccTest005
219 * @tc.desc: Test CertManager Install app cert interface base function
220 * @tc.type: FUNC
221 * @tc.require: AR000H0MI8 /SR000H09N9
222 */
223 HWTEST_F(CmAppCertTest, AppCertInstallBaseEccTest005, TestSize.Level0)
224 {
225 uint8_t keyUriBuf[MAX_LEN_URI] = {0};
226 struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
227
228 uint8_t certAliasBuf[] = "keyB";
229 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
230
231 int32_t ret = CmInstallAppCert(&g_eccAppCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
232 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertInstallBaseEccTest005 test failed, retcode:" << ret;
233
234 ret = CmInstallAppCert(&g_eccAppCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
235 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertInstallBaseEccTest005 privite test failed, retcode:" << ret;
236
237 ret = CmUninstallAllAppCert();
238 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllTest 005 test failed, retcode:" << ret;
239 }
240
241 /**
242 * @tc.name: AppCertInstallAbnormalTest005
243 * @tc.desc: Test CertManager Install app cert alias not include \0
244 * @tc.type: FUNC
245 * @tc.require: AR000H0MI8 /SR000H09N9
246 */
247 HWTEST_F(CmAppCertTest, AppCertInstallAbnormalTest005, TestSize.Level0)
248 {
249 uint8_t keyUriBuf[MAX_LEN_URI] = {0};
250 struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
251
252 uint8_t aliasBuf[] = "keyB";
253 struct CmBlob certAlias = { sizeof(aliasBuf) - 1, aliasBuf }; /* not include '\0' */
254
255 int32_t ret = CmInstallAppCert(&g_eccAppCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
256 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertInstallAbnormalTest005 test failed, retcode:" << ret;
257 }
258
259 /**
260 * @tc.name: AppCertInstallBaseEccTest006
261 * @tc.desc: Test CertManager Install app cert pwd not include \0
262 * @tc.type: FUNC
263 * @tc.require: AR000H0MI8 /SR000H09N9
264 */
265 HWTEST_F(CmAppCertTest, AppCertInstallAbnormalTest006, TestSize.Level0)
266 {
267 uint8_t keyUriBuf[MAX_LEN_URI] = {0};
268 struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
269
270 uint8_t aliasBuf[] = "keyC";
271 struct CmBlob certAlias = { sizeof(aliasBuf), aliasBuf };
272
273 uint8_t pwdBuf[] = "123";
274 struct CmBlob pwd = { sizeof(pwdBuf) - 1, pwdBuf }; /* not include '\0' */
275
276 int32_t ret = CmInstallAppCert(&g_eccAppCert, &pwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
277 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertInstallAbnormalTest006 test failed, retcode:" << ret;
278 }
279
280 /**
281 * @tc.name: CmGetAppCertBaseTest001
282 * @tc.desc: Test CertManager get app cert interface base function
283 * @tc.type: FUNC
284 * @tc.require: AR000H0MI8 /SR000H09N9
285 */
286 HWTEST_F(CmAppCertTest, CmGetAppCertBaseTest001, TestSize.Level0)
287 {
288 uint8_t certAliasBuf[] = "keyA";
289 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
290
291 uint8_t uriBuf[MAX_LEN_URI] = {0};
292 struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
293
294 int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
295 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertBaseTest001 test failed, retcode:" << ret;
296
297 struct Credential certificate;
298 (void)memset_s(&certificate, sizeof(Credential), 0, sizeof(Credential));
299 certificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
300 ASSERT_TRUE(certificate.credData.data != nullptr);
301 certificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
302
303 ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &certificate);
304 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertBaseTest001 test failed, retcode:" << ret;
305
306 FreeCMBlobData(&certificate.credData);
307 }
308
309 /**
310 * @tc.name: CmGetAppCertPerformanceTest002
311 * @tc.desc: Test CertManager get app cert interface performance
312 * @tc.type: FUNC
313 * @tc.require: AR000H0MI8 /SR000H09N9
314 */
315 HWTEST_F(CmAppCertTest, CmGetAppCertPerformanceTest002, TestSize.Level0)
316 {
317 uint32_t times = 1;
318 uint8_t certAliasBuf[] = "keyA";
319 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
320
321 uint8_t uriBuf[MAX_LEN_URI] = {0};
322 struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
323
324 int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
325 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertPerformanceTest002 install failed, retcode:" << ret;
326
327 struct Credential certificate;
328 for (uint32_t i = 0; i < times; i++) {
329 (void)memset_s(&certificate, sizeof(Credential), 0, sizeof(Credential));
330 certificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
331 ASSERT_TRUE(certificate.credData.data != nullptr);
332 certificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
333
334 ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &certificate);
335 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertPerformanceTest002 get failed,retcode:" << ret;
336
337 EXPECT_EQ(CompareCredential(&certificate, &(g_credentialexpectResult[0].certificate)), true);
338 FreeCMBlobData(&(certificate.credData));
339 }
340 }
341
342 /**
343 * @tc.name: CmGetAppBaseCertTest003
344 * @tc.desc: Test CertManager get app cert interface base function
345 * @tc.type: FUNC
346 * @tc.require: AR000H0MI8 /SR000H09N9
347 */
348 HWTEST_F(CmAppCertTest, CmGetAppBaseCertTest003, TestSize.Level0)
349 {
350 uint8_t certAliasBuf[] = "keyC";
351 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
352
353 uint8_t uriBuf[MAX_LEN_URI] = {0};
354 struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
355
356 int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
357 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppBaseCertTest003 install failed, retcode:" << ret;
358
359 struct Credential firstcertificate;
360 (void)memset_s(&firstcertificate, sizeof(Credential), 0, sizeof(Credential));
361 firstcertificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
362 ASSERT_TRUE(firstcertificate.credData.data != nullptr);
363 firstcertificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
364
365 ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &firstcertificate);
366 EXPECT_EQ(ret, CM_SUCCESS) << "first CmGetAppCert failed, retcode:" << ret;
367
368 struct Credential secondcertificate;
369 (void)memset_s(&secondcertificate, sizeof(Credential), 0, sizeof(Credential));
370 secondcertificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
371 ASSERT_TRUE(secondcertificate.credData.data != nullptr);
372 secondcertificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
373
374 ret = CmGetAppCert(&retUri, CM_CREDENTIAL_STORE, &secondcertificate);
375 EXPECT_EQ(ret, CM_SUCCESS) << "second CmGetAppCert failed, retcode:" << ret;
376
377 EXPECT_EQ(CompareCredential(&firstcertificate, &secondcertificate), true);
378 FreeCMBlobData(&(firstcertificate.credData));
379 FreeCMBlobData(&(secondcertificate.credData));
380 }
381
382 /**
383 * @tc.name: CmGetAppCertAbnormalTest004
384 * @tc.desc: Test CertManager get app cert interface abnormal function
385 * @tc.type: FUNC
386 * @tc.require: AR000H0MI8 /SR000H09N9
387 */
388 HWTEST_F(CmAppCertTest, CmGetAppCertAbnormalTest004, TestSize.Level0)
389 {
390 uint8_t keyUriBuf[] = "oh:t=ak;o=key004;u=0;a=0";
391 struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
392
393 struct Credential certificate;
394 (void)memset_s(&certificate, sizeof(Credential), 0, sizeof(Credential));
395 certificate.credData.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE_CHAIN));
396 ASSERT_TRUE(certificate.credData.data != nullptr);
397 certificate.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
398
399 int32_t ret = CmGetAppCert(nullptr, CM_CREDENTIAL_STORE, &certificate);
400 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmGetAppCertAbnormalTest004 01 failed, retcode:" << ret;
401
402 ret = CmGetAppCert(&keyUri, CM_CREDENTIAL_STORE, nullptr);
403 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmGetAppCertAbnormalTest004 02 failed, retcode:" << ret;
404
405 ret = CmGetAppCert(&keyUri, CM_PRI_CREDENTIAL_STORE + 1, &certificate);
406 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmGetAppCertAbnormalTest004 03 failed, retcode:" << ret;
407
408 ret = CmUninstallAllAppCert();
409 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllTest 004 test failed, retcode:" << ret;
410
411 FreeCMBlobData(&(certificate.credData));
412 }
413
414 /**
415 * @tc.name: CmGetAppCertListPerformanceTest001
416 * @tc.desc: Test CertManager get app cert list interface performance
417 * @tc.type: FUNC
418 * @tc.require: AR000H0MI8 /SR000H09N9
419 */
420 HWTEST_F(CmAppCertTest, CmGetAppCertListPerformanceTest001, TestSize.Level0)
421 {
422 uint8_t certAliasBuf[] = "keyA";
423 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
424
425 uint8_t uriBuf[MAX_LEN_URI] = {0};
426 struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
427
428 int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
429 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListPerformanceTest001 install failed, retcode:" << ret;
430
431 ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
432 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListPerformanceTest001 private install failed, retcode:" << ret;
433
434 struct CredentialList certificateList = { 0, nullptr };
435 uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract);
436 certificateList.credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
437 ASSERT_TRUE(certificateList.credentialAbstract != nullptr);
438
439 uint32_t times = 1;
440 for (uint32_t i = 0; i < times; i++) {
441 certificateList.credentialCount = MAX_COUNT_CERTIFICATE;
442 (void)memset_s(certificateList.credentialAbstract, buffSize, 0, buffSize);
443 ret = CmGetAppCertList(CM_PRI_CREDENTIAL_STORE, &certificateList);
444 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListTest test failed, retcode:" << ret;
445 }
446
447 if (certificateList.credentialAbstract != nullptr) {
448 CmFree(certificateList.credentialAbstract);
449 }
450 }
451
452 /**
453 * @tc.name: CmGetAppCertListBaseTest002
454 * @tc.desc: Test CertManager get app cert list interface base function
455 * @tc.type: FUNC
456 * @tc.require: AR000H0MI8 /SR000H09N9
457 */
458 HWTEST_F(CmAppCertTest, CmGetAppCertListBaseTest002, TestSize.Level0)
459 {
460 uint8_t certAliasBuf[] = "keyA";
461 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
462
463 uint8_t uriBuf[MAX_LEN_URI] = {0};
464 struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
465
466 int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
467 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListBaseTest002 install failed, retcode:" << ret;
468
469 struct CredentialList certificateList = { 0, nullptr };
470 uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract);
471 certificateList.credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
472 ASSERT_TRUE(certificateList.credentialAbstract != nullptr);
473 certificateList.credentialCount = MAX_COUNT_CERTIFICATE;
474 (void)memset_s(certificateList.credentialAbstract, buffSize, 0, buffSize);
475
476 ret = CmGetAppCertList(CM_CREDENTIAL_STORE, &certificateList);
477 EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAppCertListBaseTest002 test failed, retcode:" << ret;
478
479 uint32_t length = sizeof(g_expectList) / sizeof(g_expectList[0]);
480 for (uint32_t j = 0; j < length; ++j) {
481 bool bFind = FindCredentialAbstract(&(g_expectList[j].credentialAbstract), &certificateList);
482 EXPECT_EQ(bFind, g_expectList[j].bExpectResult);
483 }
484
485 if (certificateList.credentialAbstract != nullptr) {
486 CmFree(certificateList.credentialAbstract);
487 }
488 }
489
490 /**
491 * @tc.name: CmGetAppCertListBaseTest003
492 * @tc.desc: Test CertManager get app cert list interface base function
493 * @tc.type: FUNC
494 * @tc.require: AR000H0MI8 /SR000H09N9
495 */
496 HWTEST_F(CmAppCertTest, CmGetAppCertListBaseTest003, TestSize.Level0)
497 {
498 int32_t ret = CmGetAppCertList(CM_CREDENTIAL_STORE, nullptr);
499 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "Abnormal CmGetAppCertListBaseTest003 test failed, retcode:" << ret;
500 }
501
502 /**
503 * @tc.name: CmGetAppCertListAbnormalTest004
504 * @tc.desc: Test CertManager get app cert list interface abnormal function
505 * @tc.type: FUNC
506 * @tc.require: AR000H0MI8 /SR000H09N9
507 */
508 HWTEST_F(CmAppCertTest, CmGetAppCertListAbnormalTest004, TestSize.Level0)
509 {
510 struct CredentialList certificateList = { 0, nullptr };
511 uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract);
512 certificateList.credentialAbstract = static_cast<struct CredentialAbstract *>(CmMalloc(buffSize));
513 ASSERT_TRUE(certificateList.credentialAbstract != nullptr);
514 certificateList.credentialCount = MAX_COUNT_CERTIFICATE;
515 (void)memset_s(certificateList.credentialAbstract, buffSize, 0, buffSize);
516
517 int32_t ret = CmGetAppCertList(CM_PRI_CREDENTIAL_STORE + 1, &certificateList);
518 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "Abnormal AppCertInstallTest04 test failed, retcode:" << ret;
519
520 if (certificateList.credentialAbstract != nullptr) {
521 CmFree(certificateList.credentialAbstract);
522 }
523 }
524
525 /**
526 * @tc.name: AppCertUnInstallBaseTest001
527 * @tc.desc: Test CertManager unInstall app cert interface base function
528 * @tc.type: FUNC
529 * @tc.require: AR000H0MI8 /SR000H09N9
530 */
531 HWTEST_F(CmAppCertTest, AppCertUnInstallBaseTest001, TestSize.Level0)
532 {
533 uint8_t certAliasBuf[] = "keyA";
534 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
535
536 uint8_t uriBuf[MAX_LEN_URI] = {0};
537 struct CmBlob retUri = { sizeof(uriBuf), uriBuf };
538
539 int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &retUri);
540 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallBaseTest001 install failed, retcode:" << ret;
541
542 ret = CmUninstallAppCert(&retUri, CM_CREDENTIAL_STORE);
543 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallBaseTest001 uninstall failed, retcode:" << ret;
544 }
545
546 /**
547 * @tc.name: AppCertUnInstallAbnormalTest002
548 * @tc.desc: Test CertManager unInstall app cert interface abnormal function
549 * @tc.type: FUNC
550 * @tc.require: AR000H0MI8 /SR000H09N9
551 */
552 HWTEST_F(CmAppCertTest, AppCertUnInstallAbnormalTest002, TestSize.Level0)
553 {
554 int32_t ret = CmUninstallAppCert(nullptr, CM_CREDENTIAL_STORE);
555 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertUnInstallAbnormalTest002 test failed, retcode:" << ret;
556 }
557
558 /**
559 * @tc.name: AppCertUnInstallAbnormalTest003
560 * @tc.desc: Test CertManager unInstall app cert interface abnormal function
561 * @tc.type: FUNC
562 * @tc.require: AR000H0MI8 /SR000H09N9
563 */
564 HWTEST_F(CmAppCertTest, AppCertUnInstallAbnormalTest003, TestSize.Level0)
565 {
566 uint8_t keyUriBuf[] = "oh:t=ak;o=keyA;u=0;a=0";
567 struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
568
569 int32_t ret = CmUninstallAppCert(&keyUri, CM_PRI_CREDENTIAL_STORE + 1);
570 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "AppCertUnInstallAbnormalTest003 test failed, retcode:" << ret;
571 }
572
573 /**
574 * @tc.name: AppCertUnInstallAllAppCertBaseTest001
575 * @tc.desc: Test CertManager unInstall all app cert interface base function
576 * @tc.type: FUNC
577 * @tc.require: AR000H0MI8 /SR000H09N9
578 */
579 HWTEST_F(CmAppCertTest, AppCertUnInstallAllAppCertBaseTest001, TestSize.Level0)
580 {
581 uint8_t certAliasBuf[] = "keyB";
582 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
583
584 uint8_t uriBuf[MAX_LEN_URI] = {0};
585 struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
586
587 int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
588 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertBaseTest001 1 failed, retcode:" << ret;
589
590 ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
591 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertBaseTest001 2 failed, retcode:" << ret;
592
593 ret = CmUninstallAllAppCert();
594 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertBaseTest001 test failed, retcode:" << ret;
595 }
596
597 /**
598 * @tc.name: AppCertUnInstallAllAppCertAbnormalTest002
599 * @tc.desc: Test CertManager unInstall all app cert interface abnormal function
600 * @tc.type: FUNC
601 * @tc.require: AR000H0MI8 /SR000H09N9
602 */
603 HWTEST_F(CmAppCertTest, AppCertUnInstallAllAppCertAbnormalTest002, TestSize.Level0)
604 {
605 int32_t ret = CmUninstallAllAppCert();
606 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertUnInstallAllAppCertAbnormalTest002 test failed, retcode:" << ret;
607 }
608
609 /**
610 * @tc.name: AppCertInstallPwdTest001
611 * @tc.desc: Test CertManager Install app cert interface base function with 12 bytes special pwd, success
612 * @tc.type: FUNC
613 */
614 HWTEST_F(CmAppCertTest, AppCertInstallPwdTest001, TestSize.Level0)
615 {
616 uint8_t appCertPwdBuf[] = "123456!@#$%^";
617 struct CmBlob appCertPwd = { sizeof(appCertPwdBuf), appCertPwdBuf };
618
619 const struct CmBlob appCert = { sizeof(g_rsa2048P12CertInfoPwd12),
620 const_cast<uint8_t *>(g_rsa2048P12CertInfoPwd12) };
621
622 char retUriBuf[MAX_LEN_URI] = {0};
623 struct CmBlob keyUri = { sizeof(retUriBuf), reinterpret_cast<uint8_t *>(retUriBuf) };
624
625 uint8_t certAliasBuf[] = "keyA";
626 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
627
628 int32_t ret = CmInstallAppCert(&appCert, &appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
629 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertInstallPwdTest001 12 bytes sepcial pwd test failed, retcode:" << ret;
630
631 (void)CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
632 }
633
634 /**
635 * @tc.name: AppCertInstallPwdTest002
636 * @tc.desc: Test CertManager Install app cert interface base function with 32 bytes special pwd, success
637 * @tc.type: FUNC
638 */
639 HWTEST_F(CmAppCertTest, AppCertInstallPwdTest002, TestSize.Level0)
640 {
641 uint8_t appCertPwdBuf[] = "1234567890!@#$%^&*()_+0987654321";
642 struct CmBlob appCertPwd = { sizeof(appCertPwdBuf), appCertPwdBuf };
643
644 const struct CmBlob appCert = { sizeof(g_rsa2048P12CertInfoPwd32),
645 const_cast<uint8_t *>(g_rsa2048P12CertInfoPwd32) };
646
647 char retUriBuf[MAX_LEN_URI] = {0};
648 struct CmBlob keyUri = { sizeof(retUriBuf), reinterpret_cast<uint8_t *>(retUriBuf) };
649
650 uint8_t certAliasBuf[] = "keyA";
651 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
652
653 int32_t ret = CmInstallAppCert(&appCert, &appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
654 EXPECT_EQ(ret, CM_SUCCESS) << "AppCertInstallPwdTest002 32 bytes sepcial pwd test failed, retcode:" << ret;
655
656 (void)CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
657 }
658
659 /**
660 * @tc.name: AppCertInstallPwdTest003
661 * @tc.desc: Test CertManager Install app cert interface base function with 33 bytes special pwd, failed
662 * @tc.type: FUNC
663 */
664 HWTEST_F(CmAppCertTest, AppCertInstallPwdTest003, TestSize.Level0)
665 {
666 uint8_t appCertPwdBuf[] = "1234567890!@#$%^&*()_+0987654321~";
667 struct CmBlob appCertPwd = { sizeof(appCertPwdBuf), appCertPwdBuf };
668
669 const struct CmBlob appCert = { sizeof(g_rsa2048P12CertInfoPwd33),
670 const_cast<uint8_t *>(g_rsa2048P12CertInfoPwd33) };
671
672 char retUriBuf[MAX_LEN_URI] = {0};
673 struct CmBlob keyUri = { sizeof(retUriBuf), reinterpret_cast<uint8_t *>(retUriBuf) };
674
675 uint8_t certAliasBuf[] = "keyA";
676 struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
677
678 int32_t ret = CmInstallAppCert(&appCert, &appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
679 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << \
680 "AppCertInstallPwdTest003 33 bytes sepcial pwd test failed, retcode:" << ret;
681 }
682 } // end of namespace
683
684