!1074 feat: skeleton UT

Merge pull request !1074 from yyc1234/master
This commit is contained in:
openharmony_ci 2024-05-07 11:37:00 +00:00 committed by Gitee
commit ff9b7ddc8d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
14 changed files with 958 additions and 24 deletions

View File

@ -60,6 +60,10 @@ ohos_shared_library("ipc_ndk") {
"sasdk",
]
symlink_target_name = [ "libipc_ndk.so" ]
relative_install_dir = "./ndk/"
install_images = [ system_base_dir ]
subsystem_name = "communication"

View File

@ -0,0 +1,42 @@
# Copyright (C) 2024 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/ohos.gni")
import("//build/ohos/ndk/ndk.gni")
SUBSYSTEM_DIR = "../../.."
ohos_ndk_headers("ipc_ndk_header") {
dest_dir = "$ndk_headers_out_dir/IPCKit"
sources = [
"$SUBSYSTEM_DIR/interfaces/innerkits/c_api/include/ipc_cparcel.h",
"$SUBSYSTEM_DIR/interfaces/innerkits/c_api/include/ipc_cremote_object.h",
"$SUBSYSTEM_DIR/interfaces/innerkits/c_api/include/ipc_cskeleton.h",
"$SUBSYSTEM_DIR/interfaces/innerkits/c_api/include/ipc_error_code.h",
"$SUBSYSTEM_DIR/interfaces/innerkits/c_api/include/ipc_kit.h",
]
}
ohos_ndk_library("libipc_ndk") {
output_name = "ipc_ndk"
output_extension = "so"
ndk_description_file = "./libipc_ndk.json"
system_capability = "SystemCapability.Communication.IPC.Core"
system_capability_headers = [
"IPCKit/ipc_cparcel.h",
"IPCKit/ipc_cremote_object.h",
"IPCKit/ipc_cskeleton.h",
"IPCKit/ipc_error_code.h",
"IPCKit/ipc_kit.h",
]
}

View File

@ -0,0 +1,56 @@
[
{ "name": "OH_IPCSkeleton_JoinWorkThread" },
{ "name": "OH_IPCSkeleton_StopWorkThread" },
{ "name": "OH_IPCSkeleton_GetCallingTokenId" },
{ "name": "OH_IPCSkeleton_GetFirstTokenId" },
{ "name": "OH_IPCSkeleton_GetSelfTokenId" },
{ "name": "OH_IPCSkeleton_GetCallingPid" },
{ "name": "OH_IPCSkeleton_GetCallingUid" },
{ "name": "OH_IPCSkeleton_IsLocalCalling" },
{ "name": "OH_IPCSkeleton_SetMaxWorkThreadNum" },
{ "name": "OH_IPCSkeleton_SetCallingIdentity" },
{ "name": "OH_IPCSkeleton_ResetCallingIdentity" },
{ "name": "OH_IPCSkeleton_IsHandlingTransaction" },
{ "name": "OH_IPCRemoteStub_Create" },
{ "name": "OH_IPCRemoteStub_Destroy" },
{ "name": "OH_IPC_SendRequest" },
{ "name": "OH_IPCDeathRecipient_Create" },
{ "name": "OH_IPCDeathRecipient_Destroy" },
{ "name": "OH_IPCRemoteProxy_AddDeathRecipient" },
{ "name": "OH_IPCRemoteProxy_RemoveDeathRecipient" },
{ "name": "OH_IPC_IsRemoteDead" },
{ "name": "OH_IPCParcel_Create" },
{ "name": "OH_IPCParcel_Destroy" },
{ "name": "OH_IPCParcel_GetDataSize" },
{ "name": "OH_IPCParcel_GetWritableBytes" },
{ "name": "OH_IPCParcel_GetReadableBytes" },
{ "name": "OH_IPCParcel_GetReadPosition" },
{ "name": "OH_IPCParcel_GetWritePosition" },
{ "name": "OH_IPCParcel_RewindReadPosition" },
{ "name": "OH_IPCParcel_RewindWritePosition" },
{ "name": "OH_IPCParcel_WriteInt8" },
{ "name": "OH_IPCParcel_ReadInt8" },
{ "name": "OH_IPCParcel_WriteInt16"},
{ "name": "OH_IPCParcel_ReadInt16"},
{ "name": "OH_IPCParcel_WriteInt32" },
{ "name": "OH_IPCParcel_ReadInt32" },
{ "name": "OH_IPCParcel_WriteInt64" },
{ "name": "OH_IPCParcel_ReadInt64" },
{ "name": "OH_IPCParcel_WriteFloat" },
{ "name": "OH_IPCParcel_ReadFloat" },
{ "name": "OH_IPCParcel_WriteDouble" },
{ "name": "OH_IPCParcel_ReadDouble" },
{ "name": "OH_IPCParcel_WriteString" },
{ "name": "OH_IPCParcel_ReadString" },
{ "name": "OH_IPCParcel_WriteBuffer" },
{ "name": "OH_IPCParcel_ReadBuffer" },
{ "name": "OH_IPCParcel_WriteRemoteStub" },
{ "name": "OH_IPCParcel_ReadRemoteStub" },
{ "name": "OH_IPCParcel_WriteRemoteProxy" },
{ "name": "OH_IPCParcel_ReadRemoteProxy" },
{ "name": "OH_IPCParcel_WriteFileDescriptor" },
{ "name": "OH_IPCParcel_ReadFileDescriptor" },
{ "name": "OH_IPCParcel_Append" },
{ "name": "OH_IPCParcel_WriteInterfaceToken" },
{ "name": "OH_IPCParcel_ReadInterfaceToken" }
]

View File

