test: add IPCDbinderDataBusInvokerTest

Signed-off-by: chenchong_666 <chenchong57@huawei.com>
This commit is contained in:
chenchong_666 2024-10-28 21:36:06 +08:00
parent 6ea3b6f20a
commit e2d0ecd860
5 changed files with 430 additions and 9 deletions

View File

@ -15,6 +15,7 @@
#include <algorithm>
#include <gtest/gtest.h>
#include "mock_iremote_object.h"
#include "ipc_debug.h"
#include "ipc_skeleton.h"
#include "ipc_test_helper.h"
@ -24,10 +25,14 @@
#define private public
#include "dbinder_databus_invoker.h"
#include "mock_buffer_object.h"
#include "mock_dbinder_session_object.h"
#include "mock_ipc_process_skeleton.h"
#undef private
#include "mock_iremote_invoker.h"
#include "dbinder_session_object.h"
#include "iostream"
using namespace std;
using namespace testing::ext;
using namespace OHOS;
@ -308,6 +313,115 @@ HWTEST_F(IPCDbinderDataBusInvokerTest, NewSessionOfBinderProxy003, TestSize.Leve
delete invoker;
}
/**
* @tc.name: NewSessionOfBinderProxy004
* @tc.desc: NewSessionOfBinderProxy
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, NewSessionOfBinderProxy004, TestSize.Level1)
{
uint32_t handle = REGISTRY_HANDLE;
std::shared_ptr<DBinderSessionObject> remoteSession =
std::make_shared<DBinderSessionObject>(SERVICE_NAME_TEST, DEVICE_ID_TEST, 1, nullptr, 1);
EXPECT_TRUE (remoteSession != nullptr);
IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
MockIPCProcessSkeleton *instance = new MockIPCProcessSkeleton();
EXPECT_CALL(*instance, GetCurrent())
.WillRepeatedly(testing::Return(nullptr));
current->instance_ = instance;
DBinderDatabusInvoker testInvoker;
std::shared_ptr<DBinderSessionObject> ret = testInvoker.NewSessionOfBinderProxy(handle, remoteSession);
EXPECT_TRUE (ret == nullptr);
delete instance;
current->instance_ = nullptr;
current->exitFlag_ = false;
}
/**
* @tc.name: NewSessionOfBinderProxy005
* @tc.desc: NewSessionOfBinderProxy
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, NewSessionOfBinderProxy005, TestSize.Level1)
{
uint32_t handle = REGISTRY_HANDLE;
std::shared_ptr<DBinderSessionObject> remoteSession =
std::make_shared<DBinderSessionObject>(SERVICE_NAME_TEST, DEVICE_ID_TEST, 1, nullptr, 1);
EXPECT_TRUE (remoteSession != nullptr);
IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
MockIPCProcessSkeleton *instance = new MockIPCProcessSkeleton();
EXPECT_CALL(*instance, FindOrNewObject(handle, nullptr))
.WillRepeatedly(testing::Return(nullptr));
current->instance_ = instance;
DBinderDatabusInvoker testInvoker;
std::shared_ptr<DBinderSessionObject> ret = testInvoker.NewSessionOfBinderProxy(handle, remoteSession);
EXPECT_TRUE (ret == nullptr);
delete instance;
current->instance_ = nullptr;
current->exitFlag_ = false;
}
/**
* @tc.name: NewSessionOfBinderProxy006
* @tc.desc: NewSessionOfBinderProxy
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, NewSessionOfBinderProxy006, TestSize.Level1)
{
uint32_t handle = REGISTRY_HANDLE;
std::shared_ptr<DBinderSessionObject> remoteSession =
std::make_shared<DBinderSessionObject>(SERVICE_NAME_TEST, DEVICE_ID_TEST, 1, nullptr, 1);
EXPECT_TRUE (remoteSession != nullptr);
IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
MockIPCProcessSkeleton *instance = new MockIPCProcessSkeleton();
sptr<MockIPCObjectProxy> proxy = sptr<MockIPCObjectProxy>::MakeSptr();
EXPECT_CALL(*instance, FindOrNewObject(handle, nullptr))
.WillRepeatedly(testing::Return(proxy));
EXPECT_CALL(*proxy, GetProto())
.WillRepeatedly(testing::Return(IRemoteObject::IF_PROT_ERROR));
current->instance_ = instance;
DBinderDatabusInvoker testInvoker;
std::shared_ptr<DBinderSessionObject> ret = testInvoker.NewSessionOfBinderProxy(handle, remoteSession);
EXPECT_TRUE (ret == nullptr);
delete instance;
current->instance_ = nullptr;
current->exitFlag_ = false;
}
/**
* @tc.name: GetSessionForProxy001
* @tc.desc: GetSessionForProxy
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, GetSessionForProxy001, TestSize.Level1)
{
std::shared_ptr<DBinderSessionObject> remoteSession =
std::make_shared<DBinderSessionObject>(SERVICE_NAME_TEST, DEVICE_ID_TEST, 1, nullptr, 1);
EXPECT_TRUE (remoteSession != nullptr);
sptr<MockIPCObjectProxy> proxy = sptr<MockIPCObjectProxy>::MakeSptr();
EXPECT_CALL(*proxy, GetSessionName())
.WillRepeatedly(testing::Return("session_name"));
EXPECT_CALL(*proxy, InvokeListenThread(testing::_, testing::_))
.WillRepeatedly(testing::Return(0));
DBinderDatabusInvoker testInvoker;
std::shared_ptr<DBinderSessionObject> ret = testInvoker.GetSessionForProxy(
proxy, remoteSession, "local_device_id");
EXPECT_TRUE (ret == nullptr);
}
/**
* @tc.name: AuthSession2Proxy001
* @tc.desc: AuthSession2Proxy
@ -401,11 +515,34 @@ HWTEST_F(IPCDbinderDataBusInvokerTest, QueryClientSessionObjectTest002, TestSize
EXPECT_TRUE (remoteSession != nullptr);
bool ret = current->StubAttachDBinderSession(handle, remoteSession);
EXPECT_TRUE(ret);
std::shared_ptr<DBinderSessionObject> session = testInvoker.QueryClientSessionObject(handle);
EXPECT_TRUE(session != nullptr);
}
/**
* @tc.name: QueryClientSessionObjectTest003
* @tc.desc: QueryClientSessionObject
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, QueryClientSessionObjectTest003, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
uint32_t handle = 0;
IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
MockIPCProcessSkeleton *instance = new MockIPCProcessSkeleton();
EXPECT_CALL(*instance, GetCurrent())
.WillRepeatedly(testing::Return(nullptr));
current->instance_ = instance;
std::shared_ptr<DBinderSessionObject> ret = testInvoker.QueryClientSessionObject(handle);
EXPECT_EQ(ret, nullptr);
delete instance;
current->instance_ = nullptr;
current->exitFlag_ = false;
}
/**
* @tc.name: QueryServerSessionObjectTest001
* @tc.desc: QueryServerSessionObject
@ -436,11 +573,34 @@ HWTEST_F(IPCDbinderDataBusInvokerTest, QueryServerSessionObjectTest002, TestSize
EXPECT_TRUE (remoteSession != nullptr);
bool ret = current->ProxyAttachDBinderSession(handle, remoteSession);
EXPECT_TRUE(ret);
std::shared_ptr<DBinderSessionObject> session = testInvoker.QueryServerSessionObject(handle);
EXPECT_TRUE(session != nullptr);
}
/**
* @tc.name: QueryServerSessionObjectTest003
* @tc.desc: QueryServerSessionObject
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, QueryServerSessionObjectTest003, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
uint32_t handle = 0;
IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
MockIPCProcessSkeleton *instance = new MockIPCProcessSkeleton();
EXPECT_CALL(*instance, GetCurrent())
.WillRepeatedly(testing::Return(nullptr));
current->instance_ = instance;
std::shared_ptr<DBinderSessionObject> ret = testInvoker.QueryServerSessionObject(handle);
EXPECT_EQ(ret, nullptr);
delete instance;
current->instance_ = nullptr;
current->exitFlag_ = false;
}
/**
* @tc.name: CreateProcessThreadTest001
* @tc.desc: CreateProcessThread
@ -546,7 +706,7 @@ HWTEST_F(IPCDbinderDataBusInvokerTest, OnReceiveNewConnectionTest001, TestSize.L
IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
auto handle = 1;
IPCObjectProxy *ipcProxy = new IPCObjectProxy(handle, u"testproxy");
std::shared_ptr<DBinderSessionObject> sessionObject =
std::make_shared<DBinderSessionObject>(SERVICE_NAME_TEST, DEVICE_ID_TEST, 1, ipcProxy, 1);
@ -565,6 +725,34 @@ HWTEST_F(IPCDbinderDataBusInvokerTest, OnReceiveNewConnectionTest001, TestSize.L
delete ipcProxy;
}
/**
* @tc.name: OnReceiveNewConnectionTest002
* @tc.desc: OnReceiveNewConnection
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, OnReceiveNewConnectionTest002, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
MockIPCProcessSkeleton *instance = new MockIPCProcessSkeleton();
EXPECT_CALL(*instance, GetCurrent())
.WillRepeatedly(testing::Return(nullptr));
current->instance_ = instance;
int32_t socketId = 1;
int peerPid = 1;
int peerUid = 1;
std::string peerName = SERVICE_NAME_TEST;
std::string networkId = DEVICE_ID_TEST;
bool ret = testInvoker.OnReceiveNewConnection(socketId, peerPid, peerUid,
peerName, networkId);
EXPECT_FALSE(ret);
delete instance;
current->instance_ = nullptr;
current->exitFlag_ = false;
}
/**
* @tc.name: ResetCallingIdentityTest001
* @tc.desc: ResetCallingIdentity
@ -796,6 +984,88 @@ HWTEST_F(IPCDbinderDataBusInvokerTest, OnSendMessage003, TestSize.Level1)
EXPECT_EQ(ret, -RPC_DATABUS_INVOKER_INVALID_DATA_ERR);
}
/**
* @tc.name: OnSendMessage004
* @tc.desc: OnSendMessage return RPC_DATABUS_INVOKER_INVALID_DATA_ERR
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, OnSendMessage004, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
MockDBinderSessionObject *sessionOfPeer = new MockDBinderSessionObject();
EXPECT_CALL(*sessionOfPeer, GetSocketId())
.WillRepeatedly(testing::Return(1));
EXPECT_CALL(*sessionOfPeer, GetSessionBuff())
.WillRepeatedly(testing::Return(nullptr));
std::shared_ptr<DBinderSessionObject> ptr(dynamic_cast<DBinderSessionObject *>(sessionOfPeer));
int ret = testInvoker.OnSendMessage(ptr);
EXPECT_EQ(ret, -RPC_DATABUS_INVOKER_INVALID_DATA_ERR);
}
/**
* @tc.name: OnSendMessage005
* @tc.desc: OnSendMessage return RPC_DATABUS_INVOKER_INVALID_DATA_ERR
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, OnSendMessage005, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
MockDBinderSessionObject *sessionOfPeer = new MockDBinderSessionObject();
std::shared_ptr<MockBufferObject> sessionBuff = std::make_shared<MockBufferObject>();
int32_t socketId = 1;
EXPECT_CALL(*sessionOfPeer, GetSocketId())
.WillRepeatedly(testing::Return(socketId));
EXPECT_CALL(*sessionOfPeer, GetSessionBuff())
.WillRepeatedly(testing::Return(sessionBuff));
EXPECT_CALL(*sessionBuff, GetSendBufferAndLock(testing::_))
.WillRepeatedly(testing::Return(nullptr));
std::shared_ptr<DBinderSessionObject> ptr(dynamic_cast<DBinderSessionObject *>(sessionOfPeer));
int ret = testInvoker.OnSendMessage(ptr);
EXPECT_EQ(ret, -RPC_DATABUS_INVOKER_INVALID_DATA_ERR);
}
/**
* @tc.name: SendData001
* @tc.desc: SendData return RPC_DATABUS_INVOKER_INVALID_DATA_ERR
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, SendData001, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
int32_t socketId = 2;
std::shared_ptr<BufferObject> sessionBuff = std::make_shared<BufferObject>();;
ssize_t writeCursor = 100;
ssize_t readCursor = 10;
sessionBuff->SetSendBufferWriteCursor(writeCursor);
sessionBuff->SetSendBufferReadCursor(readCursor);
int ret = testInvoker.SendData(sessionBuff, socketId);
EXPECT_NE(ret, 0);
}
/**
* @tc.name: SendData002
* @tc.desc: SendData return ERR_NONE
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, SendData002, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
int32_t socketId = 2;
std::shared_ptr<BufferObject> sessionBuff = std::make_shared<BufferObject>();;
ssize_t writeCursor = 10;
ssize_t readCursor = 10;
sessionBuff->SetSendBufferWriteCursor(writeCursor);
sessionBuff->SetSendBufferReadCursor(readCursor);
int ret = testInvoker.SendData(sessionBuff, socketId);
EXPECT_EQ(ret, 0);
}
/**
* @tc.name: OnSendRawData001
* @tc.desc: OnSendRawData return RPC_DATABUS_INVOKER_INVALID_DATA_ERR
@ -1030,7 +1300,7 @@ HWTEST_F(IPCDbinderDataBusInvokerTest, UnFlattenSession002, TestSize.Level1)
HWTEST_F(IPCDbinderDataBusInvokerTest, QueryHandleBySession001, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
std::shared_ptr<DBinderSessionObject> session =
std::make_shared<DBinderSessionObject>(SERVICE_NAME_TEST, DEVICE_ID_TEST, 1, nullptr, 1);
@ -1431,3 +1701,31 @@ HWTEST_F(IPCDbinderDataBusInvokerTest, UnFlattenSession006, TestSize.Level1)
auto ret = testInvoker.UnFlattenSession(sessionOffset, binderVersion);
EXPECT_NE(ret, nullptr);
}
/**
* @tc.name: OnMessageAvailable001
* @tc.desc: OnMessageAvailable
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, OnMessageAvailable001, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
int32_t socketId = 0;
const char *data = nullptr;
ssize_t len = 0;
testInvoker.OnMessageAvailable(socketId, data, len);
}
/**
* @tc.name: OnMessageAvailable002
* @tc.desc: OnMessageAvailable
* @tc.type: FUNC
*/
HWTEST_F(IPCDbinderDataBusInvokerTest, OnMessageAvailable002, TestSize.Level1)
{
DBinderDatabusInvoker testInvoker;
int32_t socketId = 1;
const char *data = new char();
ssize_t len = 200;
testInvoker.OnMessageAvailable(socketId, data, len);
}

