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