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 "dsched_continue_manager_test.h"
17 
18 #include "datetime_ex.h"
19 
20 #include "dtbschedmgr_device_info_storage.h"
21 #include "distributed_sched_test_util.h"
22 #include "test_log.h"
23 #include "mock_distributed_sched.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS::DistributedHardware;
28 
29 namespace OHOS {
30 namespace DistributedSchedule {
31 
32 namespace {
33     const std::string LOCAL_DEVICEID = "localdeviceid";
34     const std::string REMOTE_DEVICEID = "remotedeviceid";
35     const std::string CONTINUETYPE = "continueType";
36     const std::string BASEDIR = "/data/service/el1/public/database/DistributedSchedule";
37     constexpr int32_t MISSION_ID = 1;
38     const int32_t WAITTIME = 2000;
39     const std::string BUNDLE_NAME = "com.ohos.permissionmanager";
40 }
41 
SetUpTestCase()42 void DSchedContinueManagerTest::SetUpTestCase()
43 {
44     mkdir(BASEDIR.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
45     const std::string pkgName = "DBinderBus_PermissionTest" + std::to_string(getprocpid());
46     std::shared_ptr<DmInitCallback> initCallback_ = std::make_shared<DeviceInitCallBack>();
47     DeviceManager::GetInstance().InitDeviceManager(pkgName, initCallback_);
48     DTEST_LOG << "DSchedContinueManagerTest::SetUpTestCase" << std::endl;
49 }
50 
TearDownTestCase()51 void DSchedContinueManagerTest::TearDownTestCase()
52 {
53     (void)remove(BASEDIR.c_str());
54     DTEST_LOG << "DSchedContinueManagerTest::TearDownTestCase" << std::endl;
55 }
56 
TearDown()57 void DSchedContinueManagerTest::TearDown()
58 {
59     usleep(WAITTIME);
60     DTEST_LOG << "DSchedContinueManagerTest::TearDown" << std::endl;
61 }
62 
SetUp()63 void DSchedContinueManagerTest::SetUp()
64 {
65     usleep(WAITTIME);
66     DTEST_LOG << "DSchedContinueManagerTest::SetUp" << std::endl;
67 }
68 
OnRemoteDied()69 void DSchedContinueManagerTest::DeviceInitCallBack::OnRemoteDied()
70 {
71 }
72 
GetDSchedService() const73 sptr<IRemoteObject> DSchedContinueManagerTest::GetDSchedService() const
74 {
75     sptr<IRemoteObject> dsched(new MockDistributedSched());
76     return dsched;
77 }
78 
CreateObject()79 std::shared_ptr<DSchedContinue> DSchedContinueManagerTest::CreateObject()
80 {
81     int32_t subServiceType = 0;
82     int32_t direction = 0;
83     sptr<IRemoteObject> callback = nullptr;
84     DSchedContinueInfo continueInfo;
85     std::shared_ptr<DSchedContinue> dContinue = std::make_shared<DSchedContinue>(subServiceType, direction,
86         callback, continueInfo);
87     dContinue->Init();
88     return dContinue;
89 }
90 
91 /**
92  * @tc.name: Init_001
93  * @tc.desc: test Init func
94  * @tc.type: FUNC
95  */
96 HWTEST_F(DSchedContinueManagerTest, Init_001, TestSize.Level3)
97 {
98     DTEST_LOG << "DSchedContinueManagerTest Init_001 begin" << std::endl;
99     DSchedContinueManager::GetInstance().Init();
100     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler =
101         DSchedContinueManager::GetInstance().eventHandler_;
102     EXPECT_NE(eventHandler, nullptr);
103     DTEST_LOG << "DSchedContinueManagerTest Init_001 end" << std::endl;
104 }
105 
106 /**
107  * @tc.name: ContinueMission_001
108  * @tc.desc: test ContinueMission func
109  * @tc.type: FUNC
110  */
111 HWTEST_F(DSchedContinueManagerTest, ContinueMission_001, TestSize.Level3)
112 {
113     DTEST_LOG << "DSchedContinueManagerTest ContinueMission_001 begin" << std::endl;
114     auto callback = GetDSchedService();
115     OHOS::AAFwk::WantParams wantParams;
116     int32_t ret = DSchedContinueManager::GetInstance().ContinueMission(LOCAL_DEVICEID, REMOTE_DEVICEID, MISSION_ID,
117         callback, wantParams);
118     EXPECT_EQ(ret, OPERATION_DEVICE_NOT_INITIATOR_OR_TARGET);
119     DTEST_LOG << "DSchedContinueManagerTest ContinueMission_001 end" << std::endl;
120 }
121 
122 /**
123  * @tc.name: ContinueMission_002
124  * @tc.desc: test ContinueMission func
125  * @tc.type: FUNC
126  */
127 HWTEST_F(DSchedContinueManagerTest, ContinueMission_002, TestSize.Level3)
128 {
129     DTEST_LOG << "DSchedContinueManagerTest ContinueMission_002 begin" << std::endl;
130     OHOS::AAFwk::WantParams wantParams;
131     auto callback = GetDSchedService();
132     DSchedContinueManager::GetInstance().HandleContinueMission("", REMOTE_DEVICEID, MISSION_ID, callback, wantParams);
133     DSchedContinueManager::GetInstance().HandleContinueMission(LOCAL_DEVICEID, "", MISSION_ID, callback, wantParams);
134     DSchedContinueManager::GetInstance().HandleContinueMission(LOCAL_DEVICEID, REMOTE_DEVICEID, MISSION_ID,
135         nullptr, wantParams);
136 
137     int32_t ret = DSchedContinueManager::GetInstance().ContinueMission(LOCAL_DEVICEID, REMOTE_DEVICEID, MISSION_ID,
138         callback, wantParams);
139     EXPECT_EQ(ret, OPERATION_DEVICE_NOT_INITIATOR_OR_TARGET);
140     DTEST_LOG << "DSchedContinueManagerTest ContinueMission_002 end" << std::endl;
141 }
142 
143 /**
144  * @tc.name: ContinueMission_003
145  * @tc.desc: test ContinueMission func
146  * @tc.type: FUNC
147  */
148 HWTEST_F(DSchedContinueManagerTest, ContinueMission_003, TestSize.Level3)
149 {
150     DTEST_LOG << "DSchedContinueManagerTest ContinueMission_003 begin" << std::endl;
151     DistributedSchedUtil::MockPermission();
152     OHOS::AAFwk::WantParams wantParams;
153     int32_t ret = DSchedContinueManager::GetInstance().ContinueMission(
154         DSchedContinueInfo("", BUNDLE_NAME, "", BUNDLE_NAME, CONTINUETYPE),
155         nullptr, wantParams);
156     EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
157 
158     ret = DSchedContinueManager::GetInstance().ContinueMission(
159         DSchedContinueInfo(LOCAL_DEVICEID, BUNDLE_NAME, "", BUNDLE_NAME, CONTINUETYPE),
160         nullptr, wantParams);
161     EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
162 
163     ret = DSchedContinueManager::GetInstance().ContinueMission(
164         DSchedContinueInfo(LOCAL_DEVICEID, BUNDLE_NAME, REMOTE_DEVICEID, BUNDLE_NAME, CONTINUETYPE),
165         nullptr, wantParams);
166     EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
167 
168     auto callback = GetDSchedService();
169     ret = DSchedContinueManager::GetInstance().ContinueMission(
170         DSchedContinueInfo(LOCAL_DEVICEID, BUNDLE_NAME, REMOTE_DEVICEID, BUNDLE_NAME, CONTINUETYPE),
171         callback, wantParams);
172     EXPECT_EQ(ret, OPERATION_DEVICE_NOT_INITIATOR_OR_TARGET);
173 
174     std::string locDevId;
175     EXPECT_EQ(true, DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(locDevId));
176     ret = DSchedContinueManager::GetInstance().ContinueMission(
177         DSchedContinueInfo(LOCAL_DEVICEID, BUNDLE_NAME, locDevId, BUNDLE_NAME, CONTINUETYPE),
178         callback, wantParams);
179     EXPECT_EQ(ret, INVALID_REMOTE_PARAMETERS_ERR);
180 
181     ret = DSchedContinueManager::GetInstance().ContinueMission(
182         DSchedContinueInfo(locDevId, BUNDLE_NAME, REMOTE_DEVICEID, BUNDLE_NAME, CONTINUETYPE),
183         callback, wantParams);
184     EXPECT_EQ(ret, INVALID_REMOTE_PARAMETERS_ERR);
185     DTEST_LOG << "DSchedContinueManagerTest ContinueMission_003 end" << std::endl;
186 }
187 
188 /**
189  * @tc.name: HandleContinueMission_001
190  * @tc.desc: test HandleContinueMission func
191  * @tc.type: FUNC
192  */
193 HWTEST_F(DSchedContinueManagerTest, HandleContinueMission_001, TestSize.Level3)
194 {
195     DTEST_LOG << "DSchedContinueManagerTest HandleContinueMission_001 begin" << std::endl;
196     OHOS::AAFwk::WantParams wantParams;
197     auto callback = GetDSchedService();
198     DSchedContinueManager::GetInstance().HandleContinueMission(
199         DSchedContinueInfo("", BUNDLE_NAME, REMOTE_DEVICEID, BUNDLE_NAME, CONTINUETYPE),
200         callback, wantParams);
201     DSchedContinueManager::GetInstance().HandleContinueMission(
202         DSchedContinueInfo(LOCAL_DEVICEID, BUNDLE_NAME, "", BUNDLE_NAME, CONTINUETYPE),
203         callback, wantParams);
204     DSchedContinueManager::GetInstance().HandleContinueMission(
205         DSchedContinueInfo(LOCAL_DEVICEID, BUNDLE_NAME, REMOTE_DEVICEID, BUNDLE_NAME, CONTINUETYPE),
206         nullptr, wantParams);
207 
208     DSchedContinueManager::GetInstance().HandleContinueMission(
209         DSchedContinueInfo(LOCAL_DEVICEID, BUNDLE_NAME, REMOTE_DEVICEID, BUNDLE_NAME, CONTINUETYPE),
210         callback, wantParams);
211     DTEST_LOG << "DSchedContinueManagerTest HandleContinueMission_001 end" << std::endl;
212 }
213 
214 /**
215  * @tc.name: SetTimeOut_001
216  * @tc.desc: test SetTimeOut func
217  * @tc.type: FUNC
218  */
219 HWTEST_F(DSchedContinueManagerTest, SetTimeOut_001, TestSize.Level3)
220 {
221     DTEST_LOG << "DSchedContinueManagerTest SetTimeOut_001 begin" << std::endl;
222     DSchedContinueInfo info;
223     int32_t timeout = 0;
224     DSchedContinueManager::GetInstance().continues_.clear();
225     DSchedContinueManager::GetInstance().SetTimeOut(info, timeout);
226     EXPECT_EQ(DSchedContinueManager::GetInstance().continues_.empty(), true);
227     DTEST_LOG << "DSchedContinueManagerTest SetTimeOut_001 end" << std::endl;
228 }
229 
230 /**
231  * @tc.name: StartContinuation_001
232  * @tc.desc: test StartContinuation func
233  * @tc.type: FUNC
234  */
235 HWTEST_F(DSchedContinueManagerTest, StartContinuation_001, TestSize.Level3)
236 {
237     DTEST_LOG << "DSchedContinueManagerTest StartContinuation_001 begin" << std::endl;
238     OHOS::AAFwk::Want want;
239     int32_t missionId = 0;
240     int32_t callerUid = 0;
241     int32_t status = 0;
242     uint32_t accessToken = 0;
243     int32_t ret1 = DSchedContinueManager::GetInstance().StartContinuation(want, missionId,
244         callerUid, status, accessToken);
245     EXPECT_EQ(ret1, INVALID_REMOTE_PARAMETERS_ERR);
246     DTEST_LOG << "DSchedContinueManagerTest StartContinuation_001 end" << std::endl;
247 }
248 
249 /**
250  * @tc.name: CheckContinuationLimit_001
251  * @tc.desc: test CheckContinuationLimit func
252  * @tc.type: FUNC
253  */
254 HWTEST_F(DSchedContinueManagerTest, CheckContinuationLimit_001, TestSize.Level3)
255 {
256     DTEST_LOG << "DSchedContinueManagerTest CheckContinuationLimit_001 begin" << std::endl;
257     int32_t direction = 0;
258     int32_t ret = DSchedContinueManager::GetInstance().CheckContinuationLimit(LOCAL_DEVICEID, REMOTE_DEVICEID,
259         direction);
260     EXPECT_EQ(ret, OPERATION_DEVICE_NOT_INITIATOR_OR_TARGET);
261     DTEST_LOG << "DSchedContinueManagerTest CheckContinuationLimit_001 end" << std::endl;
262 }
263 
264 /**
265  * @tc.name: GetContinueInfo_001
266  * @tc.desc: test GetContinueInfo func
267  * @tc.type: FUNC
268  */
269 HWTEST_F(DSchedContinueManagerTest, GetContinueInfo_001, TestSize.Level3)
270 {
271     DTEST_LOG << "DSchedContinueManagerTest GetContinueInfo_001 begin" << std::endl;
272     std::string localDeviceId = "localdeviceid";
273     std::string remoteDeviceId = "remotedeviceid";
274     int32_t ret = DSchedContinueManager::GetInstance().GetContinueInfo(localDeviceId, remoteDeviceId);
275     EXPECT_EQ(ret, ERR_OK);
276     DTEST_LOG << "DSchedContinueManagerTest GetContinueInfo_001 end" << std::endl;
277 }
278 
279 /**
280  * @tc.name: GetContinueInfo_002
281  * @tc.desc: test GetContinueInfo func
282  * @tc.type: FUNC
283  */
284 HWTEST_F(DSchedContinueManagerTest, GetContinueInfo_002, TestSize.Level3)
285 {
286     DTEST_LOG << "DSchedContinueManagerTest GetContinueInfo_002 begin" << std::endl;
287     DSchedContinueManager::GetInstance().OnShutdown(1, true);
288 
289     DSchedContinueManager::GetInstance().OnShutdown(1, false);
290 
291     DSchedContinueManager::GetInstance().OnShutdown(1, false);
292 
293     DSchedContinueInfo info;
294     std::shared_ptr<DSchedContinue> dContinue = CreateObject();
295     std::string localDeviceId = "localdeviceid";
296     std::string remoteDeviceId = "remotedeviceid";
297     DSchedContinueManager::GetInstance().continues_[info] = dContinue;
298     int32_t ret = DSchedContinueManager::GetInstance().GetContinueInfo(localDeviceId, remoteDeviceId);
299     EXPECT_EQ(ret, ERR_OK);
300     DTEST_LOG << "DSchedContinueManagerTest GetContinueInfo_002 end" << std::endl;
301 }
302 
303 /**
304  * @tc.name: HandleNotifyCompleteContinuation_001
305  * @tc.desc: test HandleNotifyCompleteContinuation func
306  * @tc.type: FUNC
307  */
308 HWTEST_F(DSchedContinueManagerTest, HandleNotifyCompleteContinuation_001, TestSize.Level3)
309 {
310     DTEST_LOG << "DSchedContinueManagerTest HandleNotifyCompleteContinuation_001 begin" << std::endl;
311     std::u16string devId;
312     int32_t missionId = 0;
313     std::string callerBundleName;
314     DSchedContinueManager::GetInstance().continues_.clear();
315     DSchedContinueManager::GetInstance().HandleNotifyCompleteContinuation(devId, missionId, false, callerBundleName);
316 
317     DSchedContinueInfo info;
318     DSchedContinueManager::GetInstance().continues_[info] = nullptr;
319     DSchedContinueManager::GetInstance().HandleNotifyCompleteContinuation(devId, missionId, false, callerBundleName);
320     bool ret = DSchedContinueManager::GetInstance().continues_.empty();
321     EXPECT_EQ(ret, false);
322     DTEST_LOG << "DSchedContinueManagerTest HandleNotifyCompleteContinuation_001 end" << std::endl;
323 }
324 
325 /**
326  * @tc.name: NotifyCompleteContinuation_001
327  * @tc.desc: test NotifyCompleteContinuation func
328  * @tc.type: FUNC
329  */
330 HWTEST_F(DSchedContinueManagerTest, NotifyCompleteContinuation_001, TestSize.Level3)
331 {
332     DTEST_LOG << "DSchedContinueManagerTest NotifyCompleteContinuation_001 begin" << std::endl;
333     std::u16string devId;
334     int32_t sessionId = 0;
335     bool isSuccess = false;
336     std::string callerBundleName;
337     DSchedContinueManager::GetInstance().Init();
338     int32_t ret = DSchedContinueManager::GetInstance().NotifyCompleteContinuation(devId,
339         sessionId, isSuccess, callerBundleName);
340     EXPECT_EQ(ret, ERR_OK);
341     DTEST_LOG << "DSchedContinueManagerTest NotifyCompleteContinuation_001 end" << std::endl;
342 }
343 
344 /**
345  * @tc.name: OnContinueEnd_001
346  * @tc.desc: test OnContinueEnd func
347  * @tc.type: FUNC
348  */
349 HWTEST_F(DSchedContinueManagerTest, OnContinueEnd_001, TestSize.Level3)
350 {
351     DTEST_LOG << "DSchedContinueManagerTest OnContinueEnd_001 begin" << std::endl;
352     DSchedContinueInfo info(LOCAL_DEVICEID, "sourceBundleName", REMOTE_DEVICEID, "sinkBundleName",
353         "continueType");
354     DSchedContinueManager::GetInstance().UnInit();
355     int32_t ret = DSchedContinueManager::GetInstance().OnContinueEnd(info);
356     EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
357 
358     DSchedContinueManager::GetInstance().Init();
359 
360     ret = DSchedContinueManager::GetInstance().OnContinueEnd(info);
361     EXPECT_EQ(ret, ERR_OK);
362     DTEST_LOG << "DSchedContinueManagerTest OnContinueEnd_001 end" << std::endl;
363 }
364 
365 /**
366  * @tc.name: HandleContinueEnd_001
367  * @tc.desc: test HandleContinueEnd func
368  * @tc.type: FUNC
369  */
370 HWTEST_F(DSchedContinueManagerTest, HandleContinueEnd_001, TestSize.Level3)
371 {
372     DTEST_LOG << "DSchedContinueManagerTest HandleContinueEnd_001 begin" << std::endl;
373     DSchedContinueInfo info(LOCAL_DEVICEID, "sourceBundleName", REMOTE_DEVICEID, "sinkBundleName",
374         "continueType");
375     DSchedContinueManager::GetInstance().RemoveTimeout(info);
376 
377     DSchedContinueManager::GetInstance().Init();
378     DSchedContinueManager::GetInstance().RemoveTimeout(info);
379 
380     DSchedContinueManager::GetInstance().continues_.clear();
381     DSchedContinueManager::GetInstance().HandleContinueEnd(info);
382     int32_t ret = DSchedContinueManager::GetInstance().continues_.empty();
383     EXPECT_EQ(ret, true);
384 
385     std::shared_ptr<DSchedContinue> ptr = nullptr;
386     DSchedContinueManager::GetInstance().continues_[info] = ptr;
387     DSchedContinueManager::GetInstance().HandleContinueEnd(info);
388     EXPECT_EQ(DSchedContinueManager::GetInstance().cntSource_, 0);
389     DTEST_LOG << "DSchedContinueManagerTest HandleContinueEnd_001 end" << std::endl;
390 }
391 }
392 }