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