• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 <iostream>
18 #include <sstream>
19 #include <unistd.h>
20 #include "common/sharing_log.h"
21 #include "network/interfaces/iclient_callback.h"
22 #include "network/interfaces/inetwork_session_callback.h"
23 #include "network/interfaces/iserver_callback.h"
24 #include "network/network_factory.h"
25 
26 using namespace std;
27 using namespace OHOS::Sharing;
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace Sharing {
32 class NetworkUdpUnitTest : public testing::Test {
33 public:
SetUpTestCase()34     static void SetUpTestCase() {}
TearDownTestCase()35     static void TearDownTestCase() {}
SetUp()36     void SetUp() {}
TearDown()37     void TearDown() {}
38 };
39 
40 class UdpTestAgent final : public IServerCallback,
41                            public IClientCallback,
42                            public INetworkSessionCallback,
43                            public std::enable_shared_from_this<UdpTestAgent> {
44 public:
UdpTestAgent()45     UdpTestAgent() {}
46 
~UdpTestAgent()47     ~UdpTestAgent()
48     {
49         if (serverPtr_) {
50             serverPtr_->Stop();
51         }
52     }
53 
StartUdpServer(uint16_t port,const std::string & localIp)54     bool StartUdpServer(uint16_t port, const std::string &localIp)
55     {
56         bool ret = NetworkFactory::CreateUdpServer(port, localIp, shared_from_this(), serverPtr_);
57         if (!ret) {
58             SHARING_LOGE("===[UdpTestAgent] StartUdpServer failed");
59         } else {
60             SHARING_LOGD("===[UdpTestAgent] server agent started");
61         }
62         return ret;
63     }
64 
StartUdpClient(const std::string & peerIp,uint16_t peerPort,const std::string & localIp,uint16_t localPort)65     bool StartUdpClient(const std::string &peerIp, uint16_t peerPort, const std::string &localIp, uint16_t localPort)
66     {
67         bool ret =
68             NetworkFactory::CreateUdpClient(peerIp, peerPort, localIp, localPort, shared_from_this(), clientPtr_);
69         if (!ret) {
70             SHARING_LOGE("===[UdpTestAgent] StartUdpClient failed");
71         } else {
72             SHARING_LOGD("===[UdpTestAgent] client agent started");
73         }
74         return ret;
75     }
76 
OnAccept(std::weak_ptr<INetworkSession> session)77     void OnAccept(std::weak_ptr<INetworkSession> session) override
78     {
79         SHARING_LOGD("===[UdpTestAgent] OnServerAccept");
80     }
81 
OnServerReadData(int32_t fd,DataBuffer::Ptr buf,INetworkSession::Ptr session)82     void OnServerReadData(int32_t fd, DataBuffer::Ptr buf, INetworkSession::Ptr session) override
83     {
84         SHARING_LOGD("===[UdpTestAgent] OnServerReadData");
85         static int index = 0;
86         if (session) {
87             sessionPtrVec_.push_back(session);
88         }
89         DataBuffer::Ptr bufSend = std::make_shared<DataBuffer>();
90         const DataBuffer::Ptr &bufRefs = bufSend;
91         string msg = "udp server message.index=" + std::to_string(index++);
92         bufSend->PushData(msg.c_str(), msg.size());
93         size_t nSize = sessionPtrVec_.size();
94         for (size_t i = 0; i < nSize; i++) {
95             sessionPtrVec_[i]->Send(bufRefs, bufRefs->Size());
96             sleep(2);
97         }
98     }
99 
OnServerWriteable(int32_t fd)100     void OnServerWriteable(int32_t fd) override
101     {
102         SHARING_LOGD("onServerWriteable");
103     }
104 
OnServerClose(int32_t fd)105     void OnServerClose(int32_t fd) override
106     {
107         SHARING_LOGD("===[UdpTestAgent] OnServerClose");
108         serverPtr_ = nullptr;
109     }
110 
OnServerException(int32_t fd)111     void OnServerException(int32_t fd) override
112     {
113         SHARING_LOGD("===[UdpTestAgent] OnServerException");
114         serverPtr_ = nullptr;
115     }
116 
OnSessionReadData(int32_t fd,DataBuffer::Ptr buf)117     void OnSessionReadData(int32_t fd, DataBuffer::Ptr buf) override
118     {
119         SHARING_LOGD("===[UdpTestAgent] OnSessionReadData");
120         static int index = 0;
121         DataBuffer::Ptr bufSend = std::make_shared<DataBuffer>();
122         string msg = "udp session send message=" + std::to_string(index++);
123         bufSend->PushData(msg.c_str(), msg.size());
124         size_t nSize = sessionPtrVec_.size();
125         for (size_t i = 0; i < nSize; i++) {
126             if (sessionPtrVec_[i]->GetSocketInfo()->GetPeerFd() == fd) {
127                 sessionPtrVec_[i]->Send(bufSend, bufSend->Size());
128                 break;
129             }
130         }
131         sleep(3);
132     }
133 
OnSessionWriteable(int32_t fd)134     void OnSessionWriteable(int32_t fd) override
135     {
136         SHARING_LOGD("===[UdpTestAgent] OnSessionWriteable");
137     }
138 
OnSessionClose(int32_t fd)139     void OnSessionClose(int32_t fd) override
140     {
141         SHARING_LOGD("===[UdpTestAgent] OnSessionClose");
142     }
143 
OnSessionException(int32_t fd)144     void OnSessionException(int32_t fd) override
145     {
146         SHARING_LOGD("===[UdpTestAgent] OnSessionException");
147     }
148 
OnClientConnect(bool isSuccess)149     void OnClientConnect(bool isSuccess) override
150     {
151         if (isSuccess) {
152             SHARING_LOGD("===[UdpTestAgent] OnClientConnect success");
153             static int index = 0;
154             string msg = "udp client OnClientConnect message.index=" + std::to_string(index++);
155             DataBuffer::Ptr bufSend = std::make_shared<DataBuffer>();
156             bufSend->PushData(msg.c_str(), msg.size());
157             if (clientPtr_ != nullptr) {
158                 SHARING_LOGD("===[UdpTestAgent] OnClientConnect send");
159                 clientPtr_->Send(bufSend, bufSend->Size());
160             } else {
161                 SHARING_LOGD("===[UdpTestAgent] OnClientConnect nullptr");
162             }
163         } else {
164             if (clientPtr_ != nullptr) {
165                 SHARING_LOGE("===[UdpTestAgent] OnClientConnect failed");
166                 clientPtr_->Disconnect();
167             }
168         }
169     }
170 
OnClientReadData(int32_t fd,DataBuffer::Ptr buf)171     void OnClientReadData(int32_t fd, DataBuffer::Ptr buf) override
172     {
173         SHARING_LOGD("===[UdpTestAgent] OnClientReadData");
174         static int index = 0;
175         string msg = "udp client message.index=" + std::to_string(index++);
176         DataBuffer::Ptr bufSend = std::make_shared<DataBuffer>();
177         bufSend->PushData(msg.c_str(), msg.size());
178         clientPtr_->Send(bufSend, bufSend->Size());
179         sleep(3);
180     }
181 
OnClientWriteable(int32_t fd)182     void OnClientWriteable(int32_t fd) override
183     {
184         SHARING_LOGD("===[UdpTestAgent] OnClientWriteable");
185     }
186 
OnClientClose(int32_t fd)187     void OnClientClose(int32_t fd) override
188     {
189         SHARING_LOGD("===[UdpTestAgent] OnClientClose");
190         clientPtr_ = nullptr;
191     }
192 
OnClientException(int32_t fd)193     void OnClientException(int32_t fd) override
194     {
195         SHARING_LOGD("===[UdpTestAgent] OnClientException");
196         clientPtr_ = nullptr;
197     }
198 
199 private:
200     NetworkFactory::ServerPtr serverPtr_{nullptr};
201     NetworkFactory::ClientPtr clientPtr_{nullptr};
202     std::vector<INetworkSession::Ptr> sessionPtrVec_;
203 };
204 
205 namespace {
206 HWTEST_F(NetworkUdpUnitTest, NetworkUdpUnitTest_001, TestSize.Level0)
207 {
208     auto udpAgent = std::make_shared<UdpTestAgent>();
209     ASSERT_TRUE(udpAgent != nullptr);
210 }
211 
212 HWTEST_F(NetworkUdpUnitTest, NetworkUdpUnitTest_002, TestSize.Level0)
213 {
214     auto udpAgent = std::make_shared<UdpTestAgent>();
215     ASSERT_TRUE(udpAgent != nullptr);
216     auto ret = udpAgent->StartUdpServer(8888, "");
217     ASSERT_TRUE(ret);
218 }
219 
220 } // namespace
221 } // namespace Sharing
222 } // namespace OHOS