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 <functional>
16 #include <chrono>
17 #include <thread>
18 #include <message_parcel.h>
19 #include <climits>
20 #include <dlfcn.h>
21
22 #include "gtest/gtest.h"
23 #include "gtest/hwext/gtest-multithread.h"
24 #include "singleton.h"
25
26 #include "device_standby_switch.h"
27 #include "time_provider.h"
28 #include "common_event_support.h"
29 #include "common_event_observer.h"
30
31 #include "state_manager_adapter.h"
32 #include "constraint_manager_adapter.h"
33 #include "istandby_ipc_inteface_code.h"
34 #include "listener_manager_adapter.h"
35 #include "strategy_manager_adapter.h"
36
37 #include "standby_state.h"
38 #include "allow_type.h"
39 #include "standby_ipc_interface_code.h"
40 #include "standby_service_client.h"
41 #include "standby_service.h"
42 #include "ability_manager_helper.h"
43 #include "standby_service_impl.h"
44 #include "standby_state_subscriber.h"
45 #include "standby_state_subscriber.h"
46 #include "standby_service_subscriber_stub.h"
47 #include "bundle_manager_helper.h"
48 #include "standby_config_manager.h"
49 #include "app_state_observer.h"
50 #include "app_mgr_constants.h"
51 using namespace testing::ext;
52 using namespace testing::mt;
53
54 extern void MockPublishCommonEvent(bool mockRet);
55 extern void MockSubscribeCommonEvent(bool mockRet);
56 extern void MockSubscribeCommonEvent(bool mockRet);
57 extern void MockGetAllRunningProcesses(bool mockRet);
58 extern void MockGetRunningSystemProcess(bool mockRet);
59 extern void MockSubscribeObserver(bool mockRet);
60 extern void MockGetTokenTypeFlag(bool mockRet);
61 extern void MockStartTimer(bool mockRet);
62
63 namespace OHOS {
64 namespace DevStandbyMgr {
65 namespace {
66 const uint32_t ALL_DEPENDS_READY = 127;
67 constexpr int32_t DEFAULT_UID = 0;
68 constexpr int32_t SAMPLE_APP_UID = 10001;
69 const std::string SAMPLE_BUNDLE_NAME = "name";
70 const std::string DEFAULT_BUNDLENAME = "test";
71 const std::string DEFAULT_KEY = "0_test";
72 const vector<std::string> COMMON_EVENT_LIST = {
73 EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED,
74 EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED,
75 EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED,
76 EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED,
77 EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED,
78 EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED,
79 EventFwk::CommonEventSupport::COMMON_EVENT_NITZ_TIMEZONE_CHANGED,
80 EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED,
81 EventFwk::CommonEventSupport::COMMON_EVENT_NITZ_TIME_CHANGED
82 };
83 constexpr int32_t SLEEP_TIMEOUT = 500;
84 }
85
86 class StandbyServiceUnitTest : public testing::Test {
87 public:
88 static void SetUpTestCase();
89 static void TearDownTestCase();
SetUp()90 void SetUp() override {}
91 void TearDown() override;
92
SleepForFC()93 inline static void SleepForFC()
94 {
95 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMEOUT));
96 }
97 };
98
SetUpTestCase()99 void StandbyServiceUnitTest::SetUpTestCase()
100 {
101 StandbyServiceImpl::GetInstance()->Init();
102
103 StandbyServiceImpl::GetInstance()->constraintManager_ = std::make_shared<ConstraintManagerAdapter>();
104 StandbyServiceImpl::GetInstance()->listenerManager_ = std::make_shared<ListenerManagerAdapter>();
105 StandbyServiceImpl::GetInstance()->strategyManager_ = std::make_shared<StrategyManagerAdapter>();
106 StandbyServiceImpl::GetInstance()->standbyStateManager_ = std::make_shared<StateManagerAdapter>();
107 StandbyServiceImpl::GetInstance()->InitReadyState();
108 SleepForFC();
109 }
110
TearDown()111 void StandbyServiceUnitTest::TearDown()
112 {
113 SleepForFC();
114 StandbyServiceImpl::GetInstance()->allowInfoMap_.clear();
115 }
116
TearDownTestCase()117 void StandbyServiceUnitTest::TearDownTestCase()
118 {
119 SleepForFC();
120 if (StandbyServiceImpl::GetInstance()->handler_) {
121 StandbyServiceImpl::GetInstance()->handler_->RemoveAllEvents();
122 auto runner = StandbyServiceImpl::GetInstance()->handler_->GetEventRunner();
123 if (runner) {
124 runner->Stop();
125 runner = nullptr;
126 }
127 StandbyServiceImpl::GetInstance()->handler_ = nullptr;
128 }
129 StandbyServiceImpl::GetInstance()->UnInit();
130 }
131
132 /**
133 * @tc.name: StandbyServiceUnitTest_001
134 * @tc.desc: test OnStart of StandbyService.
135 * @tc.type: FUNC
136 * @tc.require:
137 */
138 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_001, TestSize.Level1)
139 {
140 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
141 StandbyService::GetInstance()->OnStart();
142 EXPECT_NE(StandbyServiceImpl::GetInstance()->handler_, nullptr);
143 StandbyServiceImpl::GetInstance()->InitReadyState();
144 SleepForFC();
145 EXPECT_EQ(StandbyService::GetInstance()->state_, ServiceRunningState::STATE_RUNNING);
146 }
147
148 /**
149 * @tc.name: StandbyServiceUnitTest_002
150 * @tc.desc: test OnAddSystemAbility of StandbyService.
151 * @tc.type: FUNC
152 * @tc.require:
153 */
154 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_002, TestSize.Level1)
155 {
156 StandbyService::GetInstance()->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, "");
157 StandbyService::GetInstance()->OnAddSystemAbility(TIME_SERVICE_ID, "");
158 StandbyService::GetInstance()->OnAddSystemAbility(ABILITY_MGR_SERVICE_ID, "");
159 StandbyService::GetInstance()->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, "");
160 StandbyService::GetInstance()->OnAddSystemAbility(POWER_MANAGER_SERVICE_ID, "");
161 StandbyService::GetInstance()->OnAddSystemAbility(APP_MGR_SERVICE_ID, "");
162 StandbyService::GetInstance()->OnAddSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, "");
163 StandbyService::GetInstance()->OnAddSystemAbility(POWER_MANAGER_SERVICE_ID + 1, "");
164 StandbyService::GetInstance()->OnRemoveSystemAbility(POWER_MANAGER_SERVICE_ID + 1, "");
165 SleepForFC();
166 StandbyService::GetInstance()->OnAddSystemAbility(POWER_MANAGER_SERVICE_ID + 1, "");
167 StandbyService::GetInstance()->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID, "");
168 StandbyService::GetInstance()->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, "");
169 StandbyService::GetInstance()->OnRemoveSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, "");
170 StandbyService::GetInstance()->OnRemoveSystemAbility(TIME_SERVICE_ID, "");
171 StandbyService::GetInstance()->OnRemoveSystemAbility(ABILITY_MGR_SERVICE_ID, "");
172 StandbyService::GetInstance()->OnRemoveSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, "");
173 StandbyService::GetInstance()->OnRemoveSystemAbility(POWER_MANAGER_SERVICE_ID, "");
174 StandbyService::GetInstance()->OnRemoveSystemAbility(POWER_MANAGER_SERVICE_ID + 1, "");
175 StandbyService::GetInstance()->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, "");
176 StandbyService::GetInstance()->OnAddSystemAbility(TIME_SERVICE_ID, "");
177 StandbyService::GetInstance()->OnAddSystemAbility(ABILITY_MGR_SERVICE_ID, "");
178 StandbyService::GetInstance()->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, "");
179 StandbyService::GetInstance()->OnAddSystemAbility(POWER_MANAGER_SERVICE_ID, "");
180 StandbyService::GetInstance()->OnAddSystemAbility(APP_MGR_SERVICE_ID, "");
181 StandbyService::GetInstance()->OnAddSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, "");
182 EXPECT_EQ(StandbyService::GetInstance()->dependsReady_, ALL_DEPENDS_READY);
183 StandbyServiceImpl::GetInstance()->InitReadyState();
184 }
185
186 /**
187 * @tc.name: StandbyServiceUnitTest_003
188 * @tc.desc: test state not start of StandbyService.
189 * @tc.type: FUNC
190 * @tc.require:
191 */
192 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_003, TestSize.Level1)
193 {
194 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
195 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
196 StandbyService::GetInstance()->SubscribeStandbyCallback(subscriber);
197 StandbyService::GetInstance()->UnsubscribeStandbyCallback(subscriber);
198
199 sptr<ResourceRequest> resourceRequest = new (std::nothrow) ResourceRequest();
200 EXPECT_NE(StandbyService::GetInstance()->ApplyAllowResource(resourceRequest), ERR_OK);
201 EXPECT_NE(StandbyService::GetInstance()->UnapplyAllowResource(resourceRequest), ERR_OK);
202
203 std::vector<AllowInfo> allowInfoList;
204 EXPECT_NE(StandbyService::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0), ERR_OK);
205 bool isStandby {false};
206 EXPECT_NE(StandbyService::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
207 StandbyService::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
208 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
209 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
210 StandbyService::GetInstance()->SubscribeStandbyCallback(subscriber);
211 StandbyService::GetInstance()->UnsubscribeStandbyCallback(subscriber);
212 StandbyService::GetInstance()->ApplyAllowResource(resourceRequest);
213 StandbyService::GetInstance()->UnapplyAllowResource(resourceRequest);
214 EXPECT_EQ(StandbyService::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0), ERR_OK);
215 EXPECT_EQ(StandbyService::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
216 StandbyService::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
217 }
218
219 /**
220 * @tc.name: StandbyServiceUnitTest_004
221 * @tc.desc: test unready not start of StandbyServiceImpl.
222 * @tc.type: FUNC
223 * @tc.require:
224 */
225 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_004, TestSize.Level1)
226 {
227 StandbyServiceImpl::GetInstance()->isServiceReady_ = false;
228 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
229 SleepForFC();
230 MockStartTimer(false);
231 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
232 SleepForFC();
233 MockStartTimer(true);
234 StandbyServiceImpl::GetInstance()->RemoveAppAllowRecord(DEFAULT_UID, DEFAULT_BUNDLENAME, true);
235 sptr<ResourceRequest> resourceRequest = new (std::nothrow) ResourceRequest();
236 StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
237 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
238 std::vector<AllowInfo> allowInfoList;
239 StandbyServiceImpl::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0);
240 bool isStandby {false};
241 EXPECT_NE(StandbyServiceImpl::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
242 std::vector<std::string> argsInStr {};
243 std::string result;
244 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
245 StandbyServiceImpl::GetInstance()->OnProcessStatusChanged(-1, -1, "", true);
246 StandbyServiceImpl::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
247 StandbyServiceImpl::GetInstance()->isServiceReady_ = true;
248 StandbyServiceImpl::GetInstance()->OnProcessStatusChanged(-1, -1, "", true);
249 StandbyServiceImpl::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
250 }
251
252 /**
253 * @tc.name: StandbyServiceUnitTest_005
254 * @tc.desc: test shelldump not start of StandbyService.
255 * @tc.type: FUNC
256 * @tc.require:
257 */
258 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_005, TestSize.Level1)
259 {
260 std::vector<std::u16string> args {};
261 std::vector<std::string> argsInStr {};
262 std::string result;
263 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
264 StandbyServiceImpl::GetInstance()->ShellDumpInner({}, result);
265 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-h"}, result);
266 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-v"}, result);
267 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D"}, result);
268 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D", "--config"}, result);
269 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D", "--conf"}, result);
270 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-E"}, result);
271 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-E", "0", "false"}, result);
272 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-E", "0", "true"}, result);
273 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A"}, result);
274 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--apply"}, result);
275 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--unapply"}, result);
276 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get"}, result);
277 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--apply", "0", "test", "127", "100", "0"}, result);
278 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--apply", "0", "test", "127", "100", "1"}, result);
279 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--unapply", "0", "test", "127", "100", "0"}, result);
280 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--unapply", "0", "test", "127", "100", "1"}, result);
281 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "true"}, result);
282 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "false"}, result);
283 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S"}, result);
284 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S", "--repeat"}, result);
285 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S", "--motion"}, result);
286 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S", "--default"}, result);
287 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-O"}, result);
288 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T"}, result);
289 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "on"}, result);
290 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "off"}, result);
291 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "debug", "on"}, result);
292 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-C", "dark_timeout", "60"}, result);
293 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "on"}, result);
294 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "off"}, result);
295 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "debug", "off"}, result);
296 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-C"}, result);
297 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-C", "dark_timeout", "60"}, result);
298 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D"}, result);
299 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "false", "true"}, result);
300
301 auto allowRecord = std::make_shared<AllowRecord>(0, 0, "name", AllowType::NETWORK);
302 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
303 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
304 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D"}, result);
305 SleepForFC();
306 EXPECT_NE(StandbyService::GetInstance()->Dump(-1, args), ERR_OK);
307 }
308
309 /**
310 * @tc.name: StandbyServiceUnitTest_006
311 * @tc.desc: test init of StandbyService.
312 * @tc.type: FUNC
313 * @tc.require:
314 */
315 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_006, TestSize.Level1)
316 {
317 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("test_standby.z.so"), ERR_OK);
318 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("libstandby_utils_policy.z.so"), ERR_OK);
319 }
320
321 /**
322 * @tc.name: StandbyServiceUnitTest_007
323 * @tc.desc: test init of StandbyService.
324 * @tc.type: FUNC
325 * @tc.require:
326 */
327 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_007, TestSize.Level1)
328 {
329 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("test_standby.z.so"), ERR_OK);
330 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("libstandby_utils_policy.z.so"), ERR_OK);
331 }
332
333 /**
334 * @tc.name: StandbyServiceUnitTest_008
335 * @tc.desc: test ParsePersistentData of StandbyService.
336 * @tc.type: FUNC
337 * @tc.require:
338 */
339 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_008, TestSize.Level1)
340 {
341 StandbyServiceImpl::GetInstance()->ParsePersistentData();
342 auto allowRecord = std::make_shared<AllowRecord>(0, 0, "name", AllowType::NETWORK);
343 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
344 allowRecord = std::make_shared<AllowRecord>(-1, -1, "test", AllowType::NETWORK);
345 allowRecord->allowTimeList_.emplace_back(AllowTime{-1, INT64_MAX, "test"});
346 allowRecord = std::make_shared<AllowRecord>(-1, -1, "test", AllowType::NETWORK);
347 allowRecord->allowTimeList_.emplace_back(AllowTime{-1, INT64_MAX, "test"});
348 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
349 StandbyServiceImpl::GetInstance()->DumpPersistantData();
350 StandbyServiceImpl::GetInstance()->ParsePersistentData();
351 StandbyServiceImpl::GetInstance()->RecoverTimeLimitedTask();
352 allowRecord->allowTimeList_.clear();
353 StandbyServiceImpl::GetInstance()->DumpPersistantData();
354 StandbyServiceImpl::GetInstance()->ParsePersistentData();
355 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->allowInfoMap_.empty());
356 MockGetAllRunningProcesses(false);
357 StandbyServiceImpl::GetInstance()->ParsePersistentData();
358 MockGetAllRunningProcesses(true);
359
360 auto emptyRecord = std::make_shared<AllowRecord>(0, 0, "name", 0);
361 emptyRecord->allowTimeList_.emplace_back(AllowTime{0, 0, "reason"});
362 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, emptyRecord);
363 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(0, "test", 0, true);
364 emptyRecord->allowTimeList_.emplace_back(AllowTime{1, 0, "reason"});
365 emptyRecord->allowTimeList_.emplace_back(AllowTime{2, 0, "reason"});
366 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(0, "test", AllowType::NETWORK, true);
367 }
368
369 /**
370 * @tc.name: StandbyServiceUnitTest_009
371 * @tc.desc: test ResetTimeObserver of StandbyService.
372 * @tc.type: FUNC
373 * @tc.require:
374 */
375 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_009, TestSize.Level1)
376 {
377 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
378 MockStartTimer(false);
379 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
380 MockStartTimer(true);
381 EXPECT_NE(StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_, 0);
382 }
383
384 /**
385 * @tc.name: StandbyServiceUnitTest_010
386 * @tc.desc: test UnInit of StandbyService.
387 * @tc.type: FUNC
388 * @tc.require:
389 */
390 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_010, TestSize.Level1)
391 {
392 StandbyServiceImpl::GetInstance()->registerPlugin_ = nullptr;
393 StandbyServiceImpl::GetInstance()->UninitReadyState();
394 StandbyServiceImpl::GetInstance()->UnInit();
395 StandbyServiceImpl::GetInstance()->RegisterPlugin(StandbyConfigManager::GetInstance()
396 ->GetPluginName());
397 StandbyServiceImpl::GetInstance()->InitReadyState();
398 SleepForFC();
399 EXPECT_NE(StandbyServiceImpl::GetInstance()->registerPlugin_, nullptr);
400 }
401
402 /**
403 * @tc.name: StandbyServiceUnitTest_011
404 * @tc.desc: test RemoveAppAllowRecord of StandbyService.
405 * @tc.type: FUNC
406 * @tc.require:
407 */
408 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_011, TestSize.Level1)
409 {
410 StandbyServiceImpl::GetInstance()->RemoveAppAllowRecord(DEFAULT_UID, DEFAULT_BUNDLENAME, true);
411 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
412 }
413
414 /**
415 * @tc.name: StandbyServiceUnitTest_012
416 * @tc.desc: test CheckCallerPermission of StandbyService.
417 * @tc.type: FUNC
418 * @tc.require:
419 */
420 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_012, TestSize.Level1)
421 {
422 StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_NATIVE_API);
423 EXPECT_EQ(StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_APP_API), ERR_OK);
424 MockGetTokenTypeFlag(false);
425 StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_NATIVE_API);
426 StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_APP_API);
427 MockGetTokenTypeFlag(true);
428 Security::AccessToken::AccessTokenID tokenId {};
429 StandbyServiceImpl::GetInstance()->IsSystemAppWithPermission(-1, tokenId, ReasonCodeEnum::REASON_APP_API);
430 StandbyServiceImpl::GetInstance()->IsSystemAppWithPermission(-1, tokenId, ReasonCodeEnum::REASON_NATIVE_API);
431 }
432
433 /**
434 * @tc.name: StandbyServiceUnitTest_013
435 * @tc.desc: test ApplyAllowResource of StandbyService.
436 * @tc.type: FUNC
437 * @tc.require:
438 */
439 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_013, TestSize.Level1)
440 {
441 sptr<ResourceRequest> resourceRequest = new (std::nothrow) ResourceRequest();
442 StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
443 SleepForFC();
444 StandbyServiceImpl::GetInstance()->ApplyAllowResInner(resourceRequest, -1);
445 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
446 }
447
448 /**
449 * @tc.name: StandbyServiceUnitTest_014
450 * @tc.desc: test UpdateRecord of StandbyService.
451 * @tc.type: FUNC
452 * @tc.require:
453 */
454 HWTEST_F(StandbyServiceUnitTest, UpdateRecord_014, TestSize.Level1)
455 {
456 std::shared_ptr<AllowRecord> allowRecord = std::make_shared<AllowRecord>();
457 sptr<ResourceRequest> resourceRequest = new (std::nothrow) ResourceRequest();
458 StandbyServiceImpl::GetInstance()->UpdateRecord(allowRecord, resourceRequest);
459 SleepForFC();
460 StandbyServiceImpl::GetInstance()->UpdateRecord(allowRecord, resourceRequest);
461 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
462 }
463
464 /**
465 * @tc.name: StandbyServiceUnitTest_015
466 * @tc.desc: test UpdateRecord of StandbyService.
467 * @tc.type: FUNC
468 * @tc.require:
469 */
470 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_015, TestSize.Level1)
471 {
472 std::shared_ptr<AllowRecord> allowRecord = std::make_shared<AllowRecord>();
473 sptr<ResourceRequest> resourceRequest = new (std::nothrow) ResourceRequest(MAX_ALLOW_TYPE_NUMBER, DEFAULT_UID,
474 DEFAULT_BUNDLENAME, 10, "reason", ReasonCodeEnum::REASON_APP_API);
475 StandbyServiceImpl::GetInstance()->UpdateRecord(allowRecord, resourceRequest);
476 SleepForFC();
477 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
478 }
479
480 /**
481 * @tc.name: StandbyServiceUnitTest_016
482 * @tc.desc: test DayNightSwitchCallback of StandbyService.
483 * @tc.type: FUNC
484 * @tc.require:
485 */
486 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_016, TestSize.Level1)
487 {
488 StandbyServiceImpl::GetInstance()->standbyStateManager_->TransitToStateInner(StandbyState::WORKING);
489 SleepForFC();
490 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
491 SleepForFC();
492 StandbyServiceImpl::GetInstance()->standbyStateManager_->TransitToStateInner(StandbyState::SLEEP);
493 SleepForFC();
494 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
495 SleepForFC();
496 MockStartTimer(false);
497 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
498 SleepForFC();
499 MockStartTimer(true);
500 StandbyServiceImpl::GetInstance()->UnregisterTimeObserver();
501 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
502 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
503 SleepForFC();
504 EXPECT_NE(StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_, 0);
505 MockStartTimer(false);
506 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
507 MockStartTimer(true);
508 SleepForFC();
509 }
510
511 /**
512 * @tc.name: StandbyServiceUnitTest_017
513 * @tc.desc: test DayNightSwitchCallback of StandbyService.
514 * @tc.type: FUNC
515 * @tc.require:
516 */
517 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_017, TestSize.Level1)
518 {
519 auto allowRecord = std::make_shared<AllowRecord>(DEFAULT_UID, 0, DEFAULT_BUNDLENAME, AllowType::NETWORK);
520 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
521 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
522
523 std::vector<AllowInfo> allowInfoList;
524 StandbyServiceImpl::GetInstance()->GetAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
525 ReasonCodeEnum::REASON_APP_API);
526 StandbyServiceImpl::GetInstance()->GetAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
527 ReasonCodeEnum::REASON_NATIVE_API);
528 std::string result {""};
529 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "true"}, result);
530 StandbyServiceImpl::GetInstance()->GetAllowListInner(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
531 ReasonCodeEnum::REASON_APP_API);
532 StandbyServiceImpl::GetInstance()->GetAllowListInner(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
533 ReasonCodeEnum::REASON_NATIVE_API);
534 StandbyServiceImpl::GetInstance()->GetAllowListInner(0, allowInfoList,
535 ReasonCodeEnum::REASON_NATIVE_API);
536 StandbyServiceImpl::GetInstance()->GetTemporaryAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
537 ReasonCodeEnum::REASON_APP_API);
538 StandbyServiceImpl::GetInstance()->GetTemporaryAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
539 ReasonCodeEnum::REASON_NATIVE_API);
540 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
541 true, true);
542 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
543 true, false);
544 sptr<ResourceRequest> resourceRequest = new (std::nothrow) ResourceRequest();
545 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
546 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
547 SleepForFC();
548 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(DEFAULT_UID, DEFAULT_BUNDLENAME, 1, false);
549 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(DEFAULT_UID, DEFAULT_BUNDLENAME, 1, true);
550 allowRecord = std::make_shared<AllowRecord>(0, 0, "name", MAX_ALLOW_TYPE_NUMBER);
551 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
552 allowRecord->allowTimeList_.emplace_back(AllowTime{1, INT64_MAX, "reason"});
553 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
554 StandbyServiceImpl::GetInstance()->GetTemporaryAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
555 ReasonCodeEnum::REASON_NATIVE_API);
556 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
557 true, true);
558 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
559 false, true);
560 StandbyServiceImpl::GetInstance()->allowInfoMap_.clear();
561 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
562 }
563
564 /**
565 * @tc.name: StandbyServiceUnitTest_018
566 * @tc.desc: test AddSubscriber of StandbyService.
567 * @tc.type: FUNC
568 * @tc.require:
569 */
570 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_018, TestSize.Level1)
571 {
572 std::vector<std::string> argsInStr {};
573 std::string result {};
574 StandbyStateSubscriber::GetInstance()->ShellDump(argsInStr, result);
575 StandbyStateSubscriber::GetInstance()->NotifyIdleModeByCallback(false, false);
576 StandbyStateSubscriber::GetInstance()->ReportAllowListChanged(DEFAULT_UID, DEFAULT_BUNDLENAME,
577 AllowType::NETWORK, true);
578 sptr<IStandbyServiceSubscriber> nullSubscriber = nullptr;
579 EXPECT_NE(StandbyStateSubscriber::GetInstance()->AddSubscriber(nullSubscriber), ERR_OK);
580 EXPECT_NE(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(nullSubscriber), ERR_OK);
581 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
582 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
583 StandbyStateSubscriber::GetInstance()->ShellDump(argsInStr, result);
584 EXPECT_NE(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
585 StandbyStateSubscriber::GetInstance()->NotifyIdleModeByCallback(false, false);
586 StandbyStateSubscriber::GetInstance()->ReportAllowListChanged(DEFAULT_UID, DEFAULT_BUNDLENAME,
587 AllowType::NETWORK, true);
588 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
589 StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber);
590 StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber);
591 auto remote = subscriber->AsObject();
592 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
593 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
594 StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber);
595
596 StandbyStateSubscriber::GetInstance()->deathRecipient_ = nullptr;
597 EXPECT_NE(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
598 EXPECT_NE(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
599 StandbyStateSubscriber::GetInstance()->deathRecipient_ = new (std::nothrow) SubscriberDeathRecipient();
600
601 remote = nullptr;
602 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
603 sptr<IRemoteObject> proxy {nullptr};
604 remote = proxy;
605 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
606 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
607 }
608
609 /**
610 * @tc.name: StandbyServiceUnitTest_019
611 * @tc.desc: test AddSubscriber of StandbyService.
612 * @tc.type: FUNC
613 * @tc.require:
614 */
615 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_019, TestSize.Level1)
616 {
617 auto allowRecord = std::make_shared<AllowRecord>(DEFAULT_UID, 0, DEFAULT_BUNDLENAME, AllowType::NETWORK);
618 auto value = allowRecord->ParseToJson();
619 allowRecord->ParseFromJson(value);
620 allowRecord->allowTimeList_.emplace_back(AllowTime{0, 0, "reason"});
621 value = allowRecord->ParseToJson();
622 nlohmann::json emptyValue {};
623 allowRecord->ParseFromJson(emptyValue);
624 allowRecord->ParseFromJson(value);
625 EXPECT_FALSE(allowRecord->allowTimeList_.empty());
626 }
627
628 /**
629 * @tc.name: StandbyServiceUnitTest_020
630 * @tc.desc: test TimeProvider of StandbyService.
631 * @tc.type: FUNC
632 * @tc.require:
633 */
634 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_020, TestSize.Level1)
635 {
636 int64_t curSecTimeStamp = MiscServices::TimeServiceClient::GetInstance()->
637 GetWallTimeMs() / TimeConstant::MSEC_PER_SEC;
638 struct tm curLocalTime {};
639 EXPECT_TRUE(TimeProvider::ConvertTimeStampToLocalTime(curSecTimeStamp, curLocalTime));
640 TimeProvider::GetCondition();
641 int64_t timeDiff {0};
642 TimeProvider::TimeDiffToDayNightSwitch(timeDiff);
643 TimeProvider::DiffToFixedClock(0, 0, 0, timeDiff);
644 TimeProvider::GetNapTimeOut();
645 }
646
647 /**
648 * @tc.name: StandbyServiceUnitTest_021
649 * @tc.desc: test TimedTask of StandbyService.
650 * @tc.type: FUNC
651 * @tc.require:
652 */
653 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_021, TestSize.Level1)
654 {
655 auto timedTask = std::make_shared<TimedTask>();
656 timedTask->OnTrigger();
__anonba85dd6a0202()657 auto callBack = [](){};
658 timedTask->SetCallbackInfo(callBack);
659 timedTask->OnTrigger();
660 EXPECT_NE(timedTask, nullptr);
661 }
662
663 /**
664 * @tc.name: StandbyServiceUnitTest_022
665 * @tc.desc: test observer of StandbyService.
666 * @tc.type: FUNC
667 * @tc.require:
668 */
669 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_022, TestSize.Level1)
670 {
671 std::list<SystemProcessInfo> systemProcessInfos {};
672 AbilityManagerHelper::GetInstance()->GetRunningSystemProcess(systemProcessInfos);
673 std::vector<AppExecFwk::RunningProcessInfo> allAppProcessInfos {};
674 AppMgrHelper::GetInstance()->GetAllRunningProcesses(allAppProcessInfos);
675 AppMgrHelper::GetInstance()->GetAllRunningProcesses(allAppProcessInfos);
676 AppMgrHelper::GetInstance()->appMgrProxy_ = nullptr;
677 AppMgrHelper::GetInstance()->GetAllRunningProcesses(allAppProcessInfos);
678
679 BundleManagerHelper::GetInstance()->GetClientBundleName(0);
680 BundleManagerHelper::GetInstance()->bundleMgr_ = nullptr;
681 BundleManagerHelper::GetInstance()->GetClientBundleName(0);
682 AppExecFwk::ApplicationInfo applicationInfo {};
683 BundleManagerHelper::GetInstance()->GetApplicationInfo(DEFAULT_BUNDLENAME,
684 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, 0, applicationInfo);
685 EXPECT_EQ(BundleManagerHelper::GetInstance()->bundleMgr_, nullptr);
686
687 EXPECT_NE(StandbyServiceImpl::GetInstance()->commonEventObserver_, nullptr);
688 EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
689 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
690 for (const auto& eventName : COMMON_EVENT_LIST) {
691 AAFwk::Want want = AAFwk::Want();
692 want.SetAction(eventName);
693 eventData.SetWant(want);
694 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
695 }
696 SleepForFC();
697 }
698
699 /**
700 * @tc.name: StandbyServiceUnitTest_023
701 * @tc.desc: test GetPidAndProcName of StandbyService.
702 * @tc.type: FUNC
703 * @tc.require:
704 */
705 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_023, TestSize.Level1)
706 {
707 std::unordered_map<int32_t, std::string> pidNameMap {};
708 StandbyServiceImpl::GetInstance()->GetPidAndProcName(pidNameMap);
709 EXPECT_NE(pidNameMap.size(), 0);
710 MockGetAllRunningProcesses(false);
711 StandbyServiceImpl::GetInstance()->GetPidAndProcName(pidNameMap);
712 MockGetAllRunningProcesses(true);
713 MockGetRunningSystemProcess(false);
714 StandbyServiceImpl::GetInstance()->GetPidAndProcName(pidNameMap);
715 MockGetRunningSystemProcess(true);
716 }
717
718 /**
719 * @tc.name: StandbyServiceUnitTest_024
720 * @tc.desc: test multithread OnReceiveEvent of StandbyService.
721 * @tc.type: FUNC
722 * @tc.require:
723 */
724 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_024, TestSize.Level1, 20)
725 {
726 EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
727 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
728 for (const auto& event : COMMON_EVENT_LIST) {
729 AAFwk::Want want = AAFwk::Want();
730 want.SetAction(event);
731 eventData.SetWant(want);
732 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
733 }
734 StandbyServiceUnitTest::SleepForFC();
735 EXPECT_NE(StandbyServiceImpl::GetInstance()->commonEventObserver_, nullptr);
736 MockSubscribeCommonEvent(false);
737 StandbyServiceImpl::GetInstance()->commonEventObserver_->Subscribe();
738 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->commonEventObserver_->Unsubscribe());
739 MockSubscribeCommonEvent(true);
740 StandbyServiceImpl::GetInstance()->commonEventObserver_->Subscribe();
741 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->commonEventObserver_->Unsubscribe());
742 }
743
744 /**
745 * @tc.name: StandbyServiceUnitTest_025
746 * @tc.desc: test multithread init of StandbyService.
747 * @tc.type: FUNC
748 * @tc.require:
749 */
750 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_025, TestSize.Level1, 20)
751 {
752 StandbyServiceImpl::GetInstance()->InitReadyState();
753 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
754 StandbyServiceUnitTest::SleepForFC();
755 }
756
757 /**
758 * @tc.name: StandbyServiceUnitTest_026
759 * @tc.desc: test multithread uninit of StandbyService.
760 * @tc.type: FUNC
761 * @tc.require:
762 */
763 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_026, TestSize.Level1, 20)
764 {
765 StandbyServiceImpl::GetInstance()->UninitReadyState();
766 EXPECT_FALSE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
767 StandbyServiceUnitTest::SleepForFC();
768 }
769
770 /**
771 * @tc.name: StandbyServiceUnitTest_027
772 * @tc.desc: test multithread DayNightSwitchCallback of StandbyService.
773 * @tc.type: FUNC
774 * @tc.require:
775 */
776 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_027, TestSize.Level1, 20)
777 {
778 StandbyServiceImpl::GetInstance()->InitReadyState();
779 StandbyServiceUnitTest::SleepForFC();
780 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
781 StandbyServiceUnitTest::SleepForFC();
782 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
783 }
784
785 /**
786 * @tc.name: StandbyServiceUnitTest_028
787 * @tc.desc: test multithread ApplyAllowResource of StandbyService.
788 * @tc.type: FUNC
789 * @tc.require:
790 */
791 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_028, TestSize.Level1, 20)
792 {
793 sptr<ResourceRequest> resourceRequest = new (std::nothrow) ResourceRequest();
794 StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
795 sptr<ResourceRequest> validResRequest = new (std::nothrow) ResourceRequest(AllowType::NETWORK,
796 0, "test_process", 100, "test", 1);
797 EXPECT_EQ(StandbyServiceImpl::GetInstance()->ApplyAllowResource(validResRequest), ERR_OK);
798 sptr<ResourceRequest> invalidResRequest = new (std::nothrow) ResourceRequest(AllowType::NETWORK,
799 -1, "test_process", 100, "test", 1);
800 StandbyServiceImpl::GetInstance()->ApplyAllowResource(invalidResRequest);
801 sptr<ResourceRequest> negResRequest = new (std::nothrow) ResourceRequest(AllowType::NETWORK,
802 0, "test_process", -1, "test", 1);
803 StandbyServiceImpl::GetInstance()->ApplyAllowResource(negResRequest);
804 StandbyServiceUnitTest::SleepForFC();
805 }
806
807 /**
808 * @tc.name: StandbyServiceUnitTest_029
809 * @tc.desc: test multithread UnapplyAllowResource of StandbyService.
810 * @tc.type: FUNC
811 * @tc.require:
812 */
813 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_029, TestSize.Level1, 20)
814 {
815 sptr<ResourceRequest> resourceRequest = new (std::nothrow) ResourceRequest();
816 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
817 sptr<ResourceRequest> validResRequest = new (std::nothrow) ResourceRequest(AllowType::NETWORK,
818 0, "test_process", 100, "test", 1);
819 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnapplyAllowResource(validResRequest), ERR_OK);
820 sptr<ResourceRequest> invalidResRequest = new (std::nothrow) ResourceRequest(AllowType::NETWORK,
821 -1, "test_process", 100, "test", 1);
822 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(invalidResRequest);
823 }
824
825 /**
826 * @tc.name: StandbyServiceUnitTest_030
827 * @tc.desc: test multithread GetAllowList of StandbyService.
828 * @tc.type: FUNC
829 * @tc.require:
830 */
831 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_030, TestSize.Level1, 20)
832 {
833 std::vector<AllowInfo> allowInfoList;
834 StandbyServiceImpl::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0);
835 EXPECT_EQ(allowInfoList.size(), 0);
836 }
837
838 /**
839 * @tc.name: StandbyServiceUnitTest_031
840 * @tc.desc: test multithread IsDeviceInStandby of StandbyService.
841 * @tc.type: FUNC
842 * @tc.require:
843 */
844 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_031, TestSize.Level1, 20)
845 {
846 bool isStandby {false};
847 EXPECT_EQ(StandbyServiceImpl::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
848 }
849
850 /**
851 * @tc.name: StandbyServiceUnitTest_032
852 * @tc.desc: test multithread ShellDump of StandbyService.
853 * @tc.type: FUNC
854 * @tc.require:
855 */
856 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_032, TestSize.Level1, 20)
857 {
858 std::vector<std::string> argsInStr {};
859 std::string result;
860 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
861 EXPECT_NE(result.size(), 0);
862 }
863
864 /**
865 * @tc.name: StandbyServiceUnitTest_033
866 * @tc.desc: test RegisterCommEventObserver of StandbyService.
867 * @tc.type: FUNC
868 * @tc.require:
869 */
870 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_033, TestSize.Level1, 20)
871 {
872 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
873 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnregisterCommEventObserver(), ERR_OK);
874 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
875 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
876 MockSubscribeCommonEvent(false);
877 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
878 StandbyServiceImpl::GetInstance()->UnregisterCommEventObserver();
879 MockSubscribeCommonEvent(true);
880 }
881
882 /**
883 * @tc.name: StandbyServiceUnitTest_034
884 * @tc.desc: test RegisterCommEventObserver of StandbyService.
885 * @tc.type: FUNC
886 * @tc.require:
887 */
888 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_034, TestSize.Level1, 20)
889 {
890 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
891 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnregisterTimeObserver(), ERR_OK);
892 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
893 StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_ = 1;
894 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
895 StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_ = 0;
896 MockSubscribeCommonEvent(false);
897 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
898 MockStartTimer(false);
899 StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_ = 0;
900 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
901 MockStartTimer(true);
902 MockSubscribeCommonEvent(true);
903 }
904
905 /**
906 * @tc.name: StandbyServiceUnitTest_035
907 * @tc.desc: test AddSubscriber of StandbyService.
908 * @tc.type: FUNC
909 * @tc.require:
910 */
911 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_035, TestSize.Level1, 20)
912 {
913 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
914 StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber);
915 StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber);
916 MockPublishCommonEvent(true);
917 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::WORKING);
918 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::NAP);
919 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::SLEEP);
920 MockPublishCommonEvent(false);
921 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::WORKING);
922 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::NAP);
923 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::SLEEP);
924 MockPublishCommonEvent(true);
925 EXPECT_TRUE(true);
926 }
927
928 /**
929 * @tc.name: StandbyServiceUnitTest_036
930 * @tc.desc: test OnRemoteRequestInner of StandbyStateSubscriber.
931 * @tc.type: FUNC
932 * @tc.require:
933 */
934 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_036, TestSize.Level1, 20)
935 {
936 sptr<StandbyServiceSubscriberStub> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
937 MessageParcel data {};
938 MessageParcel reply {};
939 MessageOption option {MessageOption::TF_ASYNC};
940 data.WriteInterfaceToken(IStandbyServiceSubscriber::GetDescriptor());
941 subscriber->OnRemoteRequest(
942 (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_DEVICE_IDLE_MODE)), data, reply, option);
943 subscriber->OnRemoteRequest(
944 (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED)), data, reply, option);
945 auto ret = subscriber->OnRemoteRequest(
946 static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED) + 1, data, reply, option);
947 EXPECT_NE(ret, ERR_OK);
948 }
949
950 /**
951 * @tc.name: StandbyServiceUnitTest_037
952 * @tc.desc: test OnRemoteRequestInner of StandbyService.
953 * @tc.type: FUNC
954 * @tc.require:
955 */
956 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_037, TestSize.Level1, 20)
957 {
958 MessageParcel data;
959 MessageParcel reply;
960 MessageOption option = {MessageOption::TF_ASYNC};
961 data.WriteInterfaceToken(IStandbyService::GetDescriptor());
962 StandbyService::GetInstance()->OnRemoteRequest(StandbyServiceStub::SUBSCRIBE_STANDBY_CALLBACK, data, reply, option);
963 StandbyService::GetInstance()->OnRemoteRequest(StandbyServiceStub::UNSUBSCRIBE_STANDBY_CALLBACK,
964 data, reply, option);
965 StandbyService::GetInstance()->OnRemoteRequest(StandbyServiceStub::APPLY_ALLOW_RESOURCE, data, reply, option);
966 StandbyService::GetInstance()->OnRemoteRequest(StandbyServiceStub::UNAPPLY_ALLOW_RESOURCE, data, reply, option);
967 StandbyService::GetInstance()->OnRemoteRequest(StandbyServiceStub::GET_ALLOW_LIST, data, reply, option);
968 StandbyService::GetInstance()->OnRemoteRequest(StandbyServiceStub::IS_DEVICE_IN_STANDBY, data, reply, option);
969 StandbyService::GetInstance()->OnRemoteRequest(
970 StandbyServiceStub::REPORT_WORK_SCHEDULER_STATUS, data, reply, option);
971 auto ret = StandbyService::GetInstance()->OnRemoteRequest(StandbyServiceStub::REPORT_WORK_SCHEDULER_STATUS + 1,
972 data, reply, option);
973 StandbyService::GetInstance()->OnRemoteRequest(
974 (static_cast<uint32_t>(IStandbyInterfaceCode::APPLY_ALLOW_RESOURCE)), data, reply, option);
975 StandbyService::GetInstance()->OnRemoteRequest(
976 (static_cast<uint32_t>(IStandbyInterfaceCode::UNAPPLY_ALLOW_RESOURCE)), data, reply, option);
977 StandbyService::GetInstance()->OnRemoteRequest(
978 (static_cast<uint32_t>(IStandbyInterfaceCode::GET_ALLOW_LIST)), data, reply, option);
979 StandbyService::GetInstance()->OnRemoteRequest(
980 (static_cast<uint32_t>(IStandbyInterfaceCode::IS_DEVICE_IN_STANDBY)), data, reply, option);
981 ret = StandbyService::GetInstance()->OnRemoteRequest(
982 (static_cast<uint32_t>(IStandbyInterfaceCode::IS_DEVICE_IN_STANDBY)) + 1, data, reply, option);
983 EXPECT_NE(ret, ERR_OK);
984 }
985
986 /**
987 * @tc.name: StandbyServiceUnitTest_038
988 * @tc.desc: test TimedTask.
989 * @tc.type: FUNC
990 * @tc.require:
991 */
992 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_038, TestSize.Level1)
993 {
994 auto timedTask = std::make_shared<TimedTask>(false, 0, true);
995 timedTask = std::make_shared<TimedTask>(false, 0, false);
996 uint64_t timerId {0};
997 timedTask->StartDayNightSwitchTimer(timerId);
998 std::function<void()> callBack {};
999 MockStartTimer(false);
1000 uint64_t zeroTimeId {0};
1001 uint64_t negativeTimeId {-1};
1002 EXPECT_TRUE(timedTask->RegisterDayNightSwitchTimer(zeroTimeId, false, 0, callBack));
1003 EXPECT_TRUE(timedTask->RegisterDayNightSwitchTimer(negativeTimeId, false, 0, callBack));
1004 timedTask->StartDayNightSwitchTimer(timerId);
1005 TimedTask::CreateTimer(false, 0, false, false, callBack);
1006 StandbyServiceImpl::GetInstance()->UnregisterTimeObserver();
1007 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
1008 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
1009 StandbyServiceUnitTest::SleepForFC();
1010 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
1011 StandbyServiceUnitTest::SleepForFC();
1012 MockStartTimer(true);
1013 timedTask->RegisterDayNightSwitchTimer(zeroTimeId, false, 0, callBack);
1014 timedTask->RegisterDayNightSwitchTimer(negativeTimeId, false, 0, callBack);
1015 }
1016
1017 /**
1018 * @tc.name: StandbyServiceUnitTest_039
1019 * @tc.desc: test CheckNativePermission of StandbyService.
1020 * @tc.type: FUNC
1021 * @tc.require:
1022 */
1023 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_039, TestSize.Level1)
1024 {
1025 Security::AccessToken::AccessTokenID tokenId {};
1026 EXPECT_EQ(StandbyServiceImpl::GetInstance()->CheckNativePermission(tokenId), ERR_OK);
1027 MockGetTokenTypeFlag(false);
1028 StandbyServiceImpl::GetInstance()->CheckNativePermission(tokenId);
1029 MockGetTokenTypeFlag(true);
1030 }
1031
1032 /**
1033 * @tc.name: StandbyServiceUnitTest_040
1034 * @tc.desc: test OnRemoteRequestInner of StandbyService.
1035 * @tc.type: FUNC
1036 * @tc.require:
1037 */
1038 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_040, TestSize.Level1)
1039 {
1040 MessageParcel data;
1041 MessageParcel reply;
1042 MessageOption option = {MessageOption::TF_ASYNC};
1043 data.WriteInterfaceToken(IStandbyService::GetDescriptor());
1044 StandbyService::GetInstance()->HandleApplyAllowResource(data, reply);
1045 EXPECT_NE(StandbyService::GetInstance()->HandleUnapplyAllowResource(data, reply), ERR_OK);
1046
1047 MessageParcel workSchedulerData;
1048 workSchedulerData.WriteBool(false);
1049 workSchedulerData.WriteInt32(-1);
1050 workSchedulerData.WriteString("");
1051 StandbyService::GetInstance()->HandleReportWorkSchedulerStatus(workSchedulerData, reply);
1052 }
1053
1054 /**
1055 * @tc.name: StandbyServiceUnitTest_041
1056 * @tc.desc: test RegisterAppStateObserver of StandbyServiceImpl.
1057 * @tc.type: FUNC
1058 * @tc.require:
1059 */
1060 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_041, TestSize.Level1)
1061 {
1062 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
1063 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
1064 EXPECT_EQ(StandbyServiceImpl::GetInstance()->appStateObserver_, nullptr);
1065 MockSubscribeObserver(true);
1066 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
1067 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
1068 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
1069 MockSubscribeObserver(false);
1070 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
1071 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
1072 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
1073 }
1074
1075 /**
1076 * @tc.name: StandbyServiceUnitTest_042
1077 * @tc.desc: test AppStateObserver.
1078 * @tc.type: FUNC
1079 * @tc.require:
1080 */
1081 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_042, TestSize.Level1)
1082 {
1083 auto appStateObserver = std::make_shared<AppStateObserver>(StandbyServiceImpl::GetInstance()->handler_);
1084 AppExecFwk::ProcessData processData {};
1085 appStateObserver->OnProcessDied(processData);
1086 processData.bundleName = "com.ohos.systemui";
1087 appStateObserver->OnProcessDied(processData);
1088
1089 AppExecFwk::AppStateData appStateData {};
1090 appStateData.uid = -1;
1091 appStateObserver->OnApplicationStateChanged(appStateData);
1092 appStateData.uid = SAMPLE_APP_UID;
1093 appStateObserver->OnApplicationStateChanged(appStateData);
1094
1095 appStateData.bundleName = SAMPLE_BUNDLE_NAME;
1096 appStateObserver->OnApplicationStateChanged(appStateData);
1097
1098 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_TERMINATED);
1099 appStateObserver->OnApplicationStateChanged(appStateData);
1100 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_END);
1101 appStateObserver->OnApplicationStateChanged(appStateData);
1102 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOCUS);
1103 appStateObserver->OnApplicationStateChanged(appStateData);
1104 SleepForFC();
1105 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->allowInfoMap_.empty());
1106 }
1107
1108 /**
1109 * @tc.name: StandbyServiceUnitTest_043
1110 * @tc.desc: test GetRestrictList.
1111 * @tc.type: FUNC
1112 * @tc.require:
1113 */
1114 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_043, TestSize.Level1)
1115 {
1116 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1117 uint32_t restrictType = 1;
1118 std::vector<AllowInfo> restrictInfoList;
1119 uint32_t reasonCode = 1;
1120 EXPECT_EQ(StandbyService::GetInstance()->GetRestrictList(restrictType, restrictInfoList, reasonCode), ERR_OK);
1121
1122 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1123 EXPECT_EQ(StandbyService::GetInstance()->
1124 GetRestrictList(restrictType, restrictInfoList, reasonCode), ERR_STANDBY_SYS_NOT_READY);
1125 }
1126
1127 /**
1128 * @tc.name: StandbyServiceUnitTest_044
1129 * @tc.desc: test IsStrategyEnabled.
1130 * @tc.type: FUNC
1131 * @tc.require:
1132 */
1133 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_044, TestSize.Level1)
1134 {
1135 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1136 std::string strategyName;
1137 bool isEnabled = false;
1138 EXPECT_EQ(StandbyService::GetInstance()->IsStrategyEnabled(strategyName, isEnabled), ERR_OK);
1139
1140 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1141 EXPECT_EQ(StandbyService::GetInstance()->IsStrategyEnabled(strategyName, isEnabled), ERR_STANDBY_SYS_NOT_READY);
1142 }
1143
1144 /**
1145 * @tc.name: StandbyServiceUnitTest_045
1146 * @tc.desc: test ReportDeviceStateChanged.
1147 * @tc.type: FUNC
1148 * @tc.require:
1149 */
1150 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_045, TestSize.Level1)
1151 {
1152 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1153 DeviceStateType type = DeviceStateType::DIS_COMP_CHANGE;
1154 bool enabled = true;
1155 EXPECT_EQ(StandbyService::GetInstance()->ReportDeviceStateChanged(type, enabled), ERR_OK);
1156
1157 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1158 EXPECT_EQ(StandbyService::GetInstance()->ReportDeviceStateChanged(type, enabled), ERR_STANDBY_SYS_NOT_READY);
1159 }
1160
1161 /**
1162 * @tc.name: StandbyServiceUnitTest_046
1163 * @tc.desc: test ReportDeviceStateChanged.
1164 * @tc.type: FUNC
1165 * @tc.require:
1166 */
1167 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_046, TestSize.Level1)
1168 {
1169 int32_t type = -1;
1170 DeviceStateCache::GetInstance()->deviceState_ = {true, true, false};
1171 EXPECT_EQ(DeviceStateCache::GetInstance()->GetDeviceState(type), false);
1172
1173 type = DeviceStateCache::DEVICE_STATE_NUM;
1174 EXPECT_EQ(DeviceStateCache::GetInstance()->GetDeviceState(type), false);
1175
1176 type = 1;
1177 EXPECT_EQ(DeviceStateCache::GetInstance()->GetDeviceState(type), true);
1178 }
1179
1180 /**
1181 * @tc.name: StandbyServiceUnitTest_047
1182 * @tc.desc: test ReportDeviceStateChanged.
1183 * @tc.type: FUNC
1184 * @tc.require:
1185 */
1186 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_047, TestSize.Level1)
1187 {
1188 int32_t type = -1;
1189 bool enabled = true;
1190 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), false);
1191
1192 type = DeviceStateCache::DEVICE_STATE_NUM;
1193 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), false);
1194
1195 type = 1;
1196 DeviceStateCache::GetInstance()->deviceState_ = {true, true, false};
1197 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), false);
1198
1199 type = 2;
1200 DeviceStateCache::GetInstance()->deviceState_ = {true, true, false};
1201 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), true);
1202 }
1203 } // namespace DevStandbyMgr
1204 } // namespace OHOS
1205