Home | History | Annotate | Download | only in protocol
      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.
      4 
      5 #ifndef REMOTING_PROTOCOL_CONNECTION_TESTER_H_
      6 #define REMOTING_PROTOCOL_CONNECTION_TESTER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/memory/ref_counted.h"
     11 
     12 namespace base {
     13 class MessageLoop;
     14 }
     15 
     16 namespace net {
     17 class DrainableIOBuffer;
     18 class GrowableIOBuffer;
     19 class IOBuffer;
     20 class Socket;
     21 class StreamSocket;
     22 }  // namespace net
     23 
     24 namespace remoting {
     25 namespace protocol {
     26 
     27 // This class is used by unit tests to verify that a connection
     28 // between two sockets works properly, i.e. data is delivered from one
     29 // end to the other.
     30 class StreamConnectionTester {
     31  public:
     32   StreamConnectionTester(net::StreamSocket* client_socket,
     33                          net::StreamSocket* host_socket,
     34                          int message_size,
     35                          int message_count);
     36   ~StreamConnectionTester();
     37 
     38   void Start();
     39   bool done() { return done_; }
     40   void CheckResults();
     41 
     42  protected:
     43   void Done();
     44   void InitBuffers();
     45   void DoWrite();
     46   void OnWritten(int result);
     47   void HandleWriteResult(int result);
     48   void DoRead();
     49   void OnRead(int result);
     50   void HandleReadResult(int result);
     51 
     52  private:
     53   base::MessageLoop* message_loop_;
     54   net::StreamSocket* host_socket_;
     55   net::StreamSocket* client_socket_;
     56   int message_size_;
     57   int test_data_size_;
     58   bool done_;
     59 
     60   scoped_refptr<net::DrainableIOBuffer> output_buffer_;
     61   scoped_refptr<net::GrowableIOBuffer> input_buffer_;
     62 
     63   int write_errors_;
     64   int read_errors_;
     65 };
     66 
     67 class DatagramConnectionTester {
     68  public:
     69   DatagramConnectionTester(net::Socket* client_socket,
     70                            net::Socket* host_socket,
     71                            int message_size,
     72                            int message_count,
     73                            int delay_ms);
     74   ~DatagramConnectionTester() ;
     75 
     76   void Start();
     77   void CheckResults();
     78 
     79  private:
     80   void Done();
     81   void DoWrite();
     82   void OnWritten(int result);
     83   void HandleWriteResult(int result);
     84   void DoRead();
     85   void OnRead(int result);
     86   void HandleReadResult(int result);
     87 
     88   base::MessageLoop* message_loop_;
     89   net::Socket* host_socket_;
     90   net::Socket* client_socket_;
     91   int message_size_;
     92   int message_count_;
     93   int delay_ms_;
     94   bool done_;
     95 
     96   std::vector<scoped_refptr<net::IOBuffer> > sent_packets_;
     97   scoped_refptr<net::IOBuffer> read_buffer_;
     98 
     99   int write_errors_;
    100   int read_errors_;
    101   int packets_sent_;
    102   int packets_received_;
    103   int bad_packets_received_;
    104 };
    105 
    106 }  // namespace protocol
    107 }  // namespace remoting
    108 
    109 #endif  // REMOTING_PROTOCOL_CONNECTION_TESTER_H_
    110