diff --git a/ipc/test/ipc/BUILD.gn b/ipc/test/ipc/BUILD.gn index 671613b85a9bc8425ec39f8ab88b0028bfb06d0f..1427fd1396e557b40148aad449641cbec273a126 100644 --- a/ipc/test/ipc/BUILD.gn +++ b/ipc/test/ipc/BUILD.gn @@ -9,9 +9,10 @@ # 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. +# 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") { features = [ diff --git a/ipc/test/ipc/client/client.c b/ipc/test/ipc/client/client.c index ddc4139acbd5de594c81a8e38a276a5ecaf381b7..6f0643c85f74dc7c66ba173f7e8cfe804d5b599c 100644 --- a/ipc/test/ipc/client/client.c +++ b/ipc/test/ipc/client/client.c @@ -46,11 +46,11 @@ int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption op case CLIENT_OP_PRINT: { size_t 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; } default: - RPC_LOG_ERROR("unknown code %{public}d", code); + RPC_LOG_ERROR("unknown code %d", code); break; } return result; @@ -104,7 +104,7 @@ static void CallServerAdd(void) 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); + RPC_LOG_INFO(" 12 + 17 = %d", res); FreeBuffer((void *)ptr2); EXPECT_EQ(ret, ERR_NONE); int tmpSum = OP_A + OP_B; @@ -134,7 +134,7 @@ static void AnonymousTest(void) 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); + RPC_LOG_INFO("add self to server = %d", res); FreeBuffer((void *)anonymousptr); EXPECT_EQ(ret, ERR_NONE); EXPECT_EQ(res, ERR_NONE); diff --git a/ipc/test/ipc/samgr/samgr.c b/ipc/test/ipc/samgr/samgr.c index 322976d4b839b269c7f81429f16c150c40bf6caf..0ab7336dc015cae3dddc95fc0d30fea8c2273672 100644 --- a/ipc/test/ipc/samgr/samgr.c +++ b/ipc/test/ipc/samgr/samgr.c @@ -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 result = ERR_NONE; - RPC_LOG_INFO("OnRemoteRequest called.... code = %{public}d", code); + RPC_LOG_INFO("OnRemoteRequest called.... code = %d", code); switch (code) { case ADD_SYSTEM_ABILITY_TRANSACTION: { int32_t saId; @@ -88,7 +88,7 @@ int32_t RemoteRequest(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption op break; } default: - RPC_LOG_ERROR("unknown code %{public}d", code); + RPC_LOG_ERROR("unknown code %d", code); break; } return result; diff --git a/ipc/test/ipc/server/server.c b/ipc/test/ipc/server/server.c index 16616b8244ec029c222482c4347e83bfb215de01..5b36b92edb94a80e83ac674f372a48cebbef597f 100644 --- a/ipc/test/ipc/server/server.c +++ b/ipc/test/ipc/server/server.c @@ -79,7 +79,7 @@ int32_t RemoteRequestOne(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption break; } default: - RPC_LOG_ERROR("unknown code %{public}d", code); + RPC_LOG_ERROR("unknown code %d", code); break; } return result; @@ -111,7 +111,7 @@ int32_t RemoteRequestTwo(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption break; } default: - RPC_LOG_ERROR("unknown code %{public}d", code); + RPC_LOG_ERROR("unknown code %d", code); break; } return result; @@ -216,7 +216,7 @@ int main(int argc, char *argv[]) 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); + RPC_LOG_INFO(" 12 * 17 = %d", res); FreeBuffer((void *)ptr); EXPECT_EQ(ret, ERR_NONE); int tmpMul = OP_A * OP_B; diff --git a/ipc/test/unittest/ipc/BUILD.gn b/ipc/test/unittest/ipc/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8f235f488d58204b2f456ea4c611c1a0ebd8945e --- /dev/null +++ b/ipc/test/unittest/ipc/BUILD.gn @@ -0,0 +1,23 @@ +# 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", + ] +} diff --git a/ipc/test/unittest/ipc/client/BUILD.gn b/ipc/test/unittest/ipc/client/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a12ac7489abff7e94ca67a570c86e3fa5fae324c --- /dev/null +++ b/ipc/test/unittest/ipc/client/BUILD.gn @@ -0,0 +1,41 @@ +#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" ] +} diff --git a/ipc/test/unittest/ipc/client/client.cpp b/ipc/test/unittest/ipc/client/client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d1b3459e95cc748f54178f5207997c5683b8e00 --- /dev/null +++ b/ipc/test/unittest/ipc/client/client.cpp @@ -0,0 +1,267 @@ +/* + * 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 +#include + +#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 diff --git a/ipc/test/unittest/ipc/include/ipc_proxy.h b/ipc/test/unittest/ipc/include/ipc_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..b08461f7fdc67f04b87326ab761004a896467ed6 --- /dev/null +++ b/ipc/test/unittest/ipc/include/ipc_proxy.h @@ -0,0 +1,49 @@ +/* + * 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 diff --git a/ipc/test/unittest/ipc/samgr/BUILD.gn b/ipc/test/unittest/ipc/samgr/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..1581382fc06dc8f8591beb8f645763027960eed5 --- /dev/null +++ b/ipc/test/unittest/ipc/samgr/BUILD.gn @@ -0,0 +1,40 @@ +#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" ] +} diff --git a/ipc/test/unittest/ipc/samgr/samgr.cpp b/ipc/test/unittest/ipc/samgr/samgr.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a0bbe1dcc54628a89bb1869b8bf64f81b5f75e9 --- /dev/null +++ b/ipc/test/unittest/ipc/samgr/samgr.cpp @@ -0,0 +1,147 @@ +/* + * 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 +#include + +#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 diff --git a/ipc/test/unittest/ipc/server/BUILD.gn b/ipc/test/unittest/ipc/server/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..cee6d0327ad793f0588647b4e36d4e19c1c8023f --- /dev/null +++ b/ipc/test/unittest/ipc/server/BUILD.gn @@ -0,0 +1,40 @@ +#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" ] +} diff --git a/ipc/test/unittest/ipc/server/server.cpp b/ipc/test/unittest/ipc/server/server.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b61dde855572679b9d7ac399ac052767f3dc6f9b --- /dev/null +++ b/ipc/test/unittest/ipc/server/server.cpp @@ -0,0 +1,283 @@ +/* + * 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 +#include + +#include "rpc_log.h" +#include "rpc_errno.h" +#include "ipc_skeleton.h" +#include "serializer.h" +#include + +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