• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_client_proxy.h"
17 #include "dm_device_info.h"
18 #include "ipc_remote_broker.h"
19 #include "iremote_object.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "ipc_client_manager.h"
23 #include "dm_constants.h"
24 
25 #include <unistd.h>
26 
27 namespace OHOS {
28 namespace DistributedHardware {
SetUp()29 void IpcClientProxyTest::SetUp()
30 {
31 }
32 
TearDown()33 void IpcClientProxyTest::TearDown()
34 {
35 }
36 
SetUpTestCase()37 void IpcClientProxyTest::SetUpTestCase()
38 {
39 }
40 
TearDownTestCase()41 void IpcClientProxyTest::TearDownTestCase()
42 {
43 }
44 
45 namespace {
46 /**
47  * @tc.name: Init_001
48  * @tc.desc: 1. set pkgName not null
49  *           2. set IpcClientProxy ipcClientManager nullptr
50  *           3. call IpcClientProxy Init
51  *           4. check ret is DM_POINT_NULL
52  * @tc.type: FUNC
53  * @tc.require: AR000GHSJK
54  */
55 HWTEST_F(IpcClientProxyTest, Init_001, testing::ext::TestSize.Level0)
56 {
57     // 1. set pkgName not null
58     std::string pkgName = "com.ohos.test";
59     // 2. set IpcClientProxy ipcClientManager nullptr
60     std::shared_ptr<IpcClient> ipcClientManager = nullptr;
61     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
62     // 3. call IpcClientProxy
63     int32_t ret = ipcClientProxy->Init(pkgName);
64     // 4. check ret is DM_POINT_NULL
65     ASSERT_EQ(ret, DM_POINT_NULL);
66 }
67 
68 /**
69  * @tc.name: Init_002
70  * @tc.desc: 1. set pkgName not null
71  *           2. Mock IpcClient Init return DM_FAILED
72  *           3. call IpcClientProxy Init
73  *           4. check ret is DM_FAILED
74  * @tc.type: FUNC
75  * @tc.require: AR000GHSJK
76  */
77 HWTEST_F(IpcClientProxyTest, Init_002, testing::ext::TestSize.Level0)
78 {
79     // 1. set pkgName not null
80     std::string pkgName = "com.ohos.test";
81     // 2. Mock IpcClient Init return DM_FAILED
82     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
83     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
84     EXPECT_CALL(*mockInstance, Init(testing::_)).Times(1).WillOnce(testing::Return(DM_FAILED));
85     // 3. call IpcClientProxy Init
86     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
87     int32_t ret = ipcClientProxy->Init(pkgName);
88     // 4. check ret is DM_FAILED
89     ASSERT_EQ(ret, DM_FAILED);
90 }
91 
92 /**
93  * @tc.name: Init_003
94  * @tc.desc: 1. set pkgName not null
95  *           2. Mock IpcClient Init return DM_OK
96  *           3. call IpcClientProxy Init
97  *           4. check ret is DM_OK
98  * @tc.type: FUNC
99  * @tc.require: AR000GHSJK
100  */
101 HWTEST_F(IpcClientProxyTest, Init_003, testing::ext::TestSize.Level0)
102 {
103     // 1. set pkgName not null
104     std::string pkgName = "com.ohos.test";
105     // 2. Mock IpcClient Init return DM_OK
106     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
107     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
108     EXPECT_CALL(*mockInstance, Init(testing::_)).Times(1).WillOnce(testing::Return(DM_OK));
109     // 3. call IpcClientProxy Init
110     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
111     int32_t ret = ipcClientProxy->Init(pkgName);
112     // 4. check ret is DM_OK
113     ASSERT_EQ(ret, DM_OK);
114 }
115 
116 /**
117  * @tc.name: Init_004
118  * @tc.desc: 1. set pkgName not null
119  *           2. Mock IpcClient Init return DM_SERVICE_NOT_READY
120  *           3. call IpcClientProxy Init
121  *           4. check ret is DM_SERVICE_NOT_READY
122  * @tc.type: FUNC
123  * @tc.require: AR000GHSJK
124  */
125 HWTEST_F(IpcClientProxyTest, Init_004, testing::ext::TestSize.Level0)
126 {
127     // 1. set pkgName not null
128     std::string pkgName = "com.ohos.test";
129     // 2. Mock IpcClient Init return DM_SERVICE_NOT_READY
130     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
131     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
132     EXPECT_CALL(*mockInstance, Init(testing::_)).Times(1).WillOnce(testing::Return(DM_SERVICE_NOT_READY));
133     // 3. call IpcClientProxy Init
134     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
135     int32_t ret = ipcClientProxy->Init(pkgName);
136     // 4. check ret is DM_SERVICE_NOT_READY
137     ASSERT_EQ(ret, DM_SERVICE_NOT_READY);
138 }
139 
140 /**
141  * @tc.name: Init_005
142  * @tc.desc: 1. set pkgName not null
143  *           2. Mock IpcClient Init return DM_IPC_FAILED
144  *           3. call IpcClientProxy Init
145  *           4. check ret is DM_IPC_FAILED
146  * @tc.type: FUNC
147  * @tc.require: AR000GHSJK
148  */
149 HWTEST_F(IpcClientProxyTest, Init_005, testing::ext::TestSize.Level0)
150 {
151     // 1. set pkgName not null
152     std::string pkgName = "com.ohos.test";
153     // 2. Mock IpcClient Init return DM_IPC_FAILED
154     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
155     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
156     EXPECT_CALL(*mockInstance, Init(testing::_)).Times(1).WillOnce(testing::Return(DM_IPC_FAILED));
157     // 3. call IpcClientProxy Init
158     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
159     int32_t ret = ipcClientProxy->Init(pkgName);
160     // 4. check ret is DM_IPC_FAILED
161     ASSERT_EQ(ret, DM_IPC_FAILED);
162 }
163 
164 /**
165  * @tc.name: UnInit_001
166  * @tc.desc: 1. set pkgName not null
167  *           2. set IpcClientProxy ipcClientManager nullptr
168  *           3. call IpcClientProxy UnInit
169  *           4. check ret is DM_POINT_NULL
170  * @tc.type: FUNC
171  * @tc.require: AR000GHSJK
172  */
173 HWTEST_F(IpcClientProxyTest, UnInit_001, testing::ext::TestSize.Level0)
174 {
175     // 1. set pkgName not null
176     std::string pkgName = "com.ohos.test";
177     // 2. set IpcClientProxy ipcClientManager nullptr
178     std::shared_ptr<IpcClient> ipcClientManager = nullptr;
179     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
180     // 3. call IpcClientProxy
181     int32_t ret = ipcClientProxy->UnInit(pkgName);
182     // 4. check ret is DM_POINT_NULL
183     ASSERT_EQ(ret, DM_POINT_NULL);
184 }
185 
186 /**
187  * @tc.name: UnInit_002
188  * @tc.desc: 1. set pkgName not null
189  *           2. Mock IpcClient Init return DM_FAILED
190  *           3. call IpcClientProxy UnInit
191  *           4. check ret is DM_FAILED
192  * @tc.type: FUNC
193  * @tc.require: AR000GHSJK
194  */
195 HWTEST_F(IpcClientProxyTest, UnInit_002, testing::ext::TestSize.Level0)
196 {
197     // 1. set pkgName not null
198     std::string pkgName = "com.ohos.test";
199     // 2. Mock IpcClient Init return DM_FAILED
200     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
201     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
202     EXPECT_CALL(*mockInstance, UnInit(testing::_)).Times(1).WillOnce(testing::Return(DM_FAILED));
203     // 3. call IpcClientProxy Init
204     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
205     int32_t ret = ipcClientProxy->UnInit(pkgName);
206     // 4. check ret is DM_FAILED
207     ASSERT_EQ(ret, DM_FAILED);
208 }
209 
210 /**
211  * @tc.name: UnInit_003
212  * @tc.desc: 1. set pkgName not null
213  *           2. Mock IpcClient UnInit return DM_OK
214  *           3. call IpcClientProxy UnInit
215  *           4. check ret is DM_OK
216  * @tc.type: FUNC
217  * @tc.require: AR000GHSJK
218  */
219 HWTEST_F(IpcClientProxyTest, UnInit_003, testing::ext::TestSize.Level0)
220 {
221     // 1. set pkgName not null
222     std::string pkgName = "com.ohos.test";
223     // 2. Mock IpcClient Init return DM_OK
224     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
225     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
226     EXPECT_CALL(*mockInstance, UnInit(testing::_)).Times(1).WillOnce(testing::Return(DM_OK));
227     // 3. call IpcClientProxy Init
228     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
229     int32_t ret = ipcClientProxy->UnInit(pkgName);
230     // 4. check ret is DM_OK
231     ASSERT_EQ(ret, DM_OK);
232 }
233 
234 /**
235  * @tc.name: UnInit_004
236  * @tc.desc: 1. set pkgName not null
237  *           2. Mock IpcClient UnInit return DM_SERVICE_NOT_READY
238  *           3. call IpcClientProxy UnInit
239  *           4. check ret is DM_SERVICE_NOT_READY
240  * @tc.type: FUNC
241  * @tc.require: AR000GHSJK
242  */
243 HWTEST_F(IpcClientProxyTest, UnInit_004, testing::ext::TestSize.Level0)
244 {
245     // 1. set pkgName not null
246     std::string pkgName = "com.ohos.test";
247     // 2. Mock IpcClient Init return DM_SERVICE_NOT_READY
248     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
249     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
250     EXPECT_CALL(*mockInstance, UnInit(testing::_)).Times(1).WillOnce(testing::Return(DM_SERVICE_NOT_READY));
251     // 3. call IpcClientProxy Init
252     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
253     int32_t ret = ipcClientProxy->UnInit(pkgName);
254     // 4. check ret is DM_SERVICE_NOT_READY
255     ASSERT_EQ(ret, DM_SERVICE_NOT_READY);
256 }
257 
258 /**
259  * @tc.name: UnInit_005
260  * @tc.desc: 1. set pkgName not null
261  *           2. Mock IpcClient UnInit return DM_IPC_FAILED
262  *           3. call IpcClientProxy UnInit
263  *           4. check ret is DM_IPC_FAILED
264  * @tc.type: FUNC
265  * @tc.require: AR000GHSJK
266  */
267 HWTEST_F(IpcClientProxyTest, UnInit_005, testing::ext::TestSize.Level0)
268 {
269     // 1. set pkgName not null
270     std::string pkgName = "com.ohos.test";
271     // 2. Mock IpcClient Init return DM_IPC_FAILED
272     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
273     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
274     EXPECT_CALL(*mockInstance, UnInit(testing::_)).Times(1).WillOnce(testing::Return(DM_IPC_FAILED));
275     // 3. call IpcClientProxy Init
276     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
277     int32_t ret = ipcClientProxy->UnInit(pkgName);
278     // 4. check ret is DM_IPC_FAILED
279     ASSERT_EQ(ret, DM_IPC_FAILED);
280 }
281 
282 /**
283  * @tc.name: SendRequest_001
284  * @tc.desc: 1. set req nullptr
285  *              set rsp not nullptr
286  *              set IpcClientProxy ipcClientManager not null
287  *           2. call IpcClientProxy SendRequest
288  *           3. check ret is DEVICEMANAGER_NULLPTR
289  * @tc.type: FUNC
290  * @tc.require: AR000GHSJK
291  */
292 HWTEST_F(IpcClientProxyTest, SendRequest_001, testing::ext::TestSize.Level0)
293 {
294     // 1. set req nullptr
295     std::shared_ptr<IpcReq> req = nullptr;
296     // set rsp not nullptr
297     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
298     // set pcClientProxy ipcClientManager not null
299     std::shared_ptr<IpcClient> ipcClientManager = std::make_shared<IpcClientManager>();
300     // 2. call IpcClientProxy SendRequest
301     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
302     int32_t ret = ipcClientProxy->SendRequest(0, req, rsp);
303     // 3. check ret is DEVICEMANAGER_NULLPTR
304     ASSERT_EQ(ret, DM_POINT_NULL);
305 }
306 
307 /**
308  * @tc.name: SendRequest_002
309  * @tc.desc: 1. set req not nullptr
310  *              set rsp  nullptr
311  *              set IpcClientProxy ipcClientManager not null
312  *           2. call IpcClientProxy SendRequest
313  *           3. check ret is DM_POINT_NULL
314  * @tc.type: FUNC
315  * @tc.require: AR000GHSJK
316  */
317 HWTEST_F(IpcClientProxyTest, SendRequest_002, testing::ext::TestSize.Level0)
318 {
319     // 1. set req not nullptr
320     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
321     // set rsp nullptr
322     std::shared_ptr<IpcRsp> rsp = nullptr;
323     // set pcClientProxy ipcClientManager not null
324     std::shared_ptr<IpcClient> ipcClientManager = std::make_shared<IpcClientManager>();
325     // 2. call IpcClientProxy SendRequest
326     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
327     int32_t ret = ipcClientProxy->SendRequest(0, req, rsp);
328     // 3. check ret is DM_POINT_NULL
329     ASSERT_EQ(ret, DM_POINT_NULL);
330 }
331 
332 /**
333  * @tc.name: SendRequest_003
334  * @tc.desc: 1. set req not nullptr
335  *              set rsp not nullptr
336  *              set IpcClientProxy ipcClientManager null
337  *           2. call IpcClientProxy SendRequest
338  *           3. check ret is DM_POINT_NULL
339  * @tc.type: FUNC
340  * @tc.require: AR000GHSJK
341  */
342 HWTEST_F(IpcClientProxyTest, SendRequest_003, testing::ext::TestSize.Level0)
343 {
344     // 1. set req not nullptr
345     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
346     // set rsp not nullptr
347     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
348     // set pcClientProxy ipcClientManager  null
349     std::shared_ptr<IpcClient> ipcClientManager = nullptr;
350     // 2. call IpcClientProxy SendRequest
351     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
352     int32_t ret = ipcClientProxy->SendRequest(0, req, rsp);
353     // 3. check ret is DM_POINT_NULL
354     ASSERT_EQ(ret, DM_POINT_NULL);
355 }
356 
357 /**
358  * @tc.name: SendRequest_004
359  * @tc.desc: 1. set req not nullptr
360  *              set rsp  not nullptr
361  *           2. Mock IpcClient SendRequest return DM_FAILED
362  *           3. call IpcClientProxy SendRequest
363  *           4. check ret is DM_FAILED
364  * @tc.type: FUNC
365  * @tc.require: AR000GHSJK
366  */
367 HWTEST_F(IpcClientProxyTest, SendRequest_004, testing::ext::TestSize.Level0)
368 {
369     // 1. set req not nullptr
370     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
371     // set rsp  not nullptr
372     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
373     // 2. Mock IpcClient SendRequest return DM_FAILED
374     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
375     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
376     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
377                 .Times(1).WillOnce(testing::Return(DM_FAILED));
378     // 3. call IpcClientProxy SendRequest
379     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
380     int32_t ret = ipcClientProxy->SendRequest(0, req, rsp);
381     // 4. check ret is DM_FAILED
382     ASSERT_EQ(ret, DM_FAILED);
383 }
384 
385 /**
386  * @tc.name: SendRequest_004
387  * @tc.desc: 1. set req not nullptr
388  *              set rsp  not nullptr
389  *           2. Mock IpcClient SendRequest return DM_OK
390  *           3. call IpcClientProxy SendRequest
391  *           4. check ret is DM_OK
392  * @tc.type: FUNC
393  * @tc.require: AR000GHSJK
394  */
395 HWTEST_F(IpcClientProxyTest, SendRequest5, testing::ext::TestSize.Level0)
396 {
397     // 1. set req not nullptr
398     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
399     // set rsp  not nullptr
400     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
401     // 2. Mock IpcClient SendRequest return DM_FAILED
402     std::shared_ptr<MockIpcClient> mockInstance = std::make_shared<MockIpcClient>();
403     std::shared_ptr<IpcClient> ipcClientManager = mockInstance;
404     EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_))
405                 .Times(1).WillOnce(testing::Return(DM_OK));
406     // 3. call IpcClientProxy SendRequest
407     std::shared_ptr<IpcClientProxy> ipcClientProxy = std::make_shared<IpcClientProxy>(ipcClientManager);
408     int32_t ret = ipcClientProxy->SendRequest(0, req, rsp);
409     // 4. check ret is DM_OK
410     ASSERT_EQ(ret, DM_OK);
411 }
412 } // namespace
413 } // namespace DistributedHardware
414 } // namespace OHOS
415