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 "gtest/gtest.h"
17 #include "rpc_test.h"
18
19 #include <cstring>
20 #include <pthread.h>
21 #include <iostream>
22
23 #include "rpc_log.h"
24 #include "rpc_errno.h"
25 #include "rpc_trans.h"
26 #include "dbinder_service.h"
27 #include "ipc_skeleton.h"
28 #include "ipc_process_skeleton.h"
29 #include "rpc_process_skeleton.h"
30 #include "serializer.h"
31
32 namespace {
ServerDead1(void * args)33 void ServerDead1(void *args)
34 {
35 RPC_LOG_INFO("#### rpc server dead callback11 called");
36 }
37
38 MessageOption option = {
39 .flags = TF_OP_SYNC,
40 .waitTime = RPC_DEFAULT_SEND_WAIT_TIME
41 };
42 SvcIdentity sid;
43 char deviceId[DEVICEID_LENGTH];
44 uint32_t cbId1 = 0;
45 }
46
47 using namespace testing::ext;
48 using namespace std;
49
50 namespace OHOS {
51 class RpcClientTest : public testing::Test {
52 public:
SetUpTestCase()53 static void SetUpTestCase()
54 {
55 cout << "输入对端设备IP地址: ";
56 cin >> deviceId;
57 RPC_LOG_INFO("input deviceid is %s", deviceId);
58 }
TearDownTestCase()59 static void TearDownTestCase() {}
SetUp()60 void SetUp() {}
TearDown()61 void TearDown()
62 {
63 sleep(1);
64 }
65 };
66
67 /**
68 * @tc.name: RPC_SendRequestNum_06
69 * @tc.desc: proxy get stub failed
70 * @tc.type: FUNC
71 * @tc.require:
72 */
73 HWTEST_F(RpcClientTest, RpcClientTest001, TestSize.Level0)
74 {
75 IpcIo data1;
76 uint8_t tmpData1[IPC_LENGTH];
77 IpcIoInit(&data1, tmpData1, IPC_LENGTH, 0);
78 WriteInt32(&data1, INVALID_SAID);
79 WriteString(&data1, deviceId);
80
81 IpcIo reply1;
82 SvcIdentity target = {
83 .handle = 0
84 };
85
86 RPC_LOG_INFO("get remote system ability from samgr.");
87 uintptr_t ptr = 0;
88 int32_t ret = SendRequest(target, GET_REMOTE_SYSTEM_ABILITY_TRANSACTION, &data1, &reply1, option, &ptr);
89 EXPECT_EQ(ret, ERR_NONE);
90 ReadInt32(&reply1, &ret);
91 EXPECT_EQ(ret, ERR_FAILED);
92 FreeBuffer((void *)ptr);
93 }
94
95 /**
96 * @tc.name: RPC_SendRequestNum_05
97 * @tc.desc: proxy get stub success
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 HWTEST_F(RpcClientTest, RpcClientTest002, TestSize.Level1)
102 {
103 IpcIo data1;
104 uint8_t tmpData1[IPC_LENGTH];
105 IpcIoInit(&data1, tmpData1, IPC_LENGTH, 0);
106 WriteInt32(&data1, SAID);
107 WriteString(&data1, deviceId);
108
109 IpcIo reply1;
110 SvcIdentity target = {
111 .handle = 0
112 };
113
114 RPC_LOG_INFO("get remote system ability from samgr.");
115 uintptr_t ptr = 0;
116 int32_t ret = SendRequest(target, GET_REMOTE_SYSTEM_ABILITY_TRANSACTION, &data1, &reply1, option, &ptr);
117 EXPECT_EQ(ret, ERR_NONE);
118
119 ReadRemoteObject(&reply1, &sid);
120 RPC_LOG_INFO("call server add func server handle = %d.", sid.handle);
121 EXPECT_NE(sid.handle, INVALID_HANDLE);
122 FreeBuffer((void *)ptr);
123 }
124
125 /**
126 * @tc.name: RPC_SendRequestNum_08
127 * @tc.desc: proxy sendrequest failed
128 * @tc.type: FUNC
129 * @tc.require:
130 */
131 HWTEST_F(RpcClientTest, RpcClientTest003, TestSize.Level1)
132 {
133 IpcIo data2;
134 uint8_t tmpData2[IPC_LENGTH];
135 IpcIoInit(&data2, tmpData2, IPC_LENGTH, 0);
136 WriteInt32(&data2, NUMBER_A);
137 WriteInt32(&data2, NUMBER_B);
138
139 IpcIo reply2;
140 uintptr_t ptr2 = 0;
141 int32_t oldHandle = sid.handle;
142 sid.handle = INVALID_HANDLE;
143 int32_t ret = SendRequest(sid, OP_ADD, &data2, &reply2, option, &ptr2);
144 EXPECT_EQ(ret, ERR_FAILED);
145 RPC_LOG_ERROR("SendRequest OP_ADD failed, error = %d", ret);
146 FreeBuffer((void *)ptr2);
147
148 sid.handle = oldHandle;
149 EXPECT_EQ(sid.handle, oldHandle);
150 }
151
152 /**
153 * @tc.name: RPC_SendRequestNum_07
154 * @tc.desc: proxy sendrequest success
155 * @tc.type: FUNC
156 * @tc.require:
157 */
158 HWTEST_F(RpcClientTest, RpcClientTest004, TestSize.Level0)
159 {
160 IpcIo data2;
161 uint8_t tmpData2[IPC_LENGTH];
162 IpcIoInit(&data2, tmpData2, IPC_LENGTH, 0);
163 WriteInt32(&data2, NUMBER_A);
164 WriteInt32(&data2, NUMBER_B);
165
166 IpcIo reply2;
167 uintptr_t ptr2 = 0;
168 int32_t ret = SendRequest(sid, OP_ADD, &data2, &reply2, option, &ptr2);
169 EXPECT_EQ(ret, ERR_NONE);
170 if (ret != ERR_NONE) {
171 RPC_LOG_ERROR("SendRequest OP_ADD failed, error = %d", ret);
172 FreeBuffer((void *)ptr2);
173 return;
174 }
175
176 int32_t sum;
177 ReadInt32(&reply2, &sum);
178 RPC_LOG_INFO("%d + %d = %d", NUMBER_A, NUMBER_B, sum);
179 EXPECT_EQ(sum, NUMBER_A + NUMBER_B);
180 FreeBuffer((void *)ptr2);
181 }
182
183 /**
184 * @tc.name: RPC_SendRequestNum_09
185 * @tc.desc: stub process success
186 * @tc.type: FUNC
187 * @tc.require:
188 */
189 HWTEST_F(RpcClientTest, RpcClientTest005, TestSize.Level0)
190 {
191 IpcIo data3;
192 uint8_t tmpData3[IPC_LENGTH];
193 IpcIoInit(&data3, tmpData3, IPC_LENGTH, 0);
194 WriteInt32(&data3, NUMBER_A);
195 WriteInt32(&data3, NUMBER_B);
196
197 IpcIo reply3;
198 uintptr_t ptr3 = 0;
199 int32_t ret = SendRequest(sid, OP_MULTI, &data3, &reply3, option, &ptr3);
200 EXPECT_EQ(ret, ERR_NONE);
201 if (ret != ERR_NONE) {
202 RPC_LOG_ERROR("SendRequest OP_MULTI failed, error = %d", ret);
203 FreeBuffer((void *)ptr3);
204 return;
205 }
206
207 int32_t mutil;
208 ReadInt32(&reply3, &mutil);
209 RPC_LOG_INFO("%d * %d = %d", NUMBER_A, NUMBER_B, mutil);
210 EXPECT_EQ(mutil, NUMBER_A * NUMBER_B);
211 FreeBuffer((void *)ptr3);
212 }
213
214 /**
215 * @tc.name: RPC_SendRequestNum_10
216 * @tc.desc: stub process failed
217 * @tc.type: FUNC
218 * @tc.require:
219 */
220 HWTEST_F(RpcClientTest, RpcClientTest006, TestSize.Level1)
221 {
222 IpcIo data3;
223 uint8_t tmpData3[IPC_LENGTH];
224 IpcIoInit(&data3, tmpData3, IPC_LENGTH, 0);
225 WriteInt32(&data3, NUMBER_A);
226 WriteInt32(&data3, NUMBER_ZERO);
227
228 IpcIo reply3;
229 uintptr_t ptr3 = 0;
230 int32_t ret = SendRequest(sid, OP_DIVISION, &data3, &reply3, option, &ptr3);
231 EXPECT_EQ(ret, ERR_NONE);
232 ReadInt32(&reply3, &ret);
233 EXPECT_EQ(ret, ERR_FAILED);
234 FreeBuffer((void *)ptr3);
235 }
236
237 /**
238 * @tc.name: RPC_SendRequestNum_11
239 * @tc.desc: proxy process success
240 * @tc.type: FUNC
241 * @tc.require:
242 */
243 HWTEST_F(RpcClientTest, RpcClientTest007, TestSize.Level0)
244 {
245 IpcIo data;
246 uint8_t tmpData[IPC_LENGTH];
247 IpcIoInit(&data, tmpData, IPC_LENGTH, 0);
248 WriteInt32(&data, NUMBER_A);
249 WriteInt32(&data, NUMBER_B);
250
251 IpcIo reply;
252 uintptr_t ptr3 = 0;
253 int32_t ret = SendRequest(sid, OP_DIVISION, &data, &reply, option, &ptr3);
254 EXPECT_EQ(ret, ERR_NONE);
255 if (ret != ERR_NONE) {
256 RPC_LOG_ERROR("SendRequest OP_DIVISION failed, error = %d", ret);
257 FreeBuffer((void *)ptr3);
258 return;
259 }
260
261 int32_t division;
262 ReadInt32(&reply, &division);
263 RPC_LOG_INFO("%d / %d = %d", NUMBER_A, NUMBER_B, division);
264 EXPECT_EQ(division, NUMBER_A / NUMBER_B);
265 FreeBuffer((void *)ptr3);
266 }
267
268 /**
269 * @tc.name: RPC_SendRequestNum_12
270 * @tc.desc: proxy sendrequest oneway
271 * @tc.type: FUNC
272 * @tc.require:
273 */
274 HWTEST_F(RpcClientTest, RpcClientTest008, TestSize.Level0)
275 {
276 IpcIo data;
277 uint8_t tmpData[IPC_LENGTH];
278 IpcIoInit(&data, tmpData, IPC_LENGTH, 0);
279 WriteInt32(&data, NUMBER_A);
280 WriteInt32(&data, NUMBER_B);
281
282 IpcIo reply;
283 uintptr_t ptr = 0;
284 MessageOption oneWayOption = {
285 .flags = TF_OP_ASYNC
286 };
287 int32_t ret = SendRequest(sid, OP_DIVISION, &data, &reply, oneWayOption, &ptr);
288 EXPECT_EQ(ret, ERR_NONE);
289 FreeBuffer((void *)ptr);
290 }
291
292 /**
293 * @tc.name: RPC_SendRequestNum_14
294 * @tc.desc: update proto
295 * @tc.type: FUNC
296 * @tc.require:
297 */
298 HWTEST_F(RpcClientTest, RpcClientTest009, TestSize.Level1)
299 {
300 UpdateProtoIfNeed(&sid);
301 ThreadContext *threadContext = GetCurrentThreadContext();
302 ASSERT_TRUE(threadContext != NULL);
303 EXPECT_EQ(threadContext->proto, IF_PROT_DATABUS);
304 }
305
306 /**
307 * @tc.name: RPC_SerializerNum_01
308 * @tc.desc: data serializer
309 * @tc.type: FUNC
310 * @tc.require:
311 */
312 HWTEST_F(RpcClientTest, RpcClientTest010, TestSize.Level0)
313 {
314 IpcIo data;
315 uint8_t tmpData[IPC_LENGTH];
316 IpcIoInit(&data, tmpData, IPC_LENGTH, 0);
317 WriteInt32(&data, NUMBER_A);
318 WriteInt32(&data, NUMBER_B);
319
320 IpcIo reply;
321 uintptr_t ptr = 0;
322 int32_t ret = SendRequest(sid, OP_SERIALIZER, &data, &reply, option, &ptr);
323 EXPECT_EQ(ret, ERR_NONE);
324 ReadInt32(&reply, &ret);
325 EXPECT_EQ(ret, ERR_NONE);
326 FreeBuffer((void *)ptr);
327 }
328
329 /**
330 * @tc.name: RPC_SerializerNum_08
331 * @tc.desc: query proxy session
332 * @tc.type: FUNC
333 * @tc.require:
334 */
335 HWTEST_F(RpcClientTest, RpcClientTest011, TestSize.Level1)
336 {
337 HandleSessionList *session = QueryProxySession(sid.handle);
338 EXPECT_TRUE(session != NULL);
339 EXPECT_GE(session->sessionId, 0);
340 }
341
342 /**
343 * @tc.name: RPC_SessionNum_08
344 * @tc.desc: proxy send normal code
345 * @tc.type: FUNC
346 * @tc.require:
347 */
348 HWTEST_F(RpcClientTest, RpcClientTest012, TestSize.Level1)
349 {
350 IpcIo data;
351 uint8_t tmpData[IPC_LENGTH];
352 IpcIoInit(&data, tmpData, IPC_LENGTH, 0);
353 WriteInt32(&data, NUMBER_A);
354 WriteInt32(&data, NUMBER_B);
355
356 IpcIo reply;
357 uintptr_t ptr = 0;
358 int32_t ret = SendRequest(sid, OP_MULTI, &data, &reply, option, &ptr);
359 EXPECT_EQ(ret, ERR_NONE);
360 FreeBuffer((void *)ptr);
361 }
362
363 /**
364 * @tc.name: RPC_SessionNum_09
365 * @tc.desc: proxy send invalid code
366 * @tc.type: FUNC
367 * @tc.require:
368 */
369 HWTEST_F(RpcClientTest, RpcClientTest013, TestSize.Level1)
370 {
371 IpcIo data;
372 uint8_t tmpData[IPC_LENGTH];
373 IpcIoInit(&data, tmpData, IPC_LENGTH, 0);
374 WriteInt32(&data, NUMBER_A);
375 WriteInt32(&data, NUMBER_B);
376
377 IpcIo reply;
378 uintptr_t ptr = 0;
379 int32_t ret = SendRequest(sid, OP_INVALID, &data, &reply, option, &ptr);
380 EXPECT_EQ(ret, ERR_NONE);
381 ReadInt32(&reply, &ret);
382 EXPECT_EQ(ret, ERR_INVALID_PARAM);
383 FreeBuffer((void *)ptr);
384 }
385
386 /**
387 * @tc.name: RPC_DeathRecipient_01
388 * @tc.desc: proxy add death recipient
389 * @tc.type: FUNC
390 * @tc.require:
391 */
392 HWTEST_F(RpcClientTest, RpcClientTest014, TestSize.Level0)
393 {
394 int32_t ret = AddDeathRecipient(sid, ServerDead1, nullptr, &cbId1);
395 RPC_LOG_INFO("add death callback cbid1 = %d", ret);
396 EXPECT_EQ(ret, ERR_NONE);
397 }
398
399 /**
400 * @tc.name: RPC_DeathRecipient_06
401 * @tc.desc: proxy remote death recipient
402 * @tc.type: FUNC
403 * @tc.require:
404 */
405 HWTEST_F(RpcClientTest, RpcClientTest015, TestSize.Level0)
406 {
407 int32_t ret = RemoveDeathRecipient(sid, cbId1);
408 RPC_LOG_INFO("add death callback cbid1 = %d", ret);
409 EXPECT_EQ(ret, ERR_NONE);
410 }
411
412 /**
413 * @tc.name: RPC_SessionNum_10
414 * @tc.desc: stub disconnectted
415 * @tc.type: FUNC
416 * @tc.require:
417 */
418 HWTEST_F(RpcClientTest, RpcClientTest016, TestSize.Level1)
419 {
420 int32_t ret = AddDeathRecipient(sid, ServerDead1, nullptr, &cbId1);
421 RPC_LOG_INFO("add death callback cbid1 = %d", ret);
422 EXPECT_EQ(ret, ERR_NONE);
423 JoinWorkThread();
424 }
425 } // namespace OHOS