• 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:
81  */
82 HWTEST_F(SecCompServiceTest, OnStart001, TestSize.Level0)
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:
103  */
104 HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level0)
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::_))
__anon1e36528c0202(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:
166  */
167 HWTEST_F(SecCompServiceTest, UnregisterAppStateObserver001, TestSize.Level0)
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:
183  */
184 HWTEST_F(SecCompServiceTest, GetCallerInfo001, TestSize.Level0)
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: UnregisterSecurityComponentBody001
205  * @tc.desc: Test unregister security component
206  * @tc.type: FUNC
207  * @tc.require:
208  */
209 HWTEST_F(SecCompServiceTest, UnregisterSecurityComponentBody001, TestSize.Level0)
210 {
211     // get caller fail
212     EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_NOT_EXIST,
213         secCompService_->UnregisterSecurityComponentBody(ServiceTestCommon::TEST_SC_ID_1));
214 }
215 
216 /**
217  * @tc.name: UpdateSecurityComponentBody001
218  * @tc.desc: Test update security component
219  * @tc.type: FUNC
220  * @tc.require:
221  */
222 HWTEST_F(SecCompServiceTest, UpdateSecurityComponentBody001, TestSize.Level0)
223 {
224     // get caller fail
225     EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
226         secCompService_->UpdateSecurityComponentBody(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_->UpdateSecurityComponentBody(ServiceTestCommon::TEST_SC_ID_1, "{a"));
235 }
236 
237 /**
238  * @tc.name: ReportSecurityComponentClickEventBody001
239  * @tc.desc: Test report security component
240  * @tc.type: FUNC
241  * @tc.require:
242  */
243 HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEventBody001, TestSize.Level0)
244 {
245     auto uid = getuid();
246     // get caller fail
247     int32_t scId;
248     EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID,
249         secCompService_->RegisterSecurityComponentBody(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_->RegisterSecurityComponentBody(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     SecCompInfo secCompInfo{ scId, locationInfo, touch };
277     std::string message;
278     EXPECT_EQ(SC_OK,
279         secCompService_->ReportSecurityComponentClickEventBody(secCompInfo, nullptr, nullptr, message));
280     EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponentBody(scId));
281     setuid(uid);
282 }
283 
284 /**
285  * @tc.name: Dump001
286  * @tc.desc: Test dump
287  * @tc.type: FUNC
288  * @tc.require:
289  */
290 HWTEST_F(SecCompServiceTest, Dump001, TestSize.Level0)
291 {
292     int fd = -1;
293     std::vector<std::u16string> args;
294 
295     ASSERT_EQ(ERR_INVALID_VALUE, secCompService_->Dump(fd, args));
296 
297     fd = 0;
298 
299     // hidumper
300     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
301 
302     // hidumper -h
303     args.emplace_back(Str8ToStr16("-h"));
304     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
305 
306     args.clear();
307     // hidumper -p
308     args.emplace_back(Str8ToStr16("-p"));
309     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
310 
311     args.clear();
312     // hidumper -a
313     args.emplace_back(Str8ToStr16("-a"));
314     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
315 
316     args.clear();
317     // hidumper -""
318     args.emplace_back(Str8ToStr16(""));
319     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
320 
321     args.clear();
322     // hidumper -t
323     args.emplace_back(Str8ToStr16("-t"));
324     ASSERT_EQ(SC_OK, secCompService_->Dump(fd, args));
325 }
326 
327 /**
328  * @tc.name: Onstart002
329  * @tc.desc: Test OnStart
330  * @tc.type: FUNC
331  * @tc.require:
332  */
333 HWTEST_F(SecCompServiceTest, OnStart002, TestSize.Level0)
334 {
335     secCompService_->state_ = ServiceRunningState::STATE_NOT_START;
336     secCompService_->appStateObserver_ = nullptr;
337     std::shared_ptr<SystemAbilityManagerClient> saClient = std::make_shared<SystemAbilityManagerClient>();
338     ASSERT_NE(nullptr, saClient);
339     SystemAbilityManagerClient::clientInstance = saClient.get();
340     EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(nullptr));
341     secCompService_->OnStart();
342     secCompService_->state_ = ServiceRunningState::STATE_NOT_START;
343     secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver();
344     secCompService_->OnStart();
345 }
346 
347 /**
348  * @tc.name: GetCallerInfo002
349  * @tc.desc: Test get caller info
350  * @tc.type: FUNC
351  * @tc.require:
352  */
353 HWTEST_F(SecCompServiceTest, GetCallerInfo002, TestSize.Level0)
354 {
355     SecCompCallerInfo caller;
356     setuid(1);
357     SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID);
358     EXPECT_FALSE(secCompService_->GetCallerInfo(caller));
359 
360     setuid(0);
361     const std::string componentInfo;
362     nlohmann::json jsonRes;
363     int32_t scId  = 0;
364     EXPECT_EQ(secCompService_->ParseParams(componentInfo, caller, jsonRes), SC_SERVICE_ERROR_VALUE_INVALID);
365     EXPECT_NE(secCompService_->UnregisterSecurityComponentBody(scId), SC_SERVICE_ERROR_VALUE_INVALID);
366 
367     struct SecCompClickEvent touchInfo = {};
368     SecCompInfo secCompInfo{ scId, componentInfo, touchInfo };
369     std::string message;
370     EXPECT_EQ(secCompService_->ReportSecurityComponentClickEventBody(secCompInfo, nullptr, nullptr, message),
371       SC_SERVICE_ERROR_VALUE_INVALID);
372 }
373