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
18 #include <unordered_map>
19 #include <vector>
20
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23
24 #include "res_exe_type.h"
25 #include "res_sched_exe_client.h"
26 #include "res_sched_exe_constants.h"
27
28 namespace OHOS {
29 namespace ResourceSchedule {
30 using namespace std;
31 using namespace testing::ext;
32
33 namespace {
34 constexpr int32_t SYNC_THREAD_NUM = 100;
35 constexpr int32_t SYNC_INTERNAL_TIME = 200;
36 constexpr int32_t IPC_MESSAGE_SIZE = 220000;
37 }
38
39 class ResSchedExeClientTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp();
44 void TearDown();
45 void MockProcess(int32_t uid);
46 };
47
48
SetUpTestCase(void)49 void ResSchedExeClientTest::SetUpTestCase(void) {}
50
TearDownTestCase()51 void ResSchedExeClientTest::TearDownTestCase() {}
52
SetUp()53 void ResSchedExeClientTest::SetUp() {}
54
TearDown()55 void ResSchedExeClientTest::TearDown() {}
56
MockProcess(int32_t uid)57 void ResSchedExeClientTest::MockProcess(int32_t uid)
58 {
59 static const char *perms[] = {
60 "ohos.permission.DISTRIBUTED_DATASYNC"
61 };
62 uint64_t tokenId;
63 NativeTokenInfoParams infoInstance = {
64 .dcapsNum = 0,
65 .permsNum = 1,
66 .aclsNum = 0,
67 .dcaps = nullptr,
68 .perms = perms,
69 .acls = nullptr,
70 .processName = "samgr",
71 .aplStr = "system_core",
72 };
73 tokenId = GetAccessTokenId(&infoInstance);
74 SetSelfTokenID(tokenId);
75 setuid(uid);
76 }
77
78 /**
79 * @tc.name: SendRequestSync001
80 * @tc.desc: send res request stable test
81 * @tc.type: FUNC
82 */
83 HWTEST_F(ResSchedExeClientTest, SendRequestSync001, Function | MediumTest | Level0)
84 {
85 nlohmann::json context;
86 context["message"] = "test";
87 nlohmann::json reply;
88 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
89 ResSchedExeClient::GetInstance().SendRequestSync(ResExeType::RES_TYPE_DEBUG, 0, context, reply);
90 usleep(SYNC_INTERNAL_TIME);
91 }
92 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
93 }
94
95 /**
96 * @tc.name: SendRequestSync002
97 * @tc.desc: send res request stable test
98 * @tc.type: FUNC
99 */
100 HWTEST_F(ResSchedExeClientTest, SendRequestSync002, Function | MediumTest | Level0)
101 {
102 nlohmann::json context;
103 std::string msg(IPC_MESSAGE_SIZE, 'a');
104 context["message"] = msg;
105 nlohmann::json reply;
106 for (int i = 0; i < (SYNC_THREAD_NUM / 2); i++) {
107 ResSchedExeClient::GetInstance().SendRequestSync(ResExeType::RES_TYPE_EXECUTOR_PLUGIN_INIT,
108 0, context, reply);
109 usleep(SYNC_INTERNAL_TIME);
110 }
111 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
112 }
113
114 /**
115 * @tc.name: SendRequestAsync001
116 * @tc.desc: report data stable test
117 * @tc.type: FUNC
118 */
119 HWTEST_F(ResSchedExeClientTest, SendRequestAsync001, Function | MediumTest | Level0)
120 {
121 nlohmann::json context;
122 context["message"] = "test";
123 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
124 ResSchedExeClient::GetInstance().SendRequestAsync(ResExeType::RES_TYPE_DEBUG, 0, context);
125 usleep(SYNC_INTERNAL_TIME);
126 }
127 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
128 }
129
130 /**
131 * @tc.name: SendRequestAsync002
132 * @tc.desc: report data stable test
133 * @tc.type: FUNC
134 */
135 HWTEST_F(ResSchedExeClientTest, SendRequestAsync002, Function | MediumTest | Level0)
136 {
137 nlohmann::json context;
138 std::string msg(IPC_MESSAGE_SIZE, 'a');
139 context["message"] = msg;
140 nlohmann::json reply;
141 for (int i = 0; i < (SYNC_THREAD_NUM / 2); i++) {
142 ResSchedExeClient::GetInstance().SendRequestAsync(ResExeType::RES_TYPE_EXECUTOR_PLUGIN_INIT, 0, context);
143 usleep(SYNC_INTERNAL_TIME);
144 }
145 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
146 }
147
148 /**
149 * @tc.name: KillProcess001
150 * @tc.desc: kill process stable test
151 * @tc.type: FUNC
152 */
153 HWTEST_F(ResSchedExeClientTest, KillProcess001, Function | MediumTest | Level0)
154 {
155 int32_t uid = 5555;
156 MockProcess(uid);
157 int32_t pid = 65535;
158 for (int i = 0; i < 100; i++) {
159 ResSchedExeClient::GetInstance().KillProcess(pid);
160 usleep(SYNC_INTERNAL_TIME);
161 }
162 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
163 }
164
165 /**
166 * @tc.name: KillProcess002
167 * @tc.desc: kill process error test
168 * @tc.type: FUNC
169 */
170 HWTEST_F(ResSchedExeClientTest, KillProcess002, Function | MediumTest | Level0)
171 {
172 int32_t uid = 5555;
173 MockProcess(uid);
174
175 int32_t pid = -1;
176 std::unordered_map<std::string, std::string> mapPayload;
177 ResSchedExeClient::GetInstance().KillProcess(pid);
178 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
179
180 pid = 65535;
181 ResSchedExeClient::GetInstance().KillProcess(pid);
182 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
183 }
184
185 /**
186 * @tc.name: SendDebugCommand001
187 * @tc.desc: send debug command stable test
188 * @tc.type: FUNC
189 */
190 HWTEST_F(ResSchedExeClientTest, SendDebugCommand001, Function | MediumTest | Level0)
191 {
192 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
193 ResSchedExeClient::GetInstance().SendDebugCommand(true);
194 usleep(SYNC_INTERNAL_TIME);
195 }
196 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
197 }
198
199 /**
200 * @tc.name: SendDebugCommand002
201 * @tc.desc: send debug command stable test
202 * @tc.type: FUNC
203 */
204 HWTEST_F(ResSchedExeClientTest, SendDebugCommand002, Function | MediumTest | Level0)
205 {
206 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
207 ResSchedExeClient::GetInstance().SendDebugCommand(false);
208 usleep(SYNC_INTERNAL_TIME);
209 }
210 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
211 }
212
213 /**
214 * @tc.name: StopRemoteObject001
215 * @tc.desc: Stop Remote Object
216 * @tc.type: FUNC
217 */
218 HWTEST_F(ResSchedExeClientTest, StopRemoteObject001, Function | MediumTest | Level0)
219 {
220 ResSchedExeClient::GetInstance().StopRemoteObject();
221 EXPECT_TRUE(nullptr == ResSchedExeClient::GetInstance().resSchedExe_);
222 }
223 } // namespace ResourceSchedule
224 } // namespace OHOS
225