1 /*
2  * Copyright (c) 2023 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 #include <gtest/hwext/gtest-multithread.h>
18 
19 #include "cert_manager_api.h"
20 #include "cm_cert_data_ecc.h"
21 #include "cm_cert_data_part1_rsa.h"
22 #include "cm_mem.h"
23 #include "cm_test_common.h"
24 
25 using namespace testing::ext;
26 using namespace testing::mt;
27 using namespace CertmanagerTest;
28 namespace {
29 static constexpr uint32_t MULTI_THREAD_NUM = 10;
30 
31 static const struct CmBlob g_appCert = { sizeof(g_rsa2048P12CertInfo), const_cast<uint8_t *>(g_rsa2048P12CertInfo) };
32 static const struct CmBlob g_appCertPwd = { sizeof(g_certPwd), const_cast<uint8_t *>(g_certPwd) };
33 
34 class CmAppCertMultiThreadTest : public testing::Test {
35 public:
36     static void SetUpTestCase(void);
37 
38     static void TearDownTestCase(void);
39 
40     void SetUp();
41 
42     void TearDown();
43 };
44 
SetUpTestCase(void)45 void CmAppCertMultiThreadTest::SetUpTestCase(void)
46 {
47     SetATPermission();
48 }
49 
TearDownTestCase(void)50 void CmAppCertMultiThreadTest::TearDownTestCase(void)
51 {
52 }
53 
SetUp()54 void CmAppCertMultiThreadTest::SetUp()
55 {
56 }
57 
TearDown()58 void CmAppCertMultiThreadTest::TearDown()
59 {
60 }
61 
62 /**
63  * @tc.name: CmAppCertMultiThreadTest001
64  * @tc.desc: Test CertManager unInstall app cert interface abnormal function
65  * @tc.type: FUNC
66  * @tc.require: AR000H0MI8 /SR000H09N9
67  */
68 HWMTEST_F(CmAppCertMultiThreadTest, CmAppCertMultiThreadTest001, TestSize.Level0, MULTI_THREAD_NUM)
69 {
70     int32_t ret = CmUninstallAppCert(nullptr, CM_CREDENTIAL_STORE);
71     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmAppCertMultiThreadTest001 test failed, retcode:" << ret;
72 }
73 
74 /**
75  * @tc.name: CmAppCertMultiThreadTest002
76  * @tc.desc: Test CertManager unInstall app cert interface abnormal function
77  * @tc.type: FUNC
78  * @tc.require: AR000H0MI8 /SR000H09N9
79  */
80 HWMTEST_F(CmAppCertMultiThreadTest, CmAppCertMultiThreadTest002, TestSize.Level0, MULTI_THREAD_NUM)
81 {
82     uint8_t keyUriBuf[] = "oh:t=ak;o=keyA;u=0;a=0";
83     struct CmBlob keyUri = { sizeof(keyUriBuf), keyUriBuf };
84 
85     int32_t ret = CmUninstallAppCert(&keyUri, CM_PRI_CREDENTIAL_STORE + 1);
86     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT) << "CmAppCertMultiThreadTest002 test failed, retcode:" << ret;
87 }
88 
89 /**
90  * @tc.name: CmAppCertMultiThreadTest003
91  * @tc.desc: Test CertManager unInstall all app cert interface abnormal function
92  * @tc.type: FUNC
93  * @tc.require: AR000H0MI8 /SR000H09N9
94  */
95 HWMTEST_F(CmAppCertMultiThreadTest, CmAppCertMultiThreadTest003, TestSize.Level0, MULTI_THREAD_NUM)
96 {
97     int32_t ret = CmUninstallAllAppCert();
98     EXPECT_EQ(ret, CM_SUCCESS) << "CmAppCertMultiThreadTest003 test failed, retcode:" << ret;
99 }
100 
TestUninstallAppCertMultiThread(void)101 static void TestUninstallAppCertMultiThread(void)
102 {
103     int32_t ret = CmUninstallAllAppCert();
104     EXPECT_EQ(ret, CM_SUCCESS) << "TestUninstallAppCertMultiThread test failed, retcode:" << ret;
105 }
106 
107 /**
108  * @tc.name: CmAppCertMultiThreadTest004
109  * @tc.desc: Test CertManager unInstall all app cert interface base function
110  * @tc.type: FUNC
111  * @tc.require: AR000H0MI8 /SR000H09N9
112  */
113 HWTEST_F(CmAppCertMultiThreadTest, CmAppCertMultiThreadTest004, TestSize.Level0)
114 {
115     uint8_t certAliasBuf[] = "keyB";
116     struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf };
117 
118     uint8_t uriBuf[MAX_LEN_URI] = {0};
119     struct CmBlob keyUri = { sizeof(uriBuf), uriBuf };
120 
121     int32_t ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_CREDENTIAL_STORE, &keyUri);
122     EXPECT_EQ(ret, CM_SUCCESS) << "CmAppCertMultiThreadTest004 1 failed, retcode:" << ret;
123 
124     ret = CmInstallAppCert(&g_appCert, &g_appCertPwd, &certAlias, CM_PRI_CREDENTIAL_STORE, &keyUri);
125     EXPECT_EQ(ret, CM_SUCCESS) << "CmAppCertMultiThreadTest004 2 failed, retcode:" << ret;
126 
127     SET_THREAD_NUM(MULTI_THREAD_NUM);
128     GTEST_RUN_TASK(TestUninstallAppCertMultiThread);
129 }
130 } // end of namespace
131 
132