diff options
-rw-r--r-- | content/browser/devtools/devtools_http_handler_impl.cc | 8 | ||||
-rw-r--r-- | content/browser/devtools/devtools_http_handler_unittest.cc | 45 | ||||
-rw-r--r-- | net/socket/stream_listen_socket.h | 6 |
3 files changed, 52 insertions, 7 deletions
diff --git a/content/browser/devtools/devtools_http_handler_impl.cc b/content/browser/devtools/devtools_http_handler_impl.cc index 1b9da1d..bd1be4b 100644 --- a/content/browser/devtools/devtools_http_handler_impl.cc +++ b/content/browser/devtools/devtools_http_handler_impl.cc @@ -5,7 +5,6 @@ #include "content/browser/devtools/devtools_http_handler_impl.h" #include <algorithm> -#include <sstream> #include <utility> #include "base/bind.h" @@ -15,6 +14,7 @@ #include "base/logging.h" #include "base/message_loop/message_loop_proxy.h" #include "base/stl_util.h" +#include "base/strings/string_number_conversions.h" #include "base/threading/thread.h" #include "base/values.h" #include "content/browser/devtools/devtools_browser_target.h" @@ -699,10 +699,8 @@ void DevToolsHttpHandlerImpl::WriteActivePortToUserProfile() { // so Telemetry can pick it up. base::FilePath path = active_port_output_directory_.Append( kDevToolsActivePortFileName); - std::stringstream port_stream; - port_stream << endpoint.port(); - std::string s = port_stream.str(); - if (base::WriteFile(path, s.c_str(), s.length()) < 0) { + std::string port_string = base::IntToString(endpoint.port()); + if (base::WriteFile(path, port_string.c_str(), port_string.length()) < 0) { LOG(ERROR) << "Error writing DevTools active port to file"; } } diff --git a/content/browser/devtools/devtools_http_handler_unittest.cc b/content/browser/devtools/devtools_http_handler_unittest.cc index 77e9361..8871092 100644 --- a/content/browser/devtools/devtools_http_handler_unittest.cc +++ b/content/browser/devtools/devtools_http_handler_unittest.cc @@ -2,18 +2,27 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/file_util.h" +#include "base/files/scoped_temp_dir.h" #include "base/message_loop/message_loop.h" #include "base/run_loop.h" +#include "base/strings/string_number_conversions.h" #include "content/browser/browser_thread_impl.h" #include "content/public/browser/devtools_http_handler.h" #include "content/public/browser/devtools_http_handler_delegate.h" #include "content/public/browser/devtools_target.h" +#include "net/base/ip_endpoint.h" +#include "net/base/net_errors.h" #include "net/socket/stream_listen_socket.h" #include "testing/gtest/include/gtest/gtest.h" namespace content { namespace { +const int kDummyPort = 4321; +const base::FilePath::CharType kDevToolsActivePortFileName[] = + FILE_PATH_LITERAL("DevToolsActivePort"); + using net::StreamListenSocket; class DummyListenSocket : public StreamListenSocket, @@ -32,6 +41,12 @@ class DummyListenSocket : public StreamListenSocket, protected: virtual ~DummyListenSocket() {} virtual void Accept() OVERRIDE {} + virtual int GetLocalAddress(net::IPEndPoint* address) OVERRIDE { + net::IPAddressNumber number; + EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &number)); + *address = net::IPEndPoint(number, kDummyPort); + return net::OK; + } }; class DummyListenSocketFactory : public net::StreamListenSocketFactory { @@ -116,4 +131,34 @@ TEST_F(DevToolsHttpHandlerTest, TestStartStop) { run_loop_2.Run(); } +TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { + base::RunLoop run_loop, run_loop_2; + base::ScopedTempDir temp_dir; + EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); + content::DevToolsHttpHandler* devtools_http_handler_ = + content::DevToolsHttpHandler::Start( + new DummyListenSocketFactory(run_loop.QuitClosure(), + run_loop_2.QuitClosure()), + std::string(), + new DummyDelegate(), + temp_dir.path()); + // Our dummy socket factory will post a quit message once the server will + // become ready. + run_loop.Run(); + devtools_http_handler_->Stop(); + // Make sure the handler actually stops. + run_loop_2.Run(); + + // Now make sure the DevToolsActivePort was written into the + // temporary directory and its contents are as expected. + base::FilePath active_port_file = temp_dir.path().Append( + kDevToolsActivePortFileName); + EXPECT_TRUE(base::PathExists(active_port_file)); + std::string file_contents; + EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); + int port = 0; + EXPECT_TRUE(base::StringToInt(file_contents, &port)); + EXPECT_EQ(kDummyPort, port); +} + } // namespace content diff --git a/net/socket/stream_listen_socket.h b/net/socket/stream_listen_socket.h index 90b5450..813d96a 100644 --- a/net/socket/stream_listen_socket.h +++ b/net/socket/stream_listen_socket.h @@ -72,9 +72,11 @@ class NET_EXPORT StreamListenSocket void Send(const std::string& str, bool append_linefeed = false); // Copies the local address to |address|. Returns a network error code. - int GetLocalAddress(IPEndPoint* address); + // This method is virtual to support unit testing. + virtual int GetLocalAddress(IPEndPoint* address); // Copies the peer address to |address|. Returns a network error code. - int GetPeerAddress(IPEndPoint* address); + // This method is virtual to support unit testing. + virtual int GetPeerAddress(IPEndPoint* address); static const int kSocketError; |