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_ipc_server_client_proxy.h"
17
18 #include <unistd.h>
19
20 #include "dm_device_info.h"
21 #include "device_manager_ipc_interface_code.h"
22 #include "ipc_remote_broker.h"
23 #include "iremote_object.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 #include "ipc_client_manager.h"
27 #include "ipc_set_useroperation_req.h"
28 #include "ipc_notify_device_state_req.h"
29 #include "ipc_rsp.h"
30 #include "ipc_notify_device_found_req.h"
31 #include "ipc_notify_discover_result_req.h"
32 #include "ipc_notify_publish_result_req.h"
33 #include "ipc_notify_auth_result_req.h"
34 #include "ipc_notify_verify_auth_result_req.h"
35 #include "ipc_notify_dmfa_result_req.h"
36 #include "ipc_notify_credential_req.h"
37 #include "dm_constants.h"
38
39 namespace OHOS {
40 namespace DistributedHardware {
SetUp()41 void IpcServerClientProxyTest::SetUp()
42 {
43 }
44
TearDown()45 void IpcServerClientProxyTest::TearDown()
46 {
47 }
48
SetUpTestCase()49 void IpcServerClientProxyTest::SetUpTestCase()
50 {
51 }
52
TearDownTestCase()53 void IpcServerClientProxyTest::TearDownTestCase()
54 {
55 }
56
57 namespace {
58 /**
59 * @tc.name: SendCmd_001
60 * @tc.desc: 1. set cmdCode not null
61 * 2. set remoteObject nullptr
62 * 3. call IpcServerClientProxy SendCmd
63 * 4. check ret is DEVICEMANAGER_NULLPTR
64 * @tc.type: FUNC
65 * @tc.require: AR000GHSJK
66 */
67 HWTEST_F(IpcServerClientProxyTest, SendCmd_001, testing::ext::TestSize.Level0)
68 {
69 // 1. set cmdCode not null
70 int32_t cmdCode = 1;
71 // 2. set remoteObject nullptr
72 sptr<IRemoteObject> remoteObject = nullptr;
73 // 3. call IpcServerClientProxy SendCmd
74 auto instance = new IpcServerClientProxy(remoteObject);
75 int ret = instance->SendCmd(cmdCode, nullptr, nullptr);
76 // 4. check ret is DEVICEMANAGER_NULLPTR
77 ASSERT_EQ(ret, ERR_DM_POINT_NULL);
78 }
79
80 /**
81 * @tc.name: SendCmd_002
82 * @tc.desc: 1. set cmdCode not null
83 * set pkgName not null
84 * set action not null
85 * 2. set remoteObject not nullptr
86 * set req not null
87 * set rsp not null
88 * 3. call IpcServerClientProxy SendCmd with parameter
89 * 4. check ret is DM_OK
90 * @tc.type: FUNC
91 * @tc.require: AR000GHSJK
92 */
93 HWTEST_F(IpcServerClientProxyTest, SendCmd_002, testing::ext::TestSize.Level0)
94 {
95 // 1. set cmdCode not null
96 int32_t cmdCode = SERVER_DEVICE_STATE_NOTIFY;
97 // set pkgName not null
98 std::string pkgName = "com.ohos.test";
99 // set action not null
100 int deviceState = 1;
101 DmDeviceInfo deviceInfo;
102 // 2. set remoteObject not nullptr
103 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
104 IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
105 std::shared_ptr<IpcNotifyDeviceStateReq> req = std::make_shared<IpcNotifyDeviceStateReq>();
106 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
107 // set req not null
108 req->SetPkgName(pkgName);
109 // set rsp not null
110 req->SetDeviceState(deviceState);
111 req->SetDeviceInfo(deviceInfo);
112 // 3. call IpcServerClientProxy SendCmd with parameter
113 int ret = 0;
114 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
115 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
116 // 4. check ret is not ERR_DM_FAILED
117 ASSERT_NE(ret, ERR_DM_FAILED);
118 }
119
120 /**
121 * @tc.name: SendCmd_003
122 * @tc.desc: 1. set cmdCode not null
123 * set pkgName not null
124 * set action not null
125 * 2. set remoteObject not nullptr
126 * set req not null
127 * set rsp not null
128 * 3. call IpcServerClientProxy SendCmd with parameter
129 * 4. check ret is DM_OK
130 * @tc.type: FUNC
131 * @tc.require: AR000GHSJK
132 */
133 HWTEST_F(IpcServerClientProxyTest, SendCmd_003, testing::ext::TestSize.Level0)
134 {
135 // 1. set cmdCode not null
136 int32_t cmdCode = SERVER_DEVICE_FOUND;
137 // set pkgName not null
138 std::string pkgName = "com.ohos.test";
139 // set action not null
140 uint16_t subscribeId = 1;
141 DmDeviceInfo dmDeviceInfo;
142 // 2. set remoteObject not nullptr
143 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
144 IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
145 std::shared_ptr<IpcNotifyDeviceFoundReq> req = std::make_shared<IpcNotifyDeviceFoundReq>();
146 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
147 // set req not null
148 req->SetPkgName(pkgName);
149 // set rsp not null
150 req->SetSubscribeId(subscribeId);
151 req->SetDeviceInfo(dmDeviceInfo);
152 // 3. call IpcServerClientProxy SendCmd with parameter
153 int ret = 0;
154 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
155 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
156 // 4. check ret is not ERR_DM_FAILED
157 ASSERT_NE(ret, ERR_DM_FAILED);
158 }
159
160 /**
161 * @tc.name: SendCmd_004
162 * @tc.desc: 1. set cmdCode not null
163 * set pkgName not null
164 * set action not null
165 * 2. set remoteObject not nullptr
166 * set req not null
167 * set rsp not null
168 * 3. call IpcServerClientProxy SendCmd with parameter
169 * 4. check ret is DM_OK
170 * @tc.type: FUNC
171 * @tc.require: AR000GHSJK
172 */
173 HWTEST_F(IpcServerClientProxyTest, SendCmd_004, testing::ext::TestSize.Level0)
174 {
175 // 1. set cmdCode not null
176 int32_t cmdCode = SERVER_DISCOVER_FINISH;
177 // set pkgName not null
178 std::string pkgName = "com.ohos.test";
179 // set action not null
180 uint16_t subscribeId = 1;
181 int32_t result = 1;
182 // 2. set remoteObject not nullptr
183 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
184 IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
185 std::shared_ptr<IpcNotifyDiscoverResultReq> req = std::make_shared<IpcNotifyDiscoverResultReq>();
186 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
187 // set req not null
188 req->SetPkgName(pkgName);
189 // set rsp not null
190 req->SetSubscribeId(subscribeId);
191 req->SetResult(result);
192 // 3. call IpcServerClientProxy SendCmd with parameter
193 int ret = 0;
194 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
195 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
196 // 4. check ret is not ERR_DM_FAILED
197 ASSERT_NE(ret, ERR_DM_FAILED);
198 }
199
200 /**
201 * @tc.name: SendCmd_005
202 * @tc.desc: 1. set cmdCode not null
203 * set pkgName not null
204 * set action not null
205 * 2. set remoteObject not nullptr
206 * set req not null
207 * set rsp not null
208 * 3. call IpcServerClientProxy SendCmd with parameter
209 * 4. check ret is DM_OK
210 * @tc.type: FUNC
211 * @tc.require: I5N1K3
212 */
213 HWTEST_F(IpcServerClientProxyTest, SendCmd_005, testing::ext::TestSize.Level0)
214 {
215 // 1. set cmdCode not null
216 int32_t cmdCode = SERVER_PUBLISH_FINISH;
217 // set pkgName not null
218 std::string pkgName = "com.ohos.test";
219 // set action not null
220 int32_t publishId = 1;
221 int32_t result = 1;
222 // 2. set remoteObject not nullptr
223 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
224 IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
225 std::shared_ptr<IpcNotifyPublishResultReq> req = std::make_shared<IpcNotifyPublishResultReq>();
226 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
227 // set req not null
228 req->SetPkgName(pkgName);
229 // set rsp not null
230 req->SetPublishId(publishId);
231 req->SetResult(result);
232 // 3. call IpcServerClientProxy SendCmd with parameter
233 int ret = 0;
234 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
235 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
236 // 4. check ret is not ERR_DM_FAILED
237 ASSERT_NE(ret, ERR_DM_FAILED);
238 }
239
240 /**
241 * @tc.name: SendCmd_006
242 * @tc.type: FUNC
243 * @tc.require: I5N1K3
244 */
245 HWTEST_F(IpcServerClientProxyTest, SendCmd_006, testing::ext::TestSize.Level0)
246 {
247 int32_t cmdCode = SERVER_AUTH_RESULT;
248 std::string pkgName = "com.ohos.test";
249 std::string deviceId = "123";
250 std::string token = "123456";
251 int32_t status = 1;
252 int32_t reason = 0;
253 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
254 IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
255 std::shared_ptr<IpcNotifyAuthResultReq> req = std::make_shared<IpcNotifyAuthResultReq>();
256 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
257 req->SetPkgName(pkgName);
258 req->SetDeviceId(deviceId);
259 req->SetToken(token);
260 req->SetStatus(status);
261 req->SetReason(reason);
262 int ret = 0;
263 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
264 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
265 ASSERT_EQ(ret, DM_OK);
266 }
267
268 /**
269 * @tc.name: SendCmd_007
270 * @tc.type: FUNC
271 * @tc.require: I5N1K3
272 */
273 HWTEST_F(IpcServerClientProxyTest, SendCmd_007, testing::ext::TestSize.Level0)
274 {
275 int32_t cmdCode = SERVER_VERIFY_AUTH_RESULT;
276 std::string pkgName = "com.ohos.test";
277 std::string deviceId = "123";
278 int32_t result = 0;
279 int32_t flag = 1;
280 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
281 IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
282 std::shared_ptr<IpcNotifyVerifyAuthResultReq> req = std::make_shared<IpcNotifyVerifyAuthResultReq>();
283 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
284 req->SetPkgName(pkgName);
285 req->SetDeviceId(deviceId);
286 req->SetFlag(flag);
287 req->SetResult(result);
288 int ret = 0;
289 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
290 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
291 ASSERT_EQ(ret, DM_OK);
292 }
293
294 /**
295 * @tc.name: SendCmd_008
296 * @tc.type: FUNC
297 * @tc.require: I5N1K3
298 */
299 HWTEST_F(IpcServerClientProxyTest, SendCmd_008, testing::ext::TestSize.Level0)
300 {
301 int32_t cmdCode = SERVER_DEVICE_FA_NOTIFY;
302 std::string pkgName = "com.ohos.test";
303 std::string paramJson = "123";
304 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
305 IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
306 std::shared_ptr<IpcNotifyDMFAResultReq> req = std::make_shared<IpcNotifyDMFAResultReq>();
307 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
308 req->SetPkgName(pkgName);
309 req->SetJsonParam(paramJson);
310 int ret = 0;
311 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
312 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
313 ASSERT_EQ(ret, DM_OK);
314 }
315
316 /**
317 * @tc.name: SendCmd_09
318 * @tc.type: FUNC
319 * @tc.require: I5N1K3
320 */
321 HWTEST_F(IpcServerClientProxyTest, SendCmd_09, testing::ext::TestSize.Level0)
322 {
323 int32_t cmdCode = SERVER_CREDENTIAL_RESULT;
324 std::string pkgName = "com.ohos.test";
325 int32_t action = 1;
326 std::string credentialResult = "123";
327 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
328 IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
329 std::shared_ptr<IpcNotifyCredentialReq> req = std::make_shared<IpcNotifyCredentialReq>();
330 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
331 req->SetPkgName(pkgName);
332 req->SetCredentialResult(credentialResult);
333 req->SetCredentialAction(action);
334 int ret = 0;
335 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
336 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
337 ASSERT_EQ(ret, DM_OK);
338 }
339 } // namespace
340 } // namespace DistributedHardware
341 } // namespace OHOS
342