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