authenticator_test_base.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/authenticator_test_base.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/base64.h"
  8. #include "base/bind.h"
  9. #include "base/files/file_path.h"
  10. #include "base/files/file_util.h"
  11. #include "base/run_loop.h"
  12. #include "base/test/test_timeouts.h"
  13. #include "base/timer/timer.h"
  14. #include "net/base/net_errors.h"
  15. #include "net/test/test_data_directory.h"
  16. #include "remoting/base/rsa_key_pair.h"
  17. #include "remoting/protocol/authenticator.h"
  18. #include "remoting/protocol/channel_authenticator.h"
  19. #include "remoting/protocol/fake_stream_socket.h"
  20. #include "remoting/protocol/p2p_stream_socket.h"
  21. #include "testing/gtest/include/gtest/gtest.h"
  22. #include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
  23. using testing::_;
  24. using testing::SaveArg;
  25. namespace remoting {
  26. namespace protocol {
  27. namespace {
  28. ACTION_P(QuitThreadOnCounter, counter) {
  29. --(*counter);
  30. EXPECT_GE(*counter, 0);
  31. if (*counter == 0)
  32. base::RunLoop::QuitCurrentWhenIdleDeprecated();
  33. }
  34. } // namespace
  35. AuthenticatorTestBase::MockChannelDoneCallback::MockChannelDoneCallback() =
  36. default;
  37. AuthenticatorTestBase::MockChannelDoneCallback::~MockChannelDoneCallback() =
  38. default;
  39. AuthenticatorTestBase::AuthenticatorTestBase() = default;
  40. AuthenticatorTestBase::~AuthenticatorTestBase() = default;
  41. void AuthenticatorTestBase::SetUp() {
  42. base::FilePath certs_dir(net::GetTestCertsDirectory());
  43. base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
  44. ASSERT_TRUE(base::ReadFileToString(cert_path, &host_cert_));
  45. base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
  46. std::string key_string;
  47. ASSERT_TRUE(base::ReadFileToString(key_path, &key_string));
  48. std::string key_base64;
  49. base::Base64Encode(key_string, &key_base64);
  50. key_pair_ = RsaKeyPair::FromString(key_base64);
  51. ASSERT_TRUE(key_pair_.get());
  52. host_public_key_ = key_pair_->GetPublicKey();
  53. }
  54. void AuthenticatorTestBase::RunAuthExchange() {
  55. ContinueAuthExchangeWith(client_.get(),
  56. host_.get(),
  57. client_->started(),
  58. host_->started());
  59. }
  60. void AuthenticatorTestBase::RunHostInitiatedAuthExchange() {
  61. ContinueAuthExchangeWith(host_.get(),
  62. client_.get(),
  63. host_->started(),
  64. client_->started());
  65. }
  66. // static
  67. // This function sends a message from the sender and receiver and recursively
  68. // calls itself to the send the next message from the receiver to the sender
  69. // untils the authentication completes.
  70. void AuthenticatorTestBase::ContinueAuthExchangeWith(Authenticator* sender,
  71. Authenticator* receiver,
  72. bool sender_started,
  73. bool receiver_started) {
  74. std::unique_ptr<jingle_xmpp::XmlElement> message;
  75. ASSERT_NE(Authenticator::WAITING_MESSAGE, sender->state());
  76. if (sender->state() == Authenticator::ACCEPTED ||
  77. sender->state() == Authenticator::REJECTED) {
  78. return;
  79. }
  80. // Verify that once the started flag for either party is set to true,
  81. // it should always stay true.
  82. if (receiver_started) {
  83. ASSERT_TRUE(receiver->started());
  84. }
  85. if (sender_started) {
  86. ASSERT_TRUE(sender->started());
  87. }
  88. ASSERT_EQ(Authenticator::MESSAGE_READY, sender->state());
  89. message = sender->GetNextMessage();
  90. ASSERT_TRUE(message.get());
  91. ASSERT_NE(Authenticator::MESSAGE_READY, sender->state());
  92. ASSERT_EQ(Authenticator::WAITING_MESSAGE, receiver->state());
  93. receiver->ProcessMessage(
  94. message.get(),
  95. base::BindOnce(&AuthenticatorTestBase::ContinueAuthExchangeWith,
  96. base::Unretained(receiver), base::Unretained(sender),
  97. receiver->started(), sender->started()));
  98. }
  99. void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) {
  100. client_fake_socket_ = std::make_unique<FakeStreamSocket>();
  101. host_fake_socket_ = std::make_unique<FakeStreamSocket>();
  102. client_fake_socket_->PairWith(host_fake_socket_.get());
  103. client_auth_->SecureAndAuthenticate(
  104. std::move(client_fake_socket_),
  105. base::BindOnce(&AuthenticatorTestBase::OnClientConnected,
  106. base::Unretained(this)));
  107. host_auth_->SecureAndAuthenticate(
  108. std::move(host_fake_socket_),
  109. base::BindOnce(&AuthenticatorTestBase::OnHostConnected,
  110. base::Unretained(this)));
  111. // Expect two callbacks to be called - the client callback and the host
  112. // callback.
  113. int callback_counter = 2;
  114. EXPECT_CALL(client_callback_, OnDone(net::OK))
  115. .WillOnce(QuitThreadOnCounter(&callback_counter));
  116. if (expected_fail) {
  117. EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED))
  118. .WillOnce(QuitThreadOnCounter(&callback_counter));
  119. } else {
  120. EXPECT_CALL(host_callback_, OnDone(net::OK))
  121. .WillOnce(QuitThreadOnCounter(&callback_counter));
  122. }
  123. // Ensure that .Run() does not run unbounded if the callbacks are never
  124. // called.
  125. base::OneShotTimer shutdown_timer;
  126. shutdown_timer.Start(FROM_HERE, TestTimeouts::action_timeout(),
  127. base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
  128. base::RunLoop().Run();
  129. shutdown_timer.Stop();
  130. testing::Mock::VerifyAndClearExpectations(&client_callback_);
  131. testing::Mock::VerifyAndClearExpectations(&host_callback_);
  132. if (!expected_fail) {
  133. ASSERT_TRUE(client_socket_.get() != nullptr);
  134. ASSERT_TRUE(host_socket_.get() != nullptr);
  135. }
  136. }
  137. void AuthenticatorTestBase::OnHostConnected(
  138. int error,
  139. std::unique_ptr<P2PStreamSocket> socket) {
  140. host_callback_.OnDone(error);
  141. host_socket_ = std::move(socket);
  142. }
  143. void AuthenticatorTestBase::OnClientConnected(
  144. int error,
  145. std::unique_ptr<P2PStreamSocket> socket) {
  146. client_callback_.OnDone(error);
  147. client_socket_ = std::move(socket);
  148. }
  149. } // namespace protocol
  150. } // namespace remoting