• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "utils.h"
19 #include "test_util.h"
20 
21 #define private public
22 #define protected public
23 #include "device_profile_errors.h"
24 #include "device_profile_log.h"
25 #include "distributed_device_profile_client.h"
26 #include "hisysevent.h"
27 #include "nlohmann/json.hpp"
28 #include "syscap_info_collector.h"
29 #include "syscap_interface.h"
30 #undef private
31 #undef protected
32 
33 namespace OHOS {
34 namespace DeviceProfile {
35 using namespace OHOS::HiviewDFX;
36 namespace {
37     const std::string TAG = "SyscapInfoCollector";
38     const std::string SERVICE_ID = "syscap";
39     const std::string SERVICE_TYPE = "syscap";
40     const std::string CHARACTER_PRIVATE_SYSCAP = "privatesyscap";
41     const std::string CHARACTER_OS_SYSCAP = "ossyscap";
42     const std::string DEVICE_PROFILE_SYNC_FAILED = "DEVICE_PROFILE_SYNC_FAILED";
43     const std::string FAULT_CODE_KEY = "FAULT_CODE";
44     const std::string DOMAIN_NAME = std::string(HiSysEvent::Domain::DEVICE_PROFILE);
45 }
46 using namespace testing;
47 using namespace testing::ext;
48 
49 class ProfileCrudTest : public testing::Test {
50 public:
51     static void SetUpTestCase();
52     static void TearDownTestCase();
53     void SetUp();
54     void TearDown();
55 };
56 
SetUpTestCase()57 void ProfileCrudTest::SetUpTestCase()
58 {
59     DTEST_LOG << "SetUpTestCase" << std::endl;
60 }
61 
TearDownTestCase()62 void ProfileCrudTest::TearDownTestCase()
63 {
64     DTEST_LOG << "TearDownTestCase" << std::endl;
65 }
66 
SetUp()67 void ProfileCrudTest::SetUp()
68 {
69     DTEST_LOG << "SetUp" << std::endl;
70 }
71 
TearDown()72 void ProfileCrudTest::TearDown()
73 {
74     DTEST_LOG << "TearDown" << std::endl;
75 }
76 
77 class ProfileEventCallback : public IProfileEventCallback {
78 public:
79     ProfileEventCallback() = default;
80     ~ProfileEventCallback() = default;
81 
OnSyncCompleted(const SyncResult & syncResults)82     void OnSyncCompleted(const SyncResult& syncResults) override
83     {
84     }
85 
OnProfileChanged(const ProfileChangeNotification & changeNotification)86     void OnProfileChanged(const ProfileChangeNotification& changeNotification) override
87     {
88         if (!subServiceIds_.empty()) {
89             const auto& profileEntries = changeNotification.GetProfileEntries();
90             for (const auto& ProfileEntry : profileEntries) {
91                 auto key = ProfileEntry.key;
92                 DTEST_LOG << "key: " << key << std::endl;
93                 numNotifications_++;
94             }
95         }
96     }
97 
SetSubServiceIds(const std::list<std::string> & subServiceIds)98     void SetSubServiceIds(const std::list<std::string>& subServiceIds)
99     {
100         subServiceIds_ = subServiceIds;
101     }
102 
GetNotificationNum() const103     int32_t GetNotificationNum() const
104     {
105         return numNotifications_;
106     }
107 
108 private:
109     std::list<std::string> subServiceIds_;
110     int32_t numNotifications_ {0};
111 };
112 
113 /**
114  * @tc.name: PutDeviceProfile_001
115  * @tc.desc: put device profile with empty service id
116  * @tc.type: FUNC
117  * @tc.require: I4NY23
118  */
119 HWTEST_F(ProfileCrudTest, PutDeviceProfile_001, TestSize.Level3)
120 {
121     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
122     if (dps == nullptr) {
123         DTEST_LOG << "device profile service is nullptr" << std::endl;
124         return;
125     }
126 
127     ServiceCharacteristicProfile profile;
128     profile.SetServiceId("");
129     profile.SetServiceType("test");
130     nlohmann::json j;
131     j["testVersion"] = "3.0.0";
132     j["testApiLevel"] = 7;
133     profile.SetCharacteristicProfileJson(j.dump());
134     int32_t result = DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
135     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
136 }
137 
138 /**
139  * @tc.name: PutDeviceProfile_002
140  * @tc.desc: put device profile with empty service type
141  * @tc.type: FUNC
142  * @tc.require: I4NY23
143  */
144 HWTEST_F(ProfileCrudTest, PutDeviceProfile_002, TestSize.Level3)
145 {
146     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
147     if (dps == nullptr) {
148         DTEST_LOG << "device profile service is nullptr" << std::endl;
149         return;
150     }
151 
152     ServiceCharacteristicProfile profile;
153     profile.SetServiceId("test");
154     profile.SetServiceType("");
155     nlohmann::json j;
156     j["testVersion"] = "3.0.0";
157     j["testApiLevel"] = 7;
158     profile.SetCharacteristicProfileJson(j.dump());
159     int32_t result = DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
160     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
161 }
162 
163 /**
164  * @tc.name: PutDeviceProfile_003
165  * @tc.desc: put device profile with empty characteristics
166  * @tc.type: FUNC
167  * @tc.require: I4NY23
168  */
169 HWTEST_F(ProfileCrudTest, PutDeviceProfile_003, TestSize.Level3)
170 {
171     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
172     if (dps == nullptr) {
173         DTEST_LOG << "device profile service is nullptr" << std::endl;
174         return;
175     }
176 
177     ServiceCharacteristicProfile profile;
178     profile.SetServiceId("test");
179     profile.SetServiceType("test");
180     nlohmann::json j;
181     // the result string is "null"
182     profile.SetCharacteristicProfileJson(j.dump());
183     int32_t result = DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
184     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
185 }
186 
187 /**
188  * @tc.name: PutDeviceProfile_004
189  * @tc.desc: put device profile without set characteristics
190  * @tc.type: FUNC
191  * @tc.require: I4NY23
192  */
193 HWTEST_F(ProfileCrudTest, PutDeviceProfile_004, TestSize.Level3)
194 {
195     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
196     if (dps == nullptr) {
197         DTEST_LOG << "device profile service is nullptr" << std::endl;
198         return;
199     }
200 
201     ServiceCharacteristicProfile profile;
202     profile.SetServiceId("test");
203     profile.SetServiceType("test");
204     int32_t result = DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
205     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
206 }
207 
208 /**
209  * @tc.name: PutDeviceProfile_005
210  * @tc.desc: put device profile without set characteristics
211  * @tc.type: FUNC
212  * @tc.require: I4NY23
213  */
214 HWTEST_F(ProfileCrudTest, PutDeviceProfile_005, TestSize.Level3)
215 {
216     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
217     if (dps == nullptr) {
218         DTEST_LOG << "device profile service is nullptr" << std::endl;
219         return;
220     }
221 
222     ServiceCharacteristicProfile profile;
223     profile.SetServiceId("test");
224     profile.SetServiceType("test");
225     nlohmann::json j;
226     j["testVersion"] = "3.0.0";
227     j["testApiLevel"] = 7;
228     profile.SetCharacteristicProfileJson(j.dump());
229     int32_t result = DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
230     EXPECT_EQ(ERR_DP_PERMISSION_DENIED, result);
231 }
232 
233 /**
234  * @tc.name: DeleteDeviceProfile_001
235  * @tc.desc: delete an empty profile
236  * @tc.type: FUNC
237  * @tc.require: I4NY21
238  */
239 HWTEST_F(ProfileCrudTest, DeleteDeviceProfile_001, TestSize.Level3)
240 {
241     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
242     if (dps == nullptr) {
243         DTEST_LOG << "device profile service is nullptr" << std::endl;
244         return;
245     }
246 
247     int32_t result = DistributedDeviceProfileClient::GetInstance().DeleteDeviceProfile("");
248     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
249 }
250 
251 /**
252  * @tc.name: DeleteDeviceProfile_002
253  * @tc.desc: delete an empty profile
254  * @tc.type: FUNC
255  * @tc.require: I4NY21
256  */
257 HWTEST_F(ProfileCrudTest, DeleteDeviceProfile_002, TestSize.Level3)
258 {
259     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
260     if (dps == nullptr) {
261         DTEST_LOG << "device profile service is nullptr" << std::endl;
262         return;
263     }
264 
265     int32_t result = DistributedDeviceProfileClient::GetInstance().DeleteDeviceProfile("test");
266     EXPECT_EQ(ERR_DP_PERMISSION_DENIED, result);
267 }
268 
269 /**
270  * @tc.name: SubscribeProfileEvent_001
271  * @tc.desc: subscribe device profile
272  * @tc.type: FUNC
273  * @tc.require: I4NY1U
274  */
275 HWTEST_F(ProfileCrudTest, SubscribeProfileEvent_001, TestSize.Level3)
276 {
277     auto eventCb = std::make_shared<ProfileEventCallback>();
278     if (eventCb == nullptr) {
279         DTEST_LOG << "device profile service is nullptr" << std::endl;
280         return;
281     }
282 
283     SubscribeInfo subscribeInfo;
284     int result = DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvent(subscribeInfo, eventCb);
285     EXPECT_NE(ERR_DP_INVALID_PARAMS, result);
286 }
287 
288 /**
289  * @tc.name: SyncDeviceProfile_001
290  * @tc.desc: sync device profile
291  * @tc.type: FUNC
292  * @tc.require: I4NY1U
293  */
294 HWTEST_F(ProfileCrudTest, SyncDeviceProfile_001, TestSize.Level3)
295 {
296     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
297     if (dps == nullptr) {
298         DTEST_LOG << "device profile service is nullptr" << std::endl;
299         return;
300     }
301 
302     wptr<IRemoteObject> remote;
303     DistributedDeviceProfileClient::DeviceProfileDeathRecipient obj;
304     obj.OnRemoteDied(remote);
305     auto syncCb = std::make_shared<ProfileEventCallback>();
306     SyncOptions syncOptions;
307     int result = DistributedDeviceProfileClient::GetInstance().SyncDeviceProfile(syncOptions, syncCb);
308     EXPECT_EQ(ERR_DP_PERMISSION_DENIED, result);
309 }
310 
311 /**
312  * @tc.name: UnsubscribeProfileEvent_001
313  * @tc.desc: unsubscribe device profile
314  * @tc.type: FUNC
315  * @tc.require: I4NY1U
316  */
317 HWTEST_F(ProfileCrudTest, UnsubscribeProfileEvent_001, TestSize.Level3)
318 {
319     auto eventCb = std::make_shared<ProfileEventCallback>();
320     if (eventCb == nullptr) {
321         DTEST_LOG << "device profile service is nullptr" << std::endl;
322         return;
323     }
324 
325     Parcel parcel;
326     ProfileEvent profileEvent = static_cast<ProfileEvent>(parcel.ReadUint32());
327     int result = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvent(profileEvent, eventCb);
328     EXPECT_NE(ERR_DP_INVALID_PARAMS, result);
329 }
330 
331 /**
332  * @tc.name: SubscribeProfileEvents_001
333  * @tc.desc: Subscribe device profile
334  * @tc.type: FUNC
335  * @tc.require: I4NY1U
336  */
337 HWTEST_F(ProfileCrudTest, SubscribeProfileEvents_001, TestSize.Level3)
338 {
339     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
340     if (dps == nullptr) {
341         DTEST_LOG << "device profile service is nullptr" << std::endl;
342         return;
343     }
344 
345     std::list<SubscribeInfo> subscribeInfos;
346     auto eventCb = std::make_shared<ProfileEventCallback>();
347     std::list<ProfileEvent> failedEvents;
348     if (subscribeInfos.empty() || eventCb == nullptr) {
349         return;
350     }
351     int result = DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos,
352         eventCb, failedEvents);
353     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
354 }
355 
356 /**
357  * @tc.name: SubscribeProfileEvents_002
358  * @tc.desc: Subscribe device profile
359  * @tc.type: FUNC
360  * @tc.require: I4NY1U
361  */
362 HWTEST_F(ProfileCrudTest, SubscribeProfileEvents_002, TestSize.Level3)
363 {
364     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
365     if (dps == nullptr) {
366         DTEST_LOG << "device profile service is nullptr" << std::endl;
367         return;
368     }
369 
370     std::list<SubscribeInfo> subscribeInfos;
371     auto eventCb = std::make_shared<ProfileEventCallback>();
372     std::list<ProfileEvent> failedEvents;
373     int result = DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos,
374         eventCb, failedEvents);
375     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
376 }
377 
378 /**
379  * @tc.name: SubscribeProfileEvents_003
380  * @tc.desc: Subscribe device profile
381  * @tc.type: FUNC
382  * @tc.require: I4NY1U
383  */
384 HWTEST_F(ProfileCrudTest, SubscribeProfileEvents_003, TestSize.Level3)
385 {
386     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
387     if (dps == nullptr) {
388         DTEST_LOG << "device profile service is nullptr" << std::endl;
389         return;
390     }
391 
392     std::list<SubscribeInfo> subscribeInfos;
393     std::list<ProfileEvent> failedEvents;
394     int result = DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos,
395         nullptr, failedEvents);
396     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
397 }
398 
399 /**
400  * @tc.name: SubscribeProfileEvents_004
401  * @tc.desc: Subscribe device profile
402  * @tc.type: FUNC
403  * @tc.require: I4NY1U
404  */
405 HWTEST_F(ProfileCrudTest, SubscribeProfileEvents_004, TestSize.Level3)
406 {
407     TestUtil::MockPermission("distributedsched");
408     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
409     if (dps == nullptr) {
410         DTEST_LOG << "device profile service is nullptr" << std::endl;
411         return;
412     }
413 
414     std::list<SubscribeInfo> subscribeInfos;
415     SubscribeInfo subscribeInfo;
416     subscribeInfos.emplace_back(subscribeInfo);
417     std::list<ProfileEvent> failedEvents;
418     auto eventCb = std::make_shared<ProfileEventCallback>();
419     int result = DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos,
420         eventCb, failedEvents);
421     EXPECT_EQ(7, result);
422 }
423 
424 /**
425  * @tc.name: SubscribeProfileEvents_005
426  * @tc.desc: Subscribe device profile
427  * @tc.type: FUNC
428  * @tc.require: I4NY1U
429  */
430 HWTEST_F(ProfileCrudTest, SubscribeProfileEvents_005, TestSize.Level3)
431 {
432     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
433     if (dps == nullptr) {
434         DTEST_LOG << "device profile service is nullptr" << std::endl;
435         return;
436     }
437 
438     std::list<SubscribeInfo> subscribeInfos;
439     SubscribeInfo subscribeInfo;
440     subscribeInfos.emplace_back(subscribeInfo);
441     std::list<ProfileEvent> failedEvents;
442     int result = DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos,
443         nullptr, failedEvents);
444     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
445 }
446 
447 /**
448  * @tc.name: SubscribeProfileEvents_006
449  * @tc.desc: Subscribe device profile
450  * @tc.type: FUNC
451  * @tc.require: I4NY1U
452  */
453 HWTEST_F(ProfileCrudTest, SubscribeProfileEvents_006, TestSize.Level3)
454 {
455     TestUtil::MockPermission("distributedsched");
456     auto callback = std::make_shared<ProfileEventCallback>();
457     std::list<SubscribeInfo> subscribeInfos;
458     std::list<std::string> serviceIds;
459     serviceIds.emplace_back("test");
460     std::string deviceId = "test";
461     ExtraInfo extraInfo;
462     extraInfo["deviceId"] = deviceId;
463     extraInfo["serviceIds"] = serviceIds;
464 
465     SubscribeInfo info1;
466     info1.profileEvent = ProfileEvent::EVENT_PROFILE_CHANGED;
467     info1.extraInfo = std::move(extraInfo);
468     subscribeInfos.emplace_back(info1);
469 
470     SubscribeInfo info2;
471     info2.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
472     subscribeInfos.emplace_back(info2);
473 
474     std::list<ProfileEvent> failedEvents;
475     int result =
476         DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos, callback, failedEvents);
477     EXPECT_EQ(ERR_DP_SUBSCRIBE_FAILED, result);
478 }
479 
480 /**
481  * @tc.name: UnsubscribeProfileEvents_001
482  * @tc.desc: Unsubscribe device profile
483  * @tc.type: FUNC
484  * @tc.require: I4NY1U
485  */
486 HWTEST_F(ProfileCrudTest, UnsubscribeProfileEvents_001, TestSize.Level3)
487 {
488     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
489     if (dps == nullptr) {
490         DTEST_LOG << "device profile service is nullptr" << std::endl;
491         return;
492     }
493 
494     std::list<ProfileEvent> profileEvents;
495     auto eventCb = std::make_shared<ProfileEventCallback>();
496     std::list<ProfileEvent> failedEvents;
497     if (profileEvents.empty() || eventCb == nullptr) {
498         return;
499     }
500     int result = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(profileEvents,
501         eventCb, failedEvents);
502     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
503 }
504 
505 /**
506  * @tc.name: UnsubscribeProfileEvents_002
507  * @tc.desc: Subscribe device profile
508  * @tc.type: FUNC
509  * @tc.require: I4NY1U
510  */
511 HWTEST_F(ProfileCrudTest, UnsubscribeProfileEvents_002, TestSize.Level3)
512 {
513     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
514     if (dps == nullptr) {
515         DTEST_LOG << "device profile service is nullptr" << std::endl;
516         return;
517     }
518 
519     std::list<ProfileEvent> subscribeInfos;
520     auto eventCb = std::make_shared<ProfileEventCallback>();
521     std::list<ProfileEvent> failedEvents;
522     int result = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(subscribeInfos,
523         eventCb, failedEvents);
524     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
525 }
526 
527 /**
528  * @tc.name: UnsubscribeProfileEvents_003
529  * @tc.desc: Subscribe device profile
530  * @tc.type: FUNC
531  * @tc.require: I4NY1U
532  */
533 HWTEST_F(ProfileCrudTest, UnsubscribeProfileEvents_003, TestSize.Level3)
534 {
535     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
536     if (dps == nullptr) {
537         DTEST_LOG << "device profile service is nullptr" << std::endl;
538         return;
539     }
540     std::list<SubscribeInfo> subscribeInfos1;
541     std::list<SubscribeInfo> newSubscribeInfos1;
542     SubscribeInfo subscribeInfo1;
543     SubscribeInfo subscribeInfo2;
544     subscribeInfos1.emplace_back(subscribeInfo1);
545     newSubscribeInfos1.emplace_back(subscribeInfo2);
546     DistributedDeviceProfileClient::GetInstance().MergeSubscribeInfoLocked(subscribeInfos1, newSubscribeInfos1);
547     std::list<ProfileEvent> subscribeInfos;
548     std::list<ProfileEvent> failedEvents;
549     int result = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(subscribeInfos,
550         nullptr, failedEvents);
551     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
552 }
553 
554 /**
555  * @tc.name: SubscribeProfileEvents_004
556  * @tc.desc: Subscribe device profile
557  * @tc.type: FUNC
558  * @tc.require: I4NY1U
559  */
560 HWTEST_F(ProfileCrudTest, UnsubscribeProfileEvents_004, TestSize.Level3)
561 {
562     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
563     if (dps == nullptr) {
564         DTEST_LOG << "device profile service is nullptr" << std::endl;
565         return;
566     }
567     std::list<SubscribeInfo> subscribeInfos1;
568     std::list<SubscribeInfo> newSubscribeInfos1;
569     SubscribeInfo subscribeInfo;
570     SubscribeInfo subscribeInfo11;
571     subscribeInfos1.emplace_back(subscribeInfo);
572     newSubscribeInfos1.emplace_back(subscribeInfo);
573     DistributedDeviceProfileClient::GetInstance().MergeSubscribeInfoLocked(subscribeInfos1, newSubscribeInfos1);
574     newSubscribeInfos1.clear();
575     newSubscribeInfos1.emplace_back(subscribeInfo11);
576     DistributedDeviceProfileClient::GetInstance().MergeSubscribeInfoLocked(subscribeInfos1, newSubscribeInfos1);
577     std::list<ProfileEvent> subscribeInfos;
578     ProfileEvent subscribeInfo1;
579     subscribeInfos.emplace_back(subscribeInfo1);
580     std::list<ProfileEvent> failedEvents;
581     auto eventCb = std::make_shared<ProfileEventCallback>();
582     int result = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(subscribeInfos,
583         eventCb, failedEvents);
584     EXPECT_EQ(ERR_DP_NOT_SUBSCRIBED, result);
585 }
586 
587 /**
588  * @tc.name: UnsubscribeProfileEvents_005
589  * @tc.desc: Subscribe device profile
590  * @tc.type: FUNC
591  * @tc.require: I4NY1U
592  */
593 HWTEST_F(ProfileCrudTest, UnSubscribeProfileEvents_005, TestSize.Level3)
594 {
595     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
596     if (dps == nullptr) {
597         DTEST_LOG << "device profile service is nullptr" << std::endl;
598         return;
599     }
600     std::list<SubscribeInfo> subscribeInfos;
601     std::list<SubscribeInfo> newSubscribeInfos;
602     DistributedDeviceProfileClient::GetInstance().MergeSubscribeInfoLocked(subscribeInfos, newSubscribeInfos);
603     std::list<ProfileEvent> profileEvents;
604     ProfileEvent profileEvent;
605     profileEvents.emplace_back(profileEvent);
606     std::list<ProfileEvent> failedEvents;
607     int result = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(profileEvents,
608         nullptr, failedEvents);
609     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
610 }
611 
612 /**
613  * @tc.name: UnsubscribeProfileEvents_006
614  * @tc.desc: Subscribe device profile
615  * @tc.type: FUNC
616  * @tc.require: I4NY1U
617  */
618 HWTEST_F(ProfileCrudTest, UnSubscribeProfileEvents_006, TestSize.Level3)
619 {
620     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
621     if (dps == nullptr) {
622         DTEST_LOG << "device profile service is nullptr" << std::endl;
623         return;
624     }
625 
626     std::list<ProfileEvent> subscribeInfos;
627     ProfileEvent subscribeInfo;
628     subscribeInfos.emplace_back(subscribeInfo);
629     std::list<ProfileEvent> failedEvents;
630     auto eventCb = std::make_shared<ProfileEventCallback>();
631     DistributedDeviceProfileClient::SubscribeRecord subscribeRecord;
632     DistributedDeviceProfileClient::GetInstance().subscribeRecords_[eventCb] = subscribeRecord;
633     int result = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(subscribeInfos,
634         eventCb, failedEvents);
635     EXPECT_EQ(3, result);
636 }
637 
638 /**
639  * @tc.name: UnsubscribeProfileEvents_007
640  * @tc.desc: Subscribe device profile
641  * @tc.type: FUNC
642  * @tc.require: I4NY1U
643  */
644 HWTEST_F(ProfileCrudTest, UnSubscribeProfileEvents_007, TestSize.Level3)
645 {
646     auto callback = std::make_shared<ProfileEventCallback>();
647     std::list<ProfileEvent> profileEvents;
648     SubscribeInfo info1;
649     profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED);
650     profileEvents.emplace_back(ProfileEvent::EVENT_SYNC_COMPLETED);
651     std::list<ProfileEvent> failedEvents;
652     int result =
653         DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(profileEvents, callback, failedEvents);
654     EXPECT_EQ(ERR_DP_NOT_SUBSCRIBED, result);
655 }
656 
657 /**
658  * @tc.name: GetDeviceProfile_001
659  * @tc.desc: get device profile with syscap
660  * @tc.type: FUNC
661  * @tc.require: I59PZ3
662  */
663 HWTEST_F(ProfileCrudTest, GetDeviceProfile_001, TestSize.Level3)
664 {
665     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
666     if (dps == nullptr) {
667         DTEST_LOG << "device profile service is nullptr" << std::endl;
668         return;
669     }
670 
671     ServiceCharacteristicProfile profile;
672     profile.SetServiceId(SERVICE_ID);
673     profile.SetServiceType(SERVICE_TYPE);
674     int32_t result = DistributedDeviceProfileClient::GetInstance().GetDeviceProfile("", SERVICE_ID, profile);
675     EXPECT_EQ(ERR_DP_PERMISSION_DENIED, result);
676 
677     std::string jsonData = profile.GetCharacteristicProfileJson();
678     DTEST_LOG << "jsonData:" << jsonData << std::endl;
679     nlohmann::json jsonObject = nlohmann::json::parse(jsonData, nullptr, false);
680     if (jsonObject.is_discarded()) {
681         DTEST_LOG << "json parse faild" << std::endl;
682         return;
683     }
684 
685     std::vector<int> values = jsonObject[CHARACTER_OS_SYSCAP].get<std::vector<int>>();
686     int intValues[PCID_MAIN_INTS];
687     int i = 0;
688     for (int value : values) {
689         intValues[i++] = value;
690     }
691 
692     char (*osOutput)[SINGLE_SYSCAP_LEN] = nullptr;
693     int32_t length;
694     if (!DecodeOsSyscap((char *)intValues, &osOutput, &length)) {
695         DTEST_LOG << "DecodeOsSyscap failed" << std::endl;
696         return;
697     }
698     for (int i = 0; i < length; i++) {
699         DTEST_LOG << "OsSyscap: " << *(osOutput + i) << std::endl;
700     }
701 
702     std::string capabilities = jsonObject[CHARACTER_PRIVATE_SYSCAP];
703     char (*priOutput)[SINGLE_SYSCAP_LEN] = nullptr;
704     if (!DecodePrivateSyscap((char *)capabilities.c_str(), &priOutput, &length)) {
705         DTEST_LOG << "DecodePrivateSyscap failed" << std::endl;
706         return;
707     }
708     for (int i = 0; i < length; i++) {
709         DTEST_LOG << "PrivateSyscap: " << *(priOutput + i) << std::endl;
710     }
711 }
712 
713 /**
714  * @tc.name: GetDeviceProfile_002
715  * @tc.desc: get device profile
716  * @tc.type: FUNC
717  * @tc.require: I4NY22
718  */
719 HWTEST_F(ProfileCrudTest, GetDeviceProfile_002, TestSize.Level3)
720 {
721     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
722     if (dps == nullptr) {
723         DTEST_LOG << "device profile service is nullptr" << std::endl;
724         return;
725     }
726 
727     ServiceCharacteristicProfile profile;
728     profile.SetServiceId("test");
729     profile.SetServiceType("test");
730     nlohmann::json j;
731     profile.SetCharacteristicProfileJson(j.dump());
732     int32_t result = DistributedDeviceProfileClient::GetInstance().GetDeviceProfile("", SERVICE_ID, profile);
733     EXPECT_NE(ERR_DP_INVALID_PARAMS, result);
734 }
735 
736 /**
737  * @tc.name: DfxErrorPrint_001
738  * @tc.desc: print hisysevent error event
739  * @tc.type: FUNC
740  * @tc.require: I5EE0Y
741  */
742 HWTEST_F(ProfileCrudTest, DfxErrorPrint_001, TestSize.Level3)
743 {
744     int ret = HiSysEvent::Write(DOMAIN_NAME, DEVICE_PROFILE_SYNC_FAILED,
745         HiSysEvent::EventType::FAULT, FAULT_CODE_KEY, -1);
746     EXPECT_EQ(0, ret);
747 }
748 
749 /**
750  * @tc.name: SubscribeProfileEvents_007
751  * @tc.desc: Subscribe device profile
752  * @tc.type: FUNC
753  * @tc.require: I4NY1U
754  */
755 HWTEST_F(ProfileCrudTest, SubscribeProfileEvents_007, TestSize.Level3)
756 {
757     TestUtil::MockPermission("distributedsched");
758     auto callback = std::make_shared<ProfileEventCallback>();
759     std::list<SubscribeInfo> subscribeInfos;
760     std::list<std::string> serviceIds;
761     serviceIds.emplace_back("appInfo");
762     std::string deviceId = "";
763     ExtraInfo extraInfo;
764     extraInfo["deviceId"] = deviceId;
765     extraInfo["serviceIds"] = serviceIds;
766 
767     SubscribeInfo info1;
768     info1.profileEvent = ProfileEvent::EVENT_PROFILE_CHANGED;
769     info1.extraInfo = std::move(extraInfo);
770     subscribeInfos.emplace_back(info1);
771 
772     SubscribeInfo info2;
773     info2.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
774     subscribeInfos.emplace_back(info2);
775 
776     std::list<ProfileEvent> failedEvents;
777     int result =
778         DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos, callback, failedEvents);
779     EXPECT_EQ(0, result);
780 }
781 
782 /**
783  * @tc.name: UnsubscribeProfileEvents_008
784  * @tc.desc: Subscribe device profile
785  * @tc.type: FUNC
786  * @tc.require: I4NY1U
787  */
788 HWTEST_F(ProfileCrudTest, UnSubscribeProfileEvents_008, TestSize.Level3)
789 {
790     TestUtil::MockPermission("distributedsched");
791     auto callback = std::make_shared<ProfileEventCallback>();
792     std::list<ProfileEvent> profileEvents;
793     SubscribeInfo info1;
794     profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED);
795     profileEvents.emplace_back(ProfileEvent::EVENT_SYNC_COMPLETED);
796     std::list<ProfileEvent> failedEvents;
797     int result =
798         DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(profileEvents, callback, failedEvents);
799     EXPECT_EQ(ERR_DP_NOT_SUBSCRIBED, result);
800 }
801 
802 /**
803  * @tc.name: UnsubscribeProfileEvents_009
804  * @tc.desc: Subscribe device profile
805  * @tc.type: FUNC
806  * @tc.require: I4NY1U
807  */
808 HWTEST_F(ProfileCrudTest, UnSubscribeProfileEvents_009, TestSize.Level3)
809 {
810     TestUtil::MockPermission("distributedsched");
811     auto callback = std::make_shared<ProfileEventCallback>();
812     std::list<SubscribeInfo> subscribeInfos;
813     std::list<std::string> serviceIds;
814     serviceIds.emplace_back("appInfo");
815     std::string deviceId = "";
816     ExtraInfo extraInfo;
817     extraInfo["deviceId"] = deviceId;
818     extraInfo["serviceIds"] = serviceIds;
819 
820     SubscribeInfo info1;
821     info1.profileEvent = ProfileEvent::EVENT_PROFILE_CHANGED;
822     info1.extraInfo = std::move(extraInfo);
823     subscribeInfos.emplace_back(info1);
824 
825     SubscribeInfo info2;
826     info2.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
827     subscribeInfos.emplace_back(info2);
828 
829     std::list<ProfileEvent> failedEvents;
830     int result =
831         DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos, callback, failedEvents);
832     std::list<ProfileEvent> profileEvents;
833     profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED);
834     profileEvents.emplace_back(ProfileEvent::EVENT_SYNC_COMPLETED);
835     result =
836         DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(profileEvents, callback, failedEvents);
837     EXPECT_EQ(0, result);
838 }
839 
840 /**
841  * @tc.name: SyncDeviceProfile_002
842  * @tc.desc: sync device profile
843  * @tc.type: FUNC
844  * @tc.require: I4NY1U
845  */
846 HWTEST_F(ProfileCrudTest, SyncDeviceProfile_002, TestSize.Level3)
847 {
848     TestUtil::MockPermission("multimodalinput");
849     int64_t mode = 2;
850     SyncOptions syncOption;
851     syncOption.SetSyncMode((OHOS::DeviceProfile::SyncMode)mode);
852     syncOption.AddDevice("test");
853     int result = DistributedDeviceProfileClient::GetInstance().SyncDeviceProfile(syncOption,
854         std::make_shared<ProfileEventCallback>());
855     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
856 }
857 
858 /**
859  * @tc.name: PutDeviceProfile_006
860  * @tc.desc: delete an empty profile
861  * @tc.type: FUNC
862  * @tc.require: I4NY21
863  */
864 HWTEST_F(ProfileCrudTest, PutDeviceProfile_006, TestSize.Level3)
865 {
866     TestUtil::MockPermission("multimodalinput");
867     ServiceCharacteristicProfile profile;
868     profile.SetServiceId("InputDeviceCooperation");
869     profile.SetServiceType("InputDeviceCooperation");
870     nlohmann::json j;
871     j["testVersion"] = "3.0.0";
872     j["testApiLevel"] = 7;
873     profile.SetCharacteristicProfileJson(j.dump());
874     int32_t result = DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
875     EXPECT_EQ(0, result);
876 }
877 
878 /**
879  * @tc.name: GetDeviceProfile_003
880  * @tc.desc: get device profile
881  * @tc.type: FUNC
882  * @tc.require: I4NY22
883  */
884 HWTEST_F(ProfileCrudTest, GetDeviceProfile_003, TestSize.Level3)
885 {
886     TestUtil::MockPermission("multimodalinput");
887     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
888     if (dps == nullptr) {
889         DTEST_LOG << "device profile service is nullptr" << std::endl;
890         return;
891     }
892 
893     ServiceCharacteristicProfile profile;
894     profile.SetServiceId("InputDeviceCooperation");
895     profile.SetServiceType("InputDeviceCooperation");
896     nlohmann::json j;
897     profile.SetCharacteristicProfileJson(j.dump());
898     int32_t result =
899         DistributedDeviceProfileClient::GetInstance().GetDeviceProfile("", "InputDeviceCooperation", profile);
900     EXPECT_EQ(0, result);
901 }
902 
903 /**
904  * @tc.name: DeleteDeviceProfile_003
905  * @tc.desc: delete an empty profile
906  * @tc.type: FUNC
907  * @tc.require: I4NY21
908  */
909 HWTEST_F(ProfileCrudTest, DeleteDeviceProfile_003, TestSize.Level3)
910 {
911     TestUtil::MockPermission("multimodalinput");
912     auto dps = DistributedDeviceProfileClient::GetInstance().GetDeviceProfileService();
913     if (dps == nullptr) {
914         DTEST_LOG << "device profile service is nullptr" << std::endl;
915         return;
916     }
917 
918     int32_t result = DistributedDeviceProfileClient::GetInstance().DeleteDeviceProfile("InputDeviceCooperation");
919     EXPECT_EQ(0, result);
920 }
921 
922 /**
923  * @tc.name: PutDeviceProfile_007
924  * @tc.desc: put device profile with empty service type
925  * @tc.type: FUNC
926  * @tc.require: I4NY23
927  */
928 HWTEST_F(ProfileCrudTest, PutDeviceProfile_007, TestSize.Level3)
929 {
930     ServiceCharacteristicProfile profile;
931     profile.SetServiceId("test");
932     profile.SetServiceType("test");
933     nlohmann::json j;
934     j["testVersion"] = "3.0.0";
935     j["testApiLevel"] = 7;
936     profile.SetCharacteristicProfileJson(j.dump());
937     profile.SetServiceProfileJson(j.dump());
938     int32_t result = DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
939     EXPECT_NE(ERR_DP_INVALID_PARAMS, result);
940 }
941 
942 /**
943  * @tc.name: PutDeviceProfile_008
944  * @tc.desc: put device profile with empty service type
945  * @tc.type: FUNC
946  * @tc.require: I4NY23
947  */
948 HWTEST_F(ProfileCrudTest, PutDeviceProfile_008, TestSize.Level3)
949 {
950     ServiceCharacteristicProfile profile;
951     profile.SetServiceId("test");
952     profile.SetServiceType("test");
953     nlohmann::json j;
954     j["testVersion"] = "3.0.0";
955     j["testApiLevel"] = 7;
956     profile.SetCharacteristicProfileJson(j.dump());
957     profile.SetServiceProfileJson(j.dump());
958     int32_t result = DistributedDeviceProfileClient::GetInstance().PutDeviceProfile(profile);
959     EXPECT_NE(ERR_DP_INVALID_PARAMS, result);
960 }
961 
962 /**
963  * @tc.name: SubscribeProfileEvents_008
964  * @tc.desc: Subscribe device profile
965  * @tc.type: FUNC
966  * @tc.require: I4NY1U
967  */
968 HWTEST_F(ProfileCrudTest, SubscribeProfileEvents_008, TestSize.Level3)
969 {
970     TestUtil::MockPermission("distributedsched");
971     auto callback = std::make_shared<ProfileEventCallback>();
972     std::list<SubscribeInfo> subscribeInfos;
973     std::list<std::string> serviceIds;
974     serviceIds.emplace_back("appInfo");
975     std::string deviceId = "";
976     ExtraInfo extraInfo;
977     extraInfo["deviceId"] = deviceId;
978     extraInfo["serviceIds"] = serviceIds;
979 
980     SubscribeInfo info1;
981     info1.profileEvent = ProfileEvent::EVENT_PROFILE_CHANGED;
982     info1.extraInfo = std::move(extraInfo);
983     subscribeInfos.emplace_back(info1);
984 
985     SubscribeInfo info2;
986     info2.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
987     subscribeInfos.emplace_back(info2);
988 
989     std::list<ProfileEvent> failedEvents;
990     DistributedDeviceProfileClient::SubscribeRecord subscribeRecord;
991     subscribeRecord.subscribeInfos = subscribeInfos;
992     subscribeRecord.notifier = sptr<ProfileEventNotifierStub>(
993         new ProfileEventNotifierStub(callback));
994     subscribeRecord.profileEvents.set(static_cast<uint32_t>(ProfileEvent::EVENT_PROFILE_CHANGED));
995     DistributedDeviceProfileClient::GetInstance().subscribeRecords_[callback] = subscribeRecord;
996     int result =
997         DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos, callback, failedEvents);
998     EXPECT_EQ(0, result);
999 }
1000 }
1001 }