• 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 "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<DmDiscoveryManager> discoveryMgr_ = std::make_shared<DmDiscoveryManager>(softbusConnector_, listener_);
52 
53 /**
54  * @tc.name: DmDiscoveryManager_001
55  * @tc.desc: Test whether the DmDiscoveryManager function can generate a new pointer
56  * @tc.type: FUNC
57  * @tc.require: AR000GHSJK
58  */
59 HWTEST_F(DmDiscoveryManagerTest, DmDiscoveryManager_001, testing::ext::TestSize.Level0)
60 {
61     std::shared_ptr<DmDiscoveryManager> Test = std::make_shared<DmDiscoveryManager>(softbusConnector_, listener_);
62     ASSERT_NE(Test, nullptr);
63 }
64 
65 /**
66  * @tc.name: DmDiscoveryManager_002
67  * @tc.desc: Test whether the DmDiscoveryManager function can delete a new pointer
68  * @tc.type: FUNC
69  * @tc.require: AR000GHSJK
70  */
71 HWTEST_F(DmDiscoveryManagerTest, DmDiscoveryManager_002, testing::ext::TestSize.Level0)
72 {
73     std::shared_ptr<DmDiscoveryManager> Test = std::make_shared<DmDiscoveryManager>(softbusConnector_, listener_);
74     Test.reset();
75     EXPECT_EQ(Test, nullptr);
76 }
77 
78 /**
79  * @tc.name:StartDeviceDiscovery_001
80  * @tc.desc: keeping pkgame unchanged, call StartDeviceDiscovery twice
81  *           so that its discoveryQueue is not empty and return ERR_DM_DISCOVERY_REPEATED
82  * @tc.type: FUNC
83  * @tc.require: AR000GHSJK
84  */
85 HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_001, testing::ext::TestSize.Level0)
86 {
87     std::string pkgName = "com.ohos.helloworld";
88     DmSubscribeInfo subscribeInfo;
89     const std::string extra;
90     discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
91     int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
92     EXPECT_EQ(ret, ERR_DM_DISCOVERY_REPEATED);
93     discoveryMgr_->StopDeviceDiscovery(pkgName, subscribeInfo.subscribeId);
94 }
95 
96 /**
97  * @tc.name:StartDeviceDiscovery_002
98  * @tc.desc: pkgame changed, call StartDeviceDiscovery twice
99  *           so that its discoveryQueue is not empty and return ERR_DM_DISCOVERY_REPEATED
100  * @tc.type: FUNC
101  * @tc.require: AR000GHSJK
102  */
103 HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_002, testing::ext::TestSize.Level0)
104 {
105     std::string pkgName = "com.ohos.helloworld";
106     std::string extra;
107     DmSubscribeInfo subscribeInfo;
108     discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
109     pkgName = "com.ohos.helloworld.new";
110     int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
111     ASSERT_EQ(ret, ERR_DM_DISCOVERY_FAILED);
112     discoveryMgr_->StopDeviceDiscovery(pkgName, subscribeInfo.subscribeId);
113 }
114 
115 /**
116  * @tc.name: OnDeviceFound_001
117  * @tc.desc: The OnDeviceFound function does the correct case and assigns pkgName
118  * @tc.type: FUNC
119  * @tc.require: AR000GHSJK
120  */
121 HWTEST_F(DmDiscoveryManagerTest, OnDeviceFound_001, testing::ext::TestSize.Level0)
122 {
123     std::string pkgName = "com.ohos.helloworld";
124     std::string filterOptions = R"(
125     {
126         "filter_op": "OR",
127         "filters":
128         [
129             {
130                 "type" : "credible",
131                 "value" : 0
132             }
133         ]
134     }
135     )";
136     DmDeviceFilterOption dmFilter;
137     dmFilter.TransformToFilter(filterOptions);
138     uint16_t aaa = 11;
139     DmDiscoveryContext context { pkgName, filterOptions, aaa, dmFilter.filterOp_, dmFilter.filters_ };
140     discoveryMgr_->discoveryContextMap_[pkgName] = context;
141     sleep(1);
142     DmDeviceInfo info;
143     info.deviceId[0] = '\0';
144     info.deviceName[0] = '\0';
145     discoveryMgr_->OnDeviceFound(pkgName, info);
146     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq =
147         std::static_pointer_cast<IpcNotifyDeviceFoundReq>(listener_->ipcServerListener_.req_);
148     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
149     EXPECT_EQ(ret1, 1);
150     std::string ret = pReq->GetPkgName();
151     EXPECT_EQ(ret, pkgName);
152 }
153 
154 /**
155  * @tc.name: OnDeviceFound_002
156  * @tc.desc: set pkgName not null and discoveryContextMap_ null and return
157  * @tc.type: FUNC
158  * @tc.require: AR000GHSJK
159  */
160 HWTEST_F(DmDiscoveryManagerTest, OnDeviceFound_002, testing::ext::TestSize.Level0)
161 {
162     std::string pkgName = "com.ohos.helloworld";
163     std::string filterOptions = R"(
164     {
165         "filter_op": "AND",
166         "filters":
167         [
168             {
169                 "type" : "credible",
170                 "value" : 2
171             }
172         ]
173     }
174     )";
175     DmDeviceFilterOption dmFilter;
176     dmFilter.TransformToFilter(filterOptions);
177     uint16_t aaa = 11;
178     DmDiscoveryContext context { pkgName, filterOptions, aaa, dmFilter.filterOp_, dmFilter.filters_ };
179     discoveryMgr_->discoveryContextMap_[pkgName] = context;
180     DmDeviceInfo info;
181     discoveryMgr_->OnDeviceFound(pkgName, info);
182     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
183     EXPECT_EQ(ret1, 1);
184     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq =
185         std::static_pointer_cast<IpcNotifyDeviceFoundReq>(listener_->ipcServerListener_.req_);
186     std::string ret = pReq->GetPkgName();
187     EXPECT_EQ(ret, pkgName);
188 }
189 
190 /**
191  * @tc.name: OnDiscoveryFailed_001
192  * @tc.desc: The OnDeviceFound function takes the wrong case and emptying pkgName
193  * @tc.type: FUNC
194  * @tc.require: AR000GHSJK
195  */
196 HWTEST_F(DmDiscoveryManagerTest, OnDiscoveryFailed_001, testing::ext::TestSize.Level0)
197 {
198     std::string pkgName = "com.ohos.helloworld";
199     int32_t subscribeId = 1;
200     int32_t failedReason = 3;
201     discoveryMgr_->OnDiscoveryFailed(pkgName, subscribeId, failedReason);
202     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
203         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
204     std::string ret = pReq->GetPkgName();
205     EXPECT_EQ(ret, pkgName);
206 }
207 
208 /**
209  * @tc.name: OnDiscoveryFailed_002
210  * @tc.desc: The OnDeviceFound function takes the wrong case and emptying pkgName
211  * @tc.type: FUNC
212  * @tc.require: AR000GHSJK
213  */
214 HWTEST_F(DmDiscoveryManagerTest, OnDiscoveryFailed_002, testing::ext::TestSize.Level0)
215 {
216     std::string pkgName;
217     int32_t subscribeId = 1;
218     int32_t failedReason = 3;
219     discoveryMgr_->OnDiscoveryFailed(pkgName, subscribeId, failedReason);
220     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
221     EXPECT_EQ(ret1, 0);
222     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
223         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
224     std::string ret = pReq->GetPkgName();
225     EXPECT_EQ(ret, pkgName);
226 }
227 
228 /**
229  * @tc.name: OnDiscoverySuccess_001
230  * @tc.desc: The OnDeviceFound function takes the wrong case and return pkgName
231  * @tc.type: FUNC
232  * @tc.require: AR000GHSJK
233  */
234 HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_001, testing::ext::TestSize.Level0)
235 {
236     std::string pkgName = "com.ohos.helloworld";
237     int32_t subscribeId = 1;
238     discoveryMgr_->OnDiscoverySuccess(pkgName, subscribeId);
239     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
240         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
241     std::string ret = pReq->GetPkgName();
242     EXPECT_EQ(ret, pkgName);
243 }
244 
245 /**
246  * @tc.name: OnDiscoverySuccess_002
247  * @tc.desc: set pkgName null and return discoveryContextMap_ null and return pkgName(null)
248  * @tc.type: FUNC
249  * @tc.require: AR000GHSJK
250  */
251 HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_002, testing::ext::TestSize.Level0)
252 {
253     std::string pkgName;
254     int32_t subscribeId = 1;
255     discoveryMgr_->OnDiscoverySuccess(pkgName, subscribeId);
256     int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName);
257     EXPECT_EQ(ret1, 1);
258     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq =
259         std::static_pointer_cast<IpcNotifyDiscoverResultReq>(listener_->ipcServerListener_.req_);
260     std::string ret = pReq->GetPkgName();
261     EXPECT_EQ(ret, pkgName);
262 }
263 } // namespace
264 } // namespace DistributedHardware
265 } // namespace OHOS
266