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, StartDeviceDiscovery1, testing::ext::TestSize.Level0)
110 {
111     std::string packName = "";
112     DmSubscribeInfo subscribeInfo;
113     std::string extra = "";
114     std::shared_ptr<DiscoveryCallback> callback = nullptr;
115     int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, extra, callback);
116     ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
117 }
118 
119 HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery2, testing::ext::TestSize.Level0)
120 {
121     std::string packName = "com.ohos.helloworld";
122     DmSubscribeInfo subscribeInfo;
123     std::string extra = "";
124     test_callback_ = std::make_shared<DeviceDiscoveryCallback>();
125     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
126     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
127     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
128         .Times(1)
129         .WillOnce(testing::Return(DM_OK));
130     int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, extra, test_callback_);
131     ASSERT_EQ(ret, DM_OK);
132     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
133 }
134 
135 HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery3, testing::ext::TestSize.Level0)
136 {
137     std::string packName = "com.ohos.helloworld";
138     DmSubscribeInfo subscribeInfo;
139     std::string extra = "";
140     test_callback_ = std::make_shared<DeviceDiscoveryCallback>();
141     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
142     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
143     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
144         .Times(1)
145         .WillOnce(testing::Return(ERR_DM_FAILED));
146     int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, extra, test_callback_);
147     ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED);
148     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
149 }
150 
151 HWTEST_F(DeviceManagerImplTest, PublishDeviceDiscovery1, testing::ext::TestSize.Level0)
152 {
153     std::string packName = "";
154     DmPublishInfo publishInfo;
155     std::shared_ptr<PublishCallback> callback = nullptr;
156     int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, publishInfo, callback);
157     ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
158 }
159 
160 HWTEST_F(DeviceManagerImplTest, PublishDeviceDiscovery2, testing::ext::TestSize.Level0)
161 {
162     std::string packName = "com.ohos.helloworld";
163     DmPublishInfo publishInfo;
164     testPublishCallback_ = std::make_shared<DevicePublishCallback>();
165     std::shared_ptr<MockIpcClientProxy> mockInstance = std::make_shared<MockIpcClientProxy>();
166     DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance;
167     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
168         .Times(1)
169         .WillOnce(testing::Return(DM_OK));
170     int32_t ret = DeviceManager::GetInstance().PublishDeviceDiscovery(packName, publishInfo, testPublishCallback_);
171     ASSERT_EQ(ret, DM_OK);
172     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
173 }
174 
175 HWTEST_F(DeviceManagerImplTest, PublishDeviceDiscovery3, testing::ext::TestSize.Level0)
176 {
177     std::string packName = "com.ohos.helloworld";
178     DmPublishInfo publishInfo;
179     testPublishCallback_ = std::make_shared<DevicePublishCallback>();
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().PublishDeviceDiscovery(packName, publishInfo, testPublishCallback_);
186     ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED);
187     DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr;
188 }
189 } // namespace
190 
OnDiscoverySuccess(uint16_t subscribeId)191 void DeviceDiscoveryCallback::OnDiscoverySuccess(uint16_t subscribeId)
192 {
193     (void)subscribeId;
194 }
195 
OnDiscoveryFailed(uint16_t subscribeId,int32_t failedReason)196 void DeviceDiscoveryCallback::OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason)
197 {
198     (void)subscribeId;
199     (void)failedReason;
200 }
201 
OnDeviceFound(uint16_t subscribeId,const DmDeviceInfo & deviceInfo)202 void DeviceDiscoveryCallback::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo)
203 {
204     (void)subscribeId;
205     (void)deviceInfo;
206 }
OnPublishResult(int32_t publishId,int32_t failedReason)207 void DevicePublishCallback::OnPublishResult(int32_t publishId, int32_t failedReason)
208 {
209     (void)publishId;
210     (void)failedReason;
211 }
212 } // namespace DistributedHardware
213 } // namespace OHOS
214