1 // Copyright (c) 2012 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.
5 #include "base/files/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "content/browser/browser_thread_impl.h"
11 #include "content/public/browser/devtools_http_handler.h"
12 #include "content/public/browser/devtools_http_handler_delegate.h"
13 #include "content/public/browser/devtools_target.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/base/net_errors.h"
16 #include "net/socket/server_socket.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 const int kDummyPort
= 4321;
23 const base::FilePath::CharType kDevToolsActivePortFileName
[] =
24 FILE_PATH_LITERAL("DevToolsActivePort");
26 class DummyServerSocket
: public net::ServerSocket
{
28 DummyServerSocket() {}
30 // net::ServerSocket "implementation"
31 int Listen(const net::IPEndPoint
& address
, int backlog
) override
{
35 int ListenWithAddressAndPort(const std::string
& ip_address
,
37 int backlog
) override
{
41 int GetLocalAddress(net::IPEndPoint
* address
) const override
{
42 net::IPAddressNumber number
;
43 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &number
));
44 *address
= net::IPEndPoint(number
, kDummyPort
);
48 int Accept(scoped_ptr
<net::StreamSocket
>* socket
,
49 const net::CompletionCallback
& callback
) override
{
50 return net::ERR_IO_PENDING
;
54 class DummyServerSocketFactory
55 : public DevToolsHttpHandler::ServerSocketFactory
{
57 DummyServerSocketFactory(base::Closure quit_closure_1
,
58 base::Closure quit_closure_2
)
59 : DevToolsHttpHandler::ServerSocketFactory("", 0, 0),
60 quit_closure_1_(quit_closure_1
),
61 quit_closure_2_(quit_closure_2
) {}
63 ~DummyServerSocketFactory() override
{
64 BrowserThread::PostTask(
65 BrowserThread::UI
, FROM_HERE
, quit_closure_2_
);
69 scoped_ptr
<net::ServerSocket
> Create() const override
{
70 BrowserThread::PostTask(
71 BrowserThread::UI
, FROM_HERE
, quit_closure_1_
);
72 return scoped_ptr
<net::ServerSocket
>(new DummyServerSocket());
75 base::Closure quit_closure_1_
;
76 base::Closure quit_closure_2_
;
79 class DummyDelegate
: public DevToolsHttpHandlerDelegate
{
81 std::string
GetDiscoveryPageHTML() override
{ return std::string(); }
83 bool BundlesFrontendResources() override
{ return true; }
85 base::FilePath
GetDebugFrontendDir() override
{ return base::FilePath(); }
87 scoped_ptr
<net::StreamListenSocket
> CreateSocketForTethering(
88 net::StreamListenSocket::Delegate
* delegate
,
89 std::string
* name
) override
{
90 return scoped_ptr
<net::StreamListenSocket
>();
96 class DevToolsHttpHandlerTest
: public testing::Test
{
98 DevToolsHttpHandlerTest()
99 : ui_thread_(BrowserThread::UI
, &message_loop_
) {
103 virtual void SetUp() {
104 file_thread_
.reset(new BrowserThreadImpl(BrowserThread::FILE));
105 file_thread_
->Start();
108 virtual void TearDown() {
109 file_thread_
->Stop();
113 base::MessageLoopForIO message_loop_
;
114 BrowserThreadImpl ui_thread_
;
115 scoped_ptr
<BrowserThreadImpl
> file_thread_
;
118 TEST_F(DevToolsHttpHandlerTest
, TestStartStop
) {
119 base::RunLoop run_loop
, run_loop_2
;
120 scoped_ptr
<DevToolsHttpHandler::ServerSocketFactory
> factory(
121 new DummyServerSocketFactory(run_loop
.QuitClosure(),
122 run_loop_2
.QuitClosure()));
123 content::DevToolsHttpHandler
* devtools_http_handler_
=
124 content::DevToolsHttpHandler::Start(factory
.Pass(),
128 // Our dummy socket factory will post a quit message once the server will
131 devtools_http_handler_
->Stop();
132 // Make sure the handler actually stops.
136 TEST_F(DevToolsHttpHandlerTest
, TestDevToolsActivePort
) {
137 base::RunLoop run_loop
, run_loop_2
;
138 base::ScopedTempDir temp_dir
;
139 EXPECT_TRUE(temp_dir
.CreateUniqueTempDir());
140 scoped_ptr
<DevToolsHttpHandler::ServerSocketFactory
> factory(
141 new DummyServerSocketFactory(run_loop
.QuitClosure(),
142 run_loop_2
.QuitClosure()));
143 content::DevToolsHttpHandler
* devtools_http_handler_
=
144 content::DevToolsHttpHandler::Start(factory
.Pass(),
148 // Our dummy socket factory will post a quit message once the server will
151 devtools_http_handler_
->Stop();
152 // Make sure the handler actually stops.
155 // Now make sure the DevToolsActivePort was written into the
156 // temporary directory and its contents are as expected.
157 base::FilePath active_port_file
= temp_dir
.path().Append(
158 kDevToolsActivePortFileName
);
159 EXPECT_TRUE(base::PathExists(active_port_file
));
160 std::string file_contents
;
161 EXPECT_TRUE(base::ReadFileToString(active_port_file
, &file_contents
));
163 EXPECT_TRUE(base::StringToInt(file_contents
, &port
));
164 EXPECT_EQ(kDummyPort
, port
);
167 } // namespace content