1 /* 2 * Copyright (C) 2024-2024 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 <gtest/gtest.h> 17 #include "transmission_manager.h" 18 #include "server_session.h" 19 #include "client_session.h" 20 21 namespace OHOS { 22 namespace Telephony { 23 using namespace testing::ext; 24 25 class SessionCallbackTest : public ISessionCallback { 26 public: OnConnected()27 void OnConnected() override {} OnReceiveMsg(const char * data,uint32_t dataLen)28 void OnReceiveMsg(const char* data, uint32_t dataLen) override {} 29 }; 30 31 class DistributedTransmissionTest : public testing::Test { 32 public: SetUpTestCase()33 static void SetUpTestCase() {} TearDownTestCase()34 static void TearDownTestCase() {} SetUp()35 virtual void SetUp() {} TearDown()36 virtual void TearDown() {} 37 }; 38 39 /** 40 * @tc.number Telephony_DistributedTransmissionTest_001 41 * @tc.name test transmission manager 42 * @tc.desc Function test 43 */ 44 HWTEST_F(DistributedTransmissionTest, Telephony_DistributedTransmissionTest_001, Function | MediumTest | Level1) 45 { 46 std::shared_ptr<ISessionCallback> callback = std::make_shared<SessionCallbackTest>(); 47 auto session = DelayedSingleton<TransmissionManager>::GetInstance()->CreateServerSession(callback); 48 EXPECT_NE(session, nullptr); 49 DelayedSingleton<TransmissionManager>::GetInstance()->OnBind(0); 50 DelayedSingleton<TransmissionManager>::GetInstance()->OnReceiveMsg(0, nullptr, 0); 51 DelayedSingleton<TransmissionManager>::GetInstance()->OnShutdown(0); 52 session.reset(); 53 EXPECT_EQ(DelayedSingleton<TransmissionManager>::GetInstance()->session_.lock(), nullptr); 54 } 55 56 /** 57 * @tc.number Telephony_DistributedTransmissionTest_002 58 * @tc.name test client session 59 * @tc.desc Function test 60 */ 61 HWTEST_F(DistributedTransmissionTest, Telephony_DistributedTransmissionTest_002, Function | MediumTest | Level1) 62 { 63 std::shared_ptr<ISessionCallback> callback = std::make_shared<SessionCallbackTest>(); 64 auto session = DelayedSingleton<TransmissionManager>::GetInstance()->CreateClientSession(callback); 65 ASSERT_NE(session, nullptr); 66 session->socket_ = 0; 67 session->Connect("12345", SESSION_NAME, SESSION_NAME, 4194304); 68 session->socket_ = INVALID_SOCKET_ID; 69 session->Connect("", SESSION_NAME, SESSION_NAME, 4194304); 70 session->OnSessionBind(0); 71 session->Disconnect(); 72 session->OnSessionShutdown(0); 73 EXPECT_EQ(session->socket_, INVALID_SOCKET_ID); 74 75 std::string peerDevice = ""; 76 std::string localName = "localName"; 77 std::string peerName = "peerName"; 78 auto clientSession = std::make_shared<ClientSession>(callback); 79 clientSession->clientSocket_ = INVALID_SOCKET_ID + 1; 80 clientSession->Connect("", SESSION_NAME, SESSION_NAME, 4194304); // already connect 81 clientSession->socket_ = INVALID_SOCKET_ID + 1; 82 clientSession->Disconnect(); 83 clientSession->socket_ = 1; 84 clientSession->clientSocket_ = 1; 85 clientSession->OnSessionShutdown(1); 86 EXPECT_EQ(clientSession->CreateSocket(peerDevice, localName, peerName), INVALID_SOCKET_ID); 87 } 88 89 /** 90 * @tc.number Telephony_DistributedTransmissionTest_003 91 * @tc.name test server session 92 * @tc.desc Function test 93 */ 94 HWTEST_F(DistributedTransmissionTest, Telephony_DistributedTransmissionTest_003, Function | MediumTest | Level1) 95 { 96 std::shared_ptr<ISessionCallback> callback = std::make_shared<SessionCallbackTest>(); 97 auto session = std::make_shared<ServerSession>(callback); 98 ASSERT_NE(session, nullptr); 99 session->Create("", 4194304); 100 session->socket_ = 0; 101 session->Create(SESSION_NAME, 4194304); 102 session->socket_ = INVALID_SOCKET_ID; 103 session->Create(SESSION_NAME, 4194304); 104 session->OnSessionBind(66); 105 session->OnSessionShutdown(66); 106 session->OnSessionShutdown(session->serverSocket_); 107 session->serverSocket_ = 0; 108 109 session->serverSocket_ = INVALID_SOCKET_ID + 1; 110 EXPECT_NO_THROW(session->Create("123", 4194304)); 111 session->serverSocket_ = INVALID_SOCKET_ID; 112 EXPECT_NO_THROW(session->Destroy()); 113 session->serverSocket_ = INVALID_SOCKET_ID + 1; 114 EXPECT_NO_THROW(session->Destroy()); 115 EXPECT_EQ(session->serverSocket_, INVALID_SOCKET_ID); 116 } 117 118 /** 119 * @tc.number Telephony_DistributedTransmissionTest_004 120 * @tc.name test session adapter 121 * @tc.desc Function test 122 */ 123 HWTEST_F(DistributedTransmissionTest, Telephony_DistributedTransmissionTest_004, Function | MediumTest | Level1) 124 { 125 std::shared_ptr<ISessionCallback> callback = std::make_shared<SessionCallbackTest>(); 126 auto session = DelayedSingleton<TransmissionManager>::GetInstance()->CreateServerSession(callback); 127 ASSERT_NE(session, nullptr); 128 session->socket_ = 0; 129 session->SendMsg(nullptr, 0); 130 session->socket_ = INVALID_SOCKET_ID; 131 session->SendMsg(nullptr, 0); 132 session->OnReceiveMsg(0, nullptr, 0); 133 session->socket_ = 0; 134 session->OnReceiveMsg(0, nullptr, 0); 135 std::string name = "test"; 136 std::string network = "network"; 137 std::string pkgName = "pkgName"; 138 PeerSocketInfo info = { 139 .name = const_cast<char *>(name.c_str()), 140 .networkId = const_cast<char *>(network.c_str()), 141 .pkgName = const_cast<char *>(pkgName.c_str()), 142 .dataType = DATA_TYPE_RAW_STREAM_ENCRYPED 143 }; 144 SessionAdapter::OnBind(0, info); 145 SessionAdapter::OnShutdown(0, static_cast<ShutdownReason>(0)); 146 SessionAdapter::OnBytes(0, nullptr, 0); 147 SessionAdapter::OnError(0, 0); 148 session->socket_ = 0; 149 EXPECT_TRUE(session->IsReady()); 150 } 151 152 } // namespace Telephony 153 } // namespace OHOS 154