• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
146 
147 class ResSchedServiceMockTest : public testing::Test {
148 public:
149     static void SetUpTestCase(void);
150     static void TearDownTestCase(void);
151     void SetUp();
152     void TearDown();
153 protected:
154     std::shared_ptr<ResSchedService> resSchedService_ = nullptr;
155     std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = nullptr;
156     std::shared_ptr<TestMockResSchedServiceStub> resSchedServiceStub_ = nullptr;
157 };
158 
159 class TestMockResSchedSystemloadListener : public ResSchedSystemloadNotifierStub {
160 public:
161     TestMockResSchedSystemloadListener() = default;
162 
OnSystemloadLevel(int32_t level)163     ErrCode OnSystemloadLevel(int32_t level)
164     {
165         testSystemloadLevel = level;
166         return ERR_OK;
167     }
168 
169     static int32_t testSystemloadLevel;
170 };
171 
172 int32_t TestMockResSchedSystemloadListener::testSystemloadLevel = 0;
173 
SetUpTestCase(void)174 void ResSchedServiceMockTest::SetUpTestCase(void)
175 {
176     static const char *perms[] = {
177         "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT",
178         "ohos.permission.DUMP",
179     };
180     uint64_t tokenId;
181     NativeTokenInfoParams infoInstance = {
182         .dcapsNum = 0,
183         .permsNum = 2,
184         .aclsNum = 0,
185         .dcaps = nullptr,
186         .perms = perms,
187         .acls = nullptr,
188         .processName = "ResSchedServiceMockTest",
189         .aplStr = "system_core",
190     };
191     tokenId = GetAccessTokenId(&infoInstance);
192     SetSelfTokenID(tokenId);
193     AccessTokenKit::ReloadNativeTokenInfo();
194 }
195 
TearDownTestCase()196 void ResSchedServiceMockTest::TearDownTestCase()
197 {
198     int64_t sleepTime = 10;
199     std::this_thread::sleep_for(std::chrono::seconds(sleepTime));
200 }
201 
SetUp()202 void ResSchedServiceMockTest::SetUp()
203 {
204     /**
205      * @tc.setup: initialize the member variable resSchedServiceAbility_
206      */
207     resSchedService_ = make_shared<ResSchedService>();
208     resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
209     resSchedServiceStub_ = make_shared<TestMockResSchedServiceStub>();
210 }
211 
TearDown()212 void ResSchedServiceMockTest::TearDown()
213 {
214     /**
215      * @tc.teardown: clear resSchedServiceAbility_
216      */
217     resSchedService_ = nullptr;
218     resSchedServiceAbility_ = nullptr;
219     resSchedServiceStub_ = nullptr;
220 }
221 
222 /**
223  * @tc.name: ressched service dump 001
224  * @tc.desc: Verify if ressched service dump commonds is success.
225  * @tc.type: FUNC
226  * @tc.require: issuesIAGHOC
227  * @tc.author: fengyang
228  */
229 HWTEST_F(ResSchedServiceMockTest, ServiceDump001, Function | MediumTest | Level0)
230 {
231     Security::AccessToken::g_mockDumpTokenKit = 0;
232     PluginMgr::GetInstance().Init();
233     std::string result;
234     resSchedService_->DumpAllInfo(result);
235     EXPECT_TRUE(!result.empty());
236 
237     result = "";
238     resSchedService_->DumpUsage(result);
239     EXPECT_TRUE(!result.empty());
240 
241     int32_t wrongFd = -1;
242     std::vector<std::u16string> argsNull;
243     int res = resSchedService_->Dump(wrongFd, argsNull);
244     EXPECT_EQ(res, ERR_OK);
245     resSchedServiceAbility_->OnStart();
246     int32_t correctFd = -1;
247     res = resSchedService_->Dump(correctFd, argsNull);
248 
249     std::vector<std::u16string> argsHelp = {to_utf16("-h")};
250     res = resSchedService_->Dump(correctFd, argsHelp);
251 
252     std::vector<std::u16string> argsAll = {to_utf16("-a")};
253     res = resSchedService_->Dump(correctFd, argsAll);
254 
255     std::vector<std::u16string> argsError = {to_utf16("-e")};
256     res = resSchedService_->Dump(correctFd, argsError);
257 
258     std::vector<std::u16string> argsPlugin = {to_utf16("-p")};
259     res = resSchedService_->Dump(correctFd, argsPlugin);
260 
261     std::vector<std::u16string> argsOnePlugin = {to_utf16("-p"), to_utf16("1")};
262     res = resSchedService_->Dump(correctFd, argsOnePlugin);
263 
264     std::vector<std::u16string> argsOnePlugin1 = {to_utf16("getRunningLockInfo")};
265     res = resSchedService_->Dump(correctFd, argsOnePlugin1);
266 
267     std::vector<std::u16string> argsOnePlugin2 = {to_utf16("getProcessEventInfo")};
268     res = resSchedService_->Dump(correctFd, argsOnePlugin2);
269 
270     std::vector<std::u16string> argsOnePlugin3 = {to_utf16("getProcessWindowInfo")};
271     res = resSchedService_->Dump(correctFd, argsOnePlugin3);
272 
273     std::vector<std::u16string> argsOnePlugin4 = {to_utf16("getSystemloadInfo")};
274     res = resSchedService_->Dump(correctFd, argsOnePlugin4);
275 
276     std::vector<std::u16string> argsOnePlugin5 = {to_utf16("sendDebugToExecutor")};
277     res = resSchedService_->Dump(correctFd, argsOnePlugin5);
278 }
279 
280 /**
281  * @tc.name: ressched service dump 002
282  * @tc.desc: Verify if ressched service dump commonds is success.
283  * @tc.type: FUNC
284  * @tc.require: issuesIAGHOC
285  * @tc.author: fengyang
286  */
287 HWTEST_F(ResSchedServiceMockTest, ServiceDump002, Function | MediumTest | Level0)
288 {
289     Security::AccessToken::g_mockDumpTokenKit = 0;
290     resSchedServiceAbility_->OnStart();
291     std::shared_ptr<ResSchedService> resSchedService = make_shared<ResSchedService>();
292     int32_t correctFd = -1;
293     std::vector<std::u16string> argsNull;
294     std::vector<std::u16string> argsOnePlugin1 = {to_utf16("getRunningLockInfo")};
295     int res = resSchedService->Dump(correctFd, argsOnePlugin1);
296     EXPECT_EQ(res, ERR_OK);
297     std::vector<std::u16string> argsOnePlugin2 = {to_utf16("getProcessEventInfo")};
298     res = resSchedService->Dump(correctFd, argsOnePlugin2);
299     EXPECT_EQ(res, ERR_OK);
300     std::vector<std::u16string> argsOnePlugin3 = {to_utf16("getProcessWindowInfo")};
301     res = resSchedService->Dump(correctFd, argsOnePlugin3);
302     EXPECT_EQ(res, ERR_OK);
303 }
304 
305 /**
306  * @tc.name: ressched service dump 003
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, ServiceDump003, Function | MediumTest | Level0)
313 {
314     Security::AccessToken::g_mockDumpTokenKit = 0;
315     PluginMgr::GetInstance().Init();
316     auto notifier = new (std::nothrow) TestMockResSchedSystemloadListener();
317     EXPECT_TRUE(notifier != nullptr);
318     resSchedService_->RegisterSystemloadNotifier(notifier);
319     int32_t correctFd = -1;
320     std::vector<std::u16string> argsOnePlugin = {to_utf16("getSystemloadInfo")};
321     int res = resSchedService_->Dump(correctFd, argsOnePlugin);
322     EXPECT_EQ(res, ERR_OK);
323     std::vector<std::u16string> argsOnePlugin2 = {to_utf16("sendDebugToExecutor"), to_utf16("1")};
324     res = resSchedService_->Dump(correctFd, argsOnePlugin2);
325     resSchedService_->UnRegisterSystemloadNotifier();
326     EXPECT_EQ(res, ERR_OK);
327 }
328 
329 /**
330  * @tc.name: ressched service dump 004
331  * @tc.desc: Verify if ressched service dump commonds is success.
332  * @tc.type: FUNC
333  * @tc.require: issuesIAGHOC
334  * @tc.author: fengyang
335  */
336 HWTEST_F(ResSchedServiceMockTest, ServiceDump004, Function | MediumTest | Level0)
337 {
338     Security::AccessToken::g_mockDumpTokenKit = 1;
339     int32_t correctFd = -1;
340     std::vector<std::u16string> argsOnePlugin = {to_utf16("-h")};
341     int res = resSchedService_->Dump(correctFd, argsOnePlugin);
342     EXPECT_NE(res, ERR_OK);
343     system::g_mockEngMode = 0;
344     res = resSchedService_->Dump(correctFd, argsOnePlugin);
345     EXPECT_NE(res, ERR_OK);
346 }
347 
348 /**
349  * @tc.name: ressched service dump 005
350  * @tc.desc: Verify if ressched service dump commonds is success.
351  * @tc.type: FUNC
352  * @tc.require: issuesIAGHOC
353  * @tc.author: fengyang
354  */
355 HWTEST_F(ResSchedServiceMockTest, ServiceDump005, Function | MediumTest | Level0)
356 {
357     std::vector<std::string> args;
358     std::string result;
359     resSchedService_->DumpSystemLoadInfo(result);
360     EXPECT_NE(result, "");
361     resSchedService_->DumpExecutorDebugCommand(args, result);
362     EXPECT_NE(result, "");
363     resSchedService_->DumpAllPluginConfig(result);
364     EXPECT_NE(result, "");
365 }
366 
367 /**
368  * @tc.name: Start ResSchedServiceAbility 001
369  * @tc.desc: Verify if ResSchedServiceAbility OnStart is success.
370  * @tc.type: FUNC
371  * @tc.require: issuesIAGHOC
372  * @tc.author: fengyang
373  */
374 HWTEST_F(ResSchedServiceMockTest, OnStart001, Function | MediumTest | Level0)
375 {
376     g_mockAddAbilityListener = false;
377     resSchedServiceAbility_->OnStart();
378     EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
379     std::string action = "test";
380     resSchedServiceAbility_->OnDeviceLevelChanged(0, 2, action);
381     g_mockAddAbilityListener = true;
382 }
383 
384 } // namespace ResourceSchedule
385 } // namespace OHOS
386