OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2017 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 <memory> | |
12 | |
13 #include "webrtc/api/ortcfactoryinterface.h" | |
14 #include "webrtc/base/fakenetwork.h" | |
15 #include "webrtc/base/gunit.h" | |
16 #include "webrtc/base/physicalsocketserver.h" | |
17 #include "webrtc/base/virtualsocketserver.h" | |
18 #include "webrtc/p2p/base/udptransport.h" | |
19 | |
20 namespace { | |
21 | |
22 const int kDefaultTimeout = 10000; // 10 seconds. | |
23 static const rtc::IPAddress kIPv4LocalHostAddress = | |
24 rtc::IPAddress(0x7F000001); // 127.0.0.1 | |
25 | |
26 class PacketReceiver : public sigslot::has_slots<> { | |
27 public: | |
28 explicit PacketReceiver(rtc::PacketTransportInterface* transport) { | |
29 transport->SignalReadPacket.connect(this, &PacketReceiver::OnReadPacket); | |
30 } | |
31 int packets_read() const { return packets_read_; } | |
32 | |
33 private: | |
34 void OnReadPacket(rtc::PacketTransportInterface*, | |
35 const char*, | |
36 size_t, | |
37 const rtc::PacketTime&, | |
38 int) { | |
39 ++packets_read_; | |
40 } | |
41 | |
42 int packets_read_ = 0; | |
43 }; | |
44 | |
45 } // namespace | |
46 | |
47 namespace webrtc { | |
48 | |
49 // Used to test that things work end-to-end when using the default | |
50 // implementations of threads/etc. provided by OrtcFactory, with the exception | |
51 // of using a virtual network. | |
52 // | |
53 // By default, the virtual network manager doesn't enumerate any networks, but | |
54 // sockets can still be created in this state. | |
55 class OrtcFactoryTest : public testing::Test { | |
56 public: | |
57 OrtcFactoryTest() | |
58 : virtual_socket_server_(&physical_socket_server_), | |
59 network_thread_(&virtual_socket_server_), | |
60 ortc_factory_(OrtcFactoryInterface::Create(&network_thread_, | |
61 nullptr, | |
62 &fake_network_manager_, | |
63 nullptr)) { | |
64 // Sockets are bound to the ANY address, so this is needed to tell the | |
65 // virtual network which address to use in this case. | |
66 virtual_socket_server_.SetDefaultRoute(kIPv4LocalHostAddress); | |
67 network_thread_.Start(); | |
68 } | |
69 | |
70 protected: | |
71 rtc::PhysicalSocketServer physical_socket_server_; | |
72 rtc::VirtualSocketServer virtual_socket_server_; | |
73 rtc::Thread network_thread_; | |
74 rtc::FakeNetworkManager fake_network_manager_; | |
75 std::unique_ptr<OrtcFactoryInterface> ortc_factory_; | |
76 }; | |
77 | |
78 TEST_F(OrtcFactoryTest, EndToEndUdpTransport) { | |
79 std::unique_ptr<UdpTransportInterface> transport1 = | |
80 ortc_factory_->CreateUdpTransport(AF_INET); | |
81 std::unique_ptr<UdpTransportInterface> transport2 = | |
82 ortc_factory_->CreateUdpTransport(AF_INET); | |
83 ASSERT_NE(nullptr, transport1); | |
84 ASSERT_NE(nullptr, transport2); | |
85 // Sockets are bound to the ANY address, so we need to provide the IP address | |
86 // explicitly. | |
87 transport1->SetRemoteAddress( | |
88 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), | |
89 transport2->GetLocalAddress().port())); | |
90 transport2->SetRemoteAddress( | |
91 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), | |
92 transport1->GetLocalAddress().port())); | |
93 | |
94 // TODO(deadbeef): Once there's something (RTP senders/receivers) that can | |
95 // use UdpTransport end-to-end, use that for this end-to-end test instead of | |
96 // making assumptions about the implementation. | |
97 // | |
98 // For now, this assumes the returned object is a UdpTransportProxy that wraps | |
99 // a UdpTransport. | |
100 cricket::UdpTransport* internal_transport1 = | |
101 static_cast<UdpTransportProxyWithInternal<cricket::UdpTransport>*>( | |
102 transport1.get()) | |
103 ->internal(); | |
104 cricket::UdpTransport* internal_transport2 = | |
105 static_cast<UdpTransportProxyWithInternal<cricket::UdpTransport>*>( | |
106 transport2.get()) | |
107 ->internal(); | |
108 // Need to call internal "SendPacket" method on network thread. | |
109 network_thread_.Invoke<void>( | |
110 RTC_FROM_HERE, [internal_transport1, internal_transport2]() { | |
111 PacketReceiver receiver1(internal_transport1); | |
112 PacketReceiver receiver2(internal_transport2); | |
113 internal_transport1->SendPacket("foo", sizeof("foo"), | |
114 rtc::PacketOptions(), 0); | |
115 internal_transport2->SendPacket("foo", sizeof("foo"), | |
116 rtc::PacketOptions(), 0); | |
117 EXPECT_EQ_WAIT(1, receiver1.packets_read(), kDefaultTimeout); | |
118 EXPECT_EQ_WAIT(1, receiver2.packets_read(), kDefaultTimeout); | |
119 }); | |
120 } | |
121 | |
122 } // namespace webrtc | |
OLD | NEW |