• 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 "cm_test_common.h"
19 
20 #include "cert_manager_api.h"
21 
22 #include "cm_log.h"
23 #include "cm_mem.h"
24 
25 using namespace testing::ext;
26 using namespace CertmanagerTest;
27 namespace {
28 static uint32_t g_selfTokenId = 0;
29 static MockHapToken* g_MockHap = nullptr;
30 static constexpr uint32_t INIT_COUNT_MULTI = 16;
31 
32 class CmInitTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35 
36     static void TearDownTestCase(void);
37 
38     void SetUp();
39 
40     void TearDown();
41 };
42 
SetUpTestCase(void)43 void CmInitTest::SetUpTestCase(void)
44 {
45     g_selfTokenId = GetSelfTokenID();
46     CmTestCommon::SetTestEnvironment(g_selfTokenId);
47 }
48 
TearDownTestCase(void)49 void CmInitTest::TearDownTestCase(void)
50 {
51     CmTestCommon::ResetTestEnvironment();
52 }
53 
54 static const uint8_t g_rsaUriData[] = "oh:t=ak;o=TestInitRsa;u=0;a=0";
55 static const uint8_t g_eccUriData[] = "oh:t=ak;o=TestInitEcc;u=0;a=0";
56 static const uint8_t g_sm2UriData[] = "oh:t=ak;o=TestInitSM2;u=0;a=0";
57 static const CmBlob g_rsaKeyUri = { sizeof(g_rsaUriData), (uint8_t *)g_rsaUriData };
58 static const CmBlob g_eccKeyUri = { sizeof(g_eccUriData), (uint8_t *)g_eccUriData };
59 static const CmBlob g_sm2KeyUri = { sizeof(g_sm2UriData), (uint8_t *)g_sm2UriData };
60 
SetUp()61 void CmInitTest::SetUp()
62 {
63     g_MockHap = new (std::nothrow) MockHapToken();
64     uint8_t aliasRsaData[] = "TestInitRsa";
65     uint8_t aliasEccData[] = "TestInitEcc";
66     uint8_t aliasSM2Data[] = "TestInitSM2";
67     struct CmBlob aliasRsa = { sizeof(aliasRsaData), aliasRsaData };
68     struct CmBlob aliasEcc = { sizeof(aliasEccData), aliasEccData };
69     struct CmBlob aliasSM2 = { sizeof(aliasSM2Data), aliasSM2Data };
70 
71     int32_t ret = TestGenerateAppCert(&aliasRsa, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
72     EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert rsa failed, retcode:" << ret;
73     ret = TestGenerateAppCert(&aliasEcc, CERT_KEY_ALG_ECC, CM_CREDENTIAL_STORE);
74     EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert ecc failed, retcode:" << ret;
75     ret = TestGenerateAppCert(&aliasSM2, CERT_KEY_ALG_SM2_1, CM_CREDENTIAL_STORE);
76     EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert sm2 failed, retcode:" << ret;
77 }
78 
TearDown()79 void CmInitTest::TearDown()
80 {
81     int32_t ret = CmUninstallAppCert(&g_rsaKeyUri, CM_CREDENTIAL_STORE);
82     EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert rsa failed, retcode:" << ret;
83     ret = CmUninstallAppCert(&g_eccKeyUri, CM_CREDENTIAL_STORE);
84     EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert ecc failed, retcode:" << ret;
85     ret = CmUninstallAppCert(&g_sm2KeyUri, CM_CREDENTIAL_STORE);
86     EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert sm2 failed, retcode:" << ret;
87     if (g_MockHap != nullptr) {
88         delete g_MockHap;
89         g_MockHap = nullptr;
90     }
91 }
92 
93 /**
94 * @tc.name: CmInitTest001
95 * @tc.desc: Test CmIsAuthorizedApp authUri is NULL
96 * @tc.type: FUNC
97 * @tc.require: AR000H0MIA /SR000H09NA
98 */
99 HWTEST_F(CmInitTest, CmInitTest001, TestSize.Level0)
100 {
101     struct CmBlob *authUri = nullptr; /* authUri is NULL */
102     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
103     uint64_t handleValue = 0;
104     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
105 
106     int32_t ret = CmInit(authUri, &spec, &handle);
107     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
108 }
109 
110 /**
111  * @tc.name: CmInitTest002
112  * @tc.desc: Test CmIsAuthorizedApp authUri size is 0
113  * @tc.type: FUNC
114  * @tc.require: AR000H0MIA /SR000H09NA
115  */
116 HWTEST_F(CmInitTest, CmInitTest002, TestSize.Level0)
117 {
118     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
119     struct CmBlob authUri = { 0, uriData }; /* authUri size is 0 */
120     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
121     uint64_t handleValue = 0;
122     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
123 
124     int32_t ret = CmInit(&authUri, &spec, &handle);
125     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
126 }
127 
128 /**
129 * @tc.name: CmInitTest003
130 * @tc.desc: Test CmIsAuthorizedApp authUri data is null
131 * @tc.type: FUNC
132 * @tc.require: AR000H0MIA /SR000H09NA
133 */
134 HWTEST_F(CmInitTest, CmInitTest003, TestSize.Level0)
135 {
136     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
137     struct CmBlob authUri = { sizeof(uriData), nullptr }; /* authUri data is null */
138     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
139     uint64_t handleValue = 0;
140     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
141 
142     int32_t ret = CmInit(&authUri, &spec, &handle);
143     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
144 }
145 
146 /**
147  * @tc.name: CmInitTest004
148  * @tc.desc: Test CmIsAuthorizedApp authUri data not end of '\0'
149  * @tc.type: FUNC
150  * @tc.require: AR000H0MIA /SR000H09NA
151  */
152 HWTEST_F(CmInitTest, CmInitTest004, TestSize.Level0)
153 {
154     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
155     struct CmBlob authUri = { strlen((char *)uriData), uriData }; /* authUri data not end of '\0' */
156     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
157     uint64_t handleValue = 0;
158     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
159 
160     int32_t ret = CmInit(&authUri, &spec, &handle);
161     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
162 }
163 
164 /**
165 * @tc.name: CmInitTest005
166 * @tc.desc: Test CmIsAuthorizedApp authUri data has no app
167 * @tc.type: FUNC
168 * @tc.require: AR000H0MIA /SR000H09NA
169 */
170 HWTEST_F(CmInitTest, CmInitTest005, TestSize.Level0)
171 {
172     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0"; /* authUri data has no app */
173     struct CmBlob authUri = { sizeof(uriData), uriData };
174     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
175     uint64_t handleValue = 0;
176     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
177 
178     int32_t ret = CmInit(&authUri, &spec, &handle);
179     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
180 }
181 
182 /**
183  * @tc.name: CmInitTest006
184  * @tc.desc: Test CmIsAuthorizedApp authUri data has no user
185  * @tc.type: FUNC
186  * @tc.require: AR000H0MIA /SR000H09NA
187  */
188 HWTEST_F(CmInitTest, CmInitTest006, TestSize.Level0)
189 {
190     uint8_t uriData[] = "oh:t=ak;o=keyA;a=0"; /* authUri data has no user */
191     struct CmBlob authUri = { sizeof(uriData), uriData };
192     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
193     uint64_t handleValue = 0;
194     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
195 
196     int32_t ret = CmInit(&authUri, &spec, &handle);
197     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
198 }
199 
200 /**
201 * @tc.name: CmInitTest007
202 * @tc.desc: Test CmIsAuthorizedApp authUri data has no object
203 * @tc.type: FUNC
204 * @tc.require: AR000H0MIA /SR000H09NA
205 */
206 HWTEST_F(CmInitTest, CmInitTest007, TestSize.Level0)
207 {
208     uint8_t uriData[] = "oh:t=ak;u=0;a=0"; /* authUri data has no object */
209     struct CmBlob authUri = { sizeof(uriData), uriData };
210     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
211     uint64_t handleValue = 0;
212     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
213 
214     int32_t ret = CmInit(&authUri, &spec, &handle);
215     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
216 }
217 
218 /**
219  * @tc.name: CmInitTest008
220  * @tc.desc: Test CmIsAuthorizedApp authUri data type not ak
221  * @tc.type: FUNC
222  * @tc.require: AR000H0MIA /SR000H09NA
223  */
224 HWTEST_F(CmInitTest, CmInitTest008, TestSize.Level0)
225 {
226     uint8_t uriData[] = "oh:t=m;o=keyA;u=0;a=0"; /* authUri data type not ak */
227     struct CmBlob authUri = { sizeof(uriData), uriData };
228     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
229     uint64_t handleValue = 0;
230     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
231 
232     int32_t ret = CmInit(&authUri, &spec, &handle);
233     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
234 }
235 
236 /**
237 * @tc.name: CmInitTest009
238 * @tc.desc: Test CmIsAuthorizedApp spec is NULL
239 * @tc.type: FUNC
240 * @tc.require: AR000H0MIA /SR000H09NA
241 */
242 HWTEST_F(CmInitTest, CmInitTest009, TestSize.Level0)
243 {
244     struct CmSignatureSpec *spec = nullptr; /* spec is NULL */
245     uint64_t handleValue = 0;
246     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
247 
248     int32_t ret = CmInit(&g_rsaKeyUri, spec, &handle);
249     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
250 }
251 
252 /**
253  * @tc.name: CmInitTest010
254  * @tc.desc: Test CmIsAuthorizedApp spec->purpose is not CM_KEY_PURPOSE_SIGN/VERIFY
255  * @tc.type: FUNC
256  * @tc.require: AR000H0MIA /SR000H09NA
257  */
258 HWTEST_F(CmInitTest, CmInitTest010, TestSize.Level0)
259 {
260     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_AGREE }; /* purpose is not CM_KEY_PURPOSE_SIGN/VERIFY */
261     uint64_t handleValue = 0;
262     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
263 
264     int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
265     EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
266 }
267 
268 /**
269  * @tc.name: CmInitTest011
270  * @tc.desc: Test CmIsAuthorizedApp handle is NULL
271  * @tc.type: FUNC
272  * @tc.require: AR000H0MIA /SR000H09NA
273  */
274 HWTEST_F(CmInitTest, CmInitTest011, TestSize.Level0)
275 {
276     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
277     struct CmBlob *handle = nullptr; /* handle is NULL */
278 
279     int32_t ret = CmInit(&g_rsaKeyUri, &spec, handle);
280     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
281 }
282 
283 /**
284 * @tc.name: CmInitTest012
285 * @tc.desc: Test CmIsAuthorizedApp handle size is 0
286 * @tc.type: FUNC
287 * @tc.require: AR000H0MIA /SR000H09NA
288 */
289 HWTEST_F(CmInitTest, CmInitTest012, TestSize.Level0)
290 {
291     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
292     uint64_t handleValue = 0;
293     struct CmBlob handle = { 0, (uint8_t *)&handleValue }; /* handle size is 0 */
294 
295     int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
296     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
297 }
298 
299 /**
300  * @tc.name: CmInitTest013
301  * @tc.desc: Test CmIsAuthorizedApp handle data is NULL
302  * @tc.type: FUNC
303  * @tc.require: AR000H0MIA /SR000H09NA
304  */
305 HWTEST_F(CmInitTest, CmInitTest013, TestSize.Level0)
306 {
307     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
308     struct CmBlob handle = { sizeof(uint64_t), nullptr }; /* handle data is NULL */
309 
310     int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
311     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
312 }
313 
314 /**
315 * @tc.name: CmInitTest014
316 * @tc.desc: Test CmIsAuthorizedApp handle size smaller than sizeof(uint64_t)
317 * @tc.type: FUNC
318 * @tc.require: AR000H0MIA /SR000H09NA
319 */
320 HWTEST_F(CmInitTest, CmInitTest014, TestSize.Level0)
321 {
322     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
323     uint32_t handleValue = 0;
324     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue }; /* size smaller than sizeof(uint64_t) */
325 
326     int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
327     EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
328 }
329 
330 /**
331  * @tc.name: CmInitTest015
332  * @tc.desc: Test CmIsAuthorizedApp huks key not exist
333  * @tc.type: FUNC
334  * @tc.require: AR000H0MIA /SR000H09NA
335  */
336 HWTEST_F(CmInitTest, CmInitTest015, TestSize.Level0)
337 {
338     uint8_t uriData[] = "oh:t=ak;o=NotExist64897;u=0;a=0";
339     struct CmBlob authUri = { sizeof(uriData), uriData };
340     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
341     uint64_t handleValue = 0;
342     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
343 
344     int32_t ret = CmInit(&authUri, &spec, &handle); /* key not exist */
345     EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
346 }
347 
348 /**
349 * @tc.name: CmInitTest016
350 * @tc.desc: Test CmIsAuthorizedApp normal case: caller is producer, init once rsa
351 * @tc.type: FUNC
352 * @tc.require: AR000H0MIA /SR000H09NA
353 */
354 HWTEST_F(CmInitTest, CmInitTest016, TestSize.Level0)
355 {
356     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
357     uint64_t handleValue = 0;
358     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
359 
360     int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
361     EXPECT_EQ(ret, CM_SUCCESS);
362 }
363 
364 /**
365  * @tc.name: CmInitTest017
366  * @tc.desc: Test CmIsAuthorizedApp normal case: caller is producer, init once ecc
367  * @tc.type: FUNC
368  * @tc.require: AR000H0MIA /SR000H09NA
369  */
370 HWTEST_F(CmInitTest, CmInitTest017, TestSize.Level0)
371 {
372     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_VERIFY, CM_PADDING_PSS, CM_DIGEST_SHA256 };
373     uint64_t handleValue = 0;
374     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
375 
376     int32_t ret = CmInit(&g_eccKeyUri, &spec, &handle);
377     EXPECT_EQ(ret, CM_SUCCESS);
378 }
379 
380 /**
381  * @tc.name: CmInitTest018
382  * @tc.desc: Test CmIsAuthorizedApp normal case: caller is producer, init max times + 1
383  * @tc.type: FUNC
384  * @tc.require: AR000H0MIA /SR000H09NA
385  */
386 HWTEST_F(CmInitTest, CmInitTest018, TestSize.Level0)
387 {
388     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_VERIFY, CM_PADDING_PSS, CM_DIGEST_SHA256 };
389 
390     for (uint32_t i = 0; i < INIT_COUNT_MULTI; ++i) {
391         uint64_t handleValue = 0;
392         struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
393 
394         int32_t ret = CmInit(&g_eccKeyUri, &spec, &handle);
395         EXPECT_EQ(ret, CM_SUCCESS);
396     }
397 }
398 
399 /**
400  * @tc.name: CmInitTestPerformance019
401  * @tc.desc: 1000 times: caller is producer, init once ecc
402  * @tc.type: FUNC
403  * @tc.require: AR000H0MIA /SR000H09NA
404  */
405 HWTEST_F(CmInitTest, CmInitTestPerformance019, TestSize.Level1)
406 {
407     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_VERIFY, CM_PADDING_PSS, CM_DIGEST_SHA256 };
408     uint64_t handleValue = 0;
409     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
410 
411     int32_t ret;
412     for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
413         ret = CmInit(&g_eccKeyUri, &spec, &handle);
414         EXPECT_EQ(ret, CM_SUCCESS);
415         ret = CmAbort(&handle);
416         EXPECT_EQ(ret, CM_SUCCESS);
417     }
418 }
419 
420 /**
421  * @tc.name: CmInitTest020
422  * @tc.desc: normal case: caller is producer, init once sm2 with sm3
423  * @tc.type: FUNC
424  */
425 HWTEST_F(CmInitTest, CmInitTest020, TestSize.Level0)
426 {
427     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SM3 };
428     uint64_t handleValue1 = 0;
429     struct CmBlob handle1 = { sizeof(handleValue1), (uint8_t *)&handleValue1 };
430 
431     int32_t ret = CmInit(&g_sm2KeyUri, &spec, &handle1);
432     EXPECT_EQ(ret, CM_SUCCESS);
433 
434     spec.purpose = CM_KEY_PURPOSE_VERIFY;
435     uint64_t handleValue2 = 0;
436     struct CmBlob handle2 = { sizeof(handleValue2), (uint8_t *)&handleValue2 };
437     ret = CmInit(&g_sm2KeyUri, &spec, &handle2);
438     EXPECT_EQ(ret, CM_SUCCESS);
439 }
440 
441 /**
442  * @tc.name: CmInitTest021
443  * @tc.desc: Abnormal case: Test init sm2 with SHA256
444  * @tc.type: FUNC
445  */
446 HWTEST_F(CmInitTest, CmInitTest021, TestSize.Level0)
447 {
448     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA256 };
449     uint64_t handleValue1 = 0;
450     struct CmBlob handle1 = { sizeof(handleValue1), (uint8_t *)&handleValue1 };
451 
452     int32_t ret = CmInit(&g_sm2KeyUri, &spec, &handle1);
453     EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
454 
455     spec.purpose = CM_KEY_PURPOSE_VERIFY;
456     uint64_t handleValue2 = 0;
457     struct CmBlob handle2 = { sizeof(handleValue2), (uint8_t *)&handleValue2 };
458     ret = CmInit(&g_sm2KeyUri, &spec, &handle2);
459     EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
460 }
461 
462 /**
463  * @tc.name: CmInitTest022
464  * @tc.desc: Abnormal case: Test init ecc with sm3
465  * @tc.type: FUNC
466  */
467 HWTEST_F(CmInitTest, CmInitTest022, TestSize.Level0)
468 {
469     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SM3 };
470     uint64_t handleValue1 = 0;
471     struct CmBlob handle1 = { sizeof(handleValue1), (uint8_t *)&handleValue1 };
472 
473     int32_t ret = CmInit(&g_eccKeyUri, &spec, &handle1);
474     EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
475 
476     spec.purpose = CM_KEY_PURPOSE_VERIFY;
477     uint64_t handleValue2 = 0;
478     struct CmBlob handle2 = { sizeof(handleValue2), (uint8_t *)&handleValue2 };
479     ret = CmInit(&g_eccKeyUri, &spec, &handle2);
480     EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
481 }
482 
483 /**
484  * @tc.name: CmInitTest023
485  * @tc.desc: normal case: caller is producer, init once sm2 with non_digest
486  * @tc.type: FUNC
487  */
488 HWTEST_F(CmInitTest, CmInitTest023, TestSize.Level0)
489 {
490     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_NONE };
491     uint64_t handleValue1 = 0;
492     struct CmBlob handle1 = { sizeof(handleValue1), (uint8_t *)&handleValue1 };
493 
494     int32_t ret = CmInit(&g_sm2KeyUri, &spec, &handle1);
495     EXPECT_EQ(ret, CM_SUCCESS);
496 
497     spec.purpose = CM_KEY_PURPOSE_VERIFY;
498     uint64_t handleValue2 = 0;
499     struct CmBlob handle2 = { sizeof(handleValue2), (uint8_t *)&handleValue2 };
500     ret = CmInit(&g_sm2KeyUri, &spec, &handle2);
501     EXPECT_EQ(ret, CM_SUCCESS);
502 }
503 
504 /**
505  * @tc.name: CmInitTestPerformance024
506  * @tc.desc: 1000 times: caller is producer, init sm2
507  * @tc.type: FUNC
508  */
509 HWTEST_F(CmInitTest, CmInitTestPerformance024, TestSize.Level1)
510 {
511     struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SM3 };
512     uint64_t handleValue = 0;
513     struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
514 
515     int32_t ret;
516     for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
517         ret = CmInit(&g_sm2KeyUri, &spec, &handle);
518         EXPECT_EQ(ret, CM_SUCCESS);
519         ret = CmAbort(&handle);
520         EXPECT_EQ(ret, CM_SUCCESS);
521     }
522 }
523 } // end of namespace
524