• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "soft_bus_server_socket_test.h"
17 #include "soft_bus_server_socket.h"
18 #include "soft_bus_manager.h"
19 #include "remote_connect_listener_manager.h"
20 
21 #include "gtest/gtest.h"
22 #include "gmock/gmock.h"
23 
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 using namespace testing;
28 using namespace testing::ext;
29 
SetUpTestCase()30 void SoftBusServerSocketTest::SetUpTestCase()
31 {
32 }
33 
TearDownTestCase()34 void SoftBusServerSocketTest::TearDownTestCase()
35 {
36 }
37 
SetUp()38 void SoftBusServerSocketTest::SetUp()
39 {
40 }
41 
TearDown()42 void SoftBusServerSocketTest::TearDown()
43 {
44 }
45 
46 class ServerSocketTest : public ServerSocket {
47 public:
48     explicit ServerSocketTest(const int32_t socketId);
49     ~ServerSocketTest();
50     ResultCode SendMessage(const std::string &connectionName, const std::string &srcEndPoint,
51         const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes, MsgCallback &callback);
52     void OnBind(int32_t socketId, PeerSocketInfo info) override;
53 };
54 
ServerSocketTest(const int32_t socketId)55 ServerSocketTest::ServerSocketTest(const int32_t socketId)
56     : ServerSocket(socketId)
57 {
58 }
59 
~ServerSocketTest()60 ServerSocketTest::~ServerSocketTest()
61 {
62 }
63 
OnBind(int32_t socketId,PeerSocketInfo info)64 void ServerSocketTest::OnBind(int32_t socketId, PeerSocketInfo info)
65 {
66     return;
67 }
68 
SendMessage(const std::string & connectionName,const std::string & srcEndPoint,const std::string & destEndPoint,const std::shared_ptr<Attributes> & attributes,MsgCallback & callback)69 ResultCode ServerSocketTest::SendMessage(const std::string &connectionName, const std::string &srcEndPoint,
70     const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes, MsgCallback &callback)
71 {
72     return SUCCESS;
73 }
74 
75 HWTEST_F(SoftBusServerSocketTest, SoftBusServerSocketTestOnBind, TestSize.Level0)
76 {
77     int32_t socketId = 1;
78     ServerSocketTest *serverSocket = new ServerSocketTest(socketId);
79     PeerSocketInfo info;
80     EXPECT_NO_THROW(serverSocket->OnBind(-2, info));
81     delete serverSocket;
82 }
83 
84 HWTEST_F(SoftBusServerSocketTest, SoftBusServerSocketTestOnShutdown, TestSize.Level0)
85 {
86     int32_t socketId = 1;
87     ServerSocketTest *serverSocket = new ServerSocketTest(socketId);
88     const std::string connectionName = "connectionName";
89     ShutdownReason reason = SHUTDOWN_REASON_LOCAL;
90     serverSocket->AddClientConnection(socketId, connectionName);
91     EXPECT_NO_THROW(serverSocket->OnShutdown(socketId, reason));
92     EXPECT_NO_THROW(serverSocket->OnShutdown(socketId, reason));
93     serverSocket->clientConnectionMap_ = {};
94     delete serverSocket;
95 }
96 
97 HWTEST_F(SoftBusServerSocketTest, SoftBusServerSocketTestOnBytes, TestSize.Level0)
98 {
99     int32_t socketId = 1;
100     ServerSocketTest *serverSocket = new ServerSocketTest(socketId);
101     const std::string networkId = "connectionName";
102     void *data = new char[10];
103     uint32_t dataLen = 10;
104     EXPECT_NO_THROW(serverSocket->OnBytes(socketId, data, dataLen));
105     EXPECT_NO_THROW(serverSocket->AddServerSocket(socketId, networkId));
106     EXPECT_NO_THROW(serverSocket->OnBytes(socketId, data, dataLen));
107     serverSocket->serverSocketBindMap_ = {};
108     serverSocket->clientConnectionMap_ = {};
109     delete serverSocket;
110 }
111 
112 HWTEST_F(SoftBusServerSocketTest, SoftBusServerSocketTestAddServerSocket, TestSize.Level0)
113 {
114     int32_t socketId = 1;
115     ServerSocketTest *serverSocket = new ServerSocketTest(socketId);
116     const std::string networkId = "connectionName";
117     EXPECT_NO_THROW(serverSocket->AddServerSocket(socketId, networkId));
118     EXPECT_NO_THROW(serverSocket->AddServerSocket(socketId, networkId));
119     serverSocket->serverSocketBindMap_ = {};
120     delete serverSocket;
121 }
122 
123 HWTEST_F(SoftBusServerSocketTest, SoftBusServerSocketTestDeleteServerSocket, TestSize.Level0)
124 {
125     int32_t socketId = 1;
126     ServerSocketTest *serverSocket = new ServerSocketTest(socketId);
127     const std::string networkId = "connectionName";
128     EXPECT_NO_THROW(serverSocket->DeleteServerSocket(socketId));
129     EXPECT_NO_THROW(serverSocket->AddServerSocket(socketId, networkId));
130     EXPECT_NO_THROW(serverSocket->DeleteServerSocket(socketId));
131     delete serverSocket;
132 }
133 
134 HWTEST_F(SoftBusServerSocketTest, SoftBusServerSocketTestGetNetworkIdBySocketId, TestSize.Level0)
135 {
136     int32_t socketId = 1;
137     ServerSocketTest *serverSocket = new ServerSocketTest(socketId);
138     std::string result = serverSocket->GetNetworkIdBySocketId(socketId);
139     EXPECT_EQ(result, "");
140     const std::string networkId = "connectionName";
141     EXPECT_NO_THROW(serverSocket->AddServerSocket(socketId, networkId));
142     result = serverSocket->GetNetworkIdBySocketId(socketId);
143     EXPECT_EQ(result, networkId);
144     serverSocket->serverSocketBindMap_ = {};
145     delete serverSocket;
146 }
147 
148 HWTEST_F(SoftBusServerSocketTest, SoftBusServerSocketTestAddClientConnection, TestSize.Level0)
149 {
150     int32_t socketId = 1;
151     ServerSocketTest *serverSocket = new ServerSocketTest(socketId);
152     const std::string connectionName = "connectionName";
153     std::string clientConnectionName = serverSocket->GetClientConnectionName(socketId);
154     EXPECT_EQ(clientConnectionName, "");
155     EXPECT_NO_THROW(serverSocket->AddClientConnection(socketId, connectionName));
156     clientConnectionName = serverSocket->GetClientConnectionName(socketId);
157     EXPECT_EQ(clientConnectionName, connectionName);
158     EXPECT_NO_THROW(serverSocket->AddClientConnection(socketId, connectionName));
159     serverSocket->clientConnectionMap_ = {};
160     delete serverSocket;
161 }
162 
163 HWTEST_F(SoftBusServerSocketTest, SoftBusServerSocketTestDeleteClientConnection, TestSize.Level0)
164 {
165     int32_t socketId = 1;
166     ServerSocketTest *serverSocket = new ServerSocketTest(socketId);
167     const std::string connectionName = "connectionName";
168     EXPECT_NO_THROW(serverSocket->DeleteClientConnection(socketId));
169     EXPECT_NO_THROW(serverSocket->AddClientConnection(socketId, connectionName));
170     EXPECT_NO_THROW(serverSocket->DeleteClientConnection(socketId));
171     serverSocket->clientConnectionMap_ = {};
172     delete serverSocket;
173 }
174 
175 
176 HWTEST_F(SoftBusServerSocketTest, SoftBusServerSocketTestGetSocketIdByClientConnectionName, TestSize.Level0)
177 {
178     int32_t socketId = 1;
179     ServerSocketTest *serverSocket = new ServerSocketTest(socketId);
180     const std::string connectionName = "connectionName";
181     EXPECT_NO_THROW(serverSocket->AddClientConnection(socketId, connectionName));
182     int32_t result = serverSocket->GetSocketIdByClientConnectionName(connectionName);
183     ASSERT_EQ(result, socketId);
184     const std::string connectionName1 = "connectionName1";
185     result = serverSocket->GetSocketIdByClientConnectionName(connectionName1);
186     ASSERT_EQ(result, -1);
187     serverSocket->clientConnectionMap_ = {};
188     delete serverSocket;
189 }
190 } // namespace UserAuth
191 } // namespace UserIam
192 } // namespace OHOS
193