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_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 ProcessInfo processInfo;
99 processInfo.pkgName = pkgName;
100 processInfo.userId = 100;
101 // set action not null
102 int deviceState = 1;
103 DmDeviceInfo deviceInfo;
104 // 2. set remoteObject not nullptr
105 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
106 IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, remoteObject);
107 std::shared_ptr<IpcNotifyDeviceStateReq> req = std::make_shared<IpcNotifyDeviceStateReq>();
108 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
109 // set req not null
110 req->SetPkgName(pkgName);
111 // set rsp not null
112 req->SetDeviceState(deviceState);
113 req->SetDeviceInfo(deviceInfo);
114 // 3. call IpcServerClientProxy SendCmd with parameter
115 int ret = 0;
116 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
117 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
118 // 4. check ret is not ERR_DM_FAILED
119 ASSERT_NE(ret, ERR_DM_FAILED);
120 }
121
122 /**
123 * @tc.name: SendCmd_003
124 * @tc.desc: 1. set cmdCode not null
125 * set pkgName not null
126 * set action not null
127 * 2. set remoteObject not nullptr
128 * set req not null
129 * set rsp not null
130 * 3. call IpcServerClientProxy SendCmd with parameter
131 * 4. check ret is DM_OK
132 * @tc.type: FUNC
133 * @tc.require: AR000GHSJK
134 */
135 HWTEST_F(IpcServerClientProxyTest, SendCmd_003, testing::ext::TestSize.Level0)
136 {
137 // 1. set cmdCode not null
138 int32_t cmdCode = SERVER_DEVICE_FOUND;
139 // set pkgName not null
140 std::string pkgName = "com.ohos.test";
141 ProcessInfo processInfo;
142 processInfo.pkgName = pkgName;
143 processInfo.userId = 100;
144 // set action not null
145 uint16_t subscribeId = 1;
146 DmDeviceInfo dmDeviceInfo;
147 // 2. set remoteObject not nullptr
148 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
149 IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, remoteObject);
150 std::shared_ptr<IpcNotifyDeviceFoundReq> req = std::make_shared<IpcNotifyDeviceFoundReq>();
151 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
152 // set req not null
153 req->SetPkgName(pkgName);
154 // set rsp not null
155 req->SetSubscribeId(subscribeId);
156 req->SetDeviceInfo(dmDeviceInfo);
157 // 3. call IpcServerClientProxy SendCmd with parameter
158 int ret = 0;
159 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
160 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
161 // 4. check ret is not ERR_DM_FAILED
162 ASSERT_NE(ret, ERR_DM_FAILED);
163 }
164
165 /**
166 * @tc.name: SendCmd_004
167 * @tc.desc: 1. set cmdCode not null
168 * set pkgName not null
169 * set action not null
170 * 2. set remoteObject not nullptr
171 * set req not null
172 * set rsp not null
173 * 3. call IpcServerClientProxy SendCmd with parameter
174 * 4. check ret is DM_OK
175 * @tc.type: FUNC
176 * @tc.require: AR000GHSJK
177 */
178 HWTEST_F(IpcServerClientProxyTest, SendCmd_004, testing::ext::TestSize.Level0)
179 {
180 // 1. set cmdCode not null
181 int32_t cmdCode = SERVER_DISCOVER_FINISH;
182 // set pkgName not null
183 std::string pkgName = "com.ohos.test";
184 ProcessInfo processInfo;
185 processInfo.pkgName = pkgName;
186 processInfo.userId = 100;
187 // set action not null
188 uint16_t subscribeId = 1;
189 int32_t result = 1;
190 // 2. set remoteObject not nullptr
191 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
192 IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, remoteObject);
193 std::shared_ptr<IpcNotifyDiscoverResultReq> req = std::make_shared<IpcNotifyDiscoverResultReq>();
194 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
195 // set req not null
196 req->SetPkgName(pkgName);
197 // set rsp not null
198 req->SetSubscribeId(subscribeId);
199 req->SetResult(result);
200 // 3. call IpcServerClientProxy SendCmd with parameter
201 int ret = 0;
202 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
203 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
204 // 4. check ret is not ERR_DM_FAILED
205 ASSERT_NE(ret, ERR_DM_FAILED);
206 }
207
208 /**
209 * @tc.name: SendCmd_005
210 * @tc.desc: 1. set cmdCode not null
211 * set pkgName not null
212 * set action not null
213 * 2. set remoteObject not nullptr
214 * set req not null
215 * set rsp not null
216 * 3. call IpcServerClientProxy SendCmd with parameter
217 * 4. check ret is DM_OK
218 * @tc.type: FUNC
219 * @tc.require: I5N1K3
220 */
221 HWTEST_F(IpcServerClientProxyTest, SendCmd_005, testing::ext::TestSize.Level0)
222 {
223 // 1. set cmdCode not null
224 int32_t cmdCode = SERVER_PUBLISH_FINISH;
225 // set pkgName not null
226 std::string pkgName = "com.ohos.test";
227 ProcessInfo processInfo;
228 processInfo.pkgName = pkgName;
229 processInfo.userId = 100;
230 // set action not null
231 int32_t publishId = 1;
232 int32_t result = 1;
233 // 2. set remoteObject not nullptr
234 sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
235 IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, remoteObject);
236 std::shared_ptr<IpcNotifyPublishResultReq> req = std::make_shared<IpcNotifyPublishResultReq>();
237 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
238 // set req not null
239 req->SetPkgName(pkgName);
240 // set rsp not null
241 req->SetPublishId(publishId);
242 req->SetResult(result);
243 // 3. call IpcServerClientProxy SendCmd with parameter
244 int ret = 0;
245 std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
246 ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
247 // 4. check ret is not ERR_DM_FAILED
248 ASSERT_NE(ret, ERR_DM_FAILED);
249 }
250 } // namespace
251 } // namespace DistributedHardware
252 } // namespace OHOS
253