• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 <unordered_map>
20 #include <vector>
21 #include "nativetoken_kit.h"
22 #include "res_sa_init.h"
23 #include "res_sched_client.h"
24 #include "res_type.h"
25 #include "res_sched_systemload_notifier_client.h"
26 #include "res_sched_event_listener.h"
27 #include "token_setproc.h"
28 
29 namespace OHOS {
30 namespace ResourceSchedule {
31 using namespace std;
32 using namespace testing::ext;
33 using namespace testing::mt;
34 static constexpr int32_t RSS_SA_ID = 1901;
35 static constexpr int32_t OTHER_SA_ID = 1900;
36 class ResSchedClientTest : public testing::Test {
37 public:
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
40     void SetUp();
41     void TearDown();
42     void MockProcess(int32_t uid);
43 };
44 
45 
SetUpTestCase(void)46 void ResSchedClientTest::SetUpTestCase(void) {}
47 
TearDownTestCase()48 void ResSchedClientTest::TearDownTestCase() {}
49 
SetUp()50 void ResSchedClientTest::SetUp() {}
51 
TearDown()52 void ResSchedClientTest::TearDown() {}
53 
MockProcess(int32_t uid)54 void ResSchedClientTest::MockProcess(int32_t uid)
55 {
56     static const char *perms[] = {
57         "ohos.permission.DISTRIBUTED_DATASYNC"
58     };
59     uint64_t tokenId;
60     NativeTokenInfoParams infoInstance = {
61         .dcapsNum = 0,
62         .permsNum = 1,
63         .aclsNum = 0,
64         .dcaps = nullptr,
65         .perms = perms,
66         .acls = nullptr,
67         .processName = "samgr",
68         .aplStr = "system_core",
69     };
70     tokenId = GetAccessTokenId(&infoInstance);
71     SetSelfTokenID(tokenId);
72     setuid(uid);
73 }
74 
75 class ResSchedSystemloadNotifierClientMock : public ResSchedSystemloadNotifierClient {
76 public:
77     ResSchedSystemloadNotifierClientMock() = default;
78     ~ResSchedSystemloadNotifierClientMock() = default;
OnSystemloadLevel(int32_t level)79     void OnSystemloadLevel(int32_t level) override
80     {
81         levels = level;
82     }
83     static int32_t levels;
84 };
85 
86 int32_t ResSchedSystemloadNotifierClientMock::levels = 0;
87 
88 class ResSchedEventListenerMock : public ResSchedEventListener {
89 public:
90     ResSchedEventListenerMock() = default;
91     ~ResSchedEventListenerMock() = default;
OnReceiveEvent(uint32_t eventType,uint32_t eventValue,std::unordered_map<std::string,std::string> extInfo)92     void OnReceiveEvent(uint32_t eventType, uint32_t eventValue,
93         std::unordered_map<std::string, std::string> extInfo) override
94     {
95         type = eventType;
96         value = eventValue;
97     }
98     static int32_t type;
99     static int32_t value;
100 };
101 
102 int32_t ResSchedEventListenerMock::type = 0;
103 int32_t ResSchedEventListenerMock::value = 0;
104 
105 /**
106  * @tc.name: KillProcess001
107  * @tc.desc: kill process stable test
108  * @tc.type: FUNC
109  * @tc.require: I6EEJI
110  * @tc.author: qiunaiguang
111  */
112 HWTEST_F(ResSchedClientTest, KillProcess001, Function | MediumTest | Level0)
113 {
114     int32_t uid = 5555;
115     MockProcess(uid);
116     std::unordered_map<std::string, std::string> mapPayload;
117     mapPayload["pid"] = "65535";
118     mapPayload["processName"] = "test";
119     for (int i = 0; i < 100; i++) {
120         ResSchedClient::GetInstance().KillProcess(mapPayload);
121     }
122     EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
123 }
124 
125 /**
126  * @tc.name: KillProcess002
127  * @tc.desc: kill process error test
128  * @tc.type: FUNC
129  * @tc.require: I6EEJI
130  * @tc.author: qiunaiguang
131  */
132 HWTEST_F(ResSchedClientTest, KillProcess002, Function | MediumTest | Level0)
133 {
134     int32_t uid = 5555;
135     MockProcess(uid);
136     std::unordered_map<std::string, std::string> mapPayload;
137     ResSchedClient::GetInstance().KillProcess(mapPayload);
138     EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
139 
140     mapPayload["pid"] = "TEST";
141     ResSchedClient::GetInstance().KillProcess(mapPayload);
142     EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
143 
144     mapPayload["pid"] = "65535";
145     mapPayload["processName"] = "test";
146     ResSchedClient::GetInstance().KillProcess(mapPayload);
147     EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
148 
149     uid = 6666;
150     MockProcess(uid);
151     ResSchedClient::GetInstance().KillProcess(mapPayload);
152     EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
153 }
154 
StartKillProcess()155 static void StartKillProcess()
156 {
157     std::unordered_map<std::string, std::string> payload;
158     payload["pid"] = "65535";
159     payload["processName"] = "test_process";
160     ResSchedClient::GetInstance().KillProcess(payload);
161     EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
162 }
163 
164 /**
165  * @tc.name: MultiThreadKillProcess
166  * @tc.desc: multi-thread invoking test
167  * @tc.type: FUNC
168  * @tc.require: IACR9M
169  */
170 HWTEST_F(ResSchedClientTest, MultiThreadKillProcess, Function | MediumTest | Level0)
171 {
172     SET_THREAD_NUM(10);
173     GTEST_RUN_TASK(StartKillProcess);
174 }
175 
StartReportSyncEvent()176 static void StartReportSyncEvent()
177 {
178     uint32_t resType = ResType::SYNC_RES_TYPE_THAW_ONE_APP;
179     nlohmann::json payload;
180     payload.emplace("pid", 1);
181     payload.emplace("reason", "test_reason");
182     nlohmann::json reply;
183     int32_t ret = ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
184     // 权限校验失败,返回err
185     EXPECT_NE(ret, 0);
186     EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
187 }
188 
189 /**
190  * @tc.name: MultiThreadReportSyncEvent
191  * @tc.desc: multi-thread invoking test
192  * @tc.type: FUNC
193  * @tc.require: IACR9M
194  */
195 HWTEST_F(ResSchedClientTest, MultiThreadReportSyncEvent, Function | MediumTest | Level0)
196 {
197     SET_THREAD_NUM(10);
198     GTEST_RUN_TASK(StartReportSyncEvent);
199 }
200 
201 /**
202  * @tc.name: ReportSyncEvent
203  * @tc.desc: test func ReportSyncEvent
204  * @tc.type: FUNC
205  * @tc.require: I9QN9E
206  */
207 HWTEST_F(ResSchedClientTest, ReportSyncEvent, Function | MediumTest | Level0)
208 {
209     uint32_t resType = ResType::SYNC_RES_TYPE_THAW_ONE_APP;
210     nlohmann::json payload;
211     payload.emplace("pid", 1);
212     payload.emplace("reason", "test_reason");
213     nlohmann::json reply;
214     int32_t ret = ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
215     // 权限校验失败,返回err
216     EXPECT_NE(ret, 0);
217 }
218 
219 /**
220  * @tc.name: RegisterSystemloadNotifier001
221  * @tc.desc: Register systemload notifier
222  * @tc.type: FUNC
223  * @tc.require: issueI9G149
224  * @tc.author: shanhaiyang
225  */
226 HWTEST_F(ResSchedClientTest, RegisterSystemloadNotifier001, Function | MediumTest | Level0)
227 {
228     sptr<ResSchedSystemloadNotifierClient> notifier =
229         new (std::nothrow) ResSchedSystemloadNotifierClientMock;
230     EXPECT_TRUE(notifier != nullptr);
231     ResSchedClient::GetInstance().RegisterSystemloadNotifier(notifier);
232     ResSchedClient::GetInstance().RegisterSystemloadNotifier(notifier);
233     ResSchedClient::GetInstance().systemloadLevelListener_->OnSystemloadLevel(2);
234     EXPECT_TRUE(ResSchedSystemloadNotifierClientMock::levels == 2);
235     ResSchedSystemloadNotifierClientMock::levels = 0;
236     ResSchedClient::GetInstance().UnRegisterSystemloadNotifier(notifier);
237 }
238 
239 /**
240  * @tc.name: UnRegisterSystemloadNotifier001
241  * @tc.desc: UnRegister systemload notifier
242  * @tc.type: FUNC
243  * @tc.require: issueI9G149
244  * @tc.author: shanhaiyang
245  */
246 HWTEST_F(ResSchedClientTest, UnRegisterSystemloadNotifier001, Function | MediumTest | Level0)
247 {
248     sptr<ResSchedSystemloadNotifierClient> notifier =
249         new (std::nothrow) ResSchedSystemloadNotifierClientMock;
250     EXPECT_TRUE(notifier != nullptr);
251     ResSchedClient::GetInstance().RegisterSystemloadNotifier(notifier);
252     ResSchedClient::GetInstance().UnRegisterSystemloadNotifier(notifier);
253     ResSchedClient::GetInstance().systemloadLevelListener_->OnSystemloadLevel(2);
254     EXPECT_TRUE(ResSchedSystemloadNotifierClientMock::levels == 0);
255 }
256 
257 /**
258  * @tc.name: RegisterEventListener001
259  * @tc.desc: Register event listener
260  * @tc.type: FUNC
261  * @tc.require: issueIA9UZ9
262  * @tc.author: baiheng
263  */
264 HWTEST_F(ResSchedClientTest, RegisterEventListener001, Function | MediumTest | Level0)
265 {
266     sptr<ResSchedEventListener> eventListener =
267         new (std::nothrow) ResSchedEventListenerMock;
268     EXPECT_TRUE(eventListener != nullptr);
269     ResSchedClient::GetInstance().RegisterEventListener(eventListener,
270         ResType::EventType::EVENT_DRAW_FRAME_REPORT);
271     ResSchedClient::GetInstance().RegisterEventListener(eventListener,
272         ResType::EventType::EVENT_DRAW_FRAME_REPORT);
273     std::string extInfo;
274     ResSchedClient::GetInstance().innerEventListener_->OnReceiveEvent(ResType::EventType::EVENT_DRAW_FRAME_REPORT,
275         ResType::EventValue::EVENT_VALUE_DRAW_FRAME_REPORT_START,
276         ResType::EventListenerGroup::LISTENER_GROUP_COMMON, extInfo);
277     EXPECT_TRUE(ResSchedEventListenerMock::type == ResType::EventType::EVENT_DRAW_FRAME_REPORT);
278     EXPECT_TRUE(ResSchedEventListenerMock::value == ResType::EventValue::EVENT_VALUE_DRAW_FRAME_REPORT_START);
279     ResSchedEventListenerMock::type = 0;
280     ResSchedEventListenerMock::value = 0;
281     ResSchedClient::GetInstance().UnRegisterEventListener(eventListener,
282         ResType::EventType::EVENT_DRAW_FRAME_REPORT);
283     SUCCEED();
284 }
285 
286 /**
287  * @tc.name: UnRegisterEventListener001
288  * @tc.desc: UnRegister event listener
289  * @tc.type: FUNC
290  * @tc.require: issueIA9UZ9
291  * @tc.author: baiheng
292  */
293 HWTEST_F(ResSchedClientTest, UnRegisterEventListener001, Function | MediumTest | Level0)
294 {
295     sptr<ResSchedEventListener> eventListener =
296         new (std::nothrow) ResSchedEventListenerMock;
297     EXPECT_TRUE(eventListener != nullptr);
298     ResSchedClient::GetInstance().RegisterEventListener(eventListener,
299         ResType::EventType::EVENT_DRAW_FRAME_REPORT);
300     ResSchedClient::GetInstance().UnRegisterEventListener(eventListener,
301         ResType::EventType::EVENT_DRAW_FRAME_REPORT);
302     std::string extInfo;
303     ResSchedClient::GetInstance().innerEventListener_->OnReceiveEvent(ResType::EventType::EVENT_DRAW_FRAME_REPORT,
304         ResType::EventValue::EVENT_VALUE_DRAW_FRAME_REPORT_START,
305         ResType::EventListenerGroup::LISTENER_GROUP_COMMON, extInfo);
306     EXPECT_TRUE(ResSchedEventListenerMock::type == 0);
307     EXPECT_TRUE(ResSchedEventListenerMock::value == 0);
308 }
309 
310 /**
311  * @tc.name: GetSystemloadLevel001
312  * @tc.desc: Get systemload level
313  * @tc.type: FUNC
314  * @tc.require: issueI9G149
315  * @tc.author: shanhaiyang
316  */
317 HWTEST_F(ResSchedClientTest, GetSystemloadLevel001, Function | MediumTest | Level0)
318 {
319     int32_t res = ResSchedClient::GetInstance().GetSystemloadLevel();
320     EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
321 }
322 
323 /**
324  * @tc.name: ResSchedSvcStatusChange add 001
325  * @tc.desc: ResSchedSvcStatusChange OnAddSystemAbility
326  * @tc.type: FUNC
327  * @tc.require: issueI9G149
328  * @tc.author: shanhaiyang
329  */
330 HWTEST_F(ResSchedClientTest, OnAddSystemAbility001, Function | MediumTest | Level0)
331 {
332     ASSERT_TRUE(ResSchedClient::GetInstance().resSchedSvcStatusListener_);
333     std::string empty;
334     ResSchedClient::GetInstance().resSchedSvcStatusListener_->OnAddSystemAbility(RSS_SA_ID, empty);
335     sptr<ResSchedSystemloadNotifierClient> notifier =
336         new (std::nothrow) ResSchedSystemloadNotifierClientMock;
337     EXPECT_TRUE(notifier != nullptr);
338     ResSchedClient::GetInstance().RegisterSystemloadNotifier(notifier);
339     ResSchedClient::GetInstance().UnRegisterSystemloadNotifier(notifier);
340     SUCCEED();
341 }
342 
343 /**
344  * @tc.name: ResSchedSvcStatusChange add 002
345  * @tc.desc: ResSchedSvcStatusChange OnAddSystemAbility
346  * @tc.type: FUNC
347  * @tc.require: issueI9G149
348  * @tc.author: shanhaiyang
349  */
350 HWTEST_F(ResSchedClientTest, OnAddSystemAbility002, Function | MediumTest | Level0)
351 {
352     ASSERT_TRUE(ResSchedClient::GetInstance().resSchedSvcStatusListener_);
353     std::string empty;
354     ResSchedClient::GetInstance().resSchedSvcStatusListener_->OnAddSystemAbility(RSS_SA_ID, empty);
355     sptr<ResSchedSystemloadNotifierClient> notifier =
356         new (std::nothrow) ResSchedSystemloadNotifierClientMock;
357     EXPECT_TRUE(notifier != nullptr);
358     ResSchedClient::GetInstance().RegisterSystemloadNotifier(notifier);
359     ResSchedClient::GetInstance().UnRegisterSystemloadNotifier(notifier);
360     SUCCEED();
361 }
362 
363 /**
364  * @tc.name: ResSchedSvcStatusChange remove 001
365  * @tc.desc: ResSchedSvcStatusChange OnRemoveSystemAbility
366  * @tc.type: FUNC
367  * @tc.require: issueI9G149
368  * @tc.author: shanhaiyang
369  */
370 HWTEST_F(ResSchedClientTest, OnRemoveSystemAbility001, Function | MediumTest | Level0)
371 {
372     sptr<ResSchedSystemloadNotifierClient> notifier =
373         new (std::nothrow) ResSchedSystemloadNotifierClientMock;
374     EXPECT_TRUE(notifier != nullptr);
375     ResSchedClient::GetInstance().RegisterSystemloadNotifier(notifier);
376     ASSERT_TRUE(ResSchedClient::GetInstance().resSchedSvcStatusListener_);
377     std::string empty;
378     ResSchedClient::GetInstance().resSchedSvcStatusListener_->OnRemoveSystemAbility(OTHER_SA_ID, empty);
379     ResSchedClient::GetInstance().UnRegisterSystemloadNotifier(notifier);
380     SUCCEED();
381 }
382 
383 /**
384  * @tc.name: IsAllowedAppPreload
385  * @tc.desc: Is allowed application preload
386  * @tc.type: FUNC
387  * @tc.require: issueI9C9JN
388  * @tc.author: xiaoshun
389  */
390 HWTEST_F(ResSchedClientTest, IsAllowedAppPreload, Function | MediumTest | Level0)
391 {
392     std::string bundleName = "com.ohos.test";
393     EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
394     EXPECT_TRUE(!ResSchedClient::GetInstance().IsAllowedAppPreload(bundleName, 0));
395 }
396 
397 /**
398  * @tc.name: IsAllowedLinkJump
399  * @tc.desc: Is allowed link jump
400  * @tc.type: FUNC
401  * @tc.require: issueIBE0PK
402  * @tc.author: csc
403  */
404 HWTEST_F(ResSchedClientTest, IsAllowedLinkJump, Function | MediumTest | Level0)
405 {
406     bool isAllowedLinkJump = false;
407     int32_t ret = ResSchedClient::GetInstance().IsAllowedLinkJump(isAllowedLinkJump);
408     EXPECT_NE(ret, 0);
409 }
410 
411 
412 /**
413  * @tc.name: SaInitXmlMutex
414  * @tc.desc: Sa Init Xml Mutex
415  * @tc.type: FUNC
416  * @tc.require: I78O6Y
417  * @tc.author: lujunchao
418  */
419 HWTEST_F(ResSchedClientTest, SaInitXmlMutex, Function | MediumTest | Level0)
420 {
421     std::lock_guard<std::mutex> xmlLock(ResourceSchedule::ResSchedSaInit::GetInstance().saInitXmlMutex_);
422     EXPECT_TRUE(nullptr != ResSchedClient::GetInstance().rss_);
423 }
424 
425 /**
426  * @tc.name: StopRemoteObject
427  * @tc.desc: Stop Remote Object
428  * @tc.type: FUNC
429  * @tc.require: I78O6Y
430  * @tc.author: lujunchao
431  */
432 HWTEST_F(ResSchedClientTest, StopRemoteObject, Function | MediumTest | Level0)
433 {
434     ResSchedClient::GetInstance().StopRemoteObject();
435     EXPECT_TRUE(nullptr == ResSchedClient::GetInstance().rss_);
436 }
437 } // namespace ResourceSchedule
438 } // namespace OHOS
439