1 /*
2 * Copyright (c) 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
21 #include "accesstoken_kit.h"
22 #include "mock_ipc_skeleton.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25
26 #include "plugin_mgr.h"
27 #include "res_exe_type.h"
28 #include "res_common_util.h"
29 #include "res_json_type.h"
30 #include "res_sched_exe_constants.h"
31 #include "res_sched_exe_service.h"
32
33 namespace OHOS {
34 namespace ResourceSchedule {
35 using namespace std;
36 using namespace testing::ext;
37 using namespace testing::mt;
38 using namespace Security::AccessToken;
39
40 namespace {
41 constexpr int32_t SYNC_THREAD_NUM = 100;
42 constexpr int32_t SYNC_INTERNAL_TIME = 10000;
43 constexpr int32_t RSS_UID = 1096;
44 const std::string RES_TYPE_EXT = "extType";
45 }
46
47 class ResSchedExeServiceTest : public testing::Test {
48 public:
49 static void SetUpTestCase(void);
50 static void TearDownTestCase(void);
51 void SetUp();
52 void TearDown();
53 protected:
54 std::shared_ptr<ResSchedExeService> resSchedExeService_ = nullptr;
55 };
56
SetUpTestCase(void)57 void ResSchedExeServiceTest::SetUpTestCase(void)
58 {
59 static const char *perms[] = {
60 "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT",
61 "ohos.permission.DUMP",
62 };
63 uint64_t tokenId;
64 NativeTokenInfoParams infoInstance = {
65 .dcapsNum = 0,
66 .permsNum = 2,
67 .aclsNum = 0,
68 .dcaps = nullptr,
69 .perms = perms,
70 .acls = nullptr,
71 .processName = "ResSchedExeServiceTest",
72 .aplStr = "system_core",
73 };
74 tokenId = GetAccessTokenId(&infoInstance);
75 SetSelfTokenID(tokenId);
76 AccessTokenKit::ReloadNativeTokenInfo();
77 }
78
TearDownTestCase()79 void ResSchedExeServiceTest::TearDownTestCase() {}
80
SetUp()81 void ResSchedExeServiceTest::SetUp()
82 {
83 /**
84 * @tc.setup: initialize the member variable resSchedExeServiceAbility_
85 */
86 resSchedExeService_ = make_shared<ResSchedExeService>();
87 }
88
TearDown()89 void ResSchedExeServiceTest::TearDown()
90 {
91 /**
92 * @tc.teardown: clear resSchedExeServiceAbility_
93 */
94 resSchedExeService_ = nullptr;
95 }
96
97 /**
98 * @tc.name: resschedexe service dump 001
99 * @tc.desc: Verify if resschedexe service dump commonds is success.
100 * @tc.type: FUNC
101 */
102 HWTEST_F(ResSchedExeServiceTest, ServiceDump001, Function | MediumTest | Level0)
103 {
104 PluginMgr::GetInstance().Init(true);
105 std::string result;
106 resSchedExeService_->DumpAllInfo(result);
107 EXPECT_TRUE(!result.empty());
108
109 result = "";
110 resSchedExeService_->DumpUsage(result);
111 EXPECT_TRUE(!result.empty());
112
113 int32_t wrongFd = -1;
114 std::vector<std::u16string> argsNull;
115 int res = resSchedExeService_->Dump(wrongFd, argsNull);
116 EXPECT_NE(res, ResErrCode::RSSEXE_NO_ERR);
117
118 int32_t correctFd = 0;
119 res = resSchedExeService_->Dump(correctFd, argsNull);
120
121 std::vector<std::u16string> argsHelp = {to_utf16("-h")};
122 res = resSchedExeService_->Dump(correctFd, argsHelp);
123
124 std::vector<std::u16string> argsAll = {to_utf16("-a")};
125 res = resSchedExeService_->Dump(correctFd, argsAll);
126
127 std::vector<std::u16string> argsError = {to_utf16("-e")};
128 res = resSchedExeService_->Dump(correctFd, argsError);
129
130 std::vector<std::u16string> argsPlugin = {to_utf16("-p")};
131 res = resSchedExeService_->Dump(correctFd, argsPlugin);
132
133 std::vector<std::u16string> argsOnePlugin = {to_utf16("-p"), to_utf16("1")};
134 res = resSchedExeService_->Dump(correctFd, argsOnePlugin);
135 }
136
137 /**
138 * @tc.name: Ressched_executor service SendRequestSync001
139 * @tc.desc: Verify if Ressched_executor service SendRequestSync is success.
140 * @tc.type: FUNC
141 */
142 HWTEST_F(ResSchedExeServiceTest, SendRequestSync001, Function | MediumTest | Level0)
143 {
144 ResJsonType payload;
145 ResJsonType reply;
146 int32_t result = -1;
147
148 EXPECT_TRUE(resSchedExeService_ != nullptr);
149 resSchedExeService_->SendRequestSync(0, 0, payload, reply, result);
150 }
151
SendRequestSyncTask()152 static void SendRequestSyncTask()
153 {
154 std::shared_ptr<ResSchedExeService> resSchedExeService_ = make_shared<ResSchedExeService>();
155 ResJsonType payload;
156 ResJsonType reply;
157 int32_t result = -1;
158
159 EXPECT_TRUE(resSchedExeService_ != nullptr);
160 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
161 resSchedExeService_->SendRequestSync(0, 0, payload, reply, result);
162 usleep(SYNC_INTERNAL_TIME);
163 }
164 }
165
166 /**
167 * @tc.name: Ressched_executor service SendRequestSync002
168 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
169 * @tc.type: FUNC
170 */
171 HWTEST_F(ResSchedExeServiceTest, SendRequestSync002, Function | MediumTest | Level0)
172 {
173 SET_THREAD_NUM(10);
174 GTEST_RUN_TASK(SendRequestSyncTask);
175 }
176
177 /**
178 * @tc.name: Ressched_executor service SendRequestSync003
179 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
180 * @tc.type: FUNC
181 */
182 HWTEST_F(ResSchedExeServiceTest, SendRequestSync003, Function | MediumTest | Level0)
183 {
184 ResJsonType context;
185 nlohmann::json testJsonContext;
186 testJsonContext["test"] = "test";
187 context.jsonContext = testJsonContext;
188 ResJsonType response;
189 int32_t funcResult = -1;
190 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
191 ErrCode code = resSchedExeService_->SendRequestSync(
192 -1, 0, context, response, funcResult);
193 EXPECT_TRUE(code == ERR_INVALID_VALUE);
194 }
195
196 /**
197 * @tc.name: Ressched_executor service SendRequestSync004
198 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
199 * @tc.type: FUNC
200 */
201 HWTEST_F(ResSchedExeServiceTest, SendRequestSync004, Function | MediumTest | Level0)
202 {
203 ResJsonType context;
204 nlohmann::json testJsonContext;
205 testJsonContext["test"] = "test";
206 context.jsonContext = testJsonContext;
207 ResJsonType response;
208 int32_t funcResult = -1;
209 IPCSkeleton::SetCallingUid(100); //mock for IPCSkeleton::GetCallingUid
210 ErrCode code = resSchedExeService_->SendRequestSync(
211 -1, 0, context, response, funcResult);
212 EXPECT_TRUE(code == ERR_INVALID_OPERATION);
213 }
214
215 /**
216 * @tc.name: Ressched_executor service SendRequestSync005
217 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
218 * @tc.type: FUNC
219 */
220 HWTEST_F(ResSchedExeServiceTest, SendRequestSync005, Function | MediumTest | Level0)
221 {
222 ResJsonType context;
223 nlohmann::json testJsonContext;
224 testJsonContext["test"] = "test";
225 context.jsonContext = testJsonContext;
226 ResJsonType response;
227 int32_t funcResult = -1;
228 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
229 ErrCode code = resSchedExeService_->SendRequestSync(
230 20000, 1, context, response, funcResult);
231 EXPECT_TRUE(code == ERR_INVALID_VALUE);
232 }
233
234 /**
235 * @tc.name: Ressched_executor service SendRequestSync006
236 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
237 * @tc.type: FUNC
238 */
239 HWTEST_F(ResSchedExeServiceTest, SendRequestSync006, Function | MediumTest | Level0)
240 {
241 ResJsonType context;
242 nlohmann::json testJsonContext;
243 testJsonContext["test"] = "test";
244 context.jsonContext = testJsonContext;
245 ResJsonType response;
246 int32_t funcResult = -1;
247 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
248 ErrCode code = resSchedExeService_->SendRequestSync(
249 ResExeType::RES_TYPE_COMMON_SYNC, 1, context, response, funcResult);
250 EXPECT_TRUE(code == ERR_INVALID_VALUE);
251 }
252
253 /**
254 * @tc.name: Ressched_executor service SendRequestSync007
255 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
256 * @tc.type: FUNC
257 */
258 HWTEST_F(ResSchedExeServiceTest, SendRequestSync007, Function | MediumTest | Level0)
259 {
260 ResJsonType context;
261 nlohmann::json testJsonContext;
262 testJsonContext["test"] = "test";
263 context.jsonContext = testJsonContext;
264 ResJsonType response;
265 int32_t funcResult = -1;
266 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
267 ErrCode code = resSchedExeService_->SendRequestSync(
268 ResExeType::RES_TYPE_COMMON_ASYNC, 1, context, response, funcResult);
269 EXPECT_TRUE(code == ERR_INVALID_VALUE);
270 }
271
272 /**
273 * @tc.name: Ressched_executor service SendRequestSync008
274 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
275 * @tc.type: FUNC
276 */
277 HWTEST_F(ResSchedExeServiceTest, SendRequestSync008, Function | MediumTest | Level0)
278 {
279 ResJsonType context;
280 nlohmann::json testJsonContext;
281 testJsonContext[RES_TYPE_EXT] = RES_TYPE_EXT;
282 context.jsonContext = testJsonContext;
283 ResJsonType response;
284 int32_t funcResult = 100;
285 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
286 ErrCode code = resSchedExeService_->SendRequestSync(
287 100, 0, context, response, funcResult);
288 EXPECT_TRUE(code == ERR_OK);
289 }
290
291 /**
292 * @tc.name: Ressched_executor service SendRequestSync009
293 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
294 * @tc.type: FUNC
295 */
296 HWTEST_F(ResSchedExeServiceTest, SendRequestSync009, Function | MediumTest | Level0)
297 {
298 ResJsonType context;
299 nlohmann::json testJsonContext;
300 testJsonContext[RES_TYPE_EXT] = 100;
301 context.jsonContext = testJsonContext;
302 ResJsonType response;
303 int32_t funcResult = 100;
304 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
305 ErrCode code = resSchedExeService_->SendRequestSync(
306 100, 0, context, response, funcResult);
307 EXPECT_TRUE(code == ERR_OK);
308 }
309
310 /**
311 * @tc.name: Ressched_executor service SendRequestAsync001
312 * @tc.desc: Verify if Ressched_executor service SendRequestAsync is success.
313 * @tc.type: FUNC
314 */
315 HWTEST_F(ResSchedExeServiceTest, SendRequestAsync001, Function | MediumTest | Level0)
316 {
317 ResJsonType payload;
318 EXPECT_TRUE(resSchedExeService_ != nullptr);
319 resSchedExeService_->SendRequestAsync(0, 0, payload);
320 }
321
322 /**
323 * @tc.name: Ressched_executor service SendRequestAsync003
324 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
325 * @tc.type: FUNC
326 */
327 HWTEST_F(ResSchedExeServiceTest, SendRequestAsync003, Function | MediumTest | Level0)
328 {
329 ResJsonType context;
330 nlohmann::json testJsonContext;
331 testJsonContext["test"] = "test";
332 context.jsonContext = testJsonContext;
333 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
334 ErrCode code = resSchedExeService_->SendRequestAsync(0, 0, context);
335 EXPECT_TRUE(code == ERR_INVALID_VALUE);
336 }
337
338 /**
339 * @tc.name: Ressched_executor service SendRequestAsync004
340 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
341 * @tc.type: FUNC
342 */
343 HWTEST_F(ResSchedExeServiceTest, SendRequestAsync004, Function | MediumTest | Level0)
344 {
345 ResJsonType context;
346 nlohmann::json testJsonContext;
347 testJsonContext["test"] = "test";
348 context.jsonContext = testJsonContext;
349 IPCSkeleton::SetCallingUid(100); //mock for IPCSkeleton::GetCallingUid
350 ErrCode code = resSchedExeService_->SendRequestAsync(0, 0, context);
351 EXPECT_TRUE(code == ERR_INVALID_OPERATION);
352 }
353
354 /**
355 * @tc.name: Ressched_executor service SendRequestAsync005
356 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
357 * @tc.type: FUNC
358 */
359 HWTEST_F(ResSchedExeServiceTest, SendRequestAsync005, Function | MediumTest | Level0)
360 {
361 ResJsonType context;
362 nlohmann::json testJsonContext;
363 testJsonContext["test"] = "test";
364 context.jsonContext = testJsonContext;
365 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
366 ErrCode code = resSchedExeService_->SendRequestAsync(20000, 1, context);
367 EXPECT_TRUE(code == ERR_INVALID_VALUE);
368 }
369
370 /**
371 * @tc.name: Ressched_executor service SendRequestAsync006
372 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
373 * @tc.type: FUNC
374 */
375 HWTEST_F(ResSchedExeServiceTest, SendRequestAsync006, Function | MediumTest | Level0)
376 {
377 ResJsonType context;
378 nlohmann::json testJsonContext;
379 testJsonContext["test"] = "test";
380 context.jsonContext = testJsonContext;
381 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
382 ErrCode code = resSchedExeService_->SendRequestAsync(
383 ResExeType::RES_TYPE_COMMON_SYNC, 1, context);
384 EXPECT_TRUE(code == ERR_INVALID_VALUE);
385 }
386
387 /**
388 * @tc.name: Ressched_executor service SendRequestAsync007
389 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
390 * @tc.type: FUNC
391 */
392 HWTEST_F(ResSchedExeServiceTest, SendRequestAsync007, Function | MediumTest | Level0)
393 {
394 ResJsonType context;
395 nlohmann::json testJsonContext;
396 testJsonContext["test"] = "test";
397 context.jsonContext = testJsonContext;
398 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
399 ErrCode code = resSchedExeService_->SendRequestAsync(
400 ResExeType::RES_TYPE_COMMON_ASYNC, 1, context);
401 EXPECT_TRUE(code == ERR_INVALID_VALUE);
402 }
403
404 /**
405 * @tc.name: Ressched_executor service SendRequestAsync008
406 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
407 * @tc.type: FUNC
408 */
409 HWTEST_F(ResSchedExeServiceTest, SendRequestAsync008, Function | MediumTest | Level0)
410 {
411 ResJsonType context;
412 nlohmann::json testJsonContext;
413 testJsonContext[RES_TYPE_EXT] = RES_TYPE_EXT;
414 context.jsonContext = testJsonContext;
415 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
416 ErrCode code = resSchedExeService_->SendRequestAsync(100, 0, context);
417 EXPECT_TRUE(code == ERR_OK);
418 }
419
420 /**
421 * @tc.name: Ressched_executor service SendRequestAsync009
422 * @tc.desc: Test Ressched_executor service SendRequestSync in multithreading.
423 * @tc.type: FUNC
424 */
425 HWTEST_F(ResSchedExeServiceTest, SendRequestAsync009, Function | MediumTest | Level0)
426 {
427 ResJsonType context;
428 nlohmann::json testJsonContext;
429 testJsonContext[RES_TYPE_EXT] = 100;
430 context.jsonContext = testJsonContext;
431 IPCSkeleton::SetCallingUid(RSS_UID); //mock for IPCSkeleton::GetCallingUid
432 ErrCode code = resSchedExeService_->SendRequestAsync(100, 0, context);
433 EXPECT_TRUE(code == ERR_OK);
434 }
435
SendRequestAsyncTask()436 static void SendRequestAsyncTask()
437 {
438 std::shared_ptr<ResSchedExeService> resSchedExeService_ = make_shared<ResSchedExeService>();
439 ResJsonType payload;
440 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
441 EXPECT_TRUE(resSchedExeService_ != nullptr);
442 resSchedExeService_->SendRequestAsync(0, 0, payload);
443 usleep(SYNC_INTERNAL_TIME);
444 }
445 }
446
447 /**
448 * @tc.name: Ressched_executor service SendRequestAsync002
449 * @tc.desc: Test Ressched_executor service SendRequestAsync in multithreading.
450 * @tc.type: FUNC
451 */
452 HWTEST_F(ResSchedExeServiceTest, SendRequestAsync002, Function | MediumTest | Level0)
453 {
454 SET_THREAD_NUM(10);
455 GTEST_RUN_TASK(SendRequestAsyncTask);
456 }
457
OnStartTask()458 static void OnStartTask()
459 {
460 std::shared_ptr<ResSchedExeService> resSchedExeService_ = make_shared<ResSchedExeService>();
461 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
462 resSchedExeService_->OnStart();
463 EXPECT_TRUE(resSchedExeService_ != nullptr);
464 usleep(SYNC_INTERNAL_TIME);
465 }
466 }
467
468 /**
469 * @tc.name: Start ResSchedExeServiceAbility OnStart002
470 * @tc.desc: Test ResSchedExeServiceAbility OnStart in multithreading.
471 * @tc.type: FUNC
472 */
473 HWTEST_F(ResSchedExeServiceTest, OnStart002, Function | MediumTest | Level0)
474 {
475 SET_THREAD_NUM(10);
476 GTEST_RUN_TASK(OnStartTask);
477 }
478
479 class TestResSchedExeServiceStub : public ResSchedExeServiceStub {
480 public:
TestResSchedExeServiceStub()481 TestResSchedExeServiceStub() : ResSchedExeServiceStub() {}
482
SendRequestAsync(uint32_t resType,int64_t value,const ResJsonType & context)483 ErrCode SendRequestAsync(uint32_t resType, int64_t value, const ResJsonType& context) override
484 {
485 return ERR_OK;
486 }
487
SendRequestSync(uint32_t resType,int64_t value,const ResJsonType & context,ResJsonType & response,int32_t & funcResult)488 ErrCode SendRequestSync(uint32_t resType, int64_t value,
489 const ResJsonType& context, ResJsonType& response, int32_t& funcResult) override
490 {
491 return ERR_OK;
492 }
493
KillProcess(uint32_t pid,int32_t & funcResult)494 ErrCode KillProcess(uint32_t pid, int32_t& funcResult) override
495 {
496 return ERR_OK;
497 }
498 };
499
500 /**
501 * @tc.name: ResSchedExeServicesStub RemoteRequest001
502 * @tc.desc: Verify if resschedexestub RemoteRequest is success.
503 * @tc.type: FUNC
504 */
505 HWTEST_F(ResSchedExeServiceTest, RemoteRequest001, Function | MediumTest | Level0)
506 {
507 auto resSchedExeServiceStub_ = make_shared<TestResSchedExeServiceStub>();
508 MessageOption option;
509 MessageParcel reply;
510 int32_t res = resSchedExeServiceStub_->OnRemoteRequest(ResIpcType::REQUEST_SEND_SYNC, reply, reply, option);
511 EXPECT_TRUE(res);
512 res = resSchedExeServiceStub_->OnRemoteRequest(ResIpcType::REQUEST_SEND_ASYNC, reply, reply, option);
513 EXPECT_TRUE(res);
514 res = resSchedExeServiceStub_->OnRemoteRequest(ResIpcType::REQUEST_SEND_DEBUG, reply, reply, option);
515 EXPECT_TRUE(res);
516 }
517
RemoteRequestTask()518 static void RemoteRequestTask()
519 {
520 auto resSchedExeServiceStub_ = make_shared<TestResSchedExeServiceStub>();
521 MessageOption option;
522 MessageParcel reply;
523 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
524 int32_t res = resSchedExeServiceStub_->OnRemoteRequest(ResIpcType::REQUEST_SEND_SYNC, reply, reply, option);
525 EXPECT_TRUE(res);
526 res = resSchedExeServiceStub_->OnRemoteRequest(ResIpcType::REQUEST_SEND_ASYNC, reply, reply, option);
527 EXPECT_TRUE(res);
528 res = resSchedExeServiceStub_->OnRemoteRequest(ResIpcType::REQUEST_SEND_DEBUG, reply, reply, option);
529 EXPECT_TRUE(res);
530 usleep(SYNC_INTERNAL_TIME);
531 }
532 }
533
534 /**
535 * @tc.name: ResSchedExeServicesStub RemoteRequest002
536 * @tc.desc: Test resschedexestub RemoteRequest in multithreading.
537 * @tc.type: FUNC
538 */
539 HWTEST_F(ResSchedExeServiceTest, RemoteRequest002, Function | MediumTest | Level0)
540 {
541 SET_THREAD_NUM(10);
542 GTEST_RUN_TASK(RemoteRequestTask);
543 }
544 } // namespace ResourceSchedule
545 } // namespace OHOS
546