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_kit_test.h"
16
17 #include "location_button.h"
18 #define private public
19 #include "sec_comp_caller_authorization.h"
20 #include "sec_comp_client.h"
21 #include "sec_comp_load_callback.h"
22 #undef private
23 #include "sec_comp_err.h"
24 #include "sec_comp_info.h"
25 #include "sec_comp_log.h"
26 #include "sec_comp_tool.h"
27 #include "test_common.h"
28
29 using namespace testing::ext;
30 using namespace OHOS::Security::SecurityComponent;
31
32 namespace {
33 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
34 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompKitTest"};
35
TestInCallerNotCheckList()36 static void TestInCallerNotCheckList()
37 {
38 int32_t scId = -1;
39 struct SecCompClickEvent click;
40 std::string emptyStr = "";
41 int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, emptyStr, scId);
42 int updateRes = SecCompKit::UpdateSecurityComponent(scId, emptyStr);
43 int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, click, nullptr);
44
45 EXPECT_EQ(registerRes, SC_SERVICE_ERROR_CALLER_INVALID);
46 EXPECT_EQ(updateRes, SC_SERVICE_ERROR_CALLER_INVALID);
47 EXPECT_EQ(reportRes, SC_SERVICE_ERROR_CALLER_INVALID);
48 }
49
TestInCallerCheckList()50 static void TestInCallerCheckList()
51 {
52 int32_t scId = -1;
53 struct SecCompClickEvent click;
54 std::string emptyStr = "";
55 int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, emptyStr, scId);
56 int updateRes = SecCompKit::UpdateSecurityComponent(scId, emptyStr);
57 int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, click, nullptr);
58
59 EXPECT_NE(registerRes, SC_SERVICE_ERROR_CALLER_INVALID);
60 EXPECT_NE(updateRes, SC_SERVICE_ERROR_CALLER_INVALID);
61 EXPECT_NE(reportRes, SC_SERVICE_ERROR_CALLER_INVALID);
62 }
63 } // namespace
64
SetUpTestCase()65 void SecCompKitTest::SetUpTestCase()
66 {
67 SC_LOG_INFO(LABEL, "SetUpTestCase.");
68 }
69
TearDownTestCase()70 void SecCompKitTest::TearDownTestCase()
71 {
72 SC_LOG_INFO(LABEL, "TearDownTestCase.");
73 }
74
SetUp()75 void SecCompKitTest::SetUp()
76 {
77 SC_LOG_INFO(LABEL, "SetUp ok.");
78 }
79
TearDown()80 void SecCompKitTest::TearDown()
81 {
82 SC_LOG_INFO(LABEL, "TearDown.");
83 }
84
85 /**
86 * @tc.name: ExceptCall001
87 * @tc.desc: do kit except call.
88 * @tc.type: FUNC
89 * @tc.require: AR000HO9IN
90 */
91 HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1)
92 {
93 LocationButton comp;
94 comp.fontSize_ = TestCommon::TEST_SIZE;
95 comp.iconSize_ = TestCommon::TEST_SIZE;
96 comp.padding_.top = TestCommon::TEST_DIMENSION;
97 comp.padding_.right = TestCommon::TEST_DIMENSION;
98 comp.padding_.bottom = TestCommon::TEST_DIMENSION;
99 comp.padding_.left = TestCommon::TEST_DIMENSION;
100 comp.textIconSpace_ = TestCommon::TEST_SIZE;
101 comp.bgColor_.value = TestCommon::TEST_COLOR;
102 comp.fontColor_.value = TestCommon::TEST_COLOR;
103 comp.iconColor_.value = TestCommon::TEST_COLOR;
104 comp.borderWidth_ = TestCommon::TEST_SIZE;
105 comp.parentEffect_ = true;
106 comp.type_ = LOCATION_COMPONENT;
107 comp.rect_.x_ = TestCommon::TEST_COORDINATE;
108 comp.rect_.y_ = TestCommon::TEST_COORDINATE;
109 comp.rect_.width_ = TestCommon::TEST_COORDINATE;
110 comp.rect_.height_ = TestCommon::TEST_COORDINATE;
111
112 nlohmann::json jsonRes;
113 comp.ToJson(jsonRes);
114 int32_t scId = -1;
115 std::string jsonStr = jsonRes.dump();
116 ASSERT_NE(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, jsonStr, scId));
117 ASSERT_EQ(-1, scId);
118 ASSERT_NE(SC_OK, SecCompKit::UpdateSecurityComponent(scId, jsonStr));
119
120 struct SecCompClickEvent touch = {
121 .type = ClickEventType::POINT_EVENT_TYPE,
122 .point.touchX = TestCommon::TEST_COORDINATE,
123 .point.touchY = TestCommon::TEST_COORDINATE,
124 .point.timestamp = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count())
125 };
126 EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonStr, touch, nullptr));
127 EXPECT_NE(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
128 }
129
130 /**
131 * @tc.name: ExceptCall001
132 * @tc.desc: test caller check.
133 * @tc.type: FUNC
134 * @tc.require: AR000HO9JS
135 */
136 HWTEST_F(SecCompKitTest, TestCallerCheck001, TestSize.Level1)
137 {
138 std::vector<uintptr_t> callerList = {
139 reinterpret_cast<uintptr_t>(TestInCallerCheckList),
140 };
141 SecCompUiRegister registerCallback(callerList, nullptr);
142 TestInCallerCheckList();
143 TestInCallerNotCheckList();
144
145 // prohibit init caller list repeately
146 std::vector<uintptr_t> callerList1 = {
147 reinterpret_cast<uintptr_t>(TestInCallerNotCheckList),
148 };
149 SecCompUiRegister registerCallback1(callerList1, nullptr);
150 TestInCallerNotCheckList();
151 SecCompCallerAuthorization::GetInstance().kitCallerList_.clear();
152 SecCompCallerAuthorization::GetInstance().isInit_ = false;
153 }
154
155 /**
156 * @tc.name: ExceptCall002
157 * @tc.desc: test invalid caller register.
158 * @tc.type: FUNC
159 * @tc.require: AR000HO9JS
160 */
161 HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1)
162 {
163 std::vector<uintptr_t> callerList;
164 SecCompUiRegister registerCallback(callerList, nullptr);
165 TestInCallerNotCheckList();
166 SecCompCallerAuthorization::GetInstance().kitCallerList_.clear();
167 SecCompCallerAuthorization::GetInstance().isInit_ = false;
168
169 for (size_t i = 0; i < TestCommon::MAX_CALLER_SIZE + 1; i++) {
170 callerList.emplace_back(reinterpret_cast<uintptr_t>(TestInCallerNotCheckList));
171 }
172 SecCompUiRegister registerCallback2(callerList, nullptr);
173 TestInCallerNotCheckList();
174 SecCompCallerAuthorization::GetInstance().kitCallerList_.clear();
175 SecCompCallerAuthorization::GetInstance().isInit_ = false;
176 }
177
178 /**
179 * @tc.name: RegisterWithoutCallback001
180 * @tc.desc: test register without callback.
181 * @tc.type: FUNC
182 * @tc.require: AR000HO9JM
183 */
184 HWTEST_F(SecCompKitTest, RegisterWithoutCallback001, TestSize.Level1)
185 {
186 nlohmann::json jsonRes;
187 TestCommon::BuildLocationComponentInfo(jsonRes);
188 std::string locationInfo = jsonRes.dump();
189
190 int32_t scId;
191 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
192 ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST,
193 SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
194 ASSERT_EQ(-1, scId);
195 #else
196 ASSERT_EQ(SC_OK,
197 SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
198 ASSERT_NE(-1, scId);
199 EXPECT_EQ(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
200 #endif
201 }
202
203 /**
204 * @tc.name: FinishStartSAFail001
205 * @tc.desc: Test update security component caller error
206 * @tc.type: FUNC
207 * @tc.require: AR000HO9IN
208 */
209 HWTEST_F(SecCompKitTest, FinishStartSAFail001, TestSize.Level1)
210 {
211 SecCompClient::GetInstance().FinishStartSAFail();
212 EXPECT_TRUE(SecCompClient::GetInstance().readyFlag_);
213 SecCompClient::GetInstance().OnRemoteDiedHandle();
214 EXPECT_EQ(nullptr, SecCompClient::GetInstance().proxy_);
215 SecCompClient::GetInstance().GetProxyFromRemoteObject(nullptr);
216 }
217
218 /**
219 * @tc.name: OnLoadSystemAbilitySuccess001
220 * @tc.desc: Test update security component caller error
221 * @tc.type: FUNC
222 * @tc.require: AR000HO9IN
223 */
224 HWTEST_F(SecCompKitTest, OnLoadSystemAbilitySuccess001, TestSize.Level1)
225 {
226 std::shared_ptr<SecCompLoadCallback> loadCallback = std::make_shared<SecCompLoadCallback>();
227 EXPECT_NE(nullptr, loadCallback);
228 int32_t systemAbilityId = SC_OK;
229 loadCallback->OnLoadSystemAbilitySuccess(systemAbilityId, nullptr);
230 loadCallback->OnLoadSystemAbilityFail(systemAbilityId);
231 systemAbilityId = SA_ID_SECURITY_COMPONENT_SERVICE;
232 loadCallback->OnLoadSystemAbilitySuccess(systemAbilityId, nullptr);
233 loadCallback->OnLoadSystemAbilityFail(systemAbilityId);
234 }
235