fake_authenticator.cc 8.1 KB

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