OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "webrtc/base/gunit.h" | 13 #include "webrtc/base/gunit.h" |
14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
15 #include "webrtc/base/natserver.h" | 15 #include "webrtc/base/natserver.h" |
16 #include "webrtc/base/natsocketfactory.h" | 16 #include "webrtc/base/natsocketfactory.h" |
17 #include "webrtc/base/nethelpers.h" | 17 #include "webrtc/base/nethelpers.h" |
18 #include "webrtc/base/network.h" | 18 #include "webrtc/base/network.h" |
19 #include "webrtc/base/physicalsocketserver.h" | 19 #include "webrtc/base/physicalsocketserver.h" |
20 #include "webrtc/base/testclient.h" | 20 #include "webrtc/base/testclient.h" |
| 21 #include "webrtc/base/asynctcpsocket.h" |
21 #include "webrtc/base/virtualsocketserver.h" | 22 #include "webrtc/base/virtualsocketserver.h" |
22 #include "webrtc/test/testsupport/gtest_disable.h" | 23 #include "webrtc/test/testsupport/gtest_disable.h" |
23 | 24 |
24 using namespace rtc; | 25 using namespace rtc; |
25 | 26 |
26 bool CheckReceive( | 27 bool CheckReceive( |
27 TestClient* client, bool should_receive, const char* buf, size_t size) { | 28 TestClient* client, bool should_receive, const char* buf, size_t size) { |
28 return (should_receive) ? | 29 return (should_receive) ? |
29 client->CheckNextPacket(buf, size, 0) : | 30 client->CheckNextPacket(buf, size, 0) : |
30 client->CheckNoPacket(); | 31 client->CheckNoPacket(); |
31 } | 32 } |
32 | 33 |
33 TestClient* CreateTestClient( | 34 TestClient* CreateTestClient( |
34 SocketFactory* factory, const SocketAddress& local_addr) { | 35 SocketFactory* factory, const SocketAddress& local_addr) { |
35 AsyncUDPSocket* socket = AsyncUDPSocket::Create(factory, local_addr); | 36 AsyncUDPSocket* socket = AsyncUDPSocket::Create(factory, local_addr); |
36 return new TestClient(socket); | 37 return new TestClient(socket); |
37 } | 38 } |
38 | 39 |
| 40 TestClient* CreateTCPTestClient(AsyncSocket* socket) { |
| 41 AsyncTCPSocket* packet_socket = new AsyncTCPSocket(socket, false); |
| 42 return new TestClient(packet_socket); |
| 43 } |
| 44 |
39 // Tests that when sending from internal_addr to external_addrs through the | 45 // Tests that when sending from internal_addr to external_addrs through the |
40 // NAT type specified by nat_type, all external addrs receive the sent packet | 46 // NAT type specified by nat_type, all external addrs receive the sent packet |
41 // and, if exp_same is true, all use the same mapped-address on the NAT. | 47 // and, if exp_same is true, all use the same mapped-address on the NAT. |
42 void TestSend( | 48 void TestSend( |
43 SocketServer* internal, const SocketAddress& internal_addr, | 49 SocketServer* internal, const SocketAddress& internal_addr, |
44 SocketServer* external, const SocketAddress external_addrs[4], | 50 SocketServer* external, const SocketAddress external_addrs[4], |
45 NATType nat_type, bool exp_same) { | 51 NATType nat_type, bool exp_same) { |
46 Thread th_int(internal); | 52 Thread th_int(internal); |
47 Thread th_ext(external); | 53 Thread th_ext(external); |
48 | 54 |
49 SocketAddress server_addr = internal_addr; | 55 SocketAddress server_addr = internal_addr; |
50 server_addr.SetPort(0); // Auto-select a port | 56 server_addr.SetPort(0); // Auto-select a port |
51 NATServer* nat = new NATServer( | 57 NATServer* nat = new NATServer(nat_type, internal, server_addr, server_addr, |
52 nat_type, internal, server_addr, external, external_addrs[0]); | 58 external, external_addrs[0]); |
53 NATSocketFactory* natsf = new NATSocketFactory(internal, | 59 NATSocketFactory* natsf = new NATSocketFactory(internal, |
54 nat->internal_address()); | 60 nat->internal_udp_address(), |
| 61 nat->internal_tcp_address()); |
55 | 62 |
56 TestClient* in = CreateTestClient(natsf, internal_addr); | 63 TestClient* in = CreateTestClient(natsf, internal_addr); |
57 TestClient* out[4]; | 64 TestClient* out[4]; |
58 for (int i = 0; i < 4; i++) | 65 for (int i = 0; i < 4; i++) |
59 out[i] = CreateTestClient(external, external_addrs[i]); | 66 out[i] = CreateTestClient(external, external_addrs[i]); |
60 | 67 |
61 th_int.Start(); | 68 th_int.Start(); |
62 th_ext.Start(); | 69 th_ext.Start(); |
63 | 70 |
64 const char* buf = "filter_test"; | 71 const char* buf = "filter_test"; |
(...skipping 27 matching lines...) Expand all Loading... |
92 // is delivered according to the specified filter_ip and filter_port rules. | 99 // is delivered according to the specified filter_ip and filter_port rules. |
93 void TestRecv( | 100 void TestRecv( |
94 SocketServer* internal, const SocketAddress& internal_addr, | 101 SocketServer* internal, const SocketAddress& internal_addr, |
95 SocketServer* external, const SocketAddress external_addrs[4], | 102 SocketServer* external, const SocketAddress external_addrs[4], |
96 NATType nat_type, bool filter_ip, bool filter_port) { | 103 NATType nat_type, bool filter_ip, bool filter_port) { |
97 Thread th_int(internal); | 104 Thread th_int(internal); |
98 Thread th_ext(external); | 105 Thread th_ext(external); |
99 | 106 |
100 SocketAddress server_addr = internal_addr; | 107 SocketAddress server_addr = internal_addr; |
101 server_addr.SetPort(0); // Auto-select a port | 108 server_addr.SetPort(0); // Auto-select a port |
102 NATServer* nat = new NATServer( | 109 NATServer* nat = new NATServer(nat_type, internal, server_addr, server_addr, |
103 nat_type, internal, server_addr, external, external_addrs[0]); | 110 external, external_addrs[0]); |
104 NATSocketFactory* natsf = new NATSocketFactory(internal, | 111 NATSocketFactory* natsf = new NATSocketFactory(internal, |
105 nat->internal_address()); | 112 nat->internal_udp_address(), |
| 113 nat->internal_tcp_address()); |
106 | 114 |
107 TestClient* in = CreateTestClient(natsf, internal_addr); | 115 TestClient* in = CreateTestClient(natsf, internal_addr); |
108 TestClient* out[4]; | 116 TestClient* out[4]; |
109 for (int i = 0; i < 4; i++) | 117 for (int i = 0; i < 4; i++) |
110 out[i] = CreateTestClient(external, external_addrs[i]); | 118 out[i] = CreateTestClient(external, external_addrs[i]); |
111 | 119 |
112 th_int.Start(); | 120 th_int.Start(); |
113 th_ext.Start(); | 121 th_ext.Start(); |
114 | 122 |
115 const char* buf = "filter_test"; | 123 const char* buf = "filter_test"; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 296 } |
289 | 297 |
290 TEST(NatTest, TestVirtualIPv6) { | 298 TEST(NatTest, TestVirtualIPv6) { |
291 if (HasIPv6Enabled()) { | 299 if (HasIPv6Enabled()) { |
292 TestVirtualInternal(AF_INET6); | 300 TestVirtualInternal(AF_INET6); |
293 } else { | 301 } else { |
294 LOG(LS_WARNING) << "No IPv6, skipping"; | 302 LOG(LS_WARNING) << "No IPv6, skipping"; |
295 } | 303 } |
296 } | 304 } |
297 | 305 |
298 // TODO: Finish this test | |
299 class NatTcpTest : public testing::Test, public sigslot::has_slots<> { | 306 class NatTcpTest : public testing::Test, public sigslot::has_slots<> { |
300 public: | 307 public: |
301 NatTcpTest() : connected_(false) {} | 308 NatTcpTest() |
302 virtual void SetUp() { | 309 : int_addr_("192.168.0.1", 0), |
303 int_vss_ = new TestVirtualSocketServer(new PhysicalSocketServer()); | 310 ext_addr_("10.0.0.1", 0), |
304 ext_vss_ = new TestVirtualSocketServer(new PhysicalSocketServer()); | 311 connected_(false), |
305 nat_ = new NATServer(NAT_OPEN_CONE, int_vss_, SocketAddress(), | 312 int_pss_(new PhysicalSocketServer()), |
306 ext_vss_, SocketAddress()); | 313 ext_pss_(new PhysicalSocketServer()), |
307 natsf_ = new NATSocketFactory(int_vss_, nat_->internal_address()); | 314 int_vss_(new TestVirtualSocketServer(int_pss_)), |
| 315 ext_vss_(new TestVirtualSocketServer(ext_pss_)), |
| 316 int_thread_(new Thread(int_vss_.get())), |
| 317 ext_thread_(new Thread(ext_vss_.get())), |
| 318 nat_(new NATServer(NAT_OPEN_CONE, int_vss_.get(), int_addr_, int_addr_, |
| 319 ext_vss_.get(), ext_addr_)), |
| 320 natsf_(new NATSocketFactory(int_vss_.get(), |
| 321 nat_->internal_udp_address(), |
| 322 nat_->internal_tcp_address())) { |
| 323 int_thread_->Start(); |
| 324 ext_thread_->Start(); |
308 } | 325 } |
| 326 |
309 void OnConnectEvent(AsyncSocket* socket) { | 327 void OnConnectEvent(AsyncSocket* socket) { |
310 connected_ = true; | 328 connected_ = true; |
311 } | 329 } |
| 330 |
312 void OnAcceptEvent(AsyncSocket* socket) { | 331 void OnAcceptEvent(AsyncSocket* socket) { |
313 accepted_ = server_->Accept(NULL); | 332 accepted_.reset(server_->Accept(NULL)); |
314 } | 333 } |
| 334 |
315 void OnCloseEvent(AsyncSocket* socket, int error) { | 335 void OnCloseEvent(AsyncSocket* socket, int error) { |
316 } | 336 } |
| 337 |
317 void ConnectEvents() { | 338 void ConnectEvents() { |
318 server_->SignalReadEvent.connect(this, &NatTcpTest::OnAcceptEvent); | 339 server_->SignalReadEvent.connect(this, &NatTcpTest::OnAcceptEvent); |
319 client_->SignalConnectEvent.connect(this, &NatTcpTest::OnConnectEvent); | 340 client_->SignalConnectEvent.connect(this, &NatTcpTest::OnConnectEvent); |
320 } | 341 } |
321 TestVirtualSocketServer* int_vss_; | 342 |
322 TestVirtualSocketServer* ext_vss_; | 343 SocketAddress int_addr_; |
323 NATServer* nat_; | 344 SocketAddress ext_addr_; |
324 NATSocketFactory* natsf_; | |
325 AsyncSocket* client_; | |
326 AsyncSocket* server_; | |
327 AsyncSocket* accepted_; | |
328 bool connected_; | 345 bool connected_; |
| 346 PhysicalSocketServer* int_pss_; |
| 347 PhysicalSocketServer* ext_pss_; |
| 348 rtc::scoped_ptr<TestVirtualSocketServer> int_vss_; |
| 349 rtc::scoped_ptr<TestVirtualSocketServer> ext_vss_; |
| 350 rtc::scoped_ptr<Thread> int_thread_; |
| 351 rtc::scoped_ptr<Thread> ext_thread_; |
| 352 rtc::scoped_ptr<NATServer> nat_; |
| 353 rtc::scoped_ptr<NATSocketFactory> natsf_; |
| 354 rtc::scoped_ptr<AsyncSocket> client_; |
| 355 rtc::scoped_ptr<AsyncSocket> server_; |
| 356 rtc::scoped_ptr<AsyncSocket> accepted_; |
329 }; | 357 }; |
330 | 358 |
331 TEST_F(NatTcpTest, DISABLED_TestConnectOut) { | 359 TEST_F(NatTcpTest, TestConnectOut) { |
332 server_ = ext_vss_->CreateAsyncSocket(SOCK_STREAM); | 360 server_.reset(ext_vss_->CreateAsyncSocket(SOCK_STREAM)); |
333 server_->Bind(SocketAddress()); | 361 server_->Bind(ext_addr_); |
334 server_->Listen(5); | 362 server_->Listen(5); |
335 | 363 |
336 client_ = int_vss_->CreateAsyncSocket(SOCK_STREAM); | 364 client_.reset(natsf_->CreateAsyncSocket(SOCK_STREAM)); |
337 EXPECT_GE(0, client_->Bind(SocketAddress())); | 365 EXPECT_GE(0, client_->Bind(int_addr_)); |
338 EXPECT_GE(0, client_->Connect(server_->GetLocalAddress())); | 366 EXPECT_GE(0, client_->Connect(server_->GetLocalAddress())); |
339 | 367 |
340 | |
341 ConnectEvents(); | 368 ConnectEvents(); |
342 | 369 |
343 EXPECT_TRUE_WAIT(connected_, 1000); | 370 EXPECT_TRUE_WAIT(connected_, 1000); |
344 EXPECT_EQ(client_->GetRemoteAddress(), server_->GetLocalAddress()); | 371 EXPECT_EQ(client_->GetRemoteAddress(), server_->GetLocalAddress()); |
345 EXPECT_EQ(client_->GetRemoteAddress(), accepted_->GetLocalAddress()); | 372 EXPECT_EQ(accepted_->GetRemoteAddress().ipaddr(), ext_addr_.ipaddr()); |
346 EXPECT_EQ(client_->GetLocalAddress(), accepted_->GetRemoteAddress()); | |
347 | 373 |
348 client_->Close(); | 374 rtc::scoped_ptr<rtc::TestClient> in(CreateTCPTestClient(client_.release())); |
| 375 rtc::scoped_ptr<rtc::TestClient> out( |
| 376 CreateTCPTestClient(accepted_.release())); |
| 377 |
| 378 const char* buf = "test_packet"; |
| 379 size_t len = strlen(buf); |
| 380 |
| 381 in->Send(buf, len); |
| 382 SocketAddress trans_addr; |
| 383 EXPECT_TRUE(out->CheckNextPacket(buf, len, &trans_addr)); |
| 384 |
| 385 out->Send(buf, len); |
| 386 EXPECT_TRUE(in->CheckNextPacket(buf, len, &trans_addr)); |
349 } | 387 } |
350 //#endif | 388 // #endif |
OLD | NEW |