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