提交 3203c419 编写于 作者: Y yangguangzhao

RPC testcases for version control and access token

Signed-off-by: Nyangguangzhao <yangguangzhao1@huawei.com>
上级 d3d52ceb
......@@ -15,14 +15,14 @@
#include "rpc_feature_set.h"
#define RPC_FEATURE_LAST (43)
#define RPC_FEATURE_LAST 43
#define RPC_ACCESS_TOKEN_FLAG 0x1
static const uint32_t RPC_FEATURE_MAGIC_NUM = ('R' << 24) | ('F' << 16) | ('S' << 8) | RPC_FEATURE_LAST;
static const uint32_t RPC_ACCESS_TOKEN = 0;
static const uint32_t RpcFeatureFlag = 0x1;
static const uint32_t TokenIdSize = 4;
static const uint32_t RpcFeatureAck = 0x80000000;
static const uint32_t RPC_FEATURE_FLAG = 0x1;
static const uint32_t TOKEN_ID_SIZE = 4;
static const uint32_t RPC_FEATURE_ACK = 0x80000000;
uint32_t GetFeatureMagicNumber(void)
{
......@@ -36,12 +36,12 @@ uint32_t GetFeatureATTag(void)
uint32_t GetLocalRpcFeature(void)
{
return RpcFeatureFlag;
return RPC_FEATURE_FLAG;
}
uint32_t GetRpcFeatureAck(void)
{
return RpcFeatureAck;
return RPC_FEATURE_ACK;
}
bool IsATEnable(uint32_t featureSet)
......@@ -51,7 +51,7 @@ bool IsATEnable(uint32_t featureSet)
bool IsFeatureAck(uint32_t featureSet)
{
return (featureSet & RpcFeatureAck) > 0;
return (featureSet & RPC_FEATURE_ACK) > 0;
}
size_t GetATSize(uint32_t featureSet)
......@@ -65,5 +65,5 @@ size_t GetATSize(uint32_t featureSet)
uint32_t GetTokenIdSize(void)
{
return TokenIdSize;
return TOKEN_ID_SIZE;
}
\ No newline at end of file
......@@ -24,6 +24,7 @@ ohos_moduletest("RPCCLIENTTEST") {
"include",
"${SUBSYSTEM_DIR}/utils/include",
"${SUBSYSTEM_DIR}/ipc/native/c/adapter/access_token/include",
"${SUBSYSTEM_DIR}/ipc/native/c/rpc/include",
]
sources = [
"src/rpc_client_test.cpp",
......@@ -37,7 +38,6 @@ ohos_moduletest("RPCCLIENTTEST") {
external_deps = [
"dsoftbus_standard:softbus_client",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr_standard:samgr_proxy",
]
......@@ -61,7 +61,6 @@ ohos_moduletest("RPCSERVERTEST") {
external_deps = [
"dsoftbus_standard:softbus_client",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr_standard:samgr_proxy",
]
......
......@@ -16,16 +16,12 @@
#ifndef OHOS_RPC_FOO_TEST_H
#define OHOS_RPC_FOO_TEST_H
#include <mutex>
#include <condition_variable>
#include "iremote_broker.h"
#include "iremote_proxy.h"
#include "iremote_stub.h"
#include "hilog/log.h"
#include "log_tags.h"
namespace OHOS {
class IFoo : public IRemoteBroker {
class IRpcFooTest : public IRemoteBroker {
public:
enum FooInterFaceId {
GET_FOO_NAME = 0,
......@@ -33,47 +29,31 @@ public:
SEND_WRONG_REPLY = 2,
GET_TOKENID = 3,
};
virtual std::string GetFooName() = 0;
virtual void SendAsyncReply(int &reply) = 0;
virtual int TestNestingSend(int sendCode) = 0;
std::string GetFooName(void);
virtual std::string TestGetFooName(void) = 0;
virtual uint32_t TestAccessToken(void) = 0;
public:
DECLARE_INTERFACE_DESCRIPTOR(u"test.ipc.IFoo");
DECLARE_INTERFACE_DESCRIPTOR(u"test.rpc.IRpcFooTest");
private:
std::string fooName_ = "IRpcFooTest";
};
class FooStub : public IRemoteStub<IFoo> {
class RpcFooStub : public IRemoteStub<IRpcFooTest> {
public:
virtual ~FooStub();
int OnRemoteRequest(uint32_t code,
MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
std::string GetFooName() override;
void SendAsyncReply(int &reply) override;
int WaitForAsyncReply(int timeout);
static void CleanDecTimes();
static int GetDecTimes();
int TestNestingSend(int sendCode) override;
std::string TestGetFooName(void) override;
uint32_t TestAccessToken(void) override;
public:
static std::mutex decTimeMutex_;
static int decTimes_;
private:
int asyncReply_ = { 0 };
std::mutex mutex_;
std::condition_variable cv_;
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "FooStub" };
};
class FooProxy : public IRemoteProxy<IFoo> {
class RpcFooProxy : public IRemoteProxy<IRpcFooTest> {
public:
explicit FooProxy(const sptr<IRemoteObject> &impl);
~FooProxy() = default;
std::string GetFooName() override;
void SendAsyncReply(int &reply) override;
int TestNestingSend(int sendCode) override;
explicit RpcFooProxy(const sptr<IRemoteObject> &impl);
~RpcFooProxy() = default;
std::string TestGetFooName(void) override;
uint32_t TestAccessToken(void) override;
private:
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "FooProxy" };
static inline BrokerDelegator<FooProxy> delegator_;
static inline BrokerDelegator<RpcFooProxy> delegator_;
};
} // namespace OHOS
#endif // OHOS_RPC_FOO_TEST_H
......
......@@ -22,67 +22,62 @@
#include "ipc_object_proxy.h"
#include "rpc_test.h"
#include "access_token_adapter.h"
#include "log_tags.h"
#include "rpc_feature_set.h"
namespace OHOS {
#ifndef TITLE
#define TITLE __PRETTY_FUNCTION__
#endif
using namespace testing::ext;
using namespace OHOS;
using namespace OHOS::HiviewDFX;
static constexpr HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "RPCClientTest" };
#define DBINDER_LOGE(fmt, args...) \
(void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
#define DBINDER_LOGI(fmt, args...) \
(void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
static std::string g_deviceId;
class RPCClientTest : public testing::Test {
static sptr<IRpcFooTest> g_rpcTestProxy;
class RpcClientTest : public testing::Test {
public:
static constexpr int saId = RPC_TEST_SERVICE;
static constexpr char DBINDER_PKG_NAME[] = "DBinderService";
static constexpr int NODE_NUM = 4;
static void SetUpTestCase(void);
static void TearDownTestCase(void);
};
void RPCClientTest::SetUpTestCase()
void RpcClientTest::SetUpTestCase()
{
NodeBasicInfo *nodeInfo[NODE_NUM];
int32_t infoNum = NODE_NUM;
int32_t ret = GetAllNodeDeviceInfo(DBINDER_PKG_NAME, nodeInfo, &infoNum);
if (ret != 0) {
DBINDER_LOGE("get local node ret %{public}d", ret);
return;
}
if (infoNum == 0) {
DBINDER_LOGE("get no online nodes");
return;
}
ASSERT_EQ(ret, 0);
ASSERT_NE(infoNum, 0);
g_deviceId = nodeInfo[0]->networkId;
DBINDER_LOGI("get deviceid %{public}s", g_deviceId.c_str());
}
void RPCClientTest::TearDownTestCase() {}
void RpcClientTest::TearDownTestCase() {}
HWTEST_F(RPCClientTest, function_test_001, TestSize.Level1)
HWTEST_F(RpcClientTest, function_test_001, TestSize.Level1)
{
DBINDER_LOGI("Start RPCClient Testcase001");
// service instance
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
EXPECT_TRUE(saMgr != nullptr);
ASSERT_FALSE(saMgr == nullptr);
sptr<IRemoteObject> object = saMgr->GetSystemAbility(IPC_TEST_SERVICE, g_deviceId);
ASSERT_TRUE(object != nullptr);
sptr<IRemoteObject> object = saMgr->GetSystemAbility(RPC_TEST_SERVICE, g_deviceId);
ASSERT_TRUE(object != nullptr) << "deviceid is " << g_deviceId;
sptr<IFoo> testService = iface_cast<IFoo>(object);
ASSERT_TRUE(testService != nullptr);
g_rpcTestProxy = iface_cast<IRpcFooTest>(object);
ASSERT_TRUE(g_rpcTestProxy != nullptr);
}
HWTEST_F(RpcClientTest, function_test_002, TestSize.Level1)
{
std::string fooName = g_rpcTestProxy->TestGetFooName();
std::string selfFooName = g_rpcTestProxy->GetFooName();
ASSERT_TRUE(fooName == selfFooName) << "fooName: " << fooName << " fooName_: " << selfFooName;
}
HWTEST_F(RpcClientTest, function_test_003, TestSize.Level1)
{
uint32_t tokenId = RpcGetSelfTokenID();
uint32_t getTokenId = testService->TestAccessToken();
ASSERT_EQ(tokenId, getTokenId);
uint32_t getTokenId = g_rpcTestProxy->TestAccessToken();
if (IsATEnable(GetLocalRpcFeature()) == true) {
ASSERT_EQ(tokenId, getTokenId) << "deviceid is " << g_deviceId;
} else {
ASSERT_EQ(getTokenId, 0) << "deviceid is " << g_deviceId;
}
}
}
\ No newline at end of file
......@@ -19,43 +19,29 @@
#include "iservice_registry.h"
#include "ipc_skeleton.h"
#include "rpc_test.h"
#include "log_tags.h"
namespace OHOS {
#ifndef TITLE
#define TITLE __PRETTY_FUNCTION__
#endif
using namespace testing::ext;
using namespace OHOS;
using namespace OHOS::HiviewDFX;
static constexpr HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "RPCServerTest" };
#define DBINDER_LOGE(fmt, args...) \
(void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
#define DBINDER_LOGI(fmt, args...) \
(void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
class RPCServerTest : public testing::Test {
class RpcServerTest : public testing::Test {
public:
static constexpr int saId = RPC_TEST_SERVICE;
static void SetUpTestCase(void);
static void TearDownTestCase(void);
};
void RPCServerTest::SetUpTestCase() {}
void RpcServerTest::SetUpTestCase() {}
void RPCServerTest::TearDownTestCase() {}
void RpcServerTest::TearDownTestCase() {}
HWTEST_F(RPCServerTest, function_test_001, TestSize.Level1)
HWTEST_F(RpcServerTest, function_test_001, TestSize.Level1)
{
DBINDER_LOGI("Start RPCServer Testcase001");
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
EXPECT_TRUE(saMgr != nullptr);
ASSERT_TRUE(saMgr != nullptr);
ISystemAbilityManager::SAExtraProp saExtra;
saExtra.isDistributed = true; // 设置为分布式SA
int result = saMgr->AddSystemAbility(saId, new FooStub(), saExtra);
saExtra.isDistributed = true;
int result = saMgr->AddSystemAbility(RPC_TEST_SERVICE, new RpcFooStub(), saExtra);
ASSERT_EQ(result, 0);
IPCSkeleton::JoinWorkThread();
......
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Copyright (C) 2022 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
......@@ -15,32 +15,24 @@
#include "rpc_test.h"
#include "ipc_skeleton.h"
#include "ipc_debug.h"
#include "ipc_types.h"
namespace OHOS {
std::mutex FooStub::decTimeMutex_;
int FooStub::decTimes_ = 0;
std::string IRpcFooTest::GetFooName()
{
return fooName_;
}
int FooStub::OnRemoteRequest(uint32_t code,
int RpcFooStub::OnRemoteRequest(uint32_t code,
MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
int result = ERR_NONE;
switch (code) {
case GET_FOO_NAME: {
ZLOGI(LABEL, "%{public}s:called\n", __func__);
reply.WriteString(GetFooName());
break;
}
case SEND_ASYNC_REPLY: {
int32_t replyData = data.ReadInt32();
SendAsyncReply(replyData);
reply.WriteString(TestGetFooName());
break;
}
case SEND_WRONG_REPLY: {
return TestNestingSend(data.ReadInt32());
}
case GET_TOKENID: {
uint32_t tokenId = TestAccessToken();
reply.WriteUint32(tokenId);
......@@ -53,99 +45,39 @@ int FooStub::OnRemoteRequest(uint32_t code,
return result;
}
std::string FooStub::GetFooName()
{
return "ReallFoo";
}
int FooStub::WaitForAsyncReply(int timeout)
std::string RpcFooStub::TestGetFooName(void)
{
asyncReply_ = 0;
std::unique_lock<std::mutex> lck(mutex_);
cv_.wait_for(lck, std::chrono::milliseconds(timeout), [&]() {
return asyncReply_ != 0;
});
return asyncReply_;
return GetFooName();
}
void FooStub::SendAsyncReply(int &replyValue)
{
std::unique_lock<std::mutex> lck(mutex_);
asyncReply_ = replyValue;
cv_.notify_all();
}
FooStub::~FooStub()
{
std::unique_lock<std::mutex> lck(decTimeMutex_);
decTimes_++;
}
void FooStub::CleanDecTimes()
{
std::unique_lock<std::mutex> lck(decTimeMutex_);
decTimes_ = 0;
}
int FooStub::GetDecTimes()
{
std::unique_lock<std::mutex> lck(decTimeMutex_);
return decTimes_;
}
int FooStub::TestNestingSend(int sendCode)
{
return sendCode;
}
uint32_t FooStub::TestAccessToken(void)
uint32_t RpcFooStub::TestAccessToken(void)
{
uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
return tokenId;
}
FooProxy::FooProxy(const sptr<IRemoteObject> &impl)
: IRemoteProxy<IFoo>(impl)
RpcFooProxy::RpcFooProxy(const sptr<IRemoteObject> &impl)
: IRemoteProxy<IRpcFooTest>(impl)
{
}
std::string FooProxy::GetFooName()
{
ZLOGI(LABEL, "%{public}s:called\n", __func__);
MessageParcel data, reply;
MessageOption option;
Remote()->SendRequest(GET_FOO_NAME, data, reply, option);
return reply.ReadString();
}
void FooProxy::SendAsyncReply(int &replyValue)
{
ZLOGI(LABEL, "%{public}s:called\n", __func__);
MessageParcel data, reply;
MessageOption option = { MessageOption::TF_ASYNC };
data.WriteInt32(replyValue);
Remote()->SendRequest(SEND_ASYNC_REPLY, data, reply, option);
}
int FooProxy::TestNestingSend(int sendCode)
std::string RpcFooProxy::TestGetFooName(void)
{
MessageOption option;
MessageParcel dataParcel, replyParcel;
if (!dataParcel.WriteInt32(sendCode)) {
return -1;
int err = Remote()->SendRequest(GET_FOO_NAME, dataParcel, replyParcel, option);
if (err != 0) {
return "";
}
int error = Remote()->SendRequest(SEND_WRONG_REPLY, dataParcel, replyParcel, option);
ZLOGE(LABEL, "send foo result = %{public}d", error);
return error;
return replyParcel.ReadString();
}
uint32_t FooProxy::TestAccessToken(void)
uint32_t RpcFooProxy::TestAccessToken(void)
{
MessageOption option;
MessageParcel dataParcel, replyParcel;
int err = Remote()->SendRequest(GET_TOKENID, dataParcel, replyParcel, option);
if (err != 0) {
ZLOGE(LABEL, "get tokenid failed %{public}d", err);
return 0;
}
return replyParcel.ReadUint32();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册