| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2006-2009 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/tcp_client_socket.h" |
| 12 #include "net/base/tcp_pinger.h" |
| 13 #include "net/base/winsock_init.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() { |
| 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 std::string& s) { |
| 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 net::HostResolver resolver; |
| 69 |
| 70 int rv = resolver.Resolve("localhost", listen_port_, &addr, NULL); |
| 71 EXPECT_EQ(rv, net::OK); |
| 72 |
| 73 net::TCPPinger pinger(addr); |
| 74 rv = pinger.Ping(); |
| 75 EXPECT_EQ(rv, net::OK); |
| 76 } |
| 77 |
| 78 TEST_F(TCPPingerTest, PingFail) { |
| 79 net::AddressList addr; |
| 80 net::HostResolver resolver; |
| 81 |
| 82 // "Kill" "server" |
| 83 listen_sock_ = NULL; |
| 84 |
| 85 int rv = resolver.Resolve("localhost", listen_port_, &addr, NULL); |
| 86 EXPECT_EQ(rv, net::OK); |
| 87 |
| 88 net::TCPPinger pinger(addr); |
| 89 rv = pinger.Ping(base::TimeDelta::FromMilliseconds(100), 1); |
| 90 EXPECT_NE(rv, net::OK); |
| 91 } |
| 92 |
| OLD | NEW |