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