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 <algorithm> | 11 #include <algorithm> |
| 12 #include <memory> |
12 #include <string> | 13 #include <string> |
13 | 14 |
14 #include "webrtc/base/gunit.h" | 15 #include "webrtc/base/gunit.h" |
15 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
16 #include "webrtc/base/natserver.h" | 17 #include "webrtc/base/natserver.h" |
17 #include "webrtc/base/natsocketfactory.h" | 18 #include "webrtc/base/natsocketfactory.h" |
18 #include "webrtc/base/nethelpers.h" | 19 #include "webrtc/base/nethelpers.h" |
19 #include "webrtc/base/network.h" | 20 #include "webrtc/base/network.h" |
20 #include "webrtc/base/physicalsocketserver.h" | 21 #include "webrtc/base/physicalsocketserver.h" |
21 #include "webrtc/base/testclient.h" | 22 #include "webrtc/base/testclient.h" |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 TestRecv(internal, internal_addr, external, external_addrs, | 172 TestRecv(internal, internal_addr, external, external_addrs, |
172 NAT_PORT_RESTRICTED, true, true); | 173 NAT_PORT_RESTRICTED, true, true); |
173 TestRecv(internal, internal_addr, external, external_addrs, | 174 TestRecv(internal, internal_addr, external, external_addrs, |
174 NAT_SYMMETRIC, true, true); | 175 NAT_SYMMETRIC, true, true); |
175 } | 176 } |
176 | 177 |
177 bool TestConnectivity(const SocketAddress& src, const IPAddress& dst) { | 178 bool TestConnectivity(const SocketAddress& src, const IPAddress& dst) { |
178 // The physical NAT tests require connectivity to the selected ip from the | 179 // The physical NAT tests require connectivity to the selected ip from the |
179 // internal address used for the NAT. Things like firewalls can break that, so | 180 // internal address used for the NAT. Things like firewalls can break that, so |
180 // check to see if it's worth even trying with this ip. | 181 // check to see if it's worth even trying with this ip. |
181 scoped_ptr<PhysicalSocketServer> pss(new PhysicalSocketServer()); | 182 std::unique_ptr<PhysicalSocketServer> pss(new PhysicalSocketServer()); |
182 scoped_ptr<AsyncSocket> client(pss->CreateAsyncSocket(src.family(), | 183 std::unique_ptr<AsyncSocket> client( |
183 SOCK_DGRAM)); | 184 pss->CreateAsyncSocket(src.family(), SOCK_DGRAM)); |
184 scoped_ptr<AsyncSocket> server(pss->CreateAsyncSocket(src.family(), | 185 std::unique_ptr<AsyncSocket> server( |
185 SOCK_DGRAM)); | 186 pss->CreateAsyncSocket(src.family(), SOCK_DGRAM)); |
186 if (client->Bind(SocketAddress(src.ipaddr(), 0)) != 0 || | 187 if (client->Bind(SocketAddress(src.ipaddr(), 0)) != 0 || |
187 server->Bind(SocketAddress(dst, 0)) != 0) { | 188 server->Bind(SocketAddress(dst, 0)) != 0) { |
188 return false; | 189 return false; |
189 } | 190 } |
190 const char* buf = "hello other socket"; | 191 const char* buf = "hello other socket"; |
191 size_t len = strlen(buf); | 192 size_t len = strlen(buf); |
192 int sent = client->SendTo(buf, len, server->GetLocalAddress()); | 193 int sent = client->SendTo(buf, len, server->GetLocalAddress()); |
193 SocketAddress addr; | 194 SocketAddress addr; |
194 const size_t kRecvBufSize = 64; | 195 const size_t kRecvBufSize = 64; |
195 char recvbuf[kRecvBufSize]; | 196 char recvbuf[kRecvBufSize]; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 | 238 |
238 LOG(LS_INFO) << "selected ip " << ext_addr2.ipaddr(); | 239 LOG(LS_INFO) << "selected ip " << ext_addr2.ipaddr(); |
239 | 240 |
240 SocketAddress ext_addrs[4] = { | 241 SocketAddress ext_addrs[4] = { |
241 SocketAddress(ext_addr1), | 242 SocketAddress(ext_addr1), |
242 SocketAddress(ext_addr2), | 243 SocketAddress(ext_addr2), |
243 SocketAddress(ext_addr1), | 244 SocketAddress(ext_addr1), |
244 SocketAddress(ext_addr2) | 245 SocketAddress(ext_addr2) |
245 }; | 246 }; |
246 | 247 |
247 scoped_ptr<PhysicalSocketServer> int_pss(new PhysicalSocketServer()); | 248 std::unique_ptr<PhysicalSocketServer> int_pss(new PhysicalSocketServer()); |
248 scoped_ptr<PhysicalSocketServer> ext_pss(new PhysicalSocketServer()); | 249 std::unique_ptr<PhysicalSocketServer> ext_pss(new PhysicalSocketServer()); |
249 | 250 |
250 TestBindings(int_pss.get(), int_addr, ext_pss.get(), ext_addrs); | 251 TestBindings(int_pss.get(), int_addr, ext_pss.get(), ext_addrs); |
251 TestFilters(int_pss.get(), int_addr, ext_pss.get(), ext_addrs); | 252 TestFilters(int_pss.get(), int_addr, ext_pss.get(), ext_addrs); |
252 } | 253 } |
253 | 254 |
254 TEST(NatTest, TestPhysicalIPv4) { | 255 TEST(NatTest, TestPhysicalIPv4) { |
255 TestPhysicalInternal(SocketAddress("127.0.0.1", 0)); | 256 TestPhysicalInternal(SocketAddress("127.0.0.1", 0)); |
256 } | 257 } |
257 | 258 |
258 TEST(NatTest, TestPhysicalIPv6) { | 259 TEST(NatTest, TestPhysicalIPv6) { |
259 if (HasIPv6Enabled()) { | 260 if (HasIPv6Enabled()) { |
260 TestPhysicalInternal(SocketAddress("::1", 0)); | 261 TestPhysicalInternal(SocketAddress("::1", 0)); |
261 } else { | 262 } else { |
262 LOG(LS_WARNING) << "No IPv6, skipping"; | 263 LOG(LS_WARNING) << "No IPv6, skipping"; |
263 } | 264 } |
264 } | 265 } |
265 | 266 |
266 namespace { | 267 namespace { |
267 | 268 |
268 class TestVirtualSocketServer : public VirtualSocketServer { | 269 class TestVirtualSocketServer : public VirtualSocketServer { |
269 public: | 270 public: |
270 explicit TestVirtualSocketServer(SocketServer* ss) | 271 explicit TestVirtualSocketServer(SocketServer* ss) |
271 : VirtualSocketServer(ss), | 272 : VirtualSocketServer(ss), |
272 ss_(ss) {} | 273 ss_(ss) {} |
273 // Expose this publicly | 274 // Expose this publicly |
274 IPAddress GetNextIP(int af) { return VirtualSocketServer::GetNextIP(af); } | 275 IPAddress GetNextIP(int af) { return VirtualSocketServer::GetNextIP(af); } |
275 | 276 |
276 private: | 277 private: |
277 scoped_ptr<SocketServer> ss_; | 278 std::unique_ptr<SocketServer> ss_; |
278 }; | 279 }; |
279 | 280 |
280 } // namespace | 281 } // namespace |
281 | 282 |
282 void TestVirtualInternal(int family) { | 283 void TestVirtualInternal(int family) { |
283 scoped_ptr<TestVirtualSocketServer> int_vss(new TestVirtualSocketServer( | 284 std::unique_ptr<TestVirtualSocketServer> int_vss( |
284 new PhysicalSocketServer())); | 285 new TestVirtualSocketServer(new PhysicalSocketServer())); |
285 scoped_ptr<TestVirtualSocketServer> ext_vss(new TestVirtualSocketServer( | 286 std::unique_ptr<TestVirtualSocketServer> ext_vss( |
286 new PhysicalSocketServer())); | 287 new TestVirtualSocketServer(new PhysicalSocketServer())); |
287 | 288 |
288 SocketAddress int_addr; | 289 SocketAddress int_addr; |
289 SocketAddress ext_addrs[4]; | 290 SocketAddress ext_addrs[4]; |
290 int_addr.SetIP(int_vss->GetNextIP(family)); | 291 int_addr.SetIP(int_vss->GetNextIP(family)); |
291 ext_addrs[0].SetIP(ext_vss->GetNextIP(int_addr.family())); | 292 ext_addrs[0].SetIP(ext_vss->GetNextIP(int_addr.family())); |
292 ext_addrs[1].SetIP(ext_vss->GetNextIP(int_addr.family())); | 293 ext_addrs[1].SetIP(ext_vss->GetNextIP(int_addr.family())); |
293 ext_addrs[2].SetIP(ext_addrs[0].ipaddr()); | 294 ext_addrs[2].SetIP(ext_addrs[0].ipaddr()); |
294 ext_addrs[3].SetIP(ext_addrs[1].ipaddr()); | 295 ext_addrs[3].SetIP(ext_addrs[1].ipaddr()); |
295 | 296 |
296 TestBindings(int_vss.get(), int_addr, ext_vss.get(), ext_addrs); | 297 TestBindings(int_vss.get(), int_addr, ext_vss.get(), ext_addrs); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 void ConnectEvents() { | 345 void ConnectEvents() { |
345 server_->SignalReadEvent.connect(this, &NatTcpTest::OnAcceptEvent); | 346 server_->SignalReadEvent.connect(this, &NatTcpTest::OnAcceptEvent); |
346 client_->SignalConnectEvent.connect(this, &NatTcpTest::OnConnectEvent); | 347 client_->SignalConnectEvent.connect(this, &NatTcpTest::OnConnectEvent); |
347 } | 348 } |
348 | 349 |
349 SocketAddress int_addr_; | 350 SocketAddress int_addr_; |
350 SocketAddress ext_addr_; | 351 SocketAddress ext_addr_; |
351 bool connected_; | 352 bool connected_; |
352 PhysicalSocketServer* int_pss_; | 353 PhysicalSocketServer* int_pss_; |
353 PhysicalSocketServer* ext_pss_; | 354 PhysicalSocketServer* ext_pss_; |
354 rtc::scoped_ptr<TestVirtualSocketServer> int_vss_; | 355 std::unique_ptr<TestVirtualSocketServer> int_vss_; |
355 rtc::scoped_ptr<TestVirtualSocketServer> ext_vss_; | 356 std::unique_ptr<TestVirtualSocketServer> ext_vss_; |
356 rtc::scoped_ptr<Thread> int_thread_; | 357 std::unique_ptr<Thread> int_thread_; |
357 rtc::scoped_ptr<Thread> ext_thread_; | 358 std::unique_ptr<Thread> ext_thread_; |
358 rtc::scoped_ptr<NATServer> nat_; | 359 std::unique_ptr<NATServer> nat_; |
359 rtc::scoped_ptr<NATSocketFactory> natsf_; | 360 std::unique_ptr<NATSocketFactory> natsf_; |
360 rtc::scoped_ptr<AsyncSocket> client_; | 361 std::unique_ptr<AsyncSocket> client_; |
361 rtc::scoped_ptr<AsyncSocket> server_; | 362 std::unique_ptr<AsyncSocket> server_; |
362 rtc::scoped_ptr<AsyncSocket> accepted_; | 363 std::unique_ptr<AsyncSocket> accepted_; |
363 }; | 364 }; |
364 | 365 |
365 TEST_F(NatTcpTest, DISABLED_TestConnectOut) { | 366 TEST_F(NatTcpTest, DISABLED_TestConnectOut) { |
366 server_.reset(ext_vss_->CreateAsyncSocket(SOCK_STREAM)); | 367 server_.reset(ext_vss_->CreateAsyncSocket(SOCK_STREAM)); |
367 server_->Bind(ext_addr_); | 368 server_->Bind(ext_addr_); |
368 server_->Listen(5); | 369 server_->Listen(5); |
369 | 370 |
370 client_.reset(natsf_->CreateAsyncSocket(SOCK_STREAM)); | 371 client_.reset(natsf_->CreateAsyncSocket(SOCK_STREAM)); |
371 EXPECT_GE(0, client_->Bind(int_addr_)); | 372 EXPECT_GE(0, client_->Bind(int_addr_)); |
372 EXPECT_GE(0, client_->Connect(server_->GetLocalAddress())); | 373 EXPECT_GE(0, client_->Connect(server_->GetLocalAddress())); |
373 | 374 |
374 ConnectEvents(); | 375 ConnectEvents(); |
375 | 376 |
376 EXPECT_TRUE_WAIT(connected_, 1000); | 377 EXPECT_TRUE_WAIT(connected_, 1000); |
377 EXPECT_EQ(client_->GetRemoteAddress(), server_->GetLocalAddress()); | 378 EXPECT_EQ(client_->GetRemoteAddress(), server_->GetLocalAddress()); |
378 EXPECT_EQ(accepted_->GetRemoteAddress().ipaddr(), ext_addr_.ipaddr()); | 379 EXPECT_EQ(accepted_->GetRemoteAddress().ipaddr(), ext_addr_.ipaddr()); |
379 | 380 |
380 rtc::scoped_ptr<rtc::TestClient> in(CreateTCPTestClient(client_.release())); | 381 std::unique_ptr<rtc::TestClient> in(CreateTCPTestClient(client_.release())); |
381 rtc::scoped_ptr<rtc::TestClient> out( | 382 std::unique_ptr<rtc::TestClient> out( |
382 CreateTCPTestClient(accepted_.release())); | 383 CreateTCPTestClient(accepted_.release())); |
383 | 384 |
384 const char* buf = "test_packet"; | 385 const char* buf = "test_packet"; |
385 size_t len = strlen(buf); | 386 size_t len = strlen(buf); |
386 | 387 |
387 in->Send(buf, len); | 388 in->Send(buf, len); |
388 SocketAddress trans_addr; | 389 SocketAddress trans_addr; |
389 EXPECT_TRUE(out->CheckNextPacket(buf, len, &trans_addr)); | 390 EXPECT_TRUE(out->CheckNextPacket(buf, len, &trans_addr)); |
390 | 391 |
391 out->Send(buf, len); | 392 out->Send(buf, len); |
392 EXPECT_TRUE(in->CheckNextPacket(buf, len, &trans_addr)); | 393 EXPECT_TRUE(in->CheckNextPacket(buf, len, &trans_addr)); |
393 } | 394 } |
394 // #endif | 395 // #endif |
OLD | NEW |