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