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 #include <thread>
19
20 #include <vector>
21 #include "accesstoken_kit.h"
22 #include "ipc_skeleton.h"
23 #include "nativetoken_kit.h"
24 #include "notifier_mgr.h"
25 #include "plugin_mgr.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 system {
37 int32_t g_mockEngMode = 1;
38 template<typename T>
GetIntParameter(const std::string & key,T def)39 T GetIntParameter(const std::string& key, T def)
40 {
41 return g_mockEngMode;
42 }
43 }
44 namespace Security::AccessToken {
45 int32_t g_mockDumpTokenKit = 1;
46 int32_t g_mockReportTokenKit = 1;
VerifyAccessToken(AccessTokenID tokenId,const std::string & permissionName)47 int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenId, const std::string& permissionName)
48 {
49 if (permissionName == "ohos.permission.DUMP") {
50 return g_mockDumpTokenKit;
51 }
52 if (permissionName == "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT") {
53 return g_mockReportTokenKit;
54 }
55 return PermissionState::PERMISSION_GRANTED;
56 }
57
58 ATokenTypeEnum g_mockTokenFlag = TypeATokenTypeEnum::TOKEN_INVALID;
GetTokenTypeFlag(AccessTokenID tokenId)59 ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenId)
60 {
61 return g_mockTokenFlag;
62 }
63
64 bool g_mockHapTokenInfo = false;
GetHapTokenInfo(AccessTokenID tokenId,HapTokenInfo & hapTokenInfoRes)65 int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenId, HapTokenInfo& hapTokenInfoRes)
66 {
67 if (g_mockHapTokenInfo) {
68 hapTokenInfoRes.bundleName = "com.ohos.sceneboard";
69 }
70 return 1;
71 }
72 }
73 bool g_mockAddAbilityListener = true;
AddSystemAbilityListener(int32_t systemAbilityId)74 bool SystemAbility::AddSystemAbilityListener(int32_t systemAbilityId)
75 {
76 return g_mockAddAbilityListener;
77 }
78 int g_mockUid = 0;
GetCallingUid()79 int IPCSkeleton::GetCallingUid()
80 {
81 return g_mockUid;
82 }
83 namespace ResourceSchedule {
84 using namespace std;
85 using namespace testing::ext;
86 using namespace testing::mt;
87 using namespace Security::AccessToken;
88
89 class TestMockResSchedServiceStub : public ResSchedServiceStub {
90 public:
TestMockResSchedServiceStub()91 TestMockResSchedServiceStub() : ResSchedServiceStub() {}
92
ReportData(uint32_t restype,int64_t value,const std::string & payload)93 ErrCode ReportData(uint32_t restype, int64_t value, const std::string& payload) override
94 {
95 return ERR_OK;
96 }
97
ReportSyncEvent(uint32_t resType,int64_t value,const std::string & payload,std::string & reply,int32_t & resultValue)98 ErrCode ReportSyncEvent(uint32_t resType, int64_t value, const std::string& payload,
99 std::string& reply, int32_t& resultValue) override
100 {
101 return ERR_OK;
102 }
103
KillProcess(const std::string & payload,int32_t & resultValue)104 ErrCode KillProcess(const std::string& payload, int32_t& resultValue) override
105 {
106 return ERR_OK;
107 }
108
RegisterSystemloadNotifier(const sptr<IRemoteObject> & notifier)109 ErrCode RegisterSystemloadNotifier(const sptr<IRemoteObject>& notifier) override
110 {
111 return ERR_OK;
112 }
113
UnRegisterSystemloadNotifier()114 ErrCode UnRegisterSystemloadNotifier() override
115 {
116 return ERR_OK;
117 }
118
RegisterEventListener(const sptr<IRemoteObject> & listener,uint32_t eventType,uint32_t listenerGroup)119 ErrCode RegisterEventListener(const sptr<IRemoteObject>& listener, uint32_t eventType,
120 uint32_t listenerGroup) override
121 {
122 return ERR_OK;
123 }
124
UnRegisterEventListener(uint32_t eventType,uint32_t listenerGroup)125 ErrCode UnRegisterEventListener(uint32_t eventType,
126 uint32_t listenerGroup) override
127 {
128 return ERR_OK;
129 }
130
GetSystemloadLevel(int32_t & resultValue)131 ErrCode GetSystemloadLevel(int32_t& resultValue) override
132 {
133 return ERR_OK;
134 }
135
IsAllowedAppPreload(const std::string & bundleName,int32_t preloadMode,bool & resultValue)136 ErrCode IsAllowedAppPreload(const std::string& bundleName, int32_t preloadMode, bool& resultValue) override
137 {
138 return ERR_OK;
139 }
140
IsAllowedLinkJump(bool isAllowedLinkJump,int32_t & resultValue)141 ErrCode IsAllowedLinkJump(bool isAllowedLinkJump, int32_t& resultValue) override
142 {
143 return ERR_OK;
144 }
145
GetResTypeList(std::set<uint32_t> & resTypeList)146 ErrCode GetResTypeList(std::set<uint32_t>& resTypeList) override
147 {
148 return ERR_OK;
149 }
150
RegisterSuspendObserver(const sptr<ISuspendStateObserverBase> & observer,int32_t & funcResult)151 ErrCode RegisterSuspendObserver(const sptr<ISuspendStateObserverBase> &observer, int32_t &funcResult) override
152 {
153 return ERR_OK;
154 }
155
UnregisterSuspendObserver(const sptr<ISuspendStateObserverBase> & observer,int32_t & funcResult)156 ErrCode UnregisterSuspendObserver(const sptr<ISuspendStateObserverBase> &observer, int32_t &funcResult) override
157 {
158 return ERR_OK;
159 }
160
GetSuspendStateByUid(const int32_t uid,bool & isFrozen,int32_t & funcResult)161 ErrCode GetSuspendStateByUid(const int32_t uid, bool &isFrozen, int32_t &funcResult) override
162 {
163 return ERR_OK;
164 }
165
GetSuspendStateByPid(const int32_t pid,bool & isFrozen,int32_t & funcResult)166 ErrCode GetSuspendStateByPid(const int32_t pid, bool &isFrozen, int32_t &funcResult) override
167 {
168 return ERR_OK;
169 }
170 };
171
172 class ResSchedServiceMockTest : public testing::Test {
173 public:
174 static void SetUpTestCase(void);
175 static void TearDownTestCase(void);
176 void SetUp();
177 void TearDown();
178 protected:
179 std::shared_ptr<ResSchedService> resSchedService_ = nullptr;
180 std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = nullptr;
181 std::shared_ptr<TestMockResSchedServiceStub> resSchedServiceStub_ = nullptr;
182 };
183
184 class TestMockResSchedSystemloadListener : public ResSchedSystemloadNotifierStub {
185 public:
186 TestMockResSchedSystemloadListener() = default;
187
OnSystemloadLevel(int32_t level)188 ErrCode OnSystemloadLevel(int32_t level)
189 {
190 testSystemloadLevel = level;
191 return ERR_OK;
192 }
193
194 static int32_t testSystemloadLevel;
195 };
196
197 int32_t TestMockResSchedSystemloadListener::testSystemloadLevel = 0;
198
SetUpTestCase(void)199 void ResSchedServiceMockTest::SetUpTestCase(void)
200 {
201 static const char *perms[] = {
202 "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT",
203 "ohos.permission.DUMP",
204 };
205 uint64_t tokenId;
206 NativeTokenInfoParams infoInstance = {
207 .dcapsNum = 0,
208 .permsNum = 2,
209 .aclsNum = 0,
210 .dcaps = nullptr,
211 .perms = perms,
212 .acls = nullptr,
213 .processName = "ResSchedServiceMockTest",
214 .aplStr = "system_core",
215 };
216 tokenId = GetAccessTokenId(&infoInstance);
217 SetSelfTokenID(tokenId);
218 AccessTokenKit::ReloadNativeTokenInfo();
219 }
220
TearDownTestCase()221 void ResSchedServiceMockTest::TearDownTestCase()
222 {
223 int64_t sleepTime = 10;
224 std::this_thread::sleep_for(std::chrono::seconds(sleepTime));
225 }
226
SetUp()227 void ResSchedServiceMockTest::SetUp()
228 {
229 /**
230 * @tc.setup: initialize the member variable resSchedServiceAbility_
231 */
232 resSchedService_ = make_shared<ResSchedService>();
233 resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
234 resSchedServiceStub_ = make_shared<TestMockResSchedServiceStub>();
235 }
236
TearDown()237 void ResSchedServiceMockTest::TearDown()
238 {
239 /**
240 * @tc.teardown: clear resSchedServiceAbility_
241 */
242 resSchedService_ = nullptr;
243 resSchedServiceAbility_ = nullptr;
244 resSchedServiceStub_ = nullptr;
245 }
246
247 /**
248 * @tc.name: ressched service dump 001
249 * @tc.desc: Verify if ressched service dump commonds is success.
250 * @tc.type: FUNC
251 * @tc.require: issuesIAGHOC
252 * @tc.author: fengyang
253 */
254 HWTEST_F(ResSchedServiceMockTest, ServiceDump001, Function | MediumTest | Level0)
255 {
256 Security::AccessToken::g_mockDumpTokenKit = 0;
257 PluginMgr::GetInstance().Init();
258 std::string result;
259 resSchedService_->DumpAllInfo(result);
260 EXPECT_TRUE(!result.empty());
261
262 result = "";
263 resSchedService_->DumpUsage(result);
264 EXPECT_TRUE(!result.empty());
265
266 int32_t wrongFd = -1;
267 std::vector<std::u16string> argsNull;
268 int res = resSchedService_->Dump(wrongFd, argsNull);
269 EXPECT_EQ(res, ERR_OK);
270 resSchedServiceAbility_->OnStart();
271 int32_t correctFd = -1;
272 res = resSchedService_->Dump(correctFd, argsNull);
273
274 std::vector<std::u16string> argsHelp = {to_utf16("-h")};
275 res = resSchedService_->Dump(correctFd, argsHelp);
276
277 std::vector<std::u16string> argsAll = {to_utf16("-a")};
278 res = resSchedService_->Dump(correctFd, argsAll);
279
280 std::vector<std::u16string> argsError = {to_utf16("-e")};
281 res = resSchedService_->Dump(correctFd, argsError);
282
283 std::vector<std::u16string> argsPlugin = {to_utf16("-p")};
284 res = resSchedService_->Dump(correctFd, argsPlugin);
285
286 std::vector<std::u16string> argsOnePlugin = {to_utf16("-p"), to_utf16("1")};
287 res = resSchedService_->Dump(correctFd, argsOnePlugin);
288
289 std::vector<std::u16string> argsOnePlugin1 = {to_utf16("getRunningLockInfo")};
290 res = resSchedService_->Dump(correctFd, argsOnePlugin1);
291
292 std::vector<std::u16string> argsOnePlugin2 = {to_utf16("getProcessEventInfo")};
293 res = resSchedService_->Dump(correctFd, argsOnePlugin2);
294
295 std::vector<std::u16string> argsOnePlugin3 = {to_utf16("getProcessWindowInfo")};
296 res = resSchedService_->Dump(correctFd, argsOnePlugin3);
297
298 std::vector<std::u16string> argsOnePlugin4 = {to_utf16("getSystemloadInfo")};
299 res = resSchedService_->Dump(correctFd, argsOnePlugin4);
300
301 std::vector<std::u16string> argsOnePlugin5 = {to_utf16("sendDebugToExecutor")};
302 res = resSchedService_->Dump(correctFd, argsOnePlugin5);
303 }
304
305 /**
306 * @tc.name: ressched service dump 002
307 * @tc.desc: Verify if ressched service dump commonds is success.
308 * @tc.type: FUNC
309 * @tc.require: issuesIAGHOC
310 * @tc.author: fengyang
311 */
312 HWTEST_F(ResSchedServiceMockTest, ServiceDump002, Function | MediumTest | Level0)
313 {
314 Security::AccessToken::g_mockDumpTokenKit = 0;
315 resSchedServiceAbility_->OnStart();
316 std::shared_ptr<ResSchedService> resSchedService = make_shared<ResSchedService>();
317 int32_t correctFd = -1;
318 std::vector<std::u16string> argsNull;
319 std::vector<std::u16string> argsOnePlugin1 = {to_utf16("getRunningLockInfo")};
320 int res = resSchedService->Dump(correctFd, argsOnePlugin1);
321 EXPECT_EQ(res, ERR_OK);
322 std::vector<std::u16string> argsOnePlugin2 = {to_utf16("getProcessEventInfo")};
323 res = resSchedService->Dump(correctFd, argsOnePlugin2);
324 EXPECT_EQ(res, ERR_OK);
325 std::vector<std::u16string> argsOnePlugin3 = {to_utf16("getProcessWindowInfo")};
326 res = resSchedService->Dump(correctFd, argsOnePlugin3);
327 EXPECT_EQ(res, ERR_OK);
328 }
329
330 /**
331 * @tc.name: ressched service dump 003
332 * @tc.desc: Verify if ressched service dump commonds is success.
333 * @tc.type: FUNC
334 * @tc.require: issuesIAGHOC
335 * @tc.author: fengyang
336 */
337 HWTEST_F(ResSchedServiceMockTest, ServiceDump003, Function | MediumTest | Level0)
338 {
339 Security::AccessToken::g_mockDumpTokenKit = 0;
340 PluginMgr::GetInstance().Init();
341 auto notifier = new (std::nothrow) TestMockResSchedSystemloadListener();
342 EXPECT_TRUE(notifier != nullptr);
343 resSchedService_->RegisterSystemloadNotifier(notifier);
344 int32_t correctFd = -1;
345 std::vector<std::u16string> argsOnePlugin = {to_utf16("getSystemloadInfo")};
346 int res = resSchedService_->Dump(correctFd, argsOnePlugin);
347 EXPECT_EQ(res, ERR_OK);
348 std::vector<std::u16string> argsOnePlugin2 = {to_utf16("sendDebugToExecutor"), to_utf16("1")};
349 res = resSchedService_->Dump(correctFd, argsOnePlugin2);
350 resSchedService_->UnRegisterSystemloadNotifier();
351 EXPECT_EQ(res, ERR_OK);
352 }
353
354 /**
355 * @tc.name: ressched service dump 004
356 * @tc.desc: Verify if ressched service dump commonds is success.
357 * @tc.type: FUNC
358 * @tc.require: issuesIAGHOC
359 * @tc.author: fengyang
360 */
361 HWTEST_F(ResSchedServiceMockTest, ServiceDump004, Function | MediumTest | Level0)
362 {
363 Security::AccessToken::g_mockDumpTokenKit = 1;
364 int32_t correctFd = -1;
365 std::vector<std::u16string> argsOnePlugin = {to_utf16("-h")};
366 int res = resSchedService_->Dump(correctFd, argsOnePlugin);
367 EXPECT_NE(res, ERR_OK);
368 system::g_mockEngMode = 0;
369 res = resSchedService_->Dump(correctFd, argsOnePlugin);
370 EXPECT_NE(res, ERR_OK);
371 }
372
373 /**
374 * @tc.name: ressched service dump 005
375 * @tc.desc: Verify if ressched service dump commonds is success.
376 * @tc.type: FUNC
377 * @tc.require: issuesIAGHOC
378 * @tc.author: fengyang
379 */
380 HWTEST_F(ResSchedServiceMockTest, ServiceDump005, Function | MediumTest | Level0)
381 {
382 std::vector<std::string> args;
383 std::string result;
384 resSchedService_->DumpSystemLoadInfo(result);
385 EXPECT_NE(result, "");
386 resSchedService_->DumpExecutorDebugCommand(args, result);
387 EXPECT_NE(result, "");
388 resSchedService_->DumpAllPluginConfig(result);
389 EXPECT_NE(result, "");
390 }
391
392 /**
393 * @tc.name: Start ResSchedServiceAbility 001
394 * @tc.desc: Verify if ResSchedServiceAbility OnStart is success.
395 * @tc.type: FUNC
396 * @tc.require: issuesIAGHOC
397 * @tc.author: fengyang
398 */
399 HWTEST_F(ResSchedServiceMockTest, OnStart001, Function | MediumTest | Level0)
400 {
401 g_mockAddAbilityListener = false;
402 resSchedServiceAbility_->OnStart();
403 EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
404 std::string action = "test";
405 resSchedServiceAbility_->OnDeviceLevelChanged(0, 2, action);
406 g_mockAddAbilityListener = true;
407 }
408
409 /**
410 * @tc.name: Start ResSchedServiceAbility 002
411 * @tc.desc: Verify if ResSchedServiceAbility OnStart is success.
412 * @tc.type: FUNC
413 * @tc.require: issueI5WWV3
414 * @tc.author:lice
415 */
416 HWTEST_F(ResSchedServiceMockTest, OnStart002, Function | MediumTest | Level0)
417 {
418 resSchedServiceAbility_->OnStart();
419 EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
420 }
421
OnStartTask()422 static void OnStartTask()
423 {
424 std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
425 resSchedServiceAbility_->OnStart();
426 EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
427 }
428
429 /**
430 * @tc.name: Start ResSchedServiceAbility 003
431 * @tc.desc: Test ResSchedServiceAbility OnStart in multithreading.
432 * @tc.type: FUNC
433 * @tc.require: issueI7G8VT
434 * @tc.author: nizihao
435 */
436 HWTEST_F(ResSchedServiceMockTest, OnStart003, Function | MediumTest | Level0)
437 {
438 SET_THREAD_NUM(10);
439 GTEST_RUN_TASK(OnStartTask);
440 }
441
442 } // namespace ResourceSchedule
443 } // namespace OHOS
444