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 #include "sec_comp_enhance_test.h"
16 #include <unistd.h>
17 #include "sec_comp_err.h"
18 #include "sec_comp_log.h"
19 #include "sec_comp_info.h"
20
21 using namespace testing::ext;
22 using namespace OHOS::Security::SecurityComponent;
23
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEnhanceTest"};
27 static bool g_inputEnhanceExist = false;
28 static bool g_srvEnhanceExist = false;
29 static constexpr uint32_t SEC_COMP_ENHANCE_CFG_SIZE = 76;
30 #ifdef _ARM64_
31 static const std::string LIB_PATH = "/system/lib64/";
32 #else
33 static const std::string LIB_PATH = "/system/lib/";
34 #endif
35 static const std::string ENHANCE_INPUT_INTERFACE_LIB = LIB_PATH + "libsec_comp_input_enhance.z.so";
36 static const std::string ENHANCE_SRV_INTERFACE_LIB = LIB_PATH + "libsec_comp_service_enhance.z.so";
37 static constexpr uint32_t MAX_HMAC_SIZE = 64;
38 } // namespace
39
SetUpTestCase()40 void SecCompEnhanceTest::SetUpTestCase()
41 {
42 if (access(ENHANCE_INPUT_INTERFACE_LIB.c_str(), F_OK) == 0) {
43 g_inputEnhanceExist = true;
44 }
45
46 if (access(ENHANCE_SRV_INTERFACE_LIB.c_str(), F_OK) == 0) {
47 g_srvEnhanceExist = true;
48 }
49 system("kill -9 `pidof security_compon`");
50 SC_LOG_INFO(LABEL, "SetUpTestCase.");
51 }
52
TearDownTestCase()53 void SecCompEnhanceTest::TearDownTestCase()
54 {
55 SC_LOG_INFO(LABEL, "TearDownTestCase.");
56 }
57
SetUp()58 void SecCompEnhanceTest::SetUp()
59 {
60 SC_LOG_INFO(LABEL, "SetUp ok.");
61 }
62
TearDown()63 void SecCompEnhanceTest::TearDown()
64 {
65 SC_LOG_INFO(LABEL, "TearDown.");
66 }
67
68 /**
69 * @tc.name: SetEnhanceCfg001
70 * @tc.desc: test SetEnhanceCfg
71 * @tc.type: FUNC
72 * @tc.require: AR000HO9IN
73 */
74 HWTEST_F(SecCompEnhanceTest, SetEnhanceCfg001, TestSize.Level1)
75 {
76 uint8_t cfgData[SEC_COMP_ENHANCE_CFG_SIZE] = { 0 };
77 int32_t result = SecCompEnhanceKit::SetEnhanceCfg(cfgData, SEC_COMP_ENHANCE_CFG_SIZE);
78 if (g_inputEnhanceExist) {
79 EXPECT_EQ(result, SC_OK);
80 } else {
81 EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE);
82 }
83 }
84
85 /**
86 * @tc.name: GetPoniterEventEnhanceData001
87 * @tc.desc: test GetPoniterEventEnhanceData
88 * @tc.type: FUNC
89 * @tc.require: AR000HO9IN
90 */
91 HWTEST_F(SecCompEnhanceTest, GetPoniterEventEnhanceData001, TestSize.Level1)
92 {
93 uint8_t originData[16] = { 0 };
94 uint32_t dataLen = 16;
95 uint8_t* enhanceData = nullptr;
96 uint32_t enHancedataLen = MAX_HMAC_SIZE;
97
98 int32_t result = SecCompEnhanceKit::GetPointerEventEnhanceData(originData, dataLen, enhanceData, enHancedataLen);
99 if (g_inputEnhanceExist) {
100 EXPECT_EQ(result, SC_SERVICE_ERROR_SERVICE_NOT_EXIST);
101 } else {
102 EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE);
103 }
104 }
105
106 /**
107 * @tc.name: EnableInputEnhance001
108 * @tc.desc: test UnmarshallEnhanceCfg
109 * @tc.type: FUNC
110 * @tc.require: AR000HO9IN
111 */
112 HWTEST_F(SecCompEnhanceTest, EnableInputEnhance001, TestSize.Level1)
113 {
114 int32_t result = SecCompEnhanceAdapter::EnableInputEnhance();
115 if (g_srvEnhanceExist) {
116 EXPECT_EQ(result, SC_OK);
117 } else {
118 EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE);
119 }
120 }
121
122 /**
123 * @tc.name: DisableInputEnhance001
124 * @tc.desc: test DisableInputEnhance
125 * @tc.type: FUNC
126 * @tc.require: AR000HO9IN
127 */
128 HWTEST_F(SecCompEnhanceTest, DisableInputEnhance001, TestSize.Level1)
129 {
130 int32_t result = SecCompEnhanceAdapter::DisableInputEnhance();
131 if (g_srvEnhanceExist) {
132 EXPECT_EQ(result, SC_OK);
133 } else {
134 EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE);
135 }
136 }
137
138 /**
139 * @tc.name: InitEnhanceHandler001
140 * @tc.desc: test InitEnhanceHandler
141 * @tc.type: FUNC
142 * @tc.require: AR000HO9IN
143 */
144 HWTEST_F(SecCompEnhanceTest, InitEnhanceHandler001, TestSize.Level1)
145 {
146 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
147 uint8_t originData[16] = { 0 };
148 uint32_t dataLen = 16;
149 uint8_t* enhanceData = nullptr;
150 uint32_t enHancedataLen = MAX_HMAC_SIZE;
151 ASSERT_NE(SC_OK,
152 SecCompEnhanceAdapter::GetPointerEventEnhanceData(originData, dataLen, enhanceData, enHancedataLen));
153 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
154 SecCompClickEvent touchInfo;
155 ASSERT_NE(SC_OK, SecCompEnhanceAdapter::CheckExtraInfo(touchInfo));
156 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
157
158 std::string componentInfo;
159 int32_t scId = 1;
160 SecCompEnhanceAdapter::EnhanceDataPreprocess(scId, componentInfo);
161 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
162 SecCompEnhanceAdapter::RegisterScIdEnhance(scId);
163 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
164 SecCompEnhanceAdapter::DisableInputEnhance();
165 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
166 SecCompEnhanceAdapter::StartEnhanceService();
167 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
168 SecCompEnhanceAdapter::ExistEnhanceService();
169 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
170 SecCompEnhanceAdapter::NotifyProcessDied(scId);
171 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
172 std::shared_ptr<SecCompBase> compInfo;
173 const nlohmann::json jsonComponent;
174 SecCompEnhanceAdapter::CheckComponentInfoEnhnace(scId, compInfo, jsonComponent);
175 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
176 SecCompEnhanceAdapter::GetEnhanceRemoteObject();
177 }
178
179 /**
180 * @tc.name: InitEnhanceHandler002
181 * @tc.desc: test InitEnhanceHandler
182 * @tc.type: FUNC
183 * @tc.require: AR000HO9IN
184 */
185 HWTEST_F(SecCompEnhanceTest, InitEnhanceHandler002, TestSize.Level1)
186 {
187 SecCompEnhanceAdapter::isEnhanceInputHandlerInit = true;
188 uint8_t cfgData[SEC_COMP_ENHANCE_CFG_SIZE] = { 0 };
189 SecCompEnhanceAdapter::inputHandler = nullptr;
190 ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE,
191 SecCompEnhanceAdapter::SetEnhanceCfg(cfgData, SEC_COMP_ENHANCE_CFG_SIZE));
192 uint8_t originData[16] = { 0 };
193 uint32_t dataLen = 16;
194 uint8_t* enhanceData = nullptr;
195 uint32_t enHancedataLen = MAX_HMAC_SIZE;
196 ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE,
197 SecCompEnhanceAdapter::GetPointerEventEnhanceData(originData, dataLen, enhanceData, enHancedataLen));
198 SecCompClickEvent touchInfo;
199 SecCompEnhanceAdapter::srvHandler = nullptr;
200 ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE, SecCompEnhanceAdapter::CheckExtraInfo(touchInfo));
201 std::string componentInfo;
202 int32_t scId = 1;
203 SecCompEnhanceAdapter::clientHandler = nullptr;
204 ASSERT_TRUE(SecCompEnhanceAdapter::EnhanceDataPreprocess(componentInfo));
205 ASSERT_TRUE(SecCompEnhanceAdapter::EnhanceDataPreprocess(scId, componentInfo));
206 SecCompEnhanceAdapter::RegisterScIdEnhance(scId);
207 ASSERT_EQ(SecCompEnhanceAdapter::srvHandler, nullptr);
208 ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE, SecCompEnhanceAdapter::EnableInputEnhance());
209 ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE, SecCompEnhanceAdapter::DisableInputEnhance());
210 SecCompEnhanceAdapter::StartEnhanceService();
211 SecCompEnhanceAdapter::ExistEnhanceService();
212 SecCompEnhanceAdapter::NotifyProcessDied(scId);
213 std::shared_ptr<SecCompBase> compInfo;
214 const nlohmann::json jsonComponent;
215 ASSERT_EQ(SC_OK, SecCompEnhanceAdapter::CheckComponentInfoEnhnace(scId, compInfo, jsonComponent));
216 ASSERT_EQ(nullptr, SecCompEnhanceAdapter::GetEnhanceRemoteObject());
217 }
218