提交 eeff5fad 编写于 作者: L liubb_0516

add ipc test case

Signed-off-by: Nliubb_0516 <liubeibei8@huawei.com>
上级 323c6e55
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//foundation/communication/dsoftbus/dsoftbus.gni")
lite_component("ipc_test") {
features = [
"client:ipc_client",
"server:ipc_server",
"samgr:samgr",
]
}
\ No newline at end of file
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
SUBSYSTEM_DIR = "//foundation/communication/ipc/test"
IPC_CORE_ROOT = "//foundation/communication/ipc/ipc/native/c"
executable("ipc_client") {
sources = [
"$SUBSYSTEM_DIR/ipc/client/client.c",
]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//utils/native/lite/include",
"$IPC_CORE_ROOT/manager/include",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits",
"$SUBSYSTEM_DIR/ipc/include",
]
ldflags = [
"-lstdc++",
"-lpthread",
]
deps = [
"//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single"
]
configs -= [ "//build/lite/config:clang_opt" ]
}
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include "rpc_log.h"
#include "rpc_errno.h"
#include "ipc_skeleton.h"
#include "serializer.h"
#include "ipc_proxy.h"
static SvcIdentity g_serverSid;
int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
{
int32_t result = ERR_NONE;
RPC_LOG_INFO("client OnRemoteRequest called....");
int a = 0;
int b = 0;
switch (code) {
case CLIENT_OP_ADD: {
ReadInt32(data, &a);
ReadInt32(data, &b);
WriteInt32(reply, a + b);
break;
}
case CLIENT_OP_SUB: {
ReadInt32(data, &a);
ReadInt32(data, &b);
WriteInt32(reply, a - b);
break;
}
case CLIENT_OP_PRINT: {
size_t len;
char *str = (char *)ReadString(data, &len);
RPC_LOG_INFO("client pop string %{public}s....", str);
break;
}
default:
RPC_LOG_ERROR("unknown code %{public}d", code);
break;
}
return result;
}
void ServerDead1()
{
RPC_LOG_INFO("#### server dead callback11 called ... ");
}
void ServerDead2()
{
RPC_LOG_INFO("#### server dead callback22 called ... ");
}
void ServerDead3()
{
RPC_LOG_INFO("#### server dead callback33 called ... ");
}
static SvcIdentity g_samgr = {
.handle = 0
};
MessageOption g_option = TF_OP_SYNC;
static void GetServerOne()
{
IpcIo data1;
uint8_t tmpData1[IPC_MAX_SIZE];
IpcIoInit(&data1, tmpData1, IPC_MAX_SIZE, 0);
WriteInt32(&data1, SERVER_SA_ID1);
IpcIo reply1;
uintptr_t ptr = 0;
int ret = SendRequest(g_samgr, GET_SYSTEM_ABILITY_TRANSACTION, &data1, &reply1, g_option, &ptr);
ReadRemoteObject(&reply1, &g_serverSid);
FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE);
}
static void CallServerAdd()
{
IpcIo data2;
uint8_t tmpData2[IPC_MAX_SIZE];
IpcIoInit(&data2, tmpData2, IPC_MAX_SIZE, 0);
WriteInt32(&data2, OP_A);
WriteInt32(&data2, OP_B);
IpcIo reply2;
uintptr_t ptr2 = 0;
int ret = SendRequest(g_serverSid, SERVER_OP_ADD, &data2, &reply2, g_option, &ptr2);
int res;
ReadInt32(&reply2, &res);
RPC_LOG_INFO(" 12 + 17 = %{public}d", res);
FreeBuffer((void *)ptr2);
EXPECT_EQ(ret, ERR_NONE);
int tmpSum = OP_A + OP_B;
EXPECT_EQ(res, tmpSum);
}
static void AnonymousTest()
{
IpcObjectStub objectStub = {
.func = RemoteRequest,
.isRemote = false
};
SvcIdentity svc = {
.handle = -1,
.token = (uintptr_t)&objectStub,
.cookie = (uintptr_t)&objectStub
};
IpcIo anonymous;
uint8_t anonymousData[IPC_MAX_SIZE];
IpcIoInit(&anonymous, anonymousData, IPC_MAX_SIZE, 1);
WriteRemoteObject(&anonymous, &svc);
IpcIo anonymousreply;
uintptr_t anonymousptr = 0;
int ret = SendRequest(g_serverSid, SERVER_OP_ADD_SERVICE, &anonymous, &anonymousreply, g_option, &anonymousptr);
int res = -1;
ReadInt32(&anonymousreply, &res);
RPC_LOG_INFO("add self to server = %{public}d", res);
FreeBuffer((void *)anonymousptr);
EXPECT_EQ(ret, ERR_NONE);
EXPECT_EQ(res, ERR_NONE);
}
static void DeathCallbackTest()
{
uint32_t cbId1 = -1;
uint32_t cbId2 = -1;
uint32_t cbId3 = -1;
uint32_t cbId4 = -1;
uint32_t cbId5 = -1;
RPC_LOG_INFO("============= test case for add death callback ============");
int ret = AddDeathRecipient(g_serverSid, ServerDead1, NULL, &cbId1);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(g_serverSid, ServerDead2, NULL, &cbId2);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(g_serverSid, ServerDead3, NULL, &cbId3);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(g_serverSid, ServerDead3, NULL, &cbId4);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(g_serverSid, ServerDead3, NULL, &cbId5); // failed
EXPECT_EQ(ret, ERR_INVALID_PARAM);
RPC_LOG_INFO("============= test case for remove death callback ============");
ret = RemoveDeathRecipient(g_serverSid, cbId2);
EXPECT_EQ(ret, ERR_NONE);
ret = RemoveDeathRecipient(g_serverSid, cbId4);
EXPECT_EQ(ret, ERR_NONE);
ret = RemoveDeathRecipient(g_serverSid, cbId1);
EXPECT_EQ(ret, ERR_NONE);
ret = RemoveDeathRecipient(g_serverSid, cbId3);
EXPECT_EQ(ret, ERR_NONE);
int handleOld = g_serverSid.handle;
g_serverSid.handle = 17;
ret = AddDeathRecipient(g_serverSid, ServerDead3, NULL, &cbId5); // failed
EXPECT_EQ(ret, ERR_INVALID_PARAM);
ret = RemoveDeathRecipient(g_serverSid, cbId3); // failed
EXPECT_EQ(ret, ERR_INVALID_PARAM);
g_serverSid.handle = handleOld;
ret = AddDeathRecipient(g_serverSid, ServerDead1, NULL, &cbId1);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(g_serverSid, ServerDead2, NULL, &cbId2);
EXPECT_EQ(ret, ERR_NONE);
}
int main()
{
RPC_LOG_INFO("Enter System Ability Client .... ");
GetServerOne();
CallServerAdd();
DeathCallbackTest();
JoinWorkThread();
return -1;
}
\ No newline at end of file
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IPC_PROXY_H
#define OHOS_IPC_PROXY_H
enum {
GET_SYSTEM_ABILITY_TRANSACTION = 1,
ADD_SYSTEM_ABILITY_TRANSACTION = 2,
};
enum {
CLIENT_OP_ADD = 1,
CLIENT_OP_SUB = 2,
CLIENT_OP_PRINT = 3,
};
enum {
SERVER_OP_ADD = 1,
SERVER_OP_SUB = 2,
SERVER_OP_MULTI = 3,
SERVER_OP_ADD_SERVICE = 4,
};
#define IPC_MAX_SIZE 128
enum {
SERVER_SA_ID1 = 15,
SERVER_SA_ID2 = 18,
};
#define EXPECT_EQ(a, b) \
if ((a) != (b)) { \
printf("FAILED:Expected equality of these values: %d:%d\n", (a), (b)); \
} else { \
printf("SUCCESS:test ok.\n"); \
}
#define IPC_TEST_TIME_INTERVAL 120
#define OP_A 12
#define OP_B 17
#endif // OHOS_IPC_PROXY_H
\ No newline at end of file
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
SUBSYSTEM_DIR = "//foundation/communication/ipc/test"
IPC_CORE_ROOT = "//foundation/communication/ipc/ipc/native/c"
executable("samgr") {
sources = [
"$SUBSYSTEM_DIR/ipc/samgr/samgr.c",
]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//utils/native/lite/include",
"$IPC_CORE_ROOT/manager/include",
"$IPC_CORE_ROOT/ipc/include",
"//foundation/communication/ipc/services/dbinder/c/include",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits",
"$SUBSYSTEM_DIR/ipc/include",
]
ldflags = [
"-lstdc++",
"-lpthread",
]
deps = [
"//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single",
]
configs -= [ "//build/lite/config:clang_opt" ]
}
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <string.h>
#include "rpc_log.h"
#include "rpc_errno.h"
#include "ipc_skeleton.h"
#include "ipc_proxy.h"
#include "serializer.h"
#include "utils_list.h"
typedef struct {
UTILS_DL_LIST list;
int32_t saId;
SvcIdentity *sid;
} SvcInfo;
static UTILS_DL_LIST *g_saList = NULL;
int32_t AddSystemAbility(int32_t saId, SvcIdentity *sid)
{
if (g_saList == NULL) {
return ERR_FAILED;
}
SvcInfo* node = (SvcInfo *)calloc(1, sizeof(SvcInfo));
node->saId = saId;
node->sid = sid;
UtilsListAdd(g_saList, &node->list);
return ERR_NONE;
}
int32_t GetSystemAbility(int32_t saId, const char* deviceId, SvcIdentity *sid)
{
SvcInfo* node = NULL;
SvcInfo* next = NULL;
UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(node, next, g_saList, SvcInfo, list)
{
if (node->saId == saId) {
sid->handle = node->sid->handle;
sid->token = node->sid->token;
sid->cookie = node->sid->cookie;
return ERR_NONE;
}
}
return ERR_FAILED;
}
int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
{
int32_t result = ERR_NONE;
RPC_LOG_INFO("OnRemoteRequest called.... code = %{public}d", code);
switch (code) {
case ADD_SYSTEM_ABILITY_TRANSACTION: {
int32_t saId;
ReadInt32(data, &saId);
SvcIdentity *sid = (SvcIdentity *)malloc(sizeof(SvcIdentity));
ReadRemoteObject(data, sid);
result = AddSystemAbility(saId, sid);
break;
}
case GET_SYSTEM_ABILITY_TRANSACTION: {
int32_t saId;
ReadInt32(data, &saId);
SvcIdentity sid;
result = GetSystemAbility(saId, "", &sid);
if (result != ERR_NONE) {
return result;
}
WriteRemoteObject(reply, &sid);
break;
}
default:
RPC_LOG_ERROR("unknown code %{public}d", code);
break;
}
return result;
}
int main(int argc, char *argv[])
{
RPC_LOG_INFO("Enter System Ability Manager .... ");
g_saList = (UTILS_DL_LIST *)calloc(1, sizeof(UTILS_DL_LIST));
UtilsListInit(g_saList);
IpcObjectStub objectStub = {
.func = RemoteRequest,
.isRemote = false
};
SvcIdentity target = {
.handle = 0,
.cookie = (uintptr_t)&objectStub
};
if (SetContextObject(target) != ERR_NONE) {
RPC_LOG_ERROR("SAMGR register samgr failed");
return -1;
}
JoinWorkThread();
return -1;
}
\ No newline at end of file
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
SUBSYSTEM_DIR = "//foundation/communication/ipc/test"
IPC_CORE_ROOT = "//foundation/communication/ipc/ipc/native/c"
executable("ipc_server") {
sources = [
"$SUBSYSTEM_DIR/ipc/server/server.c",
]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//utils/native/lite/include",
"$IPC_CORE_ROOT/manager/include",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits",
"$SUBSYSTEM_DIR/ipc/include",
]
ldflags = [
"-lstdc++",
"-lpthread",
]
deps = [
"//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single"
]
configs -= [ "//build/lite/config:clang_opt" ]
}
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <pthread.h>
#include "rpc_log.h"
#include "rpc_errno.h"
#include "ipc_skeleton.h"
#include "serializer.h"
#include <unistd.h>
#include "ipc_proxy.h"
static SvcIdentity *sid = NULL;
static SvcIdentity g_samgr = {
.handle = 0
};
static void CallAnonymosFunc(const char *str)
{
if (sid == NULL) {
RPC_LOG_INFO("invalid anonymous client");
return;
}
RPC_LOG_INFO("now server call client anonymous func");
IpcIo data;
uint8_t tmpData1[IPC_MAX_SIZE];
IpcIoInit(&data, tmpData1, IPC_MAX_SIZE, 1);
WriteString(&data, str);
IpcIo reply;
MessageOption option = TF_OP_ASYNC;
SendRequest(*sid, CLIENT_OP_PRINT, &data, &reply, option, NULL);
}
int32_t RemoteRequestOne(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
{
int32_t result = ERR_NONE;
RPC_LOG_INFO("server OnRemoteRequestOne called....");
int a = 0;
int b = 0;
switch (code) {
case SERVER_OP_ADD: {
ReadInt32(data, &a);
ReadInt32(data, &b);
WriteInt32(reply, a + b);
break;
}
case SERVER_OP_SUB: {
ReadInt32(data, &a);
ReadInt32(data, &b);
WriteInt32(reply, a - b);
break;
}
case SERVER_OP_MULTI: {
ReadInt32(data, &a);
ReadInt32(data, &b);
WriteInt32(reply, a * b);
break;
}
case SERVER_OP_ADD_SERVICE: {
sid = (SvcIdentity *)malloc(sizeof(SvcIdentity));
ReadRemoteObject(data, sid);
const char *str = "server call anonymos service one.";
CallAnonymosFunc(str);
break;
}
default:
RPC_LOG_ERROR("unknown code %{public}d", code);
break;
}
return result;
}
int32_t RemoteRequestTwo(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
{
int32_t result = ERR_NONE;
RPC_LOG_INFO("server OnRemoteRequestTwo called....");
int a = 0;
int b = 0;
switch (code) {
case SERVER_OP_ADD: {
ReadInt32(data, &a);
ReadInt32(data, &b);
WriteInt32(reply, a + b);
break;
}
case SERVER_OP_SUB: {
ReadInt32(data, &a);
ReadInt32(data, &b);
WriteInt32(reply, a - b);
break;
}
case SERVER_OP_MULTI: {
ReadInt32(data, &a);
ReadInt32(data, &b);
WriteInt32(reply, a * b);
break;
}
default:
RPC_LOG_ERROR("unknown code %{public}d", code);
break;
}
return result;
}
static void *ThreadHandler()
{
sleep(IPC_TEST_TIME_INTERVAL); // sleep 2 min
const char *str = "server call anonymos service new thread.";
CallAnonymosFunc(str);
return NULL;
}
MessageOption g_option = TF_OP_SYNC;
static IpcObjectStub objectStubOne = {
.func = RemoteRequestOne,
.isRemote = false
};
static IpcObjectStub objectStubTwo = {
.func = RemoteRequestTwo,
.isRemote = false
};
static SvcIdentity svcOne = {
.handle = -1,
.token = (uintptr_t)&objectStubOne,
.cookie = (uintptr_t)&objectStubOne
};
static SvcIdentity svcTwo = {
.handle = -1,
.token = (uintptr_t)&objectStubTwo,
.cookie = (uintptr_t)&objectStubTwo
};
static void AddSaOne()
{
IpcIo data;
uint8_t tmpData1[IPC_MAX_SIZE];
IpcIoInit(&data, tmpData1, IPC_MAX_SIZE, 1);
WriteInt32(&data, SERVER_SA_ID1);
WriteRemoteObject(&data, &svcOne);
IpcIo reply;
uintptr_t ptr = 0;
RPC_LOG_INFO("====== add ability one to samgr ======");
int ret = SendRequest(g_samgr, ADD_SYSTEM_ABILITY_TRANSACTION, &data, &reply, g_option, &ptr);
int res = -1;
ReadInt32(&reply, &res);
FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE);
EXPECT_EQ(res, ERR_NONE);
sleep(2);
}
static void AddSaTwo()
{
IpcIo dataTwo;
uint8_t tmpData2[IPC_MAX_SIZE];
IpcIoInit(&dataTwo, tmpData2, IPC_MAX_SIZE, 1);
WriteInt32(&dataTwo, SERVER_SA_ID2);
WriteRemoteObject(&dataTwo, &svcTwo);
IpcIo reply;
uintptr_t ptr = 0;
RPC_LOG_INFO("====== add ability two to samgr ======");
int ret = SendRequest(g_samgr, ADD_SYSTEM_ABILITY_TRANSACTION, &dataTwo, &reply, g_option, &ptr);
int res = -1;
ReadInt32(&reply, &res);
FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE);
EXPECT_EQ(res, ERR_NONE);
sleep(2);
}
int main()
{
RPC_LOG_INFO("Enter System Ability Server .... ");
AddSaOne();
AddSaTwo();
IpcIo reply;
uintptr_t ptr = 0;
RPC_LOG_INFO("====== get ability one from samgr ======");
IpcIo data1;
uint8_t dataGet[IPC_MAX_SIZE];
IpcIoInit(&data1, dataGet, IPC_MAX_SIZE, 0);
WriteInt32(&data1, SERVER_SA_ID1);
int ret = SendRequest(g_samgr, GET_SYSTEM_ABILITY_TRANSACTION, &data1, &reply, g_option, &ptr);
SvcIdentity sidOne;
ReadRemoteObject(&reply, &sidOne);
FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE);
sleep(2);
RPC_LOG_INFO("====== call serverone OP_MULTI ======");
IpcIo data2;
uint8_t dataMulti[IPC_MAX_SIZE];
IpcIoInit(&data2, dataMulti, IPC_MAX_SIZE, 0);
WriteInt32(&data2, OP_A);
WriteInt32(&data2, OP_B);
ret = SendRequest(sidOne, SERVER_OP_MULTI, &data2, &reply, g_option, &ptr);
int res = -1;
ReadInt32(&reply, &res);
RPC_LOG_INFO(" 12 * 17 = %{public}d", res);
FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE);
int tmpMul = OP_A * OP_B;
EXPECT_EQ(res, tmpMul);
pthread_t pid;
ret = pthread_create(&pid, NULL, ThreadHandler, NULL);
pthread_detach(pid);
JoinWorkThread();
return -1;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册