!1468 【IPC/RPC】场景测试用例优化

Merge pull request !1468 from chenchong_666/master
This commit is contained in:
openharmony_ci 2024-10-30 03:45:37 +00:00 committed by Gitee
commit 93c2ecc111
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
21 changed files with 1282 additions and 991 deletions

View File

@ -28,7 +28,7 @@
#include "ipc_object_stub.h"
#include "ipc_process_skeleton.h"
#include "ipc_thread_skeleton.h"
#include "test_service_skeleton.h"
#include "test_service_proxy.h"
#include "test_service.h"
#include "test_service_command.h"
#include "test_service_client.h"
@ -618,11 +618,11 @@ HWTEST_F(IPCNativeUnitTest, SyncTransaction004, TestSize.Level1)
ASSERT_TRUE(res);
std::unique_ptr<TestServiceClient> testClient = std::make_unique<TestServiceClient>();
int result = testClient->ConnectService();
ASSERT_EQ(result, 0);
bool result = testClient->ConnectService();
ASSERT_EQ(result, true);
int count = testClient->StartLoopTest(MAX_TEST_COUNT);
EXPECT_EQ(count, MAX_TEST_COUNT);
result = testClient->StartLoopTest(MAX_TEST_COUNT);
ASSERT_EQ(result, true);
}
/**

View File

@ -26,7 +26,7 @@
#include "hitrace/trace.h"
#include "ipc_skeleton.h"
#include "ipc_object_proxy.h"
#include "test_service_skeleton.h"
#include "test_service_proxy.h"
#include "test_service.h"
#include "ipc_test_helper.h"
#include "binder_connector.h"

View File

@ -26,7 +26,9 @@ ohos_shared_library("ipc_test_helper") {
"./src/test_capi_skeleton.cpp",
"./src/test_service.cpp",
"./src/test_service_client.cpp",
"./src/test_service_skeleton.cpp",
"./src/test_service_death_recipient.cpp",
"./src/test_service_proxy.cpp",
"./src/test_service_stub.cpp",
]
configs = [
@ -109,7 +111,9 @@ ohos_shared_library("ipc_test_helper_extra") {
"./src/test_capi_skeleton.cpp",
"./src/test_service.cpp",
"./src/test_service_client.cpp",
"./src/test_service_skeleton.cpp",
"./src/test_service_death_recipient.cpp",
"./src/test_service_proxy.cpp",
"./src/test_service_stub.cpp",
]
cflags = [ "-DIPCSERVERTESTEXTRA" ]

View File

@ -17,7 +17,9 @@
#define OHOS_TEST_CAPI_SKELETON_H
#include "ipc_cremote_object.h"
#include "test_service_skeleton.h"
#include "test_service_death_recipient.h"
#include "test_service_proxy.h"
#include "test_service_stub.h"
#include <random>

View File

@ -18,7 +18,7 @@
#include "hilog/log.h"
#include "log_tags.h"
#include "test_service_skeleton.h"
#include "test_service_stub.h"
namespace OHOS {
@ -27,14 +27,14 @@ public:
TestService(bool serialInvokeFlag = false);
~TestService();
static int Instantiate(bool isEnableSerialInvokeFlag);
int TestSyncTransaction(int data, int &rep, int delayTime = 0) override;
int TestSyncTransaction(int value, int &rep, int delayTime = 0) override;
int TestAsyncTransaction(int data, int timeout = 0) override;
int TestAsyncCallbackTrans(int data, int &reply, int timeout) override;
int TestPingService(const std::u16string &serviceName) override;
int TestGetFileDescriptor() override;
int TestStringTransaction(const std::string& data) override;
int TestZtraceTransaction(std::string& send, std::string& reply, int len) override;
void TestDumpService() override;
int TestDumpService() override;
int TestRawDataTransaction(int length, int &reply) override;
int TestRawDataReply(int length) override;
sptr<IFoo> TestGetFooService() override;
@ -43,7 +43,6 @@ public:
int TestFlushAsyncCalls(int count, int length) override;
int TestMultipleProcesses(int data, int &rep, int delayTime) override;
std::u16string TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize) override;
void TestAsyncDumpService() override;
int TestNestingSend(int sendCode, int &replyCode) override;
int TestAccessTokenID(int32_t ftoken_expected) override;
int TestAccessTokenID64(uint64_t token_expected, uint64_t ftoken_expected) override;

View File

@ -0,0 +1,94 @@
/*
* 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_TEST_SERVICE_SKELETON_H
#define OHOS_TEST_SERVICE_SKELETON_H
#include <map>
#include "ipc_debug.h"
#include "iremote_broker.h"
#include "iremote_stub.h"
#include "iremote_proxy.h"
#include "foo_service.h"
#include "ipc_file_descriptor.h"
#include "log_tags.h"
namespace OHOS {
class ITestService : public IRemoteBroker {
public:
enum {
TRANS_ID_SYNC_TRANSACTION = 1,
TRANS_ID_ASYNC_TRANSACTION = 2,
TRANS_ID_PING_SERVICE = 3,
TRANS_ID_GET_FOO_SERVICE = 4,
TRANS_ID_TRANSACT_FILE_DESC = 5,
TRANS_ID_STRING_TRANSACTION = 6,
TRANS_ID_ZTRACE_TRANSACTION = 7,
TRANS_ID_LOOP_TRANSACTION = 8,
TRANS_ID_DUMP_SERVICE = 9,
TRANS_ID_RAWDATA_TRANSACTION = 10,
TRANS_ID_RAWDATA_REPLY = 11,
TRANS_ID_CALLING_UID_PID = 12,
TRANS_ID_FLUSH_ASYNC_CALLS = 13,
TRANS_ID_MULTIPLE_PROCESSES = 14,
TRANS_ID_ASHMEM = 15,
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,
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 value, int &reply, int delayTime = 0) = 0;
virtual int TestAsyncTransaction(int data, int timeout = 0) = 0;
virtual int TestAsyncCallbackTrans(int data, int &reply, int timeout = 0) = 0;
virtual int TestGetFileDescriptor() = 0;
virtual int TestPingService(const std::u16string &serviceName) = 0;
virtual int TestStringTransaction(const std::string &data) = 0;
virtual int TestZtraceTransaction(std::string &send, std::string &reply, int len) = 0;
virtual int TestDumpService() = 0;
virtual int TestRawDataTransaction(int length, int &reply) = 0;
virtual int TestRawDataReply(int length) = 0;
virtual sptr<IFoo> TestGetFooService() = 0;
virtual int TestCallingUidPid() = 0;
virtual int TestFlushAsyncCalls(int count, int length) = 0;
virtual int TestMultipleProcesses(int data, int &rep, int delayTime) = 0;
virtual std::u16string TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize) = 0;
virtual int TestNestingSend(int sendCode, int &replyCode) = 0;
virtual int TestAccessTokenID(int32_t ftoken_expected) = 0;
virtual int TestAccessTokenID64(uint64_t token_expected, uint64_t ftoken_expected) = 0;
virtual int TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src) = 0;
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;
virtual int TestSendTooManyRequest(int data, int &reply) = 0;
virtual int TestMultiThreadSendRequest(int data, int &reply) = 0;
public:
DECLARE_INTERFACE_DESCRIPTOR(u"test.ipc.ITestService");
};
} // namespace OHOS
#endif // OHOS_TEST_SERVICE_SKELETON_H

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Copyright (C) 2021-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
@ -16,10 +16,10 @@
#ifndef OHOS_IPC_TEST_SERVICE_CLIENT_H
#define OHOS_IPC_TEST_SERVICE_CLIENT_H
#include <vector>
#include <string>
#include "test_service_skeleton.h"
#include <vector>
#include "log_tags.h"
#include "test_service_proxy.h"
#include "test_capi_skeleton.h"
namespace OHOS {
@ -31,22 +31,20 @@ struct TraceParam {
class TestServiceClient {
public:
int ConnectService();
void StartSyncTransaction();
void StartSyncDelayReply();
void StartAsyncTransaction();
void StartPingService();
void StartGetFooService();
int StartLoopTest(int maxCount);
void StartDumpService();
void StartTestFileDescriptor();
void StartAsyncDumpService();
void TestEnableSerialInvokeFlag();
void TestNativeIPCSendRequests(int subCmd);
void TestRegisterRemoteStub();
void TestUnRegisterRemoteStub();
void TestSendTooManyRequest();
void TestMultiThreadSendRequest();
bool ConnectService();
bool StartSyncTransaction();
bool StartPingService();
bool StartGetFooService();
bool StartLoopTest(int maxCount);
bool StartDumpService();
bool StartTestFileDescriptor();
bool StartAsyncDumpService();
bool TestEnableSerialInvokeFlag();
bool TestNativeIPCSendRequests(int subCmd);
bool TestRegisterRemoteStub();
bool TestUnRegisterRemoteStub();
bool TestSendTooManyRequest();
bool TestMultiThreadSendRequest();
private:
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TestServiceClient" };

View File

@ -33,4 +33,4 @@ enum TestCommand {
TEST_CMD_TOO_MANY_SENDREQUEST = 14,
TEST_CMD_MULTI_THREAD_SEND = 15,
};
#endif // OHOS_IPC_TEST_SERVICE_COMMAND_H
#endif // OHOS_IPC_TEST_SERVICE_COMMAND_H

View File

@ -0,0 +1,34 @@
/*
* 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.
*/
#ifndef OHOS_TEST_SERVICE_DEATH_RECIPIENT_H
#define OHOS_TEST_SERVICE_DEATH_RECIPIENT_H
#include "test_service_base.h"
namespace OHOS {
class TestDeathRecipient : public IRemoteObject::DeathRecipient {
public:
virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
TestDeathRecipient();
virtual ~TestDeathRecipient();
static bool GotDeathRecipient();
static bool gotDeathRecipient_;
private:
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TestDeathRecipient" };
};
} // namespace OHOS
#endif // OHOS_TEST_SERVICE_DEATH_RECIPIENT_H

View File

@ -0,0 +1,64 @@
/*
* 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.
*/
#ifndef OHOS_TEST_SERVICE_PROXY_H
#define OHOS_TEST_SERVICE_PROXY_H
#include "test_service_base.h"
namespace OHOS {
class TestServiceProxy : public IRemoteProxy<ITestService> {
public:
explicit TestServiceProxy(const sptr<IRemoteObject> &impl);
~TestServiceProxy() = default;
int TestSyncTransaction(int value, int &reply, int delayTime = 0) override;
int TestAsyncTransaction(int data, int timeout = 0) override;
int TestAsyncCallbackTrans(int data, int &reply, int timeout = 0) override;
int TestPingService(const std::u16string &serviceName) override;
int TestGetFileDescriptor() override;
int TestStringTransaction(const std::string &data) override;
int TestZtraceTransaction(std::string &send, std::string &reply, int len) override;
int TestDumpService() override;
int TestRawDataTransaction(int length, int &reply) override;
int TestRawDataReply(int length) override;
sptr<IFoo> TestGetFooService() override;
int TestCallingUidPid() override;
int TestFlushAsyncCalls(int count, int length) override;
int TestMultipleProcesses(int data, int &rep, int delayTime) override;
std::u16string TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize) override;
int TestNestingSend(int sendCode, int &replyCode) override;
int TestAccessTokenID(int32_t ftoken_expected) override;
int TestAccessTokenID64(uint64_t token_expected, uint64_t ftoken_expected) override;
int TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src) override;
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;
int TestSendTooManyRequest(int data, int &reply) override;
int TestMultiThreadSendRequest(int data, int &reply) override;
private:
static inline BrokerDelegator<TestServiceProxy> delegator_;
bool CheckTokenSelf(uint64_t token, uint64_t tokenSelf, uint64_t ftoken, uint64_t ftoken_expected);
bool CheckSetFirstToken(uint64_t ftoken_expected);
bool CheckSetSelfToken(uint64_t token_expected);
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TestServiceProxy" };
};
} // namespace OHOS
#endif // OHOS_TEST_SERVICE_PROXY_H

View File

