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