• 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 "device_manager_impl_test.h"
17 
18 #include <unistd.h>
19 
20 #include "dm_constants.h"
21 #include "dm_device_info.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
SetUp()25 void DeviceManagerImplTest::SetUp()
26 {
27 }
28 
TearDown()29 void DeviceManagerImplTest::TearDown()
30 {
31 }
32 
SetUpTestCase()33 void DeviceManagerImplTest::SetUpTestCase()
34 {
35 }
36 
TearDownTestCase()37 void DeviceManagerImplTest::TearDownTestCase()
38 {
39 }
40 
41 namespace {
42 /**
43  * @tc.name: InitDeviceManager
44  * @tc.desc: 1. set packName not null
45  *              set callback nullptr
46  *           2. MOCK DeviceManager InitDeviceManager return ERR_DM_INPUT_PARA_INVALID
47  *           3. call DeviceManager::InitDeviceManager with parameter
48  *           4. check ret is ERR_DM_INPUT_PARA_INVALID
49  * deviceTypeId
50  * @tc.type: FUNC
51  * @tc.require: AR000GHSJK
52  */
53 HWTEST_F(DeviceManagerImplTest, InitDeviceManager, testing::ext::TestSize.Level0)
54 {
55     // 1. set packName not null
56     std::string packName = "com.ohos.helloworld";
57     // set callback nullptr
58     // 3. call DeviceManager::InitDeviceManager with parameter
59     int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, nullptr);
60     // 4. check ret is ERR_DM_INPUT_PARA_INVALID
61     ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
62 }
63 
64 HWTEST_F(DeviceManagerImplTest, AuthenticateDevice1, testing::ext::TestSize.Level0)
65 {
66     std::string packName = "";
67     int32_t authType = 0;
68     DmDeviceInfo dmDeviceInfo;
69     std::string extra = "";
70     std::shared_ptr<AuthenticateCallback> callback = nullptr;
71     int32_t ret = DeviceManager::GetInstance().AuthenticateDevice(packName, authType, dmDeviceInfo, extra, callback);
72     ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
73 }
74 
75 HWTEST_F(DeviceManagerImplTest, AuthenticateDevice2, testing::ext::TestSize.Level0)
76 {
77     std::string packName = "com.ohos.helloworld";
78     int32_t authType = 0;
79     DmDeviceInfo dmDeviceInfo;
80     std::string extra = "";
81     std::shared_ptr<AuthenticateCallback> callback = nullptr;
82     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
83     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
84     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
85         .Times(1)
86         .WillOnce(testing::Return(ERR_DM_FAILED));
87     int32_t ret = DeviceManager::GetInstance().AuthenticateDevice(packName, authType, dmDeviceInfo, extra, callback);
88     ASSERT_EQ(ret, ERR_DM_IPC_RESPOND_FAILED);
89     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
90 }
91 
92 HWTEST_F(DeviceManagerImplTest, AuthenticateDevice3, testing::ext::TestSize.Level0)
93 {
94     std::string packName = "com.ohos.helloworld";
95     int32_t authType = 0;
96     DmDeviceInfo dmDeviceInfo;
97     std::string extra = "";
98     std::shared_ptr<AuthenticateCallback> callback = nullptr;
99     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
100     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
101     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
102         .Times(1)
103         .WillOnce(testing::Return(DM_OK));
104     int32_t ret = DeviceManager::GetInstance().AuthenticateDevice(packName, authType, dmDeviceInfo, extra, callback);
105     ASSERT_EQ(ret, DM_OK);
106     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
107 }
108 
109 HWTEST_F(DeviceManagerImplTest, VerifyAuthentication1, testing::ext::TestSize.Level0)
110 {
111     std::string packName = "";
112     std::string authPara = "";
113     std::shared_ptr<VerifyAuthCallback> callback = nullptr;
114     int32_t ret = DeviceManager::GetInstance().VerifyAuthentication(packName, authPara, callback);
115     ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
116 }
117 
118 HWTEST_F(DeviceManagerImplTest, VerifyAuthentication2, testing::ext::TestSize.Level0)
119 {
120     std::string packName = "com.ohos.helloworld";
121     std::string authPara = "";
122     std::shared_ptr<VerifyAuthCallback> callback = nullptr;
123     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
124     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
125     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
126         .Times(1)
127         .WillOnce(testing::Return(ERR_DM_FAILED));
128     int32_t ret = DeviceManager::GetInstance().VerifyAuthentication(packName, authPara, callback);
129     ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED);
130     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
131 }
132 
133 HWTEST_F(DeviceManagerImplTest, VerifyAuthentication3, testing::ext::TestSize.Level0)
134 {
135     std::string packName = "com.ohos.helloworld";
136     std::string authPara = "";
137     std::shared_ptr<VerifyAuthCallback> callback = nullptr;
138     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
139     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
140     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
141         .Times(1)
142         .WillOnce(testing::Return(DM_OK));
143     int32_t ret = DeviceManager::GetInstance().VerifyAuthentication(packName, authPara, callback);
144     ASSERT_EQ(ret, DM_OK);
145     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
146 }
147 
148 HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery1, testing::ext::TestSize.Level0)
149 {
150     std::string packName = "";
151     DmSubscribeInfo subscribeInfo;
152     std::string extra = "";
153     std::shared_ptr<DiscoveryCallback> callback = nullptr;
154     int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, extra, callback);
155     ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
156 }
157 
158 HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery2, testing::ext::TestSize.Level0)
159 {
160     std::string packName = "com.ohos.helloworld";
161     DmSubscribeInfo subscribeInfo;
162     std::string extra = "";
163     test_callback_ = std::make_shared<DeviceDiscoveryCallback>();
164     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
165     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
166     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
167         .Times(1)
168         .WillOnce(testing::Return(DM_OK));
169     int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, extra, test_callback_);
170     ASSERT_EQ(ret, DM_OK);
171     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
172 }
173 
174 HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery3, testing::ext::TestSize.Level0)
175 {
176     std::string packName = "com.ohos.helloworld";
177     DmSubscribeInfo subscribeInfo;
178     std::string extra = "";
179     test_callback_ = std::make_shared<DeviceDiscoveryCallback>();
180     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
181     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
182     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
183         .Times(1)
184         .WillOnce(testing::Return(ERR_DM_FAILED));
185     int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, extra, test_callback_);
186     ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED);
187     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
188 }
189 
190 HWTEST_F(DeviceManagerImplTest, PublishDeviceDiscovery1, testing::ext::TestSize.Level0)
191 {
192     std::string packName = "";
193     DmPublishInfo publishInfo;
194     std::shared_ptr<PublishCallback> callback = nullptr;
195     int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, publishInfo, callback);
196     ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
197 }
198 
199 HWTEST_F(DeviceManagerImplTest, PublishDeviceDiscovery2, testing::ext::TestSize.Level0)
200 {
201     std::string packName = "com.ohos.helloworld";
202     DmPublishInfo publishInfo;
203     testPublishCallback_ = std::make_shared<DevicePublishCallback>();
204     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
205     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
206     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
207         .Times(1)
208         .WillOnce(testing::Return(DM_OK));
209     int32_t ret = DeviceManager::GetInstance().PublishDeviceDiscovery(packName, publishInfo, testPublishCallback_);
210     ASSERT_EQ(ret, DM_OK);
211     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
212 }
213 
214 HWTEST_F(DeviceManagerImplTest, PublishDeviceDiscovery3, testing::ext::TestSize.Level0)
215 {
216     std::string packName = "com.ohos.helloworld";
217     DmPublishInfo publishInfo;
218     testPublishCallback_ = std::make_shared<DevicePublishCallback>();
219     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
220     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
221     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
222         .Times(1)
223         .WillOnce(testing::Return(ERR_DM_FAILED));
224     int32_t ret = DeviceManager::GetInstance().PublishDeviceDiscovery(packName, publishInfo, testPublishCallback_);
225     ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED);
226     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
227 }
228 } // namespace
229 
OnDiscoverySuccess(uint16_t subscribeId)230 void DeviceDiscoveryCallback::OnDiscoverySuccess(uint16_t subscribeId)
231 {
232     (void)subscribeId;
233 }
234 
OnDiscoveryFailed(uint16_t subscribeId,int32_t failedReason)235 void DeviceDiscoveryCallback::OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason)
236 {
237     (void)subscribeId;
238     (void)failedReason;
239 }
240 
OnDeviceFound(uint16_t subscribeId,const DmDeviceInfo & deviceInfo)241 void DeviceDiscoveryCallback::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo)
242 {
243     (void)subscribeId;
244     (void)deviceInfo;
245 }
OnPublishResult(int32_t publishId,int32_t failedReason)246 void DevicePublishCallback::OnPublishResult(int32_t publishId, int32_t failedReason)
247 {
248     (void)publishId;
249     (void)failedReason;
250 }
251 } // namespace DistributedHardware
252 } // namespace OHOS
253