提交 a73b9d4f 编写于 作者: L liubb_0516

add ipc gtest test case

Signed-off-by: Nliubb_0516 <liubeibei8@huawei.com>
上级 f562d553
...@@ -11,7 +11,8 @@ ...@@ -11,7 +11,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import("//foundation/communication/dsoftbus/dsoftbus.gni") import("//build/lite/config/component/lite_component.gni")
import("//build/lite/config/test.gni")
lite_component("ipc_test") { lite_component("ipc_test") {
features = [ features = [
......
...@@ -46,11 +46,11 @@ int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption op ...@@ -46,11 +46,11 @@ int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption op
case CLIENT_OP_PRINT: { case CLIENT_OP_PRINT: {
size_t len; size_t len;
char *str = (char *)ReadString(data, &len); char *str = (char *)ReadString(data, &len);
RPC_LOG_INFO("client pop string %{public}s....", str); RPC_LOG_INFO("client pop string %s....", str);
break; break;
} }
default: default:
RPC_LOG_ERROR("unknown code %{public}d", code); RPC_LOG_ERROR("unknown code %d", code);
break; break;
} }
return result; return result;
...@@ -104,7 +104,7 @@ static void CallServerAdd(void) ...@@ -104,7 +104,7 @@ static void CallServerAdd(void)
int ret = SendRequest(g_serverSid, SERVER_OP_ADD, &data2, &reply2, g_option, &ptr2); int ret = SendRequest(g_serverSid, SERVER_OP_ADD, &data2, &reply2, g_option, &ptr2);
int res; int res;
ReadInt32(&reply2, &res); ReadInt32(&reply2, &res);
RPC_LOG_INFO(" 12 + 17 = %{public}d", res); RPC_LOG_INFO(" 12 + 17 = %d", res);
FreeBuffer((void *)ptr2); FreeBuffer((void *)ptr2);
EXPECT_EQ(ret, ERR_NONE); EXPECT_EQ(ret, ERR_NONE);
int tmpSum = OP_A + OP_B; int tmpSum = OP_A + OP_B;
...@@ -134,7 +134,7 @@ static void AnonymousTest(void) ...@@ -134,7 +134,7 @@ static void AnonymousTest(void)
int ret = SendRequest(g_serverSid, SERVER_OP_ADD_SERVICE, &anonymous, &anonymousreply, g_option, &anonymousptr); int ret = SendRequest(g_serverSid, SERVER_OP_ADD_SERVICE, &anonymous, &anonymousreply, g_option, &anonymousptr);
int res = -1; int res = -1;
ReadInt32(&anonymousreply, &res); ReadInt32(&anonymousreply, &res);
RPC_LOG_INFO("add self to server = %{public}d", res); RPC_LOG_INFO("add self to server = %d", res);
FreeBuffer((void *)anonymousptr); FreeBuffer((void *)anonymousptr);
EXPECT_EQ(ret, ERR_NONE); EXPECT_EQ(ret, ERR_NONE);
EXPECT_EQ(res, ERR_NONE); EXPECT_EQ(res, ERR_NONE);
......
...@@ -66,7 +66,7 @@ int32_t GetSystemAbility(int32_t saId, const char* deviceId, SvcIdentity *sid) ...@@ -66,7 +66,7 @@ int32_t GetSystemAbility(int32_t saId, const char* deviceId, SvcIdentity *sid)
int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option) int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
{ {
int32_t result = ERR_NONE; int32_t result = ERR_NONE;
RPC_LOG_INFO("OnRemoteRequest called.... code = %{public}d", code); RPC_LOG_INFO("OnRemoteRequest called.... code = %d", code);
switch (code) { switch (code) {
case ADD_SYSTEM_ABILITY_TRANSACTION: { case ADD_SYSTEM_ABILITY_TRANSACTION: {
int32_t saId; int32_t saId;
...@@ -88,7 +88,7 @@ int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption op ...@@ -88,7 +88,7 @@ int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption op
break; break;
} }
default: default:
RPC_LOG_ERROR("unknown code %{public}d", code); RPC_LOG_ERROR("unknown code %d", code);
break; break;
} }
return result; return result;
......
...@@ -79,7 +79,7 @@ int32_t RemoteRequestOne(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption ...@@ -79,7 +79,7 @@ int32_t RemoteRequestOne(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption
break; break;
} }
default: default:
RPC_LOG_ERROR("unknown code %{public}d", code); RPC_LOG_ERROR("unknown code %d", code);
break; break;
} }
return result; return result;
...@@ -111,7 +111,7 @@ int32_t RemoteRequestTwo(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption ...@@ -111,7 +111,7 @@ int32_t RemoteRequestTwo(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption
break; break;
} }
default: default:
RPC_LOG_ERROR("unknown code %{public}d", code); RPC_LOG_ERROR("unknown code %d", code);
break; break;
} }
return result; return result;
...@@ -216,7 +216,7 @@ int main(int argc, char *argv[]) ...@@ -216,7 +216,7 @@ int main(int argc, char *argv[])
ret = SendRequest(sidOne, SERVER_OP_MULTI, &data2, &reply, g_option, &ptr); ret = SendRequest(sidOne, SERVER_OP_MULTI, &data2, &reply, g_option, &ptr);
int res = -1; int res = -1;
ReadInt32(&reply, &res); ReadInt32(&reply, &res);
RPC_LOG_INFO(" 12 * 17 = %{public}d", res); RPC_LOG_INFO(" 12 * 17 = %d", res);
FreeBuffer((void *)ptr); FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE); EXPECT_EQ(ret, ERR_NONE);
int tmpMul = OP_A * OP_B; int tmpMul = OP_A * OP_B;
......
# 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")
import("//build/lite/config/test.gni")
group("ipc_test_gtest") {
deps = [
"client:ipc_client",
"samgr:samgr",
"server:ipc_server",
]
}
#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("//build/lite/config/component/lite_component.gni")
import("//build/lite/config/test.gni")
SUBSYSTEM_DIR = "//foundation/communication/ipc/test/unittest/ipc"
IPC_CORE_ROOT = "//foundation/communication/ipc/ipc/native/c"
unittest("ipc_client") {
output_extension = "bin"
output_dir = "$root_out_dir/test/unittest/ipc"
ldflags = [
"-lstdc++",
"-lpthread",
"-lrt",
]
include_dirs = [
"$SUBSYSTEM_DIR/include",
"//third_party/bounds_checking_function/include",
"//utils/native/lite/include",
"$IPC_CORE_ROOT/manager/include",
"$IPC_CORE_ROOT/ipc/include",
]
sources = [ "$SUBSYSTEM_DIR/client/client.cpp" ]
deps =
[ "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single" ]
}
/*
* 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 "gtest/gtest.h"
#include <time.h>
#include <stdlib.h>
#include "ipc_proxy.h"
#include "rpc_log.h"
#include "rpc_errno.h"
#include "ipc_skeleton.h"
#include "serializer.h"
static SvcIdentity sidServer;
MessageOption g_option = TF_OP_SYNC;
uint32_t cbId1 = -1;
uint32_t cbId2 = -1;
uint32_t cbId3 = -1;
uint32_t cbId4 = -1;
uint32_t cbId5 = -1;
int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
{
int32_t result = ERR_NONE;
RPC_LOG_INFO("client OnRemoteRequest called....");
switch (code) {
case CLIENT_OP_ADD: {
int32_t a;
ReadInt32(data, &a);
int32_t b;
ReadInt32(data, &b);
WriteInt32(reply, a + b);
break;
}
case CLIENT_OP_SUB: {
int32_t a;
ReadInt32(data, &a);
int32_t b;
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 %s....", str);
break;
}
default:
RPC_LOG_ERROR("unknown code %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 ... ");
}
using namespace testing::ext;
namespace OHOS {
class IpcClientTest : public testing::Test {
public:
static void SetUpTestCase()
{
RPC_LOG_INFO("----------test case for ipc client start-------------\n");
}
static void TearDownTestCase() {}
void SetUp() {}
void TearDown() {}
};
HWTEST_F(IpcClientTest, IpcClientTest001, TestSize.Level1)
{
IpcIo data1;
uint8_t tmpData1[IPC_MAX_SIZE];
IpcIoInit(&data1, tmpData1, IPC_MAX_SIZE, 0);
WriteInt32(&data1, SERVER_SA_ID1);
IpcIo reply1;
SvcIdentity target = {
.handle = 0
};
uintptr_t ptr = 0;
int ret = SendRequest(target, GET_SYSTEM_ABILITY_TRANSACTION, &data1, &reply1, g_option, &ptr);
ReadRemoteObject(&reply1, &sidServer);
FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE);
}
HWTEST_F(IpcClientTest, IpcClientTest002, TestSize.Level1)
{
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(sidServer, SERVER_OP_ADD, &data2, &reply2, g_option, &ptr2);
int res;
ReadInt32(&reply2, &res);
RPC_LOG_INFO(" 12 + 17 = %d", res);
FreeBuffer((void *)ptr2);
EXPECT_EQ(ret, ERR_NONE);
int tmpSum = OP_A + OP_B;
EXPECT_EQ(res, tmpSum);
}
static IpcObjectStub objectStub = {
.func = RemoteRequest,
.isRemote = false
};
static SvcIdentity clientSvc = {
.handle = -1,
.token = (uintptr_t)&objectStub,
.cookie = (uintptr_t)&objectStub
};
HWTEST_F(IpcClientTest, IpcClientTest003, TestSize.Level1)
{
IpcIo anonymous;
uint8_t anonymousData[IPC_MAX_SIZE];
IpcIoInit(&anonymous, anonymousData, IPC_MAX_SIZE, 1);
WriteRemoteObject(&anonymous, &clientSvc);
IpcIo anonymousreply;
uintptr_t anonymousptr = 0;
int ret = SendRequest(sidServer, SERVER_OP_ADD_SERVICE, &anonymous, &anonymousreply, g_option, &anonymousptr);
int res;
ReadInt32(&anonymousreply, &res);
RPC_LOG_INFO("add self to server = %d", res);
FreeBuffer((void *)anonymousptr);
EXPECT_EQ(ret, ERR_NONE);
EXPECT_EQ(res, ERR_NONE);
}
HWTEST_F(IpcClientTest, IpcClientTest004, TestSize.Level0)
{
RPC_LOG_INFO("============= test case for add death callback ============");
int ret = AddDeathRecipient(sidServer, (OnRemoteDead)ServerDead1, NULL, &cbId1);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(sidServer, (OnRemoteDead)ServerDead2, NULL, &cbId2);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(sidServer, (OnRemoteDead)ServerDead3, NULL, &cbId3);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(sidServer, (OnRemoteDead)ServerDead3, NULL, &cbId4);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(sidServer, (OnRemoteDead)ServerDead3, NULL, &cbId5); // failed
EXPECT_EQ(ret, ERR_INVALID_PARAM);
}
HWTEST_F(IpcClientTest, IpcClientTest005, TestSize.Level0)
{
RPC_LOG_INFO("============= test case for remove death callback ============");
int ret = RemoveDeathRecipient(sidServer, cbId2);
EXPECT_EQ(ret, ERR_NONE);
ret = RemoveDeathRecipient(sidServer, cbId4);
EXPECT_EQ(ret, ERR_NONE);
ret = RemoveDeathRecipient(sidServer, cbId1);
EXPECT_EQ(ret, ERR_NONE);
ret = RemoveDeathRecipient(sidServer, cbId3);
EXPECT_EQ(ret, ERR_NONE);
}
HWTEST_F(IpcClientTest, IpcClientTest006, TestSize.Level1)
{
++sidServer.handle;
int ret = AddDeathRecipient(sidServer, (OnRemoteDead)ServerDead3, NULL, &cbId5); // failed
EXPECT_EQ(ret, ERR_INVALID_PARAM);
ret = RemoveDeathRecipient(sidServer, cbId3); // failed
EXPECT_EQ(ret, ERR_INVALID_PARAM);
--sidServer.handle;
}
HWTEST_F(IpcClientTest, IpcClientTest007, TestSize.Level2) // 同步性能测试
{
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 res;
struct timespec start = {0, 0};
struct timespec end = {0, 0};
clock_gettime(CLOCK_REALTIME, &start);
for (int i = 0; i < 100; i++) {
SendRequest(sidServer, SERVER_OP_ADD, &data2, &reply2, g_option, &ptr2);
ReadInt32(&reply2, &res);
FreeBuffer((void *)ptr2);
}
clock_gettime(CLOCK_REALTIME, &end);
float time = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000; // ms
RPC_LOG_INFO("############ sync time with 100 times = %f ms", time);
}
HWTEST_F(IpcClientTest, IpcClientTest008, TestSize.Level2) // 异步性能测试
{
IpcIo data2;
uint8_t tmpData2[IPC_MAX_SIZE];
IpcIoInit(&data2, tmpData2, IPC_MAX_SIZE, 0);
WriteInt32(&data2, OP_A);
WriteInt32(&data2, OP_B);
int res;
struct timespec start = {0, 0};
struct timespec end = {0, 0};
MessageOption option = TF_OP_ASYNC;
clock_gettime(CLOCK_REALTIME, &start);
for (int i = 0; i < 100; i++) {
SendRequest(sidServer, SERVER_OP_ADD, &data2, NULL, option, NULL);
}
clock_gettime(CLOCK_REALTIME, &end);
float time = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000; // ms
RPC_LOG_INFO("########### async time with 100 times = %f ms", time);
}
HWTEST_F(IpcClientTest, IpcClientTest009, TestSize.Level0)
{
int ret = AddDeathRecipient(sidServer, (OnRemoteDead)ServerDead1, NULL, &cbId1);
EXPECT_EQ(ret, ERR_NONE);
ret = AddDeathRecipient(sidServer, (OnRemoteDead)ServerDead2, NULL, &cbId2);
EXPECT_EQ(ret, ERR_NONE);
JoinWorkThread();
}
} // namespace OHOS
\ 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 IPC_TEST_TIME_INTERVAL 60
#define OP_A 12
#define OP_B 17
#endif // OHOS_IPC_PROXY_H
\ 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.
import("//build/lite/config/component/lite_component.gni")
import("//build/lite/config/test.gni")
SUBSYSTEM_DIR = "//foundation/communication/ipc/test/unittest/ipc"
IPC_CORE_ROOT = "//foundation/communication/ipc/ipc/native/c"
unittest("samgr") {
output_extension = "bin"
output_dir = "$root_out_dir/test/unittest/ipc"
ldflags = [
"-lstdc++",
"-lpthread",
]
include_dirs = [
"$SUBSYSTEM_DIR/include",
"//third_party/bounds_checking_function/include",
"//utils/native/lite/include",
"$IPC_CORE_ROOT/manager/include",
"$IPC_CORE_ROOT/ipc/include",
]
sources = [ "samgr.cpp" ]
deps =
[ "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single" ]
}
/*
* 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 "ipc_proxy.h"
#include "gtest/gtest.h"
#include <stdlib.h>
#include <string.h>
#include "rpc_log.h"
#include "rpc_errno.h"
#include "ipc_skeleton.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;
static int32_t g_saSum = 0;
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);
g_saSum++;
RPC_LOG_INFO("samgr sa count = %d", g_saSum);
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 = %d", code);
switch (code) {
case ADD_SYSTEM_ABILITY_TRANSACTION: {
int32_t saId;
ReadInt32(data, &saId);
RPC_LOG_INFO("samgr pop said = %d....", 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 %d", code);
break;
}
return result;
}
int32_t mainFunc(void)
{
RPC_LOG_INFO("Enter System Ability Manager .... ");
g_saList = (UTILS_DL_LIST *)calloc(1, sizeof(UTILS_DL_LIST));
if (g_saList == NULL) {
return -1;
}
UtilsListInit(g_saList);
return ERR_NONE;
}
using namespace testing::ext;
namespace OHOS {
class IpcSamgrTest : public testing::Test {
public:
static void SetUpTestCase()
{
RPC_LOG_INFO("----------test case for samgr start-------------\n");
mainFunc();
}
static void TearDownTestCase()
{
RPC_LOG_INFO("----------test case for samgr end -------------\n");
}
void SetUp() {}
void TearDown() {}
};
HWTEST_F(IpcSamgrTest, IpcSamgrTest001, TestSize.Level0)
{
IpcObjectStub objectStub = {
.func = RemoteRequest,
.isRemote = false
};
SvcIdentity target = {
.handle = 0,
.cookie = (uintptr_t)&objectStub
};
int ret = SetContextObject(target);
EXPECT_EQ(ret, ERR_NONE);
JoinWorkThread();
}
} // namespace OHOS
\ 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.
import("//build/lite/config/component/lite_component.gni")
import("//build/lite/config/test.gni")
SUBSYSTEM_DIR = "//foundation/communication/ipc/test/unittest/ipc"
IPC_CORE_ROOT = "//foundation/communication/ipc/ipc/native/c"
unittest("ipc_server") {
output_extension = "bin"
output_dir = "$root_out_dir/test/unittest/ipc"
ldflags = [
"-lstdc++",
"-lpthread",
]
include_dirs = [
"$SUBSYSTEM_DIR/include",
"//third_party/bounds_checking_function/include",
"//utils/native/lite/include",
"$IPC_CORE_ROOT/manager/include",
"$IPC_CORE_ROOT/ipc/include",
]
sources = [ "server.cpp" ]
deps =
[ "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single" ]
}
/*
* 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 "ipc_proxy.h"
#include "gtest/gtest.h"
#include <stdlib.h>
#include <pthread.h>
#include "rpc_log.h"
#include "rpc_errno.h"
#include "ipc_skeleton.h"
#include "serializer.h"
#include <unistd.h>
static SvcIdentity *sid = NULL;
MessageOption g_option = TF_OP_SYNC;
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;
switch (code) {
case SERVER_OP_ADD: {
int32_t a;
ReadInt32(data, &a);
int32_t b;
ReadInt32(data, &b);
WriteInt32(reply, a + b);
break;
}
case SERVER_OP_SUB: {
int32_t a;
ReadInt32(data, &a);
int32_t b;
ReadInt32(data, &b);
WriteInt32(reply, a - b);
break;
}
case SERVER_OP_MULTI: {
int32_t a;
ReadInt32(data, &a);
int32_t b;
ReadInt32(data, &b);
WriteInt32(reply, a * b);
break;
}
case SERVER_OP_ADD_SERVICE: {
sid = (SvcIdentity *)calloc(1, sizeof(SvcIdentity));
ReadRemoteObject(data, sid);
const char *str = "server call anonymos service one.";
break;
}
default:
RPC_LOG_ERROR("unknown code %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....");
switch (code) {
case SERVER_OP_ADD: {
int32_t a;
ReadInt32(data, &a);
int32_t b;
ReadInt32(data, &b);
WriteInt32(reply, a + b);
break;
}
case SERVER_OP_SUB: {
int32_t a;
ReadInt32(data, &a);
int32_t b;
ReadInt32(data, &b);
WriteInt32(reply, a - b);
break;
}
case SERVER_OP_MULTI: {
int32_t a;
ReadInt32(data, &a);
int32_t b;
ReadInt32(data, &b);
WriteInt32(reply, a * b);
break;
}
default:
RPC_LOG_ERROR("unknown code %d", code);
break;
}
return result;
}
static void *ThreadHandler(void *args)
{
sleep(IPC_TEST_TIME_INTERVAL); // sleep 2 min
const char *str = "server call anonymos service new thread.";
CallAnonymosFunc(str);
return NULL;
}
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 SvcIdentity sidOne;
using namespace testing::ext;
namespace OHOS {
class IpcServerTest : public testing::Test {
public:
static void SetUpTestCase()
{
RPC_LOG_INFO("----------test case for ipc server start-------------\n");
}
static void TearDownTestCase() {}
void SetUp() {}
void TearDown()
{
sleep(1);
}
};
HWTEST_F(IpcServerTest, IpcServerTest001, TestSize.Level1)
{
IpcIo data;
uint8_t tmpData1[IPC_MAX_SIZE];
IpcIoInit(&data, tmpData1, IPC_MAX_SIZE, 1);
WriteInt32(&data, SERVER_SA_ID1);
bool res = WriteRemoteObject(NULL, &svcOne);
EXPECT_EQ(res, false);
}
HWTEST_F(IpcServerTest, IpcServerTest002, TestSize.Level1)
{
IpcIo data;
uint8_t tmpData1[IPC_MAX_SIZE];
IpcIoInit(&data, tmpData1, IPC_MAX_SIZE, 1);
WriteInt32(&data, SERVER_SA_ID1);
bool res = WriteRemoteObject(&data, NULL);
EXPECT_EQ(res, false);
}
HWTEST_F(IpcServerTest, IpcServerTest003, TestSize.Level0)
{
IpcIo data;
uint8_t tmpData1[IPC_MAX_SIZE];
IpcIoInit(&data, tmpData1, IPC_MAX_SIZE, 1);
bool wr = WriteInt32(&data, SERVER_SA_ID1);
EXPECT_TRUE(wr);
wr = WriteRemoteObject(&data, &svcOne);
EXPECT_TRUE(wr);
IpcIo reply;
uintptr_t ptr = 0;
RPC_LOG_INFO("====== add ability one to samgr ======");
SvcIdentity target = {
.handle = 0
};
int ret = SendRequest(target, 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);
}
HWTEST_F(IpcServerTest, IpcServerTest004, TestSize.Level0)
{
IpcIo data;
uint8_t tmpData2[IPC_MAX_SIZE];
IpcIoInit(&data, tmpData2, IPC_MAX_SIZE, 1);
WriteInt32(&data, SERVER_SA_ID2);
WriteRemoteObject(&data, &svcTwo);
RPC_LOG_INFO("====== add ability two to samgr ======");
uintptr_t ptr = 0;
IpcIo reply;
SvcIdentity target = {
.handle = 0
};
int ret = SendRequest(target, ADD_SYSTEM_ABILITY_TRANSACTION, &data, &reply, g_option, &ptr);
int res;
ReadInt32(&reply, &res);
FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE);
EXPECT_EQ(res, ERR_NONE);
}
HWTEST_F(IpcServerTest, IpcServerTest005, TestSize.Level0)
{
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_ID2);
uintptr_t ptr = 0;
IpcIo reply;
const SvcIdentity *target = GetContextObject();
int ret = SendRequest(*target, GET_SYSTEM_ABILITY_TRANSACTION, &data1, &reply, g_option, &ptr);
ReadRemoteObject(&reply, &sidOne);
FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE);
}
HWTEST_F(IpcServerTest, IpcServerTest006, TestSize.Level0)
{
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);
uintptr_t ptr = 0;
IpcIo reply;
int ret = SendRequest(sidOne, SERVER_OP_MULTI, &data2, &reply, g_option, &ptr);
int res;
ReadInt32(&reply, &res);
RPC_LOG_INFO(" 12 * 17 = %d", res);
FreeBuffer((void *)ptr);
EXPECT_EQ(ret, ERR_NONE);
int tmpMul = OP_A * OP_B;
EXPECT_EQ(res, tmpMul);
}
HWTEST_F(IpcServerTest, IpcServerTest007, TestSize.Level0)
{
pthread_t pid;
pthread_create(&pid, NULL, ThreadHandler, NULL);
pthread_detach(pid);
JoinWorkThread();
}
} // namespace OHOS
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册