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