1 /*
2 * Copyright (c) 2022-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
16 #include "gtest/gtest.h"
17 #include "gtest/hwext/gtest-multithread.h"
18
19 #include <vector>
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include "nativetoken_kit.h"
23 #include "notifier_mgr.h"
24 #include "plugin_mgr.h"
25 #include "res_common_util.h"
26 #include "res_sched_ipc_interface_code.h"
27 #include "res_sched_common_death_recipient.h"
28 #include "res_sched_service.h"
29 #include "res_sched_service_ability.h"
30 #include "res_sched_systemload_notifier_proxy.h"
31 #include "res_sched_systemload_notifier_stub.h"
32 #include "res_type.h"
33 #include "token_setproc.h"
34
35 namespace OHOS {
36 namespace ResourceSchedule {
37 using namespace std;
38 using namespace testing::ext;
39 using namespace testing::mt;
40 using namespace Security::AccessToken;
41 class ResSchedServiceTest : public testing::Test {
42 public:
43 static void SetUpTestCase(void);
44 static void TearDownTestCase(void);
45 void SetUp();
46 void TearDown();
47 protected:
48 std::shared_ptr<ResSchedService> resSchedService_ = nullptr;
49 std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = nullptr;
50 };
51
52 class TestResSchedSystemloadListener : public ResSchedSystemloadNotifierStub {
53 public:
54 TestResSchedSystemloadListener() = default;
55
OnSystemloadLevel(int32_t level)56 void OnSystemloadLevel(int32_t level)
57 {
58 testSystemloadLevel = level;
59 }
60
61 static int32_t testSystemloadLevel;
62 };
63
64 int32_t TestResSchedSystemloadListener::testSystemloadLevel = 0;
65
SetUpTestCase(void)66 void ResSchedServiceTest::SetUpTestCase(void)
67 {
68 static const char *perms[] = {
69 "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT",
70 "ohos.permission.DUMP",
71 };
72 uint64_t tokenId;
73 NativeTokenInfoParams infoInstance = {
74 .dcapsNum = 0,
75 .permsNum = 2,
76 .aclsNum = 0,
77 .dcaps = nullptr,
78 .perms = perms,
79 .acls = nullptr,
80 .processName = "ResSchedServiceTest",
81 .aplStr = "system_core",
82 };
83 tokenId = GetAccessTokenId(&infoInstance);
84 SetSelfTokenID(tokenId);
85 AccessTokenKit::ReloadNativeTokenInfo();
86 }
87
TearDownTestCase()88 void ResSchedServiceTest::TearDownTestCase() {}
89
SetUp()90 void ResSchedServiceTest::SetUp()
91 {
92 /**
93 * @tc.setup: initialize the member variable resSchedServiceAbility_
94 */
95 resSchedService_ = make_shared<ResSchedService>();
96 resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
97 }
98
TearDown()99 void ResSchedServiceTest::TearDown()
100 {
101 /**
102 * @tc.teardown: clear resSchedServiceAbility_
103 */
104 resSchedService_ = nullptr;
105 resSchedServiceAbility_ = nullptr;
106 }
107
108 /**
109 * @tc.name: ressched service dump 001
110 * @tc.desc: Verify if ressched service dump commonds is success.
111 * @tc.type: FUNC
112 * @tc.require: issueI5WWV3
113 * @tc.author:lice
114 */
115 HWTEST_F(ResSchedServiceTest, ServiceDump001, Function | MediumTest | Level0)
116 {
117 PluginMgr::GetInstance().Init();
118 std::string result;
119 resSchedService_->DumpAllInfo(result);
120 EXPECT_TRUE(!result.empty());
121
122 result = "";
123 resSchedService_->DumpUsage(result);
124 EXPECT_TRUE(!result.empty());
125
126 int32_t wrongFd = -1;
127 std::vector<std::u16string> argsNull;
128 int res = resSchedService_->Dump(wrongFd, argsNull);
129 EXPECT_NE(res, ERR_OK);
130
131 int32_t correctFd = -1;
132 res = resSchedService_->Dump(correctFd, argsNull);
133
134 std::vector<std::u16string> argsHelp = {to_utf16("-h")};
135 res = resSchedService_->Dump(correctFd, argsHelp);
136
137 std::vector<std::u16string> argsAll = {to_utf16("-a")};
138 res = resSchedService_->Dump(correctFd, argsAll);
139
140 std::vector<std::u16string> argsError = {to_utf16("-e")};
141 res = resSchedService_->Dump(correctFd, argsError);
142
143 std::vector<std::u16string> argsPlugin = {to_utf16("-p")};
144 res = resSchedService_->Dump(correctFd, argsPlugin);
145
146 std::vector<std::u16string> argsOnePlugin = {to_utf16("-p"), to_utf16("1")};
147 res = resSchedService_->Dump(correctFd, argsOnePlugin);
148
149 std::vector<std::u16string> argsOnePlugin1 = {to_utf16("getRunningLockInfo")};
150 res = resSchedService_->Dump(correctFd, argsOnePlugin1);
151
152 std::vector<std::u16string> argsOnePlugin2 = {to_utf16("getProcessEventInfo")};
153 res = resSchedService_->Dump(correctFd, argsOnePlugin2);
154
155 std::vector<std::u16string> argsOnePlugin3 = {to_utf16("getProcessWindowInfo")};
156 res = resSchedService_->Dump(correctFd, argsOnePlugin3);
157
158 std::vector<std::u16string> argsOnePlugin4 = {to_utf16("getSystemloadInfo")};
159 res = resSchedService_->Dump(correctFd, argsOnePlugin4);
160
161 std::vector<std::u16string> argsOnePlugin5 = {to_utf16("sendDebugToExecutor")};
162 res = resSchedService_->Dump(correctFd, argsOnePlugin5);
163 }
164
165 /**
166 * @tc.name: Ressched service ReportData 001
167 * @tc.desc: Verify if Ressched service ReportData is success.
168 * @tc.type: FUNC
169 * @tc.require: issueI5WWV3
170 * @tc.author:lice
171 */
172 HWTEST_F(ResSchedServiceTest, Report001, Function | MediumTest | Level0)
173 {
174 nlohmann::json payload;
175 EXPECT_TRUE(resSchedService_ != nullptr);
176 resSchedService_->ReportData(0, 0, payload);
177 }
178
179 /**
180 * @tc.name: ReportSyncEvent
181 * @tc.desc: test func ReportSyncEvent.
182 * @tc.type: FUNC
183 * @tc.require: I9QN9E
184 */
185 HWTEST_F(ResSchedServiceTest, ReportSyncEvent, Function | MediumTest | Level0)
186 {
187 EXPECT_NE(resSchedService_, nullptr);
188 nlohmann::json payload({{"pid", 100}});
189 nlohmann::json reply;
190 int32_t ret = resSchedService_->ReportSyncEvent(ResType::SYNC_RES_TYPE_THAW_ONE_APP, 0, payload, reply);
191 // 事件分发失败,返回err
192 EXPECT_NE(ret, 0);
193 }
194
ReportTask()195 static void ReportTask()
196 {
197 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
198 nlohmann::json payload;
199 EXPECT_TRUE(resSchedService_ != nullptr);
200 resSchedService_->ReportData(0, 0, payload);
201 }
202
203 /**
204 * @tc.name: Ressched service ReportData 002
205 * @tc.desc: Test Ressched service ReportData in multithreading.
206 * @tc.type: FUNC
207 * @tc.require: issueI7G8VT
208 * @tc.author: nizihao
209 */
210 HWTEST_F(ResSchedServiceTest, Report002, Function | MediumTest | Level0)
211 {
212 SET_THREAD_NUM(10);
213 GTEST_RUN_TASK(ReportTask);
214 }
215
216 /**
217 * @tc.name: Ressched service KillProcess 001
218 * @tc.desc: test the interface service KillProcess
219 * @tc.type: FUNC
220 * @tc.require: issueI8VZVN
221 * @tc.author:z30053169
222 */
223 HWTEST_F(ResSchedServiceTest, KillProcess001, Function | MediumTest | Level0)
224 {
225 nlohmann::json payload;
226 int32_t t = resSchedService_->KillProcess(payload);
227 EXPECT_EQ(t, -1);
228 }
229
230 /**
231 * @tc.name: Ressched service TestResSchedSystemloadListener 001
232 * @tc.desc: test the interface service TestResSchedSystemloadListener
233 * @tc.type: FUNC
234 * @tc.require: issueI97M6C
235 * @tc.author:shanhaiyang
236 */
237 HWTEST_F(ResSchedServiceTest, TestResSchedSystemloadListener001, Function | MediumTest | Level0)
238 {
239 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
240 EXPECT_TRUE(resSchedService_ != nullptr);
241 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
242 EXPECT_TRUE(notifier != nullptr);
243 NotifierMgr::GetInstance().Init();
244 resSchedService_->RegisterSystemloadNotifier(notifier);
245 NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
246 resSchedService_->OnDeviceLevelChanged(0, 2);
247 sleep(1);
248 EXPECT_TRUE(TestResSchedSystemloadListener::testSystemloadLevel == 2);
249 resSchedService_->UnRegisterSystemloadNotifier();
250 NotifierMgr::GetInstance().OnApplicationStateChange(4, IPCSkeleton::GetCallingPid());
251 TestResSchedSystemloadListener::testSystemloadLevel = 0;
252 }
253
254 /**
255 * @tc.name: Ressched service TestResSchedSystemloadListener 002
256 * @tc.desc: test the interface service TestResSchedSystemloadListener
257 * @tc.type: FUNC
258 * @tc.require: issueI97M6C
259 * @tc.author:shanhaiyang
260 */
261 HWTEST_F(ResSchedServiceTest, TestResSchedSystemloadListener002, Function | MediumTest | Level0)
262 {
263 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
264 EXPECT_TRUE(resSchedService_ != nullptr);
265 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
266 EXPECT_TRUE(notifier != nullptr);
267 NotifierMgr::GetInstance().Init();
268 resSchedService_->RegisterSystemloadNotifier(notifier);
269 resSchedService_->OnDeviceLevelChanged(0, 2);
270 sleep(1);
271 EXPECT_TRUE(TestResSchedSystemloadListener::testSystemloadLevel == 2);
272 resSchedService_->UnRegisterSystemloadNotifier();
273 TestResSchedSystemloadListener::testSystemloadLevel = 0;
274 }
275
276 /**
277 * @tc.name: Ressched service TestResSchedSystemloadListener 003
278 * @tc.desc: test the interface service TestResSchedSystemloadListener
279 * @tc.type: FUNC
280 * @tc.require: issueI97M6C
281 * @tc.author:shanhaiyang
282 */
283 HWTEST_F(ResSchedServiceTest, TestResSchedSystemloadListener003, Function | MediumTest | Level0)
284 {
285 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
286 EXPECT_TRUE(resSchedService_ != nullptr);
287 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
288 EXPECT_TRUE(notifier != nullptr);
289 NotifierMgr::GetInstance().Init();
290 resSchedService_->RegisterSystemloadNotifier(notifier);
291 NotifierMgr::GetInstance().OnApplicationStateChange(2, 111111);
292 resSchedService_->OnDeviceLevelChanged(0, 2);
293 sleep(1);
294 EXPECT_TRUE(TestResSchedSystemloadListener::testSystemloadLevel == 2);
295 resSchedService_->UnRegisterSystemloadNotifier();
296 NotifierMgr::GetInstance().OnApplicationStateChange(4, 111111);
297 TestResSchedSystemloadListener::testSystemloadLevel = 0;
298 }
299
300 /**
301 * @tc.name: Ressched service TestResSchedSystemloadListener 004
302 * @tc.desc: test the interface service TestResSchedSystemloadListener
303 * @tc.type: FUNC
304 * @tc.require: issueI97M6C
305 * @tc.author:shanhaiyang
306 */
307 HWTEST_F(ResSchedServiceTest, TestResSchedSystemloadListener004, Function | MediumTest | Level0)
308 {
309 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
310 EXPECT_TRUE(resSchedService_ != nullptr);
311 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
312 EXPECT_TRUE(notifier != nullptr);
313 NotifierMgr::GetInstance().Init();
314 std::string cbType = "systemLoadChange";
315 resSchedService_->RegisterSystemloadNotifier(notifier);
316 NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
317 NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
318 resSchedService_->OnDeviceLevelChanged(0, 2);
319 sleep(1);
320 EXPECT_TRUE(TestResSchedSystemloadListener::testSystemloadLevel == 0);
321 resSchedService_->UnRegisterSystemloadNotifier();
322 NotifierMgr::GetInstance().OnApplicationStateChange(4, IPCSkeleton::GetCallingPid());
323 TestResSchedSystemloadListener::testSystemloadLevel = 0;
324 }
325
326 /**
327 * @tc.name: Ressched service RegisterSystemloadNotifier 001
328 * @tc.desc: test the interface service RegisterSystemloadNotifier
329 * @tc.type: FUNC
330 * @tc.require: issueI97M6C
331 * @tc.author:shanhaiyang
332 */
333 HWTEST_F(ResSchedServiceTest, RegisterSystemloadNotifier001, Function | MediumTest | Level0)
334 {
335 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
336 EXPECT_TRUE(resSchedService_ != nullptr);
337 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
338 EXPECT_TRUE(notifier != nullptr);
339 NotifierMgr::GetInstance().Init();
340 resSchedService_->RegisterSystemloadNotifier(notifier);
341 }
342
343 /**
344 * @tc.name: Ressched service RegisterSystemloadNotifier 002
345 * @tc.desc: test the interface service RegisterSystemloadNotifier
346 * @tc.type: FUNC
347 * @tc.require: issueI97M6C
348 * @tc.author:shanhaiyang
349 */
350 HWTEST_F(ResSchedServiceTest, RegisterSystemloadNotifier002, Function | MediumTest | Level0)
351 {
352 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
353 EXPECT_TRUE(resSchedService_ != nullptr);
354 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
355 EXPECT_TRUE(notifier != nullptr);
356 NotifierMgr::GetInstance().Init();
357 resSchedService_->RegisterSystemloadNotifier(notifier);
358 resSchedService_->RegisterSystemloadNotifier(notifier);
359 }
360
361 /**
362 * @tc.name: Ressched service UnRegisterSystemloadNotifier 001
363 * @tc.desc: test the interface service UnRegisterSystemloadNotifier
364 * @tc.type: FUNC
365 * @tc.require: issueI97M6C
366 * @tc.author:shanhaiyang
367 */
368 HWTEST_F(ResSchedServiceTest, UnRegisterSystemloadNotifier001, Function | MediumTest | Level0)
369 {
370 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
371 EXPECT_TRUE(resSchedService_ != nullptr);
372 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
373 EXPECT_TRUE(notifier != nullptr);
374 NotifierMgr::GetInstance().Init();
375 resSchedService_->RegisterSystemloadNotifier(notifier);
376 resSchedService_->UnRegisterSystemloadNotifier();
377 }
378
379 /**
380 * @tc.name: Ressched service UnRegisterSystemloadNotifier 002
381 * @tc.desc: test the interface service UnRegisterSystemloadNotifier
382 * @tc.type: FUNC
383 * @tc.require: issueI97M6C
384 * @tc.author:shanhaiyang
385 */
386 HWTEST_F(ResSchedServiceTest, UnRegisterSystemloadNotifier002, Function | MediumTest | Level0)
387 {
388 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
389 EXPECT_TRUE(resSchedService_ != nullptr);
390 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
391 EXPECT_TRUE(notifier != nullptr);
392 NotifierMgr::GetInstance().Init();
393 resSchedService_->UnRegisterSystemloadNotifier();
394 }
395
396 /**
397 * @tc.name: Ressched service GetSystemloadLevel 001
398 * @tc.desc: test the interface service GetSystemloadLevel
399 * @tc.type: FUNC
400 * @tc.require: issueI97M6C
401 * @tc.author:shanhaiyang
402 */
403 HWTEST_F(ResSchedServiceTest, GetSystemloadLevel001, Function | MediumTest | Level0)
404 {
405 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
406 EXPECT_TRUE(resSchedService_ != nullptr);
407 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
408 EXPECT_TRUE(notifier != nullptr);
409 NotifierMgr::GetInstance().Init();
410 resSchedService_->OnDeviceLevelChanged(0, 0);
411 int32_t res = resSchedService_->GetSystemloadLevel();
412 EXPECT_TRUE(res == 0);
413 }
414
415 /**
416 * @tc.name: Ressched service GetSystemloadLevel 002
417 * @tc.desc: test the interface service GetSystemloadLevel
418 * @tc.type: FUNC
419 * @tc.require: issueI97M6C
420 * @tc.author:shanhaiyang
421 */
422 HWTEST_F(ResSchedServiceTest, GetSystemloadLevel002, Function | MediumTest | Level0)
423 {
424 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
425 EXPECT_TRUE(resSchedService_ != nullptr);
426 sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
427 EXPECT_TRUE(notifier != nullptr);
428 NotifierMgr::GetInstance().Init();
429 resSchedService_->OnDeviceLevelChanged(0, 2);
430 resSchedService_->OnDeviceLevelChanged(1, 5);
431 int32_t res = resSchedService_->GetSystemloadLevel();
432 EXPECT_TRUE(res == 2);
433 }
434
435 /**
436 * @tc.name: Start ResSchedServiceAbility 001
437 * @tc.desc: Verify if ResSchedServiceAbility OnStart is success.
438 * @tc.type: FUNC
439 * @tc.require: issueI5WWV3
440 * @tc.author:lice
441 */
442 HWTEST_F(ResSchedServiceTest, OnStart001, Function | MediumTest | Level0)
443 {
444 resSchedServiceAbility_->OnStart();
445 EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
446 }
447
OnStartTask()448 static void OnStartTask()
449 {
450 std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
451 resSchedServiceAbility_->OnStart();
452 EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
453 }
454
455 /**
456 * @tc.name: Start ResSchedServiceAbility 002
457 * @tc.desc: Test ResSchedServiceAbility OnStart in multithreading.
458 * @tc.type: FUNC
459 * @tc.require: issueI7G8VT
460 * @tc.author: nizihao
461 */
462 HWTEST_F(ResSchedServiceTest, OnStart002, Function | MediumTest | Level0)
463 {
464 SET_THREAD_NUM(10);
465 GTEST_RUN_TASK(OnStartTask);
466 }
467
468 /**
469 * @tc.name: ResSchedServiceAbility ChangeAbility 001
470 * @tc.desc: Verify if add and remove system ability is success.
471 * @tc.type: FUNC
472 * @tc.require: issueI5WWV3
473 * @tc.author:lice
474 */
475 HWTEST_F(ResSchedServiceTest, ChangeAbility001, Function | MediumTest | Level0)
476 {
477 std::string deviceId;
478 int32_t systemAbilityId = -1;
479 resSchedServiceAbility_->OnAddSystemAbility(systemAbilityId, deviceId);
480 resSchedServiceAbility_->OnRemoveSystemAbility(systemAbilityId, deviceId);
481 EXPECT_EQ(systemAbilityId, -1);
482 }
483
ChangeAbilityTask()484 static void ChangeAbilityTask()
485 {
486 std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
487 std::string deviceId;
488 resSchedServiceAbility_->OnAddSystemAbility(-1, deviceId);
489 resSchedServiceAbility_->OnRemoveSystemAbility(-1, deviceId);
490 }
491
492 /**
493 * @tc.name: ResSchedServiceAbility ChangeAbility 002
494 * @tc.desc: Test add and remove system ability in multithreading.
495 * @tc.type: FUNC
496 * @tc.require: issueI7G8VT
497 * @tc.author: nizihao
498 */
499 HWTEST_F(ResSchedServiceTest, ChangeAbility002, Function | MediumTest | Level0)
500 {
501 SET_THREAD_NUM(10);
502 GTEST_RUN_TASK(ChangeAbilityTask);
503 }
504
505 class TestResSchedServiceStub : public ResSchedServiceStub {
506 public:
TestResSchedServiceStub()507 TestResSchedServiceStub() : ResSchedServiceStub() {}
508
ReportData(uint32_t restype,int64_t value,const nlohmann::json & payload)509 void ReportData(uint32_t restype, int64_t value, const nlohmann::json& payload) override
510 {
511 }
512
ReportSyncEvent(const uint32_t resType,const int64_t value,const nlohmann::json & payload,nlohmann::json & reply)513 int32_t ReportSyncEvent(const uint32_t resType, const int64_t value, const nlohmann::json& payload,
514 nlohmann::json& reply) override
515 {
516 return 0;
517 }
518
KillProcess(const nlohmann::json & payload)519 int32_t KillProcess(const nlohmann::json& payload) override
520 {
521 return 0;
522 }
523
RegisterSystemloadNotifier(const sptr<IRemoteObject> & notifier)524 void RegisterSystemloadNotifier(const sptr<IRemoteObject>& notifier) override
525 {
526 }
527
UnRegisterSystemloadNotifier()528 void UnRegisterSystemloadNotifier() override
529 {
530 }
531
RegisterEventListener(const sptr<IRemoteObject> & listener,uint32_t eventType,uint32_t listenerGroup=ResType::EventListenerGroup::LISTENER_GROUP_COMMON)532 void RegisterEventListener(const sptr<IRemoteObject>& listener, uint32_t eventType,
533 uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON) override
534 {
535 }
536
UnRegisterEventListener(uint32_t eventType,uint32_t listenerGroup=ResType::EventListenerGroup::LISTENER_GROUP_COMMON)537 void UnRegisterEventListener(uint32_t eventType,
538 uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON) override
539 {
540 }
541
GetSystemloadLevel()542 int32_t GetSystemloadLevel() override
543 {
544 return 0;
545 }
546
IsAllowedAppPreload(const std::string & bundleName,int32_t preloadMode)547 bool IsAllowedAppPreload(const std::string& bundleName, int32_t preloadMode) override
548 {
549 return true;
550 }
551 };
552
553 /**
554 * @tc.name: ResSchedServicesStub ReportDataInner 001
555 * @tc.desc: Verify if resschedstub reportdatainner is success.
556 * @tc.type: FUNC
557 * @tc.require: issueI5WWV3
558 * @tc.author:lice
559 */
560 HWTEST_F(ResSchedServiceTest, ReportDataInner001, Function | MediumTest | Level0)
561 {
562 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
563 resSchedServiceStub_->Init();
564 MessageParcel reply;
565 MessageParcel emptyData;
566 EXPECT_TRUE(resSchedServiceStub_->ReportDataInner(emptyData, reply));
567
568 MessageParcel reportData;
569 reportData.WriteInterfaceToken(ResSchedServiceStub::GetDescriptor());
570 reportData.WriteUint32(1);
571 reportData.WriteInt64(1);
572 reportData.WriteString("{ { \" uid \" : \" 1 \" } }");
573 SUCCEED();
574 }
575
576 /**
577 * @tc.name: ReportSyncEventInner
578 * @tc.desc: test func ReportSyncEventInner.
579 * @tc.type: FUNC
580 * @tc.require: I9QN9E
581 */
582 HWTEST_F(ResSchedServiceTest, ReportSyncEventInner, Function | MediumTest | Level0)
583 {
584 auto serviceStub = make_shared<TestResSchedServiceStub>();
585 EXPECT_NE(serviceStub, nullptr);
586 serviceStub->Init();
587 MessageParcel data;
588 data.WriteInterfaceToken(ResSchedServiceStub::GetDescriptor());
589 data.WriteUint32(ResType::SYNC_RES_TYPE_THAW_ONE_APP);
590 data.WriteInt64(0);
591 data.WriteString(R"({"pid": 100})");
592 MessageParcel reply;
593 EXPECT_NE(serviceStub->ReportSyncEventInner(data, reply), 0);
594 }
595
ReportDataInnerTask()596 static void ReportDataInnerTask()
597 {
598 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
599 resSchedServiceStub_->Init();
600 MessageParcel reply;
601 MessageParcel emptyData;
602 EXPECT_TRUE(resSchedServiceStub_->ReportDataInner(emptyData, reply));
603
604 MessageParcel reportData;
605 reportData.WriteInterfaceToken(ResSchedServiceStub::GetDescriptor());
606 reportData.WriteUint32(1);
607 reportData.WriteInt64(1);
608 reportData.WriteString("{ { \" uid \" : \" 1 \" } }");
609 SUCCEED();
610 }
611
612 /**
613 * @tc.name: ResSchedServicesStub ReportDataInner 002
614 * @tc.desc: Test resschedstub reportdatainner in multithreading.
615 * @tc.type: FUNC
616 * @tc.require: issueI7G8VT
617 * @tc.author: nizihao
618 */
619 HWTEST_F(ResSchedServiceTest, ReportDataInner002, Function | MediumTest | Level0)
620 {
621 SET_THREAD_NUM(10);
622 GTEST_RUN_TASK(ReportDataInnerTask);
623 }
624
625 /**
626 * @tc.name: ResSchedServicesStub StringToJson 001
627 * @tc.desc: Verify if resschedstub StringToJson is success.
628 * @tc.type: FUNC
629 * @tc.require: issueI5WWV3
630 * @tc.author:lice
631 */
632 HWTEST_F(ResSchedServiceTest, StringToJson001, Function | MediumTest | Level0)
633 {
634 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
635 nlohmann::json res = resSchedServiceStub_->StringToJsonObj("");
636 EXPECT_TRUE(!res.dump().empty());
637 }
638
StringToJsonTask()639 static void StringToJsonTask()
640 {
641 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
642 nlohmann::json res = resSchedServiceStub_->StringToJsonObj("");
643 EXPECT_TRUE(!res.dump().empty());
644 }
645
646 /**
647 * @tc.name: ResSchedServicesStub StringToJson 002
648 * @tc.desc: Test resschedstub StringToJson in multithreading.
649 * @tc.type: FUNC
650 * @tc.require: issueI7G8VT
651 * @tc.author: nizihao
652 */
653 HWTEST_F(ResSchedServiceTest, StringToJson002, Function | MediumTest | Level0)
654 {
655 SET_THREAD_NUM(10);
656 GTEST_RUN_TASK(StringToJsonTask);
657 }
658
659 /**
660 * @tc.name: ResSchedServicesStub RemoteRequest 001
661 * @tc.desc: Verify if resschedstub RemoteRequest is success.
662 * @tc.type: FUNC
663 * @tc.require: issueI5WWV3 issueI6D6BM
664 * @tc.author:lice
665 */
666 HWTEST_F(ResSchedServiceTest, RemoteRequest001, Function | MediumTest | Level0)
667 {
668 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
669 MessageOption option;
670 MessageParcel reply;
671 int32_t res = resSchedServiceStub_->OnRemoteRequest(
672 static_cast<uint32_t>(ResourceScheduleInterfaceCode::REPORT_DATA), reply, reply, option);
673 EXPECT_TRUE(res);
674 res = resSchedServiceStub_->OnRemoteRequest(
675 static_cast<uint32_t>(ResourceScheduleInterfaceCode::REPORT_SYNC_EVENT), reply, reply, option);
676 EXPECT_TRUE(res);
677 res = resSchedServiceStub_->OnRemoteRequest(
678 static_cast<uint32_t>(ResourceScheduleInterfaceCode::KILL_PROCESS), reply, reply, option);
679 EXPECT_TRUE(res);
680 res = resSchedServiceStub_->OnRemoteRequest(0, reply, reply, option);
681 EXPECT_TRUE(res);
682 res = resSchedServiceStub_->OnRemoteRequest(
683 static_cast<uint32_t>(ResourceScheduleInterfaceCode::REGISTER_SYSTEMLOAD_NOTIFIER), reply, reply, option);
684 EXPECT_TRUE(!res);
685 res = resSchedServiceStub_->OnRemoteRequest(
686 static_cast<uint32_t>(ResourceScheduleInterfaceCode::UNREGISTER_SYSTEMLOAD_NOTIFIER), reply, reply, option);
687 EXPECT_TRUE(!res);
688 res = resSchedServiceStub_->OnRemoteRequest(
689 static_cast<uint32_t>(ResourceScheduleInterfaceCode::GET_SYSTEMLOAD_LEVEL), reply, reply, option);
690 EXPECT_TRUE(res);
691 }
692
RemoteRequestTask()693 static void RemoteRequestTask()
694 {
695 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
696 MessageOption option;
697 MessageParcel reply;
698 int32_t res = resSchedServiceStub_->OnRemoteRequest(
699 static_cast<uint32_t>(ResourceScheduleInterfaceCode::REPORT_DATA), reply, reply, option);
700 EXPECT_TRUE(res);
701 res = resSchedServiceStub_->OnRemoteRequest(
702 static_cast<uint32_t>(ResourceScheduleInterfaceCode::KILL_PROCESS), reply, reply, option);
703 EXPECT_TRUE(res);
704 res = resSchedServiceStub_->OnRemoteRequest(0, reply, reply, option);
705 EXPECT_TRUE(res);
706 res = resSchedServiceStub_->OnRemoteRequest(
707 static_cast<uint32_t>(ResourceScheduleInterfaceCode::REGISTER_SYSTEMLOAD_NOTIFIER), reply, reply, option);
708 EXPECT_TRUE(!res);
709 res = resSchedServiceStub_->OnRemoteRequest(
710 static_cast<uint32_t>(ResourceScheduleInterfaceCode::UNREGISTER_SYSTEMLOAD_NOTIFIER), reply, reply, option);
711 EXPECT_TRUE(!res);
712 res = resSchedServiceStub_->OnRemoteRequest(
713 static_cast<uint32_t>(ResourceScheduleInterfaceCode::GET_SYSTEMLOAD_LEVEL), reply, reply, option);
714 EXPECT_TRUE(res);
715 }
716
717 /**
718 * @tc.name: ResSchedServicesStub RemoteRequest 002
719 * @tc.desc: Test resschedstub RemoteRequest in multithreading.
720 * @tc.type: FUNC
721 * @tc.require: issueI7G8VT
722 * @tc.author: nizihao
723 */
724 HWTEST_F(ResSchedServiceTest, RemoteRequest002, Function | MediumTest | Level0)
725 {
726 SET_THREAD_NUM(10);
727 GTEST_RUN_TASK(RemoteRequestTask);
728 }
729
730 /**
731 * @tc.name: ResSchedServicesStub RegisterSystemloadNotifier 001
732 * @tc.desc: Verify if resschedstub RegisterSystemloadNotifier is success.
733 * @tc.type: FUNC
734 * @tc.require: issueI97M6C
735 * @tc.author:shanhaiyang
736 */
737 HWTEST_F(ResSchedServiceTest, StubRegisterSystemloadNotifier001, Function | MediumTest | Level0)
738 {
739 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
740 MessageParcel reply;
741 MessageParcel emptyData;
742 EXPECT_TRUE(resSchedServiceStub_ != nullptr);
743 resSchedServiceStub_->RegisterSystemloadNotifierInner(emptyData, reply);
744 }
745
746 /**
747 * @tc.name: ResSchedServicesStub UnRegisterSystemloadNotifier 001
748 * @tc.desc: Verify if resschedstub UnRegisterSystemloadNotifier is success.
749 * @tc.type: FUNC
750 * @tc.require: issueI97M6C
751 * @tc.author:shanhaiyang
752 */
753 HWTEST_F(ResSchedServiceTest, StubUnRegisterSystemloadNotifier001, Function | MediumTest | Level0)
754 {
755 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
756 MessageParcel reply;
757 MessageParcel emptyData;
758 EXPECT_TRUE(resSchedServiceStub_ != nullptr);
759 resSchedServiceStub_->UnRegisterSystemloadNotifierInner(emptyData, reply);
760 }
761
762 /**
763 * @tc.name: ResSchedServicesStub GetSystemloadLevel 001
764 * @tc.desc: Verify if resschedstub GetSystemloadLevel is success.
765 * @tc.type: FUNC
766 * @tc.require: issueI97M6C
767 * @tc.author:shanhaiyang
768 */
769 HWTEST_F(ResSchedServiceTest, StubGetSystemloadLevel001, Function | MediumTest | Level0)
770 {
771 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
772 MessageParcel reply;
773 MessageParcel emptyData;
774 EXPECT_TRUE(resSchedServiceStub_->GetSystemloadLevelInner(emptyData, reply));
775 }
776
777 /**
778 * @tc.name: ResSchedServicesStub IsAllowedAppPreloadInner 001
779 * @tc.desc: Verify resschedstub allowedAppPreloadInner.
780 * @tc.type: FUNC
781 * @tc.require: issueI9C9JN
782 * @tc.author:xiaoshun
783 */
784 HWTEST_F(ResSchedServiceTest, IsAllowedAppPreloadInner001, Function | MediumTest | Level0)
785 {
786 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
787 resSchedServiceStub_->Init();
788 MessageParcel reply;
789 MessageParcel emptyData;
790 EXPECT_TRUE(!resSchedServiceStub_->IsAllowedAppPreloadInner(emptyData, reply));
791 }
792
793 /**
794 * @tc.name: ResSchedServicesStub IsLimitRequest 001
795 * @tc.desc: IsLimitRequestTest
796 * @tc.type: FUNC
797 * @tc.require: issueI9U0YF
798 * @tc.author:fengyang
799 */
800 HWTEST_F(ResSchedServiceTest, IsLimitRequest001, Function | MediumTest | Level0)
801 {
802 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
803 resSchedServiceStub_->Init();
804 int32_t uid = 0;
805 EXPECT_EQ(resSchedServiceStub_->IsLimitRequest(uid), false);
806 resSchedServiceStub_->appRequestCountMap_[uid] = 300;
807 EXPECT_EQ(resSchedServiceStub_->IsLimitRequest(uid), true);
808 resSchedServiceStub_->allRequestCount_.store(800);
809 EXPECT_EQ(resSchedServiceStub_->IsLimitRequest(uid), true);
810 }
811
812 /**
813 * @tc.name: ResSchedServicesStub PrintLimitLog 001
814 * @tc.desc: PrintLimitLog
815 * @tc.type: FUNC
816 * @tc.require: issuesIAGHOC
817 * @tc.author:fengyang
818 */
819 HWTEST_F(ResSchedServiceTest, PrintLimitLog001, Function | MediumTest | Level0)
820 {
821 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
822 resSchedServiceStub_->Init();
823 int32_t uid = 0;
824 resSchedServiceStub_->isPrintLimitLog_.store(true);
825 resSchedServiceStub_->PrintLimitLog(uid);
826 EXPECT_EQ(resSchedServiceStub_->isPrintLimitLog_.load(), false);
827 }
828
829 /**
830 * @tc.name: ResSchedServicesStub ReportBigData 001
831 * @tc.desc: ReportBigData
832 * @tc.type: FUNC
833 * @tc.require: issuesIAGHOC
834 * @tc.author:fengyang
835 */
836 HWTEST_F(ResSchedServiceTest, ReportBigData001, Function | MediumTest | Level0)
837 {
838 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
839 resSchedServiceStub_->Init();
840 resSchedServiceStub_->isReportBigData_.store(false);
841 resSchedServiceStub_->ReportBigData();
842 resSchedServiceStub_->isReportBigData_.store(true);
843 resSchedServiceStub_->ReportBigData();
844 resSchedServiceStub_->nextReportBigDataTime_ = ResCommonUtil::GetNowMillTime();
845 resSchedServiceStub_->ReportBigData();
846 EXPECT_EQ(resSchedServiceStub_->isReportBigData_.load(), false);
847 }
848
849 /**
850 * @tc.name: ResSchedServicesStub InreaseBigDataCount 001
851 * @tc.desc: InreaseBigDataCount
852 * @tc.type: FUNC
853 * @tc.require: issuesIAGHOC
854 * @tc.author:fengyang
855 */
856 HWTEST_F(ResSchedServiceTest, InreaseBigDataCount001, Function | MediumTest | Level0)
857 {
858 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
859 resSchedServiceStub_->Init();
860 resSchedServiceStub_->isReportBigData_.store(false);
861 resSchedServiceStub_->InreaseBigDataCount();
862 resSchedServiceStub_->isReportBigData_.store(true);
863 resSchedServiceStub_->InreaseBigDataCount();
864 EXPECT_EQ(resSchedServiceStub_->isReportBigData_.load(), true);
865 }
866
867 } // namespace ResourceSchedule
868 } // namespace OHOS
869