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_client_stub.h"
17
18 #include <memory>
19 #include <unistd.h>
20
21 #include "device_manager.h"
22 #include "device_manager_impl.h"
23 #include "device_manager_ipc_interface_code.h"
24 #include "dm_constants.h"
25 #include "dm_device_info.h"
26 #include "mock/mock_ipc_client_stub.h"
27 #include "ipc_remote_broker.h"
28 #include "iremote_object.h"
29 #include "iservice_registry.h"
30 #include "system_ability_definition.h"
31 #include "ipc_client_manager.h"
32 #include "ipc_set_useroperation_req.h"
33 #include "ipc_cmd_register.h"
34 #include "ipc_skeleton.h"
35 #include "ipc_types.h"
36 #include "ipc_rsp.h"
37 #include "ipc_def.h"
38
39 using namespace testing;
40 using namespace testing::ext;
41
42 namespace OHOS {
43 namespace DistributedHardware {
SetUp()44 void IpcClientStubTest::SetUp()
45 {
46 }
47
TearDown()48 void IpcClientStubTest::TearDown()
49 {
50 }
51
SetUpTestCase()52 void IpcClientStubTest::SetUpTestCase()
53 {
54 ipcCmdRegisterMock_ = std::make_shared<IpcCmdRegisterMock>();
55 DmIpcCmdRegister::dmIpcCmdRegister = ipcCmdRegisterMock_;
56 }
57
TearDownTestCase()58 void IpcClientStubTest::TearDownTestCase()
59 {
60 DmIpcCmdRegister::dmIpcCmdRegister = nullptr;
61 ipcCmdRegisterMock_ = nullptr;
62 }
63
64 namespace {
65 /**
66 * @tc.name: OnRemoteRequest_001
67 * @tc.desc: 1. set MessageOption not null
68 * set MessageParcel not null
69 * set MessageParcel not null
70 * 2. set set code is 999
71 * 3. call IpcClientStub OnRemoteRequest with parameter
72 * 4. check result is ERR_DM_FAILED
73 * @tc.type: FUNC
74 * @tc.require: AR000GHSJK
75 */
76 HWTEST_F(IpcClientStubTest, OnRemoteRequest_001, testing::ext::TestSize.Level0)
77 {
78 // 1. set MessageOption not null
79 MessageOption option;
80 // set MessageParcel not null
81 MessageParcel data;
82 // set MessageParcel not null
83 MessageParcel reply;
84 // 2. set set code is 999
85 int code = 999;
86 sptr<IpcClientStub> instance(new IpcClientStub());
87 // 3. call IpcClientStub OnRemoteRequest with parameter
88 int32_t result = instance->OnRemoteRequest(code, data, reply, option);
89 if (result != DM_OK) {
90 result = ERR_DM_FAILED;
91 }
92 // 4. check result is ERR_DM_FAILED
93 ASSERT_EQ(result, ERR_DM_FAILED);
94 }
95
96 /**
97 * @tc.name: OnRemoteRequest_002
98 * @tc.desc: 1. set MessageOption not null
99 * set MessageParcel not null
100 * set MessageParcel not null
101 * 2. set set code is SERVER_DEVICE_FA_NOTIFY
102 * 3. call IpcClientStub OnRemoteRequest with parameter
103 * 4. check result is ERR_DM_IPC_READ_FAILED
104 * @tc.type: FUNC
105 * @tc.require: AR000GHSJK
106 */
107 HWTEST_F(IpcClientStubTest, OnRemoteRequest_002, testing::ext::TestSize.Level0)
108 {
109 // 1. set MessageOption not null
110 MessageOption option;
111 // set MessageParcel not null
112 MessageParcel data;
113 // set MessageParcel not null
114 MessageParcel reply;
115 // 2. set set code is SERVER_DEVICE_FA_NOTIFY
116 int code = SERVER_DEVICE_FA_NOTIFY;
117 sptr<IpcClientStub> instance(new IpcClientStub());
118 // 3. call IpcClientStub OnRemoteRequest with parameter
119 int ret = instance->OnRemoteRequest(code, data, reply, option);
120 // 4. check result is ERR_DM_IPC_READ_FAILED
121 ASSERT_EQ(ret, ERR_DM_IPC_READ_FAILED);
122 }
123
124 /**
125 * @tc.name: OnRemoteRequest_003
126 * @tc.type: FUNC
127 */
128 HWTEST_F(IpcClientStubTest, OnRemoteRequest_003, testing::ext::TestSize.Level0)
129 {
130 MessageOption option;
131 MessageParcel data;
132 MessageParcel reply;
133 int code = GET_TRUST_DEVICE_LIST;
134 std::shared_ptr<MockIpcClientStub> mockInstance = std::make_shared<MockIpcClientStub>();
135 std::shared_ptr<IpcClientStub> ipcClientStub= mockInstance;
136 EXPECT_CALL(*mockInstance, OnRemoteRequest(testing::_, testing::_, testing::_, testing::_))
137 .Times(1).WillOnce(testing::Return(DM_OK));
138 std::shared_ptr<IpcClientStub> instance = std::shared_ptr<IpcClientStub>(ipcClientStub);
139 int ret = instance->OnRemoteRequest(code, data, reply, option);
140 ASSERT_EQ(ret, DM_OK);
141 }
142
143 /**
144 * @tc.name: SendCmd_001
145 * @tc.desc: 1. set set code is SERVER_DEVICE_FA_NOTIFY
146 * set req is nullptr
147 * set rsp is nullptr
148 * 2. call IpcClientStub SendCmd with parameter
149 * 3. check result is ERR_DM_IPC_SEND_REQUEST_FAILED
150 * @tc.type: FUNC
151 * @tc.require: AR000GHSJK
152 */
153 HWTEST_F(IpcClientStubTest, SendCmd_001, testing::ext::TestSize.Level0)
154 {
155 // 1. set set code is SERVER_DEVICE_FA_NOTIFY
156 int cmdCode = SERVER_DEVICE_FA_NOTIFY;
157 // set req is nullptr
158 std::shared_ptr<IpcReq> req = nullptr;
159 // set rsp is nullptr
160 std::shared_ptr<IpcRsp> rsp = nullptr;
161 sptr<IpcClientStub> instance(new IpcClientStub());
162 // 2. call IpcClientStub SendCmd with parameter
163 int ret = instance->SendCmd(cmdCode, req, rsp);
164 // 3. check result is ERR_DM_IPC_SEND_REQUEST_FAILED
165 ASSERT_EQ(ret, DM_OK);
166 }
167
168 /**
169 * @tc.name: SendCmd_002
170 * @tc.type: FUNC
171 */
172 HWTEST_F(IpcClientStubTest, SendCmd_002, testing::ext::TestSize.Level0)
173 {
174 int result = 305;
175 int cmdCode = IPC_MSG_BUTT;
176 std::shared_ptr<IpcReq> req = nullptr;
177 std::shared_ptr<IpcRsp> rsp = nullptr;
178 sptr<IpcClientStub> instance(new IpcClientStub());
179 int ret = instance->SendCmd(cmdCode, req, rsp);
180 ASSERT_EQ(ret, result);
181 }
182
183 /**
184 * @tc.name: SendCmd_003
185 * @tc.type: FUNC
186 */
187 HWTEST_F(IpcClientStubTest, SendCmd_003, testing::ext::TestSize.Level0)
188 {
189 int cmdCode = GET_TRUST_DEVICE_LIST;
190 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
191 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
192 std::shared_ptr<MockIpcClientStub> mockInstance = std::make_shared<MockIpcClientStub>();
193 std::shared_ptr<IpcClientStub> ipcClientStub= mockInstance;
194 EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_))
195 .Times(1).WillOnce(testing::Return(DM_OK));
196 std::shared_ptr<IpcClientStub> instance = std::shared_ptr<IpcClientStub>(ipcClientStub);
197 int ret = instance->SendCmd(cmdCode, req, rsp);
198 ASSERT_EQ(ret, DM_OK);
199 }
200
201 /**
202 * @tc.name: SendCmd_004
203 * @tc.type: FUNC
204 */
205 HWTEST_F(IpcClientStubTest, SendCmd_004, testing::ext::TestSize.Level0)
206 {
207 int result = 305;
208 int cmdCode = -1;
209 std::shared_ptr<IpcReq> req = nullptr;
210 std::shared_ptr<IpcRsp> rsp = nullptr;
211 sptr<IpcClientStub> instance = sptr<IpcClientStub>(new IpcClientStub());
212 int ret = instance->SendCmd(cmdCode, req, rsp);
213 ASSERT_EQ(ret, result);
214 }
215
216 HWTEST_F(IpcClientStubTest, SendCmd_005, testing::ext::TestSize.Level0)
217 {
218 int cmdCode = 1;
219 std::shared_ptr<IpcReq> req = nullptr;
220 std::shared_ptr<IpcRsp> rsp = nullptr;
221 sptr<IpcClientStub> instance = sptr<IpcClientStub>(new IpcClientStub());
222 EXPECT_CALL(*ipcCmdRegisterMock_, SetRequest(_, _, _)).WillOnce(Return(DM_OK));
223 EXPECT_CALL(*ipcCmdRegisterMock_, OnIpcCmd(_, _, _)).WillOnce(Return(DM_OK));
224 int ret = instance->SendCmd(cmdCode, req, rsp);
225 ASSERT_EQ(ret, DM_OK);
226
227 req = std::make_shared<IpcReq>();
228 rsp = std::make_shared<IpcRsp>();
229 EXPECT_CALL(*ipcCmdRegisterMock_, SetRequest(_, _, _)).WillOnce(Return(DM_OK));
230 EXPECT_CALL(*ipcCmdRegisterMock_, OnIpcCmd(_, _, _)).WillOnce(Return(ERR_DM_FAILED));
231 ret = instance->SendCmd(cmdCode, req, rsp);
232 ASSERT_EQ(ret, DM_OK);
233 }
234 } // namespace
235 } // namespace DistributedHardware
236 } // namespace OHOS
237