• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "device_profile_storage_manager.h"
20 #include "distributed_device_profile_client.h"
21 #include "ipc_skeleton.h"
22 #include "profile_event.h"
23 #include "subscribe_info.h"
24 #include "subscriber_death_recipient.h"
25 #include "utils.h"
26 
27 #define private public
28 #include "profile_event_handler_factory.h"
29 #include "profile_sync_handler.h"
30 #include "subscribe_manager.h"
31 
32 namespace OHOS {
33 namespace DeviceProfile {
34 using namespace testing;
35 using namespace testing::ext;
36 
37 namespace {
38     constexpr int32_t MAX_SUBSCRIBS_PER_UID = 100;
39 }
40 
41 class SubscribeManagerTest : public testing::Test {
42 public:
43     static void SetUpTestCase();
44     static void TearDownTestCase();
45     void SetUp();
46     void TearDown();
47 };
48 
SetUpTestCase()49 void SubscribeManagerTest::SetUpTestCase()
50 {
51     DTEST_LOG << "SetUpTestCase" << std::endl;
52     DeviceProfileStorageManager::GetInstance().Init();
53 }
54 
TearDownTestCase()55 void SubscribeManagerTest::TearDownTestCase()
56 {
57     DTEST_LOG << "TearDownTestCase" << std::endl;
58 }
59 
SetUp()60 void SubscribeManagerTest::SetUp()
61 {
62     DTEST_LOG << "SetUp" << std::endl;
63 }
64 
TearDown()65 void SubscribeManagerTest::TearDown()
66 {
67     DTEST_LOG << "TearDown" << std::endl;
68 }
69 
70 class StorageProfileEventCallback : public IProfileEventCallback {
71 };
72 
73 /**
74  * @tc.name: IncSubsOfUidLocked_001
75  * @tc.desc: IncSubsOfUidLocked
76  * @tc.type: FUNC
77  * @tc.require: I4NY1T
78  */
79 HWTEST_F(SubscribeManagerTest, IncSubsOfUidLocked_001, TestSize.Level3)
80 {
81     int32_t mockUid = -1;
82     SubscribeManager::GetInstance().IncSubsOfUidLocked(mockUid);
83     int32_t expectedCount = SubscribeManager::GetInstance().uidSubsMap_[mockUid];
84     EXPECT_EQ(expectedCount, 1);
85     SubscribeManager::GetInstance().IncSubsOfUidLocked(mockUid);
86     expectedCount = SubscribeManager::GetInstance().uidSubsMap_[mockUid];
87     EXPECT_EQ(expectedCount, 2);
88     SubscribeManager::GetInstance().uidSubsMap_.clear();
89 }
90 
91 /**
92  * @tc.name: CheckSubsOfUid_001
93  * @tc.desc: IncSubsOfUidLocked
94  * @tc.type: FUNC
95  * @tc.require: I4NY1T
96  */
97 HWTEST_F(SubscribeManagerTest, CheckSubsOfUid_001, TestSize.Level3)
98 {
99     int32_t mockUid = -1;
100     bool ret = SubscribeManager::GetInstance().CheckSubsOfUid(mockUid);
101     EXPECT_TRUE(ret);
102     SubscribeManager::GetInstance().uidSubsMap_.clear();
103 }
104 
105 /**
106  * @tc.name: CheckSubsOfUid_002
107  * @tc.desc: CheckSubsOfUid
108  * @tc.type: FUNC
109  * @tc.require: I4NY1T
110  */
111 HWTEST_F(SubscribeManagerTest, CheckSubsOfUid_002, TestSize.Level3)
112 {
113     int32_t mockUid = -1;
114     SubscribeManager::GetInstance().IncSubsOfUidLocked(mockUid);
115     bool ret = SubscribeManager::GetInstance().CheckSubsOfUid(mockUid);
116     EXPECT_TRUE(ret);
117     SubscribeManager::GetInstance().uidSubsMap_.clear();
118 }
119 
120 /**
121  * @tc.name: CheckSubsOfUid_003
122  * @tc.desc: CheckSubsOfUid
123  * @tc.type: FUNC
124  * @tc.require: I4NY1T
125  */
126 HWTEST_F(SubscribeManagerTest, CheckSubsOfUid_003, TestSize.Level3)
127 {
128     int32_t mockUid = -1;
129     SubscribeManager::GetInstance().uidSubsMap_[mockUid] = MAX_SUBSCRIBS_PER_UID;
130     bool ret = SubscribeManager::GetInstance().CheckSubsOfUid(mockUid);
131     EXPECT_FALSE(ret);
132     SubscribeManager::GetInstance().uidSubsMap_.clear();
133 }
134 
135 /**
136  * @tc.name: DecSubsOfUidLocked_001
137  * @tc.desc: DecSubsOfUidLocked
138  * @tc.type: FUNC
139  * @tc.require: I4NY1T
140  */
141 HWTEST_F(SubscribeManagerTest, DecSubsOfUidLocked_001, TestSize.Level3)
142 {
143     int32_t mockUid = -1;
144     SubscribeManager::GetInstance().DecSubsOfUidLocked(mockUid);
145     bool ret = SubscribeManager::GetInstance().uidSubsMap_.empty();
146     EXPECT_TRUE(ret);
147 }
148 
149 /**
150  * @tc.name: DecSubsOfUidLocked_002
151  * @tc.desc: DecSubsOfUidLocked
152  * @tc.type: FUNC
153  * @tc.require: I4NY1T
154  */
155 HWTEST_F(SubscribeManagerTest, DecSubsOfUidLocked_002, TestSize.Level3)
156 {
157     int32_t mockUid = -1;
158     SubscribeManager::GetInstance().IncSubsOfUidLocked(mockUid);
159     bool ret = SubscribeManager::GetInstance().uidSubsMap_.empty();
160     EXPECT_FALSE(ret);
161     SubscribeManager::GetInstance().DecSubsOfUidLocked(mockUid);
162     ret = SubscribeManager::GetInstance().uidSubsMap_.empty();
163     EXPECT_TRUE(ret);
164 }
165 
166 /**
167  * @tc.name: DecSubsOfUidLocked_002
168  * @tc.desc: DecSubsOfUidLocked
169  * @tc.type: FUNC
170  * @tc.require: I4NY1T
171  */
172 HWTEST_F(SubscribeManagerTest, DecSubsOfUidLocked_003, TestSize.Level3)
173 {
174     int32_t mockUid = -1;
175     SubscribeManager::GetInstance().uidSubsMap_[mockUid] = MAX_SUBSCRIBS_PER_UID;
176     bool ret = SubscribeManager::GetInstance().uidSubsMap_.empty();
177     EXPECT_FALSE(ret);
178     SubscribeManager::GetInstance().DecSubsOfUidLocked(mockUid);
179     ret = SubscribeManager::GetInstance().uidSubsMap_.empty();
180     EXPECT_FALSE(ret);
181     SubscribeManager::GetInstance().uidSubsMap_.clear();
182 }
183 
184 /**
185  * @tc.name: OnSubscriberDied_001
186  * @tc.desc: OnSubscriberDied
187  * @tc.type: FUNC
188  * @tc.require: I4NY1T
189  */
190 HWTEST_F(SubscribeManagerTest, OnSubscriberDied_001, TestSize.Level3)
191 {
192     sptr<IRemoteObject> object;
193     SubscribeManager::GetInstance().OnSubscriberDied(object);
194     bool ret = SubscribeManager::GetInstance().notifiersMap_.empty();
195     EXPECT_TRUE(ret);
196 }
197 
198 /**
199  * @tc.name: Init_001
200  * @tc.desc: Init
201  * @tc.type: FUNC
202  * @tc.require: I4NY1T
203  */
204 HWTEST_F(SubscribeManagerTest, Init_001, TestSize.Level3)
205 {
206     bool ret = SubscribeManager::GetInstance().Init();
207     EXPECT_TRUE(ret);
208 }
209 
210 /**
211  * @tc.name: SubscribeProfileEvents_001
212  * @tc.desc: SubscribeProfileEvents
213  * @tc.type: FUNC
214  * @tc.require: I4NY1T
215  */
216 HWTEST_F(SubscribeManagerTest, SubscribeProfileEvents_001, TestSize.Level3)
217 {
218     auto syncCb = std::make_shared<StorageProfileEventCallback>();
219     sptr<IRemoteObject> notifier =
220         sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
221     std::list<SubscribeInfo> subscribeInfos;
222     std::list<ProfileEvent> failedEvents;
223     int32_t ret = SubscribeManager::GetInstance().SubscribeProfileEvents(subscribeInfos, notifier, failedEvents);
224     EXPECT_EQ(ret, ERR_OK);
225 }
226 
227 /**
228  * @tc.name: SubscribeProfileEvents_001
229  * @tc.desc: SubscribeProfileEvents
230  * @tc.type: FUNC
231  * @tc.require: I4NY1T
232  */
233 HWTEST_F(SubscribeManagerTest, SubscribeProfileEvents_002, TestSize.Level3)
234 {
235     auto syncCb = std::make_shared<StorageProfileEventCallback>();
236     sptr<IRemoteObject> notifier =
237         sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
238     std::list<SubscribeInfo> subscribeInfos;
239     SubscribeInfo eventChange;
240     subscribeInfos.emplace_back(eventChange);
241     std::list<ProfileEvent> failedEvents;
242     int32_t ret = SubscribeManager::GetInstance().SubscribeProfileEvents(subscribeInfos, notifier, failedEvents);
243     EXPECT_EQ(ret, ERR_DP_INVALID_PARAMS);
244 }
245 
246 /**
247  * @tc.name: SubscribeProfileEvents_003
248  * @tc.desc: SubscribeProfileEvents
249  * @tc.type: FUNC
250  * @tc.require: I4NY1T
251  */
252 HWTEST_F(SubscribeManagerTest, SubscribeProfileEvents_003, TestSize.Level3)
253 {
254     auto syncCb = std::make_shared<StorageProfileEventCallback>();
255     sptr<IRemoteObject> notifier =
256         sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
257 
258     std::list<SubscribeInfo> subscribeInfos;
259     SubscribeInfo eventChange;
260     eventChange.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
261     ExtraInfo extraInfo;
262     eventChange.extraInfo = std::move(extraInfo);
263     subscribeInfos.emplace_back(eventChange);
264 
265     std::list<ProfileEvent> failedEvents;
266 
267     int32_t callingUid = IPCSkeleton::GetCallingUid();
268     SubscribeManager::GetInstance().uidSubsMap_[callingUid] = MAX_SUBSCRIBS_PER_UID;
269     int32_t ret = SubscribeManager::GetInstance().SubscribeProfileEvents(subscribeInfos, notifier, failedEvents);
270     EXPECT_EQ(ret, ERR_DP_SUBSCRIBE_LIMIT_EXCEEDED);
271 }
272 
273 /**
274  * @tc.name: UnsubscribeProfileEvents_001
275  * @tc.desc: UnsubscribeProfileEvents
276  * @tc.type: FUNC
277  * @tc.require: I4NY1T
278  */
279 HWTEST_F(SubscribeManagerTest, UnsubscribeProfileEvents_001, TestSize.Level3)
280 {
281     auto syncCb = std::make_shared<StorageProfileEventCallback>();
282     sptr<IRemoteObject> notifier =
283         sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
284 
285     std::list<ProfileEvent> profileEvents = { ProfileEvent::EVENT_SYNC_COMPLETED };
286     std::list<ProfileEvent> failedEvents;
287 
288     int32_t ret = SubscribeManager::GetInstance().UnsubscribeProfileEvents(profileEvents, notifier, failedEvents);
289     EXPECT_EQ(ret, ERR_OK);
290 }
291 
292 /**
293  * @tc.name: TryAddNotifierLocked_001
294  * @tc.desc: TryAddNotifierLocked
295  * @tc.type: FUNC
296  * @tc.require: I4NY1T
297  */
298 HWTEST_F(SubscribeManagerTest, TryAddNotifierLocked_001, TestSize.Level3)
299 {
300     auto syncCb = std::make_shared<StorageProfileEventCallback>();
301     sptr<IRemoteObject> notifier =
302         sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
303 
304     ProfileEvents subProfileEvents;
305     subProfileEvents.set(1);
306     ASSERT_TRUE(SubscribeManager::GetInstance().subscriberDeathRecipient_ != nullptr);
307     SubscribeManager::GetInstance().TryAddNotifierLocked(notifier, subProfileEvents);
308     bool ret = SubscribeManager::GetInstance().notifiersMap_.empty();
309     EXPECT_FALSE(ret);
310     SubscribeManager::GetInstance().TryAddNotifierLocked(notifier, subProfileEvents);
311     SubscribeManager::GetInstance().notifiersMap_.clear();
312     ret = SubscribeManager::GetInstance().notifiersMap_.empty();
313     EXPECT_TRUE(ret);
314 }
315 
316 /**
317  * @tc.name: TryRemoveNotiferLocked_001
318  * @tc.desc: TryRemoveNotiferLocked
319  * @tc.type: FUNC
320  * @tc.require: I4NY1T
321  */
322 HWTEST_F(SubscribeManagerTest, TryRemoveNotiferLocked001, TestSize.Level3)
323 {
324     auto syncCb = std::make_shared<StorageProfileEventCallback>();
325     sptr<IRemoteObject> notifier =
326         sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub (syncCb));
327 
328     ProfileEvents subProfileEvents;
329     subProfileEvents.set(1);
330     ASSERT_TRUE(SubscribeManager::GetInstance().subscriberDeathRecipient_ != nullptr);
331     SubscribeManager::GetInstance().TryRemoveNotiferLocked(notifier, subProfileEvents);
332     bool ret = SubscribeManager::GetInstance().notifiersMap_.empty();
333     EXPECT_TRUE(ret);
334 }
335 
336 /**
337  * @tc.name: TryRemoveNotiferLocked_002
338  * @tc.desc: TryRemoveNotiferLocked
339  * @tc.type: FUNC
340  * @tc.require: I4NY1T
341  */
342 HWTEST_F(SubscribeManagerTest, TryRemoveNotiferLocked002, TestSize.Level3)
343 {
344     auto syncCb = std::make_shared<StorageProfileEventCallback>();
345     sptr<IRemoteObject> notifier =
346         sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
347 
348     ProfileEvents subProfileEvents;
349     subProfileEvents.set(1);
350     ASSERT_TRUE(SubscribeManager::GetInstance().subscriberDeathRecipient_ != nullptr);
351     SubscribeManager::GetInstance().TryAddNotifierLocked(notifier, subProfileEvents);
352     bool ret = SubscribeManager::GetInstance().notifiersMap_.empty();
353     EXPECT_FALSE(ret);
354     SubscribeManager::GetInstance().TryRemoveNotiferLocked(notifier, subProfileEvents);
355     ret = SubscribeManager::GetInstance().notifiersMap_.empty();
356     EXPECT_TRUE(ret);
357 }
358 
359 /**
360  * @tc.name: TryRemoveNotiferLocked_003
361  * @tc.desc: TryRemoveNotiferLocked
362  * @tc.type: FUNC
363  * @tc.require: I4NY1T
364  */
365 HWTEST_F(SubscribeManagerTest, TryRemoveNotiferLocked003, TestSize.Level3)
366 {
367     auto syncCb = std::make_shared<StorageProfileEventCallback>();
368     sptr<IRemoteObject> notifier =
369         sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
370 
371     ProfileEvents subProfileEvents;
372     subProfileEvents.set(1);
373     SubscribeManager::GetInstance().TryAddNotifierLocked(notifier, subProfileEvents);
374     bool ret = SubscribeManager::GetInstance().notifiersMap_.empty();
375     EXPECT_FALSE(ret);
376     ProfileEvents unSubProfileEvents;
377     unSubProfileEvents.set(0);
378     SubscribeManager::GetInstance().TryRemoveNotiferLocked(notifier, unSubProfileEvents);
379     ret = SubscribeManager::GetInstance().notifiersMap_.empty();
380     EXPECT_FALSE(ret);
381 }
382 
383 /**
384  * @tc.name: Unregister_001
385  * @tc.desc: Unregister
386  * @tc.type: FUNC
387  * @tc.require: I4NY1T
388  */
389 HWTEST_F(SubscribeManagerTest, Unregister_001, TestSize.Level3)
390 {
391     auto handler = std::make_shared<ProfileSyncHandler>("syncHandler");
392     auto result = handler->Unregister();
393 
394     std::map<std::string, Status> syncResult;
395     syncResult["testdeviceid"] = Status::SUCCESS;
396     handler->SyncCompleted(syncResult);
397 
398     SyncResult syncResults;
399     syncResults.emplace("testdeviceid", SUCCEEDED);
400     handler->NotifySyncCompleted(syncResults);
401 
402     auto syncCb = std::make_shared<StorageProfileEventCallback>();
403     wptr<IRemoteObject> notifier =
404     sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
405     auto death = std::make_shared<SubscriberDeathRecipient>();
406     death->OnRemoteDied(notifier);
407     DTEST_LOG << "result: " << result << std::endl;
408     EXPECT_EQ(0, result);
409 }
410 
411 /**
412  * @tc.name: GetProfileChangeHandlerInner_001
413  * @tc.desc: GetProfileChangeHandlerInner
414  * @tc.type: FUNC
415  * @tc.require: I4NY1T
416  */
417 HWTEST_F(SubscribeManagerTest, GetProfileChangeHandlerInner_001, TestSize.Level3)
418 {
419     auto result = ProfileEventHandlerFactory::GetInstance().GetProfileChangeHandlerInner();
420     DTEST_LOG << "result: " << result << std::endl;
421     EXPECT_NE(nullptr, result);
422 }
423 }
424 }
425