blob: 4db252b3a4ea1e9abe0074e0e89158a78781f574 [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2012 The Chromium Authors
[email protected]85488f22012-01-25 04:34:012// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "remoting/protocol/authenticator_test_base.h"
6
Peter Boström42afa23f2021-04-02 22:10:467#include <memory>
sergeyu89d088b2015-12-24 00:22:448#include <utility>
9
[email protected]8f1504b2013-03-07 13:43:1010#include "base/base64.h"
Sebastien Marchand6d0558fd2019-01-25 16:49:3711#include "base/bind.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
thestig1ecdcf42014-09-12 05:09:1413#include "base/files/file_util.h"
fdoray2ad58be2016-06-22 20:36:1614#include "base/run_loop.h"
[email protected]53998282012-06-06 22:08:5215#include "base/test/test_timeouts.h"
[email protected]5d7eb862013-06-28 15:21:2416#include "base/timer/timer.h"
sergeyu9cb142f2014-09-12 20:43:0117#include "net/base/net_errors.h"
rsleevia69c79a2016-06-22 03:28:4318#include "net/test/test_data_directory.h"
[email protected]8f1504b2013-03-07 13:43:1019#include "remoting/base/rsa_key_pair.h"
[email protected]85488f22012-01-25 04:34:0120#include "remoting/protocol/authenticator.h"
21#include "remoting/protocol/channel_authenticator.h"
sergeyu98d8fe92014-09-23 21:56:2022#include "remoting/protocol/fake_stream_socket.h"
sergeyuaa22c082015-07-20 19:41:1323#include "remoting/protocol/p2p_stream_socket.h"
[email protected]85488f22012-01-25 04:34:0124#include "testing/gtest/include/gtest/gtest.h"
kjellanderf0e410b2017-01-04 14:45:0125#include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
[email protected]85488f22012-01-25 04:34:0126
27using testing::_;
28using testing::SaveArg;
29
Joe Downing39d710e2022-08-25 20:11:4530namespace remoting::protocol {
[email protected]85488f22012-01-25 04:34:0131
[email protected]53998282012-06-06 22:08:5232namespace {
33
34ACTION_P(QuitThreadOnCounter, counter) {
35 --(*counter);
36 EXPECT_GE(*counter, 0);
Joe Downing353ba2c72023-01-11 22:37:3437 if (*counter == 0) {
Gabriel Charette53a9ef812017-07-26 12:36:2338 base::RunLoop::QuitCurrentWhenIdleDeprecated();
Joe Downing353ba2c72023-01-11 22:37:3439 }
[email protected]85488f22012-01-25 04:34:0140}
41
[email protected]53998282012-06-06 22:08:5242} // namespace
43
Chris Watkins6fe52aa2017-11-28 03:24:0544AuthenticatorTestBase::MockChannelDoneCallback::MockChannelDoneCallback() =
45 default;
[email protected]53998282012-06-06 22:08:5246
Chris Watkins6fe52aa2017-11-28 03:24:0547AuthenticatorTestBase::MockChannelDoneCallback::~MockChannelDoneCallback() =
48 default;
[email protected]53998282012-06-06 22:08:5249
Chris Watkins6fe52aa2017-11-28 03:24:0550AuthenticatorTestBase::AuthenticatorTestBase() = default;
[email protected]53998282012-06-06 22:08:5251
Chris Watkins6fe52aa2017-11-28 03:24:0552AuthenticatorTestBase::~AuthenticatorTestBase() = default;
[email protected]85488f22012-01-25 04:34:0153
54void AuthenticatorTestBase::SetUp() {
[email protected]023ad6ab2013-02-17 05:07:2355 base::FilePath certs_dir(net::GetTestCertsDirectory());
[email protected]85488f22012-01-25 04:34:0156
[email protected]023ad6ab2013-02-17 05:07:2357 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
[email protected]82f84b92013-08-30 18:23:5058 ASSERT_TRUE(base::ReadFileToString(cert_path, &host_cert_));
[email protected]85488f22012-01-25 04:34:0159
[email protected]023ad6ab2013-02-17 05:07:2360 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
[email protected]85488f22012-01-25 04:34:0161 std::string key_string;
[email protected]82f84b92013-08-30 18:23:5062 ASSERT_TRUE(base::ReadFileToString(key_path, &key_string));
[email protected]8f1504b2013-03-07 13:43:1063 std::string key_base64;
[email protected]33fca122013-12-11 01:48:5064 base::Base64Encode(key_string, &key_base64);
[email protected]8f1504b2013-03-07 13:43:1065 key_pair_ = RsaKeyPair::FromString(key_base64);
[email protected]f9d8a772013-06-01 04:33:1766 ASSERT_TRUE(key_pair_.get());
[email protected]9a6361d02013-03-23 16:27:5267 host_public_key_ = key_pair_->GetPublicKey();
[email protected]85488f22012-01-25 04:34:0168}
69
70void AuthenticatorTestBase::RunAuthExchange() {
Joe Downing353ba2c72023-01-11 22:37:3471 ContinueAuthExchangeWith(client_.get(), host_.get(), client_->started(),
[email protected]064128c2014-04-07 22:33:2872 host_->started());
[email protected]c22db292013-03-01 07:59:4073}
[email protected]85488f22012-01-25 04:34:0174
[email protected]9a6361d02013-03-23 16:27:5275void AuthenticatorTestBase::RunHostInitiatedAuthExchange() {
Joe Downing353ba2c72023-01-11 22:37:3476 ContinueAuthExchangeWith(host_.get(), client_.get(), host_->started(),
[email protected]064128c2014-04-07 22:33:2877 client_->started());
[email protected]9a6361d02013-03-23 16:27:5278}
79
[email protected]c22db292013-03-01 07:59:4080// static
[email protected]064128c2014-04-07 22:33:2881// This function sends a message from the sender and receiver and recursively
82// calls itself to the send the next message from the receiver to the sender
83// untils the authentication completes.
[email protected]c22db292013-03-01 07:59:4084void AuthenticatorTestBase::ContinueAuthExchangeWith(Authenticator* sender,
[email protected]064128c2014-04-07 22:33:2885 Authenticator* receiver,
86 bool sender_started,
87 bool receiver_started) {
Mirko Bonadei80d1cea2019-01-18 22:22:1788 std::unique_ptr<jingle_xmpp::XmlElement> message;
[email protected]c22db292013-03-01 07:59:4089 ASSERT_NE(Authenticator::WAITING_MESSAGE, sender->state());
90 if (sender->state() == Authenticator::ACCEPTED ||
sergeyu843ef122016-03-17 01:44:1791 sender->state() == Authenticator::REJECTED) {
[email protected]c22db292013-03-01 07:59:4092 return;
sergeyu843ef122016-03-17 01:44:1793 }
[email protected]064128c2014-04-07 22:33:2894
95 // Verify that once the started flag for either party is set to true,
96 // it should always stay true.
97 if (receiver_started) {
98 ASSERT_TRUE(receiver->started());
99 }
100
101 if (sender_started) {
102 ASSERT_TRUE(sender->started());
103 }
104
[email protected]c22db292013-03-01 07:59:40105 ASSERT_EQ(Authenticator::MESSAGE_READY, sender->state());
106 message = sender->GetNextMessage();
107 ASSERT_TRUE(message.get());
108 ASSERT_NE(Authenticator::MESSAGE_READY, sender->state());
[email protected]85488f22012-01-25 04:34:01109
[email protected]c22db292013-03-01 07:59:40110 ASSERT_EQ(Authenticator::WAITING_MESSAGE, receiver->state());
Evan Stadece9372b2020-03-12 01:28:16111 receiver->ProcessMessage(
112 message.get(),
113 base::BindOnce(&AuthenticatorTestBase::ContinueAuthExchangeWith,
114 base::Unretained(receiver), base::Unretained(sender),
115 receiver->started(), sender->started()));
[email protected]85488f22012-01-25 04:34:01116}
117
118void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) {
Peter Boström42afa23f2021-04-02 22:10:46119 client_fake_socket_ = std::make_unique<FakeStreamSocket>();
120 host_fake_socket_ = std::make_unique<FakeStreamSocket>();
[email protected]85488f22012-01-25 04:34:01121 client_fake_socket_->PairWith(host_fake_socket_.get());
122
123 client_auth_->SecureAndAuthenticate(
sergeyuaa6fa2342015-12-22 23:26:48124 std::move(client_fake_socket_),
Evan Stadec7ae01902020-07-06 16:50:40125 base::BindOnce(&AuthenticatorTestBase::OnClientConnected,
126 base::Unretained(this)));
[email protected]85488f22012-01-25 04:34:01127
128 host_auth_->SecureAndAuthenticate(
sergeyuaa6fa2342015-12-22 23:26:48129 std::move(host_fake_socket_),
Evan Stadec7ae01902020-07-06 16:50:40130 base::BindOnce(&AuthenticatorTestBase::OnHostConnected,
131 base::Unretained(this)));
[email protected]85488f22012-01-25 04:34:01132
[email protected]53998282012-06-06 22:08:52133 // Expect two callbacks to be called - the client callback and the host
134 // callback.
135 int callback_counter = 2;
136
[email protected]de0c4f52013-02-05 00:41:37137 EXPECT_CALL(client_callback_, OnDone(net::OK))
138 .WillOnce(QuitThreadOnCounter(&callback_counter));
[email protected]85488f22012-01-25 04:34:01139 if (expected_fail) {
[email protected]de0c4f52013-02-05 00:41:37140 EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED))
Joe Downing353ba2c72023-01-11 22:37:34141 .WillOnce(QuitThreadOnCounter(&callback_counter));
[email protected]85488f22012-01-25 04:34:01142 } else {
[email protected]de0c4f52013-02-05 00:41:37143 EXPECT_CALL(host_callback_, OnDone(net::OK))
144 .WillOnce(QuitThreadOnCounter(&callback_counter));
[email protected]85488f22012-01-25 04:34:01145 }
146
[email protected]53998282012-06-06 22:08:52147 // Ensure that .Run() does not run unbounded if the callbacks are never
148 // called.
tzik9cd62f9d2018-07-11 02:27:25149 base::OneShotTimer shutdown_timer;
ki.stfu659c758f2015-10-12 20:10:06150 shutdown_timer.Start(FROM_HERE, TestTimeouts::action_timeout(),
Gabriel Charetteea918012018-05-16 11:53:44151 base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
fdoray2ad58be2016-06-22 20:36:16152 base::RunLoop().Run();
[email protected]53998282012-06-06 22:08:52153 shutdown_timer.Stop();
[email protected]85488f22012-01-25 04:34:01154
155 testing::Mock::VerifyAndClearExpectations(&client_callback_);
156 testing::Mock::VerifyAndClearExpectations(&host_callback_);
157
[email protected]53998282012-06-06 22:08:52158 if (!expected_fail) {
sergeyuc5f104b2015-01-09 19:33:24159 ASSERT_TRUE(client_socket_.get() != nullptr);
160 ASSERT_TRUE(host_socket_.get() != nullptr);
[email protected]53998282012-06-06 22:08:52161 }
[email protected]85488f22012-01-25 04:34:01162}
163
[email protected]d7c6cc22012-02-05 05:19:27164void AuthenticatorTestBase::OnHostConnected(
sergeyu9cb142f2014-09-12 20:43:01165 int error,
dcheng0765c492016-04-06 22:41:53166 std::unique_ptr<P2PStreamSocket> socket) {
[email protected]de0c4f52013-02-05 00:41:37167 host_callback_.OnDone(error);
sergeyuaa6fa2342015-12-22 23:26:48168 host_socket_ = std::move(socket);
[email protected]d7c6cc22012-02-05 05:19:27169}
170
171void AuthenticatorTestBase::OnClientConnected(
sergeyu9cb142f2014-09-12 20:43:01172 int error,
dcheng0765c492016-04-06 22:41:53173 std::unique_ptr<P2PStreamSocket> socket) {
[email protected]de0c4f52013-02-05 00:41:37174 client_callback_.OnDone(error);
sergeyuaa6fa2342015-12-22 23:26:48175 client_socket_ = std::move(socket);
[email protected]d7c6cc22012-02-05 05:19:27176}
177
Joe Downing39d710e2022-08-25 20:11:45178} // namespace remoting::protocol