• 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_server_client_proxy.h"
17 #include <unistd.h>
18 #include "dm_device_info.h"
19 #include "ipc_remote_broker.h"
20 #include "iremote_object.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "ipc_client_manager.h"
24 #include "ipc_set_useroperation_req.h"
25 #include "ipc_notify_device_state_req.h"
26 #include "ipc_rsp.h"
27 #include "ipc_notify_device_found_req.h"
28 #include "ipc_notify_discover_result_req.h"
29 #include "dm_constants.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
SetUp()33 void IpcServerClientProxyTest::SetUp()
34 {
35 }
36 
TearDown()37 void IpcServerClientProxyTest::TearDown()
38 {
39 }
40 
SetUpTestCase()41 void IpcServerClientProxyTest::SetUpTestCase()
42 {
43 }
44 
TearDownTestCase()45 void IpcServerClientProxyTest::TearDownTestCase()
46 {
47 }
48 
49 namespace {
50 /**
51  * @tc.name: SendCmd_001
52  * @tc.desc: 1. set cmdCode not null
53  *           2. set remoteObject nullptr
54  *           3. call IpcServerClientProxy SendCmd
55  *           4. check ret is DEVICEMANAGER_NULLPTR
56  * @tc.type: FUNC
57  * @tc.require: AR000GHSJK
58  */
59 HWTEST_F(IpcServerClientProxyTest, SendCmd_001, testing::ext::TestSize.Level0)
60 {
61     // 1. set cmdCode not null
62     int32_t cmdCode = 1;
63     // 2. set remoteObject nullptr
64     sptr<IRemoteObject> remoteObject = nullptr;
65     // 3. call IpcServerClientProxy SendCmd
66     auto instance = new IpcServerClientProxy(remoteObject);
67     int ret = instance->SendCmd(cmdCode, nullptr, nullptr);
68     // 4. check ret is DEVICEMANAGER_NULLPTR
69     ASSERT_EQ(ret, DM_POINT_NULL);
70 }
71 
72 /**
73  * @tc.name: SendCmd_002
74  * @tc.desc: 1. set cmdCode not null
75  *              set pkgName not null
76  *              set action not null
77  *           2. set remoteObject not nullptr
78  *              set req not null
79  *              set rsp not null
80  *           3. call IpcServerClientProxy SendCmd with parameter
81  *           4. check ret is DM_OK
82  * @tc.type: FUNC
83  * @tc.require: AR000GHSJK
84  */
85 HWTEST_F(IpcServerClientProxyTest, SendCmd_002, testing::ext::TestSize.Level0)
86 {
87     // 1. set cmdCode not null
88     int32_t cmdCode = SERVER_DEVICE_STATE_NOTIFY;
89     // set pkgName not null
90     std::string pkgName = "com.ohos.test";
91     // set action not null
92     int deviceState = 1;
93     DmDeviceInfo deviceInfo;
94     // 2. set remoteObject not nullptr
95     sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
96     IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
97     std::shared_ptr<IpcNotifyDeviceStateReq> req = std::make_shared<IpcNotifyDeviceStateReq>();
98     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
99     // set req not null
100     req->SetPkgName(pkgName);
101     // set rsp not null
102     req->SetDeviceState(deviceState);
103     req->SetDeviceInfo(deviceInfo);
104     // 3. call IpcServerClientProxy SendCmd with parameter
105     int ret = 0;
106     std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
107     ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
108     // 4. check ret is DM_OK
109     ASSERT_NE(ret, DM_FAILED);
110 }
111 
112 /**
113  * @tc.name: SendCmd_003
114  * @tc.desc: 1. set cmdCode not null
115  *              set pkgName not null
116  *              set action not null
117  *           2. set remoteObject not nullptr
118  *              set req not null
119  *              set rsp not null
120  *           3. call IpcServerClientProxy SendCmd with parameter
121  *           4. check ret is DM_OK
122  * @tc.type: FUNC
123  * @tc.require: AR000GHSJK
124  */
125 HWTEST_F(IpcServerClientProxyTest, SendCmd_003, testing::ext::TestSize.Level0)
126 {
127     // 1. set cmdCode not null
128     int32_t cmdCode = SERVER_DEVICE_FOUND;
129     // set pkgName not null
130     std::string pkgName = "com.ohos.test";
131     // set action not null
132     uint16_t subscribeId = 1;
133     DmDeviceInfo dmDeviceInfo;
134     // 2. set remoteObject not nullptr
135     sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
136     IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
137     std::shared_ptr<IpcNotifyDeviceFoundReq> req = std::make_shared<IpcNotifyDeviceFoundReq>();
138     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
139     // set req not null
140     req->SetPkgName(pkgName);
141     // set rsp not null
142     req->SetSubscribeId(subscribeId);
143     req->SetDeviceInfo(dmDeviceInfo);
144     // 3. call IpcServerClientProxy SendCmd with parameter
145     int ret = 0;
146     std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
147     ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
148     // 4. check ret is DM_OK
149     ASSERT_NE(ret, DM_FAILED);
150 }
151 
152 /**
153  * @tc.name: SendCmd_004
154  * @tc.desc: 1. set cmdCode not null
155  *              set pkgName not null
156  *              set action not null
157  *           2. set remoteObject not nullptr
158  *              set req not null
159  *              set rsp not null
160  *           3. call IpcServerClientProxy SendCmd with parameter
161  *           4. check ret is DM_OK
162  * @tc.type: FUNC
163  * @tc.require: AR000GHSJK
164  */
165 HWTEST_F(IpcServerClientProxyTest, SendCmd_004, testing::ext::TestSize.Level0)
166 {
167     // 1. set cmdCode not null
168     int32_t cmdCode = SERVER_DISCOVER_FINISH;
169     // set pkgName not null
170     std::string pkgName = "com.ohos.test";
171     // set action not null
172     uint16_t subscribeId = 1;
173     int32_t result = 1;
174     // 2. set remoteObject not nullptr
175     sptr<IpcClientStub> remoteObject = sptr<IpcClientStub>(new IpcClientStub());
176     IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject);
177     std::shared_ptr<IpcNotifyDiscoverResultReq> req = std::make_shared<IpcNotifyDiscoverResultReq>();
178     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
179     // set req not null
180     req->SetPkgName(pkgName);
181     // set rsp not null
182     req->SetSubscribeId(subscribeId);
183     req->SetResult(result);
184     // 3. call IpcServerClientProxy SendCmd with parameter
185     int ret = 0;
186     std::shared_ptr<IpcServerListener> ipcServerListener = std::make_shared<IpcServerListener>();
187     ret = ipcServerListener->SendRequest(cmdCode, req, rsp);
188     // 4. check ret is DM_OK
189     ASSERT_NE(ret, DM_FAILED);
190 }
191 } // namespace
192 } // namespace DistributedHardware
193 } // namespace OHOS
194