PageRenderTime 103ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/remoting/protocol/fake_authenticator.cc

https://gitlab.com/jonnialva90/iridium-browser
C++ | 227 lines | 181 code | 34 blank | 12 comment | 39 complexity | 2941cdc86ca5b8cb87dee82fa73c5b77 MD5 | raw file
  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. #include "remoting/protocol/fake_authenticator.h"
  5. #include "base/base64.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/message_loop/message_loop.h"
  8. #include "base/rand_util.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "net/base/io_buffer.h"
  11. #include "net/base/net_errors.h"
  12. #include "remoting/base/constants.h"
  13. #include "remoting/protocol/p2p_stream_socket.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
  16. namespace remoting {
  17. namespace protocol {
  18. FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async)
  19. : result_(accept ? net::OK : net::ERR_FAILED),
  20. async_(async),
  21. did_read_bytes_(false),
  22. did_write_bytes_(false),
  23. weak_factory_(this) {
  24. }
  25. FakeChannelAuthenticator::~FakeChannelAuthenticator() {
  26. }
  27. void FakeChannelAuthenticator::SecureAndAuthenticate(
  28. scoped_ptr<P2PStreamSocket> socket,
  29. const DoneCallback& done_callback) {
  30. socket_ = socket.Pass();
  31. if (async_) {
  32. done_callback_ = done_callback;
  33. if (result_ != net::OK) {
  34. // Don't write anything if we are going to reject auth to make test
  35. // ordering deterministic.
  36. did_write_bytes_ = true;
  37. } else {
  38. scoped_refptr<net::IOBuffer> write_buf = new net::IOBuffer(1);
  39. write_buf->data()[0] = 0;
  40. int result = socket_->Write(
  41. write_buf.get(), 1,
  42. base::Bind(&FakeChannelAuthenticator::OnAuthBytesWritten,
  43. weak_factory_.GetWeakPtr()));
  44. if (result != net::ERR_IO_PENDING) {
  45. // This will not call the callback because |did_read_bytes_| is
  46. // still set to false.
  47. OnAuthBytesWritten(result);
  48. }
  49. }
  50. scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(1);
  51. int result =
  52. socket_->Read(read_buf.get(), 1,
  53. base::Bind(&FakeChannelAuthenticator::OnAuthBytesRead,
  54. weak_factory_.GetWeakPtr()));
  55. if (result != net::ERR_IO_PENDING)
  56. OnAuthBytesRead(result);
  57. } else {
  58. CallDoneCallback();
  59. }
  60. }
  61. void FakeChannelAuthenticator::OnAuthBytesWritten(int result) {
  62. EXPECT_EQ(1, result);
  63. EXPECT_FALSE(did_write_bytes_);
  64. did_write_bytes_ = true;
  65. if (did_read_bytes_)
  66. CallDoneCallback();
  67. }
  68. void FakeChannelAuthenticator::OnAuthBytesRead(int result) {
  69. EXPECT_EQ(1, result);
  70. EXPECT_FALSE(did_read_bytes_);
  71. did_read_bytes_ = true;
  72. if (did_write_bytes_)
  73. CallDoneCallback();
  74. }
  75. void FakeChannelAuthenticator::CallDoneCallback() {
  76. if (result_ != net::OK)
  77. socket_.reset();
  78. base::ResetAndReturn(&done_callback_).Run(result_, socket_.Pass());
  79. }
  80. FakeAuthenticator::FakeAuthenticator(Type type,
  81. int round_trips,
  82. Action action,
  83. bool async)
  84. : type_(type),
  85. round_trips_(round_trips),
  86. action_(action),
  87. async_(async),
  88. messages_(0),
  89. messages_till_started_(0) {
  90. }
  91. FakeAuthenticator::~FakeAuthenticator() {
  92. }
  93. void FakeAuthenticator::set_messages_till_started(int messages) {
  94. messages_till_started_ = messages;
  95. }
  96. Authenticator::State FakeAuthenticator::state() const {
  97. EXPECT_LE(messages_, round_trips_ * 2);
  98. if (messages_ >= round_trips_ * 2) {
  99. if (action_ == REJECT) {
  100. return REJECTED;
  101. } else {
  102. return ACCEPTED;
  103. }
  104. }
  105. // Don't send the last message if this is a host that wants to
  106. // reject a connection.
  107. if (messages_ == round_trips_ * 2 - 1 &&
  108. type_ == HOST && action_ == REJECT) {
  109. return REJECTED;
  110. }
  111. // We are not done yet. process next message.
  112. if ((messages_ % 2 == 0 && type_ == CLIENT) ||
  113. (messages_ % 2 == 1 && type_ == HOST)) {
  114. return MESSAGE_READY;
  115. } else {
  116. return WAITING_MESSAGE;
  117. }
  118. }
  119. bool FakeAuthenticator::started() const {
  120. return messages_ > messages_till_started_;
  121. }
  122. Authenticator::RejectionReason FakeAuthenticator::rejection_reason() const {
  123. EXPECT_EQ(REJECTED, state());
  124. return INVALID_CREDENTIALS;
  125. }
  126. void FakeAuthenticator::ProcessMessage(const buzz::XmlElement* message,
  127. const base::Closure& resume_callback) {
  128. EXPECT_EQ(WAITING_MESSAGE, state());
  129. std::string id =
  130. message->TextNamed(buzz::QName(kChromotingXmlNamespace, "id"));
  131. EXPECT_EQ(id, base::IntToString(messages_));
  132. // On the client receive the key in the last message.
  133. if (type_ == CLIENT && messages_ == round_trips_ * 2 - 1) {
  134. std::string key_base64 =
  135. message->TextNamed(buzz::QName(kChromotingXmlNamespace, "key"));
  136. EXPECT_TRUE(!key_base64.empty());
  137. EXPECT_TRUE(base::Base64Decode(key_base64, &auth_key_));
  138. }
  139. ++messages_;
  140. resume_callback.Run();
  141. }
  142. scoped_ptr<buzz::XmlElement> FakeAuthenticator::GetNextMessage() {
  143. EXPECT_EQ(MESSAGE_READY, state());
  144. scoped_ptr<buzz::XmlElement> result(new buzz::XmlElement(
  145. buzz::QName(kChromotingXmlNamespace, "authentication")));
  146. buzz::XmlElement* id = new buzz::XmlElement(
  147. buzz::QName(kChromotingXmlNamespace, "id"));
  148. id->AddText(base::IntToString(messages_));
  149. result->AddElement(id);
  150. // Add authentication key in the last message sent from host to client.
  151. if (type_ == HOST && messages_ == round_trips_ * 2 - 1) {
  152. auth_key_ = base::RandBytesAsString(16);
  153. buzz::XmlElement* key = new buzz::XmlElement(
  154. buzz::QName(kChromotingXmlNamespace, "key"));
  155. std::string key_base64;
  156. base::Base64Encode(auth_key_, &key_base64);
  157. key->AddText(key_base64);
  158. result->AddElement(key);
  159. }
  160. ++messages_;
  161. return result.Pass();
  162. }
  163. const std::string& FakeAuthenticator::GetAuthKey() const {
  164. EXPECT_EQ(ACCEPTED, state());
  165. return auth_key_;
  166. }
  167. scoped_ptr<ChannelAuthenticator>
  168. FakeAuthenticator::CreateChannelAuthenticator() const {
  169. EXPECT_EQ(ACCEPTED, state());
  170. return make_scoped_ptr(
  171. new FakeChannelAuthenticator(action_ != REJECT_CHANNEL, async_));
  172. }
  173. FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory(
  174. int round_trips, int messages_till_started,
  175. FakeAuthenticator::Action action, bool async)
  176. : round_trips_(round_trips),
  177. messages_till_started_(messages_till_started),
  178. action_(action), async_(async) {
  179. }
  180. FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() {
  181. }
  182. scoped_ptr<Authenticator> FakeHostAuthenticatorFactory::CreateAuthenticator(
  183. const std::string& local_jid,
  184. const std::string& remote_jid,
  185. const buzz::XmlElement* first_message) {
  186. FakeAuthenticator* authenticator = new FakeAuthenticator(
  187. FakeAuthenticator::HOST, round_trips_, action_, async_);
  188. authenticator->set_messages_till_started(messages_till_started_);
  189. scoped_ptr<Authenticator> result(authenticator);
  190. return result.Pass();
  191. }
  192. } // namespace protocol
  193. } // namespace remoting