@ -201,7 +201,7 @@ int OH_IPCParcel_WriteString(OHIPCParcel *parcel, const char *str)
return OH_IPC_CHECK_PARAM_ERROR;
}
return parcel->msgParcel->WriteString(std::string(str)) ? OH_IPC_SUCCESS : OH_IPC_PARCEL_WRITE_ERROR;
return parcel->msgParcel->WriteCString(str) ? OH_IPC_SUCCESS : OH_IPC_PARCEL_WRITE_ERROR;
}
const char* OH_IPCParcel_ReadString(const OHIPCParcel *parcel)
@ -233,9 +233,9 @@ const uint8_t* OH_IPCParcel_ReadBuffer(const OHIPCParcel *parcel, int32_t len)
if (!IsIPCParcelValid(parcel, __func__)) {
return nullptr;
}
if (len <= 0 || len > parcel->msgParcel->GetReadableBytes()) {
ZLOGE(LOG_LABEL, "read buf len:%{public}d invalid! ReadableBytes:%{public}d",
len, parcel->msgParcel->GetReadableBytes());
int readableBytes = static_cast<int>(parcel->msgParcel->GetReadableBytes());
if (len <= 0 || len > readableBytes) {
ZLOGE(LOG_LABEL, "read buf len:%{public}d invalid! ReadableBytes:%{public}d", len, readableBytes);
return nullptr;
}

View File

@ -363,6 +363,7 @@ ohos_unittest("IpcCApiUnitTest") {
sources = [
"ipc_capi_parcel_unittest.cpp",
"ipc_capi_remote_object_unittest.cpp",
"ipc_capi_skeleton_unittest.cpp",
]
configs = [
@ -381,6 +382,7 @@ ohos_unittest("IpcCApiUnitTest") {
"c_utils:utils",
"hilog:libhilog",
"hitrace:libhitracechain",
"ipc:ipc_core",
"samgr:samgr_proxy",
]

View File

@ -20,7 +20,12 @@
#include <fcntl.h>
#include <cstring>
#include <securec.h>
#include <thread>
#include <chrono>
#include "parcel.h"
#include "refbase.h"
#include "ipc_cparcel.h"
#include "ipc_cremote_object.h"
#include "ipc_test_helper.h"
#include "test_service_command.h"
#include "ipc_error_code.h"
@ -40,6 +45,17 @@ static constexpr float FLOAT_CONSTANT = 1.1;
static const char *STRING_CONSTANT = "HELLO";
static constexpr int DEFAULT_CAPACITY = 0;
static constexpr int MAX_MEMORY_SIZE = 204800;
static constexpr int MAX_INTERFACE_TOKEN_LEN = 100;
static constexpr int TEST_PARCEL_SIZE = MAX_MEMORY_SIZE - 10;
static constexpr int TEST_PERFORMANCE_OPERATOR_COUNT = 2000;
struct PerformanceResult {
uint32_t min{ 100 };
uint32_t max{ 0 };
uint32_t average{ 0 };
};
using time_point = std::chrono::time_point<std::chrono::steady_clock>;
class IpcCApiParcelUnitTest : public testing::Test {
public:
@ -47,9 +63,48 @@ public:
static void TearDownTestCase(void);
void SetUp();
void TearDown();
static constexpr HiLogLabel LABEL = {LOG_CORE, LOG_ID_TEST, "IpcCApiUnitTest"};
uint32_t CalcSpendTime(time_point &start, time_point &end);
void ReadWriteStringPerformance(PerformanceResult &writeResult, PerformanceResult &readResult);
void ReadWriteStringCppPerformance(PerformanceResult &writeResult, PerformanceResult &readResult);
void ReadWriteBufferPerformance(PerformanceResult &writeResult, PerformanceResult &readResult);
void ReadWriteBufferCppPerformance(PerformanceResult &writeResult, PerformanceResult &readResult);
void ReadWriteInterfaceTokenPerformance(PerformanceResult &writeResult, PerformanceResult &readResult);
void ReadWriteInterfaceTokenCppPerformance(PerformanceResult &writeResult, PerformanceResult &readResult);
static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "IpcCApiUnitTest" };
};
static void* LocalMemAllocator(int32_t len)
{
if (len < 0 || len > MAX_MEMORY_SIZE) {
return nullptr;
}
void *buffer = malloc(len);
if (buffer != nullptr) {
if (memset_s(buffer, len, 0, len) != EOK) {
ZLOGE(IpcCApiParcelUnitTest::LABEL, "memset_s failed!");
}
}
return buffer;
}
static void* LocalMemAllocatorErr(int32_t len)
{
return nullptr;
}
static int OnRemoteRequestStub(uint32_t code, const OHIPCParcel *data, OHIPCParcel *reply,
void *userData)
{
(void)userData;
(void)code;
(void)data;
(void)reply;
return 0;
}
void IpcCApiParcelUnitTest::SetUpTestCase() {}
void IpcCApiParcelUnitTest::TearDownTestCase() {}
@ -58,22 +113,224 @@ void IpcCApiParcelUnitTest::SetUp() {}
void IpcCApiParcelUnitTest::TearDown() {}
static void* LocalMemAllocator(int32_t len)
uint32_t IpcCApiParcelUnitTest::CalcSpendTime(time_point& start, time_point& end)
{
if (len < 0 || len > MAX_MEMORY_SIZE) {
return nullptr;
}
void *buffer = malloc(len);
if (buffer != nullptr) {
(void)memset_s(buffer, len, 0, len);
}
return buffer;
auto duration = end - start;
return static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::microseconds>(duration).count());
}
static void* LocalMemAllocatorErr(int32_t len)
void IpcCApiParcelUnitTest::ReadWriteStringPerformance(PerformanceResult &writeResult,
PerformanceResult &readResult)
{
return nullptr;
char buf[TEST_PARCEL_SIZE] = {0};
if (memset_s(buf, sizeof(buf), '1', sizeof(buf) - 1) != EOK) {
ZLOGE(LABEL, "memset_s failed!");
}
for (int i = 0; i < TEST_PERFORMANCE_OPERATOR_COUNT; ++i) {
auto dataCParcel = OH_IPCParcel_Create();
int ret = OH_IPC_SUCCESS;
auto startPoint = std::chrono::steady_clock::now();
ret = OH_IPCParcel_WriteString(dataCParcel, buf);
auto endPoint = std::chrono::steady_clock::now();
ASSERT_EQ(ret, OH_IPC_SUCCESS);
uint32_t duration = CalcSpendTime(startPoint, endPoint);
writeResult.min = (duration > writeResult.min) ? writeResult.min : duration;
writeResult.max = (duration < writeResult.max) ? writeResult.max : duration;
writeResult.average += duration;
startPoint = std::chrono::steady_clock::now();
const char *readStr = OH_IPCParcel_ReadString(dataCParcel);
endPoint = std::chrono::steady_clock::now();
ASSERT_NE(readStr, nullptr);
EXPECT_EQ(strlen(readStr), sizeof(buf) - 1);
duration = CalcSpendTime(startPoint, endPoint);
readResult.min = (duration > readResult.min) ? readResult.min : duration;
readResult.max = (duration < readResult.max) ? readResult.max : duration;
readResult.average += duration;
OH_IPCParcel_Destroy(dataCParcel);
}
writeResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
readResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
}
void IpcCApiParcelUnitTest::ReadWriteStringCppPerformance(PerformanceResult &writeResult,
PerformanceResult &readResult)
{
char buf[TEST_PARCEL_SIZE] = {0};
if (memset_s(buf, sizeof(buf), '1', sizeof(buf) - 1) != EOK) {
ZLOGE(LABEL, "memset_s failed!");
}
for (int i = 0; i < TEST_PERFORMANCE_OPERATOR_COUNT; ++i) {
MessageParcel dataCpp;
auto startPoint = std::chrono::steady_clock::now();
dataCpp.WriteCString(buf);
auto endPoint = std::chrono::steady_clock::now();
uint32_t duration = CalcSpendTime(startPoint, endPoint);
writeResult.min = (duration > writeResult.min) ? writeResult.min : duration;
writeResult.max = (duration < writeResult.max) ? writeResult.max : duration;
writeResult.average += duration;
startPoint = std::chrono::steady_clock::now();
const char* readCString = dataCpp.ReadCString();
endPoint = std::chrono::steady_clock::now();
ASSERT_NE(readCString, nullptr);
EXPECT_EQ(strlen(readCString), sizeof(buf) - 1);
duration = CalcSpendTime(startPoint, endPoint);
readResult.min = (duration > readResult.min) ? readResult.min : duration;
readResult.max = (duration < readResult.max) ? readResult.max : duration;
readResult.average += duration;
}
writeResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
readResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
}
void IpcCApiParcelUnitTest::ReadWriteBufferPerformance(PerformanceResult &writeResult,
PerformanceResult &readResult)
{
uint8_t buf[TEST_PARCEL_SIZE] = {0};
if (memset_s(buf, sizeof(buf), '2', sizeof(buf)) != EOK) {
ZLOGE(LABEL, "memset_s failed!");
}
for (int i = 0; i < TEST_PERFORMANCE_OPERATOR_COUNT; ++i) {
auto dataCParcel = OH_IPCParcel_Create();
int ret = OH_IPC_SUCCESS;
auto startPoint = std::chrono::steady_clock::now();
ret = OH_IPCParcel_WriteBuffer(dataCParcel, buf, TEST_PARCEL_SIZE);
auto endPoint = std::chrono::steady_clock::now();
ASSERT_EQ(ret, OH_IPC_SUCCESS);
uint32_t duration = CalcSpendTime(startPoint, endPoint);
writeResult.min = (duration > writeResult.min) ? writeResult.min : duration;
writeResult.max = (duration < writeResult.max) ? writeResult.max : duration;
writeResult.average += duration;
startPoint = std::chrono::steady_clock::now();
const uint8_t *readBuffer = OH_IPCParcel_ReadBuffer(dataCParcel, TEST_PARCEL_SIZE);
endPoint = std::chrono::steady_clock::now();
ASSERT_NE(readBuffer, nullptr);
EXPECT_EQ(memcmp(readBuffer, buf, TEST_PARCEL_SIZE), 0);
duration = CalcSpendTime(startPoint, endPoint);
readResult.min = (duration > readResult.min) ? readResult.min : duration;
readResult.max = (duration < readResult.max) ? readResult.max : duration;
readResult.average += duration;
OH_IPCParcel_Destroy(dataCParcel);
}
writeResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
readResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
}
void IpcCApiParcelUnitTest::ReadWriteBufferCppPerformance(PerformanceResult &writeResult,
PerformanceResult &readResult)
{
uint8_t buf[TEST_PARCEL_SIZE] = {0};
if (memset_s(buf, sizeof(buf), '2', sizeof(buf)) != EOK) {
ZLOGE(LABEL, "memset_s failed!");
}
for (int i = 0; i < TEST_PERFORMANCE_OPERATOR_COUNT; ++i) {
MessageParcel dataCpp;
auto startPoint = std::chrono::steady_clock::now();
dataCpp.WriteBuffer(buf, TEST_PARCEL_SIZE);
auto endPoint = std::chrono::steady_clock::now();
uint32_t duration = CalcSpendTime(startPoint, endPoint);
writeResult.min = (duration > writeResult.min) ? writeResult.min : duration;
writeResult.max = (duration < writeResult.max) ? writeResult.max : duration;
writeResult.average += duration;
startPoint = std::chrono::steady_clock::now();
const uint8_t* readBuf = dataCpp.ReadBuffer(TEST_PARCEL_SIZE);
endPoint = std::chrono::steady_clock::now();
ASSERT_NE(readBuf, nullptr);
EXPECT_EQ(memcmp(readBuf, buf, TEST_PARCEL_SIZE), 0);
duration = CalcSpendTime(startPoint, endPoint);
readResult.min = (duration > readResult.min) ? readResult.min : duration;
readResult.max = (duration < readResult.max) ? readResult.max : duration;
readResult.average += duration;
}
writeResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
readResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
}
void IpcCApiParcelUnitTest::ReadWriteInterfaceTokenPerformance(PerformanceResult &writeResult,
PerformanceResult &readResult)
{
char token[MAX_INTERFACE_TOKEN_LEN] = {0};
if (memset_s(token, sizeof(token), '1', sizeof(token) - 1) != EOK) {
ZLOGE(LABEL, "memset_s failed!");
}
for (int i = 0; i < TEST_PERFORMANCE_OPERATOR_COUNT; ++i) {
auto dataCParcel = OH_IPCParcel_Create();
int ret = OH_IPC_SUCCESS;
auto startPoint = std::chrono::steady_clock::now();
ret = OH_IPCParcel_WriteInterfaceToken(dataCParcel, token);
auto endPoint = std::chrono::steady_clock::now();
ASSERT_EQ(ret, OH_IPC_SUCCESS);
uint32_t duration = CalcSpendTime(startPoint, endPoint);
writeResult.min = (duration > writeResult.min) ? writeResult.min : duration;
writeResult.max = (duration < writeResult.max) ? writeResult.max : duration;
writeResult.average += duration;
int readLen = 0;
char *readInterfaceToken = nullptr;
startPoint = std::chrono::steady_clock::now();
ret = OH_IPCParcel_ReadInterfaceToken(dataCParcel, &readInterfaceToken, &readLen, LocalMemAllocator);
endPoint = std::chrono::steady_clock::now();
EXPECT_EQ(strcmp(token, readInterfaceToken), 0);
EXPECT_EQ(readLen, MAX_INTERFACE_TOKEN_LEN);
duration = CalcSpendTime(startPoint, endPoint);
readResult.min = (duration > readResult.min) ? readResult.min : duration;
readResult.max = (duration < readResult.max) ? readResult.max : duration;
readResult.average += duration;
OH_IPCParcel_Destroy(dataCParcel);
}
writeResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
readResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
}
void IpcCApiParcelUnitTest::ReadWriteInterfaceTokenCppPerformance(PerformanceResult &writeResult,
PerformanceResult &readResult)
{
char token[MAX_INTERFACE_TOKEN_LEN] = {0};
if (memset_s(token, sizeof(token), '1', sizeof(token) - 1) != EOK) {
ZLOGE(LABEL, "memset_s failed!");
}
for (int i = 0; i < TEST_PERFORMANCE_OPERATOR_COUNT; ++i) {
MessageParcel dataCpp;
auto startPoint = std::chrono::steady_clock::now();
auto u16Token = OHOS::Str8ToStr16(token);
dataCpp.WriteInterfaceToken(u16Token.c_str());
auto endPoint = std::chrono::steady_clock::now();
uint32_t duration = CalcSpendTime(startPoint, endPoint);
writeResult.min = (duration > writeResult.min) ? writeResult.min : duration;
writeResult.max = (duration < writeResult.max) ? writeResult.max : duration;
writeResult.average += duration;
startPoint = std::chrono::steady_clock::now();
auto u16TokenRead = dataCpp.ReadInterfaceToken();
std::string strTokenRead = OHOS::Str16ToStr8(u16TokenRead);
endPoint = std::chrono::steady_clock::now();
EXPECT_EQ(strTokenRead.length(), MAX_INTERFACE_TOKEN_LEN - 1);
EXPECT_EQ(strTokenRead.compare(token), 0);
duration = CalcSpendTime(startPoint, endPoint);
readResult.min = (duration > readResult.min) ? readResult.min : duration;
readResult.max = (duration < readResult.max) ? readResult.max : duration;
readResult.average += duration;
}
writeResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
readResult.average /= TEST_PERFORMANCE_OPERATOR_COUNT;
}
HWTEST_F(IpcCApiParcelUnitTest, OH_IPCParcel_Create_001, TestSize.Level1)
@ -385,6 +642,55 @@ HWTEST_F(IpcCApiParcelUnitTest, OH_IPCParcel_TestReadWriteFileDescriptor_001, Te
OH_IPCParcel_Destroy(parcel);
}
HWTEST_F(IpcCApiParcelUnitTest, OH_IPCParcel_TestReadWriteRemoteStub_001, TestSize.Level1)
{
OHIPCParcel *parcel = OH_IPCParcel_Create();
EXPECT_NE(parcel, nullptr);
const char *descriptor = "test descriptor";
OHIPCRemoteStub *stub = OH_IPCRemoteStub_Create(descriptor, OnRemoteRequestStub,
nullptr, nullptr);
EXPECT_NE(stub, nullptr);
EXPECT_EQ(OH_IPCParcel_WriteRemoteStub(parcel, nullptr), OH_IPC_CHECK_PARAM_ERROR);
EXPECT_EQ(OH_IPCParcel_WriteRemoteStub(nullptr, stub), OH_IPC_CHECK_PARAM_ERROR);
EXPECT_EQ(OH_IPCParcel_WriteRemoteStub(parcel, stub), OH_IPC_SUCCESS);
// read after write
EXPECT_EQ(OH_IPCParcel_ReadRemoteStub(nullptr), nullptr);
auto obj = OH_IPCParcel_ReadRemoteStub(parcel);
EXPECT_NE(obj, nullptr);
// destroy the objects
OH_IPCParcel_Destroy(parcel);
OH_IPCRemoteStub_Destroy(obj);
OH_IPCRemoteStub_Destroy(stub);
}
HWTEST_F(IpcCApiParcelUnitTest, OH_IPCParcel_TestReadWriteRemoteProxy_001, TestSize.Level1)
{
OHIPCParcel *parcel = OH_IPCParcel_Create();
EXPECT_NE(parcel, nullptr);
OHIPCRemoteProxy *remoteProxy = OH_IPCParcel_ReadRemoteProxy(nullptr);
EXPECT_EQ(remoteProxy, nullptr);
IPCTestHelper helper;
bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
ASSERT_TRUE(res);
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
ASSERT_NE(saMgr, nullptr);
sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
OHIPCRemoteProxy *proxy = CreateIPCRemoteProxy(object);
ASSERT_NE(proxy, nullptr);
EXPECT_EQ(OH_IPCParcel_WriteRemoteProxy(nullptr, proxy), OH_IPC_CHECK_PARAM_ERROR);
EXPECT_EQ(OH_IPCParcel_WriteRemoteProxy(parcel, nullptr), OH_IPC_CHECK_PARAM_ERROR);
EXPECT_EQ(OH_IPCParcel_WriteRemoteProxy(parcel, proxy), OH_IPC_SUCCESS);
remoteProxy = OH_IPCParcel_ReadRemoteProxy(parcel);
EXPECT_NE(remoteProxy, nullptr);
// destroy the objects
OH_IPCParcel_Destroy(parcel);
OH_IPCRemoteProxy_Destroy(proxy);
OH_IPCRemoteProxy_Destroy(remoteProxy);
res = helper.StopTestApp(IPCTestHelper::IPC_TEST_SERVER);
ASSERT_TRUE(res);
}
HWTEST_F(IpcCApiParcelUnitTest, OH_IPCParcel_Append_001, TestSize.Level1)
{
OHIPCParcel *parcel1 = OH_IPCParcel_Create();
@ -467,3 +773,67 @@ HWTEST_F(IpcCApiParcelUnitTest, OH_IPCParcel_TestReadWriteInterfaceToken_001, Te
}
OH_IPCParcel_Destroy(parcel);
}
HWTEST_F(IpcCApiParcelUnitTest, OH_IPCParcel_TestReadWriteStringPerformance_001, TestSize.Level1)
{
PerformanceResult writeResult;
PerformanceResult readResult;
ReadWriteStringPerformance(writeResult, readResult);
PerformanceResult writeCppResult;
PerformanceResult readCppResult;
ReadWriteStringCppPerformance(writeCppResult, readCppResult);
std::cout << "Test String len:" << TEST_PARCEL_SIZE << ", count:" << TEST_PERFORMANCE_OPERATOR_COUNT << std::endl;
std::cout << "OHIPCParcel WriteCString spend:[min:" << writeResult.min << ", max:" << writeResult.max
<< ", avg:" << writeResult.average << "]us" << std::endl;
std::cout << "MessageParcel WriteCString spend:[min:" << writeCppResult.min << ", max:" << writeCppResult.max
<< ", avg:" << writeCppResult.average << "]us" << std::endl;
std::cout << "OHIPCParcel ReadCString spend:[min:" << readResult.min << ", max:" << readResult.max
<< ", avg:" << readResult.average << "]us" << std::endl;
std::cout << "MessageParcel ReadCString spend:[min:" << readCppResult.min << ", max:" << readCppResult.max
<< ", avg:" << readCppResult.average << "]us" << std::endl;
}
HWTEST_F(IpcCApiParcelUnitTest, OH_IPCParcel_TestReadWriteBufferPerformance_001, TestSize.Level1)
{
PerformanceResult writeResult;
PerformanceResult readResult;
ReadWriteBufferPerformance(writeResult, readResult);
PerformanceResult writeCppResult;
PerformanceResult readCppResult;
ReadWriteBufferCppPerformance(writeCppResult, readCppResult);
std::cout << "Test Buffer len:" << TEST_PARCEL_SIZE << ", count:" << TEST_PERFORMANCE_OPERATOR_COUNT << std::endl;
std::cout << "OHIPCParcel WriteBuffer spend:[min:" << writeResult.min << ", max:" << writeResult.max
<< ", avg:" << writeResult.average << "]us" << std::endl;
std::cout << "MessageParcel WriteBuffer spend:[min:" << writeCppResult.min << ", max:" << writeCppResult.max
<< ", avg:" << writeCppResult.average << "]us" << std::endl;
std::cout << "OHIPCParcel ReadBuffer spend:[min:" << readResult.min << ", max:" << readResult.max
<< ", avg:" << readResult.average << "]us" << std::endl;
std::cout << "MessageParcel ReadBuffer spend:[min:" << readCppResult.min << ", max:" << readCppResult.max
<< ", avg:" << readCppResult.average << "]us" << std::endl;
}
HWTEST_F(IpcCApiParcelUnitTest, OH_IPCParcel_TestReadWriteInterfaceTokenPerformance_001, TestSize.Level1)
{
PerformanceResult writeResult;
PerformanceResult readResult;
ReadWriteInterfaceTokenPerformance(writeResult, readResult);
PerformanceResult writeCppResult;
PerformanceResult readCppResult;
ReadWriteInterfaceTokenCppPerformance(writeCppResult, readCppResult);
std::cout << "Test token len:" << MAX_INTERFACE_TOKEN_LEN << ", count:"
<< TEST_PERFORMANCE_OPERATOR_COUNT << std::endl;
std::cout << "OHIPCParcel WriteInterfaceToken spend:[min:" << writeResult.min << ", max:"
<< writeResult.max << ", avg:" << writeResult.average << "]us" << std::endl;
std::cout << "MessageParcel WriteInterfaceToken spend:[min:" << writeCppResult.min
<< ", max:" << writeCppResult.max << ", avg:" << writeCppResult.average << "]us" << std::endl;
std::cout << "OHIPCParcel ReadInterfaceToken spend:[min:" << readResult.min << ", max:"
<< readResult.max << ", avg:" << readResult.average << "]us" << std::endl;
std::cout << "MessageParcel ReadInterfaceToken spend:[min:" << readCppResult.min << ", max:"
<< readCppResult.max << ", avg:" << readCppResult.average << "]us" << std::endl;
}

View File

@ -21,11 +21,8 @@
#include <condition_variable>
#include "if_system_ability_manager.h"
#include "ipc_error_code.h"
#include "ipc_inner_object.h"
#include "ipc_error_code.h"
#include "ipc_cparcel.h"
#include "ipc_cremote_object.h"
#include "ipc_kit.h"
#include "ipc_test_helper.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
@ -161,6 +158,17 @@ HWTEST_F(IpcCApiRemoteObjectUnitTest, RemoteStub_Create_001, TestSize.Level1)
OH_IPCRemoteStub_Destroy(remote);
}
HWTEST_F(IpcCApiRemoteObjectUnitTest, RemoteStub_Destroy_001, TestSize.Level1)
{
OHIPCRemoteStub *remote = OH_IPCRemoteStub_Create("RemoteStub_Destroy_001", OnRemoteRequestStub,
OnRemoteObjectDestroy, this);
EXPECT_NE(remote, nullptr);
ResetCallbackReply();
OH_IPCRemoteStub_Destroy(remote);
EXPECT_EQ(GetCallbackReply(), ON_CALLBACK_REPLIED_INT);
}
HWTEST_F(IpcCApiRemoteObjectUnitTest, RemoteProxy_Destroy_001, TestSize.Level1)
{
IPCTestHelper helper;
@ -177,6 +185,55 @@ HWTEST_F(IpcCApiRemoteObjectUnitTest, RemoteProxy_Destroy_001, TestSize.Level1)
OH_IPCRemoteProxy_Destroy(remoteProxy);
}
HWTEST_F(IpcCApiRemoteObjectUnitTest, SendRequestSync_001, TestSize.Level1)
{
IPCTestHelper helper;
bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
ASSERT_TRUE(res);
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
ASSERT_NE(saMgr, nullptr);
sptr<IRemoteObject> objectServer = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
ASSERT_NE(objectServer, nullptr);
auto *remoteProxy = CreateIPCRemoteProxy(objectServer);
ASSERT_NE(remoteProxy, nullptr);
OHIPCParcel *dataParcel = OH_IPCParcel_Create();
OHIPCParcel *replyParcel = OH_IPCParcel_Create();
OH_IPC_MessageOption option = {OH_IPC_REQUEST_MODE_SYNC, 0};
int ret = OH_IPC_SendRequest(nullptr, TestCommand::TEST_CMD_GET_FOO_SERVICE, dataParcel, replyParcel, &option);
EXPECT_EQ(ret, OH_IPC_CHECK_PARAM_ERROR);
ret = OH_IPC_SendRequest(remoteProxy, TestCommand::TEST_CMD_GET_FOO_SERVICE, nullptr, replyParcel, &option);
EXPECT_EQ(ret, OH_IPC_CHECK_PARAM_ERROR);
ret = OH_IPC_SendRequest(remoteProxy, TestCommand::TEST_CMD_GET_FOO_SERVICE, dataParcel, nullptr, &option);
EXPECT_EQ(ret, OH_IPC_CHECK_PARAM_ERROR);
ret = OH_IPC_SendRequest(remoteProxy, TestCommand::TEST_CMD_GET_FOO_SERVICE, dataParcel, replyParcel, &option);
EXPECT_EQ(ret, OH_IPC_SUCCESS);
auto *fooProxy = OH_IPCParcel_ReadRemoteProxy(replyParcel);
EXPECT_NE(fooProxy, nullptr);
OH_IPCParcel_Destroy(dataParcel);
OH_IPCParcel_Destroy(replyParcel);
OH_IPCRemoteProxy_Destroy(fooProxy);
OH_IPCRemoteProxy_Destroy(remoteProxy);
}
HWTEST_F(IpcCApiRemoteObjectUnitTest, DeathRecipient_Create_001, TestSize.Level1)
{
auto deathRecipient = OH_IPCDeathRecipient_Create(nullptr, OnDeathRecipientDestroyCallback, this);
EXPECT_EQ(deathRecipient, nullptr);
deathRecipient = OH_IPCDeathRecipient_Create(OnDeathRecipientCallback, nullptr, nullptr);
EXPECT_NE(deathRecipient, nullptr);
OH_IPCDeathRecipient_Destroy(deathRecipient);
deathRecipient = OH_IPCDeathRecipient_Create(OnDeathRecipientCallback, OnDeathRecipientDestroyCallback, this);
EXPECT_NE(deathRecipient, nullptr);
OH_IPCDeathRecipient_Destroy(deathRecipient);
}
HWTEST_F(IpcCApiRemoteObjectUnitTest, DeathRecipient_Destroy_001, TestSize.Level1)
{
auto deathRecipient = OH_IPCDeathRecipient_Create(OnDeathRecipientCallback, OnDeathRecipientDestroyCallback, this);
@ -213,6 +270,37 @@ HWTEST_F(IpcCApiRemoteObjectUnitTest, RemoteProxy_AddDeathRecipient_001, TestSiz
OH_IPCRemoteProxy_Destroy(remoteProxy);
}
HWTEST_F(IpcCApiRemoteObjectUnitTest, RemoteProxy_RemoveDeathRecipient_001, TestSize.Level1)
{
IPCTestHelper helper;
bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
ASSERT_TRUE(res);
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
ASSERT_NE(saMgr, nullptr);
sptr<IRemoteObject> objectServer = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
ASSERT_NE(objectServer, nullptr);
auto *remoteProxy = CreateIPCRemoteProxy(objectServer);
ASSERT_NE(remoteProxy, nullptr);
auto deathRecipient = OH_IPCDeathRecipient_Create(OnDeathRecipientCallback, OnDeathRecipientDestroyCallback,
this);
ASSERT_NE(deathRecipient, nullptr);
int ret = OH_IPCRemoteProxy_AddDeathRecipient(remoteProxy, deathRecipient);
ASSERT_EQ(ret, 0);
ret = OH_IPCRemoteProxy_RemoveDeathRecipient(remoteProxy, deathRecipient);
EXPECT_EQ(ret, 0);
ResetCallbackReply();
res = helper.StopTestApp(IPCTestHelper::IPC_TEST_SERVER);
ASSERT_TRUE(res);
EXPECT_NE(GetCallbackReply(), ON_CALLBACK_REPLIED_INT);
OH_IPCDeathRecipient_Destroy(deathRecipient);
OH_IPCRemoteProxy_Destroy(remoteProxy);
}
HWTEST_F(IpcCApiRemoteObjectUnitTest, IsRemoteDead_001, TestSize.Level1)
{
IPCTestHelper helper;
@ -261,4 +349,4 @@ HWTEST_F(IpcCApiRemoteObjectUnitTest, GetInterfaceDescriptor_001, TestSize.Level
free(descriptor);
}
OH_IPCRemoteProxy_Destroy(remoteProxy);
}
}

