1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <iostream> 16 #define private public 17 #include "mock_dbinder_base_invoker.h" 18 #undef private 19 20 using namespace testing::ext; 21 using namespace OHOS; 22 using namespace OHOS::HiviewDFX; 23 24 class DBinderBaseInvokerUnitTest : public testing::Test { 25 public: SetUpTestCase(void)26 static void SetUpTestCase(void) {} TearDownTestCase(void)27 static void TearDownTestCase(void) {} SetUp()28 void SetUp() {} TearDown()29 void TearDown() {} 30 }; 31 32 /** 33 * @tc.name: ProcessTransactionAbnormalBranch001 34 * @tc.desc: Mock IRemoteObjectTranslateWhenRcv translate remote object fail 35 * @tc.type: FUNC 36 */ 37 HWTEST_F(DBinderBaseInvokerUnitTest, ProcessTransactionAbnormalBranch001, TestSize.Level1) 38 { 39 std::shared_ptr<MockDBinderBaseInvoker> invoker = std::make_shared<MockDBinderBaseInvoker>(); 40 EXPECT_TRUE(invoker != nullptr); 41 int32_t listenFd = 0; 42 std::shared_ptr<dbinder_transaction_data> tr(new dbinder_transaction_data); 43 EXPECT_TRUE(tr != nullptr); 44 (void)memset_s(tr.get(), sizeof(dbinder_transaction_data), 0, sizeof(dbinder_transaction_data)); 45 invoker->ProcessTransaction(tr.get(), listenFd); 46 EXPECT_EQ(invoker->result_, RPC_BASE_INVOKER_TRANSLATE_ERR); 47 } 48 49 /** 50 * @tc.name: ProcessTransactionAbnormalBranch002 51 * @tc.desc: Mock CheckAndSetCallerInfo return err branch 52 * @tc.type: FUNC 53 */ 54 HWTEST_F(DBinderBaseInvokerUnitTest, ProcessTransactionAbnormalBranch002, TestSize.Level1) 55 { 56 std::shared_ptr<MockDBinderBaseInvoker> invoker = std::make_shared<MockDBinderBaseInvoker>(); 57 EXPECT_TRUE(invoker != nullptr); 58 int32_t listenFd = 0; 59 std::shared_ptr<dbinder_transaction_data> tr(new dbinder_transaction_data); 60 EXPECT_TRUE(tr != nullptr); 61 (void)memset_s(tr.get(), sizeof(dbinder_transaction_data) + 1, 0, sizeof(dbinder_transaction_data) + 1); 62 tr->buffer_size = 1; 63 EXPECT_CALL(*invoker, 64 CheckAndSetCallerInfo(testing::_, testing::_)).WillOnce(testing::Return(RPC_DATABUS_INVOKER_INVALID_DATA_ERR)); 65 invoker->ProcessTransaction(tr.get(), listenFd); 66 EXPECT_EQ(invoker->result_, RPC_DATABUS_INVOKER_INVALID_DATA_ERR); 67 } 68 69 /** 70 * @tc.name: StartProcessLoopAbnormalBranch001 71 * @tc.desc: Mock StartProcessLoop can not get the IPCProcessSkeleton instance 72 * @tc.type: FUNC 73 */ 74 HWTEST_F(DBinderBaseInvokerUnitTest, StartProcessLoopAbnormalBranch001, TestSize.Level1) 75 { 76 testing::NiceMock<MockDBinderBaseInvoker> mock; 77 int32_t socketId = 0; 78 std::shared_ptr<dbinder_transaction_data> tr(new dbinder_transaction_data); 79 EXPECT_TRUE(tr != nullptr); 80 uint32_t size = sizeof(dbinder_transaction_data) + sizeof(binder_size_t); 81 82 tr->magic = DBINDER_MAGICWORD; 83 tr->flags = MessageOption::TF_STATUS_CODE; 84 tr->offsets = sizeof(binder_size_t); 85 tr->buffer_size = sizeof(binder_size_t); 86 tr->sizeOfSelf = sizeof(dbinder_transaction_data) + sizeof(binder_size_t); 87 88 IPCProcessSkeleton::exitFlag_ = true; 89 IPCProcessSkeleton::instance_ = nullptr; 90 91 mock.StartProcessLoop(socketId, reinterpret_cast<const char*>(tr.get()), size); 92 EXPECT_EQ(mock.result_, RPC_BASE_INVOKER_CURRENT_NULL_ERR); 93 94 IPCProcessSkeleton::exitFlag_ = false; 95 } 96 97 /** 98 * @tc.name: StartProcessLoopAbnormalBranch002 99 * @tc.desc: Mock StartProcessLoop can not get the IPCProcessSkeleton instance 100 * @tc.type: FUNC 101 */ 102 HWTEST_F(DBinderBaseInvokerUnitTest, StartProcessLoopAbnormalBranch002, TestSize.Level1) 103 { 104 testing::NiceMock<MockDBinderBaseInvoker> mock; 105 int32_t socketId = 0; 106 std::shared_ptr<dbinder_transaction_data> tr(new dbinder_transaction_data); 107 EXPECT_TRUE(tr != nullptr); 108 uint32_t size = SOCKET_MAX_BUFF_SIZE + 1; 109 110 tr->magic = DBINDER_MAGICWORD; 111 tr->flags = MessageOption::TF_STATUS_CODE; 112 tr->offsets = sizeof(binder_size_t); 113 tr->buffer_size = sizeof(binder_size_t); 114 tr->sizeOfSelf = sizeof(dbinder_transaction_data) + sizeof(binder_size_t); 115 116 mock.StartProcessLoop(socketId, reinterpret_cast<const char*>(tr.get()), size); 117 EXPECT_EQ(mock.result_, RPC_BASE_INVOKER_MALLOC_ERR); 118 } 119