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 "UTTest_dm_publish_manager.h"
17 
18 #include "dm_log.h"
19 #include "dm_constants.h"
20 #include "dm_anonymous.h"
21 #include "ipc_server_listener.h"
22 #include "device_manager_service_listener.h"
23 #include "softbus_bus_center.h"
24 #include "device_manager_service_listener.h"
25 #include "softbus_error_code.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
SetUp()29 void DmPublishManagerTest::SetUp()
30 {
31 }
32 
TearDown()33 void DmPublishManagerTest::TearDown()
34 {
35 }
36 
SetUpTestCase()37 void DmPublishManagerTest::SetUpTestCase()
38 {
39 }
40 
TearDownTestCase()41 void DmPublishManagerTest::TearDownTestCase()
42 {
43 }
44 
45 namespace {
46 std::shared_ptr<SoftbusConnector> softbusConnector_ = std::make_shared<SoftbusConnector>();
47 std::shared_ptr<DeviceManagerServiceListener> listener_ = std::make_shared<DeviceManagerServiceListener>();
48 std::shared_ptr<DmPublishManager> publishMgr_ = std::make_shared<DmPublishManager>(softbusConnector_, listener_);
49 
50 /**
51  * @tc.name: DmPublishManager_001
52  * @tc.desc: Test whether the DmPublishManager function can generate a new pointer
53  * @tc.type: FUNC
54  * @tc.require: I5N1K3
55  */
HWTEST_F(DmPublishManagerTest, DmPublishManager_001, testing::ext::TestSize.Level0)56 HWTEST_F(DmPublishManagerTest, DmPublishManager_001, testing::ext::TestSize.Level0)
57 {
58     std::shared_ptr<DmPublishManager> Test = std::make_shared<DmPublishManager>(softbusConnector_, listener_);
59     ASSERT_NE(Test, nullptr);
60 }
61 
62 /**
63  * @tc.name: DmPublishManager_002
64  * @tc.desc: Test whether the DmPublishManager function can delete a new pointer
65  * @tc.type: FUNC
66  * @tc.require: I5N1K3
67  */
HWTEST_F(DmPublishManagerTest, DmPublishManager_002, testing::ext::TestSize.Level0)68 HWTEST_F(DmPublishManagerTest, DmPublishManager_002, testing::ext::TestSize.Level0)
69 {
70     std::shared_ptr<DmPublishManager> Test = std::make_shared<DmPublishManager>(softbusConnector_, listener_);
71     Test.reset();
72     EXPECT_EQ(Test, nullptr);
73 }
74 
75 /**
76  * @tc.name: PublishDeviceDiscovery_001
77  * @tc.desc: keeping pkgame unchanged, call PublishDeviceDiscovery twice
78  *           so that its PublishQueue is not empty and return ERR_DM_PUBLISH_REPEATED
79  * @tc.type: FUNC
80  * @tc.require: I5N1K3
81  */
HWTEST_F(DmPublishManagerTest, PublishDeviceDiscovery_001, testing::ext::TestSize.Level0)82 HWTEST_F(DmPublishManagerTest, PublishDeviceDiscovery_001, testing::ext::TestSize.Level0)
83 {
84     std::string pkgName = "com.ohos.helloworld";
85     DmPublishInfo publishInfo;
86     publishInfo.publishId = 9;
87     publishInfo.mode = DM_DISCOVER_MODE_PASSIVE;
88     publishInfo.freq = DM_HIGH;
89     publishInfo.ranging = 1;
90     publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
91     int32_t ret = publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
92     EXPECT_EQ(ret, ERR_DM_PUBLISH_REPEATED);
93     publishMgr_->UnPublishDeviceDiscovery(pkgName, publishInfo.publishId);
94 }
95 
96 /**
97  * @tc.name: PublishDeviceDiscovery_002
98  * @tc.desc: pkgame changed, call PublishDeviceDiscovery twice
99  *           so that its discoveryQueue is not empty and return ERR_DM_PUBLISH_REPEATED
100  * @tc.type: FUNC
101  * @tc.require: I5N1K3
102  */
HWTEST_F(DmPublishManagerTest, PublishDeviceDiscovery_002, testing::ext::TestSize.Level0)103 HWTEST_F(DmPublishManagerTest, PublishDeviceDiscovery_002, testing::ext::TestSize.Level0)
104 {
105     std::string pkgName = "com.ohos.helloworld";
106     DmPublishInfo publishInfo;
107     publishInfo.publishId = 9;
108     publishInfo.mode = DM_DISCOVER_MODE_PASSIVE;
109     publishInfo.freq = DM_FREQ_BUTT;
110     publishInfo.ranging = 1;
111     publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
112     pkgName = "com.ohos.helloworld.new";
113     int32_t ret = publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
114     ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM || ret == SOFTBUS_NETWORK_NOT_INIT || ret == SOFTBUS_NETWORK_LOOPER_ERR);
115     publishMgr_->UnPublishDeviceDiscovery(pkgName, publishInfo.publishId);
116 }
117 
118 /**
119  * @tc.name: OnPublishResult_001
120  * @tc.desc: The OnPublishFailed function takes the wrong case and emptying pkgName
121  * @tc.type: FUNC
122  * @tc.require: I5N1K3
123  */
HWTEST_F(DmPublishManagerTest, OnPublishResult_001, testing::ext::TestSize.Level0)124 HWTEST_F(DmPublishManagerTest, OnPublishResult_001, testing::ext::TestSize.Level0)
125 {
126     std::string pkgName = "com.ohos.helloworld";
127     int32_t publishId = 1;
128     int32_t failedReason = 3;
129     publishMgr_->OnPublishResult(pkgName, publishId, failedReason);
130     std::shared_ptr<IpcNotifyPublishResultReq> pReq =
131         std::static_pointer_cast<IpcNotifyPublishResultReq>(listener_->ipcServerListener_.req_);
132     std::string ret = pReq->GetPkgName();
133     EXPECT_EQ(ret, pkgName);
134 }
135 
136 /**
137  * @tc.name: OnPublishResult_002
138  * @tc.desc: The OnPublishSuccess function takes the wrong case and return pkgName
139  * @tc.type: FUNC
140  * @tc.require: I5N1K3
141  */
HWTEST_F(DmPublishManagerTest, OnPublishResult_002, testing::ext::TestSize.Level0)142 HWTEST_F(DmPublishManagerTest, OnPublishResult_002, testing::ext::TestSize.Level0)
143 {
144     std::string pkgName;
145     int32_t publishId = 1;
146     publishMgr_->OnPublishResult(pkgName, publishId, 0);
147     std::shared_ptr<IpcNotifyPublishResultReq> pReq =
148         std::static_pointer_cast<IpcNotifyPublishResultReq>(listener_->ipcServerListener_.req_);
149     std::string ret = pReq->GetPkgName();
150     EXPECT_NE(ret, pkgName);
151 }
152 } // namespace
153 } // namespace DistributedHardware
154 } // namespace OHOS
155