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_mission_info_test.h"
17 #include <memory>
18 #include "change_notification.h"
19 #define private public
20 #include "mission/distributed_data_change_listener.h"
21 #include "mission/distributed_mission_info.h"
22 #include "mission/mission_changed_notify.h"
23 #undef private
24 #include "mission/mission_constant.h"
25 #include "mock_remote_stub.h"
26 #include "parcel_helper.h"
27 #include "test_log.h"
28
29 using namespace testing;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace DistributedSchedule {
34 namespace {
35 constexpr int32_t TEST_INVALID_VALUE = -1;
36 constexpr int32_t TEST_PARCEL_WRITE_VALUE = 1;
37 constexpr size_t TEST_PARCEL_WRITE_LEN = 5;
38 const std::string TEST_DEVICE_ID = "test deviceId";
39 const std::u16string TEST_DEVICEID = u"invalid deviceId";
40 const std::u16string TEST_DEVICEID_TO_RETURN_FALSE = u"deviceId to return false";
41 }
SetUpTestCase()42 void DistributedMissionInfoTest::SetUpTestCase()
43 {
44 }
45
TearDownTestCase()46 void DistributedMissionInfoTest::TearDownTestCase()
47 {
48 }
49
SetUp()50 void DistributedMissionInfoTest::SetUp()
51 {
52 }
53
TearDown()54 void DistributedMissionInfoTest::TearDown()
55 {
56 }
57
58 /**
59 * @tc.name: testReadFromParcel001
60 * @tc.desc: test ReadFromParcel
61 * @tc.type: FUNC
62 * @tc.require: I5O2P9
63 */
64 HWTEST_F(DistributedMissionInfoTest, testReadFromParcel001, TestSize.Level3)
65 {
66 DTEST_LOG << "DistributedMissionInfoTest testReadFromParcel001 start" << std::endl;
67 DstbMissionInfo dstbMissionInfo;
68 Parcel parcel;
69 bool ret = dstbMissionInfo.Marshalling(parcel);
70 EXPECT_TRUE(ret);
71 ret = dstbMissionInfo.ReadFromParcel(parcel);
72 EXPECT_TRUE(ret);
73 DTEST_LOG << "DistributedMissionInfoTest testReadFromParcel001 end" << std::endl;
74 }
75
76 /**
77 * @tc.name: testReadFromParcel002
78 * @tc.desc: test ReadFromParcel when combinedMissionIds read failed
79 * @tc.type: FUNC
80 * @tc.require: I5O2P9
81 */
82 HWTEST_F(DistributedMissionInfoTest, testReadFromParcel002, TestSize.Level3)
83 {
84 DTEST_LOG << "DistributedMissionInfoTest testReadFromParcel002 start" << std::endl;
85 DstbMissionInfo dstbMissionInfo;
86 Parcel parcel;
87 for (size_t i = 0; i < TEST_PARCEL_WRITE_LEN; i++) {
88 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
89 }
90 bool ret = dstbMissionInfo.ReadFromParcel(parcel);
91 EXPECT_FALSE(ret);
92 DTEST_LOG << "DistributedMissionInfoTest testReadFromParcel002 end" << std::endl;
93 }
94
95 /**
96 * @tc.name: testUnmarshalling001
97 * @tc.desc: test Unmarshalling
98 * @tc.type: FUNC
99 * @tc.require: I5O2P9
100 */
101 HWTEST_F(DistributedMissionInfoTest, testUnmarshalling001, TestSize.Level3)
102 {
103 DTEST_LOG << "DistributedMissionInfoTest testUnmarshalling001 start" << std::endl;
104 DstbMissionInfo dstbMissionInfo;
105 Parcel parcel;
106 bool ret = dstbMissionInfo.Marshalling(parcel);
107 EXPECT_TRUE(ret);
108 DstbMissionInfo* dstbMissionInfoReturn = dstbMissionInfo.Unmarshalling(parcel);
109 EXPECT_NE(nullptr, dstbMissionInfoReturn);
110 DTEST_LOG << "DistributedMissionInfoTest testUnmarshalling001 end" << std::endl;
111 }
112
113 /**
114 * @tc.name: testUnmarshalling002
115 * @tc.desc: test Unmarshalling when combinedMissionIds read failed
116 * @tc.type: FUNC
117 * @tc.require: I5O2P9
118 */
119 HWTEST_F(DistributedMissionInfoTest, testUnmarshalling002, TestSize.Level3)
120 {
121 DTEST_LOG << "DistributedMissionInfoTest testUnmarshalling002 start" << std::endl;
122 DstbMissionInfo dstbMissionInfo;
123 Parcel parcel;
124 for (size_t i = 0; i < TEST_PARCEL_WRITE_LEN; i++) {
125 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
126 }
127 DstbMissionInfo* dstbMissionInfoReturn = dstbMissionInfo.Unmarshalling(parcel);
128 EXPECT_EQ(nullptr, dstbMissionInfoReturn);
129 DTEST_LOG << "DistributedMissionInfoTest testUnmarshalling002 end" << std::endl;
130 }
131
132 /**
133 * @tc.name: testMarshalling001
134 * @tc.desc: write data to parcel
135 * @tc.type: FUNC
136 * @tc.require: I5O2P9
137 */
138 HWTEST_F(DistributedMissionInfoTest, testMarshalling001, TestSize.Level3)
139 {
140 /**
141 * @tc.steps: step1. test Marshalling.
142 */
143 DstbMissionInfo dstbMissionInfo;
144 Parcel data;
145 auto ret = dstbMissionInfo.Marshalling(data);
146 EXPECT_EQ(ret, true);
147 /**
148 * @tc.steps: step2. test OnChange when insertEntries is not empty.
149 */
150 std::vector<DistributedKv::Entry> insertEntries;
151 std::vector<DistributedKv::Entry> updateEntries;
152 std::vector<DistributedKv::Entry> deleteEntries;
153 DistributedKv::Entry entry;
154 entry.key = "key";
155 entry.value = "value";
156 insertEntries.emplace_back(entry);
157 updateEntries.emplace_back(entry);
158 deleteEntries.emplace_back(entry);
159 std::string deviceId = "device";
160 DistributedKv::ChangeNotification changeNotification(std::move(insertEntries), std::move(updateEntries),
161 std::move(deleteEntries), deviceId, false);
162 DistributedDataChangeListener distributedDataChangeListener;
163 distributedDataChangeListener.OnChange(changeNotification);
164 /**
165 * @tc.steps: step3. test KeyInfo when strVector.size() == MAX_SPLIT_VARS.
166 */
167 insertEntries.clear();
168 updateEntries.clear();
169 deleteEntries.clear();
170 entry.key = "key_0";
171 insertEntries.emplace_back(entry);
172 updateEntries.emplace_back(entry);
173 deleteEntries.emplace_back(entry);
174 DistributedKv::ChangeNotification changeNotification1(std::move(insertEntries), std::move(updateEntries),
175 std::move(deleteEntries), deviceId, false);
176 distributedDataChangeListener.OnChange(changeNotification);
177 }
178
179 /**
180 * @tc.name: testReadDstbMissionInfosFromParcel001
181 * @tc.desc: test ReadDstbMissionInfosFromParcel
182 * @tc.type: FUNC
183 * @tc.require: I5O2P9
184 */
185 HWTEST_F(DistributedMissionInfoTest, testReadDstbMissionInfosFromParcel001, TestSize.Level3)
186 {
187 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel001 start" << std::endl;
188 /**
189 * @tc.steps: step1. test ReadDstbMissionInfosFromParcel.
190 */
191 DstbMissionInfo dstbMissionInfo;
192 Parcel parcel;
193 std::vector<DstbMissionInfo> missionInfos;
194 bool ret = dstbMissionInfo.ReadDstbMissionInfosFromParcel(parcel, missionInfos);
195 EXPECT_TRUE(ret);
196 EXPECT_EQ(0, missionInfos.size());
197 /**
198 * @tc.steps: step2. test KeyInfo when StrToInt failed.
199 */
200 std::vector<DistributedKv::Entry> insertEntries;
201 std::vector<DistributedKv::Entry> updateEntries;
202 std::vector<DistributedKv::Entry> deleteEntries;
203 DistributedKv::Entry entry;
204 entry.key = "invalid_key";
205 entry.value = "value";
206 insertEntries.emplace_back(entry);
207 updateEntries.emplace_back(entry);
208 deleteEntries.emplace_back(entry);
209 std::string deviceId = "device";
210 DistributedKv::ChangeNotification changeNotification(std::move(insertEntries), std::move(updateEntries),
211 std::move(deleteEntries), deviceId, false);
212 DistributedDataChangeListener distributedDataChangeListener;
213 distributedDataChangeListener.OnChange(changeNotification);
214 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel001 end" << std::endl;
215 }
216
217 /**
218 * @tc.name: testReadDstbMissionInfosFromParcel002
219 * @tc.desc: test ReadDstbMissionInfosFromParcel when len < 0
220 * @tc.type: FUNC
221 * @tc.require: I5O2P9
222 */
223 HWTEST_F(DistributedMissionInfoTest, testReadDstbMissionInfosFromParcel002, TestSize.Level3)
224 {
225 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel002 start" << std::endl;
226 DstbMissionInfo dstbMissionInfo;
227 Parcel parcel;
228 std::vector<DstbMissionInfo> missionInfos;
229 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
230 parcel.WriteInt32(TEST_INVALID_VALUE);
231 bool ret = dstbMissionInfo.ReadDstbMissionInfosFromParcel(parcel, missionInfos);
232 EXPECT_FALSE(ret);
233 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel002 end" << std::endl;
234 }
235
236 /**
237 * @tc.name: testReadDstbMissionInfosFromParcel003
238 * @tc.desc: test ReadDstbMissionInfosFromParcel when size > parcel.GetReadableBytes()
239 * @tc.type: FUNC
240 * @tc.require: I5O2P9
241 */
242 HWTEST_F(DistributedMissionInfoTest, testReadDstbMissionInfosFromParcel003, TestSize.Level3)
243 {
244 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel003 start" << std::endl;
245 DstbMissionInfo dstbMissionInfo;
246 Parcel parcel;
247 std::vector<DstbMissionInfo> missionInfos;
248 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
249 parcel.WriteInt32(parcel.GetReadableBytes() + 1);
250 bool ret = dstbMissionInfo.ReadDstbMissionInfosFromParcel(parcel, missionInfos);
251 EXPECT_FALSE(ret);
252 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel003 end" << std::endl;
253 }
254
255 /**
256 * @tc.name: testReadDstbMissionInfosFromParcel004
257 * @tc.desc: test ReadDstbMissionInfosFromParcel when size > GET_MAX_MISSIONS
258 * @tc.type: FUNC
259 * @tc.require: I5O2P9
260 */
261 HWTEST_F(DistributedMissionInfoTest, testReadDstbMissionInfosFromParcel004, TestSize.Level3)
262 {
263 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel004 start" << std::endl;
264 DstbMissionInfo dstbMissionInfo;
265 Parcel parcel;
266 std::vector<DstbMissionInfo> missionInfos;
267 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
268 parcel.WriteInt32(Constants::Mission::GET_MAX_MISSIONS + 1);
269 bool ret = dstbMissionInfo.ReadDstbMissionInfosFromParcel(parcel, missionInfos);
270 EXPECT_FALSE(ret);
271 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel004 end" << std::endl;
272 }
273
274 /**
275 * @tc.name: testReadDstbMissionInfosFromParcel005
276 * @tc.desc: test ReadDstbMissionInfosFromParcel when size = GET_MAX_MISSIONS - 1
277 * @tc.type: FUNC
278 * @tc.require: I5O2P9
279 */
280 HWTEST_F(DistributedMissionInfoTest, testReadDstbMissionInfosFromParcel005, TestSize.Level3)
281 {
282 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel005 start" << std::endl;
283 DstbMissionInfo dstbMissionInfo;
284 Parcel parcel;
285 std::vector<DstbMissionInfo> missionInfos;
286 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
287 parcel.WriteInt32(Constants::Mission::GET_MAX_MISSIONS - 1);
288 bool ret = dstbMissionInfo.ReadDstbMissionInfosFromParcel(parcel, missionInfos);
289 EXPECT_FALSE(ret);
290 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel005 end" << std::endl;
291 }
292
293 /**
294 * @tc.name: testReadDstbMissionInfosFromParcel006
295 * @tc.desc: test ReadDstbMissionInfosFromParcel when ptr is nullptr
296 * @tc.type: FUNC
297 * @tc.require: I5O2P9
298 */
299 HWTEST_F(DistributedMissionInfoTest, testReadDstbMissionInfosFromParcel006, TestSize.Level3)
300 {
301 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel006 start" << std::endl;
302 DstbMissionInfo dstbMissionInfo;
303 Parcel parcel;
304 std::vector<DstbMissionInfo> missionInfos;
305 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
306 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
307 parcel.WriteParcelable(nullptr);
308 bool ret = dstbMissionInfo.ReadDstbMissionInfosFromParcel(parcel, missionInfos);
309 EXPECT_FALSE(ret);
310 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel006 end" << std::endl;
311 }
312
313 /**
314 * @tc.name: testReadDstbMissionInfosFromParcel007
315 * @tc.desc: test ReadDstbMissionInfosFromParcel when ptr is not nullptr
316 * @tc.type: FUNC
317 * @tc.require: I5O2P9
318 */
319 HWTEST_F(DistributedMissionInfoTest, testReadDstbMissionInfosFromParcel007, TestSize.Level3)
320 {
321 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel007 start" << std::endl;
322 DstbMissionInfo dstbMissionInfo;
323 Parcel parcel;
324 std::vector<DstbMissionInfo> missionInfos;
325 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
326 parcel.WriteInt32(TEST_PARCEL_WRITE_VALUE);
327 parcel.WriteParcelable(&dstbMissionInfo);
328 bool ret = dstbMissionInfo.ReadDstbMissionInfosFromParcel(parcel, missionInfos);
329 EXPECT_TRUE(ret);
330 EXPECT_EQ(1, missionInfos.size());
331 DTEST_LOG << "DistributedMissionInfoTest testReadDstbMissionInfosFromParcel007 end" << std::endl;
332 }
333
334 /**
335 * @tc.name: testWriteDstbMissionInfosToParcel001
336 * @tc.desc: test WriteDstbMissionInfosToParcel when missionInfos is empty
337 * @tc.type: FUNC
338 * @tc.require: I5O2P9
339 */
340 HWTEST_F(DistributedMissionInfoTest, testWriteDstbMissionInfosToParcel001, TestSize.Level3)
341 {
342 DTEST_LOG << "DistributedMissionInfoTest testWriteDstbMissionInfosToParcel001 start" << std::endl;
343 /**
344 * @tc.steps: step1. test NotifyMissionsChanged when remoteObject is nullptr
345 */
346 sptr<IRemoteObject> remoteObject = nullptr;
347 MissionChangedNotify::NotifyMissionsChanged(remoteObject, TEST_DEVICEID);
348 /**
349 * @tc.steps: step2. test NotifyMissionsChanged when remoteObject is not nullptr
350 */
351 remoteObject = new MockRemoteStub();
352 MissionChangedNotify::NotifyMissionsChanged(remoteObject, TEST_DEVICEID);
353 /**
354 * @tc.steps: step3. test NotifyMissionsChanged when remoteObject return false
355 */
356 MissionChangedNotify::NotifyMissionsChanged(remoteObject, TEST_DEVICEID_TO_RETURN_FALSE);
357 /**
358 * @tc.steps: step4. test WriteDstbMissionInfosToParcel when missionInfos is empty
359 */
360 DstbMissionInfo dstbMissionInfo;
361 Parcel parcel;
362 std::vector<DstbMissionInfo> missionInfos;
363 bool ret = dstbMissionInfo.WriteDstbMissionInfosToParcel(parcel, missionInfos);
364 EXPECT_TRUE(ret);
365 DTEST_LOG << "DistributedMissionInfoTest testWriteDstbMissionInfosToParcel001 end" << std::endl;
366 }
367
368 /**
369 * @tc.name: testWriteDstbMissionInfosToParcel002
370 * @tc.desc: test WriteDstbMissionInfosToParcel
371 * @tc.type: FUNC
372 * @tc.require: I5O2P9
373 */
374 HWTEST_F(DistributedMissionInfoTest, testWriteDstbMissionInfosToParcel002, TestSize.Level3)
375 {
376 DTEST_LOG << "DistributedMissionInfoTest testWriteDstbMissionInfosToParcel002 start" << std::endl;
377 int32_t missionId = 0;
378 /**
379 * @tc.steps: step1. test NotifySnapshot when remoteObject is nullptr
380 */
381 sptr<IRemoteObject> remoteObject = nullptr;
382 MissionChangedNotify::NotifySnapshot(remoteObject, TEST_DEVICEID, missionId);
383 /**
384 * @tc.steps: step2. test NotifySnapshot when remoteObject is not nullptr
385 */
386 remoteObject = new MockRemoteStub();
387 MissionChangedNotify::NotifySnapshot(remoteObject, TEST_DEVICEID, missionId);
388 /**
389 * @tc.steps: step3. test NotifySnapshot when remoteObject return false
390 */
391 MissionChangedNotify::NotifySnapshot(remoteObject, TEST_DEVICEID_TO_RETURN_FALSE, missionId);
392 /**
393 * @tc.steps: step4. test WriteDstbMissionInfosToParcel
394 */
395 DstbMissionInfo dstbMissionInfo;
396 Parcel parcel;
397 std::vector<DstbMissionInfo> missionInfos;
398 missionInfos.emplace_back(dstbMissionInfo);
399 bool ret = dstbMissionInfo.WriteDstbMissionInfosToParcel(parcel, missionInfos);
400 EXPECT_TRUE(ret);
401 DTEST_LOG << "DistributedMissionInfoTest testWriteDstbMissionInfosToParcel002 end" << std::endl;
402 }
403
404 /**
405 * @tc.name: testToString001
406 * @tc.desc: print a missionInfo
407 * @tc.type: FUNC
408 * @tc.require: I5O2P9
409 */
410 HWTEST_F(DistributedMissionInfoTest, testToString001, TestSize.Level3)
411 {
412 int32_t state = 0;
413 /**
414 * @tc.steps: step1. test NotifyNetDisconnect when remoteObject is nullptr
415 */
416 sptr<IRemoteObject> remoteObject = nullptr;
417 MissionChangedNotify::NotifyNetDisconnect(remoteObject, TEST_DEVICEID, state);
418 /**
419 * @tc.steps: step2. test NotifyNetDisconnect when remoteObject is not nullptr
420 */
421 remoteObject = new MockRemoteStub();
422 MissionChangedNotify::NotifyNetDisconnect(remoteObject, TEST_DEVICEID, state);
423 /**
424 * @tc.steps: step3. test NotifyNetDisconnect when remoteObject is not nullptr
425 */
426 MissionChangedNotify::NotifyNetDisconnect(remoteObject, TEST_DEVICEID_TO_RETURN_FALSE, state);
427 /**
428 * @tc.steps: step4. test ToString print a missionInfo.
429 */
430 DstbMissionInfo dstbMissionInfo;
431 auto ret = dstbMissionInfo.ToString();
432 EXPECT_NE(ret, "");
433 }
434
435 /**
436 * @tc.name: testToString002
437 * @tc.desc: test ToString
438 * @tc.type: FUNC
439 * @tc.require: I5O2P9
440 */
441 HWTEST_F(DistributedMissionInfoTest, testToString002, TestSize.Level3)
442 {
443 DTEST_LOG << "DistributedMissionInfoTest testToString002 start" << std::endl;
444 /**
445 * @tc.steps: step1. test ToString.
446 */
447 DstbMissionInfo dstbMissionInfo;
448 dstbMissionInfo.baseWant = std::make_shared<AAFwk::Want>();
449 dstbMissionInfo.topAbility = std::make_shared<AppExecFwk::ElementName>();
450 dstbMissionInfo.baseAbility = std::make_shared<AppExecFwk::ElementName>();
451 dstbMissionInfo.reservedAbility = std::make_shared<AppExecFwk::ElementName>();
452 std::string ret = dstbMissionInfo.ToString();
453 EXPECT_FALSE(ret.empty());
454 /**
455 * @tc.steps: step2. test OnChange when insertEntries is empty.
456 */
457 DistributedDataChangeListener distributedDataChangeListener;
458 std::vector<DistributedKv::Entry> insertEntries;
459 std::vector<DistributedKv::Entry> updateEntries;
460 std::vector<DistributedKv::Entry> deleteEntries;
461 DistributedKv::ChangeNotification changeNotification(std::move(insertEntries),
462 std::move(updateEntries), std::move(deleteEntries), TEST_DEVICE_ID, false);
463 distributedDataChangeListener.OnChange(changeNotification);
464 DTEST_LOG << "DistributedMissionInfoTest testToString002 end" << std::endl;
465 }
466 } // DistributedSchedule
467 } // namespace OHOS