1 /*
2 * Copyright (c) 2022-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 #include "gtest/hwext/gtest-multithread.h"
18
19 #define private public
20 #define protected public
21 #include <vector>
22 #include "res_sched_service.h"
23 #include "res_sched_service_ability.h"
24 #include "plugin_mgr.h"
25
26 namespace OHOS {
27 namespace ResourceSchedule {
28 using namespace std;
29 using namespace testing::ext;
30 using namespace testing::mt;
31 class ResSchedServiceTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 protected:
38 std::shared_ptr<ResSchedService> resSchedService_ = nullptr;
39 std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = nullptr;
40 };
41
42
SetUpTestCase(void)43 void ResSchedServiceTest::SetUpTestCase(void) {}
44
TearDownTestCase()45 void ResSchedServiceTest::TearDownTestCase() {}
46
SetUp()47 void ResSchedServiceTest::SetUp()
48 {
49 /**
50 * @tc.setup: initialize the member variable resSchedServiceAbility_
51 */
52 resSchedService_ = make_shared<ResSchedService>();
53 resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
54 }
55
TearDown()56 void ResSchedServiceTest::TearDown()
57 {
58 /**
59 * @tc.teardown: clear resSchedServiceAbility_
60 */
61 resSchedService_ = nullptr;
62 resSchedServiceAbility_ = nullptr;
63 }
64
65 /**
66 * @tc.name: ressched service dump 001
67 * @tc.desc: Verify if ressched service dump commonds is success.
68 * @tc.type: FUNC
69 * @tc.require: issueI5WWV3
70 * @tc.author:lice
71 */
72 HWTEST_F(ResSchedServiceTest, ServiceDump001, Function | MediumTest | Level0)
73 {
74 PluginMgr::GetInstance().Init();
75 std::string result;
76 resSchedService_->DumpAllInfo(result);
77 EXPECT_TRUE(!result.empty());
78
79 result = "";
80 resSchedService_->DumpUsage(result);
81 EXPECT_TRUE(!result.empty());
82
83 int32_t wrongFd = -1;
84 std::vector<std::u16string> argsNull;
85 int res = resSchedService_->Dump(wrongFd, argsNull);
86 EXPECT_TRUE(!res);
87
88 int32_t correctFd = -1;
89 res = resSchedService_->Dump(correctFd, argsNull);
90
91 std::vector<std::u16string> argsHelp = {to_utf16("-h")};
92 res = resSchedService_->Dump(correctFd, argsHelp);
93
94 std::vector<std::u16string> argsAll = {to_utf16("-a")};
95 res = resSchedService_->Dump(correctFd, argsAll);
96
97 std::vector<std::u16string> argsError = {to_utf16("-e")};
98 res = resSchedService_->Dump(correctFd, argsError);
99
100 std::vector<std::u16string> argsPlugin = {to_utf16("-p")};
101 res = resSchedService_->Dump(correctFd, argsPlugin);
102
103 std::vector<std::u16string> argsOnePlugin = {to_utf16("-p"), to_utf16("1")};
104 res = resSchedService_->Dump(correctFd, argsOnePlugin);
105 }
106
107 /**
108 * @tc.name: Ressched service ReportData 001
109 * @tc.desc: Verify if Ressched service ReportData is success.
110 * @tc.type: FUNC
111 * @tc.require: issueI5WWV3
112 * @tc.author:lice
113 */
114 HWTEST_F(ResSchedServiceTest, Report001, Function | MediumTest | Level0)
115 {
116 nlohmann::json payload;
117 EXPECT_TRUE(resSchedService_ != nullptr);
118 resSchedService_->ReportData(0, 0, payload);
119 }
120
ReportTask()121 static void ReportTask()
122 {
123 std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
124 nlohmann::json payload;
125 EXPECT_TRUE(resSchedService_ != nullptr);
126 resSchedService_->ReportData(0, 0, payload);
127 }
128
129 /**
130 * @tc.name: Ressched service ReportData 002
131 * @tc.desc: Test Ressched service ReportData in multithreading.
132 * @tc.type: FUNC
133 * @tc.require: issueI7G8VT
134 * @tc.author: nizihao
135 */
136 HWTEST_F(ResSchedServiceTest, Report002, Function | MediumTest | Level0)
137 {
138 SET_THREAD_NUM(10);
139 GTEST_RUN_TASK(ReportTask);
140 }
141
142 /**
143 * @tc.name: Start ResSchedServiceAbility 001
144 * @tc.desc: Verify if ResSchedServiceAbility OnStart is success.
145 * @tc.type: FUNC
146 * @tc.require: issueI5WWV3
147 * @tc.author:lice
148 */
149 HWTEST_F(ResSchedServiceTest, OnStart001, Function | MediumTest | Level0)
150 {
151 resSchedServiceAbility_->OnStart();
152 EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
153 }
154
OnStartTask()155 static void OnStartTask()
156 {
157 std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
158 resSchedServiceAbility_->OnStart();
159 EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
160 }
161
162 /**
163 * @tc.name: Start ResSchedServiceAbility 002
164 * @tc.desc: Test ResSchedServiceAbility OnStart in multithreading.
165 * @tc.type: FUNC
166 * @tc.require: issueI7G8VT
167 * @tc.author: nizihao
168 */
169 HWTEST_F(ResSchedServiceTest, OnStart002, Function | MediumTest | Level0)
170 {
171 SET_THREAD_NUM(10);
172 GTEST_RUN_TASK(OnStartTask);
173 }
174
175 /**
176 * @tc.name: ResSchedServiceAbility ChangeAbility 001
177 * @tc.desc: Verify if add and remove system ability is success.
178 * @tc.type: FUNC
179 * @tc.require: issueI5WWV3
180 * @tc.author:lice
181 */
182 HWTEST_F(ResSchedServiceTest, ChangeAbility001, Function | MediumTest | Level0)
183 {
184 std::string deviceId;
185 resSchedServiceAbility_->OnAddSystemAbility(-1, deviceId);
186 resSchedServiceAbility_->OnRemoveSystemAbility(-1, deviceId);
187 }
188
ChangeAbilityTask()189 static void ChangeAbilityTask()
190 {
191 std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
192 std::string deviceId;
193 resSchedServiceAbility_->OnAddSystemAbility(-1, deviceId);
194 resSchedServiceAbility_->OnRemoveSystemAbility(-1, deviceId);
195 }
196
197 /**
198 * @tc.name: ResSchedServiceAbility ChangeAbility 002
199 * @tc.desc: Test add and remove system ability in multithreading.
200 * @tc.type: FUNC
201 * @tc.require: issueI7G8VT
202 * @tc.author: nizihao
203 */
204 HWTEST_F(ResSchedServiceTest, ChangeAbility002, Function | MediumTest | Level0)
205 {
206 SET_THREAD_NUM(10);
207 GTEST_RUN_TASK(ChangeAbilityTask);
208 }
209
210 class TestResSchedServiceStub : public ResSchedServiceStub {
211 public:
TestResSchedServiceStub()212 TestResSchedServiceStub() : ResSchedServiceStub() {}
213
ReportData(uint32_t restype,int64_t value,const nlohmann::json & payload)214 void ReportData(uint32_t restype, int64_t value, const nlohmann::json& payload) override
215 {
216 }
217
KillProcess(const nlohmann::json & payload)218 int32_t KillProcess(const nlohmann::json& payload) override
219 {
220 return 0;
221 }
222 };
223
224 /**
225 * @tc.name: ResSchedServicesStub ReportDataInner 001
226 * @tc.desc: Verify if resschedstub reportdatainner is success.
227 * @tc.type: FUNC
228 * @tc.require: issueI5WWV3
229 * @tc.author:lice
230 */
231 HWTEST_F(ResSchedServiceTest, ReportDataInner001, Function | MediumTest | Level0)
232 {
233 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
234 resSchedServiceStub_->Init();
235 MessageParcel reply;
236 MessageParcel emptyData;
237 EXPECT_TRUE(resSchedServiceStub_->ReportDataInner(emptyData, reply));
238
239 MessageParcel reportData;
240 reportData.WriteInterfaceToken(ResSchedServiceStub::GetDescriptor());
241 reportData.WriteUint32(1);
242 reportData.WriteInt64(1);
243 reportData.WriteString("{ { \" uid \" : \" 1 \" } }");
244 EXPECT_TRUE(!resSchedServiceStub_->ReportDataInner(reportData, reply));
245 }
246
ReportDataInnerTask()247 static void ReportDataInnerTask()
248 {
249 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
250 resSchedServiceStub_->Init();
251 MessageParcel reply;
252 MessageParcel emptyData;
253 EXPECT_TRUE(resSchedServiceStub_->ReportDataInner(emptyData, reply));
254
255 MessageParcel reportData;
256 reportData.WriteInterfaceToken(ResSchedServiceStub::GetDescriptor());
257 reportData.WriteUint32(1);
258 reportData.WriteInt64(1);
259 reportData.WriteString("{ { \" uid \" : \" 1 \" } }");
260 EXPECT_TRUE(!resSchedServiceStub_->ReportDataInner(reportData, reply));
261 }
262
263 /**
264 * @tc.name: ResSchedServicesStub ReportDataInner 002
265 * @tc.desc: Test resschedstub reportdatainner in multithreading.
266 * @tc.type: FUNC
267 * @tc.require: issueI7G8VT
268 * @tc.author: nizihao
269 */
270 HWTEST_F(ResSchedServiceTest, ReportDataInner002, Function | MediumTest | Level0)
271 {
272 SET_THREAD_NUM(10);
273 GTEST_RUN_TASK(ReportDataInnerTask);
274 }
275
276 /**
277 * @tc.name: ResSchedServicesStub StringToJson 001
278 * @tc.desc: Verify if resschedstub StringToJson is success.
279 * @tc.type: FUNC
280 * @tc.require: issueI5WWV3
281 * @tc.author:lice
282 */
283 HWTEST_F(ResSchedServiceTest, StringToJson001, Function | MediumTest | Level0)
284 {
285 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
286 nlohmann::json res = resSchedServiceStub_->StringToJsonObj("");
287 EXPECT_TRUE(!res.dump().empty());
288 }
289
StringToJsonTask()290 static void StringToJsonTask()
291 {
292 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
293 nlohmann::json res = resSchedServiceStub_->StringToJsonObj("");
294 EXPECT_TRUE(!res.dump().empty());
295 }
296
297 /**
298 * @tc.name: ResSchedServicesStub StringToJson 002
299 * @tc.desc: Test resschedstub StringToJson in multithreading.
300 * @tc.type: FUNC
301 * @tc.require: issueI7G8VT
302 * @tc.author: nizihao
303 */
304 HWTEST_F(ResSchedServiceTest, StringToJson002, Function | MediumTest | Level0)
305 {
306 SET_THREAD_NUM(10);
307 GTEST_RUN_TASK(StringToJsonTask);
308 }
309
310 /**
311 * @tc.name: ResSchedServicesStub RemoteRequest 001
312 * @tc.desc: Verify if resschedstub RemoteRequest is success.
313 * @tc.type: FUNC
314 * @tc.require: issueI5WWV3 issueI6D6BM
315 * @tc.author:lice
316 */
317 HWTEST_F(ResSchedServiceTest, RemoteRequest001, Function | MediumTest | Level0)
318 {
319 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
320 MessageOption option;
321 MessageParcel reply;
322 int32_t res = resSchedServiceStub_->OnRemoteRequest(1, reply, reply, option);
323 EXPECT_TRUE(res);
324 res = resSchedServiceStub_->OnRemoteRequest(2, reply, reply, option);
325 EXPECT_TRUE(res);
326 res = resSchedServiceStub_->OnRemoteRequest(0, reply, reply, option);
327 EXPECT_TRUE(res);
328 }
329
RemoteRequestTask()330 static void RemoteRequestTask()
331 {
332 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
333 MessageOption option;
334 MessageParcel reply;
335 int32_t res = resSchedServiceStub_->OnRemoteRequest(1, reply, reply, option);
336 EXPECT_TRUE(res);
337 res = resSchedServiceStub_->OnRemoteRequest(2, reply, reply, option);
338 EXPECT_TRUE(res);
339 res = resSchedServiceStub_->OnRemoteRequest(0, reply, reply, option);
340 EXPECT_TRUE(res);
341 }
342
343 /**
344 * @tc.name: ResSchedServicesStub RemoteRequest 002
345 * @tc.desc: Test resschedstub RemoteRequest in multithreading.
346 * @tc.type: FUNC
347 * @tc.require: issueI7G8VT
348 * @tc.author: nizihao
349 */
350 HWTEST_F(ResSchedServiceTest, RemoteRequest002, Function | MediumTest | Level0)
351 {
352 SET_THREAD_NUM(10);
353 GTEST_RUN_TASK(RemoteRequestTask);
354 }
355 #undef private
356 #undef protected
357 } // namespace ResourceSchedule
358 } // namespace OHOS
359