mirror of
https://gitee.com/openharmony/communication_ipc
synced 2025-03-02 13:48:44 +00:00
ipc:add new interface of MessageParcelAppend
Signed-off-by: liubb_0516 <liubeibei8@huawei.com>
This commit is contained in:
parent
00b47491a5
commit
4e39b19177
@ -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,9 @@ 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" };
|
||||
|
@ -47,6 +47,8 @@ public:
|
||||
TRANS_ID_ASYNC_DUMP_SERVICE = 16,
|
||||
TRANS_ID_NESTING_SEND = 17,
|
||||
TRANS_ID_ACCESS_TOKENID = 18,
|
||||
TRANS_MESSAGE_PARCEL_ADDPED = 19,
|
||||
TRANS_MESSAGE_PARCEL_ADDPED_WITH_OBJECT = 20,
|
||||
};
|
||||
public:
|
||||
virtual int TestSyncTransaction(int data, int &reply, int delayTime = 0) = 0;
|
||||
@ -67,6 +69,9 @@ public:
|
||||
virtual void TestAsyncDumpService() = 0;
|
||||
virtual int TestNestingSend(int sendCode, int &replyCode) = 0;
|
||||
virtual int TestAccessTokenID(int32_t ftoken_expected) = 0;
|
||||
virtual int TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src) = 0;
|
||||
virtual int TestMessageParcelAppendWithIpc(MessageParcel &dst, MessageParcel &src,
|
||||
MessageParcel &reply, bool withObject) = 0;
|
||||
public:
|
||||
DECLARE_INTERFACE_DESCRIPTOR(u"test.ipc.ITestService");
|
||||
};
|
||||
@ -105,6 +110,9 @@ 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:
|
||||
static inline BrokerDelegator<TestServiceProxy> delegator_;
|
||||
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "TestServiceProxy" };
|
||||
|
@ -192,6 +192,23 @@ 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,34 @@ 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 +674,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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user