From 76a50785fdc369900980d72a5fe5d71889de8cba Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 9 Dec 2022 09:20:27 +0800 Subject: [PATCH] update ut Signed-off-by: wanderer-dl122 --- ipc/native/test/unittest/common/BUILD.gn | 3 + .../databus_session_callback_unittest.cpp | 49 ++++++ .../common/ipc_file_desc_unittest.cpp | 51 ++++++ .../common/ipc_thread_pool_unittest.cpp | 147 ++++++++++++++++++ .../common/message_parcel_unittest.cpp | 26 ++++ 5 files changed, 276 insertions(+) create mode 100644 ipc/native/test/unittest/common/ipc_thread_pool_unittest.cpp diff --git a/ipc/native/test/unittest/common/BUILD.gn b/ipc/native/test/unittest/common/BUILD.gn index 470c330..c601ed7 100644 --- a/ipc/native/test/unittest/common/BUILD.gn +++ b/ipc/native/test/unittest/common/BUILD.gn @@ -38,6 +38,7 @@ ohos_unittest("IPCNativeUnitTest") { "ipc_object_proxy_unittest.cpp", "ipc_process_skeleton_unittest.cpp", "ipc_thread_skeleton_unittest.cpp", + "ipc_thread_pool_unittest.cpp", "iremote_object_unitest.cpp", "message_parcel_unittest.cpp", ] @@ -69,6 +70,8 @@ ohos_unittest("IPCFileDescOpsTest") { include_dirs = [ "//utils/system/safwk/native/include", + "//foundation/communication/ipc/ipc/native/c/rpc/include", + "//foundation/communication/ipc/ipc/native/c/ipc_adapter/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", "//foundation/communication/ipc/interfaces/innerkits/libdbinder/include", "//foundation/communication/ipc/native/src/core/include", diff --git a/ipc/native/test/unittest/common/databus_session_callback_unittest.cpp b/ipc/native/test/unittest/common/databus_session_callback_unittest.cpp index f60dba6..9632305 100644 --- a/ipc/native/test/unittest/common/databus_session_callback_unittest.cpp +++ b/ipc/native/test/unittest/common/databus_session_callback_unittest.cpp @@ -16,9 +16,13 @@ #include #include +#define private public #include "databus_session_callback.h" +#include "ipc_thread_skeleton.h" #include "ipc_types.h" +#include "iremote_object.h" #include "mock_session_impl.h" +#undef private using namespace testing::ext; using namespace OHOS; @@ -131,3 +135,48 @@ HWTEST_F(DbSessionCallbackUnitTest, OnSessionOpenedTest003, TestSize.Level1) EXPECT_EQ(ret, SESSION_UNOPEN_ERR); } + +/** + * @tc.name: OnSessionOpenedTest004 + * @tc.desc: Verify the OnSessionOpened function + * @tc.type: FUNC + */ +HWTEST_F(DbSessionCallbackUnitTest, OnSessionOpenedTest004, TestSize.Level1) +{ + std::shared_ptr session = std::make_shared(); + + EXPECT_CALL(*session, GetChannelId()) + .WillRepeatedly(testing::Return(1)); + + EXPECT_CALL(*session, IsServerSide()) + .WillRepeatedly(testing::Return(true)); + + EXPECT_CALL(*session, GetPeerPid()) + .WillRepeatedly(testing::Return(PID_TEST)); + + EXPECT_CALL(*session, GetPeerUid()) + .WillRepeatedly(testing::Return(UID_TEST)); + + EXPECT_CALL(*session, GetPeerDeviceId()) + .WillRepeatedly(testing::ReturnRef(DEVICE_ID_TEST)); + + EXPECT_CALL(*session, GetMySessionName()) + .WillRepeatedly(testing::ReturnRef(SESSION_NAME_TEST)); + + EXPECT_CALL(*session, GetPeerSessionName()) + .WillRepeatedly(testing::ReturnRef(PEER_SESSION_NAME_TEST)); + + IRemoteInvoker *invoker = nullptr; + IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent(); + current->invokers_[IRemoteObject::IF_PROT_DATABUS] = invoker; + + DatabusSessionCallback dbSessionCallback; + int ret = dbSessionCallback.OnSessionOpened(session); + + EXPECT_EQ(ret, SESSION_INVOKER_NULL_ERR); + char data[] = "testdata"; + ssize_t len = strlen(data); + dbSessionCallback.OnBytesReceived(session, data, len); + dbSessionCallback.OnSessionClosed(session); + +} \ No newline at end of file diff --git a/ipc/native/test/unittest/common/ipc_file_desc_unittest.cpp b/ipc/native/test/unittest/common/ipc_file_desc_unittest.cpp index 4d0b0d9..6723be2 100644 --- a/ipc/native/test/unittest/common/ipc_file_desc_unittest.cpp +++ b/ipc/native/test/unittest/common/ipc_file_desc_unittest.cpp @@ -18,9 +18,14 @@ #include #include #include + +#define private public +#include "binder_invoker.h" #include "ipc_debug.h" #include "ipc_file_descriptor.h" #include "log_tags.h" +#include "ipc_thread_skeleton.h" +#undef private namespace OHOS { using namespace testing::ext; @@ -90,4 +95,50 @@ HWTEST_F(IPCFileDescOpsTest, fd_parcelable_003, TestSize.Level1) fdesc.SetFd(fd); EXPECT_EQ(fd, fdesc.GetFd()); } + +HWTEST_F(IPCFileDescOpsTest, Marshalling001, TestSize.Level1) +{ + int fd = 9876; + IPCFileDescriptor fdesc; + + fdesc.SetFd(fd); + BinderInvoker *invoker = nullptr; + IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent(); + current->invokers_[IRemoteObject::IF_PROT_DEFAULT] = invoker; + + Parcel parcel; + auto ret = fdesc.Marshalling(parcel); + ASSERT_FALSE(ret); +} + +HWTEST_F(IPCFileDescOpsTest, Unmarshalling001, TestSize.Level1) +{ + int fd = 9876; + IPCFileDescriptor fdesc; + + fdesc.SetFd(fd); + BinderInvoker *invoker = nullptr; + IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent(); + current->invokers_[IRemoteObject::IF_PROT_DEFAULT] = invoker; + + Parcel parcel; + auto ret = fdesc.Unmarshalling(parcel); + ASSERT_TRUE(ret == nullptr); +} + +HWTEST_F(IPCFileDescOpsTest, Unmarshalling002, TestSize.Level1) +{ + int fd = 9876; + IPCFileDescriptor fdesc; + Parcel parcel; + + fdesc.SetFd(fd); + BinderInvoker *invoker = new BinderInvoker(); + invoker->WriteFileDescriptor(parcel, fd, true); + IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent(); + current->invokers_[IRemoteObject::IF_PROT_DEFAULT] = invoker; + + auto ret = fdesc.Unmarshalling(parcel); + ASSERT_TRUE(ret != nullptr); +} } // namespace OHOS diff --git a/ipc/native/test/unittest/common/ipc_thread_pool_unittest.cpp b/ipc/native/test/unittest/common/ipc_thread_pool_unittest.cpp new file mode 100644 index 0000000..52e8590 --- /dev/null +++ b/ipc/native/test/unittest/common/ipc_thread_pool_unittest.cpp @@ -0,0 +1,147 @@ +/* + * 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 + * + * 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 +#include + +#define private public +#include "ipc_types.h" +#include "ipc_thread_pool.h" +#include "ipc_workthread.h" +#undef private + +using namespace testing::ext; +using namespace OHOS; + +class IPCWorkThreadPoolUnitTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void IPCWorkThreadPoolUnitTest::SetUpTestCase() +{ +} + +void IPCWorkThreadPoolUnitTest::TearDownTestCase() +{ +} + +void IPCWorkThreadPoolUnitTest::SetUp() {} + +void IPCWorkThreadPoolUnitTest::TearDown() {} + +/** + * @tc.name: RemoveThreadTest001 + * @tc.desc: Verify the RemoveThread function + * @tc.type: FUNC + */ +HWTEST_F(IPCWorkThreadPoolUnitTest, RemoveThreadTest001, TestSize.Level1) +{ + IPCWorkThreadPool threadPool(1); + std::string threadName = "threadName1"; + + auto ipcThread = new (std::nothrow) IPCWorkThread(threadName); + threadPool.threads_[threadName] = ipcThread; + ipcThread->proto_ = IRemoteObject::IF_PROT_DEFAULT; + + auto ret = threadPool.RemoveThread(threadName); + + EXPECT_EQ(ret, true); +} + +/** + * @tc.name: RemoveThreadTest002 + * @tc.desc: Verify the RemoveThread function + * @tc.type: FUNC + */ +HWTEST_F(IPCWorkThreadPoolUnitTest, RemoveThreadTest002, TestSize.Level1) +{ + IPCWorkThreadPool threadPool(1); + std::string threadName = "threadName1"; + + auto ipcThread = new (std::nothrow) IPCWorkThread(threadName); + threadPool.threads_[threadName] = ipcThread; + ipcThread->proto_ = IRemoteObject::IF_PROT_DATABUS; + + auto ret = threadPool.RemoveThread(threadName); + + EXPECT_EQ(ret, true); +} + +/** + * @tc.name: RemoveThreadTest004 + * @tc.desc: Verify the RemoveThread function + * @tc.type: FUNC + */ +HWTEST_F(IPCWorkThreadPoolUnitTest, RemoveThreadTest004, TestSize.Level1) +{ + IPCWorkThreadPool threadPool(1); + std::string threadName = "threadName1"; + + auto ipcThread = new (std::nothrow) IPCWorkThread(threadName); + threadPool.threads_[threadName] = ipcThread; + ipcThread->proto_ = IRemoteObject::IF_PROT_ERROR; + + auto ret = threadPool.RemoveThread(threadName); + + EXPECT_EQ(ret, true); +} + +/** + * @tc.name: RemoveThreadTest005 + * @tc.desc: Verify the RemoveThread function + * @tc.type: FUNC + */ +HWTEST_F(IPCWorkThreadPoolUnitTest, RemoveThreadTest005, TestSize.Level1) +{ + IPCWorkThreadPool threadPool(1); + std::string threadName = "threadName1"; + + threadPool.threads_[threadName] = nullptr; + auto ret = threadPool.RemoveThread(threadName); + + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: RemoveThreadTest006 + * @tc.desc: Verify the RemoveThread function + * @tc.type: FUNC + */ +HWTEST_F(IPCWorkThreadPoolUnitTest, RemoveThreadTest006, TestSize.Level1) +{ + IPCWorkThreadPool threadPool(1); + std::string threadName = "threadName1"; + + auto ret = threadPool.RemoveThread(threadName); + + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: UpdateMaxThreadNumTest001 + * @tc.desc: Verify the UpdateMaxThreadNum function + * @tc.type: FUNC + */ +HWTEST_F(IPCWorkThreadPoolUnitTest, UpdateMaxThreadNumTest001, TestSize.Level1) +{ + IPCWorkThreadPool threadPool(1); + threadPool.UpdateMaxThreadNum(0); + + EXPECT_EQ(threadPool.maxThreadNum_, 1); +} \ No newline at end of file diff --git a/ipc/native/test/unittest/common/message_parcel_unittest.cpp b/ipc/native/test/unittest/common/message_parcel_unittest.cpp index ad7e8af..55e1a14 100644 --- a/ipc/native/test/unittest/common/message_parcel_unittest.cpp +++ b/ipc/native/test/unittest/common/message_parcel_unittest.cpp @@ -384,4 +384,30 @@ HWTEST_F(MessageParcelTest, WriteAshmemTest001, TestSize.Level1) auto ret = parcel.WriteAshmem(ashmem); ASSERT_TRUE(ret == true); ashmem->CloseAshmem(); +} + +/** + * @tc.name: ReadAshmemTest001 + * @tc.desc: Verify the MessageParcel::ReadAshmem function + * @tc.type: FUNC + */ +HWTEST_F(MessageParcelTest, ReadAshmemTest001, TestSize.Level1) +{ + MessageParcel parcel; + + auto ret = parcel.ReadAshmem(); + ASSERT_TRUE(ret == nullptr); +} + +/** + * @tc.name: AppendTest001 + * @tc.desc: Verify the MessageParcel::Append function + * @tc.type: FUNC + */ +HWTEST_F(MessageParcelTest, AppendTest001, TestSize.Level1) +{ + MessageParcel parcel; + MessageParcel data; + auto ret = parcel.Append(data); + ASSERT_TRUE(ret); } \ No newline at end of file -- GitLab