fake_authenticator.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef REMOTING_PROTOCOL_FAKE_AUTHENTICATOR_H_
  5. #define REMOTING_PROTOCOL_FAKE_AUTHENTICATOR_H_
  6. #include "base/callback.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "remoting/protocol/authenticator.h"
  9. #include "remoting/protocol/channel_authenticator.h"
  10. namespace remoting {
  11. namespace protocol {
  12. class FakeChannelAuthenticator : public ChannelAuthenticator {
  13. public:
  14. FakeChannelAuthenticator(bool accept, bool async);
  15. FakeChannelAuthenticator(const FakeChannelAuthenticator&) = delete;
  16. FakeChannelAuthenticator& operator=(const FakeChannelAuthenticator&) = delete;
  17. ~FakeChannelAuthenticator() override;
  18. // ChannelAuthenticator interface.
  19. void SecureAndAuthenticate(std::unique_ptr<P2PStreamSocket> socket,
  20. DoneCallback done_callback) override;
  21. private:
  22. void OnAuthBytesWritten(int result);
  23. void OnAuthBytesRead(int result);
  24. void CallDoneCallback();
  25. const int result_;
  26. const bool async_;
  27. std::unique_ptr<P2PStreamSocket> socket_;
  28. DoneCallback done_callback_;
  29. bool did_read_bytes_ = false;
  30. bool did_write_bytes_ = false;
  31. base::WeakPtrFactory<FakeChannelAuthenticator> weak_factory_{this};
  32. };
  33. class FakeAuthenticator : public Authenticator {
  34. public:
  35. enum Type {
  36. HOST,
  37. CLIENT,
  38. };
  39. enum Action {
  40. ACCEPT,
  41. REJECT,
  42. REJECT_CHANNEL
  43. };
  44. struct Config {
  45. Config();
  46. Config(Action action);
  47. Config(int round_trips, Action action, bool async);
  48. int round_trips = 1;
  49. Action action = Action::ACCEPT;
  50. bool async = true;
  51. };
  52. FakeAuthenticator(Type type,
  53. Config config,
  54. const std::string& local_id,
  55. const std::string& remote_id);
  56. // Special constructor for authenticators in ACCEPTED or REJECTED state that
  57. // don't exchange any messages.
  58. FakeAuthenticator(Action action);
  59. FakeAuthenticator(const FakeAuthenticator&) = delete;
  60. FakeAuthenticator& operator=(const FakeAuthenticator&) = delete;
  61. ~FakeAuthenticator() override;
  62. // Set the number of messages that the authenticator needs to process before
  63. // started() returns true. Default to 0.
  64. void set_messages_till_started(int messages);
  65. // Sets auth key to be returned by GetAuthKey(). Must be called when
  66. // |round_trips| is set to 0.
  67. void set_auth_key(const std::string& auth_key) { auth_key_ = auth_key; }
  68. // When pause_message_index is set the authenticator will pause in
  69. // PROCESSING_MESSAGE state after that message, until
  70. // TakeResumeClosure().Run() is called.
  71. void set_pause_message_index(int pause_message_index) {
  72. pause_message_index_ = pause_message_index;
  73. }
  74. void Resume();
  75. // Authenticator interface.
  76. State state() const override;
  77. bool started() const override;
  78. RejectionReason rejection_reason() const override;
  79. void ProcessMessage(const jingle_xmpp::XmlElement* message,
  80. base::OnceClosure resume_callback) override;
  81. std::unique_ptr<jingle_xmpp::XmlElement> GetNextMessage() override;
  82. const std::string& GetAuthKey() const override;
  83. std::unique_ptr<ChannelAuthenticator> CreateChannelAuthenticator()
  84. const override;
  85. protected:
  86. const Type type_;
  87. const Config config_;
  88. const std::string local_id_;
  89. const std::string remote_id_;
  90. // Total number of messages that have been processed.
  91. int messages_ = 0;
  92. // Number of messages that the authenticator needs to process before started()
  93. // returns true. Default to 0.
  94. int messages_till_started_ = 0;
  95. int pause_message_index_ = -1;
  96. base::OnceClosure resume_closure_;
  97. std::string auth_key_;
  98. };
  99. class FakeHostAuthenticatorFactory : public AuthenticatorFactory {
  100. public:
  101. FakeHostAuthenticatorFactory(int messages_till_start,
  102. FakeAuthenticator::Config config);
  103. FakeHostAuthenticatorFactory(const FakeHostAuthenticatorFactory&) = delete;
  104. FakeHostAuthenticatorFactory& operator=(const FakeHostAuthenticatorFactory&) =
  105. delete;
  106. ~FakeHostAuthenticatorFactory() override;
  107. // AuthenticatorFactory interface.
  108. std::unique_ptr<Authenticator> CreateAuthenticator(
  109. const std::string& local_jid,
  110. const std::string& remote_jid) override;
  111. private:
  112. const int messages_till_started_;
  113. const FakeAuthenticator::Config config_;
  114. };
  115. } // namespace protocol
  116. } // namespace remoting
  117. #endif // REMOTING_PROTOCOL_FAKE_AUTHENTICATOR_H_