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 16 #include <fcntl.h> 17 #include <sys/mman.h> 18 #include "gtest/gtest.h" 19 #include "gmock/gmock.h" 20 #define private public 21 #define protected public 22 #include "api_caller_client.h" 23 #include "api_caller_server.h" 24 #undef private 25 26 using namespace std; 27 using namespace testing::ext; 28 using namespace OHOS; 29 using namespace OHOS::perftest; 30 31 class IpcTransactorTest : public testing::Test { 32 public: 33 ~IpcTransactorTest() override = default; 34 protected: 35 shared_ptr<ApiCallerClient> apiCallerClient_ = make_shared<ApiCallerClient>(); 36 shared_ptr<ApiCallerServer> apiCallerServer_ = make_shared<ApiCallerServer>(); TearDown()37 void TearDown() override 38 { 39 apiCallerClient_->connectState_ = UNINIT; 40 apiCallerClient_->remoteCaller_ = nullptr; 41 apiCallerClient_->processingApi_ = ""; 42 apiCallerServer_->connectState_ = UNINIT; 43 } 44 }; 45 46 HWTEST_F(IpcTransactorTest, testClientInitAndConnectPeer, TestSize.Level1) 47 { 48 ASSERT_EQ(apiCallerClient_->GetConnectionStat(), UNINIT); 49 bool result = apiCallerClient_->InitAndConnectPeer("TestToken", nullptr); 50 ASSERT_EQ(result, false); 51 ASSERT_EQ(apiCallerClient_->GetConnectionStat(), DISCONNECTED); 52 apiCallerClient_->connectState_ = CONNECTED; 53 ASSERT_EQ(apiCallerClient_->GetConnectionStat(), CONNECTED); 54 result = apiCallerClient_->InitAndConnectPeer("TestToken", nullptr); 55 ASSERT_EQ(result, true); 56 } 57 58 HWTEST_F(IpcTransactorTest, testClientTransact, TestSize.Level1) 59 { 60 apiCallerClient_->connectState_ = DISCONNECTED; 61 auto call = ApiCallInfo(); 62 auto reply = ApiReplyInfo(); 63 apiCallerClient_->Transact(call, reply); 64 ASSERT_EQ(reply.exception_.code_, ERR_INTERNAL); 65 apiCallerClient_->connectState_ = CONNECTED; 66 apiCallerClient_->remoteCaller_ = new ApiCallerProxy(new ApiCallerStub()); 67 apiCallerClient_->processingApi_ = "TestID"; 68 apiCallerClient_->Transact(call, reply); 69 ASSERT_EQ(reply.exception_.code_, ERR_API_USAGE); 70 } 71 72 HWTEST_F(IpcTransactorTest, testClientOnPeerDeath, TestSize.Level1) 73 { 74 ASSERT_EQ(apiCallerClient_->GetConnectionStat(), UNINIT); 75 apiCallerClient_->OnPeerDeath(); 76 ASSERT_EQ(apiCallerClient_->GetConnectionStat(), DISCONNECTED); 77 } 78 79 HWTEST_F(IpcTransactorTest, testServerInitAndConnectPeer, TestSize.Level1) 80 { 81 ASSERT_EQ(apiCallerServer_->GetConnectionStat(), UNINIT); 82 bool result = apiCallerServer_->InitAndConnectPeer("TestToken", nullptr); 83 ASSERT_EQ(result, false); 84 } 85 86 HWTEST_F(IpcTransactorTest, testServerTransact, TestSize.Level1) 87 { 88 apiCallerServer_->connectState_ = DISCONNECTED; 89 auto call = ApiCallInfo(); 90 auto reply = ApiReplyInfo(); 91 apiCallerServer_->Transact(call, reply); 92 ASSERT_EQ(reply.exception_.code_, ERR_INTERNAL); 93 } 94 95 HWTEST_F(IpcTransactorTest, testServerOnPeerDeath, TestSize.Level1) 96 { 97 ASSERT_EQ(apiCallerServer_->GetConnectionStat(), UNINIT); 98 apiCallerServer_->OnPeerDeath(); 99 ASSERT_EQ(apiCallerServer_->GetConnectionStat(), DISCONNECTED); 100 }