14e56987cSopenharmony_ci/*
24e56987cSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34e56987cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44e56987cSopenharmony_ci * you may not use this file except in compliance with the License.
54e56987cSopenharmony_ci * You may obtain a copy of the License at
64e56987cSopenharmony_ci *
74e56987cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84e56987cSopenharmony_ci *
94e56987cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104e56987cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114e56987cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124e56987cSopenharmony_ci * See the License for the specific language governing permissions and
134e56987cSopenharmony_ci * limitations under the License.
144e56987cSopenharmony_ci */
154e56987cSopenharmony_ci
164e56987cSopenharmony_ci#include <gtest/gtest.h>
174e56987cSopenharmony_ci
184e56987cSopenharmony_ci#include "cm_test_common.h"
194e56987cSopenharmony_ci
204e56987cSopenharmony_ci#include "cert_manager_api.h"
214e56987cSopenharmony_ci
224e56987cSopenharmony_ci#include "cm_log.h"
234e56987cSopenharmony_ci#include "cm_mem.h"
244e56987cSopenharmony_ci
254e56987cSopenharmony_ciusing namespace testing::ext;
264e56987cSopenharmony_ciusing namespace CertmanagerTest;
274e56987cSopenharmony_cinamespace {
284e56987cSopenharmony_cistatic constexpr uint32_t INIT_COUNT_MULTI = 16;
294e56987cSopenharmony_ci
304e56987cSopenharmony_ciclass CmInitTest : public testing::Test {
314e56987cSopenharmony_cipublic:
324e56987cSopenharmony_ci    static void SetUpTestCase(void);
334e56987cSopenharmony_ci
344e56987cSopenharmony_ci    static void TearDownTestCase(void);
354e56987cSopenharmony_ci
364e56987cSopenharmony_ci    void SetUp();
374e56987cSopenharmony_ci
384e56987cSopenharmony_ci    void TearDown();
394e56987cSopenharmony_ci};
404e56987cSopenharmony_ci
414e56987cSopenharmony_civoid CmInitTest::SetUpTestCase(void)
424e56987cSopenharmony_ci{
434e56987cSopenharmony_ci    SetATPermission();
444e56987cSopenharmony_ci}
454e56987cSopenharmony_ci
464e56987cSopenharmony_civoid CmInitTest::TearDownTestCase(void)
474e56987cSopenharmony_ci{
484e56987cSopenharmony_ci}
494e56987cSopenharmony_ci
504e56987cSopenharmony_cistatic const uint8_t g_rsaUriData[] = "oh:t=ak;o=TestInitRsa;u=0;a=0";
514e56987cSopenharmony_cistatic const uint8_t g_eccUriData[] = "oh:t=ak;o=TestInitEcc;u=0;a=0";
524e56987cSopenharmony_cistatic const CmBlob g_rsaKeyUri = { sizeof(g_rsaUriData), (uint8_t *)g_rsaUriData };
534e56987cSopenharmony_cistatic const CmBlob g_eccKeyUri = { sizeof(g_eccUriData), (uint8_t *)g_eccUriData };
544e56987cSopenharmony_ci
554e56987cSopenharmony_civoid CmInitTest::SetUp()
564e56987cSopenharmony_ci{
574e56987cSopenharmony_ci    uint8_t aliasRsaData[] = "TestInitRsa";
584e56987cSopenharmony_ci    uint8_t aliasEccData[] = "TestInitEcc";
594e56987cSopenharmony_ci    struct CmBlob aliasRsa = { sizeof(aliasRsaData), aliasRsaData };
604e56987cSopenharmony_ci    struct CmBlob aliasEcc = { sizeof(aliasEccData), aliasEccData };
614e56987cSopenharmony_ci
624e56987cSopenharmony_ci    int32_t ret = TestGenerateAppCert(&aliasRsa, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
634e56987cSopenharmony_ci    EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert rsa failed, retcode:" << ret;
644e56987cSopenharmony_ci    ret = TestGenerateAppCert(&aliasEcc, CERT_KEY_ALG_ECC, CM_CREDENTIAL_STORE);
654e56987cSopenharmony_ci    EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert ecc failed, retcode:" << ret;
664e56987cSopenharmony_ci}
674e56987cSopenharmony_ci
684e56987cSopenharmony_civoid CmInitTest::TearDown()
694e56987cSopenharmony_ci{
704e56987cSopenharmony_ci    int32_t ret = CmUninstallAppCert(&g_rsaKeyUri, CM_CREDENTIAL_STORE);
714e56987cSopenharmony_ci    EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert rsa failed, retcode:" << ret;
724e56987cSopenharmony_ci    ret = CmUninstallAppCert(&g_eccKeyUri, CM_CREDENTIAL_STORE);
734e56987cSopenharmony_ci    EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert ecc failed, retcode:" << ret;
744e56987cSopenharmony_ci}
754e56987cSopenharmony_ci
764e56987cSopenharmony_ci/**
774e56987cSopenharmony_ci* @tc.name: CmInitTest001
784e56987cSopenharmony_ci* @tc.desc: Test CmIsAuthorizedApp authUri is NULL
794e56987cSopenharmony_ci* @tc.type: FUNC
804e56987cSopenharmony_ci* @tc.require: AR000H0MIA /SR000H09NA
814e56987cSopenharmony_ci*/
824e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest001, TestSize.Level0)
834e56987cSopenharmony_ci{
844e56987cSopenharmony_ci    struct CmBlob *authUri = nullptr; /* authUri is NULL */
854e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
864e56987cSopenharmony_ci    uint64_t handleValue = 0;
874e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
884e56987cSopenharmony_ci
894e56987cSopenharmony_ci    int32_t ret = CmInit(authUri, &spec, &handle);
904e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
914e56987cSopenharmony_ci}
924e56987cSopenharmony_ci
934e56987cSopenharmony_ci/**
944e56987cSopenharmony_ci * @tc.name: CmInitTest002
954e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp authUri size is 0
964e56987cSopenharmony_ci * @tc.type: FUNC
974e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
984e56987cSopenharmony_ci */
994e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest002, TestSize.Level0)
1004e56987cSopenharmony_ci{
1014e56987cSopenharmony_ci    uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
1024e56987cSopenharmony_ci    struct CmBlob authUri = { 0, uriData }; /* authUri size is 0 */
1034e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
1044e56987cSopenharmony_ci    uint64_t handleValue = 0;
1054e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
1064e56987cSopenharmony_ci
1074e56987cSopenharmony_ci    int32_t ret = CmInit(&authUri, &spec, &handle);
1084e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
1094e56987cSopenharmony_ci}
1104e56987cSopenharmony_ci
1114e56987cSopenharmony_ci/**
1124e56987cSopenharmony_ci* @tc.name: CmInitTest003
1134e56987cSopenharmony_ci* @tc.desc: Test CmIsAuthorizedApp authUri data is null
1144e56987cSopenharmony_ci* @tc.type: FUNC
1154e56987cSopenharmony_ci* @tc.require: AR000H0MIA /SR000H09NA
1164e56987cSopenharmony_ci*/
1174e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest003, TestSize.Level0)
1184e56987cSopenharmony_ci{
1194e56987cSopenharmony_ci    uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
1204e56987cSopenharmony_ci    struct CmBlob authUri = { sizeof(uriData), nullptr }; /* authUri data is null */
1214e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
1224e56987cSopenharmony_ci    uint64_t handleValue = 0;
1234e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
1244e56987cSopenharmony_ci
1254e56987cSopenharmony_ci    int32_t ret = CmInit(&authUri, &spec, &handle);
1264e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
1274e56987cSopenharmony_ci}
1284e56987cSopenharmony_ci
1294e56987cSopenharmony_ci/**
1304e56987cSopenharmony_ci * @tc.name: CmInitTest004
1314e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp authUri data not end of '\0'
1324e56987cSopenharmony_ci * @tc.type: FUNC
1334e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
1344e56987cSopenharmony_ci */
1354e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest004, TestSize.Level0)
1364e56987cSopenharmony_ci{
1374e56987cSopenharmony_ci    uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
1384e56987cSopenharmony_ci    struct CmBlob authUri = { strlen((char *)uriData), uriData }; /* authUri data not end of '\0' */
1394e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
1404e56987cSopenharmony_ci    uint64_t handleValue = 0;
1414e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
1424e56987cSopenharmony_ci
1434e56987cSopenharmony_ci    int32_t ret = CmInit(&authUri, &spec, &handle);
1444e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
1454e56987cSopenharmony_ci}
1464e56987cSopenharmony_ci
1474e56987cSopenharmony_ci/**
1484e56987cSopenharmony_ci* @tc.name: CmInitTest005
1494e56987cSopenharmony_ci* @tc.desc: Test CmIsAuthorizedApp authUri data has no app
1504e56987cSopenharmony_ci* @tc.type: FUNC
1514e56987cSopenharmony_ci* @tc.require: AR000H0MIA /SR000H09NA
1524e56987cSopenharmony_ci*/
1534e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest005, TestSize.Level0)
1544e56987cSopenharmony_ci{
1554e56987cSopenharmony_ci    uint8_t uriData[] = "oh:t=ak;o=keyA;u=0"; /* authUri data has no app */
1564e56987cSopenharmony_ci    struct CmBlob authUri = { sizeof(uriData), uriData };
1574e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
1584e56987cSopenharmony_ci    uint64_t handleValue = 0;
1594e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
1604e56987cSopenharmony_ci
1614e56987cSopenharmony_ci    int32_t ret = CmInit(&authUri, &spec, &handle);
1624e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
1634e56987cSopenharmony_ci}
1644e56987cSopenharmony_ci
1654e56987cSopenharmony_ci/**
1664e56987cSopenharmony_ci * @tc.name: CmInitTest006
1674e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp authUri data has no user
1684e56987cSopenharmony_ci * @tc.type: FUNC
1694e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
1704e56987cSopenharmony_ci */
1714e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest006, TestSize.Level0)
1724e56987cSopenharmony_ci{
1734e56987cSopenharmony_ci    uint8_t uriData[] = "oh:t=ak;o=keyA;a=0"; /* authUri data has no user */
1744e56987cSopenharmony_ci    struct CmBlob authUri = { sizeof(uriData), uriData };
1754e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
1764e56987cSopenharmony_ci    uint64_t handleValue = 0;
1774e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
1784e56987cSopenharmony_ci
1794e56987cSopenharmony_ci    int32_t ret = CmInit(&authUri, &spec, &handle);
1804e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
1814e56987cSopenharmony_ci}
1824e56987cSopenharmony_ci
1834e56987cSopenharmony_ci/**
1844e56987cSopenharmony_ci* @tc.name: CmInitTest007
1854e56987cSopenharmony_ci* @tc.desc: Test CmIsAuthorizedApp authUri data has no object
1864e56987cSopenharmony_ci* @tc.type: FUNC
1874e56987cSopenharmony_ci* @tc.require: AR000H0MIA /SR000H09NA
1884e56987cSopenharmony_ci*/
1894e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest007, TestSize.Level0)
1904e56987cSopenharmony_ci{
1914e56987cSopenharmony_ci    uint8_t uriData[] = "oh:t=ak;u=0;a=0"; /* authUri data has no object */
1924e56987cSopenharmony_ci    struct CmBlob authUri = { sizeof(uriData), uriData };
1934e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
1944e56987cSopenharmony_ci    uint64_t handleValue = 0;
1954e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
1964e56987cSopenharmony_ci
1974e56987cSopenharmony_ci    int32_t ret = CmInit(&authUri, &spec, &handle);
1984e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
1994e56987cSopenharmony_ci}
2004e56987cSopenharmony_ci
2014e56987cSopenharmony_ci/**
2024e56987cSopenharmony_ci * @tc.name: CmInitTest008
2034e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp authUri data type not ak
2044e56987cSopenharmony_ci * @tc.type: FUNC
2054e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
2064e56987cSopenharmony_ci */
2074e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest008, TestSize.Level0)
2084e56987cSopenharmony_ci{
2094e56987cSopenharmony_ci    uint8_t uriData[] = "oh:t=m;o=keyA;u=0;a=0"; /* authUri data type not ak */
2104e56987cSopenharmony_ci    struct CmBlob authUri = { sizeof(uriData), uriData };
2114e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
2124e56987cSopenharmony_ci    uint64_t handleValue = 0;
2134e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
2144e56987cSopenharmony_ci
2154e56987cSopenharmony_ci    int32_t ret = CmInit(&authUri, &spec, &handle);
2164e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
2174e56987cSopenharmony_ci}
2184e56987cSopenharmony_ci
2194e56987cSopenharmony_ci/**
2204e56987cSopenharmony_ci* @tc.name: CmInitTest009
2214e56987cSopenharmony_ci* @tc.desc: Test CmIsAuthorizedApp spec is NULL
2224e56987cSopenharmony_ci* @tc.type: FUNC
2234e56987cSopenharmony_ci* @tc.require: AR000H0MIA /SR000H09NA
2244e56987cSopenharmony_ci*/
2254e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest009, TestSize.Level0)
2264e56987cSopenharmony_ci{
2274e56987cSopenharmony_ci    struct CmSignatureSpec *spec = nullptr; /* spec is NULL */
2284e56987cSopenharmony_ci    uint64_t handleValue = 0;
2294e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
2304e56987cSopenharmony_ci
2314e56987cSopenharmony_ci    int32_t ret = CmInit(&g_rsaKeyUri, spec, &handle);
2324e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
2334e56987cSopenharmony_ci}
2344e56987cSopenharmony_ci
2354e56987cSopenharmony_ci/**
2364e56987cSopenharmony_ci * @tc.name: CmInitTest010
2374e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp spec->purpose is not CM_KEY_PURPOSE_SIGN/VERIFY
2384e56987cSopenharmony_ci * @tc.type: FUNC
2394e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
2404e56987cSopenharmony_ci */
2414e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest010, TestSize.Level0)
2424e56987cSopenharmony_ci{
2434e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_AGREE }; /* purpose is not CM_KEY_PURPOSE_SIGN/VERIFY */
2444e56987cSopenharmony_ci    uint64_t handleValue = 0;
2454e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
2464e56987cSopenharmony_ci
2474e56987cSopenharmony_ci    int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
2484e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
2494e56987cSopenharmony_ci}
2504e56987cSopenharmony_ci
2514e56987cSopenharmony_ci/**
2524e56987cSopenharmony_ci * @tc.name: CmInitTest011
2534e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp handle is NULL
2544e56987cSopenharmony_ci * @tc.type: FUNC
2554e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
2564e56987cSopenharmony_ci */
2574e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest011, TestSize.Level0)
2584e56987cSopenharmony_ci{
2594e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
2604e56987cSopenharmony_ci    struct CmBlob *handle = nullptr; /* handle is NULL */
2614e56987cSopenharmony_ci
2624e56987cSopenharmony_ci    int32_t ret = CmInit(&g_rsaKeyUri, &spec, handle);
2634e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
2644e56987cSopenharmony_ci}
2654e56987cSopenharmony_ci
2664e56987cSopenharmony_ci/**
2674e56987cSopenharmony_ci* @tc.name: CmInitTest012
2684e56987cSopenharmony_ci* @tc.desc: Test CmIsAuthorizedApp handle size is 0
2694e56987cSopenharmony_ci* @tc.type: FUNC
2704e56987cSopenharmony_ci* @tc.require: AR000H0MIA /SR000H09NA
2714e56987cSopenharmony_ci*/
2724e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest012, TestSize.Level0)
2734e56987cSopenharmony_ci{
2744e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
2754e56987cSopenharmony_ci    uint64_t handleValue = 0;
2764e56987cSopenharmony_ci    struct CmBlob handle = { 0, (uint8_t *)&handleValue }; /* handle size is 0 */
2774e56987cSopenharmony_ci
2784e56987cSopenharmony_ci    int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
2794e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
2804e56987cSopenharmony_ci}
2814e56987cSopenharmony_ci
2824e56987cSopenharmony_ci/**
2834e56987cSopenharmony_ci * @tc.name: CmInitTest013
2844e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp handle data is NULL
2854e56987cSopenharmony_ci * @tc.type: FUNC
2864e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
2874e56987cSopenharmony_ci */
2884e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest013, TestSize.Level0)
2894e56987cSopenharmony_ci{
2904e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
2914e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(uint64_t), nullptr }; /* handle data is NULL */
2924e56987cSopenharmony_ci
2934e56987cSopenharmony_ci    int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
2944e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
2954e56987cSopenharmony_ci}
2964e56987cSopenharmony_ci
2974e56987cSopenharmony_ci/**
2984e56987cSopenharmony_ci* @tc.name: CmInitTest014
2994e56987cSopenharmony_ci* @tc.desc: Test CmIsAuthorizedApp handle size smaller than sizeof(uint64_t)
3004e56987cSopenharmony_ci* @tc.type: FUNC
3014e56987cSopenharmony_ci* @tc.require: AR000H0MIA /SR000H09NA
3024e56987cSopenharmony_ci*/
3034e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest014, TestSize.Level0)
3044e56987cSopenharmony_ci{
3054e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
3064e56987cSopenharmony_ci    uint32_t handleValue = 0;
3074e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue }; /* size smaller than sizeof(uint64_t) */
3084e56987cSopenharmony_ci
3094e56987cSopenharmony_ci    int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
3104e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
3114e56987cSopenharmony_ci}
3124e56987cSopenharmony_ci
3134e56987cSopenharmony_ci/**
3144e56987cSopenharmony_ci * @tc.name: CmInitTest015
3154e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp huks key not exist
3164e56987cSopenharmony_ci * @tc.type: FUNC
3174e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
3184e56987cSopenharmony_ci */
3194e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest015, TestSize.Level0)
3204e56987cSopenharmony_ci{
3214e56987cSopenharmony_ci    uint8_t uriData[] = "oh:t=ak;o=NotExist64897;u=0;a=0";
3224e56987cSopenharmony_ci    struct CmBlob authUri = { sizeof(uriData), uriData };
3234e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
3244e56987cSopenharmony_ci    uint64_t handleValue = 0;
3254e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
3264e56987cSopenharmony_ci
3274e56987cSopenharmony_ci    int32_t ret = CmInit(&authUri, &spec, &handle); /* key not exist */
3284e56987cSopenharmony_ci    EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
3294e56987cSopenharmony_ci}
3304e56987cSopenharmony_ci
3314e56987cSopenharmony_ci/**
3324e56987cSopenharmony_ci* @tc.name: CmInitTest016
3334e56987cSopenharmony_ci* @tc.desc: Test CmIsAuthorizedApp normal case: caller is producer, init once rsa
3344e56987cSopenharmony_ci* @tc.type: FUNC
3354e56987cSopenharmony_ci* @tc.require: AR000H0MIA /SR000H09NA
3364e56987cSopenharmony_ci*/
3374e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest016, TestSize.Level0)
3384e56987cSopenharmony_ci{
3394e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
3404e56987cSopenharmony_ci    uint64_t handleValue = 0;
3414e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
3424e56987cSopenharmony_ci
3434e56987cSopenharmony_ci    int32_t ret = CmInit(&g_rsaKeyUri, &spec, &handle);
3444e56987cSopenharmony_ci    EXPECT_EQ(ret, CM_SUCCESS);
3454e56987cSopenharmony_ci}
3464e56987cSopenharmony_ci
3474e56987cSopenharmony_ci/**
3484e56987cSopenharmony_ci * @tc.name: CmInitTest017
3494e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp normal case: caller is producer, init once ecc
3504e56987cSopenharmony_ci * @tc.type: FUNC
3514e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
3524e56987cSopenharmony_ci */
3534e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest017, TestSize.Level0)
3544e56987cSopenharmony_ci{
3554e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_VERIFY, CM_PADDING_PSS, CM_DIGEST_SHA256 };
3564e56987cSopenharmony_ci    uint64_t handleValue = 0;
3574e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
3584e56987cSopenharmony_ci
3594e56987cSopenharmony_ci    int32_t ret = CmInit(&g_eccKeyUri, &spec, &handle);
3604e56987cSopenharmony_ci    EXPECT_EQ(ret, CM_SUCCESS);
3614e56987cSopenharmony_ci}
3624e56987cSopenharmony_ci
3634e56987cSopenharmony_ci/**
3644e56987cSopenharmony_ci * @tc.name: CmInitTest018
3654e56987cSopenharmony_ci * @tc.desc: Test CmIsAuthorizedApp normal case: caller is producer, init max times + 1
3664e56987cSopenharmony_ci * @tc.type: FUNC
3674e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
3684e56987cSopenharmony_ci */
3694e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTest018, TestSize.Level0)
3704e56987cSopenharmony_ci{
3714e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_VERIFY, CM_PADDING_PSS, CM_DIGEST_SHA256 };
3724e56987cSopenharmony_ci
3734e56987cSopenharmony_ci    for (uint32_t i = 0; i < INIT_COUNT_MULTI; ++i) {
3744e56987cSopenharmony_ci        uint64_t handleValue = 0;
3754e56987cSopenharmony_ci        struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
3764e56987cSopenharmony_ci
3774e56987cSopenharmony_ci        int32_t ret = CmInit(&g_eccKeyUri, &spec, &handle);
3784e56987cSopenharmony_ci        EXPECT_EQ(ret, CM_SUCCESS);
3794e56987cSopenharmony_ci    }
3804e56987cSopenharmony_ci}
3814e56987cSopenharmony_ci
3824e56987cSopenharmony_ci/**
3834e56987cSopenharmony_ci * @tc.name: CmInitTestPerformance019
3844e56987cSopenharmony_ci * @tc.desc: 1000 times: caller is producer, init once ecc
3854e56987cSopenharmony_ci * @tc.type: FUNC
3864e56987cSopenharmony_ci * @tc.require: AR000H0MIA /SR000H09NA
3874e56987cSopenharmony_ci */
3884e56987cSopenharmony_ciHWTEST_F(CmInitTest, CmInitTestPerformance019, TestSize.Level1)
3894e56987cSopenharmony_ci{
3904e56987cSopenharmony_ci    struct CmSignatureSpec spec = { CM_KEY_PURPOSE_VERIFY, CM_PADDING_PSS, CM_DIGEST_SHA256 };
3914e56987cSopenharmony_ci    uint64_t handleValue = 0;
3924e56987cSopenharmony_ci    struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
3934e56987cSopenharmony_ci
3944e56987cSopenharmony_ci    int32_t ret;
3954e56987cSopenharmony_ci    for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
3964e56987cSopenharmony_ci        ret = CmInit(&g_eccKeyUri, &spec, &handle);
3974e56987cSopenharmony_ci        EXPECT_EQ(ret, CM_SUCCESS);
3984e56987cSopenharmony_ci        ret = CmAbort(&handle);
3994e56987cSopenharmony_ci        EXPECT_EQ(ret, CM_SUCCESS);
4004e56987cSopenharmony_ci    }
4014e56987cSopenharmony_ci}
4024e56987cSopenharmony_ci} // end of namespace
403