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_FAKE_AUTHENTICATOR_H_
      6 #define REMOTING_PROTOCOL_FAKE_AUTHENTICATOR_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/memory/weak_ptr.h"
     10 #include "remoting/protocol/authenticator.h"
     11 #include "remoting/protocol/channel_authenticator.h"
     12 
     13 namespace remoting {
     14 namespace protocol {
     15 
     16 class FakeChannelAuthenticator : public ChannelAuthenticator {
     17  public:
     18   FakeChannelAuthenticator(bool accept, bool async);
     19   virtual ~FakeChannelAuthenticator();
     20 
     21   // ChannelAuthenticator interface.
     22   virtual void SecureAndAuthenticate(
     23       scoped_ptr<net::StreamSocket> socket,
     24       const DoneCallback& done_callback) OVERRIDE;
     25 
     26  private:
     27   void CallCallback(
     28       net::Error error,
     29       scoped_ptr<net::StreamSocket> socket);
     30 
     31   void OnAuthBytesWritten(int result);
     32   void OnAuthBytesRead(int result);
     33 
     34   net::Error result_;
     35   bool async_;
     36 
     37   scoped_ptr<net::StreamSocket> socket_;
     38   DoneCallback done_callback_;
     39 
     40   bool did_read_bytes_;
     41   bool did_write_bytes_;
     42 
     43   base::WeakPtrFactory<FakeChannelAuthenticator> weak_factory_;
     44 
     45   DISALLOW_COPY_AND_ASSIGN(FakeChannelAuthenticator);
     46 };
     47 
     48 class FakeAuthenticator : public Authenticator {
     49  public:
     50   enum Type {
     51     HOST,
     52     CLIENT,
     53   };
     54 
     55   enum Action {
     56     ACCEPT,
     57     REJECT,
     58     REJECT_CHANNEL
     59   };
     60 
     61   FakeAuthenticator(Type type, int round_trips, Action action, bool async);
     62 
     63   virtual ~FakeAuthenticator();
     64 
     65   // Set the number of messages that the authenticator needs to process before
     66   // started() returns true.  Default to 0.
     67   void set_messages_till_started(int messages);
     68 
     69   // Authenticator interface.
     70   virtual State state() const OVERRIDE;
     71   virtual bool started() const OVERRIDE;
     72   virtual RejectionReason rejection_reason() const OVERRIDE;
     73   virtual void ProcessMessage(const buzz::XmlElement* message,
     74                               const base::Closure& resume_callback) OVERRIDE;
     75   virtual scoped_ptr<buzz::XmlElement> GetNextMessage() OVERRIDE;
     76   virtual scoped_ptr<ChannelAuthenticator>
     77       CreateChannelAuthenticator() const OVERRIDE;
     78 
     79  protected:
     80   Type type_;
     81   int round_trips_;
     82   Action action_;
     83   bool async_;
     84 
     85   // Total number of messages that have been processed.
     86   int messages_;
     87   // Number of messages that the authenticator needs to process before started()
     88   // returns true.  Default to 0.
     89   int messages_till_started_;
     90 
     91   DISALLOW_COPY_AND_ASSIGN(FakeAuthenticator);
     92 };
     93 
     94 class FakeHostAuthenticatorFactory : public AuthenticatorFactory {
     95  public:
     96   FakeHostAuthenticatorFactory(
     97       int round_trips, int messages_till_start,
     98       FakeAuthenticator::Action action, bool async);
     99   virtual ~FakeHostAuthenticatorFactory();
    100 
    101   // AuthenticatorFactory interface.
    102   virtual scoped_ptr<Authenticator> CreateAuthenticator(
    103       const std::string& local_jid,
    104       const std::string& remote_jid,
    105       const buzz::XmlElement* first_message) OVERRIDE;
    106 
    107  private:
    108   int round_trips_;
    109   int messages_till_started_;
    110   FakeAuthenticator::Action action_;
    111   bool async_;
    112 
    113   DISALLOW_COPY_AND_ASSIGN(FakeHostAuthenticatorFactory);
    114 };
    115 
    116 }  // namespace protocol
    117 }  // namespace remoting
    118 
    119 #endif  // REMOTING_PROTOCOL_FAKE_AUTHENTICATOR_H_
    120