Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/devtools/adb_client_socket_browsertest.cc

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

Powered by Google App Engine
This is Rietveld 408576698