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 "distributed_ability_manager_dumper_test.h"
17
18 #define private public
19 #include "distributed_ability_manager_dumper.h"
20 #undef private
21 #include "dtbschedmgr_log.h"
22 #include "nativetoken_kit.h"
23 #include "test_log.h"
24 #include "token_setproc.h"
25
26 namespace OHOS {
27 namespace DistributedSchedule {
28 using namespace testing;
29 using namespace testing::ext;
30 namespace {
31 const char* HIDUMPER_PROCESS_NAME = "hidumper_service";
32 const char* DISTSCHED_PROCESS_NAME = "distributedsched";
33 const char* INVALID_PROCESS_NAME = "invalid_process";
34 }
35
MockProcess(const char * processName)36 static void MockProcess(const char* processName)
37 {
38 uint64_t tokenId;
39 NativeTokenInfoParams infoInstance = {
40 .dcapsNum = 0,
41 .permsNum = 0,
42 .aclsNum = 0,
43 .dcaps = nullptr,
44 .perms = nullptr,
45 .acls = nullptr,
46 .processName = processName,
47 .aplStr = "system_core",
48 };
49 tokenId = GetAccessTokenId(&infoInstance);
50 SetSelfTokenID(tokenId);
51 }
52
SetUpTestCase()53 void DistributedAbilityManagerDumperTest::SetUpTestCase()
54 {
55 DTEST_LOG << "DistributedAbilityManagerDumperTest::SetUpTestCase" << std::endl;
56 }
57
TearDownTestCase()58 void DistributedAbilityManagerDumperTest::TearDownTestCase()
59 {
60 DTEST_LOG << "DistributedAbilityManagerDumperTest::TearDownTestCase" << std::endl;
61 }
62
SetUp()63 void DistributedAbilityManagerDumperTest::SetUp()
64 {
65 dtbabilitymgrService_ = new DistributedAbilityManagerService();
66 MockProcess(HIDUMPER_PROCESS_NAME);
67 DTEST_LOG << "DistributedAbilityManagerDumperTest::SetUp" << std::endl;
68 }
69
TearDown()70 void DistributedAbilityManagerDumperTest::TearDown()
71 {
72 DTEST_LOG << "DistributedAbilityManagerDumperTest::TearDown" << std::endl;
73 }
74
75 /**
76 * @tc.name: Dump_001
77 * @tc.desc: call dump with invalid fd.
78 * @tc.type: FUNC
79 * @tc.require: I5PUBK
80 */
81 HWTEST_F(DistributedAbilityManagerDumperTest, Dump_001, TestSize.Level4)
82 {
83 DTEST_LOG << "ContinuationManagerTest Dump_001 start" << std::endl;
84 if (dtbabilitymgrService_ == nullptr) {
85 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
86 return;
87 }
88 int32_t fd = -1;
89 const std::vector<std::u16string> args;
90 int32_t result = dtbabilitymgrService_->Dump(fd, args);
91 EXPECT_EQ(DMS_WRITE_FILE_FAILED_ERR, result);
92 DTEST_LOG << "ContinuationManagerTest Dump_001 end" << std::endl;
93 }
94
95 /**
96 * @tc.name: Dump_002
97 * @tc.desc: call dump with fd = 1.
98 * @tc.type: FUNC
99 * @tc.require: I5PUBK
100 */
101 HWTEST_F(DistributedAbilityManagerDumperTest, Dump_002, TestSize.Level3)
102 {
103 DTEST_LOG << "ContinuationManagerTest Dump_002 start" << std::endl;
104 if (dtbabilitymgrService_ == nullptr) {
105 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
106 return;
107 }
108 int32_t fd = 1;
109 const std::vector<std::u16string> args;
110 int32_t result = dtbabilitymgrService_->Dump(fd, args);
111 EXPECT_NE(DMS_WRITE_FILE_FAILED_ERR, result);
112 DTEST_LOG << "ContinuationManagerTest Dump_002 end" << std::endl;
113 }
114
115 /**
116 * @tc.name: Dumper_Dump_001
117 * @tc.desc: call DistributedAbilityManagerDumper::Dump not from hidumper.
118 * @tc.type: FUNC
119 * @tc.require: I5PUBK
120 */
121 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_001, TestSize.Level3)
122 {
123 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_001 start" << std::endl;
124 if (dtbabilitymgrService_ == nullptr) {
125 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
126 return;
127 }
128 MockProcess(DISTSCHED_PROCESS_NAME);
129 const std::vector<std::string> args;
130 std::string dumpResult;
131 bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
132 EXPECT_EQ(false, result);
133 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_001 end" << std::endl;
134 }
135
136 /**
137 * @tc.name: Dumper_Dump_002
138 * @tc.desc: call DistributedAbilityManagerDumper::Dump with empty args.
139 * @tc.type: FUNC
140 * @tc.require: I5PUBK
141 */
142 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_002, TestSize.Level3)
143 {
144 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_002 start" << std::endl;
145 if (dtbabilitymgrService_ == nullptr) {
146 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
147 return;
148 }
149 const std::vector<std::string> args;
150 std::string dumpResult;
151 bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
152 EXPECT_EQ(true, result);
153 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_002 end" << std::endl;
154 }
155
156 /**
157 * @tc.name: Dumper_Dump_003
158 * @tc.desc: call DistributedAbilityManagerDumper::Dump with args -h.
159 * @tc.type: FUNC
160 * @tc.require: I5PUBK
161 */
162 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_003, TestSize.Level3)
163 {
164 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_003 start" << std::endl;
165 if (dtbabilitymgrService_ == nullptr) {
166 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
167 return;
168 }
169 const std::vector<std::string> args = {"-h"};
170 std::string dumpResult;
171 bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
172 EXPECT_EQ(true, result);
173 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_003 end" << std::endl;
174 }
175
176 /**
177 * @tc.name: Dumper_Dump_004
178 * @tc.desc: call DistributedAbilityManagerDumper::Dump with args -register.
179 * @tc.type: FUNC
180 * @tc.require: I5PUBK
181 */
182 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_004, TestSize.Level3)
183 {
184 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_004 start" << std::endl;
185 if (dtbabilitymgrService_ == nullptr) {
186 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
187 return;
188 }
189 const std::vector<std::string> args = {"-register"};
190 std::string dumpResult;
191 bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
192 EXPECT_EQ(true, result);
193 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_004 end" << std::endl;
194 }
195
196 /**
197 * @tc.name: Dumper_Dump_005
198 * @tc.desc: call DistributedAbilityManagerDumper::Dump with invalid args.
199 * @tc.type: FUNC
200 * @tc.require: I5PUBK
201 */
202 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_005, TestSize.Level3)
203 {
204 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_005 start" << std::endl;
205 if (dtbabilitymgrService_ == nullptr) {
206 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
207 return;
208 }
209 const std::vector<std::string> args = {"-mockArgs"};
210 std::string dumpResult;
211 bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
212 EXPECT_EQ(false, result);
213 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_005 end" << std::endl;
214 DTEST_LOG << dumpResult << std::endl;
215 }
216
217 /**
218 * @tc.name: Dumper_Dump_006
219 * @tc.desc: call DistributedAbilityManagerDumper::Dump from invalid process.
220 * @tc.type: FUNC
221 * @tc.require: I5PUBK
222 */
223 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_006, TestSize.Level4)
224 {
225 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_006 start" << std::endl;
226 if (dtbabilitymgrService_ == nullptr) {
227 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
228 return;
229 }
230 MockProcess(INVALID_PROCESS_NAME);
231 const std::vector<std::string> args;
232 std::string dumpResult;
233 bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
234 EXPECT_EQ(false, result);
235 DTEST_LOG << "ContinuationManagerTest Dumper_Dump_006 end" << std::endl;
236 }
237
238 /**
239 * @tc.name: Dumper_DumpDefault_001
240 * @tc.desc: call DistributedAbilityManagerDumper::DumpDefault.
241 * @tc.type: FUNC
242 */
243 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_DumpDefault_001, TestSize.Level4)
244 {
245 DTEST_LOG << "ContinuationManagerTest Dumper_DumpDefault_001 start" << std::endl;
246 if (dtbabilitymgrService_ == nullptr) {
247 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
248 return;
249 }
250 std::string dumpResult;
251 bool result = DistributedAbilityManagerDumper::DumpDefault(dumpResult);
252 EXPECT_EQ(true, result);
253 DTEST_LOG << "ContinuationManagerTest Dumper_DumpDefault_001 end" << std::endl;
254 }
255
256 /**
257 * @tc.name: Dumper_ShowAppRegisterInfo_001
258 * @tc.desc: call DistributedAbilityManagerDumper::ShowAppRegisterInfo.
259 * @tc.type: FUNC
260 */
261 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_ShowAppRegisterInfo_001, TestSize.Level4)
262 {
263 DTEST_LOG << "ContinuationManagerTest Dumper_ShowAppRegisterInfo_001 start" << std::endl;
264 if (dtbabilitymgrService_ == nullptr) {
265 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
266 return;
267 }
268 std::string dumpResult;
269 DistributedAbilityManagerDumper::ShowAppRegisterInfo(dumpResult);
270 EXPECT_EQ(false, dumpResult.empty());
271 DTEST_LOG << "ContinuationManagerTest Dumper_ShowAppRegisterInfo_001 end" << std::endl;
272 }
273 } // namespace DistributedSchedule
274 } // namespace OHOS