• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_service_test.h"
16 
17 #include "ipc_skeleton.h"
18 #include "iservice_registry.h"
19 #include "location_button.h"
20 #include "mock_system_ability_proxy.h"
21 #include "mock_app_mgr_proxy.h"
22 #include "paste_button.h"
23 #include "save_button.h"
24 #include "sec_comp_err.h"
25 #include "sec_comp_log.h"
26 #include "sec_comp_tool.h"
27 #include "service_test_common.h"
28 #include "system_ability.h"
29 #include "token_setproc.h"
30 #include "window_manager.h"
31 
32 using namespace testing::ext;
33 using namespace OHOS;
34 using namespace OHOS::Security::SecurityComponent;
35 using namespace OHOS::Security::AccessToken;
36 
37 namespace {
38 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
39     LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompServiceTest"};
40 static AccessTokenID g_selfTokenId = 0;
41 }
42 
SetUpTestCase()43 void SecCompServiceTest::SetUpTestCase()
44 {
45     system("kill -9 `pidof security_compon`");
46 }
47 
TearDownTestCase()48 void SecCompServiceTest::TearDownTestCase()
49 {}
50 
SetUp()51 void SecCompServiceTest::SetUp()
52 {
53     SC_LOG_INFO(LABEL, "setup");
54     if (secCompService_ != nullptr) {
55         return;
56     }
57     SecCompService* ptr = new (std::nothrow) SecCompService(ServiceTestCommon::SA_ID, true);
58     secCompService_ = sptr<SecCompService>(ptr);
59     ASSERT_NE(nullptr, secCompService_);
60     secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver();
61     ASSERT_NE(nullptr, secCompService_->appStateObserver_);
62     g_selfTokenId = GetSelfTokenID();
63 
64     Rosen::WindowManager::GetInstance().SetDefaultSecCompScene();
65 }
66 
TearDown()67 void SecCompServiceTest::TearDown()
68 {
69     if (secCompService_ != nullptr) {
70         secCompService_->appStateObserver_ = nullptr;
71     }
72     secCompService_ = nullptr;
73     EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
74 }
75 
76 /**
77  * @tc.name: Onstart001
78  * @tc.desc: Test OnStart
79  * @tc.type: FUNC
80  * @tc.require: AR000HO9J7
81  */
82 HWTEST_F(SecCompServiceTest, OnStart001, TestSize.Level1)
83 {
84     secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
85     secCompService_->appStateObserver_ = nullptr;
86     secCompService_->OnStart();
87     ASSERT_EQ(nullptr, secCompService_->appStateObserver_);
88     EXPECT_CALL(*secCompService_, Publish(testing::_)).WillOnce(testing::Return(false));
89 
90     secCompService_->state_ = ServiceRunningState::STATE_NOT_START;
91     secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver();
92     secCompService_->OnStart();
93     ASSERT_EQ(ServiceRunningState::STATE_RUNNING, secCompService_->state_);
94 
95     secCompService_->OnStop();
96 }
97 
98 /**
99  * @tc.name: RegisterAppStateObserver001
100  * @tc.desc: Test RegisterAppStateObserver
101  * @tc.type: FUNC
102  * @tc.require: AR000HO9J7
103  */
104 HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1)
105 {
106     // GetSystemAbilityManager get failed
107     secCompService_->appStateObserver_ = nullptr;
108     std::shared_ptr<SystemAbilityManagerClient> saClient = std::make_shared<SystemAbilityManagerClient>();
109     ASSERT_NE(nullptr, saClient);
110     SystemAbilityManagerClient::clientInstance = saClient.get();
111     EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(nullptr));
112     EXPECT_FALSE(secCompService_->RegisterAppStateObserver());
113 
114     // GetSystemAbility get app mgr failed
115     secCompService_->appStateObserver_ = nullptr;
116     sptr<SystemAbilityManagerProxy> proxy = new SystemAbilityManagerProxy(nullptr);
117     EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy));
118     EXPECT_FALSE(secCompService_->RegisterAppStateObserver());
119 
120     // RegisterApplicationStateObserver failed
121     secCompService_->appStateObserver_ = nullptr;
122     EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy));
123     sptr<MockIRemoteObject> object = new MockIRemoteObject();
124     EXPECT_CALL(*proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(object));
125     sptr<MockAppMgrProxy> appProxy = new (std::nothrow) MockAppMgrProxy(nullptr);
126     MockAppMgrProxy::g_MockAppMgrProxy = appProxy;
127     EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy,
128         RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(-1));
129     EXPECT_FALSE(secCompService_->RegisterAppStateObserver());
130 
131     // GetForegroundApplications failed
132     secCompService_->appStateObserver_ = nullptr;
133     EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy));
134     EXPECT_CALL(*proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(object));
135     EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy,
136         RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(0));
137     EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy,
138         GetForegroundApplications(testing::_)).WillOnce(testing::Return(-1));
139     EXPECT_TRUE(secCompService_->RegisterAppStateObserver());
140     EXPECT_EQ(static_cast<const size_t>(0), secCompService_->appStateObserver_->foregrandProcList_.size());
141 
142     // get one foreground app
143     secCompService_->appStateObserver_ = nullptr;
144     EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy));
145     EXPECT_CALL(*proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(object));
146     EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy,
147         RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(0));
148     EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, GetForegroundApplications(testing::_))
__anonae7413ac0202(std::vector<AppExecFwk::AppStateData>& list) 149         .WillOnce([](std::vector<AppExecFwk::AppStateData>& list) {
150             AppExecFwk::AppStateData data;
151             data.uid = 1000;
152             list.emplace_back(data);
153             return 0;
154         });
155     EXPECT_TRUE(secCompService_->RegisterAppStateObserver());
156     EXPECT_EQ(static_cast<const size_t>(1), secCompService_->appStateObserver_->foregrandProcList_.size());
157     secCompService_->UnregisterAppStateObserver();
158     SystemAbilityManagerClient::clientInstance = nullptr;
159 }
160 
161 /**
162  * @tc.name: UnregisterAppStateObserver001
163  * @tc.desc: Test RegisterAppStateObserver
164  * @tc.type: FUNC
165  * @tc.require: AR000HO9J7
166  */
167 HWTEST_F(SecCompServiceTest, UnregisterAppStateObserver001, TestSize.Level1)
168 {
169     // GetSystemAbilityManager get failed
170     secCompService_->appStateObserver_ = nullptr;
171     sptr<MockAppMgrProxy> appProxy = new (std::nothrow) MockAppMgrProxy(nullptr);
172     secCompService_->iAppMgr_ = appProxy;
173 
174     EXPECT_CALL(*appProxy, UnregisterApplicationStateObserver(testing::_)).Times(testing::Exactly(0));
175     secCompService_->UnregisterAppStateObserver();
176 }
177 
178 /**
179  * @tc.name: GetCallerInfo001
180  * @tc.desc: Test get caller info
181  * @tc.type: FUNC
182  * @tc.require: AR000HO9J7
183  */
184 HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level1)
185 {
186     // not root uid
187     setuid(1);
188     SecCompCallerInfo caller;
189     EXPECT_FALSE(secCompService_->GetCallerInfo(caller));
190 
191     // set token id to hap token, but uid is not in foreground
192     EXPECT_FALSE(secCompService_->GetCallerInfo(caller));
193     setuid(0);
194     ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
195     // add local uid to foreground.
196     AppExecFwk::AppStateData stateData = {
197         .uid = getuid()
198     };
199     secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
200     EXPECT_TRUE(secCompService_->GetCallerInfo(caller));
201 }
202 
203 /**
204  * @tc.name: UnregisterSecurityComponent001
205  * @tc.desc: Test unregister security component
206  * @tc.type: FUNC
207  * @tc.require: AR000HO9J7
208  */
209 HWTEST_F(SecCompServiceTest, UnregisterSecurityComponent001, TestSize.Level1)
210 {
211     // get caller fail
212     EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST,
213         secCompService_->UnregisterSecurityComponent(ServiceTestCommon::TEST_SC_ID_1));
214 }
215 
216 /**
217  * @tc.name: UpdateSecurityComponent001
218  * @tc.desc: Test update security component
219  * @tc.type: FUNC
220  * @tc.require: AR000HO9J7
221  */
222 HWTEST_F(SecCompServiceTest, UpdateSecurityComponent001, TestSize.Level1)
223 {
224     // get caller fail
225     EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
226         secCompService_->UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, ""));
227 
228     ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
229     AppExecFwk::AppStateData stateData = {
230         .uid = getuid()
231     };
232     secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
233     EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
234         secCompService_->UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, "{a"));
235 }
236 
237 /**
238  * @tc.name: ReportSecurityComponentClickEvent001
239  * @tc.desc: Test report security component
240  * @tc.type: FUNC
241  * @tc.require: AR000HO9J7
242  */
243 HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Level1)
244 {
245     auto uid = getuid();
246     // get caller fail
247     int32_t scId;
248     EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
249         secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId));
250 
251     nlohmann::json jsonRes;
252     ServiceTestCommon::BuildLocationComponentJson(jsonRes);
253     std::string locationInfo = jsonRes.dump();
254 
255     // parse component json fail
256     ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
257     setuid(100);
258     AppExecFwk::AppStateData stateData = {
259         .uid = getuid()
260     };
261     secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
262 
263     EXPECT_EQ(SC_OK,
264         secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
265     uint8_t data[16] = { 0 };
266     struct SecCompClickEvent touch = {
267         .type = ClickEventType::POINT_EVENT_TYPE,
268         .point.touchX = 100,
269         .point.touchY = 100,
270         .point.timestamp =
271             static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) /
272             ServiceTestCommon::TIME_CONVERSION_UNIT,
273         .extraInfo.data = data,
274         .extraInfo.dataSize = 16,
275     };
276     EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
277         secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr, nullptr));
278     EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId));
279     setuid(uid);
280 }
281 
282 /**
283  * @tc.name: Dump001
284  * @tc.desc: Test dump
285  * @tc.type: FUNC
286  * @tc.require: AR000HO9J7
287  */
288 HWTEST_F(SecCompServiceTest, Dump001, TestSize.Level1)
289 {
290     int fd = -1;
291     std::vector<std::u16string> args;
292 
293     ASSERT_EQ(ERR_INVALID_VALUE, secCompService_->Dump(fd, args));
294 
295     fd = 0;
296 
297     // hidumper
298     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
299 
300     // hidumper -h
301     args.emplace_back(Str8ToStr16("-h"));
302     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
303 
304     args.clear();
305     // hidumper -p
306     args.emplace_back(Str8ToStr16("-p"));
307     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
308 
309     args.clear();
310     // hidumper -a
311     args.emplace_back(Str8ToStr16("-a"));
312     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
313 
314     args.clear();
315     // hidumper -""
316     args.emplace_back(Str8ToStr16(""));
317     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
318 
319     args.clear();
320     // hidumper -t
321     args.emplace_back(Str8ToStr16("-t"));
322     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
323 }
324 
325 /**
326  * @tc.name: Onstart002
327  * @tc.desc: Test OnStart
328  * @tc.type: FUNC
329  * @tc.require: AR000HO9J7
330  */
331 HWTEST_F(SecCompServiceTest, OnStart002, TestSize.Level1)
332 {
333     secCompService_->state_ = ServiceRunningState::STATE_NOT_START;
334     secCompService_->appStateObserver_ = nullptr;
335     std::shared_ptr<SystemAbilityManagerClient> saClient = std::make_shared<SystemAbilityManagerClient>();
336     ASSERT_NE(nullptr, saClient);
337     SystemAbilityManagerClient::clientInstance = saClient.get();
338     EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(nullptr));
339     secCompService_->OnStart();
340     secCompService_->state_ = ServiceRunningState::STATE_NOT_START;
341     secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver();
342     secCompService_->OnStart();
343 }
344 
345 /**
346  * @tc.name: GetCallerInfo002
347  * @tc.desc: Test get caller info
348  * @tc.type: FUNC
349  * @tc.require: AR000HO9J7
350  */
351 HWTEST_F(SecCompServiceTest, GetCallerInfo002, TestSize.Level1)
352 {
353     SecCompCallerInfo caller;
354     setuid(1);
355     SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID);
356     EXPECT_FALSE(secCompService_->GetCallerInfo(caller));
357 
358     setuid(0);
359     const std::string componentInfo;
360     nlohmann::json jsonRes;
361     int32_t scId  = 0;
362     EXPECT_EQ(secCompService_->ParseParams(componentInfo, caller, jsonRes), SC_SERVICE_ERROR_VALUE_INVALID);
363     EXPECT_NE(secCompService_->UnregisterSecurityComponent(scId), SC_SERVICE_ERROR_VALUE_INVALID);
364 
365     struct SecCompClickEvent touchInfo;
366     EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo, nullptr, nullptr),
367       SC_SERVICE_ERROR_VALUE_INVALID);
368     secCompService_->GetEnhanceRemoteObject();
369 }
370