View File

@ -0,0 +1,265 @@
/*
* Copyright (C) 2024 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 <cstring>
#include <securec.h>
#include "ipc_cparcel.h"
#include "ipc_cremote_object.h"
#include "ipc_cskeleton.h"
#include "ipc_test_helper.h"
#include "test_service_command.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
#include "ipc_inner_object.h"
#include "ipc_skeleton.h"
#include "ipc_error_code.h"
#define private public
#define protected public
#include "comm_auth_info.h"
#include "dbinder_databus_invoker.h"
#include "dbinder_session_object.h"
#include "binder_invoker.h"
#include "ipc_debug.h"
#include "ipc_skeleton.h"
#include "ipc_object_proxy.h"
#include "ipc_object_stub.h"
#include "ipc_process_skeleton.h"
#include "ipc_thread_skeleton.h"
#include "dbinder_session_object.h"
#include "message_option.h"
#include "mock_iremote_invoker.h"
#undef protected
#undef private
#include <iostream>
#include <thread>
using namespace std;
using namespace testing::ext;
using namespace OHOS;
using namespace OHOS::HiviewDFX;
using namespace testing::ext;
static constexpr int MAX_MEMORY_SIZE = 204800;
class IpcCApiSkeletonUnitTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, OHOS::LOG_ID_IPC_CAPI, "IpcCApiUnitTest" };
};
void IpcCApiSkeletonUnitTest::SetUpTestCase()
{}
void IpcCApiSkeletonUnitTest::TearDownTestCase()
{}
void IpcCApiSkeletonUnitTest::SetUp()
{}
void IpcCApiSkeletonUnitTest::TearDown()
{}
static void* LocalMemAllocator(int32_t len)
{
if (len < 0 || len > MAX_MEMORY_SIZE) {
return nullptr;
}
void *buffer = malloc(len);
if (buffer == nullptr) {
return nullptr;
}
(void)memset_s(buffer, len, 0, len);
return buffer;
}
static void* LocalMemAllocatorErr(int32_t len)
{
return nullptr;
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_JoinWorkThread_001, TestSize.Level1)
{
MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
current->invokers_[IRemoteObject::IF_PROT_DEFAULT] = invoker;
EXPECT_CALL(*invoker, GetStatus())
.WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
OH_IPCSkeleton_JoinWorkThread();
ASSERT_TRUE(IPCThreadSkeleton::GetCurrent() != nullptr);
current->invokers_.clear();
delete invoker;
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_StopWorkThread_001, TestSize.Level1)
{
MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
current->invokers_[IRemoteObject::IF_PROT_DEFAULT] = invoker;
EXPECT_CALL(*invoker, GetStatus())
.WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
OH_IPCSkeleton_StopWorkThread();
ASSERT_TRUE(IPCThreadSkeleton::GetCurrent() != nullptr);
current->invokers_.clear();
delete invoker;
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetCallingTokenId_001, TestSize.Level1)
{
uint64_t tokenId = 1213;
MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
EXPECT_CALL(*invoker, GetStatus())
.WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
EXPECT_CALL(*invoker, GetCallerTokenID())
.WillRepeatedly(testing::Return(tokenId));
auto result = OH_IPCSkeleton_GetCallingTokenId();
EXPECT_EQ(result, tokenId);
current->invokers_.clear();
delete invoker;
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetFirstTokenId_001, TestSize.Level1)
{
auto id = OH_IPCSkeleton_GetFirstTokenId();
EXPECT_EQ(id, 0);
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetSelfTokenId_001, TestSize.Level1)
{
auto id = OH_IPCSkeleton_GetSelfTokenId();
EXPECT_GT(id, 0);
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetCallingPid_001, TestSize.Level1)
{
auto id = OH_IPCSkeleton_GetCallingPid();
EXPECT_NE(id, 0);
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_GetCallingUid_001, TestSize.Level1)
{
auto uid = OH_IPCSkeleton_GetCallingUid();
EXPECT_EQ(uid, 0);
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_IsLocalCalling_001, TestSize.Level1)
{
EXPECT_EQ(OH_IPCSkeleton_IsLocalCalling(), true);
MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
EXPECT_CALL(*invoker, GetStatus())
.WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
EXPECT_CALL(*invoker, IsLocalCalling())
.WillRepeatedly(testing::Return(false));
EXPECT_EQ(OH_IPCSkeleton_IsLocalCalling(), false);
current->invokers_.clear();
delete invoker;
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_SetCallingIdentity_001, TestSize.Level1)
{
EXPECT_NE(OH_IPCSkeleton_SetCallingIdentity(nullptr), OH_IPC_SUCCESS);
MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
EXPECT_CALL(*invoker, GetStatus())
.WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
std::string testStr = "hello, world.";
EXPECT_CALL(*invoker, SetCallingIdentity(testStr))
.WillRepeatedly(testing::Return(true));
auto ret = OH_IPCSkeleton_SetCallingIdentity(testStr.c_str());
EXPECT_EQ(ret, OH_IPC_SUCCESS);
current->invokers_.clear();
delete invoker;
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_ResetCallingIdentity_001, TestSize.Level1)
{
char *identity = nullptr;
int32_t len = sizeof(identity);
EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(nullptr, &len, LocalMemAllocator), OH_IPC_CHECK_PARAM_ERROR);
EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, nullptr, LocalMemAllocator), OH_IPC_CHECK_PARAM_ERROR);
EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, &len, nullptr), OH_IPC_CHECK_PARAM_ERROR);
EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, &len, LocalMemAllocatorErr), OH_IPC_MEM_ALLOCATOR_ERROR);
MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
EXPECT_CALL(*invoker, GetStatus())
.WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
std::string testStr = "testStr";
EXPECT_CALL(*invoker, ResetCallingIdentity())
.WillRepeatedly(testing::Return(testStr));
EXPECT_EQ(OH_IPCSkeleton_ResetCallingIdentity(&identity, &len, LocalMemAllocator), OH_IPC_SUCCESS);
ZLOGE(LOG_LABEL, "identity is %{public}s, len is %{public}d", identity, len);
EXPECT_STREQ(identity, testStr.c_str());
EXPECT_EQ(len, strlen(testStr.c_str()) + 1);
if (identity != nullptr) {
delete identity;
}
current->invokers_.clear();
delete invoker;
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_IsHandlingTransaction_001, TestSize.Level1)
{
EXPECT_EQ(OH_IPCSkeleton_IsHandlingTransaction(), false);
MockIRemoteInvoker *invoker = new MockIRemoteInvoker();
IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
current->invokers_[IRemoteObject::IF_PROT_BINDER] = invoker;
EXPECT_CALL(*invoker, GetStatus())
.WillRepeatedly(testing::Return(IRemoteInvoker::ACTIVE_INVOKER));
EXPECT_EQ(OH_IPCSkeleton_IsHandlingTransaction(), true);
current->invokers_.clear();
delete invoker;
}
HWTEST_F(IpcCApiSkeletonUnitTest, Skeleton_SetMaxWorkThreadNum_001, TestSize.Level1)
{
int normalCount = 4;
int negativeCount = -1;
int maxCount = 33;
EXPECT_EQ(OH_IPCSkeleton_SetMaxWorkThreadNum(normalCount), OH_IPC_SUCCESS);
EXPECT_EQ(OH_IPCSkeleton_SetMaxWorkThreadNum(negativeCount), OH_IPC_CHECK_PARAM_ERROR);
EXPECT_EQ(OH_IPCSkeleton_SetMaxWorkThreadNum(maxCount), OH_IPC_CHECK_PARAM_ERROR);
}

View File

@ -33,6 +33,8 @@ ohos_shared_library("ipc_test_helper") {
"$SUBSYSTEM_DIR/config:ipc_util_config",
]
deps = [ "$SUBSYSTEM_DIR/interfaces/innerkits/c_api:ipc_ndk" ]
external_deps = [
"access_token:libnativetoken",
"access_token:libtoken_setproc",
@ -78,7 +80,10 @@ ohos_executable("ipc_client_test") {
"$IPC_TEST_ROOT:ipc_test_config",
]
deps = [ ":ipc_test_helper" ]
deps = [
":ipc_test_helper",
"$SUBSYSTEM_DIR/interfaces/innerkits/c_api:ipc_ndk",
]
external_deps = [
"access_token:libnativetoken",
@ -112,6 +117,8 @@ ohos_shared_library("ipc_test_helper_extra") {
"$SUBSYSTEM_DIR/config:ipc_util_config",
]
deps = [ "$SUBSYSTEM_DIR/interfaces/innerkits/c_api:ipc_ndk" ]
external_deps = [
"access_token:libnativetoken",
"access_token:libtoken_setproc",

View File

@ -51,9 +51,14 @@ public:
int TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src,
MessageParcel &reply, bool withObject) override;
int TestEnableSerialInvokeFlag() override;
int TestRegisterRemoteStub(const char *descriptor, const sptr<IRemoteObject> object) override;
int TestUnRegisterRemoteStub(const char *descriptor) override;
sptr<IRemoteObject> TestQueryRemoteProxy(const char *descriptor) override;
private:
int testFd_;
std::mutex remoteObjectsMutex_;
std::map<std::string, sptr<IRemoteObject>> remoteObjects_;
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TestService" };
};
} // namespace OHOS
#endif // OHOS_IPC_TEST_SERVICE_H
#endif // OHOS_IPC_TEST_SERVICE_H

View File

@ -28,5 +28,7 @@ enum TestCommand {
TEST_CMD_LOOP_TRANSACTION = 9,
TEST_CMD_ASYNC_DUMP_SERVICE = 10,
TEST_CMD_ENABLE_SERIAL_INVOKE_FLAG = 11,
TEST_CMD_NATIVE_IPC_REGISTER_REMOTE_STUB_OBJECT = 12,
TEST_CMD_NATIVE_IPC_REQUESTS = 13,
};
#endif // OHOS_IPC_TEST_SERVICE_COMMAND_H

View File

@ -17,6 +17,7 @@
#define OHOS_TEST_SERVICE_SKELETON_H
#include <map>
#include <iostream>
#include "ipc_debug.h"
#include "iremote_broker.h"
#include "iremote_stub.h"
@ -52,6 +53,9 @@ public:
TRANS_MESSAGE_PARCEL_ADDPED_WITH_OBJECT = 20,
TRANS_ID_ACCESS_TOKENID_64 = 21,
TRANS_ENABLE_SERIAL_INVOKE_FLAG = 22,
TRANS_ID_REGISTER_REMOTE_STUB_OBJECT = 23,
TRANS_ID_UNREGISTER_REMOTE_STUB_OBJECT = 24,
TRANS_ID_QUERY_REMOTE_PROXY_OBJECT = 25,
};
public:
virtual int TestSyncTransaction(int data, int &reply, int delayTime = 0) = 0;
@ -77,6 +81,11 @@ public:
virtual int TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src,
MessageParcel &reply, bool withObject) = 0;
virtual int TestEnableSerialInvokeFlag() = 0;
virtual int TestRegisterRemoteStub(const char *descriptor, const sptr<IRemoteObject> object) = 0;
virtual int TestUnRegisterRemoteStub(const char *descriptor) = 0;
virtual sptr<IRemoteObject> TestQueryRemoteProxy(const char *descriptor) = 0;
public:
DECLARE_INTERFACE_DESCRIPTOR(u"test.ipc.ITestService");
};
@ -140,6 +149,11 @@ public:
int TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src,
MessageParcel &reply, bool withObject) override;
int TestEnableSerialInvokeFlag() override;
int TestRegisterRemoteStub(const char *descriptor, const sptr<IRemoteObject> object) override;
int TestUnRegisterRemoteStub(const char *descriptor) override;
sptr<IRemoteObject> TestQueryRemoteProxy(const char *descriptor) override;
private:
static inline BrokerDelegator<TestServiceProxy> delegator_;
bool CheckTokenSelf(uint64_t token, uint64_t tokenSelf, uint64_t ftoken, uint64_t ftoken_expected);

View File

@ -16,6 +16,7 @@
#include "test_service.h"
#include <fcntl.h>
#include <cstring>
#include <iostream>
#include <unistd.h>
@ -288,4 +289,37 @@ int TestService::TestEnableSerialInvokeFlag()
{
return 0;
}
int TestService::TestRegisterRemoteStub(const char *descriptor, const sptr<IRemoteObject> object)
{
if (descriptor == nullptr || strlen(descriptor) < 1 || object == nullptr) {
return -1;
}
std::lock_guard<std::mutex> lockGuard(remoteObjectsMutex_);
remoteObjects_.emplace(descriptor, object);
return 0;
}
int TestService::TestUnRegisterRemoteStub(const char *descriptor)
{
if (descriptor == nullptr || strlen(descriptor) < 1) {
return -1;
}
std::lock_guard<std::mutex> lockGuard(remoteObjectsMutex_);
remoteObjects_.erase(descriptor);
return 0;
}
sptr<IRemoteObject> TestService::TestQueryRemoteProxy(const char *descriptor)
{
if (descriptor == nullptr || strlen(descriptor) < 1) {
return nullptr;
}
auto data = remoteObjects_.find(descriptor);
if (data != remoteObjects_.end()) {
return data->second;
}
return nullptr;
}
} // namespace OHOS

View File

@ -24,6 +24,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <thread>
#include <cstring>
#include "access_token_adapter.h"
#include "ipc_debug.h"
#include "string_ex.h"
@ -328,6 +329,50 @@ int TestServiceProxy::TestCallingUidPid()
return -1;
}
int TestServiceProxy::TestRegisterRemoteStub(const char *descriptor, const sptr<IRemoteObject> object)
{
MessageOption option;
MessageParcel dataParcel;
MessageParcel replyParcel;
dataParcel.WriteString(descriptor);
dataParcel.WriteRemoteObject(object);
int ret = Remote()->SendRequest(TRANS_ID_REGISTER_REMOTE_STUB_OBJECT, dataParcel, replyParcel, option);
if (ret != ERR_NONE) {
ZLOGE(LABEL, "ret = %{public}d", ret);
return ret;
}
return 0;
}
int TestServiceProxy::TestUnRegisterRemoteStub(const char *descriptor)
{
MessageOption option;
MessageParcel dataParcel;
MessageParcel replyParcel;
dataParcel.WriteString(descriptor);
int ret = Remote()->SendRequest(TRANS_ID_UNREGISTER_REMOTE_STUB_OBJECT, dataParcel, replyParcel, option);
if (ret != ERR_NONE) {
ZLOGE(LABEL, "ret = %{public}d", ret);
return ret;
}
return 0;
}
sptr<IRemoteObject> TestServiceProxy::TestQueryRemoteProxy(const char *descriptor)
{
MessageOption option;
MessageParcel dataParcel;
MessageParcel replyParcel;
dataParcel.WriteString(descriptor);
int ret = Remote()->SendRequest(TRANS_ID_QUERY_REMOTE_PROXY_OBJECT, dataParcel, replyParcel, option);
if (ret != ERR_NONE) {
ZLOGE(LABEL, "ret = %{public}d", ret);
return nullptr;
}
auto readRemoteObject = replyParcel.ReadRemoteObject();
return readRemoteObject;
}
constexpr char ACCESS_TOKEN_ID_IOCTL_BASE = 'A';
enum {