提交 ba943bea 编写于 作者: L liubb_0516

ipc:add new interface of MessageParcelAppend

Signed-off-by: Nliubb_0516 <liubeibei8@huawei.com>
上级 00b47491
......@@ -50,6 +50,7 @@ public:
{
needCloseFd_ = true;
};
bool MessageParcelAppend(MessageParcel &data);
private:
#ifndef CONFIG_IPC_SINGLE
......
......@@ -422,4 +422,38 @@ sptr<Ashmem> MessageParcel::ReadAshmem()
}
return new (std::nothrow) Ashmem(fd, size);
}
bool MessageParcel::MessageParcelAppend(MessageParcel &data)
{
size_t dataSize = data.GetDataSize();
if (dataSize == 0) {
DBINDER_LOGE("no data to append");
return false;
}
uintptr_t dataPtr = data.GetData();
size_t writeCursorOld = this->GetWritePosition();
if (!WriteBuffer(reinterpret_cast<void *>(dataPtr), dataSize)) {
DBINDER_LOGE("data append write buffer failed");
return false;
}
size_t objectSize = data.GetOffsetsSize();
if (objectSize == 0) {
return true;
}
binder_size_t objectOffsets = data.GetObjectOffsets();
auto *newObjectOffsets = reinterpret_cast<binder_size_t *>(objectOffsets);
for (size_t index = 0; index < objectSize; index++) {
if (EnsureObjectsCapacity()) {
bool res = WriteObjectOffset(writeCursorOld + newObjectOffsets[index]);
if (!res) {
DBINDER_LOGE("parcel append write offsets failed");
return false;
}
} else {
DBINDER_LOGE("Failed to ensure parcel capacity");
return false;
}
}
return true;
}
} // namespace OHOS
......@@ -46,6 +46,8 @@ public:
void TestAsyncDumpService() override;
int TestNestingSend(int sendCode, int &replyCode) override;
int TestAccessTokenID(int32_t ftoken_expected) override;
int TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src) override;
int TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src, MessageParcel &reply, bool withObject) override;
private:
int testFd_;
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "TestService" };
......
......@@ -192,6 +192,22 @@ int TestService::TestAccessTokenID(int32_t ftoken_expected)
return 0;
}
int TestService::TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src)
{
(void)dst;
(void)src;
return 0;
}
int TestService::TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src, MessageParcel &reply, bool withObject)
{
(void)dst;
(void)src;
(void)reply;
(void)withObject;
return 0;
}
int TestService::TestFlushAsyncCalls(int count, int length)
{
return 0;
......
......@@ -426,6 +426,33 @@ int TestServiceProxy::TestAccessTokenID(int32_t ftoken_expected)
return 0;
}
int TestServiceProxy::TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src)
{
bool res = dst.MessageParcelAppend(src);
if (!res) {
ZLOGE(LABEL, "TestMessageParcelAppend without ipc failed");
return -1;
}
return 0;
}
int TestServiceProxy::TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src, MessageParcel &reply, bool withObject)
{
bool res = dst.MessageParcelAppend(src);
if (!res) {
ZLOGE(LABEL, "TestMessageParcelAppend with ipc failed");
return -1;
}
MessageOption option;
uint32_t code = TRANS_MESSAGE_PARCEL_ADDPED;
if (withObject) {
code = TRANS_MESSAGE_PARCEL_ADDPED_WITH_OBJECT;
}
int ret = Remote()->SendRequest(code, dst, reply, option);
ZLOGE(LABEL, "TestMessageParcelAppend with ipc sendrequest ret = %{public}d", ret);
return ret;
}
int TestServiceProxy::TestFlushAsyncCalls(int count, int length)
{
int ret;
......@@ -646,6 +673,19 @@ int TestServiceStub::OnRemoteRequest(uint32_t code,
reply.WriteInt32(ftoken);
break;
}
case TRANS_MESSAGE_PARCEL_ADDPED: {
reply.WriteInt32(data.ReadInt32());
reply.WriteInt32(data.ReadInt32());
reply.WriteString(data.ReadString());
break;
}
case TRANS_MESSAGE_PARCEL_ADDPED_WITH_OBJECT: {
reply.WriteInt32(data.ReadInt32());
reply.WriteInt32(data.ReadInt32());
reply.WriteString(data.ReadString());
reply.WriteRemoteObject(data.ReadRemoteObject());
break;
}
default:
ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
break;
......
......@@ -27,6 +27,8 @@
#include "system_ability_definition.h"
#include "ipc_object_proxy.h"
#include "foo_service.h"
#include "log_tags.h"
using namespace testing::ext;
......@@ -562,6 +564,7 @@ HWTEST_F(IPCNativeFrameworkTest, function_test_018, TestSize.Level1)
}
}
#ifndef CONFIG_STANDARD_SYSTEM
/**
* @tc.name: function_test_019
......@@ -738,3 +741,119 @@ HWTEST_F(IPCNativeFrameworkTest, function_test_024, TestSize.Level1)
thread->join();
}
}
/**
* @tc.name: function_test_025
* @tc.desc: Test messageparcel append in same process
* @tc.type: FUNC
* @tc.require: AR000H0FUK
*/
HWTEST_F(IPCNativeFrameworkTest, function_test_025, TestSize.Level1)
{
IPCTestHelper helper;
bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
ASSERT_TRUE(res);
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
ASSERT_TRUE(saMgr != nullptr);
sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
ASSERT_TRUE(object != nullptr);
sptr<ITestService> testService = iface_cast<ITestService>(object);
ASSERT_TRUE(testService != nullptr);
MessageParcel dstParcel, srcParcel;
int ret = testService->TestMessageParcelAppend(dstParcel, srcParcel);
EXPECT_EQ(ret, -1);
const int32_t num = 5767168;
dstParcel.WriteInt32(num);
srcParcel.WriteInt32(num);
const std::string strwrite1 =
"test for write string padded**********************************************************##################";
srcParcel.WriteString(strwrite1);
sptr<FooStub> fooCallback = new FooStub();
srcParcel.WriteRemoteObject(fooCallback->AsObject());
ret = testService->TestMessageParcelAppend(dstParcel, srcParcel);
EXPECT_EQ(ret, 0);
EXPECT_EQ(num, dstParcel.ReadInt32());
EXPECT_EQ(num, dstParcel.ReadInt32());
EXPECT_EQ(strwrite1, dstParcel.ReadString());
res = dstParcel.ReadRemoteObject();
ASSERT_TRUE(res);
}
/**
* @tc.name: function_test_026
* @tc.desc: Test messageparcel append with ipc
* @tc.type: FUNC
* @tc.require: AR000H0FUK
*/
HWTEST_F(IPCNativeFrameworkTest, function_test_026, TestSize.Level1)
{
IPCTestHelper helper;
bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
ASSERT_TRUE(res);
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
ASSERT_TRUE(saMgr != nullptr);
sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
ASSERT_TRUE(object != nullptr);
sptr<ITestService> testService = iface_cast<ITestService>(object);
ASSERT_TRUE(testService != nullptr);
MessageParcel dstParcel, srcParcel, reply;
const int32_t num = 5767168;
dstParcel.WriteInt32(num);
srcParcel.WriteInt32(num);
const std::string strwrite1 =
"test for write string padded**********************************************************##################";
srcParcel.WriteString(strwrite1);
int ret = testService->TestMessageParcelAppendWithIpc(dstParcel, srcParcel, reply, false);
EXPECT_EQ(ret, 0);
EXPECT_EQ(num, reply.ReadInt32());
EXPECT_EQ(num, reply.ReadInt32());
EXPECT_EQ(strwrite1, reply.ReadString());
}
/**
* @tc.name: function_test_027
* @tc.desc: Test messageparcel append with ipc
* @tc.type: FUNC
* @tc.require: AR000H0FUK
*/
HWTEST_F(IPCNativeFrameworkTest, function_test_027, TestSize.Level1)
{
IPCTestHelper helper;
bool res = helper.StartTestApp(IPCTestHelper::IPC_TEST_SERVER);
ASSERT_TRUE(res);
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
ASSERT_TRUE(saMgr != nullptr);
sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
ASSERT_TRUE(object != nullptr);
sptr<ITestService> testService = iface_cast<ITestService>(object);
ASSERT_TRUE(testService != nullptr);
MessageParcel dstParcel, srcParcel, reply;
const int32_t num = 5767168;
dstParcel.WriteInt32(num);
srcParcel.WriteInt32(num);
const std::string strwrite1 =
"test for write string padded**********************************************************##################";
srcParcel.WriteString(strwrite1);
sptr<FooStub> fooCallback = new FooStub();
srcParcel.WriteRemoteObject(fooCallback->AsObject());
int ret = testService->TestMessageParcelAppendWithIpc(dstParcel, srcParcel, reply, true);
EXPECT_EQ(ret, 0);
EXPECT_EQ(num, reply.ReadInt32());
EXPECT_EQ(num, reply.ReadInt32());
EXPECT_EQ(strwrite1, reply.ReadString());
res = reply.ReadRemoteObject();
ASSERT_TRUE(res);
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册