View File

@ -0,0 +1,33 @@
/*
* 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_MOCK_BUFFER_OBJECT_H
#define OHOS_MOCK_BUFFER_OBJECT_H
#include <string>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "buffer_object.h"
namespace OHOS {
class MockBufferObject : public BufferObject {
public:
MOCK_METHOD1(GetSendBufferAndLock, char *(uint32_t size));
MOCK_CONST_METHOD0(GetSendBufferWriteCursor, ssize_t());
MOCK_CONST_METHOD0(GetSendBufferReadCursor, ssize_t());
};
} // namespace OHOS
#endif // OHOS_MOCK_BUFFER_OBJECT_H

View File

@ -0,0 +1,37 @@
/*
* 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_MOCK_DBINDER_SESSION_OBJECT_H
#define OHOS_MOCK_DBINDER_SESSION_OBJECT_H
#include <string>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "dbinder_session_object.h"
namespace OHOS {
class MockDBinderSessionObject : public DBinderSessionObject {
public:
MockDBinderSessionObject();
MOCK_METHOD0(GetSessionBuff, std::shared_ptr<BufferObject>());
MOCK_CONST_METHOD0(GetSocketId, int32_t());
};
MockDBinderSessionObject::MockDBinderSessionObject()
: DBinderSessionObject("serviceNameTest", "deviceidTest", 1, nullptr, 1)
{
}
} // namespace OHOS
#endif // OHOS_MOCK_DBINDER_SESSION_OBJECT_H

View File

@ -0,0 +1,32 @@
/*
* 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_MOCK_IPC_PROCESS_SKELETON_H
#define OHOS_MOCK_IPC_PROCESS_SKELETON_H
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "ipc_process_skeleton.h"
namespace OHOS {
class MockIPCProcessSkeleton : public IPCProcessSkeleton {
public:
MockIPCProcessSkeleton() {}
MOCK_METHOD0(GetCurrent, IPCProcessSkeleton* ());
MOCK_METHOD2(FindOrNewObject, sptr<IRemoteObject>(
int handle, const dbinder_negotiation_data *data));
};
} // namespace OHOS
#endif // OHOS_MOCK_IPC_PROCESS_SKELETON_H

View File

@ -20,8 +20,7 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "iremote_object.h"
#include "ipc_object_proxy.h"
namespace OHOS {
class MockDeathRecipient : public IRemoteObject::DeathRecipient {
@ -32,6 +31,28 @@ public:
{
(void)object;
}
}; // namespace OHOS
}
};
class MockIPCObjectProxy : public IPCObjectProxy {
public:
MockIPCObjectProxy() : IPCObjectProxy(1, u"mockProxyService") {};
~MockIPCObjectProxy() {};
MOCK_METHOD0(GetObjectRefCount, int32_t());
MOCK_METHOD0(GetSessionName, std::string());
MOCK_METHOD0(GetInterfaceDescriptor, std::u16string());
MOCK_METHOD1(AddDeathRecipient, bool(const sptr<DeathRecipient> &recipient));
MOCK_METHOD1(RemoveDeathRecipient, bool(const sptr<DeathRecipient> &recipient));
MOCK_METHOD2(Dump, int(int fd, const std::vector<std::u16string> &args));
MOCK_METHOD2(InvokeListenThread, int(MessageParcel &data, MessageParcel &reply));
MOCK_METHOD4(SendRequest, int(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option));
MOCK_CONST_METHOD0(GetProto, int());
MOCK_CONST_METHOD0(IsProxyObject, bool());
MOCK_CONST_METHOD0(IsObjectDead, bool());
MOCK_CONST_METHOD0(CheckObjectLegality, bool());
MOCK_CONST_METHOD0(GetObjectDescriptor, std::u16string());
MOCK_CONST_METHOD1(Marshalling, bool(Parcel &parcel));
};
} // namespace OHOS
#endif // OHOS_MOCK_DEATH_RECIPIENT_H