@ -1,184 +0,0 @@
/*
* 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_TEST_SERVICE_SKELETON_H
#define OHOS_TEST_SERVICE_SKELETON_H
#include <map>
#include "ipc_debug.h"
#include "iremote_broker.h"
#include "iremote_stub.h"
#include "iremote_proxy.h"
#include "foo_service.h"
#include "ipc_file_descriptor.h"
#include "log_tags.h"
namespace OHOS {
class ITestService : public IRemoteBroker {
public:
enum {
TRANS_ID_SYNC_TRANSACTION = 1,
TRANS_ID_ASYNC_TRANSACTION = 2,
TRANS_ID_PING_SERVICE = 3,
TRANS_ID_GET_FOO_SERVICE = 4,
TRANS_ID_TRANSACT_FILE_DESC = 5,
TRANS_ID_STRING_TRANSACTION = 6,
TRANS_ID_ZTRACE_TRANSACTION = 7,
TRANS_ID_LOOP_TRANSACTION = 8,
TRANS_ID_DUMP_SERVICE = 9,
TRANS_ID_RAWDATA_TRANSACTION = 10,
TRANS_ID_RAWDATA_REPLY = 11,
TRANS_ID_CALLING_UID_PID = 12,
TRANS_ID_FLUSH_ASYNC_CALLS = 13,
TRANS_ID_MULTIPLE_PROCESSES = 14,
TRANS_ID_ASHMEM = 15,
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,
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;
virtual int TestAsyncTransaction(int data, int timeout = 0) = 0;
virtual int TestAsyncCallbackTrans(int data, int &reply, int timeout = 0) = 0;
virtual int TestGetFileDescriptor() = 0;
virtual int TestPingService(const std::u16string &serviceName) = 0;
virtual int TestStringTransaction(const std::string &data) = 0;
virtual int TestZtraceTransaction(std::string &send, std::string &reply, int len) = 0;
virtual void TestDumpService() = 0;
virtual int TestRawDataTransaction(int length, int &reply) = 0;
virtual int TestRawDataReply(int length) = 0;
virtual sptr<IFoo> TestGetFooService() = 0;
virtual int TestCallingUidPid() = 0;
virtual int TestFlushAsyncCalls(int count, int length) = 0;
virtual int TestMultipleProcesses(int data, int &rep, int delayTime) = 0;
virtual std::u16string TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize) = 0;
virtual void TestAsyncDumpService() = 0;
virtual int TestNestingSend(int sendCode, int &replyCode) = 0;
virtual int TestAccessTokenID(int32_t ftoken_expected) = 0;
virtual int TestAccessTokenID64(uint64_t token_expected, uint64_t ftoken_expected) = 0;
virtual int TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src) = 0;
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;
virtual int TestSendTooManyRequest(int data, int &reply) = 0;
virtual int TestMultiThreadSendRequest(int data, int &reply) = 0;
public:
DECLARE_INTERFACE_DESCRIPTOR(u"test.ipc.ITestService");
};
class TestServiceStub : public IRemoteStub<ITestService> {
public:
TestServiceStub(bool serialInvokeFlag = false);
int OnRemoteRequest(uint32_t code,
MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
bool serialInvokeFlag_ = { false };
private:
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TestServiceStub" };
int32_t TransferRawData(MessageParcel &data, MessageParcel &reply);
int32_t ReplyRawData(MessageParcel &data, MessageParcel &reply);
int32_t TransferToNextProcess(MessageParcel &data, MessageParcel &reply);
int32_t ReadAshmem(MessageParcel &data, MessageParcel &reply);
int32_t ServerSyncTransaction(MessageParcel &data, MessageParcel &reply);
int32_t ServerAsyncTransaction(MessageParcel &data, MessageParcel &reply);
int32_t ServerPingService(MessageParcel &data, MessageParcel &reply);
int32_t ServerGetFooService(MessageParcel &data, MessageParcel &reply);
int32_t ServerTransactFileDesc(MessageParcel &data, MessageParcel &reply);
int32_t ServerStringTransaction(MessageParcel &data, MessageParcel &reply);
int32_t ServerZtraceTransaction(MessageParcel &data, MessageParcel &reply);
int32_t ServerCallingUidAndPid(MessageParcel &data, MessageParcel &reply);
int32_t ServerNestingSend(MessageParcel &data, MessageParcel &reply);
int32_t ServerAccessTokenId(MessageParcel &data, MessageParcel &reply);
int32_t ServerAccessTokenId64(MessageParcel &data, MessageParcel &reply);
int32_t ServerMessageParcelAddped(MessageParcel &data, MessageParcel &reply);
int32_t ServerMessageParcelAddpedWithObject(MessageParcel &data, MessageParcel &reply);
int32_t ServerEnableSerialInvokeFlag(MessageParcel &data, MessageParcel &reply);
int32_t RegisterRemoteStub(MessageParcel &data, MessageParcel &reply);
int32_t UnRegisterRemoteStub(MessageParcel &data, MessageParcel &reply);
int32_t QueryRemoteProxy(MessageParcel &data, MessageParcel &reply);
int32_t ServerFlushAsyncCalls(MessageParcel &data, MessageParcel &reply);
void InitMessageProcessMap();
using TestServiceStubFunc = int32_t(TestServiceStub::*)(MessageParcel &data, MessageParcel &reply);
std::map<uint32_t, TestServiceStubFunc> funcMap_;
};
class TestServiceProxy : public IRemoteProxy<ITestService> {
public:
explicit TestServiceProxy(const sptr<IRemoteObject> &impl);
~TestServiceProxy() = default;
int TestSyncTransaction(int data, int &reply, int delayTime = 0) override;
int TestAsyncTransaction(int data, int timeout = 0) override;
int TestAsyncCallbackTrans(int data, int &reply, int timeout = 0) override;
int TestPingService(const std::u16string &serviceName) override;
int TestGetFileDescriptor() override;
int TestStringTransaction(const std::string &data) override;
int TestZtraceTransaction(std::string &send, std::string &reply, int len) override;
void TestDumpService() override;
int TestRawDataTransaction(int length, int &reply) override;
int TestRawDataReply(int length) override;
sptr<IFoo> TestGetFooService() override;
int TestCallingUidPid() override;
int TestFlushAsyncCalls(int count, int length) override;
int TestMultipleProcesses(int data, int &rep, int delayTime) override;
std::u16string TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize) override;
void TestAsyncDumpService() override;
int TestNestingSend(int sendCode, int &replyCode) override;
int TestAccessTokenID(int32_t ftoken_expected) override;
int TestAccessTokenID64(uint64_t token_expected, uint64_t ftoken_expected) override;
int TestMessageParcelAppend(MessageParcel &dst, MessageParcel &src) override;
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;
int TestSendTooManyRequest(int data, int &reply) override;
int TestMultiThreadSendRequest(int data, int &reply) override;
private:
static inline BrokerDelegator<TestServiceProxy> delegator_;
bool CheckTokenSelf(uint64_t token, uint64_t tokenSelf, uint64_t ftoken, uint64_t ftoken_expected);
bool CheckSetFirstToken(uint64_t ftoken_expected);
bool CheckSetSelfToken(uint64_t token_expected);
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TestServiceProxy" };
};
class TestDeathRecipient : public IRemoteObject::DeathRecipient {
public:
virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
TestDeathRecipient();
virtual ~TestDeathRecipient();
static bool GotDeathRecipient();
static bool gotDeathRecipient_;
private:
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TestDeathRecipient" };
};
} // namespace OHOS
#endif // OHOS_TEST_SERVICE_SKELETON_H

View File

@ -0,0 +1,59 @@
/*
* 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_TEST_SERVICE_STUB_H
#define OHOS_TEST_SERVICE_STUB_H
#include "test_service_base.h"
namespace OHOS {
class TestServiceStub : public IRemoteStub<ITestService> {
public:
TestServiceStub(bool serialInvokeFlag = false);
int OnRemoteRequest(uint32_t code,
MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
bool serialInvokeFlag_ = { false };
private:
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TestServiceStub" };
int32_t TransferRawData(MessageParcel &data, MessageParcel &reply);
int32_t ReplyRawData(MessageParcel &data, MessageParcel &reply);
int32_t TransferToNextProcess(MessageParcel &data, MessageParcel &reply);
int32_t ReadAshmem(MessageParcel &data, MessageParcel &reply);
int32_t ServerSyncTransaction(MessageParcel &data, MessageParcel &reply);
int32_t ServerAsyncTransaction(MessageParcel &data, MessageParcel &reply);
int32_t ServerPingService(MessageParcel &data, MessageParcel &reply);
int32_t ServerGetFooService(MessageParcel &data, MessageParcel &reply);
int32_t ServerTransactFileDesc(MessageParcel &data, MessageParcel &reply);
int32_t ServerStringTransaction(MessageParcel &data, MessageParcel &reply);
int32_t ServerZtraceTransaction(MessageParcel &data, MessageParcel &reply);
int32_t ServerCallingUidAndPid(MessageParcel &data, MessageParcel &reply);
int32_t ServerNestingSend(MessageParcel &data, MessageParcel &reply);
int32_t ServerAccessTokenId(MessageParcel &data, MessageParcel &reply);
int32_t ServerAccessTokenId64(MessageParcel &data, MessageParcel &reply);
int32_t ServerMessageParcelAddped(MessageParcel &data, MessageParcel &reply);
int32_t ServerMessageParcelAddpedWithObject(MessageParcel &data, MessageParcel &reply);
int32_t ServerEnableSerialInvokeFlag(MessageParcel &data, MessageParcel &reply);
int32_t RegisterRemoteStub(MessageParcel &data, MessageParcel &reply);
int32_t UnRegisterRemoteStub(MessageParcel &data, MessageParcel &reply);
int32_t QueryRemoteProxy(MessageParcel &data, MessageParcel &reply);
int32_t ServerFlushAsyncCalls(MessageParcel &data, MessageParcel &reply);
void InitMessageProcessMap();
using TestServiceStubFunc = int32_t(TestServiceStub::*)(MessageParcel &data, MessageParcel &reply);
std::map<uint32_t, TestServiceStubFunc> funcMap_;
};
} // namespace OHOS
#endif // OHOS_TEST_SERVICE_STUB_H

View File

@ -13,33 +13,29 @@
* limitations under the License.
*/
#include <iostream>
#include <string>
#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <thread>
#include <csignal>
#include "ipc_debug.h"
#include "ipc_skeleton.h"
#include "test_service_command.h"
#include "test_service_client.h"
#include "test_service_skeleton.h"
#include "if_system_ability_manager.h"
#include "log_tags.h"
#include <nativetoken_kit.h>
#include <token_setproc.h>
#include "log_tags.h"
#include "if_system_ability_manager.h"
#include "ipc_debug.h"
#include "ipc_skeleton.h"
#include "test_service_client.h"
using namespace OHOS;
using namespace OHOS::HiviewDFX;
static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "IPCTestClient" };
void ThreadFunc(std::shared_ptr<TestServiceClient> testClient)
{
testClient->TestEnableSerialInvokeFlag();
}
static std::shared_ptr<TestServiceClient> gTestClient{ nullptr };
static void InitTokenId(void)
{
uint64_t tokenId;
@ -57,87 +53,9 @@ static void InitTokenId(void)
SetSelfTokenID(tokenId);
}
std::vector<std::string> GetArgvOptions(int argc, char **argv)
void ThreadFunc(std::shared_ptr<TestServiceClient> testClient)
{
std::vector<std::string> argvOptions;
for (int i = 1; i < argc; i++) {
argvOptions.emplace_back(std::string(argv[i]));
}
return argvOptions;
}
static std::shared_ptr<TestServiceClient> gTestClient{ nullptr };
void SignalHandler(int signum);
int main(int argc, char *argv[])
{
InitTokenId();
int result = 0;
TestCommand commandId = TestCommand::TEST_CMD_SYNC_TRANS;
if (argc > 1) {
commandId = TestCommand(std::stoi(argv[1]));
} else {
ZLOGE(LABEL, "unknown command");
}
std::vector<std::string> argvOptions;
argvOptions = GetArgvOptions(argc, argv);
std::shared_ptr<TestServiceClient> testClient = std::make_shared<TestServiceClient>();
if (testClient->ConnectService()) {
return -1;
}
ZLOGE(LABEL, "commandId= : %{public}d", commandId);
std::map<TestCommand, std::function<void()>> commandMap = {
{TestCommand::TEST_CMD_SYNC_TRANS, [&]() { testClient->StartSyncTransaction(); }},
{TestCommand::TEST_CMD_ASYNC_TRANS, [&]() { testClient->StartAsyncTransaction(); }},
{TestCommand::TEST_CMD_PING_SERVICE, [&]() { testClient->StartPingService(); }},
{TestCommand::TEST_CMD_GET_FOO_SERVICE, [&]() { testClient->StartGetFooService(); }},
{TestCommand::TEST_CMD_TRANS_FILE_DESC, [&]() { testClient->StartTestFileDescriptor(); }},
{TestCommand::TEST_CMD_LOOP_TRANSACTION, [&]() { constexpr int maxTestCount = 1000;
testClient->StartLoopTest(maxTestCount); }},
{TestCommand::TEST_CMD_DUMP_SERVICE, [&]() { testClient->StartDumpService(); }},
{TestCommand::TEST_CMD_ASYNC_DUMP_SERVICE, [&]() { testClient->StartAsyncDumpService(); }},
{TestCommand::TEST_CMD_ENABLE_SERIAL_INVOKE_FLAG, [&]() {
std::thread temp(ThreadFunc, testClient);
testClient->TestEnableSerialInvokeFlag();
temp.join();
}},
{TestCommand::TEST_CMD_NATIVE_IPC_REQUESTS, [&]() {
if (argc < 3) {
ZLOGE(LABEL, "sub cmd is needed!");
return;
}
int subCmd = std::stoi(argvOptions[1]);
testClient->TestNativeIPCSendRequests(subCmd);
}},
{TestCommand::TEST_CMD_NATIVE_IPC_REGISTER_REMOTE_STUB_OBJECT, [&]() {
testClient->TestRegisterRemoteStub();
gTestClient = testClient;
if (signal(SIGINT, SignalHandler) == SIG_ERR) {
ZLOGE(LABEL, "Failed to caught signal");
}
}},
{TestCommand::TEST_CMD_TOO_MANY_SENDREQUEST, [&]() {
testClient->TestSendTooManyRequest();
testClient->StartSyncTransaction();
}},
{TestCommand::TEST_CMD_MULTI_THREAD_SEND, [&]() {
testClient->TestMultiThreadSendRequest();
}},
};
auto it = commandMap.find(commandId);
if (it != commandMap.end()) {
it->second();
} else {
ZLOGD(LABEL, "main arg error");
}
// The non IPC context obtains one's own sid
std::string selfSid = IPCSkeleton::GetCallingSid();
ZLOGI(LABEL, "get from service: %{public}d, sid: %{public}s", result, selfSid.c_str());
IPCSkeleton::JoinWorkThread();
return 0;
testClient->TestEnableSerialInvokeFlag();
}
void SignalHandler(int signum)
@ -153,3 +71,172 @@ void SignalHandler(int signum)
IPCSkeleton::StopWorkThread();
}
}
void TestCaseSyncTrans(std::shared_ptr<TestServiceClient> &testClient)
{
bool ret = testClient->StartSyncTransaction();
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseSyncTrans case failed" <<std::endl;
} else {
std::cout << "[PASS] Execution of TestCaseSyncTrans case Successful" <<std::endl;
}
}
void TestCasePingService(std::shared_ptr<TestServiceClient> &testClient)
{
bool ret = testClient->StartPingService();
if (!ret) {
std::cout << "[FAILED] Execution of TestCasePingService case failed" <<std::endl;
} else {
std::cout << "[PASS] Execution of TestCasePingService case Successful" <<std::endl;
}
}
void TestCaseGetFooService(std::shared_ptr<TestServiceClient> &testClient)
{
bool ret = testClient->StartGetFooService();
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseGetFooService case failed" <<std::endl;
} else {
std::cout << "[PASS] Execution of TestCaseGetFooService case Successful" <<std::endl;
}
}
void TestCaseGetFileDescriptor(std::shared_ptr<TestServiceClient> &testClient)
{
bool ret = testClient->StartTestFileDescriptor();
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseGetFileDescriptor case failed" <<std::endl;
} else {
std::cout << "[PASS] Execution of TestCaseGetFileDescriptor case Successful" <<std::endl;
}
}
void TestCaseLoopTest(std::shared_ptr<TestServiceClient> &testClient)
{
constexpr int maxTestCount = 10240;
bool ret = testClient->StartLoopTest(maxTestCount);
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseLoopTest case failed" <<std::endl;
} else {
std::cout << "[PASS] Execution of TestCaseLoopTest case Successful" <<std::endl;
}
}
void TestCaseDumpService(std::shared_ptr<TestServiceClient> &testClient)
{
bool ret = testClient->StartDumpService();
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseDumpService case failed" <<std::endl;
} else {
std::cout << "[PASS] Execution of TestCaseDumpService case Successful" <<std::endl;
}
}
void TestCaseEnableSerialInvokeFlag(std::shared_ptr<TestServiceClient> &testClient)
{
std::thread temp(ThreadFunc, testClient);
bool ret = testClient->TestEnableSerialInvokeFlag();
temp.join();
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseEnableSerialInvokeFlag case failed" <<std::endl;
} else {
std::cout << "[PASS] Execution of TestCaseEnableSerialInvokeFlag case Successful" <<std::endl;
}
}
void TestCaseNativeIPCSendRequests(std::shared_ptr<TestServiceClient> &testClient)
{
bool ret = testClient->TestRegisterRemoteStub();
if (!ret) {
std::cout << "[FAILED] Execution of TestRegisterRemoteStub case failed" <<std::endl;
return;
}
ret = testClient->TestNativeIPCSendRequests(1);
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseNativeIPCSendRequests case failed" <<std::endl;
return;
}
std::cout << "[PASS] Execution of TestCaseNativeIPCSendRequests case Successful" <<std::endl;
}
void TestCaseRegisterRemoteStub(std::shared_ptr<TestServiceClient> &testClient)
{
bool ret = testClient->TestRegisterRemoteStub();
if (!ret) {
std::cout << "[FAILED] Execution of TestRegisterRemoteStub case failed" <<std::endl;
return;
}
gTestClient = testClient;
if (signal(SIGINT, SignalHandler) == SIG_ERR) {
ZLOGE(LABEL, "Failed to caught signal");
std::cout << "[FAILED] Execution of TestRegisterRemoteStub case failed" <<std::endl;
return;
}
std::cout << "[PASS] Execution of TestRegisterRemoteStub case Successful" <<std::endl;
}
void TestCaseTooManyRequests(std::shared_ptr<TestServiceClient> &testClient)
{
bool ret = testClient->TestSendTooManyRequest();
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseTooManyRequests case failed" <<std::endl;
}
ret = testClient->StartSyncTransaction();
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseTooManyRequests case failed" <<std::endl;
return;
}
std::cout << "[PASS] Execution of TestCaseTooManyRequests case Successful" <<std::endl;
}
void TestCaseMultiThreadSendRequest(std::shared_ptr<TestServiceClient> &testClient)
{
bool ret = testClient->TestMultiThreadSendRequest();
if (!ret) {
std::cout << "[FAILED] Execution of TestCaseMultiThreadSendRequest case failed" <<std::endl;
} else {
std::cout << "[PASS] Execution of TestCaseMultiThreadSendRequest case Successful" <<std::endl;
}
}
void ExecuteAllTestCase()
{
std::shared_ptr<TestServiceClient> testClient = std::make_shared<TestServiceClient>();
int ret = testClient->ConnectService();
if (!ret) {
ZLOGE(LABEL, "ConnectService failed");
return;
}
TestCaseSyncTrans(testClient);
TestCasePingService(testClient);
TestCaseGetFooService(testClient);
TestCaseGetFileDescriptor(testClient);
TestCaseLoopTest(testClient);
TestCaseDumpService(testClient);
TestCaseEnableSerialInvokeFlag(testClient);
TestCaseNativeIPCSendRequests(testClient);
TestCaseRegisterRemoteStub(testClient);
TestCaseTooManyRequests(testClient);
TestCaseMultiThreadSendRequest(testClient);
ZLOGI(LABEL, "All test cases have been executed");
}
int main(int argc, char *argv[])
{
if (fork() == 0) {
system("/system/bin/ipc_server_test");
return 0;
}
sleep(1);
std::cout << "Start executing the client" <<std::endl;
InitTokenId();
ExecuteAllTestCase();
// The non IPC context obtains one's own sid
std::string selfSid = IPCSkeleton::GetCallingSid();
ZLOGI(LABEL, "get from service: sid: %{public}s", selfSid.c_str());
system("kill -9 $(pidof /system/bin/ipc_server_test)");
return 0;
}

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include <iostream>
#include "ipc_debug.h"
#include "ipc_skeleton.h"
#include "test_service.h"
@ -44,6 +45,7 @@ static void InitTokenId(void)
int main(int argc, char *argv[])
{
std::cout << "Start executing the server" <<std::endl;
bool isEnableSerialInvokeFlag = false;
if (argc > 1) {
isEnableSerialInvokeFlag = true;

View File

@ -30,7 +30,6 @@
#include "test_capi_skeleton.h"
#include "ipc_debug.h"
#include "ipc_inner_object.h"
#include "test_service_command.h"
namespace OHOS {
@ -96,15 +95,18 @@ NativeRemoteProxyTest::~NativeRemoteProxyTest()
int NativeRemoteProxyTest::SyncAdd()
{
if (proxy_ == nullptr) {
ZLOGE(LABEL, "proxy_ is nullptr");
return -1;
}
OHIPCParcel *data = OH_IPCParcel_Create();
if (data == nullptr) {
ZLOGE(LABEL, "data is nullptr");
return -1;
}
OHIPCParcel *reply = OH_IPCParcel_Create();
if (reply == nullptr) {
OH_IPCParcel_Destroy(data);
ZLOGE(LABEL, "reply is nullptr");
return -1;
}
OH_IPC_MessageOption option{ OH_IPC_REQUEST_MODE_SYNC, 0 };
@ -131,7 +133,7 @@ int NativeRemoteProxyTest::SyncAdd()
OH_IPCParcel_Destroy(data);
OH_IPCParcel_Destroy(reply);
if ((a + b) == result) {
ZLOGD(LABEL, "SyncAdd success! %{public}d + %{public}d = %{public}d", a, b, result);
ZLOGI(LABEL, "SyncAdd success! %{public}d + %{public}d = %{public}d", a, b, result);
return 0;
}
ZLOGE(LABEL, "SyncAdd failed! %{public}d + %{public}d = %{public}d", a, b, result);

View File

@ -93,17 +93,6 @@ int TestService::TestSyncTransaction(int data, int &rep, int delayTime)
return ERR_NONE;
}
int TestService::TestAsyncTransaction(int data, int timeout)
{
ZLOGE(LABEL, "TestServiceStub:read from client data = %{public}d", data);
if (timeout > 0) {
sleep((uint32_t)timeout);
}
return Reverse(data);
}
int TestService::TestAsyncCallbackTrans(int data, int &reply, int timeout)
{
if (timeout > 0) {
@ -167,14 +156,10 @@ int TestService::TestStringTransaction(const std::string &data)
return data.size();
}
void TestService::TestDumpService()
{
// use for proxy only.
}
void TestService::TestAsyncDumpService()
int TestService::TestDumpService()
{
// use for proxy only.
return 0;
}
int TestService::TestRawDataTransaction(int length, int &reply)
@ -251,27 +236,27 @@ int TestService::Dump(int fd, const std::vector<std::u16string> &args)
if (!IPCPayloadStatistics::GetStatisticsStatus()) {
IPCPayloadStatistics::StartStatistics();
}
std::cout << " ---------------------------------------- "<< std::endl;
std::cout << " TotalCount = " << IPCPayloadStatistics::GetTotalCount();
std::cout << " TotalCost = " << IPCPayloadStatistics::GetTotalCost() << std::endl;
ZLOGI(LABEL, " ---------------------------------------- ");
ZLOGI(LABEL, " TotalCount = %{public}d", (int)IPCPayloadStatistics::GetTotalCount());
ZLOGI(LABEL, " TotalCost = %{public}d", (int)IPCPayloadStatistics::GetTotalCost());
std::vector<int32_t> pidVec = IPCPayloadStatistics::GetPids();
for (int32_t &val : pidVec) {
std::cout << " Pid = " << val << std::endl;
std::cout << " PidCount = " << IPCPayloadStatistics::GetCount(val)
<< " PidCost = " << IPCPayloadStatistics::GetCost(val) << std::endl;
ZLOGI(LABEL, " Pid = %{public}d", val);
ZLOGI(LABEL, " PidCount = %{public}d", (int)IPCPayloadStatistics::GetCount(val));
ZLOGI(LABEL, " IdCost = %{public}d", (int)IPCPayloadStatistics::GetCount(val));
std::vector<IPCInterfaceInfo> infoVec = IPCPayloadStatistics::GetDescriptorCodes(val);
for (auto &info : infoVec) {
std::cout << " desc = " << Str16ToStr8(info.desc) << " code = " << info.code;
std::cout << " DescCount = " << IPCPayloadStatistics::GetDescriptorCodeCount(val, info.desc, info.code);
ZLOGI(LABEL, " desc = %{public}s, code = %{public}d", Str16ToStr8(info.desc).c_str(), info.code);
ZLOGI(LABEL, " DescCount = %{public}d",
(int)IPCPayloadStatistics::GetDescriptorCodeCount(val, info.desc, info.code));
IPCPayloadCost payloadCost = IPCPayloadStatistics::GetDescriptorCodeCost(val, info.desc, info.code);
std::cout << " DescTotalCost = " << payloadCost.totalCost;
std::cout << " DescMaxCost = " << payloadCost.maxCost;
std::cout << " DescMinCost = " << payloadCost.minCost;
std::cout << " DescAverCost = " << payloadCost.averCost << std::endl;
ZLOGI(LABEL, " DescMaxCost = %{public}d", (int)payloadCost.totalCost);
ZLOGI(LABEL, " DescTotalCost = %{public}d", (int)payloadCost.maxCost);
ZLOGI(LABEL, " DescMinCost = %{public}d", (int)payloadCost.minCost);
ZLOGI(LABEL, " DescAverCost = %{public}d", (int)payloadCost.averCost);
}
}
std::cout << " ---------------------------------------- "<< std::endl;
ZLOGI(LABEL, " ---------------------------------------- ");
return writeCount > 0 ? ERR_NONE : ERR_TRANSACTION_FAILED;
}
@ -337,4 +322,15 @@ int TestService::TestMultiThreadSendRequest(int data, int &reply)
(void)reply;
return 0;
}
int TestService::TestAsyncTransaction(int data, int timeout)
{
ZLOGE(LABEL, "TestServiceStub:read from client data = %{public}d", data);
if (timeout > 0) {
sleep((uint32_t)timeout);
}
return Reverse(data);
}
} // namespace OHOS

View File

@ -25,18 +25,20 @@
namespace OHOS {
int TestServiceClient::ConnectService()
constexpr int PARCEL_MAX_CAPACITY = 200 * 1024;
bool TestServiceClient::ConnectService()
{
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (saMgr == nullptr) {
ZLOGE(LABEL, "get registry fail");
return -1;
return false;
}
sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE);
if (object != nullptr) {
ZLOGD(LABEL, "Got test Service object");
ZLOGI(LABEL, "Got test Service object");
sptr<IRemoteObject::DeathRecipient> death(new TestDeathRecipient());
object->AddDeathRecipient(death.GetRefPtr());
testService_ = iface_cast<ITestService>(object);
@ -44,202 +46,259 @@ int TestServiceClient::ConnectService()
if (testService_ == nullptr) {
ZLOGE(LABEL, "Could not find Test Service!");
return -1;
return false;
}
return 0;
return true;
}
void TestServiceClient::StartSyncTransaction()
bool TestServiceClient::StartSyncTransaction()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "StartSyncTransaction");
[[maybe_unused]] int result = 0;
testService_->TestSyncTransaction(2019, result);
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
ZLOGD(LABEL, "StartSyncTransaction");
int result = 0;
int originalValue = 2019;
int reversalValue = 9102;
int ret = testService_->TestSyncTransaction(originalValue, result);
if (ret != 0) {
ZLOGE(LABEL, "TestSyncTransaction function call failed");
return false;
}
if (result != reversalValue) {
return false;
}
return true;
}
void TestServiceClient::StartSyncDelayReply()
bool TestServiceClient::StartPingService()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "StartSyncDelayReply");
[[maybe_unused]] int result = 0;
testService_->TestSyncTransaction(2019, result, 2);
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
ZLOGD(LABEL, "StartPingService");
const std::u16string descriptor = ITestService::GetDescriptor();
int ret = testService_->TestPingService(descriptor);
if (ret != 0) {
ZLOGE(LABEL, "TestPingService function call failed");
return false;
}
return true;
}
void TestServiceClient::StartAsyncTransaction()
bool TestServiceClient::StartGetFooService()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "StartAsyncTransaction");
[[maybe_unused]] int result = 0;
testService_->TestAsyncTransaction(2019, result);
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
ZLOGD(LABEL, "StartGetFooService");
sptr<IFoo> foo = testService_->TestGetFooService();
if (foo == nullptr) {
ZLOGE(LABEL, "TestGetFooService function call failed");
return false;
}
return true;
}
void TestServiceClient::StartPingService()
bool TestServiceClient::StartDumpService()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "StartPingService");
const std::u16string descriptor = ITestService::GetDescriptor();
testService_->TestPingService(descriptor);
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
ZLOGD(LABEL, "StartDumpService");
int ret = testService_->TestDumpService();
if (ret != 0) {
ZLOGE(LABEL, "TestDumpService function call failed");
return false;
}
return true;
}
void TestServiceClient::StartGetFooService()
bool TestServiceClient::StartTestFileDescriptor()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "StartGetFooService");
sptr<IFoo> foo = testService_->TestGetFooService();
if (foo == nullptr) {
ZLOGD(LABEL, "Fail to get foo service");
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
ZLOGD(LABEL, "StartTestFileDescriptor");
int fd = testService_->TestGetFileDescriptor();
if (fd == INVALID_FD) {
ZLOGE(LABEL, "TestGetFileDescriptor function call failed");
return false;
}
if (write(fd, "client write!\n", strlen("client write!\n")) < 0) {
ZLOGE(LABEL, "write fd error");
return false;
}
close(fd);
return true;
}
bool TestServiceClient::StartLoopTest(int maxCount)
{
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
ZLOGD(LABEL, "StartLoopTest");
int count = 0;
std::string testString;
// start loop test, test times is 10240
for (count = 0; count < maxCount; count++) {
testString += "0123456789!";
int ret = testService_->TestStringTransaction(testString);
if (ret > PARCEL_MAX_CAPACITY) {
return false;
}
}
return true;
}
void TestServiceClient::StartDumpService()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "StartDumpService");
testService_->TestDumpService();
}
}
void TestServiceClient::StartAsyncDumpService()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "StartAsyncDumpService");
testService_->TestAsyncDumpService();
}
}
void TestServiceClient::StartTestFileDescriptor()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "StartTestFileDescriptor");
int fd = testService_->TestGetFileDescriptor();
if (fd != INVALID_FD) {
if (write(fd, "client write!\n", strlen("client write!\n")) < 0) {
ZLOGE(LABEL, "write fd error");
}
close(fd);
}
}
}
int TestServiceClient::StartLoopTest(int maxCount)
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "StartLoopTest");
int count = 0;
std::string testString;
// start loop test, test times is 1000
for (count = 0; count < maxCount; count++) {
testString += "0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+{}?/[]<>-='|~";
testService_->TestStringTransaction(testString);
}
return count;
}
return 0;
}
void TestServiceClient::TestEnableSerialInvokeFlag()
bool TestServiceClient::TestEnableSerialInvokeFlag()
{
ZLOGD(LABEL, "TestEnableSerialInvokeFlag");
if (testService_ == nullptr) {
ZLOGE(LABEL, "Member variable testService_ Is a null pointer");
return;
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
int result = testService_->TestEnableSerialInvokeFlag();
if (result != 0) {
std::cout << "TestServiceClient::TestEnableSerialInvokeFlag function call failed" << std::endl;
return;
ZLOGE(LABEL, "TestEnableSerialInvokeFlag function call failed");
return false;
}
std::cout << "TestServiceClient::TestEnableSerialInvokeFlag function call successful" << std::endl;
return true;
}
void TestServiceClient::TestNativeIPCSendRequests(int subCmd)
bool TestServiceClient::TestNativeIPCSendRequests(int subCmd)
{
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
auto remoteProxy = std::make_shared<NativeRemoteProxyTest>(testService_);
if (remoteProxy == nullptr) {
ZLOGE(LABEL, "create remote proxy test failed!");
return;
return false;
}
static std::map<int, std::function<int()>> commandMap = {
{ NATIVE_TEST_CMD_SYNC_ADD, [&]() { return remoteProxy->SyncAdd(); }},
{ NATIVE_TEST_CMD_ASYNC_ADD, [&]() { return remoteProxy->ASyncAdd(); }},
{ NATIVE_TEST_CMD_SYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(true); }},
{ NATIVE_TEST_CMD_ASYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(false); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_BASE, [&]() { return remoteProxy->SendAndEchoBase(); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_SRING, [&]() { return remoteProxy->SendAndEchoString(); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER, [&]() { return remoteProxy->SendAndEchoBuffer(); }},
{ NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR, [&]() { return remoteProxy->SendAndEchoFileDescriptor(); }},
{ NATIVE_TEST_CMD_SEND_ERROR_CODE, [&]() { return remoteProxy->SendErrorCode(); }},
{ NATIVE_TEST_CMD_SYNC_ADD, [&]() { return remoteProxy->SyncAdd(); }},
{ NATIVE_TEST_CMD_ASYNC_ADD, [&]() { return remoteProxy->ASyncAdd(); }},
{ NATIVE_TEST_CMD_SYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(true); }},
{ NATIVE_TEST_CMD_ASYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(false); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_BASE, [&]() { return remoteProxy->SendAndEchoBase(); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_SRING, [&]() { return remoteProxy->SendAndEchoString(); }},
{ NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER, [&]() { return remoteProxy->SendAndEchoBuffer(); }},
{ NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR, [&]() { return remoteProxy->SendAndEchoFileDescriptor(); }},
{ NATIVE_TEST_CMD_SEND_ERROR_CODE, [&]() { return remoteProxy->SendErrorCode(); }},
};
auto it = commandMap.find(subCmd);
if (it != commandMap.end()) {
if (it->second() != 0) {
ZLOGE(LABEL, "Test sub cmd:%{public}d failed!", subCmd);
} else {
ZLOGD(LABEL, "Test sub cmd:%{public}d success!", subCmd);
}
} else {
ZLOGD(LABEL, "error sub cmd:%{public}d", subCmd);
return;
if (it == commandMap.end()) {
ZLOGE(LABEL, "error sub cmd:%{public}d", subCmd);
return false;
}
if (it->second() != 0) {
ZLOGE(LABEL, "Test sub cmd:%{public}d failed!", subCmd);
return false;
}
ZLOGI(LABEL, "Test sub cmd:%{public}d success!", subCmd);
return true;
}
void TestServiceClient::TestRegisterRemoteStub()
bool TestServiceClient::TestRegisterRemoteStub()
{
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
if (remoteStub_ == nullptr) {
remoteStub_ = std::make_shared<NativeRemoteStubTest>(testService_);
if (remoteStub_ == nullptr) {
ZLOGE(LABEL, "create remote stub test failed!");
return;
return false;
}
}
int ret = remoteStub_->RegisterRemoteStub();
if (ret != 0) {
ZLOGE(LABEL, "function call failed");
return;
ZLOGE(LABEL, "RegisterRemoteStub function call failed");
return false;
}
ZLOGD(LABEL, "function call success");
ZLOGI(LABEL, "RegisterRemoteStub function success");
return true;
}
void TestServiceClient::TestUnRegisterRemoteStub()
bool TestServiceClient::TestUnRegisterRemoteStub()
{
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
if (remoteStub_ == nullptr) {
remoteStub_ = std::make_shared<NativeRemoteStubTest>(testService_);
if (remoteStub_ == nullptr) {
ZLOGE(LABEL, "create remote stub test failed!");
return;
return false;
}
}
int ret = remoteStub_->UnRegisterRemoteStub();
if (ret != 0) {
ZLOGE(LABEL, "function call failed");
return;
return false;
}
ZLOGD(LABEL, "function call success");
ZLOGI(LABEL, "function call success");
return true;
}
void TestServiceClient::TestSendTooManyRequest()
bool TestServiceClient::TestSendTooManyRequest()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "TestSendTooManyRequest");
int ret = 0;
int data = 2024;
testService_->TestSendTooManyRequest(data, ret);
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
ZLOGD(LABEL, "TestSendTooManyRequest");
int res = 0;
int data = 2024;
int ret = testService_->TestSendTooManyRequest(data, res);
if (ret != 0) {
ZLOGE(LABEL, "function call failed");
return false;
}
return true;
}
void TestServiceClient::TestMultiThreadSendRequest()
bool TestServiceClient::TestMultiThreadSendRequest()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "TestMultiThreadSendRequest");
int ret = 0;
int value = 2024;
testService_->TestMultiThreadSendRequest(value, ret);
if (testService_ == nullptr) {
ZLOGE(LABEL, "The testService_ object is an empty object");
return false;
}
int res = 0;
int value = 2024;
int ret = testService_->TestMultiThreadSendRequest(value, res);
if (ret != 0) {
ZLOGE(LABEL, "function call failed");
return false;
}
return true;
}
} // namespace OHOS

