• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_service_mock_test.h"
16 
17 #include "accesstoken_kit.h"
18 #include "ipc_skeleton.h"
19 #include "iservice_registry.h"
20 #include "location_button.h"
21 #include "paste_button.h"
22 #include "save_button.h"
23 #include "sec_comp_err.h"
24 #include "sec_comp_log.h"
25 #include "sec_comp_tool.h"
26 #include "sec_comp_enhance_adapter.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, "SecCompServiceMockTest"};
40 static AccessTokenID g_selfTokenId = 0;
41 }
42 
SetUpTestCase()43 void SecCompServiceMockTest::SetUpTestCase()
44 {}
45 
TearDownTestCase()46 void SecCompServiceMockTest::TearDownTestCase()
47 {}
48 
SetUp()49 void SecCompServiceMockTest::SetUp()
50 {
51     SC_LOG_INFO(LABEL, "setup");
52     if (secCompService_ != nullptr) {
53         return;
54     }
55     SecCompService* ptr = new (std::nothrow) SecCompService(ServiceTestCommon::SA_ID, true);
56     secCompService_ = sptr<SecCompService>(ptr);
57     ASSERT_NE(nullptr, secCompService_);
58     secCompService_->appStateObserver_ = new (std::nothrow) AppStateObserver();
59     ASSERT_NE(nullptr, secCompService_->appStateObserver_);
60     g_selfTokenId = GetSelfTokenID();
61 
62     Rosen::WindowManager::GetInstance().SetDefaultSecCompScene();
63 }
64 
TearDown()65 void SecCompServiceMockTest::TearDown()
66 {
67     if (secCompService_ != nullptr) {
68         secCompService_->appStateObserver_ = nullptr;
69     }
70     secCompService_ = nullptr;
71     EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
72 }
73 
74 /**
75  * @tc.name: RegisterSecurityComponent001
76  * @tc.desc: Test register security component
77  * @tc.type: FUNC
78  * @tc.require: AR000HO9J7
79  */
80 HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1)
81 {
82     // get caller fail
83     int32_t scId;
84     secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
85     secCompService_->Initialize();
86     EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "", scId));
87     nlohmann::json jsonRes;
88     ServiceTestCommon::BuildSaveComponentJson(jsonRes);
89     std::string saveInfo = jsonRes.dump();
90 
91     // parse component json fail
92     ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
93     AppExecFwk::AppStateData stateData = {
94         .uid = getuid()
95     };
96     secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
97     // wrong json
98     EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "{a=", scId));
99 
100     // register security component ok
101     EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
102     EXPECT_EQ(SC_OK, secCompService_->UpdateSecurityComponent(scId, saveInfo));
103     uint8_t buffer[1] = { 0 };
104     struct SecCompClickEvent touch = {
105         .type = ClickEventType::POINT_EVENT_TYPE,
106         .point.touchX = 100,
107         .point.touchY = 100,
108         .point.timestamp =
109             static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) /
110             ServiceTestCommon::TIME_CONVERSION_UNIT,
111         .extraInfo = {
112             .data = buffer,
113             .dataSize = 1
114         },
115     };
116 
117     EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, secCompService_->ReportSecurityComponentClickEvent(scId,
118             saveInfo, touch, nullptr, nullptr));
119     EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId));
120     SecCompPermManager::GetInstance().applySaveCountMap_.clear();
121 }
122 
123 /**
124  * @tc.name: RegisterSecurityComponent002
125  * @tc.desc: Test register security component check touch info failed
126  * @tc.type: FUNC
127  * @tc.require: AR000HO9J7
128  */
129 HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1)
130 {
131     int32_t scId;
132     secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
133     secCompService_->Initialize();
134     nlohmann::json jsonRes;
135     ServiceTestCommon::BuildSaveComponentJson(jsonRes);
136     std::string saveInfo = jsonRes.dump();
137 
138     ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
139     AppExecFwk::AppStateData stateData = {
140         .uid = getuid()
141     };
142     secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
143     // register security component ok
144     EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
145     struct SecCompClickEvent touch = {
146         .type = ClickEventType::POINT_EVENT_TYPE,
147         .point.touchX = 100,
148         .point.touchY = 100,
149         .point.timestamp = static_cast<uint64_t>(
150             std::chrono::high_resolution_clock::now().time_since_epoch().count())
151     };
152     EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
153         secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr, nullptr));
154     EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId));
155     SecCompPermManager::GetInstance().applySaveCountMap_.clear();
156 }
157 
158 /**
159  * @tc.name: RegisterSecurityComponent003
160  * @tc.desc: Test register security component permission grant failed
161  * @tc.type: FUNC
162  * @tc.require: AR000HO9J7
163  */
164 HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1)
165 {
166     int32_t scId;
167     secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
168     secCompService_->Initialize();
169     nlohmann::json jsonRes;
170     ServiceTestCommon::BuildSaveComponentJson(jsonRes);
171     std::string saveInfo = jsonRes.dump();
172 
173     ASSERT_EQ(0, SetSelfTokenID(0));
174     AppExecFwk::AppStateData stateData = {
175         .uid = getuid()
176     };
177     secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
178     // register security component ok
179     EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
180     uint8_t buffer[1] = { 0 };
181     struct SecCompClickEvent touch = {
182         .type = ClickEventType::POINT_EVENT_TYPE,
183         .point.touchX = 100,
184         .point.touchY = 100,
185         .point.timestamp =
186             static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) /
187             ServiceTestCommon::TIME_CONVERSION_UNIT,
188         .extraInfo = {
189             .data = buffer,
190             .dataSize = 1
191         },
192     };
193     EXPECT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
194         secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr, nullptr));
195     EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId));
196     SecCompPermManager::GetInstance().applySaveCountMap_.clear();
197 }
198 
199 /**
200  * @tc.name: ReportSecurityComponentClickEvent001
201  * @tc.desc: Test register security component
202  * @tc.type: FUNC
203  * @tc.require: AR000HO9J7
204  */
205 HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize.Level1)
206 {
207     int32_t scId;
208     secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
209     secCompService_->Initialize();
210     nlohmann::json jsonRes;
211     ServiceTestCommon::BuildSaveComponentJson(jsonRes);
212     std::string saveInfo = jsonRes.dump();
213 
214     ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
215     AppExecFwk::AppStateData stateData = {
216         .uid = getuid()
217     };
218     secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
219     // register security component ok
220     EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId));
221     uint8_t buffer[1] = { 0 };
222     struct SecCompClickEvent clickInfo = {
223         .type = ClickEventType::POINT_EVENT_TYPE,
224         .point.touchX = 100,
225         .point.touchY = 100,
226         .point.timestamp =
227             static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) /
228             ServiceTestCommon::TIME_CONVERSION_UNIT,
229         .extraInfo = {
230             .data = buffer,
231             .dataSize = 1
232         },
233     };
234 
235     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo,
236         clickInfo, nullptr, nullptr));
237 
238     // test 10s valid
239     ASSERT_FALSE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID));
240     ASSERT_FALSE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID));
241     sleep(11);
242     ASSERT_FALSE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID));
243 
244     // test 10s multiple clicks
245     clickInfo.point.timestamp = static_cast<uint64_t>(
246         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT;
247     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, secCompService_->ReportSecurityComponentClickEvent(scId,
248         saveInfo, clickInfo, nullptr, nullptr));
249     sleep(3);
250     clickInfo.point.timestamp = static_cast<uint64_t>(
251         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / ServiceTestCommon::TIME_CONVERSION_UNIT;
252     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID, secCompService_->ReportSecurityComponentClickEvent(scId,
253         saveInfo, clickInfo, nullptr, nullptr));
254     sleep(8);
255     ASSERT_FALSE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID));
256     sleep(2);
257     EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId));
258 }
259 
260 /**
261  * @tc.name: ReportSecurityComponentClickEvent002
262  * @tc.desc: Test verify location permission
263  * @tc.type: FUNC
264  * @tc.require: AR000HO9IN
265  */
266 HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize.Level1)
267 {
268     SC_LOG_INFO(LABEL, "ReportSecurityComponentClickEvent002");
269     int32_t scId;
270     secCompService_->state_ = ServiceRunningState::STATE_RUNNING;
271     secCompService_->Initialize();
272     nlohmann::json jsonRes;
273     ServiceTestCommon::BuildLocationComponentJson(jsonRes);
274     std::string locationInfo = jsonRes.dump();
275 
276     ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID));
277     AppExecFwk::AppStateData stateData = {
278         .uid = getuid()
279     };
280     secCompService_->appStateObserver_->AddProcessToForegroundSet(stateData);
281     // register security component ok
282     EXPECT_EQ(SC_OK, secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
283     uint8_t buffer[1] = { 0 };
284     struct SecCompClickEvent clickInfo1 = {
285         .type = ClickEventType::POINT_EVENT_TYPE,
286         .point.touchX = 100,
287         .point.touchY = 100,
288         .point.timestamp =
289             static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()) /
290             ServiceTestCommon::TIME_CONVERSION_UNIT,
291         .extraInfo = {
292             .data = buffer,
293             .dataSize = 1
294         },
295     };
296 
297     ASSERT_EQ(SC_SERVICE_ERROR_CLICK_EVENT_INVALID,
298         secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, clickInfo1, nullptr, nullptr));
299 
300     // test 10s valid
301     ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0);
302     ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID,
303         "ohos.permission.APPROXIMATELY_LOCATION"), 0);
304     sleep(11);
305     ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0);
306     ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID,
307         "ohos.permission.APPROXIMATELY_LOCATION"), 0);
308 
309     SecCompManager::GetInstance().NotifyProcessBackground(getpid());
310     ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0);
311     ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID,
312         "ohos.permission.APPROXIMATELY_LOCATION"), 0);
313     sleep(11);
314     ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0);
315     ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID,
316         "ohos.permission.APPROXIMATELY_LOCATION"), 0);
317 }
318