• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "UTTest_dm_discovery_manager.h"
17 
18 #include <iostream>
19 #include <string>
20 #include <unistd.h>
21 
22 #include "dm_log.h"
23 #include "dm_constants.h"
24 #include "dm_anonymous.h"
25 #include "ipc_server_listener.h"
26 #include "device_manager_service_listener.h"
27 #include "softbus_bus_center.h"
28 #include "device_manager_service_listener.h"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
SetUp()32 void DmDiscoveryManagerTest::SetUp()
33 {
34 }
35 
TearDown()36 void DmDiscoveryManagerTest::TearDown()
37 {
38 }
39 
SetUpTestCase()40 void DmDiscoveryManagerTest::SetUpTestCase()
41 {
42 }
43 
TearDownTestCase()44 void DmDiscoveryManagerTest::TearDownTestCase()
45 {
46 }
47 
48 namespace {
49 std::shared_ptr<SoftbusConnector> softbusConnector_ = std::make_shared<SoftbusConnector>();
50 std::shared_ptr<DeviceManagerServiceListener> listener_ = std::make_shared<DeviceManagerServiceListener>();
51 std::shared_ptr<HiChainConnector> hiChainConnector_ = std::make_shared<HiChainConnector>();
52 std::shared_ptr<DmDiscoveryManager> discoveryMgr_ =
53     std::make_shared<DmDiscoveryManager>(softbusConnector_, listener_, hiChainConnector_);
54 
55 /**
56  * @tc.name: DmDiscoveryManager_001
57  * @tc.desc: Test whether the DmDiscoveryManager function can generate a new pointer
58  * @tc.type: FUNC
59  * @tc.require: AR000GHSJK
60  */
61 HWTEST_F(DmDiscoveryManagerTest, DmDiscoveryManager_001, testing::ext::TestSize.Level0)
62 {
63     std::shared_ptr<DmDiscoveryManager> Test =
64         std::make_shared<DmDiscoveryManager>(softbusConnector_, listener_, hiChainConnector_);
65     ASSERT_NE(Test, nullptr);
66 }
67 
68 /**
69  * @tc.name: DmDiscoveryManager_002
70  * @tc.desc: Test whether the DmDiscoveryManager function can delete a new pointer
71  * @tc.type: FUNC
72  * @tc.require: AR000GHSJK
73  */
74 HWTEST_F(DmDiscoveryManagerTest, DmDiscoveryManager_002, testing::ext::TestSize.Level0)
75 {
76     std::shared_ptr<DmDiscoveryManager> Test =
77         std::make_shared<DmDiscoveryManager>(softbusConnector_, listener_, hiChainConnector_);
78     Test.reset();
79     EXPECT_EQ(Test, nullptr);
80 }
81 
82 /**
83  * @tc.name:StartDeviceDiscovery_001
84  * @tc.desc: keeping pkgame unchanged, call StartDeviceDiscovery twice
85  *           so that its discoveryQueue is not empty and return ERR_DM_DISCOVERY_FAILED
86  * @tc.type: FUNC
87  * @tc.require: AR000GHSJK
88  */
89 HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_001, testing::ext::TestSize.Level0)
90 {
91     std::string pkgName = "com.ohos.helloworld";
92     DmSubscribeInfo subscribeInfo;
93     const std::string extra;
94     int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
95     EXPECT_EQ(ret, ERR_DM_DISCOVERY_FAILED);
96     discoveryMgr_->StopDeviceDiscovery(pkgName, subscribeInfo.subscribeId);
97 }
98 
99 /**
100  * @tc.name:StartDeviceDiscovery_002
101  * @tc.desc: pkgame changed, call StartDeviceDiscovery twice
102  *           so that its discoveryQueue is not empty and return ERR_DM_DISCOVERY_REPEATED
103  * @tc.type: FUNC
104  * @tc.require: AR000GHSJK
105  */
106 HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_002, testing::ext::TestSize.Level0)
107 {
108     std::string pkgName = "com.ohos.helloworld";
109     std::string extra;
110     DmSubscribeInfo subscribeInfo;
111     discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
112     pkgName = "com.ohos.helloworld.new";
113     int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
114     ASSERT_EQ(ret, ERR_DM_DISCOVERY_FAILED);
115     discoveryMgr_->StopDeviceDiscovery(pkgName, subscribeInfo.subscribeId);
116 }
117 
118 /**
119  * @tc.name: OnDeviceFound_001
120  * @tc.desc: The OnDeviceFound function does the correct case and assigns pkgName
121  * @tc.type: FUNC
122  * @tc.require: AR000GHSJK
123  */
124 HWTEST_F(DmDiscoveryManagerTest, OnDeviceFound_001, testing::ext::TestSize.Level0)
125 {
126     std::string pkgName = "com.ohos.helloworld";
127     std::string filterOptions = R"(
128     {
129         "filter_op": "OR",
130         "filters":
131         [
132             {
133                 "type" : "credible",
134                 "value" : 0
135             }
136         ]
137     }
138     )";
139     DmDeviceFilterOption dmFilter;
140     dmFilter.TransformToFilter(filterOptions);
141     uint16_t aaa = 11;
142     DmDiscoveryContext context { pkgName, filterOptions, aaa, dmFilter.filterOp_, dmFilter.filters_ };
143     discoveryMgr_->discoveryContextMap_[pkgName] = context;
144     sleep(1);
145     DmDeviceInfo info;
146     info.deviceId[0] = '\0';
147     info.deviceName[0] = '\0';
148     bool isOnline = false;
149     discoveryMgr_->OnDeviceFound(pkgName, info, isOnline);
150     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq =
151         std::static_pointer_cast<IpcNotifyDeviceFoundReq>(listener_->ipcServerListener_.req_);
152     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
153     EXPECT_EQ(ret1, 1);
154     std::string ret = pReq->GetPkgName();
155     EXPECT_EQ(ret, pkgName);
156 }
157 
158 /**
159  * @tc.name: OnDeviceFound_002
160  * @tc.desc: set pkgName not null and discoveryContextMap_ null and return
161  * @tc.type: FUNC
162  * @tc.require: AR000GHSJK
163  */
164 HWTEST_F(DmDiscoveryManagerTest, OnDeviceFound_002, testing::ext::TestSize.Level0)
165 {
166     std::string pkgName = "com.ohos.helloworld";
167     std::string filterOptions = R"(
168     {
169         "filter_op": "AND",
170         "filters":
171         [
172             {
173                 "type" : "credible",
174                 "value" : 2
175             }
176         ]
177     }
178     )";
179     DmDeviceFilterOption dmFilter;
180     dmFilter.TransformToFilter(filterOptions);
181     uint16_t aaa = 11;
182     DmDiscoveryContext context { pkgName, filterOptions, aaa, dmFilter.filterOp_, dmFilter.filters_ };
183     discoveryMgr_->discoveryContextMap_[pkgName] = context;
184     DmDeviceInfo info;
185     bool isOnline = false;
186     discoveryMgr_->OnDeviceFound(pkgName, info, isOnline);
187     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
188     EXPECT_EQ(ret1, 1);
189     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq =
190         std::static_pointer_cast<IpcNotifyDeviceFoundReq>(listener_->ipcServerListener_.req_);
191     std::string ret = pReq->GetPkgName();
192     EXPECT_EQ(ret, pkgName);
193 }
194 
195 /**
196  * @tc.name: OnDiscoveryFailed_001
197  * @tc.desc: The OnDeviceFound function takes the wrong case and emptying pkgName
198  * @tc.type: FUNC
199  * @tc.require: AR000GHSJK
200  */
201 HWTEST_F(DmDiscoveryManagerTest, OnDiscoveryFailed_001, testing::ext::TestSize.Level0)
202 {
203     std::string pkgName = "com.ohos.helloworld";
204     int32_t subscribeId = 1;
205     int32_t failedReason = 3;
206     discoveryMgr_->OnDiscoveryFailed(pkgName, subscribeId, failedReason);
207     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
208         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
209     std::string ret = pReq->GetPkgName();
210     EXPECT_EQ(ret, pkgName);
211 }
212 
213 /**
214  * @tc.name: OnDiscoveryFailed_002
215  * @tc.desc: The OnDeviceFound function takes the wrong case and emptying pkgName
216  * @tc.type: FUNC
217  * @tc.require: AR000GHSJK
218  */
219 HWTEST_F(DmDiscoveryManagerTest, OnDiscoveryFailed_002, testing::ext::TestSize.Level0)
220 {
221     std::string pkgName;
222     int32_t subscribeId = 1;
223     int32_t failedReason = 3;
224     discoveryMgr_->OnDiscoveryFailed(pkgName, subscribeId, failedReason);
225     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
226     EXPECT_EQ(ret1, 0);
227 }
228 
229 /**
230  * @tc.name: OnDiscoverySuccess_001
231  * @tc.desc: The OnDeviceFound function takes the wrong case and return pkgName
232  * @tc.type: FUNC
233  * @tc.require: AR000GHSJK
234  */
235 HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_001, testing::ext::TestSize.Level0)
236 {
237     std::string pkgName = "com.ohos.helloworld";
238     int32_t subscribeId = 1;
239     discoveryMgr_->OnDiscoverySuccess(pkgName, subscribeId);
240     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
241         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
242     std::string ret = pReq->GetPkgName();
243     EXPECT_EQ(ret, pkgName);
244 }
245 
246 /**
247  * @tc.name: OnDiscoverySuccess_002
248  * @tc.desc: set pkgName null and return discoveryContextMap_ null and return pkgName(null)
249  * @tc.type: FUNC
250  * @tc.require: AR000GHSJK
251  */
252 HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_002, testing::ext::TestSize.Level0)
253 {
254     std::string pkgName;
255     int32_t subscribeId = 1;
256     discoveryMgr_->OnDiscoverySuccess(pkgName, subscribeId);
257     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
258     EXPECT_EQ(ret1, 1);
259     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
260         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
261     std::string ret = pReq->GetPkgName();
262     EXPECT_EQ(ret, pkgName);
263 }
264 
265 /**
266  * @tc.name: StartDeviceDiscovery_003
267  * @tc.desc: return ERR_DM_DISCOVERY_FAILED
268  * @tc.type: FUNC
269  * @tc.require: AR000GHSJK
270  */
271 HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_003, testing::ext::TestSize.Level0)
272 {
273     std::string pkgName = "com.ohos.helloworld";
274     uint16_t subscribeId = 1;
275     std::string filterOptions = "filterOptions";
276     int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeId, filterOptions);
277     EXPECT_EQ(ret, ERR_DM_DISCOVERY_FAILED);
278 }
279 
280 /**
281  * @tc.name: StartDeviceDiscovery_004
282  * @tc.desc: return ERR_DM_DISCOVERY_REPEATED
283  * @tc.type: FUNC
284  * @tc.require: AR000GHSJK
285  */
286 HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_004, testing::ext::TestSize.Level0)
287 {
288     std::string pkgName = "com.ohos.helloworld";
289     uint16_t subscribeId = 1;
290     std::string filterOptions;
291     discoveryMgr_->discoveryQueue_.push(pkgName);
292     int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeId, filterOptions);
293     EXPECT_EQ(ret, ERR_DM_DISCOVERY_REPEATED);
294 }
295 
296 /**
297  * @tc.name: StartDeviceDiscovery_005
298  * @tc.desc: return ERR_DM_DISCOVERY_REPEATED
299  * @tc.type: FUNC
300  * @tc.require: AR000GHSJK
301  */
302 HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_005, testing::ext::TestSize.Level0)
303 {
304     std::string pkgName = "com.ohos.helloworld";
305     uint16_t subscribeId = 1;
306     std::string filterOptions;
307     while (discoveryMgr_->discoveryQueue_.empty()) {
308         discoveryMgr_->discoveryQueue_.pop();
309     }
310     int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeId, filterOptions);
311     EXPECT_EQ(ret, ERR_DM_DISCOVERY_REPEATED);
312 }
313 
314 /**
315  * @tc.name: OnDeviceFound_003
316  * @tc.desc: set pkgName not null
317  * @tc.type: FUNC
318  * @tc.require: AR000GHSJK
319  */
320 HWTEST_F(DmDiscoveryManagerTest, OnDeviceFound_003, testing::ext::TestSize.Level0)
321 {
322     std::string pkgName = "com.ohos.helloworld";
323     DmDeviceBasicInfo info;
324     int32_t range = 0;
325     bool isOnline = true;
326     discoveryMgr_->OnDeviceFound(pkgName, info, range, isOnline);
327     EXPECT_EQ(discoveryMgr_->discoveryContextMap_.empty(), false);
328 }
329 } // namespace
330 } // namespace DistributedHardware
331 } // namespace OHOS
332