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 <gtest/gtest.h>
17
18 #include "device_profile_errors.h"
19 #include "distributed_device_profile_client.h"
20 #include "profile_change_notification.h"
21 #include "subscribe_info.h"
22 #include "utils.h"
23
24 #define private public
25 #define protected public
26 #include "dp_device_manager.h"
27 #include "profile_change_handler.h"
28 #include "profile_event_handler.h"
29 #include "profile_sync_handler.h"
30 #include "sync_coordinator.h"
31
32 namespace OHOS {
33 namespace DeviceProfile {
34 using namespace testing;
35 using namespace testing::ext;
36
37 class ProfileChangeHandlerTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 std::shared_ptr<ProfileChangeHandler> profileChangeHandler_;
44 };
45
46 class StorageProfileEventCallback : public IProfileEventCallback {
47 };
48
SetUpTestCase()49 void ProfileChangeHandlerTest::SetUpTestCase()
50 {
51 DTEST_LOG << "SetUpTestCase" << std::endl;
52 }
53
TearDownTestCase()54 void ProfileChangeHandlerTest::TearDownTestCase()
55 {
56 DTEST_LOG << "TearDownTestCase" << std::endl;
57 }
58
SetUp()59 void ProfileChangeHandlerTest::SetUp()
60 {
61 DTEST_LOG << "SetUp" << std::endl;
62 if (profileChangeHandler_ == nullptr) {
63 profileChangeHandler_ = std::make_shared<ProfileChangeHandler>("profileChangeTest");
64 }
65 }
66
TearDown()67 void ProfileChangeHandlerTest::TearDown()
68 {
69 DTEST_LOG << "TearDown" << std::endl;
70 }
71
72
73 /**
74 * @tc.name: ConvertEntry_001
75 * @tc.desc: ConvertEntry
76 * @tc.type: FUNC
77 * @tc.require: I4NY1T
78 */
79 HWTEST_F(ProfileChangeHandlerTest, ConvertEntry_001, TestSize.Level3)
80 {
81 DistributedKv::Entry entry1;
82 entry1.key = "key";
83 entry1.value = "value";
84 std::vector<DistributedKv::Entry> inserts;
85 inserts.emplace_back(entry1);
86 ProfileChangeType changeType {ProfileChangeType::UNKNOWN_CHANGE_TYPE};
87 std::vector<ProfileEntry> filteredEntries;
88 Service2Index service2Index;
89 profileChangeHandler_->ConvertEntry(inserts, changeType, filteredEntries, service2Index);
90 EXPECT_TRUE(filteredEntries.empty());
91 }
92
93 /**
94 * @tc.name: ConvertEntry_002
95 * @tc.desc: ConvertEntry
96 * @tc.type: FUNC
97 * @tc.require: I4NY1T
98 */
99 HWTEST_F(ProfileChangeHandlerTest, ConvertEntry_002, TestSize.Level3)
100 {
101 DistributedKv::Entry entry1;
102 entry1.key = "key/key/key";
103 entry1.value = "value";
104 std::vector<DistributedKv::Entry> inserts;
105 inserts.emplace_back(entry1);
106 ProfileChangeType changeType {ProfileChangeType::UNKNOWN_CHANGE_TYPE};
107 std::vector<ProfileEntry> filteredEntries;
108 Service2Index service2Index;
109 profileChangeHandler_->ConvertEntry(inserts, changeType, filteredEntries, service2Index);
110 EXPECT_TRUE(filteredEntries.empty());
111 }
112
113 /**
114 * @tc.name: ConvertEntry_003
115 * @tc.desc: ConvertEntry
116 * @tc.type: FUNC
117 * @tc.require: I4NY1T
118 */
119 HWTEST_F(ProfileChangeHandlerTest, ConvertEntry_003, TestSize.Level3)
120 {
121 DistributedKv::Entry entry1;
122 entry1.key = "1234567812345678123456781234567812345678123456781234567812345678/key/key";
123 entry1.value = "value";
124 std::vector<DistributedKv::Entry> inserts;
125 inserts.emplace_back(entry1);
126 ProfileChangeType changeType {ProfileChangeType::UNKNOWN_CHANGE_TYPE};
127 std::vector<ProfileEntry> filteredEntries;
128 Service2Index service2Index;
129 profileChangeHandler_->ConvertEntry(inserts, changeType, filteredEntries, service2Index);
130 EXPECT_TRUE(filteredEntries.empty());
131 }
132
133 /**
134 * @tc.name: ConvertEntry_004
135 * @tc.desc: ConvertEntry
136 * @tc.type: FUNC
137 * @tc.require: I4NY1T
138 */
139 HWTEST_F(ProfileChangeHandlerTest, ConvertEntry_004, TestSize.Level3)
140 {
141 DistributedKv::Entry entry1;
142 entry1.key = "1234567812345678123456781234567812345678123456781234567812345678/1/key";
143 entry1.value = "value";
144 std::vector<DistributedKv::Entry> inserts;
145 inserts.emplace_back(entry1);
146 ProfileChangeType changeType {ProfileChangeType::UNKNOWN_CHANGE_TYPE};
147 std::vector<ProfileEntry> filteredEntries;
148 Service2Index service2Index;
149 profileChangeHandler_->ConvertEntry(inserts, changeType, filteredEntries, service2Index);
150 EXPECT_TRUE(filteredEntries.empty());
151 }
152
153 /**
154 * @tc.name: ConvertEntry_005
155 * @tc.desc: ConvertEntry
156 * @tc.type: FUNC
157 * @tc.require: I4NY1T
158 */
159 HWTEST_F(ProfileChangeHandlerTest, ConvertEntry_005, TestSize.Level3)
160 {
161 DistributedKv::Entry entry1;
162 entry1.key = "1234567812345678123456781234567812345678123456781234567812345678/0/1";
163 entry1.value = "value";
164 std::vector<DistributedKv::Entry> inserts;
165 inserts.emplace_back(entry1);
166 ProfileChangeType changeType {ProfileChangeType::UNKNOWN_CHANGE_TYPE};
167 std::vector<ProfileEntry> filteredEntries;
168 Service2Index service2Index;
169 profileChangeHandler_->ConvertEntry(inserts, changeType, filteredEntries, service2Index);
170 EXPECT_FALSE(filteredEntries.empty());
171 EXPECT_FALSE(service2Index.empty());
172 }
173
174 /**
175 * @tc.name: Subscribe_001
176 * @tc.desc: Subscribe
177 * @tc.type: FUNC
178 * @tc.require: I4NY1T
179 */
180 HWTEST_F(ProfileChangeHandlerTest, Subscribe_001, TestSize.Level3)
181 {
182 SubscribeInfo subInfo;
183 subInfo.extraInfo = {{"serviceIds", "1111"}};
184 auto syncCb = std::make_shared<StorageProfileEventCallback>();
185 sptr<IRemoteObject> notifier =
186 sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
187 int32_t ret = profileChangeHandler_->Subscribe(subInfo, notifier);
188 ASSERT_EQ(ret, ERR_DP_PERMISSION_DENIED);
189 }
190
191 /**
192 * @tc.name: FilterChangedProfileLocked_001
193 * @tc.desc: FilterChangedProfileLocked
194 * @tc.type: FUNC
195 * @tc.require: I4NY1T
196 */
197 HWTEST_F(ProfileChangeHandlerTest, FilterChangedProfileLocked_001, TestSize.Level3)
198 {
199 SubscribeInfo subInfo;
200 subInfo.extraInfo = {
201 {"deviceId", ""},
202 {"serviceIds", {}}
203 };
204 ProfileChangeNotification profileNotification;
205 FilterInfo filterInfo;
206 Service2Index service2Index;
207 profileChangeHandler_->FilterChangedProfileLocked(subInfo, profileNotification,
208 service2Index, filterInfo);
209 ASSERT_FALSE(filterInfo.filtered);
210 }
211
212 /**
213 * @tc.name: FilterChangedProfileLocked_002
214 * @tc.desc: FilterChangedProfileLocked
215 * @tc.type: FUNC
216 * @tc.require: I4NY1T
217 */
218 HWTEST_F(ProfileChangeHandlerTest, FilterChangedProfileLocked_002, TestSize.Level3)
219 {
220 SubscribeInfo subInfo;
221 subInfo.extraInfo = {
222 {"deviceId", "111"}
223 };
224 ProfileChangeNotification profileNotification;
225 FilterInfo filterInfo;
226 Service2Index service2Index;
227 profileChangeHandler_->FilterChangedProfileLocked(subInfo, profileNotification,
228 service2Index, filterInfo);
229 ASSERT_TRUE(filterInfo.filtered);
230 }
231
232 /**
233 * @tc.name: FilterChangedProfileLocked_003
234 * @tc.desc: FilterChangedProfileLocked
235 * @tc.type: FUNC
236 * @tc.require: I4NY1T
237 */
238 HWTEST_F(ProfileChangeHandlerTest, FilterChangedProfileLocked_003, TestSize.Level3)
239 {
240 SubscribeInfo subInfo;
241 subInfo.extraInfo = {
242 {"deviceId", ""},
243 {"serviceIds", ""}
244 };
245 ProfileChangeNotification profileNotification;
246 FilterInfo filterInfo;
247 Service2Index service2Index;
248 profileChangeHandler_->FilterChangedProfileLocked(subInfo, profileNotification,
249 service2Index, filterInfo);
250 ASSERT_TRUE(filterInfo.filtered);
251 }
252
253 /**
254 * @tc.name: FilterChangedProfileLocked_004
255 * @tc.desc: FilterChangedProfileLocked
256 * @tc.type: FUNC
257 * @tc.require: I4NY1T
258 */
259 HWTEST_F(ProfileChangeHandlerTest, FilterChangedProfileLocked_004, TestSize.Level3)
260 {
261 SubscribeInfo subInfo;
262 subInfo.extraInfo = {
263 {"deviceId", ""},
264 {"serviceIds", "1111"}
265 };
266 ProfileChangeNotification profileNotification;
267 FilterInfo filterInfo;
268 Service2Index service2Index;
269 profileChangeHandler_->FilterChangedProfileLocked(subInfo, profileNotification,
270 service2Index, filterInfo);
271 ASSERT_TRUE(filterInfo.filtered);
272 }
273
274 /**
275 * @tc.name: FilterChangedProfileLocked_005
276 * @tc.desc: FilterChangedProfileLocked
277 * @tc.type: FUNC
278 * @tc.require: I4NY1T
279 */
280 HWTEST_F(ProfileChangeHandlerTest, FilterChangedProfileLocked_005, TestSize.Level3)
281 {
282 SubscribeInfo subInfo;
283 subInfo.extraInfo = {
284 {"deviceId", ""},
285 {"serviceIds", "1111"}
286 };
287 ProfileChangeNotification profileNotification;
288 FilterInfo filterInfo;
289 Service2Index service2Index = {{"1111", 1}};
290 profileChangeHandler_->FilterChangedProfileLocked(subInfo, profileNotification,
291 service2Index, filterInfo);
292 ASSERT_TRUE(filterInfo.filtered);
293 ASSERT_FALSE(filterInfo.indexes.empty());
294 }
295
296 /**
297 * @tc.name: NotifyProfileChangedLocked_001
298 * @tc.desc: NotifyProfileChangedLocked
299 * @tc.type: FUNC
300 * @tc.require: I4NY1T
301 */
302 HWTEST_F(ProfileChangeHandlerTest, NotifyProfileChangedLocked_001, TestSize.Level3)
303 {
304 ProfileChangeNotification profileNotification;
305 FilterInfo filterInfo;
306 auto syncCb = std::make_shared<StorageProfileEventCallback>();
307 sptr<IProfileEventNotifier> notifier =
308 sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
309 profileChangeHandler_->NotifyProfileChangedLocked(profileNotification, filterInfo, notifier);
310 ASSERT_FALSE(filterInfo.filtered);
311 }
312
313 /**
314 * @tc.name: NotifyProfileChangedLocked_002
315 * @tc.desc: NotifyProfileChangedLocked
316 * @tc.type: FUNC
317 * @tc.require: I4NY1T
318 */
319 HWTEST_F(ProfileChangeHandlerTest, NotifyProfileChangedLocked_002, TestSize.Level3)
320 {
321 ProfileChangeNotification profileNotification;
322 FilterInfo filterInfo;
323 filterInfo.filtered = true;
324 auto syncCb = std::make_shared<StorageProfileEventCallback>();
325 sptr<IProfileEventNotifier> notifier =
326 sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
327 profileChangeHandler_->NotifyProfileChangedLocked(profileNotification, filterInfo, notifier);
328 ASSERT_TRUE(filterInfo.indexes.empty());
329 }
330
331 /**
332 * @tc.name: NotifyProfileChangedLocked_003
333 * @tc.desc: NotifyProfileChangedLocked
334 * @tc.type: FUNC
335 * @tc.require: I4NY1T
336 */
337 HWTEST_F(ProfileChangeHandlerTest, NotifyProfileChangedLocked_003, TestSize.Level3)
338 {
339 ProfileEntry entry;
340 std::vector<ProfileEntry> entries(1, entry);
341 std::string networkId = "1";
342 ProfileChangeNotification profileNotification(entries, networkId, true);
343 FilterInfo filterInfo;
344 filterInfo.filtered = true;
345 filterInfo.indexes.emplace(0);
346 auto syncCb = std::make_shared<StorageProfileEventCallback>();
347 sptr<IProfileEventNotifier> notifier =
348 sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
349 profileChangeHandler_->NotifyProfileChangedLocked(profileNotification, filterInfo, notifier);
350 ASSERT_FALSE(filterInfo.indexes.empty());
351 }
352
353 /**
354 * @tc.name: NotifyProfileChanged_001
355 * @tc.desc: NotifyProfileChanged
356 * @tc.type: FUNC
357 * @tc.require: I4NY1T
358 */
359 HWTEST_F(ProfileChangeHandlerTest, NotifyProfileChanged_001, TestSize.Level3)
360 {
361 auto syncCb = std::make_shared<StorageProfileEventCallback>();
362 sptr<IRemoteObject> notifier =
363 sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
364
365 SubscribeInfo subInfo;
366 subInfo.extraInfo = {
367 {"deviceId", ""},
368 {"serviceIds", "1111"}
369 };
370
371 profileChangeHandler_->profileEventSubscribeInfos_[notifier] = subInfo;
372 Service2Index service2Index;
373 ProfileChangeNotification profileNotification;
374 profileChangeHandler_->NotifyProfileChanged(profileNotification, service2Index);
375 ASSERT_TRUE(service2Index.empty());
376 }
377
378 /**
379 * @tc.name: OnChange_001
380 * @tc.desc: OnChange
381 * @tc.type: FUNC
382 * @tc.require: I4NY1T
383 */
384 HWTEST_F(ProfileChangeHandlerTest, OnChange_001, TestSize.Level3)
385 {
386 std::shared_ptr<ProfileSyncHandler> profileSyncHandler_ =
387 std::make_shared<ProfileSyncHandler>("profileSyncTest");
388 std::vector<std::string> deviceIds = {"", std::move("testudid123"), ""};
389 std::map<std::string, DistributedKv::Status> results;
390 SyncCoordinator::GetInstance().Init();
391 SyncCoordinator::GetInstance().isOnlineTrigger_ = false;
392 profileSyncHandler_->SyncCompleted(results);
393 SyncCoordinator::GetInstance().isOnlineTrigger_ = true;
394 profileSyncHandler_->SyncCompleted(results);
395 results["success123"] = Status::SUCCESS;
396 results["test1"] = Status::ERROR;
397 profileSyncHandler_->SyncCompleted(results);
398 results.clear();
399 results["success123"] = Status::SUCCESS;
400 results["test1"] = Status::ERROR;
401 results["test2"] = Status::ERROR;
402 results["test3"] = Status::ERROR;
403 results["test4"] = Status::ERROR;
404 std::vector<std::string> deviceIds1;
405 deviceIds1.emplace_back("test1");
406 deviceIds1.emplace_back("test2");
407 deviceIds1.emplace_back("test3");
408 deviceIds1.emplace_back("test4");
409 DpDeviceManager::GetInstance().deviceIdsList_.emplace_back(deviceIds1);
410 profileSyncHandler_->SyncCompleted(results);
411 DistributedKv::ChangeNotification profileNotification({}, {}, {}, "", true);
412 profileChangeHandler_->OnChange(profileNotification);
413 ASSERT_TRUE(profileChangeHandler_->profileEventSubscribeInfos_.empty());
414 }
415
416 /**
417 * @tc.name: OnChange_002
418 * @tc.desc: OnChange
419 * @tc.type: FUNC
420 * @tc.require: I4NY1T
421 */
422 HWTEST_F(ProfileChangeHandlerTest, OnChange_002, TestSize.Level3)
423 {
424 std::vector<DistributedKv::Entry> insertEntries;
425 std::vector<DistributedKv::Entry> updateEntries;
426 std::vector<DistributedKv::Entry> deleteEntries;
427 DistributedKv::Entry entry;
428 entry.key = "key";
429 entry.value = "value";
430 insertEntries.emplace_back(entry);
431 updateEntries.emplace_back(entry);
432 deleteEntries.emplace_back(entry);
433 std::string deviceId = "device";
434 DistributedKv::ChangeNotification profileNotification(std::move(insertEntries), std::move(updateEntries),
435 std::move(deleteEntries), deviceId, false);
436 profileChangeHandler_->OnChange(profileNotification);
437 ASSERT_TRUE(profileChangeHandler_->profileEventSubscribeInfos_.empty());
438 }
439 }
440 }