| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/ref_counted.h" | |
| 6 #include "base/trace_event.h" | |
| 7 #include "net/base/address_list.h" | |
| 8 #include "net/base/host_resolver.h" | |
| 9 #include "net/base/listen_socket.h" | |
| 10 #include "net/base/net_errors.h" | |
| 11 #include "net/base/winsock_init.h" | |
| 12 #include "net/socket/tcp_client_socket.h" | |
| 13 #include "net/socket/tcp_pinger.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "testing/platform_test.h" | |
| 16 | |
| 17 class TCPPingerTest | |
| 18 : public PlatformTest, public ListenSocket::ListenSocketDelegate { | |
| 19 public: | |
| 20 TCPPingerTest() : listen_port_(0) { | |
| 21 } | |
| 22 | |
| 23 // Implement ListenSocketDelegate methods | |
| 24 virtual void DidAccept(ListenSocket* server, ListenSocket* connection) { | |
| 25 // This callback doesn't seem to happen | |
| 26 // right away, so this handler may not be called at all | |
| 27 // during connect-only tests. | |
| 28 LOG(INFO) << "TCPPinger accepted connection"; | |
| 29 connected_sock_ = connection; | |
| 30 } | |
| 31 virtual void DidRead(ListenSocket*, const char* str, int len) { | |
| 32 // Not really needed yet, as TCPPinger doesn't support Read | |
| 33 connected_sock_->Send(std::string("HTTP/1.1 404 Not Found"), true); | |
| 34 connected_sock_ = NULL; | |
| 35 } | |
| 36 virtual void DidClose(ListenSocket* sock) {} | |
| 37 | |
| 38 // Testcase hooks | |
| 39 virtual void SetUp(); | |
| 40 | |
| 41 protected: | |
| 42 int listen_port_; | |
| 43 scoped_refptr<ListenSocket> listen_sock_; | |
| 44 | |
| 45 private: | |
| 46 scoped_refptr<ListenSocket> connected_sock_; | |
| 47 }; | |
| 48 | |
| 49 void TCPPingerTest::SetUp() { | |
| 50 PlatformTest::SetUp(); | |
| 51 | |
| 52 // Find a free port to listen on | |
| 53 // Range of ports to listen on. Shouldn't need to try many. | |
| 54 static const int kMinPort = 10100; | |
| 55 static const int kMaxPort = 10200; | |
| 56 #if defined(OS_WIN) | |
| 57 net::EnsureWinsockInit(); | |
| 58 #endif | |
| 59 for (listen_port_ = kMinPort; listen_port_ < kMaxPort; listen_port_++) { | |
| 60 listen_sock_ = ListenSocket::Listen("127.0.0.1", listen_port_, this); | |
| 61 if (listen_sock_.get()) break; | |
| 62 } | |
| 63 ASSERT_TRUE(listen_sock_.get() != NULL); | |
| 64 } | |
| 65 | |
| 66 TEST_F(TCPPingerTest, Ping) { | |
| 67 net::AddressList addr; | |
| 68 scoped_refptr<net::HostResolver> resolver( | |
| 69 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | |
| 70 NULL)); | |
| 71 | |
| 72 net::HostResolver::RequestInfo info( | |
| 73 net::HostPortPair("localhost", listen_port_)); | |
| 74 int rv = resolver->Resolve(info, &addr, NULL, NULL, net::BoundNetLog()); | |
| 75 EXPECT_EQ(rv, net::OK); | |
| 76 | |
| 77 net::TCPPinger pinger(addr); | |
| 78 rv = pinger.Ping(); | |
| 79 EXPECT_EQ(rv, net::OK); | |
| 80 } | |
| 81 | |
| 82 TEST_F(TCPPingerTest, PingFail) { | |
| 83 net::AddressList addr; | |
| 84 scoped_refptr<net::HostResolver> resolver( | |
| 85 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | |
| 86 NULL)); | |
| 87 | |
| 88 // "Kill" "server" | |
| 89 listen_sock_ = NULL; | |
| 90 | |
| 91 net::HostResolver::RequestInfo info( | |
| 92 net::HostPortPair("localhost", listen_port_)); | |
| 93 int rv = resolver->Resolve(info, &addr, NULL, NULL, net::BoundNetLog()); | |
| 94 EXPECT_EQ(rv, net::OK); | |
| 95 | |
| 96 net::TCPPinger pinger(addr); | |
| 97 rv = pinger.Ping(base::TimeDelta::FromMilliseconds(100), 1); | |
| 98 EXPECT_NE(rv, net::OK); | |
| 99 } | |
| OLD | NEW |