View File

@ -0,0 +1,39 @@
/*
* 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 "test_service_death_recipient.h"
namespace OHOS {
bool TestDeathRecipient::gotDeathRecipient_ = false;
bool TestDeathRecipient::GotDeathRecipient()
{
return gotDeathRecipient_;
}
void TestDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
{
gotDeathRecipient_ = true;
ZLOGI(LABEL, "recv death notice");
}
TestDeathRecipient::TestDeathRecipient()
{
}
TestDeathRecipient::~TestDeathRecipient()
{
}
} // namespace OHOS

View File

@ -13,13 +13,13 @@
* limitations under the License.
*/
#include "test_service_skeleton.h"
#include "test_service_proxy.h"
#include <cinttypes>
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
#include <cstdlib>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
@ -38,44 +38,9 @@
namespace OHOS {
using namespace OHOS::HiviewDFX;
constexpr int32_t MAX_RECURSIVE_SENDS = 5;
constexpr int32_t SEDNREQUEST_TIMES = 2000;
constexpr int32_t NUMBER_OF_THREADS = 20;
void TestServiceStub::InitMessageProcessMap()
{
funcMap_[static_cast<uint32_t>(TRANS_ID_SYNC_TRANSACTION)] = &TestServiceStub::ServerSyncTransaction;
funcMap_[static_cast<uint32_t>(TRANS_ID_ASYNC_TRANSACTION)] = &TestServiceStub::ServerAsyncTransaction;
funcMap_[static_cast<uint32_t>(TRANS_ID_PING_SERVICE)] = &TestServiceStub::ServerPingService;
funcMap_[static_cast<uint32_t>(TRANS_ID_GET_FOO_SERVICE)] = &TestServiceStub::ServerGetFooService;
funcMap_[static_cast<uint32_t>(TRANS_ID_TRANSACT_FILE_DESC)] = &TestServiceStub::ServerTransactFileDesc;
funcMap_[static_cast<uint32_t>(TRANS_ID_STRING_TRANSACTION)] = &TestServiceStub::ServerStringTransaction;
funcMap_[static_cast<uint32_t>(TRANS_ID_ZTRACE_TRANSACTION)] = &TestServiceStub::ServerZtraceTransaction;
funcMap_[static_cast<uint32_t>(TRANS_ID_RAWDATA_TRANSACTION)] = &TestServiceStub::TransferRawData;
funcMap_[static_cast<uint32_t>(TRANS_ID_RAWDATA_REPLY)] = &TestServiceStub::ReplyRawData;
funcMap_[static_cast<uint32_t>(TRANS_ID_FLUSH_ASYNC_CALLS)] = &TestServiceStub::ServerFlushAsyncCalls;
funcMap_[static_cast<uint32_t>(TRANS_ID_ASHMEM)] = &TestServiceStub::ReadAshmem;
funcMap_[static_cast<uint32_t>(TRANS_ID_MULTIPLE_PROCESSES)] = &TestServiceStub::TransferToNextProcess;
funcMap_[static_cast<uint32_t>(TRANS_ID_CALLING_UID_PID)] = &TestServiceStub::ServerCallingUidAndPid;
funcMap_[static_cast<uint32_t>(TRANS_ID_NESTING_SEND)] = &TestServiceStub::ServerNestingSend;
funcMap_[static_cast<uint32_t>(TRANS_ID_ACCESS_TOKENID)] = &TestServiceStub::ServerAccessTokenId;
funcMap_[static_cast<uint32_t>(TRANS_ID_ACCESS_TOKENID_64)] = &TestServiceStub::ServerAccessTokenId64;
funcMap_[static_cast<uint32_t>(TRANS_MESSAGE_PARCEL_ADDPED)] = &TestServiceStub::ServerMessageParcelAddped;
funcMap_[static_cast<uint32_t>(TRANS_MESSAGE_PARCEL_ADDPED_WITH_OBJECT)] =
&TestServiceStub::ServerMessageParcelAddpedWithObject;
funcMap_[static_cast<uint32_t>(TRANS_ENABLE_SERIAL_INVOKE_FLAG)] = &TestServiceStub::ServerEnableSerialInvokeFlag;
funcMap_[static_cast<uint32_t>(TRANS_ID_REGISTER_REMOTE_STUB_OBJECT)] = &TestServiceStub::RegisterRemoteStub;
funcMap_[static_cast<uint32_t>(TRANS_ID_UNREGISTER_REMOTE_STUB_OBJECT)] = &TestServiceStub::UnRegisterRemoteStub;
funcMap_[static_cast<uint32_t>(TRANS_ID_QUERY_REMOTE_PROXY_OBJECT)] = &TestServiceStub::QueryRemoteProxy;
}
TestServiceStub::TestServiceStub(bool serialInvokeFlag)
: IRemoteStub(serialInvokeFlag), serialInvokeFlag_(serialInvokeFlag)
{
InitMessageProcessMap();
}
TestServiceProxy::TestServiceProxy(const sptr<IRemoteObject> &impl)
: IRemoteProxy<ITestService>(impl)
{
@ -83,38 +48,54 @@ TestServiceProxy::TestServiceProxy(const sptr<IRemoteObject> &impl)
int TestServiceProxy::TestEnableSerialInvokeFlag()
{
int error;
MessageOption option;
MessageParcel dataParcel, replyParcel;
std::string value = "testData";
ZLOGD(LABEL, "send to server data = %{public}s", value.c_str());
ZLOGI(LABEL, "send to server data = %{public}s", value.c_str());
dataParcel.WriteString(value);
error = Remote()->SendRequest(TRANS_ENABLE_SERIAL_INVOKE_FLAG, dataParcel, replyParcel, option);
auto remote = Remote();
if (remote == nullptr) {
ZLOGD(LABEL, "The obtained proxy is a null pointer");
return -1;
}
int ret = remote->SendRequest(TRANS_ENABLE_SERIAL_INVOKE_FLAG, dataParcel, replyParcel, option);
std::string reply = replyParcel.ReadString();
ZLOGD(LABEL, "get result from server data = %{public}s", reply.c_str());
return error;
ZLOGI(LABEL, "get result from server data = %{public}s", reply.c_str());
return ret;
}
int TestServiceProxy::TestSyncTransaction(int data, int &reply, int delayTime)
int TestServiceProxy::TestSyncTransaction(int value, int &reply, int delayTime)
{
int error;
MessageOption option;
MessageParcel dataParcel, replyParcel;
ZLOGD(LABEL, "send to server data = %{public}d", data);
if (data > 0) {
dataParcel.WriteInt32(data);
int ret = -1;
ZLOGD(LABEL, "send to server data = %{public}d", value);
if (value > 0) {
dataParcel.WriteInt32(value);
dataParcel.WriteInt32(delayTime);
}
error = Remote()->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel, option);
auto remote = Remote();
if (remote == nullptr) {
ZLOGD(LABEL, "The obtained proxy is a null pointer");
return ret;
}
ret = remote->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel, option);
if (ret != 0) {
return ret;
}
reply = replyParcel.ReadInt32();
ZLOGD(LABEL, "get result from server data = %{public}d", reply);
return error;
return ret;
}
int TestServiceProxy::TestSendTooManyRequest(int data, int &reply)
{
int error;
int ret = 0;
int delayTime = 0;
MessageOption option;
MessageParcel dataParcel;
@ -126,16 +107,16 @@ int TestServiceProxy::TestSendTooManyRequest(int data, int &reply)
dataParcel.WriteInt32(delayTime);
}
for (int32_t i = 0; i < SEDNREQUEST_TIMES; i++) {
error = Remote()->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel[i], option);
ret = Remote()->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel[i], option);
}
reply = replyParcel[0].ReadInt32();
ZLOGD(LABEL, "get result from server data = %{public}d", reply);
return error;
return ret;
}
int TestServiceProxy::TestMultiThreadSendRequest(int data, int &reply)
{
int error = 0;
int ret = 0;
sptr<IRemoteObject> proxyObject = Remote();
for (int32_t i = 0; i < NUMBER_OF_THREADS; i++) {
@ -148,76 +129,86 @@ int TestServiceProxy::TestMultiThreadSendRequest(int data, int &reply)
dataParcel.WriteInt32(data);
dataParcel.WriteInt32(0);
}
int error = proxyObject->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel, option);
if (error != 0) {
ZLOGD(LABEL, "SendRequest is failed: %{public}d", error);
int ret = proxyObject->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel, option);
if (ret != 0) {
ZLOGD(LABEL, "SendRequest is failed: %{public}d", ret);
return;
}
reply = replyParcel.ReadInt32();
});
t.detach();
std::cout << "Thead: " << i << std::endl;
}
sleep(1); // Waiting for the thread to finish executing
return error;
return ret;
}
int TestServiceProxy::TestAsyncTransaction(int data, int timeout)
{
MessageOption option = { MessageOption::TF_ASYNC };
MessageParcel dataParcel, replyParcel;
ZLOGD(LABEL, "%{public}s:in, data = %{public}d", __func__, data);
ZLOGI(LABEL, "%{public}s:in, data = %{public}d", __func__, data);
dataParcel.WriteInt32(data);
dataParcel.WriteInt32(timeout);
dataParcel.WriteBool(false);
return Remote()->SendRequest(TRANS_ID_ASYNC_TRANSACTION, dataParcel, replyParcel, option);
auto remote = Remote();
if (remote == nullptr) {
ZLOGE(LABEL, "remote is nullptr");
return -1;
}
return remote->SendRequest(TRANS_ID_ASYNC_TRANSACTION, dataParcel, replyParcel, option);
}
int TestServiceProxy::TestAsyncCallbackTrans(int data, int &reply, int timeout)
{
int error;
int ret;
MessageOption option = { MessageOption::TF_ASYNC };
MessageParcel dataParcel, replyParcel;
ZLOGD(LABEL, "%{public}s:in, data = %{public}d", __func__, data);
ZLOGI(LABEL, "%{public}s:in, data = %{public}d", __func__, data);
dataParcel.WriteInt32(data);
dataParcel.WriteInt32(timeout);
dataParcel.WriteBool(true);
sptr<FooStub> fooCallback = new FooStub();
dataParcel.WriteRemoteObject(fooCallback->AsObject());
error = Remote()->SendRequest(TRANS_ID_ASYNC_TRANSACTION, dataParcel, replyParcel, option);
ret = Remote()->SendRequest(TRANS_ID_ASYNC_TRANSACTION, dataParcel, replyParcel, option);
reply = fooCallback->WaitForAsyncReply(timeout);
FooStub::CleanDecTimes();
fooCallback = nullptr;
if (FooStub::GetDecTimes() != 1) {
error = ERR_TRANSACTION_FAILED;
ret = ERR_TRANSACTION_FAILED;
}
return error;
return ret;
}
int TestServiceProxy::TestZtraceTransaction(std::string &send, std::string &reply, int len)
{
int error;
MessageParcel dataParcel, replyParcel;
MessageOption option;
dataParcel.WriteInt32(len);
dataParcel.WriteString(send);
error = Remote()->SendRequest(TRANS_ID_ZTRACE_TRANSACTION, dataParcel, replyParcel, option);
reply = replyParcel.ReadString();
int ret = Remote()->SendRequest(TRANS_ID_ZTRACE_TRANSACTION, dataParcel, replyParcel, option);
if (ret != 0) {
return ret;
}
return error;
if (!replyParcel.ReadString(reply)) {
return -1;
}
return ret;
}
int TestServiceProxy::TestPingService(const std::u16string &serviceName)
{
int error;
int ret;
MessageOption option;
MessageParcel dataParcel, replyParcel;
ZLOGD(LABEL, "PingService");
dataParcel.WriteString16(serviceName);
error = Remote()->SendRequest(TRANS_ID_PING_SERVICE, dataParcel, replyParcel, option);
int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1;
ret = Remote()->SendRequest(TRANS_ID_PING_SERVICE, dataParcel, replyParcel, option);
int result = (ret == ERR_NONE) ? replyParcel.ReadInt32() : -1;
ZLOGD(LABEL, "PingService result = %{public}d", result);
return result;
}
@ -256,42 +247,28 @@ int TestServiceProxy::TestStringTransaction(const std::string &data)
return testSize;
}
void TestServiceProxy::TestDumpService()
int TestServiceProxy::TestDumpService()
{
ZLOGD(LABEL, "call StartDumpService");
int fd = open("/data/dump.txt",
O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
if (fd != INVALID_FD) {
ZLOGD(LABEL, "Start Dump Service");
std::vector<std::u16string> args;
args.push_back(u"DumpTest");
Remote()->Dump(fd, args);
close(fd);
}
}
void TestServiceProxy::TestAsyncDumpService()
{
int fd = open("/data/nonblockingDump.txt",
O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
int fd = open("/data/dump.txt", O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
if (fd == INVALID_FD) {
return;
ZLOGE(LABEL, "Call to open system function failed.");
return -1;
}
ZLOGD(LABEL, "Start Async Dump Service");
ZLOGD(LABEL, "Start Dump Service");
std::vector<std::u16string> args;
args.push_back(u"NonblockingDumpTest");
MessageParcel data, reply;
MessageOption option {MessageOption::TF_ASYNC};
data.WriteFileDescriptor(fd);
data.WriteString16Vector(args);
(void)Remote()->SendRequest(DUMP_TRANSACTION, data, reply, option);
args.push_back(u"DumpTest");
int ret = Remote()->Dump(fd, args);
if (ret != 0) {
return -1;
}
close(fd);
return ret;
}
int TestServiceProxy::TestRawDataTransaction(int length, int &reply)
{
int error;
int ret;
MessageOption option;
MessageParcel dataParcel, replyParcel;
ZLOGE(LABEL, "send to server data length = %{public}d", length);
@ -310,11 +287,11 @@ int TestServiceProxy::TestRawDataTransaction(int length, int &reply)
dataParcel.WriteInt32(length);
dataParcel.WriteRawData(buffer, length);
dataParcel.WriteInt32(length);
error = Remote()->SendRequest(TRANS_ID_RAWDATA_TRANSACTION, dataParcel, replyParcel, option);
ret = Remote()->SendRequest(TRANS_ID_RAWDATA_TRANSACTION, dataParcel, replyParcel, option);
reply = replyParcel.ReadInt32();
ZLOGE(LABEL, "get result from server data = %{public}d", reply);
delete [] buffer;
return error;
return ret;
}
int TestServiceProxy::TestRawDataReply(int length)
@ -555,8 +532,8 @@ int TestServiceProxy::TestAccessTokenID64(uint64_t token_expected, uint64_t ftok
ret = -1;
goto ERR;
}
token = replyParcel1.ReadUint64();
ftoken = replyParcel1.ReadUint64();
token = replyParcel1.ReadUint64();
ftoken = replyParcel1.ReadUint64();
if (token != token_expected) {
ZLOGE(LABEL, "token != token_expected, token:%{public}" PRIu64, token);
@ -675,7 +652,7 @@ int TestServiceProxy::TestFlushAsyncCalls(int count, int length)
int TestServiceProxy::TestMultipleProcesses(int data, int &rep, int delayTime)
{
int error;
int ret;
MessageOption option;
MessageParcel dataParcel, replyParcel;
ZLOGD(LABEL, "send to server data = %{public}d", data);
@ -683,10 +660,10 @@ int TestServiceProxy::TestMultipleProcesses(int data, int &rep, int delayTime)
dataParcel.WriteInt32(data);
dataParcel.WriteInt32(delayTime);
}
error = Remote()->SendRequest(TRANS_ID_MULTIPLE_PROCESSES, dataParcel, replyParcel, option);
ret = Remote()->SendRequest(TRANS_ID_MULTIPLE_PROCESSES, dataParcel, replyParcel, option);
rep = replyParcel.ReadInt32();
error += replyParcel.ReadInt32();
return error;
ret += replyParcel.ReadInt32();
return ret;
}
std::u16string TestServiceProxy::TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize)
@ -701,8 +678,8 @@ std::u16string TestServiceProxy::TestAshmem(sptr<Ashmem> ashmem, int32_t content
return u"";
}
int error = Remote()->SendRequest(TRANS_ID_ASHMEM, dataParcel, replyParcel, option);
if (error != ERR_NONE) {
int ret = Remote()->SendRequest(TRANS_ID_ASHMEM, dataParcel, replyParcel, option);
if (ret != ERR_NONE) {
return u"";
}
@ -742,421 +719,10 @@ int TestServiceProxy::TestNestingSend(int sendCode, int &replyCode)
ZLOGE(LABEL, "%{public}s: fail to write data", __func__);
return -1;
}
int error = Remote()->SendRequest(TRANS_ID_NESTING_SEND, dataParcel, replyParcel, option);
int ret = Remote()->SendRequest(TRANS_ID_NESTING_SEND, dataParcel, replyParcel, option);
replyCode = replyParcel.ReadInt32();
ZLOGW(LABEL, "%{public}s: outer = %{public}d, inner = %{public}d", __func__, error, replyCode);
return error;
}
int32_t TestServiceStub::ServerSyncTransaction(MessageParcel &data, MessageParcel &reply)
{
int32_t result = 0;
int32_t reqData = data.ReadInt32();
int32_t delayTime = data.ReadInt32();
int32_t ret = TestSyncTransaction(reqData, result, delayTime);
reply.WriteInt32(result);
ZLOGW(LABEL, "%{public}s: outer = %{public}d, inner = %{public}d", __func__, ret, replyCode);
return ret;
}
int32_t TestServiceStub::ServerAsyncTransaction(MessageParcel &data, MessageParcel &reply)
{
int32_t result = 0;
int32_t reqData = data.ReadInt32();
int timeout = data.ReadInt32();
bool acquireResult = data.ReadBool();
if (acquireResult) {
sptr<IFoo> fooProxy = iface_cast<IFoo>(data.ReadRemoteObject());
if (fooProxy != nullptr) {
TestAsyncCallbackTrans(reqData, result, timeout);
fooProxy->SendAsyncReply(result);
}
} else {
(void)TestAsyncTransaction(reqData, timeout);
}
return 0;
}
int32_t TestServiceStub::ServerPingService(MessageParcel &data, MessageParcel &reply)
{
std::u16string serviceName = data.ReadString16();
int32_t result = TestPingService(serviceName);
ZLOGD(LABEL, "%{public}s:PingService: result=%{public}d", __func__, result);
reply.WriteInt32(result);
return 0;
}
int32_t TestServiceStub::ServerGetFooService(MessageParcel &data, MessageParcel &reply)
{
sptr<IFoo> fooService = TestGetFooService();
sptr<IRemoteObject> object = fooService->AsObject();
object->SetBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
reply.WriteRemoteObject(object);
return 0;
}
int32_t TestServiceStub::ServerTransactFileDesc(MessageParcel &data, MessageParcel &reply)
{
int desc = TestGetFileDescriptor();
reply.WriteFileDescriptor(desc);
close(desc);
return 0;
}
int32_t TestServiceStub::ServerStringTransaction(MessageParcel &data, MessageParcel &reply)
{
const std::string testString = data.ReadString();
int testSize = TestStringTransaction(testString);
reply.WriteInt32(testSize);
reply.WriteString(testString);
return 0;
}
int32_t TestServiceStub::ServerZtraceTransaction(MessageParcel &data, MessageParcel &reply)
{
int32_t ret = 0;
int32_t len = data.ReadInt32();
if (len < 0) {
ZLOGE(LABEL, "%{public}s:get len failed, len = %{public}d", __func__, len);
return ret;
}
std::string recvString;
std::string replyString(len, 0);
recvString = data.ReadString();
ret = TestZtraceTransaction(recvString, replyString, len);
if (!ret) {
reply.WriteString(replyString);
}
return ret;
}
int32_t TestServiceStub::ServerCallingUidAndPid(MessageParcel &data, MessageParcel &reply)
{
reply.WriteInt32(IPCSkeleton::GetCallingUid());
reply.WriteInt32(IPCSkeleton::GetCallingPid());
ZLOGE(LABEL, "calling before reset uid = %{public}d, pid = %{public}d",
IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid());
std::string token = IPCSkeleton::ResetCallingIdentity();
ZLOGE(LABEL, "calling before set uid = %{public}d, pid = %{public}d",
IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid());
if (!IPCSkeleton::SetCallingIdentity(token)) {
ZLOGE(LABEL, "Set Calling Identity fail");
return 0;
}
ZLOGE(LABEL, "calling after set uid = %{public}d, pid = %{public}d",
IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid());
return 0;
}
int32_t TestServiceStub::ServerNestingSend(MessageParcel &data, MessageParcel &reply)
{
sptr<IRemoteObject> object = data.ReadRemoteObject();
sptr<IFoo> foo = iface_cast<IFoo>(object);
int innerResult = foo->TestNestingSend(data.ReadInt32());
reply.WriteInt32(innerResult);
return 0;
}
int32_t TestServiceStub::ServerAccessTokenId(MessageParcel &data, MessageParcel &reply)
{
int32_t token = static_cast<int32_t>(IPCSkeleton::GetCallingTokenID());
int32_t ftoken = static_cast<int32_t>(IPCSkeleton::GetFirstTokenID());
ZLOGE(LABEL, "server GetCallingTokenID:%{public}d", token);
ZLOGE(LABEL, "server GetFirstTokenID:%{public}d", ftoken);
reply.WriteInt32(token);
reply.WriteInt32(ftoken);
return 0;
}
int32_t TestServiceStub::ServerAccessTokenId64(MessageParcel &data, MessageParcel &reply)
{
uint64_t token = IPCSkeleton::GetCallingFullTokenID();
uint64_t ftoken = IPCSkeleton::GetFirstFullTokenID();
ZLOGE(LABEL, "server GetCallingFullTokenID:%{public}" PRIu64, token);
ZLOGE(LABEL, "server GetFirstFullTokenID:%{public}" PRIu64, ftoken);
reply.WriteUint64(token);
reply.WriteUint64(ftoken);
return 0;
}
int32_t TestServiceStub::ServerMessageParcelAddped(MessageParcel &data, MessageParcel &reply)
{
reply.WriteInt32(data.ReadInt32());
reply.WriteInt32(data.ReadInt32());
reply.WriteString(data.ReadString());
return 0;
}
int32_t TestServiceStub::ServerMessageParcelAddpedWithObject(MessageParcel &data, MessageParcel &reply)
{
reply.WriteInt32(data.ReadInt32());
reply.WriteInt32(data.ReadInt32());
reply.WriteString(data.ReadString());
reply.WriteRemoteObject(data.ReadRemoteObject());
reply.WriteFileDescriptor(data.ReadFileDescriptor());
return 0;
}
int32_t TestServiceStub::ServerEnableSerialInvokeFlag(MessageParcel &data, MessageParcel &reply)
{
int32_t ret = 0;
static int sendCount = 0;
if (sendCount == 0) {
if (serialInvokeFlag_) {
std::cout<< "Enable serial invoke flag" << std::endl;
} else {
std::cout<< "Not enable serial invoke flag" << std::endl;
}
}
reply.WriteString(data.ReadString());
sendCount++;
if (sendCount >= MAX_RECURSIVE_SENDS) {
return ret;
}
MessageOption option;
MessageParcel dataParcel;
MessageParcel replyParcel;
std::string value = std::to_string(sendCount); // The last time was 4.
std::cout << "Current thread ID = " << std::this_thread::get_id();
std::cout << " Send to server data = " << value << std::endl;
dataParcel.WriteString(value);
ret = IPCObjectStub::SendRequest(TRANS_ENABLE_SERIAL_INVOKE_FLAG, dataParcel, replyParcel, option);
std::string result = replyParcel.ReadString();
std::cout << "Current thread ID = " << std::this_thread::get_id();
std::cout << " Get result from server data = " << result << std::endl;
return ret;
}
int32_t TestServiceStub::RegisterRemoteStub(MessageParcel &data, MessageParcel &reply)
{
std::string descriptor = data.ReadString();
auto remoteObject = data.ReadRemoteObject();
return TestRegisterRemoteStub(descriptor.c_str(), remoteObject);
}
int32_t TestServiceStub::UnRegisterRemoteStub(MessageParcel &data, MessageParcel &reply)
{
std::string descriptor = data.ReadString();
return TestUnRegisterRemoteStub(descriptor.c_str());
}
int32_t TestServiceStub::QueryRemoteProxy(MessageParcel &data, MessageParcel &reply)
{
std::string descriptor = data.ReadString();
sptr<IRemoteObject> remoteObject = TestQueryRemoteProxy(descriptor.c_str());
return reply.WriteRemoteObject(remoteObject);
}
int TestServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
std::map<uint32_t, OHOS::TestServiceStub::TestServiceStubFunc>::iterator it = funcMap_.find(code);
if (it != funcMap_.end()) {
OHOS::TestServiceStub::TestServiceStubFunc itFunc = it->second;
if (itFunc != nullptr) {
return (this->*itFunc)(data, reply);
}
}
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
int32_t TestServiceStub::TransferRawData(MessageParcel &data, MessageParcel &reply)
{
ZLOGD(LABEL, "enter transfer raw data");
int length = data.ReadInt32();
if (length <= 1) {
ZLOGE(LABEL, "%{public}s: length should > 1, length is %{public}d", __func__, length);
if (!reply.WriteInt32(length)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
if (!data.ContainFileDescriptors()) {
ZLOGE(LABEL, "sent raw data is less than 32k");
}
const char *buffer = nullptr;
if ((buffer = reinterpret_cast<const char *>(data.ReadRawData((size_t)length))) == nullptr) {
ZLOGE(LABEL, "%{public}s:read raw data failed, length = %{public}d", __func__, length);
if (reply.WriteInt32(0)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
if (buffer[0] != 'a' || buffer[length - 1] != 'z') {
ZLOGE(LABEL, "%{public}s:buffer error, length = %{public}d", __func__, length);
if (reply.WriteInt32(0)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
if (data.ReadInt32() != length) {
ZLOGE(LABEL, "%{public}s:read raw data after failed, length = %{public}d", __func__, length);
if (!reply.WriteInt32(0)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
if (!reply.WriteInt32(length)) {
ZLOGE(LABEL, "fail to write parcel");
return ERR_INVALID_DATA;
}
return ERR_NONE;
}
int32_t TestServiceStub::ReplyRawData(MessageParcel &data, MessageParcel &reply)
{
ZLOGD(LABEL, "enter reply raw data");
int length = data.ReadInt32();
if (length <= 1) {
ZLOGE(LABEL, "%{public}s: length should > 1, length is %{public}d", __func__, length);
if (!reply.WriteInt32(length)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
unsigned char *buffer = new (std::nothrow) unsigned char[length];
if (buffer == nullptr) {
ZLOGE(LABEL, "new buffer failed of length = %{public}d", length);
return ERR_INVALID_STATE;
}
buffer[0] = 'a';
buffer[length - 1] = 'z';
if (!reply.WriteInt32(length) ||
!reply.WriteRawData(buffer, (size_t)length) ||
!reply.WriteInt32(length)) {
ZLOGE(LABEL, "fail to write parcel");
delete [] buffer;
return ERR_INVALID_STATE;
}
delete [] buffer;
return ERR_NONE;
}
int32_t TestServiceStub::TransferToNextProcess(MessageParcel &data, MessageParcel &reply)
{
int32_t reqData = data.ReadInt32();
int32_t delayTime = data.ReadInt32();
int result;
int ret = TestSyncTransaction(reqData, result, delayTime);
reply.WriteInt32(result);
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (saMgr == nullptr) {
reply.WriteInt32(ERR_TRANSACTION_FAILED);
return ERR_TRANSACTION_FAILED;
}
sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_EXTRA_TEST_SERVICE);
if (object == nullptr) {
reply.WriteInt32(ERR_TRANSACTION_FAILED);
return ERR_TRANSACTION_FAILED;
}
sptr<ITestService> testService = iface_cast<ITestService>(object);
if (testService == nullptr) {
reply.WriteInt32(ERR_TRANSACTION_FAILED);
return ERR_TRANSACTION_FAILED;
}
ret += testService->TestSyncTransaction(reqData, result, delayTime);
ret += testService->TestAsyncCallbackTrans(reqData, result, delayTime);
if (ret != 0) {
reply.WriteInt32(ERR_TRANSACTION_FAILED);
return ERR_TRANSACTION_FAILED;
}
reply.WriteInt32(ERR_NONE);
return ERR_NONE;
}
int32_t TestServiceStub::ReadAshmem(MessageParcel &data, MessageParcel &reply)
{
int32_t contentSize = data.ReadInt32();
if (contentSize < 0) {
reply.WriteInt32(-1);
return ERR_TRANSACTION_FAILED;
}
sptr<Ashmem> ashmem = data.ReadAshmem();
if (ashmem == nullptr) {
reply.WriteInt32(-1);
return ERR_TRANSACTION_FAILED;
}
int32_t ashmemSize = ashmem->GetAshmemSize();
if (ashmemSize < contentSize || !ashmem->MapReadOnlyAshmem()) {
reply.WriteInt32(-1);
return ERR_TRANSACTION_FAILED;
}
const void *content = ashmem->ReadFromAshmem(contentSize, 0);
if (content == nullptr) {
reply.WriteInt32(-1);
ashmem->UnmapAshmem();
ashmem->CloseAshmem();
return ERR_TRANSACTION_FAILED;
}
auto pt = static_cast<const char *>(content);
std::string str(pt, contentSize);
ashmem->UnmapAshmem();
ashmem->CloseAshmem();
std::string name = "ashmem2";
sptr<Ashmem> ashmem2 = Ashmem::CreateAshmem(name.c_str(), ashmemSize);
if (ashmem2 == nullptr || !ashmem2->MapReadAndWriteAshmem() ||
!ashmem2->WriteToAshmem(str.c_str(), contentSize, 0)) {
reply.WriteInt32(-1);
if (ashmem2 != nullptr) {
ashmem2->UnmapAshmem();
ashmem2->CloseAshmem();
}
return ERR_TRANSACTION_FAILED;
}
reply.WriteInt32(contentSize);
reply.WriteAshmem(ashmem2);
ashmem2->UnmapAshmem();
ashmem2->CloseAshmem();
return ERR_NONE;
}
int32_t TestServiceStub::ServerFlushAsyncCalls(MessageParcel &data, MessageParcel &reply)
{
(void)data.ReadString16();
return ERR_NONE;
}
bool TestDeathRecipient::gotDeathRecipient_ = false;
bool TestDeathRecipient::GotDeathRecipient()
{
return gotDeathRecipient_;
}
void TestDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
{
gotDeathRecipient_ = true;
ZLOGD(LABEL, "recv death notice");
}
TestDeathRecipient::TestDeathRecipient()
{
}
TestDeathRecipient::~TestDeathRecipient()
{
}
} // namespace OHOS

View File

@ -0,0 +1,469 @@
/*
* 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 "test_service_stub.h"
#include <cinttypes>
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <thread>
#include "access_token_adapter.h"
#include "ipc_debug.h"
#include "string_ex.h"
#include "iremote_proxy.h"
#include "ipc_skeleton.h"
#include "ipc_file_descriptor.h"
#include "ipc_test_helper.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
namespace OHOS {
using namespace OHOS::HiviewDFX;
constexpr int32_t MAX_RECURSIVE_SENDS = 5;
TestServiceStub::TestServiceStub(bool serialInvokeFlag)
: IRemoteStub(serialInvokeFlag), serialInvokeFlag_(serialInvokeFlag)
{
InitMessageProcessMap();
}
void TestServiceStub::InitMessageProcessMap()
{
funcMap_[static_cast<uint32_t>(TRANS_ID_SYNC_TRANSACTION)] = &TestServiceStub::ServerSyncTransaction;
funcMap_[static_cast<uint32_t>(TRANS_ID_ASYNC_TRANSACTION)] = &TestServiceStub::ServerAsyncTransaction;
funcMap_[static_cast<uint32_t>(TRANS_ID_PING_SERVICE)] = &TestServiceStub::ServerPingService;
funcMap_[static_cast<uint32_t>(TRANS_ID_GET_FOO_SERVICE)] = &TestServiceStub::ServerGetFooService;
funcMap_[static_cast<uint32_t>(TRANS_ID_TRANSACT_FILE_DESC)] = &TestServiceStub::ServerTransactFileDesc;
funcMap_[static_cast<uint32_t>(TRANS_ID_STRING_TRANSACTION)] = &TestServiceStub::ServerStringTransaction;
funcMap_[static_cast<uint32_t>(TRANS_ID_ZTRACE_TRANSACTION)] = &TestServiceStub::ServerZtraceTransaction;
funcMap_[static_cast<uint32_t>(TRANS_ID_RAWDATA_TRANSACTION)] = &TestServiceStub::TransferRawData;
funcMap_[static_cast<uint32_t>(TRANS_ID_RAWDATA_REPLY)] = &TestServiceStub::ReplyRawData;
funcMap_[static_cast<uint32_t>(TRANS_ID_FLUSH_ASYNC_CALLS)] = &TestServiceStub::ServerFlushAsyncCalls;
funcMap_[static_cast<uint32_t>(TRANS_ID_ASHMEM)] = &TestServiceStub::ReadAshmem;
funcMap_[static_cast<uint32_t>(TRANS_ID_MULTIPLE_PROCESSES)] = &TestServiceStub::TransferToNextProcess;
funcMap_[static_cast<uint32_t>(TRANS_ID_CALLING_UID_PID)] = &TestServiceStub::ServerCallingUidAndPid;
funcMap_[static_cast<uint32_t>(TRANS_ID_NESTING_SEND)] = &TestServiceStub::ServerNestingSend;
funcMap_[static_cast<uint32_t>(TRANS_ID_ACCESS_TOKENID)] = &TestServiceStub::ServerAccessTokenId;
funcMap_[static_cast<uint32_t>(TRANS_ID_ACCESS_TOKENID_64)] = &TestServiceStub::ServerAccessTokenId64;
funcMap_[static_cast<uint32_t>(TRANS_MESSAGE_PARCEL_ADDPED)] = &TestServiceStub::ServerMessageParcelAddped;
funcMap_[static_cast<uint32_t>(TRANS_MESSAGE_PARCEL_ADDPED_WITH_OBJECT)] =
&TestServiceStub::ServerMessageParcelAddpedWithObject;
funcMap_[static_cast<uint32_t>(TRANS_ENABLE_SERIAL_INVOKE_FLAG)] = &TestServiceStub::ServerEnableSerialInvokeFlag;
funcMap_[static_cast<uint32_t>(TRANS_ID_REGISTER_REMOTE_STUB_OBJECT)] = &TestServiceStub::RegisterRemoteStub;
funcMap_[static_cast<uint32_t>(TRANS_ID_UNREGISTER_REMOTE_STUB_OBJECT)] = &TestServiceStub::UnRegisterRemoteStub;
funcMap_[static_cast<uint32_t>(TRANS_ID_QUERY_REMOTE_PROXY_OBJECT)] = &TestServiceStub::QueryRemoteProxy;
}
int32_t TestServiceStub::ServerSyncTransaction(MessageParcel &data, MessageParcel &reply)
{
int32_t result = 0;
int32_t reqData = data.ReadInt32();
int32_t delayTime = data.ReadInt32();
int32_t ret = TestSyncTransaction(reqData, result, delayTime);
reply.WriteInt32(result);
return ret;
}
int32_t TestServiceStub::ServerAsyncTransaction(MessageParcel &data, MessageParcel &reply)
{
int32_t result = 0;
int32_t reqData = data.ReadInt32();
int timeout = data.ReadInt32();
bool acquireResult = data.ReadBool();
if (acquireResult) {
sptr<IFoo> fooProxy = iface_cast<IFoo>(data.ReadRemoteObject());
if (fooProxy != nullptr) {
TestAsyncCallbackTrans(reqData, result, timeout);
fooProxy->SendAsyncReply(result);
}
} else {
(void)TestAsyncTransaction(reqData, timeout);
}
return 0;
}
int32_t TestServiceStub::ServerPingService(MessageParcel &data, MessageParcel &reply)
{
std::u16string serviceName = data.ReadString16();
int32_t result = TestPingService(serviceName);
ZLOGD(LABEL, "%{public}s:PingService: result=%{public}d", __func__, result);
reply.WriteInt32(result);
return 0;
}
int32_t TestServiceStub::ServerGetFooService(MessageParcel &data, MessageParcel &reply)
{
sptr<IFoo> fooService = TestGetFooService();
sptr<IRemoteObject> object = fooService->AsObject();
object->SetBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
reply.WriteRemoteObject(object);
return 0;
}
int32_t TestServiceStub::ServerTransactFileDesc(MessageParcel &data, MessageParcel &reply)
{
int desc = TestGetFileDescriptor();
reply.WriteFileDescriptor(desc);
close(desc);
return 0;
}
int32_t TestServiceStub::ServerStringTransaction(MessageParcel &data, MessageParcel &reply)
{
const std::string testString = data.ReadString();
int testSize = TestStringTransaction(testString);
reply.WriteInt32(testSize);
reply.WriteString(testString);
return 0;
}
int32_t TestServiceStub::ServerZtraceTransaction(MessageParcel &data, MessageParcel &reply)
{
int32_t ret = 0;
int32_t len = data.ReadInt32();
if (len < 0) {
ZLOGE(LABEL, "%{public}s:get len failed, len = %{public}d", __func__, len);
return ret;
}
std::string recvString;
std::string replyString(len, 0);
recvString = data.ReadString();
ret = TestZtraceTransaction(recvString, replyString, len);
if (!ret) {
reply.WriteString(replyString);
}
return ret;
}
int32_t TestServiceStub::ServerCallingUidAndPid(MessageParcel &data, MessageParcel &reply)
{
reply.WriteInt32(IPCSkeleton::GetCallingUid());
reply.WriteInt32(IPCSkeleton::GetCallingPid());
ZLOGE(LABEL, "calling before reset uid = %{public}d, pid = %{public}d",
IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid());
std::string token = IPCSkeleton::ResetCallingIdentity();
ZLOGE(LABEL, "calling before set uid = %{public}d, pid = %{public}d",
IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid());
if (!IPCSkeleton::SetCallingIdentity(token)) {
ZLOGE(LABEL, "Set Calling Identity fail");
return 0;
}
ZLOGE(LABEL, "calling after set uid = %{public}d, pid = %{public}d",
IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid());
return 0;
}
int32_t TestServiceStub::ServerNestingSend(MessageParcel &data, MessageParcel &reply)
{
sptr<IRemoteObject> object = data.ReadRemoteObject();
sptr<IFoo> foo = iface_cast<IFoo>(object);
int innerResult = foo->TestNestingSend(data.ReadInt32());
reply.WriteInt32(innerResult);
return 0;
}
int32_t TestServiceStub::ServerAccessTokenId(MessageParcel &data, MessageParcel &reply)
{
int32_t token = static_cast<int32_t>(IPCSkeleton::GetCallingTokenID());
int32_t ftoken = static_cast<int32_t>(IPCSkeleton::GetFirstTokenID());
ZLOGE(LABEL, "server GetCallingTokenID:%{public}d", token);
ZLOGE(LABEL, "server GetFirstTokenID:%{public}d", ftoken);
reply.WriteInt32(token);
reply.WriteInt32(ftoken);
return 0;
}
int32_t TestServiceStub::ServerAccessTokenId64(MessageParcel &data, MessageParcel &reply)
{
uint64_t token = IPCSkeleton::GetCallingFullTokenID();
uint64_t ftoken = IPCSkeleton::GetFirstFullTokenID();
ZLOGE(LABEL, "server GetCallingFullTokenID:%{public}" PRIu64, token);
ZLOGE(LABEL, "server GetFirstFullTokenID:%{public}" PRIu64, ftoken);
reply.WriteUint64(token);
reply.WriteUint64(ftoken);
return 0;
}
int32_t TestServiceStub::ServerMessageParcelAddped(MessageParcel &data, MessageParcel &reply)
{
reply.WriteInt32(data.ReadInt32());
reply.WriteInt32(data.ReadInt32());
reply.WriteString(data.ReadString());
return 0;
}
int32_t TestServiceStub::ServerMessageParcelAddpedWithObject(MessageParcel &data, MessageParcel &reply)
{
reply.WriteInt32(data.ReadInt32());
reply.WriteInt32(data.ReadInt32());
reply.WriteString(data.ReadString());
reply.WriteRemoteObject(data.ReadRemoteObject());
reply.WriteFileDescriptor(data.ReadFileDescriptor());
return 0;
}
int32_t TestServiceStub::ServerEnableSerialInvokeFlag(MessageParcel &data, MessageParcel &reply)
{
int32_t ret = 0;
static int sendCount = 0;
if (sendCount == 0) {
if (serialInvokeFlag_) {
ZLOGI(LABEL, "Enable serial invoke flag");
} else {
ZLOGI(LABEL, "Not enable serial invoke flag");
}
}
reply.WriteString(data.ReadString());
sendCount++;
if (sendCount >= MAX_RECURSIVE_SENDS) {
return ret;
}
MessageOption option;
MessageParcel dataParcel;
MessageParcel replyParcel;
std::string value = std::to_string(sendCount); // The last time was 4.
std::thread::id id_1 = std::this_thread::get_id();
ZLOGI(LABEL, "Current thread ID = %{public}u", *(unsigned int*)&id_1);
ZLOGI(LABEL, "Send to server data = %{public}s", value.c_str());
dataParcel.WriteString(value);
ret = IPCObjectStub::SendRequest(TRANS_ENABLE_SERIAL_INVOKE_FLAG, dataParcel, replyParcel, option);
std::string result = replyParcel.ReadString();
std::thread::id id_2 = std::this_thread::get_id();
ZLOGI(LABEL, "Current thread ID = %{public}u", *(unsigned int*)&id_2);
ZLOGI(LABEL, "Get result from server data = %{public}s", result.c_str());
return ret;
}
int32_t TestServiceStub::RegisterRemoteStub(MessageParcel &data, MessageParcel &reply)
{
std::string descriptor = data.ReadString();
auto remoteObject = data.ReadRemoteObject();
return TestRegisterRemoteStub(descriptor.c_str(), remoteObject);
}
int32_t TestServiceStub::UnRegisterRemoteStub(MessageParcel &data, MessageParcel &reply)
{
std::string descriptor = data.ReadString();
return TestUnRegisterRemoteStub(descriptor.c_str());
}
int32_t TestServiceStub::QueryRemoteProxy(MessageParcel &data, MessageParcel &reply)
{
std::string descriptor = data.ReadString();
sptr<IRemoteObject> remoteObject = TestQueryRemoteProxy(descriptor.c_str());
return reply.WriteRemoteObject(remoteObject);
}
int TestServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
std::map<uint32_t, OHOS::TestServiceStub::TestServiceStubFunc>::iterator it = funcMap_.find(code);
if (it != funcMap_.end()) {
OHOS::TestServiceStub::TestServiceStubFunc itFunc = it->second;
if (itFunc != nullptr) {
return (this->*itFunc)(data, reply);
}
}
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
int32_t TestServiceStub::TransferRawData(MessageParcel &data, MessageParcel &reply)
{
ZLOGD(LABEL, "enter transfer raw data");
int length = data.ReadInt32();
if (length <= 1) {
ZLOGE(LABEL, "%{public}s: length should > 1, length is %{public}d", __func__, length);
if (!reply.WriteInt32(length)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
if (!data.ContainFileDescriptors()) {
ZLOGE(LABEL, "sent raw data is less than 32k");
}
const char *buffer = nullptr;
if ((buffer = reinterpret_cast<const char *>(data.ReadRawData((size_t)length))) == nullptr) {
ZLOGE(LABEL, "%{public}s:read raw data failed, length = %{public}d", __func__, length);
if (reply.WriteInt32(0)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
if (buffer[0] != 'a' || buffer[length - 1] != 'z') {
ZLOGE(LABEL, "%{public}s:buffer error, length = %{public}d", __func__, length);
if (reply.WriteInt32(0)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
if (data.ReadInt32() != length) {
ZLOGE(LABEL, "%{public}s:read raw data after failed, length = %{public}d", __func__, length);
if (!reply.WriteInt32(0)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
if (!reply.WriteInt32(length)) {
ZLOGE(LABEL, "fail to write parcel");
return ERR_INVALID_DATA;
}
return ERR_NONE;
}
int32_t TestServiceStub::ReplyRawData(MessageParcel &data, MessageParcel &reply)
{
ZLOGD(LABEL, "enter reply raw data");
int length = data.ReadInt32();
if (length <= 1) {
ZLOGE(LABEL, "%{public}s: length should > 1, length is %{public}d", __func__, length);
if (!reply.WriteInt32(length)) {
ZLOGE(LABEL, "fail to write parcel");
}
return ERR_INVALID_DATA;
}
unsigned char *buffer = new (std::nothrow) unsigned char[length];
if (buffer == nullptr) {
ZLOGE(LABEL, "new buffer failed of length = %{public}d", length);
return ERR_INVALID_STATE;
}
buffer[0] = 'a';
buffer[length - 1] = 'z';
if (!reply.WriteInt32(length) ||
!reply.WriteRawData(buffer, (size_t)length) ||
!reply.WriteInt32(length)) {
ZLOGE(LABEL, "fail to write parcel");
delete [] buffer;
return ERR_INVALID_STATE;
}
delete [] buffer;
return ERR_NONE;
}
int32_t TestServiceStub::TransferToNextProcess(MessageParcel &data, MessageParcel &reply)
{
int32_t reqData = data.ReadInt32();
int32_t delayTime = data.ReadInt32();
int result;
int ret = TestSyncTransaction(reqData, result, delayTime);
reply.WriteInt32(result);
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (saMgr == nullptr) {
reply.WriteInt32(ERR_TRANSACTION_FAILED);
return ERR_TRANSACTION_FAILED;
}
sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_EXTRA_TEST_SERVICE);
if (object == nullptr) {
reply.WriteInt32(ERR_TRANSACTION_FAILED);
return ERR_TRANSACTION_FAILED;
}
sptr<ITestService> testService = iface_cast<ITestService>(object);
if (testService == nullptr) {
reply.WriteInt32(ERR_TRANSACTION_FAILED);
return ERR_TRANSACTION_FAILED;
}
ret += testService->TestSyncTransaction(reqData, result, delayTime);
ret += testService->TestAsyncCallbackTrans(reqData, result, delayTime);
if (ret != 0) {
reply.WriteInt32(ERR_TRANSACTION_FAILED);
return ERR_TRANSACTION_FAILED;
}
reply.WriteInt32(ERR_NONE);
return ERR_NONE;
}
int32_t TestServiceStub::ReadAshmem(MessageParcel &data, MessageParcel &reply)
{
int32_t contentSize = data.ReadInt32();
if (contentSize < 0) {
reply.WriteInt32(-1);
return ERR_TRANSACTION_FAILED;
}
sptr<Ashmem> ashmem = data.ReadAshmem();
if (ashmem == nullptr) {
reply.WriteInt32(-1);
return ERR_TRANSACTION_FAILED;
}
int32_t ashmemSize = ashmem->GetAshmemSize();
if (ashmemSize < contentSize || !ashmem->MapReadOnlyAshmem()) {
reply.WriteInt32(-1);
return ERR_TRANSACTION_FAILED;
}
const void *content = ashmem->ReadFromAshmem(contentSize, 0);
if (content == nullptr) {
reply.WriteInt32(-1);
ashmem->UnmapAshmem();
ashmem->CloseAshmem();
return ERR_TRANSACTION_FAILED;
}
auto pt = static_cast<const char *>(content);
std::string str(pt, contentSize);
ashmem->UnmapAshmem();
ashmem->CloseAshmem();
std::string name = "ashmem2";
sptr<Ashmem> ashmem2 = Ashmem::CreateAshmem(name.c_str(), ashmemSize);
if (ashmem2 == nullptr || !ashmem2->MapReadAndWriteAshmem() ||
!ashmem2->WriteToAshmem(str.c_str(), contentSize, 0)) {
reply.WriteInt32(-1);
if (ashmem2 != nullptr) {
ashmem2->UnmapAshmem();
ashmem2->CloseAshmem();
}
return ERR_TRANSACTION_FAILED;
}
reply.WriteInt32(contentSize);
reply.WriteAshmem(ashmem2);
ashmem2->UnmapAshmem();
ashmem2->CloseAshmem();
return ERR_NONE;
}
int32_t TestServiceStub::ServerFlushAsyncCalls(MessageParcel &data, MessageParcel &reply)
{
(void)data.ReadString16();
return ERR_NONE;
}
} // namespace OHOS

View File

@ -21,7 +21,8 @@
#include "ipc_skeleton.h"
#include "ipc_test_helper.h"
#include "test_service.h"
#include "test_service_skeleton.h"
#include "test_service_death_recipient.h"
#include "test_service_proxy.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"