Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: webrtc/p2p/base/udptransportchannel_unittest.cc

Issue 2377883003: First step in providing a UdpTransportChannel. (Closed)
Patch Set: Unit test: VirtualSocketServer, constructor and nits. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <algorithm>
12 #include <list>
13 #include <memory>
14 #include <utility>
15 #include <vector>
16
17 #include "webrtc/base/gunit.h"
18 #include "webrtc/base/thread.h"
19 #include "webrtc/base/asyncpacketsocket.h"
20 #include "webrtc/base/ipaddress.h"
21 #include "webrtc/base/physicalsocketserver.h"
22 #include "webrtc/base/socketaddress.h"
23 #include "webrtc/base/socketserver.h"
24 #include "webrtc/base/virtualsocketserver.h"
25 #include "webrtc/p2p/base/packettransportinterface.h"
26 #include "webrtc/p2p/base/udptransportchannel.h"
27
28 namespace cricket {
29
30 constexpr int kTimeoutMs = 10000;
31 static const rtc::IPAddress kIPv4LocalHostAddress =
32 rtc::IPAddress(0x7F000001); // 127.0.0.1
33
34 class UdpTransportChannelTest : public testing::Test,
35 public sigslot::has_slots<> {
36 public:
37 UdpTransportChannelTest()
38 : network_thread_(rtc::Thread::Current()),
39 physical_socket_server_(new rtc::PhysicalSocketServer),
40 virtual_socket_server_(
41 new rtc::VirtualSocketServer(physical_socket_server_.get())),
42 ss_scope_(virtual_socket_server_.get()),
43 ep1_("Name1", 1),
44 ep2_("name2", 2) {
45 // Setup IP Address for outgoing packets from sockets bound to
46 // IPV4 INADDR_ANY ("0.0.0.0."). Virtual socket server sends packets these
47 // only,
Taylor Brandstetter 2016/11/04 17:51:07 nit: comment formatting
48 // if the default address is explicit set. With a physical socket, the
49 // actual network stack / operating system would set the IP address for
50 // outgoing packets.
51 virtual_socket_server_->SetDefaultRoute(kIPv4LocalHostAddress);
52 }
53
54 struct Endpoint : public sigslot::has_slots<> {
55 Endpoint(std::string tch_name, int component) {
56 ch_.reset(new UdpTransportChannel(std::move(tch_name), component));
57 ch_->SignalReadPacket.connect(this, &Endpoint::OnReadPacket);
58 ch_->SignalSentPacket.connect(this, &Endpoint::OnSentPacket);
59 ch_->SignalReadyToSend.connect(this, &Endpoint::OnReadyToSend);
60 ch_->SignalWritableState.connect(this, &Endpoint::OnWritableState);
61 }
62
63 bool CheckData(const char* data, int len) {
64 bool ret = false;
65 if (!ch_packets_.empty()) {
66 std::string packet = ch_packets_.front();
67 ret = (packet == std::string(data, len));
68 ch_packets_.pop_front();
69 }
70 return ret;
71 }
72
73 void OnWritableState(rtc::PacketTransportInterface* transport) {
74 num_sig_writable_++;
75 }
76
77 void OnReadyToSend(rtc::PacketTransportInterface* transport) {
78 num_sig_ready_to_send_++;
79 }
80
81 void OnReadPacket(rtc::PacketTransportInterface* transport,
82 const char* data,
83 size_t len,
84 const rtc::PacketTime& packet_time,
85 int flags) {
86 num_received_packets_++;
87 LOG(LS_VERBOSE) << "OnReadPacket (unittest)";
88 ch_packets_.push_front(std::string(data, len));
89 }
90
91 void OnSentPacket(rtc::PacketTransportInterface* transport,
92 const rtc::SentPacket&) {
93 num_sig_sent_packets_++;
94 }
95
96 int SendData(const char* data, size_t len) {
97 rtc::PacketOptions options;
98 return ch_->SendPacket(data, len, options, 0);
99 }
100
101 void GetLocalPort(uint16_t* local_port) {
102 const rtc::SocketAddress& addr = ch_->local_parameters();
103 *local_port = addr.port();
104 }
105
106 std::list<std::string> ch_packets_;
107 std::unique_ptr<UdpTransportChannel> ch_;
108 uint32_t num_received_packets_ = 0; // Increases on SignalReadPacket.
109 uint32_t num_sig_sent_packets_ = 0; // Increases on SignalSentPacket.
110 uint32_t num_sig_writable_ = 0; // Increases on SignalWritable.
111 uint32_t num_sig_ready_to_send_ = 0; // Increases on SignalReadyToSend.
112 };
113
114 rtc::Thread* network_thread_ = nullptr;
115 std::unique_ptr<rtc::PhysicalSocketServer> physical_socket_server_;
116 std::unique_ptr<rtc::VirtualSocketServer> virtual_socket_server_;
117 rtc::SocketServerScope ss_scope_;
118
119 Endpoint ep1_;
120 Endpoint ep2_;
121
122 void TestSendRecv() {
123 for (uint32_t i = 0; i < 5; ++i) {
124 static const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
125 int len = static_cast<int>(strlen(data));
126 // local_channel <==> remote_channel
127 EXPECT_EQ_WAIT(len, ep1_.SendData(data, len), kTimeoutMs);
128 EXPECT_TRUE_WAIT(ep2_.CheckData(data, len), kTimeoutMs);
129 EXPECT_EQ_WAIT(i + 1u, ep2_.num_received_packets_, kTimeoutMs);
130 EXPECT_EQ_WAIT(len, ep2_.SendData(data, len), kTimeoutMs);
131 EXPECT_TRUE_WAIT(ep1_.CheckData(data, len), kTimeoutMs);
132 EXPECT_EQ_WAIT(i + 1u, ep1_.num_received_packets_, kTimeoutMs);
133 }
134 }
135 };
136
137 TEST_F(UdpTransportChannelTest, SendRecvBasic) {
138 ep1_.ch_->CreateSocket();
139 ep2_.ch_->CreateSocket();
140 uint16_t port;
141 ep2_.GetLocalPort(&port);
142 rtc::SocketAddress addr2 = rtc::SocketAddress("127.0.0.1", port);
143 ep1_.ch_->SetRemoteParameters(addr2);
144 ep1_.GetLocalPort(&port);
145 rtc::SocketAddress addr1 = rtc::SocketAddress("127.0.0.1", port);
146 ep2_.ch_->SetRemoteParameters(addr1);
147 TestSendRecv();
148 }
149
150 TEST_F(UdpTransportChannelTest, DefaultLocalParameters) {
151 EXPECT_TRUE(ep1_.ch_->local_parameters().IsNil());
152 }
153
154 TEST_F(UdpTransportChannelTest, CreateSocketTwice) {
155 ep1_.ch_->CreateSocket();
156 EXPECT_EQ(UDPTRANSPORT_STATE_CONNECTING, ep1_.ch_->GetState());
157 ep1_.ch_->CreateSocket();
158 EXPECT_EQ(UDPTRANSPORT_STATE_CONNECTING, ep1_.ch_->GetState());
159 }
160
161 TEST_F(UdpTransportChannelTest, StatusAndSignals) {
162 EXPECT_EQ(UDPTRANSPORT_STATE_INIT, ep1_.ch_->GetState());
163 ep1_.ch_->CreateSocket();
164 EXPECT_EQ(UDPTRANSPORT_STATE_CONNECTING, ep1_.ch_->GetState());
165 EXPECT_EQ(0u, ep1_.num_sig_writable_);
166 EXPECT_EQ(0u, ep1_.num_sig_ready_to_send_);
167 // Loopback
168 EXPECT_TRUE(!ep1_.ch_->writable());
169 rtc::SocketAddress addr = ep1_.ch_->local_parameters();
170 // Keep port, but explicitly set IP.
171 addr.SetIP("127.0.0.1");
172 ep1_.ch_->SetRemoteParameters(addr);
173 EXPECT_TRUE(ep1_.ch_->writable());
174 EXPECT_EQ(1u, ep1_.num_sig_writable_);
175 EXPECT_EQ(1u, ep1_.num_sig_ready_to_send_);
176 const char data[] = "abc";
177 ep1_.SendData(data, sizeof(data));
178 EXPECT_EQ_WAIT(1u, ep1_.ch_packets_.size(), kTimeoutMs);
179 EXPECT_EQ_WAIT(1u, ep1_.num_sig_sent_packets_, kTimeoutMs);
180 }
181 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698