1 /*
2 * Copyright (c) 2022 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 "distributed_ability_manager_dumper_test.h"
17
18 #include "distributed_ability_manager_dumper.h"
19 #include "dtbschedmgr_log.h"
20 #include "nativetoken_kit.h"
21 #include "test_log.h"
22 #include "token_setproc.h"
23
24 namespace OHOS {
25 namespace DistributedSchedule {
26 using namespace testing;
27 using namespace testing::ext;
28 namespace {
29 constexpr int32_t DISTRIBUTED_SCHED_SA_ID = 1401;
30 const char* HIDUMPER_PROCESS_NAME = "hidumper_service";
31 const char* DISTSCHED_PROCESS_NAME = "distributedsched";
32 const char* INVALID_PROCESS_NAME = "invalid_process";
33 bool g_mockResult = false;
34 }
35
MockDistributedSchedDumpFunc(const std::vector<std::string> & args,std::string & result)36 static bool MockDistributedSchedDumpFunc(const std::vector<std::string>& args, std::string& result)
37 {
38 return g_mockResult;
39 }
MockProcess(const char * processName)40 static void MockProcess(const char* processName)
41 {
42 uint64_t tokenId;
43 NativeTokenInfoParams infoInstance = {
44 .dcapsNum = 0,
45 .permsNum = 0,
46 .aclsNum = 0,
47 .dcaps = nullptr,
48 .perms = nullptr,
49 .acls = nullptr,
50 .processName = processName,
51 .aplStr = "system_core",
52 };
53 tokenId = GetAccessTokenId(&infoInstance);
54 SetSelfTokenID(tokenId);
55 }
56
SetUpTestCase()57 void DistributedAbilityManagerDumperTest::SetUpTestCase()
58 {
59 DTEST_LOG << "DistributedAbilityManagerDumperTest::SetUpTestCase" << std::endl;
60 }
61
TearDownTestCase()62 void DistributedAbilityManagerDumperTest::TearDownTestCase()
63 {
64 DTEST_LOG << "DistributedAbilityManagerDumperTest::TearDownTestCase" << std::endl;
65 }
66
SetUp()67 void DistributedAbilityManagerDumperTest::SetUp()
68 {
69 dtbabilitymgrService_ = new DistributedAbilityManagerService(DISTRIBUTED_SCHED_SA_ID, true);
70 MockProcess(HIDUMPER_PROCESS_NAME);
71 DTEST_LOG << "DistributedAbilityManagerDumperTest::SetUp" << std::endl;
72 }
73
TearDown()74 void DistributedAbilityManagerDumperTest::TearDown()
75 {
76 DTEST_LOG << "DistributedAbilityManagerDumperTest::TearDown" << std::endl;
77 }
78
79 /**
80 * @tc.name: Dump_001
81 * @tc.desc: call dump with invalid fd.
82 * @tc.type: FUNC
83 * @tc.require: I5PUBK
84 */
85 HWTEST_F(DistributedAbilityManagerDumperTest, Dump_001, TestSize.Level4)
86 {
87 DTEST_LOG << "ContinuationManagerTest Dump_001 start" << std::endl;
88 if (dtbabilitymgrService_ == nullptr) {
89 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
90 return;
91 }
92 int32_t fd = -1;
93 const std::vector<std::u16string> args;
94 int32_t result = dtbabilitymgrService_->Dump(fd, args);
95 EXPECT_EQ(DMS_WRITE_FILE_FAILED_ERR, result);
96 DTEST_LOG << "ContinuationManagerTest Dump_001 end" << std::endl;
97 }
98
99 /**
100 * @tc.name: Dump_002
101 * @tc.desc: call dump with fd = 1.
102 * @tc.type: FUNC
103 * @tc.require: I5PUBK
104 */
105 HWTEST_F(DistributedAbilityManagerDumperTest, Dump_002, TestSize.Level3)
106 {
107 DTEST_LOG << "ContinuationManagerTest Dump_002 start" << std::endl;
108 if (dtbabilitymgrService_ == nullptr) {
109 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
110 return;
111 }
112 int32_t fd = 1;
113 const std::vector<std::u16string> args;
114 int32_t result = dtbabilitymgrService_->Dump(fd, args);
115 EXPECT_NE(DMS_WRITE_FILE_FAILED_ERR, result);
116 DTEST_LOG << "ContinuationManagerTest Dump_002 end" << std::endl;
117 }
118
119 /**
120 * @tc.name: ProcessDistributedSchedDump_001
121 * @tc.desc: call ProcessDistributedSchedDump with distributed server not load.
122 * @tc.type: FUNC
123 * @tc.require: I5PUBK
124 */
125 HWTEST_F(DistributedAbilityManagerDumperTest, ProcessDistributedSchedDump_001, TestSize.Level3)
126 {
127 DTEST_LOG << "ContinuationManagerTest ProcessDistributedSchedDump_001 start" << std::endl;
128 if (dtbabilitymgrService_ == nullptr) {
129 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
130 return;
131 }
132 dtbabilitymgrService_->isLoaded_ = false;
133 dtbabilitymgrService_->distributedSchedDumpFunc_ = nullptr;
134 const std::vector<std::string> args;
135 std::string dumpResult;
136 bool result = dtbabilitymgrService_->ProcessDistributedSchedDump(args, dumpResult);
137 EXPECT_EQ(false, result);
138 DTEST_LOG << "ContinuationManagerTest ProcessDistributedSchedDump_001 end" << std::endl;
139 }
140
141 /**
142 * @tc.name: ProcessDistributedSchedDump_002
143 * @tc.desc: call ProcessDistributedSchedDump with distributedSchedDumpFunc_ is nullptr.
144 * @tc.type: FUNC
145 * @tc.require: I5PUBK
146 */
147 HWTEST_F(DistributedAbilityManagerDumperTest, ProcessDistributedSchedDump_002, TestSize.Level3)
148 {
149 DTEST_LOG << "ContinuationManagerTest ProcessDistributedSchedDump_002 start" << std::endl;
150 if (dtbabilitymgrService_ == nullptr) {
151 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
152 return;
153 }
154 dtbabilitymgrService_->isLoaded_ = true;
155 dtbabilitymgrService_->distributedSchedDumpFunc_ = nullptr;
156 const std::vector<std::string> args;
157 std::string dumpResult;
158 bool result = dtbabilitymgrService_->ProcessDistributedSchedDump(args, dumpResult);
159 EXPECT_EQ(false, result);
160 DTEST_LOG << "ContinuationManagerTest ProcessDistributedSchedDump_002 end" << std::endl;
161 }
162
163 /**
164 * @tc.name: ProcessDistributedSchedDump_003
165 * @tc.desc: call ProcessDistributedSchedDump with distributed server load.
166 * @tc.type: FUNC
167 * @tc.require: I5PUBK
168 */
169 HWTEST_F(DistributedAbilityManagerDumperTest, ProcessDistributedSchedDump_003, TestSize.Level3)
170 {
171 DTEST_LOG << "ContinuationManagerTest ProcessDistributedSchedDump_003 start" << std::endl;
172 if (dtbabilitymgrService_ == nullptr) {
173 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
174 return;
175 }
176 dtbabilitymgrService_->isLoaded_ = true;
177 g_mockResult = true;
178 dtbabilitymgrService_->distributedSchedDumpFunc_ = &MockDistributedSchedDumpFunc;
179 const std::vector<std::string> args;
180 std::string dumpResult;
181 bool result = dtbabilitymgrService_->ProcessDistributedSchedDump(args, dumpResult);
182 EXPECT_EQ(true, result);
183 DTEST_LOG << "ContinuationManagerTest ProcessDistributedSchedDump_003 end" << std::endl;
184 }
185
186 /**
187 * @tc.name: ProcessDistributedSchedDump_004
188 * @tc.desc: call ProcessDistributedSchedDump with distributed server load.
189 * @tc.type: FUNC
190 * @tc.require: I5PUBK
191 */
192 HWTEST_F(DistributedAbilityManagerDumperTest, ProcessDistributedSchedDump_004, TestSize.Level3)
193 {
194 DTEST_LOG << "ContinuationManagerTest ProcessDistributedSchedDump_004 start" << std::endl;
195 if (dtbabilitymgrService_ == nullptr) {
196 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
197 return;
198 }
199 dtbabilitymgrService_->isLoaded_ = true;
200 g_mockResult = false;
201 dtbabilitymgrService_->distributedSchedDumpFunc_ = &MockDistributedSchedDumpFunc;
202 const std::vector<std::string> args;
203 std::string dumpResult;
204 bool result = dtbabilitymgrService_->ProcessDistributedSchedDump(args, dumpResult);
205 EXPECT_EQ(false, result);
206 DTEST_LOG << "ContinuationManagerTest ProcessDistributedSchedDump_004 end" << std::endl;
207 }
208
209 /**
210 * @tc.name: Dumper_Dump_001
211 * @tc.desc: call DistributedAbilityManagerDumper::Dump not from hidumper.
212 * @tc.type: FUNC
213 * @tc.require: I5PUBK
214 */
215 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_001, TestSize.Level3)
216 {
217 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_001 start" << std::endl;
218 if (dtbabilitymgrService_ == nullptr) {
219 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
220 return;
221 }
222 MockProcess(DISTSCHED_PROCESS_NAME);
223 const std::vector<std::string> args;
224 std::string dumpResult;
225 bool result = DistributedAbilityManagerDumper::Dump(dtbabilitymgrService_, args, dumpResult);
226 EXPECT_EQ(false, result);
227 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_001 end" << std::endl;
228 }
229
230 /**
231 * @tc.name: Dumper_Dump_002
232 * @tc.desc: call DistributedAbilityManagerDumper::Dump with empty args.
233 * @tc.type: FUNC
234 * @tc.require: I5PUBK
235 */
236 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_002, TestSize.Level3)
237 {
238 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_002 start" << std::endl;
239 if (dtbabilitymgrService_ == nullptr) {
240 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
241 return;
242 }
243 dtbabilitymgrService_->isLoaded_ = true;
244 g_mockResult = true;
245 dtbabilitymgrService_->distributedSchedDumpFunc_ = &MockDistributedSchedDumpFunc;
246 const std::vector<std::string> args;
247 std::string dumpResult;
248 bool result = DistributedAbilityManagerDumper::Dump(dtbabilitymgrService_, args, dumpResult);
249 EXPECT_EQ(true, result);
250 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_002 end" << std::endl;
251 }
252
253 /**
254 * @tc.name: Dumper_Dump_003
255 * @tc.desc: call DistributedAbilityManagerDumper::Dump with args -h.
256 * @tc.type: FUNC
257 * @tc.require: I5PUBK
258 */
259 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_003, TestSize.Level3)
260 {
261 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_003 start" << std::endl;
262 if (dtbabilitymgrService_ == nullptr) {
263 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
264 return;
265 }
266 dtbabilitymgrService_->isLoaded_ = true;
267 g_mockResult = true;
268 dtbabilitymgrService_->distributedSchedDumpFunc_ = &MockDistributedSchedDumpFunc;
269 const std::vector<std::string> args = {"-h"};
270 std::string dumpResult;
271 bool result = DistributedAbilityManagerDumper::Dump(dtbabilitymgrService_, args, dumpResult);
272 EXPECT_EQ(true, result);
273 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_003 end" << std::endl;
274 }
275
276 /**
277 * @tc.name: Dumper_Dump_004
278 * @tc.desc: call DistributedAbilityManagerDumper::Dump with args -connect.
279 * @tc.type: FUNC
280 * @tc.require: I5PUBK
281 */
282 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_004, TestSize.Level3)
283 {
284 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_004 start" << std::endl;
285 if (dtbabilitymgrService_ == nullptr) {
286 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
287 return;
288 }
289 dtbabilitymgrService_->isLoaded_ = true;
290 g_mockResult = true;
291 dtbabilitymgrService_->distributedSchedDumpFunc_ = &MockDistributedSchedDumpFunc;
292 const std::vector<std::string> args = {"-connect"};
293 std::string dumpResult;
294 bool result = DistributedAbilityManagerDumper::Dump(dtbabilitymgrService_, args, dumpResult);
295 EXPECT_EQ(true, result);
296 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_004 end" << std::endl;
297 }
298
299 /**
300 * @tc.name: Dumper_Dump_005
301 * @tc.desc: call DistributedAbilityManagerDumper::Dump with invalid args.
302 * @tc.type: FUNC
303 * @tc.require: I5PUBK
304 */
305 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_005, TestSize.Level3)
306 {
307 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_005 start" << std::endl;
308 if (dtbabilitymgrService_ == nullptr) {
309 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
310 return;
311 }
312 dtbabilitymgrService_->isLoaded_ = true;
313 g_mockResult = true;
314 dtbabilitymgrService_->distributedSchedDumpFunc_ = &MockDistributedSchedDumpFunc;
315 const std::vector<std::string> args = {"-mockArgs"};
316 std::string dumpResult;
317 bool result = DistributedAbilityManagerDumper::Dump(dtbabilitymgrService_, args, dumpResult);
318 EXPECT_EQ(false, result);
319 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_005 end" << std::endl;
320 DTEST_LOG << dumpResult << std::endl;
321 }
322
323 /**
324 * @tc.name: Dumper_Dump_006
325 * @tc.desc: call DistributedAbilityManagerDumper::Dump with dmsDumper is nullptr.
326 * @tc.type: FUNC
327 * @tc.require: I5PUBK
328 */
329 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_006, TestSize.Level4)
330 {
331 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_006 start" << std::endl;
332 dtbabilitymgrService_->isLoaded_ = true;
333 g_mockResult = true;
334 dtbabilitymgrService_->distributedSchedDumpFunc_ = &MockDistributedSchedDumpFunc;
335 const std::vector<std::string> args;
336 std::string dumpResult;
337 bool result = DistributedAbilityManagerDumper::Dump(nullptr, args, dumpResult);
338 EXPECT_EQ(false, result);
339 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_006 end" << std::endl;
340 }
341
342 /**
343 * @tc.name: Dumper_Dump_007
344 * @tc.desc: call DistributedAbilityManagerDumper::Dump from invalid process.
345 * @tc.type: FUNC
346 * @tc.require: I5PUBK
347 */
348 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_007, TestSize.Level4)
349 {
350 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_007 start" << std::endl;
351 if (dtbabilitymgrService_ == nullptr) {
352 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
353 return;
354 }
355 MockProcess(INVALID_PROCESS_NAME);
356 const std::vector<std::string> args;
357 std::string dumpResult;
358 bool result = DistributedAbilityManagerDumper::Dump(dtbabilitymgrService_, args, dumpResult);
359 EXPECT_EQ(false, result);
360 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_007 end" << std::endl;
361 }
362 } // namespace DistributedSchedule
363 } // namespace OHOS