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     // 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 } // namespace
239 } // namespace DistributedHardware
240 } // namespace OHOS
241