OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" |
5 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
6 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" |
7 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
8 #include "content/public/browser/devtools_http_handler.h" | 11 #include "content/public/browser/devtools_http_handler.h" |
9 #include "content/public/browser/devtools_http_handler_delegate.h" | 12 #include "content/public/browser/devtools_http_handler_delegate.h" |
10 #include "content/public/browser/devtools_target.h" | 13 #include "content/public/browser/devtools_target.h" |
| 14 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_errors.h" |
11 #include "net/socket/stream_listen_socket.h" | 16 #include "net/socket/stream_listen_socket.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
13 | 18 |
14 namespace content { | 19 namespace content { |
15 namespace { | 20 namespace { |
16 | 21 |
| 22 const int kDummyPort = 4321; |
| 23 const base::FilePath::CharType kDevToolsActivePortFileName[] = |
| 24 FILE_PATH_LITERAL("DevToolsActivePort"); |
| 25 |
17 using net::StreamListenSocket; | 26 using net::StreamListenSocket; |
18 | 27 |
19 class DummyListenSocket : public StreamListenSocket, | 28 class DummyListenSocket : public StreamListenSocket, |
20 public StreamListenSocket::Delegate { | 29 public StreamListenSocket::Delegate { |
21 public: | 30 public: |
22 DummyListenSocket() | 31 DummyListenSocket() |
23 : StreamListenSocket(net::kInvalidSocket, this) {} | 32 : StreamListenSocket(net::kInvalidSocket, this) {} |
24 | 33 |
25 // StreamListenSocket::Delegate "implementation" | 34 // StreamListenSocket::Delegate "implementation" |
26 virtual void DidAccept(StreamListenSocket* server, | 35 virtual void DidAccept(StreamListenSocket* server, |
27 scoped_ptr<StreamListenSocket> connection) OVERRIDE {} | 36 scoped_ptr<StreamListenSocket> connection) OVERRIDE {} |
28 virtual void DidRead(StreamListenSocket* connection, | 37 virtual void DidRead(StreamListenSocket* connection, |
29 const char* data, | 38 const char* data, |
30 int len) OVERRIDE {} | 39 int len) OVERRIDE {} |
31 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {} | 40 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {} |
32 protected: | 41 protected: |
33 virtual ~DummyListenSocket() {} | 42 virtual ~DummyListenSocket() {} |
34 virtual void Accept() OVERRIDE {} | 43 virtual void Accept() OVERRIDE {} |
| 44 virtual int GetLocalAddress(net::IPEndPoint* address) OVERRIDE { |
| 45 net::IPAddressNumber number; |
| 46 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &number)); |
| 47 *address = net::IPEndPoint(number, kDummyPort); |
| 48 return net::OK; |
| 49 } |
35 }; | 50 }; |
36 | 51 |
37 class DummyListenSocketFactory : public net::StreamListenSocketFactory { | 52 class DummyListenSocketFactory : public net::StreamListenSocketFactory { |
38 public: | 53 public: |
39 DummyListenSocketFactory( | 54 DummyListenSocketFactory( |
40 base::Closure quit_closure_1, base::Closure quit_closure_2) | 55 base::Closure quit_closure_1, base::Closure quit_closure_2) |
41 : quit_closure_1_(quit_closure_1), quit_closure_2_(quit_closure_2) {} | 56 : quit_closure_1_(quit_closure_1), quit_closure_2_(quit_closure_2) {} |
42 virtual ~DummyListenSocketFactory() { | 57 virtual ~DummyListenSocketFactory() { |
43 BrowserThread::PostTask( | 58 BrowserThread::PostTask( |
44 BrowserThread::UI, FROM_HERE, quit_closure_2_); | 59 BrowserThread::UI, FROM_HERE, quit_closure_2_); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 new DummyDelegate(), | 124 new DummyDelegate(), |
110 base::FilePath()); | 125 base::FilePath()); |
111 // Our dummy socket factory will post a quit message once the server will | 126 // Our dummy socket factory will post a quit message once the server will |
112 // become ready. | 127 // become ready. |
113 run_loop.Run(); | 128 run_loop.Run(); |
114 devtools_http_handler_->Stop(); | 129 devtools_http_handler_->Stop(); |
115 // Make sure the handler actually stops. | 130 // Make sure the handler actually stops. |
116 run_loop_2.Run(); | 131 run_loop_2.Run(); |
117 } | 132 } |
118 | 133 |
| 134 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { |
| 135 base::RunLoop run_loop, run_loop_2; |
| 136 base::ScopedTempDir temp_dir; |
| 137 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 138 content::DevToolsHttpHandler* devtools_http_handler_ = |
| 139 content::DevToolsHttpHandler::Start( |
| 140 new DummyListenSocketFactory(run_loop.QuitClosure(), |
| 141 run_loop_2.QuitClosure()), |
| 142 std::string(), |
| 143 new DummyDelegate(), |
| 144 temp_dir.path()); |
| 145 // Our dummy socket factory will post a quit message once the server will |
| 146 // become ready. |
| 147 run_loop.Run(); |
| 148 devtools_http_handler_->Stop(); |
| 149 // Make sure the handler actually stops. |
| 150 run_loop_2.Run(); |
| 151 |
| 152 // Now make sure the DevToolsActivePort was written into the |
| 153 // temporary directory and its contents are as expected. |
| 154 base::FilePath active_port_file = temp_dir.path().Append( |
| 155 kDevToolsActivePortFileName); |
| 156 EXPECT_TRUE(base::PathExists(active_port_file)); |
| 157 std::string file_contents; |
| 158 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); |
| 159 int port = 0; |
| 160 EXPECT_TRUE(base::StringToInt(file_contents, &port)); |
| 161 EXPECT_EQ(kDummyPort, port); |
| 162 } |
| 163 |
119 } // namespace content | 164 } // namespace content |
OLD | NEW |