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
23 using namespace testing::ext;
24 using namespace CertmanagerTest;
25
26 namespace {
27 static uint32_t g_selfTokenId = 0;
28 static MockHapToken* g_MockHap = nullptr;
29 class CmAbortTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32
33 static void TearDownTestCase(void);
34
35 void SetUp();
36
37 void TearDown();
38 };
39
SetUpTestCase(void)40 void CmAbortTest::SetUpTestCase(void)
41 {
42 g_selfTokenId = GetSelfTokenID();
43 CmTestCommon::SetTestEnvironment(g_selfTokenId);
44 }
45
TearDownTestCase(void)46 void CmAbortTest::TearDownTestCase(void)
47 {
48 CmTestCommon::ResetTestEnvironment();
49 }
50
SetUp()51 void CmAbortTest::SetUp()
52 {
53 g_MockHap = new (std::nothrow) MockHapToken();
54 }
55
TearDown()56 void CmAbortTest::TearDown()
57 {
58 if (g_MockHap != nullptr) {
59 delete g_MockHap;
60 g_MockHap = nullptr;
61 }
62 }
63
64 /**
65 * @tc.name: CmAbortTest001
66 * @tc.desc: Test CmIsAuthorizedApp handle is null
67 * @tc.type: FUNC
68 * @tc.require: AR000H0MIA /SR000H09NA
69 */
70 HWTEST_F(CmAbortTest, CmAbortTest001, TestSize.Level0)
71 {
72 struct CmBlob *handle = nullptr;
73 int32_t ret = CmAbort(handle);
74 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
75 }
76
77 /**
78 * @tc.name: CmAbortTest002
79 * @tc.desc: Test CmIsAuthorizedApp handle size is 0
80 * @tc.type: FUNC
81 * @tc.require: AR000H0MIA /SR000H09NA
82 */
83 HWTEST_F(CmAbortTest, CmAbortTest002, TestSize.Level0)
84 {
85 uint64_t handleValue = 0;
86 struct CmBlob handle = { 0, (uint8_t *)&handleValue };
87 int32_t ret = CmAbort(&handle);
88 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
89 }
90
91 /**
92 * @tc.name: CmAbortTest003
93 * @tc.desc: Test CmIsAuthorizedApp handle data is null
94 * @tc.type: FUNC
95 * @tc.require: AR000H0MIA /SR000H09NA
96 */
97 HWTEST_F(CmAbortTest, CmAbortTest003, TestSize.Level0)
98 {
99 struct CmBlob handle = { sizeof(uint64_t), nullptr };
100 int32_t ret = CmAbort(&handle);
101 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
102 }
103
104 /**
105 * @tc.name: CmAbortTest004
106 * @tc.desc: Test CmIsAuthorizedApp handle not exist
107 * @tc.type: FUNC
108 * @tc.require: AR000H0MIA /SR000H09NA
109 */
110 HWTEST_F(CmAbortTest, CmAbortTest004, TestSize.Level0)
111 {
112 uint64_t handleValue = 0;
113 struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
114 int32_t ret = CmAbort(&handle);
115 EXPECT_EQ(ret, CM_SUCCESS);
116 }
117
118 /**
119 * @tc.name: CmAbortTest005
120 * @tc.desc: Test CmIsAuthorizedApp handle exist then abort
121 * @tc.type: FUNC
122 * @tc.require: AR000H0MIA /SR000H09NA
123 */
124 HWTEST_F(CmAbortTest, CmAbortTest005, TestSize.Level0)
125 {
126 uint8_t aliasData[] = "CmAbortTest005";
127 struct CmBlob alias = { sizeof(aliasData), aliasData };
128 int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_ECC, CM_CREDENTIAL_STORE);
129 EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
130
131 uint8_t uriData[] = "oh:t=ak;o=CmAbortTest005;u=0;a=0";
132 struct CmBlob keyUri = { sizeof(uriData), uriData };
133 uint64_t handleValue = 0;
134 struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
135 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
136
137 ret = CmInit(&keyUri, &spec, &handle);
138 EXPECT_EQ(ret, CM_SUCCESS) << "CmInit failed, retcode:" << ret;
139
140 ret = CmAbort(&handle);
141 EXPECT_EQ(ret, CM_SUCCESS) << "CmAbort failed, retcode:" << ret;
142
143 ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
144 EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
145 }
146
147 /**
148 * @tc.name: CmAbortTestPerformance006
149 * @tc.desc: 1000 times abort handle not exist
150 * @tc.type: FUNC
151 * @tc.require: AR000H0MIA /SR000H09NA
152 */
153 HWTEST_F(CmAbortTest, CmAbortTestPerformance006, TestSize.Level1)
154 {
155 uint64_t handleValue = 0;
156 struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
157 int32_t ret;
158 for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
159 ret = CmAbort(&handle);
160 EXPECT_EQ(ret, CM_SUCCESS);
161 }
162 }
163 } // end of namespace
164