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     ASSERT_NE(nullptr, dtbabilitymgrService_);
85     int32_t fd = -1;
86     const std::vector<std::u16string> args;
87     int32_t result = dtbabilitymgrService_->Dump(fd, args);
88     EXPECT_EQ(DMS_WRITE_FILE_FAILED_ERR, result);
89     DTEST_LOG << "ContinuationManagerTest Dump_001 end" << std::endl;
90 }
91 
92 /**
93  * @tc.name: Dump_002
94  * @tc.desc: call dump with fd = 1.
95  * @tc.type: FUNC
96  * @tc.require: I5PUBK
97  */
98 HWTEST_F(DistributedAbilityManagerDumperTest, Dump_002, TestSize.Level3)
99 {
100     DTEST_LOG << "ContinuationManagerTest Dump_002 start" << std::endl;
101     ASSERT_NE(nullptr, dtbabilitymgrService_);
102     int32_t fd = 1;
103     const std::vector<std::u16string> args;
104     int32_t result = dtbabilitymgrService_->Dump(fd, args);
105     EXPECT_NE(DMS_WRITE_FILE_FAILED_ERR, result);
106     DTEST_LOG << "ContinuationManagerTest Dump_002 end" << std::endl;
107 }
108 
109 /**
110  * @tc.name: Dumper_Dump_001
111  * @tc.desc: call DistributedAbilityManagerDumper::Dump not from hidumper.
112  * @tc.type: FUNC
113  * @tc.require: I5PUBK
114  */
115 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_001, TestSize.Level3)
116 {
117     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_001 start" << std::endl;
118     ASSERT_NE(nullptr, dtbabilitymgrService_);
119     MockProcess(DISTSCHED_PROCESS_NAME);
120     const std::vector<std::string> args;
121     std::string dumpResult;
122     bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
123     EXPECT_EQ(false, result);
124     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_001 end" << std::endl;
125 }
126 
127 /**
128  * @tc.name: Dumper_Dump_002
129  * @tc.desc: call DistributedAbilityManagerDumper::Dump with empty args.
130  * @tc.type: FUNC
131  * @tc.require: I5PUBK
132  */
133 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_002, TestSize.Level3)
134 {
135     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_002 start" << std::endl;
136     ASSERT_NE(nullptr, dtbabilitymgrService_);
137     const std::vector<std::string> args;
138     std::string dumpResult;
139     bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
140     EXPECT_EQ(true, result);
141     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_002 end" << std::endl;
142 }
143 
144 /**
145  * @tc.name: Dumper_Dump_003
146  * @tc.desc: call DistributedAbilityManagerDumper::Dump with args -h.
147  * @tc.type: FUNC
148  * @tc.require: I5PUBK
149  */
150 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_003, TestSize.Level3)
151 {
152     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_003 start" << std::endl;
153     ASSERT_NE(nullptr, dtbabilitymgrService_);
154     const std::vector<std::string> args = {"-h"};
155     std::string dumpResult;
156     bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
157     EXPECT_EQ(true, result);
158     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_003 end" << std::endl;
159 }
160 
161 /**
162  * @tc.name: Dumper_Dump_004
163  * @tc.desc: call DistributedAbilityManagerDumper::Dump with args -register.
164  * @tc.type: FUNC
165  * @tc.require: I5PUBK
166  */
167 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_004, TestSize.Level3)
168 {
169     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_004 start" << std::endl;
170     ASSERT_NE(nullptr, dtbabilitymgrService_);
171     const std::vector<std::string> args = {"-register"};
172     std::string dumpResult;
173     bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
174     EXPECT_EQ(true, result);
175     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_004 end" << std::endl;
176 }
177 
178 /**
179  * @tc.name: Dumper_Dump_005
180  * @tc.desc: call DistributedAbilityManagerDumper::Dump with invalid args.
181  * @tc.type: FUNC
182  * @tc.require: I5PUBK
183  */
184 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_005, TestSize.Level3)
185 {
186     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_005 start" << std::endl;
187     ASSERT_NE(nullptr, dtbabilitymgrService_);
188     const std::vector<std::string> args = {"-mockArgs"};
189     std::string dumpResult;
190     bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
191     EXPECT_EQ(false, result);
192     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_005 end" << std::endl;
193     DTEST_LOG << dumpResult << std::endl;
194 }
195 
196 /**
197  * @tc.name: Dumper_Dump_006
198  * @tc.desc: call DistributedAbilityManagerDumper::Dump from invalid process.
199  * @tc.type: FUNC
200  * @tc.require: I5PUBK
201  */
202 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_Dump_006, TestSize.Level4)
203 {
204     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_006 start" << std::endl;
205     ASSERT_NE(nullptr, dtbabilitymgrService_);
206     MockProcess(INVALID_PROCESS_NAME);
207     const std::vector<std::string> args;
208     std::string dumpResult;
209     bool result = DistributedAbilityManagerDumper::Dump(args, dumpResult);
210     EXPECT_EQ(false, result);
211     DTEST_LOG << "ContinuationManagerTest Dumper_Dump_006 end" << std::endl;
212 }
213 
214 /**
215  * @tc.name: Dumper_DumpDefault_001
216  * @tc.desc: call DistributedAbilityManagerDumper::DumpDefault.
217  * @tc.type: FUNC
218  */
219 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_DumpDefault_001, TestSize.Level4)
220 {
221     DTEST_LOG << "ContinuationManagerTest Dumper_DumpDefault_001 start" << std::endl;
222     ASSERT_NE(nullptr, dtbabilitymgrService_);
223     std::string dumpResult;
224     bool result = DistributedAbilityManagerDumper::DumpDefault(dumpResult);
225     EXPECT_EQ(true, result);
226     DTEST_LOG << "ContinuationManagerTest Dumper_DumpDefault_001 end" << std::endl;
227 }
228 
229 /**
230  * @tc.name: Dumper_ShowAppRegisterInfo_001
231  * @tc.desc: call DistributedAbilityManagerDumper::ShowAppRegisterInfo.
232  * @tc.type: FUNC
233  */
234 HWTEST_F(DistributedAbilityManagerDumperTest, Dumper_ShowAppRegisterInfo_001, TestSize.Level4)
235 {
236     DTEST_LOG << "ContinuationManagerTest Dumper_ShowAppRegisterInfo_001 start" << std::endl;
237     ASSERT_NE(nullptr, dtbabilitymgrService_);
238     std::string dumpResult;
239     DistributedAbilityManagerDumper::ShowAppRegisterInfo(dumpResult);
240     EXPECT_EQ(false, dumpResult.empty());
241     DTEST_LOG << "ContinuationManagerTest Dumper_ShowAppRegisterInfo_001 end" << std::endl;
242 }
243 } // namespace DistributedSchedule
244 } // namespace OHOS