OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/strings/string_number_conversions.h" | |
6 #include "chrome/browser/devtools/devtools_adb_bridge.h" | |
7 #include "chrome/browser/ui/browser.h" | |
8 #include "chrome/test/base/in_process_browser_test.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "content/public/test/browser_test.h" | |
11 #include "content/public/test/test_server.h" | |
12 #include "content/public/test/test_utils.h" | |
13 #include "net/base/host_port_pair.h" | |
14 #include "net/base/io_buffer.h" | |
15 #include "net/base/ip_endpoint.h" | |
16 #include "net/base/net_errors.h" | |
17 #include "net/base/net_log.h" | |
18 #include "net/socket/stream_socket.h" | |
19 #include "net/socket/tcp_server_socket.h" | |
20 | |
21 const char kOpenedUnixSocketsCommand[] = "shell:cat /proc/net/unix"; | |
22 const char kDeviceModelCommand[] = "shell:getprop ro.product.model"; | |
23 const char kDumpsysCommand[] = "shell:dumpsys window policy"; | |
24 const char kListProcessesCommand[] = "shell:ps"; | |
25 const char kDeviceModel[] = "Nexus 8"; | |
26 | |
27 const char kSampleOpenedUnixSocketsWithoutBrowsers[] = | |
28 "Num RefCount Protocol Flags Type St Inode Path\n" | |
29 "00000000: 00000004 00000000" | |
30 " 00000000 0002 01 3328 /dev/socket/wpa_wlan0\n" | |
31 "00000000: 00000002 00000000" | |
32 " 00010000 0001 01 5394 /dev/socket/vold\n"; | |
33 | |
34 const char kSampleDumpsys[] = | |
35 "WINDOW MANAGER POLICY STATE (dumpsys window policy)\r\n" | |
36 " mStable=(0,50)-(720,1184)\r\n"; | |
37 | |
38 const char kSampleListProcesses[] = | |
39 "USER PID PPID VSIZE RSS WCHAN PC NAME\n" | |
40 "root 1 0 688 508 ffffffff 00000000 S /init\n"; | |
41 | |
42 static const int kBufferSize = 16*1024; | |
43 static const int kAdbPort = 5037; | |
44 | |
45 static const int kAdbMessageHeaderSize = 4; | |
46 | |
47 // This is single connection server which listens on specified port and | |
48 // simplifies asynchronous IO. | |
49 // To write custom server, extend this class and implement TryProcessData | |
50 // method which is invoked everytime data arrives. In case of successful data | |
51 // processing(e.g. enough data collected already to parse client reply/request) | |
52 // return amount of bytes processed to throw them away from buffer | |
53 // To send data, SendData method should be used. This method is non-blocking | |
54 // and appends data to be sent to internal buffer. | |
55 // Since all calls are non-blocking and no callbacks are given, internal | |
56 // overflows may occur in case too heavy traffic. | |
57 // In case of heavy traffic performance may suffer because of memcpy calls. | |
58 class SingleConnectionServer { | |
59 public: | |
60 SingleConnectionServer(net::IPEndPoint endpoint, int buffer_size); | |
61 virtual ~SingleConnectionServer(); | |
62 | |
63 protected: | |
64 virtual int TryProcessData(const char* data, int size) = 0; | |
65 void SendData(const char* data, int size); | |
66 | |
67 private: | |
68 void AcceptConnection(); | |
69 void OnAccepted(int result); | |
70 | |
71 void ReadData(); | |
72 void OnDataRead(int count); | |
73 | |
74 void WriteData(); | |
75 void OnDataWritten(int count); | |
76 | |
77 private: | |
78 int bytes_to_write_; | |
79 scoped_ptr<net::TCPServerSocket> server_socket_; | |
80 scoped_ptr<net::StreamSocket> client_socket_; | |
81 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | |
82 scoped_refptr<net::GrowableIOBuffer> output_buffer_; | |
83 | |
84 DISALLOW_COPY_AND_ASSIGN(SingleConnectionServer); | |
85 }; | |
86 | |
87 SingleConnectionServer::SingleConnectionServer(net::IPEndPoint endpoint, | |
88 int buffer_size) | |
89 : bytes_to_write_(0) { | |
90 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
91 | |
92 input_buffer_ = new net::GrowableIOBuffer(); | |
93 input_buffer_->SetCapacity(buffer_size); | |
94 | |
95 output_buffer_ = new net::GrowableIOBuffer(); | |
96 | |
97 server_socket_.reset(new net::TCPServerSocket(NULL, net::NetLog::Source())); | |
98 server_socket_->Listen(endpoint, 1); | |
99 | |
100 AcceptConnection(); | |
101 } | |
102 | |
103 SingleConnectionServer::~SingleConnectionServer() { | |
104 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
105 | |
106 server_socket_.reset(); | |
107 | |
108 if (client_socket_) { | |
109 client_socket_->Disconnect(); | |
110 client_socket_.reset(); | |
111 } | |
112 } | |
113 | |
114 void SingleConnectionServer::SendData(const char* data, int size) { | |
115 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
116 | |
117 if ((output_buffer_->offset() + bytes_to_write_ + size) > | |
118 output_buffer_->capacity()) { | |
119 // If not enough space without relocation | |
120 if (output_buffer_->capacity() < (bytes_to_write_ + size)) { | |
121 // If even buffer is not enough | |
122 int new_size = std::max(output_buffer_->capacity() * 2, size * 2); | |
123 output_buffer_->SetCapacity(new_size); | |
124 } | |
125 memmove(output_buffer_->StartOfBuffer(), | |
126 output_buffer_->data(), | |
127 bytes_to_write_); | |
128 output_buffer_->set_offset(0); | |
129 } | |
130 | |
131 mempcpy(output_buffer_->data() + bytes_to_write_, data, size); | |
132 bytes_to_write_ += size; | |
133 | |
134 if (bytes_to_write_ == size) | |
135 // If write loop wasn't yet started, then start it | |
136 WriteData(); | |
137 } | |
138 | |
139 void SingleConnectionServer::AcceptConnection() { | |
140 if (client_socket_) { | |
141 client_socket_->Disconnect(); | |
142 client_socket_.reset(); | |
143 } | |
144 | |
145 int accept_result = server_socket_->Accept(&client_socket_, | |
146 base::Bind(&SingleConnectionServer::OnAccepted, base::Unretained(this))); | |
147 | |
148 if (accept_result != net::ERR_IO_PENDING) | |
149 content::BrowserThread::PostTask( | |
150 content::BrowserThread::IO, | |
151 FROM_HERE, | |
152 base::Bind(&SingleConnectionServer::OnAccepted, | |
153 base::Unretained(this), | |
154 accept_result)); | |
155 } | |
156 | |
157 void SingleConnectionServer::OnAccepted(int result) { | |
158 CHECK_EQ(result, 0); | |
159 ReadData(); | |
160 } | |
161 | |
162 void SingleConnectionServer::ReadData() { | |
163 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
164 | |
165 if (input_buffer_->RemainingCapacity() == 0) | |
166 input_buffer_->SetCapacity(input_buffer_->capacity() * 2); | |
167 | |
168 int read_result = client_socket_->Read( | |
169 input_buffer_.get(), | |
170 input_buffer_->RemainingCapacity(), | |
171 base::Bind(&SingleConnectionServer::OnDataRead, base::Unretained(this))); | |
172 | |
173 if (read_result != net::ERR_IO_PENDING) | |
174 OnDataRead(read_result); | |
175 } | |
176 | |
177 void SingleConnectionServer::OnDataRead(int count) { | |
178 if (count <= 0) { | |
179 AcceptConnection(); | |
180 return; | |
181 } | |
182 | |
183 input_buffer_->set_offset(input_buffer_->offset() + count); | |
184 | |
185 int bytes_processed; | |
186 | |
187 do { | |
188 char* data = input_buffer_->StartOfBuffer(); | |
189 int data_size = input_buffer_->offset(); | |
190 | |
191 bytes_processed = TryProcessData(data, data_size); | |
192 | |
193 if (bytes_processed) { | |
194 memmove(data, data + bytes_processed, data_size - bytes_processed); | |
195 input_buffer_->set_offset( data_size - bytes_processed); | |
196 } | |
197 } while (bytes_processed); | |
198 | |
199 // Posting is needed not to enter deep recursion in case too synchronous IO | |
200 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | |
201 base::Bind(&SingleConnectionServer::ReadData, base::Unretained(this))); | |
202 } | |
203 | |
204 void SingleConnectionServer::WriteData() { | |
205 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
206 CHECK_GE(output_buffer_->capacity(), | |
207 output_buffer_->offset() + bytes_to_write_) << "Overflow"; | |
208 | |
209 int write_result = client_socket_->Write( | |
210 output_buffer_, | |
211 bytes_to_write_, | |
212 base::Bind(&SingleConnectionServer::OnDataWritten, | |
213 base::Unretained(this))); | |
214 if (write_result != net::ERR_IO_PENDING) | |
215 OnDataWritten(write_result); | |
216 } | |
217 | |
218 void SingleConnectionServer::OnDataWritten(int count) { | |
219 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
220 if (count < 0) { | |
221 AcceptConnection(); | |
222 return; | |
223 } | |
224 | |
225 CHECK_GT(count, 0); | |
226 CHECK_GE(output_buffer_->capacity(), | |
227 output_buffer_->offset() + bytes_to_write_) << "Overflow"; | |
228 | |
229 bytes_to_write_ -= count; | |
230 output_buffer_->set_offset(output_buffer_->offset() + count); | |
231 | |
232 if (bytes_to_write_ != 0) | |
233 // Posting is needed not to enter deep recursion in case too synchronous IO | |
234 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | |
235 base::Bind(&SingleConnectionServer::WriteData, base::Unretained(this))); | |
236 } | |
237 | |
238 | |
239 class MockAdbServer: public SingleConnectionServer { | |
240 public: | |
241 MockAdbServer(net::IPEndPoint endpoint, int buffer_size) | |
242 : SingleConnectionServer(endpoint, buffer_size) | |
243 {} | |
244 | |
245 virtual ~MockAdbServer() OVERRIDE | |
Vladislav Kaznacheev
2013/11/01 12:18:16
no line break here
| |
246 {} | |
247 | |
248 private: | |
249 virtual int TryProcessData(const char* data, int size) OVERRIDE { | |
250 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
251 | |
252 if (size >= kAdbMessageHeaderSize) { | |
253 std::string message_header(data, kAdbMessageHeaderSize); | |
254 int message_size; | |
255 | |
256 EXPECT_TRUE(base::HexStringToInt(message_header, &message_size)); | |
257 | |
258 if (size >= message_size + kAdbMessageHeaderSize) { | |
259 std::string message_body(data + kAdbMessageHeaderSize, message_size ); | |
260 | |
261 ProcessCommand(message_body); | |
262 | |
263 return kAdbMessageHeaderSize + message_size; | |
264 } | |
265 } | |
266 | |
267 return 0; | |
268 } | |
269 | |
270 void ProcessCommand(const std::string& command) { | |
271 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
272 | |
273 if (command == "host:devices") { | |
274 SendResponse("01498B321301A00A\tdevice\n01498B2B0D01300E\toffline"); | |
275 } else if (command == "host:transport:01498B321301A00A") { | |
276 SendResponse(""); | |
277 } else if (command == kDeviceModelCommand) { | |
278 SendResponse(kDeviceModel); | |
279 } else if (command == kOpenedUnixSocketsCommand) { | |
280 SendResponse(kSampleOpenedUnixSocketsWithoutBrowsers); | |
281 } else if (command == kDumpsysCommand) { | |
282 SendResponse(kSampleDumpsys); | |
283 } else if (command == kListProcessesCommand) { | |
284 SendResponse(kSampleListProcesses); | |
285 } else { | |
286 NOTREACHED() << "Unknown command - " << command; | |
287 } | |
288 } | |
289 | |
290 void SendResponse(const std::string& response) { | |
291 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
292 | |
293 std::stringstream response_stream; | |
294 response_stream << "OKAY"; | |
295 | |
296 int size = response.size(); | |
297 if (size > 0) { | |
298 static const char kHexChars[] = "0123456789ABCDEF"; | |
299 for (int i = 3; i >= 0; i--) | |
300 response_stream << kHexChars[ (size >> 4*i) & 0x0f ]; | |
301 response_stream << response; | |
302 } | |
303 | |
304 std::string response_data = response_stream.str(); | |
305 SendData(response_data.c_str(), response_data.size()); | |
306 } | |
307 }; | |
308 | |
309 class AdbClientSocketTest : public InProcessBrowserTest, | |
310 public DevToolsAdbBridge::Listener { | |
311 | |
312 public: | |
313 void StartTest() { | |
314 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
315 | |
316 content::BrowserThread::PostTaskAndReply( | |
317 content::BrowserThread::IO, | |
318 FROM_HERE, | |
319 base::Bind(&AdbClientSocketTest::StartMockAdbServer, | |
320 base::Unretained(this)), | |
321 base::Bind(&AdbClientSocketTest::AddListener, | |
322 base::Unretained(this))); | |
323 } | |
324 | |
325 void EndTest() { | |
Vladislav Kaznacheev
2013/11/01 12:18:16
Does not need to be public.
| |
326 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
327 adb_bridge_ = NULL; | |
328 | |
329 content::BrowserThread::PostTaskAndReply( | |
330 content::BrowserThread::IO, | |
331 FROM_HERE, | |
332 base::Bind(&AdbClientSocketTest::StopMockAdbServer, | |
333 base::Unretained(this)), | |
334 base::Bind(&AdbClientSocketTest::StopMessageLoop, | |
335 base::Unretained(this))); | |
336 } | |
337 | |
338 virtual void RemoteDevicesChanged(DevToolsAdbBridge::RemoteDevices* devices) | |
339 OVERRIDE { | |
340 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
341 adb_bridge_->RemoveListener(this); | |
342 | |
343 #if defined(DEBUG_DEVTOOLS) | |
344 // Mock device is added | |
345 ASSERT_EQ(3U, devices->size()); | |
346 #else | |
347 ASSERT_EQ(2U, devices->size()); | |
348 #endif | |
349 | |
350 scoped_refptr<DevToolsAdbBridge::RemoteDevice> online_device_; | |
351 scoped_refptr<DevToolsAdbBridge::RemoteDevice> offline_device_; | |
352 | |
353 for (DevToolsAdbBridge::RemoteDevices::const_iterator it = | |
354 devices->begin(); it != devices->end(); ++it) { | |
355 if ((*it)->GetSerial() == "01498B321301A00A") | |
356 online_device_ = *it; | |
357 else if ((*it)->GetSerial() == "01498B2B0D01300E") | |
358 offline_device_ = *it; | |
359 } | |
360 | |
361 ASSERT_EQ(online_device_->GetSerial(), "01498B321301A00A"); | |
362 ASSERT_TRUE(online_device_->device()->is_connected()); | |
363 ASSERT_FALSE(offline_device_->device()->is_connected()); | |
364 | |
365 ASSERT_EQ(online_device_->GetModel(), kDeviceModel); | |
366 ASSERT_EQ(online_device_->browsers().size(), 0U); | |
367 ASSERT_EQ(online_device_->screen_size(), gfx::Size(720, 1184)); | |
368 | |
369 EndTest(); | |
370 } | |
371 | |
372 private: | |
373 void StartMockAdbServer() { | |
374 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
375 net::IPAddressNumber address; | |
376 net::ParseIPLiteralToNumber("127.0.0.1", &address); | |
377 net::IPEndPoint endpoint(address, kAdbPort); | |
378 | |
379 adb_server_.reset(new MockAdbServer(endpoint, kBufferSize)); | |
380 } | |
381 | |
382 void StopMockAdbServer() { | |
383 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
384 adb_server_.reset(); | |
385 } | |
386 | |
387 void StopMessageLoop() { | |
388 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
389 runner->Quit(); | |
390 } | |
391 | |
392 void AddListener() { | |
393 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
394 adb_bridge_ = DevToolsAdbBridge::Factory::GetForProfile( | |
395 browser()->profile()); | |
396 | |
397 DevToolsAdbBridge::DeviceProviders device_providers; | |
398 device_providers.push_back(AndroidDeviceProvider::GetAdbDeviceProvider()); | |
399 | |
400 adb_bridge_->set_device_providers(device_providers); | |
401 adb_bridge_->AddListener(this); | |
402 } | |
403 | |
404 public: | |
405 scoped_refptr<content::MessageLoopRunner> runner; | |
406 | |
407 private: | |
408 scoped_ptr<MockAdbServer> adb_server_; | |
409 scoped_refptr<DevToolsAdbBridge> adb_bridge_; | |
410 }; | |
411 | |
412 IN_PROC_BROWSER_TEST_F(AdbClientSocketTest, TestAdbClientSocket) { | |
413 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
414 runner = new content::MessageLoopRunner; | |
415 | |
416 StartTest(); | |
417 | |
418 runner->Run(); | |
419 } | |
OLD | NEW |