1 /*
2 * Copyright (c) 2023 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
18 #define private public
19 #define protected public
20 #include <unordered_map>
21 #include <vector>
22 #include "nativetoken_kit.h"
23 #include "res_sched_client.h"
24 #include "token_setproc.h"
25
26 namespace OHOS {
27 namespace ResourceSchedule {
28 using namespace std;
29 using namespace testing::ext;
30 class ResSchedClientTest : public testing::Test {
31 public:
32 static void SetUpTestCase(void);
33 static void TearDownTestCase(void);
34 void SetUp();
35 void TearDown();
36 void MockProcess(std::string processName);
37 };
38
39
SetUpTestCase(void)40 void ResSchedClientTest::SetUpTestCase(void) {}
41
TearDownTestCase()42 void ResSchedClientTest::TearDownTestCase() {}
43
SetUp()44 void ResSchedClientTest::SetUp() {}
45
TearDown()46 void ResSchedClientTest::TearDown() {}
47
MockProcess(std::string processName)48 void ResSchedClientTest::MockProcess(std::string processName)
49 {
50 static const char *perms[] = {
51 "ohos.permission.DISTRIBUTED_DATASYNC"
52 };
53 uint64_t tokenId;
54 NativeTokenInfoParams infoInstance = {
55 .dcapsNum = 0,
56 .permsNum = 1,
57 .aclsNum = 0,
58 .dcaps = nullptr,
59 .perms = perms,
60 .acls = nullptr,
61 .processName = processName.c_str(),
62 .aplStr = "system_core",
63 };
64 tokenId = GetAccessTokenId(&infoInstance);
65 SetSelfTokenID(tokenId);
66 }
67
68 /**
69 * @tc.name: KillProcess001
70 * @tc.desc: kill process stable test
71 * @tc.type: FUNC
72 * @tc.require: I6EEJI
73 * @tc.author: qiunaiguang
74 */
75 HWTEST_F(ResSchedClientTest, KillProcess001, Function | MediumTest | Level0)
76 {
77 std::string processName = "samgr";
78 MockProcess(processName);
79 std::unordered_map<std::string, std::string> mapPayload;
80 mapPayload["pid"] = "65535";
81 mapPayload["processName"] = "test";
82 for (int i = 0; i < 100; i++) {
83 ResSchedClient::GetInstance().KillProcess(mapPayload);
84 }
85 EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
86 }
87
88 /**
89 * @tc.name: KillProcess002
90 * @tc.desc: kill process error test
91 * @tc.type: FUNC
92 * @tc.require: I6EEJI
93 * @tc.author: qiunaiguang
94 */
95 HWTEST_F(ResSchedClientTest, KillProcess002, Function | MediumTest | Level0)
96 {
97 std::string processName = "samgr";
98 MockProcess(processName);
99 std::unordered_map<std::string, std::string> mapPayload;
100 ResSchedClient::GetInstance().KillProcess(mapPayload);
101 EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
102
103 mapPayload["pid"] = "TEST";
104 ResSchedClient::GetInstance().KillProcess(mapPayload);
105 EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
106
107 mapPayload["pid"] = "65535";
108 mapPayload["processName"] = "test";
109 ResSchedClient::GetInstance().KillProcess(mapPayload);
110 EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
111
112 processName = "resource_schedule_service";
113 MockProcess(processName);
114 ResSchedClient::GetInstance().KillProcess(mapPayload);
115 EXPECT_TRUE(ResSchedClient::GetInstance().rss_);
116 }
117
118 /**
119 * @tc.name: StopRemoteObject
120 * @tc.desc: Stop Remote Object
121 * @tc.type: FUNC
122 * @tc.require: I78O6Y
123 * @tc.author: lujunchao
124 */
125 HWTEST_F(ResSchedClientTest, StopRemoteObject, Function | MediumTest | Level0)
126 {
127 ResSchedClient::GetInstance().StopRemoteObject();
128 EXPECT_TRUE(nullptr == ResSchedClient::GetInstance().rss_);
129 }
130
131 #undef private
132 #undef protected
133 } // namespace ResourceSchedule
134 } // namespace OHOS
135