Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 2 | // 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öm | 42afa23f | 2021-04-02 22:10:46 | [diff] [blame] | 7 | #include <memory> |
sergeyu | 89d088b | 2015-12-24 00:22:44 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | 8f1504b | 2013-03-07 13:43:10 | [diff] [blame] | 10 | #include "base/base64.h" |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
thestig | 1ecdcf4 | 2014-09-12 05:09:14 | [diff] [blame] | 13 | #include "base/files/file_util.h" |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 14 | #include "base/run_loop.h" |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 15 | #include "base/test/test_timeouts.h" |
[email protected] | 5d7eb86 | 2013-06-28 15:21:24 | [diff] [blame] | 16 | #include "base/timer/timer.h" |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 17 | #include "net/base/net_errors.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 18 | #include "net/test/test_data_directory.h" |
[email protected] | 8f1504b | 2013-03-07 13:43:10 | [diff] [blame] | 19 | #include "remoting/base/rsa_key_pair.h" |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 20 | #include "remoting/protocol/authenticator.h" |
| 21 | #include "remoting/protocol/channel_authenticator.h" |
sergeyu | 98d8fe9 | 2014-09-23 21:56:20 | [diff] [blame] | 22 | #include "remoting/protocol/fake_stream_socket.h" |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 23 | #include "remoting/protocol/p2p_stream_socket.h" |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
kjellander | f0e410b | 2017-01-04 14:45:01 | [diff] [blame] | 25 | #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 26 | |
| 27 | using testing::_; |
| 28 | using testing::SaveArg; |
| 29 | |
Joe Downing | 39d710e | 2022-08-25 20:11:45 | [diff] [blame] | 30 | namespace remoting::protocol { |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 31 | |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | ACTION_P(QuitThreadOnCounter, counter) { |
| 35 | --(*counter); |
| 36 | EXPECT_GE(*counter, 0); |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 37 | if (*counter == 0) { |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 38 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 39 | } |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 42 | } // namespace |
| 43 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 44 | AuthenticatorTestBase::MockChannelDoneCallback::MockChannelDoneCallback() = |
| 45 | default; |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 46 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 47 | AuthenticatorTestBase::MockChannelDoneCallback::~MockChannelDoneCallback() = |
| 48 | default; |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 49 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 50 | AuthenticatorTestBase::AuthenticatorTestBase() = default; |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 51 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 52 | AuthenticatorTestBase::~AuthenticatorTestBase() = default; |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 53 | |
| 54 | void AuthenticatorTestBase::SetUp() { |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 55 | base::FilePath certs_dir(net::GetTestCertsDirectory()); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 56 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 57 | base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 58 | ASSERT_TRUE(base::ReadFileToString(cert_path, &host_cert_)); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 59 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 60 | base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 61 | std::string key_string; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 62 | ASSERT_TRUE(base::ReadFileToString(key_path, &key_string)); |
[email protected] | 8f1504b | 2013-03-07 13:43:10 | [diff] [blame] | 63 | std::string key_base64; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 64 | base::Base64Encode(key_string, &key_base64); |
[email protected] | 8f1504b | 2013-03-07 13:43:10 | [diff] [blame] | 65 | key_pair_ = RsaKeyPair::FromString(key_base64); |
[email protected] | f9d8a77 | 2013-06-01 04:33:17 | [diff] [blame] | 66 | ASSERT_TRUE(key_pair_.get()); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 67 | host_public_key_ = key_pair_->GetPublicKey(); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void AuthenticatorTestBase::RunAuthExchange() { |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 71 | ContinueAuthExchangeWith(client_.get(), host_.get(), client_->started(), |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 72 | host_->started()); |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 73 | } |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 74 | |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 75 | void AuthenticatorTestBase::RunHostInitiatedAuthExchange() { |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 76 | ContinueAuthExchangeWith(host_.get(), client_.get(), host_->started(), |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 77 | client_->started()); |
[email protected] | 9a6361d0 | 2013-03-23 16:27:52 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 80 | // static |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 81 | // 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] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 84 | void AuthenticatorTestBase::ContinueAuthExchangeWith(Authenticator* sender, |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 85 | Authenticator* receiver, |
| 86 | bool sender_started, |
| 87 | bool receiver_started) { |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 88 | std::unique_ptr<jingle_xmpp::XmlElement> message; |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 89 | ASSERT_NE(Authenticator::WAITING_MESSAGE, sender->state()); |
| 90 | if (sender->state() == Authenticator::ACCEPTED || |
sergeyu | 843ef12 | 2016-03-17 01:44:17 | [diff] [blame] | 91 | sender->state() == Authenticator::REJECTED) { |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 92 | return; |
sergeyu | 843ef12 | 2016-03-17 01:44:17 | [diff] [blame] | 93 | } |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 94 | |
| 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] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 105 | 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] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 109 | |
[email protected] | c22db29 | 2013-03-01 07:59:40 | [diff] [blame] | 110 | ASSERT_EQ(Authenticator::WAITING_MESSAGE, receiver->state()); |
Evan Stade | ce9372b | 2020-03-12 01:28:16 | [diff] [blame] | 111 | receiver->ProcessMessage( |
| 112 | message.get(), |
| 113 | base::BindOnce(&AuthenticatorTestBase::ContinueAuthExchangeWith, |
| 114 | base::Unretained(receiver), base::Unretained(sender), |
| 115 | receiver->started(), sender->started())); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) { |
Peter Boström | 42afa23f | 2021-04-02 22:10:46 | [diff] [blame] | 119 | client_fake_socket_ = std::make_unique<FakeStreamSocket>(); |
| 120 | host_fake_socket_ = std::make_unique<FakeStreamSocket>(); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 121 | client_fake_socket_->PairWith(host_fake_socket_.get()); |
| 122 | |
| 123 | client_auth_->SecureAndAuthenticate( |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 124 | std::move(client_fake_socket_), |
Evan Stade | c7ae0190 | 2020-07-06 16:50:40 | [diff] [blame] | 125 | base::BindOnce(&AuthenticatorTestBase::OnClientConnected, |
| 126 | base::Unretained(this))); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 127 | |
| 128 | host_auth_->SecureAndAuthenticate( |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 129 | std::move(host_fake_socket_), |
Evan Stade | c7ae0190 | 2020-07-06 16:50:40 | [diff] [blame] | 130 | base::BindOnce(&AuthenticatorTestBase::OnHostConnected, |
| 131 | base::Unretained(this))); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 132 | |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 133 | // Expect two callbacks to be called - the client callback and the host |
| 134 | // callback. |
| 135 | int callback_counter = 2; |
| 136 | |
[email protected] | de0c4f5 | 2013-02-05 00:41:37 | [diff] [blame] | 137 | EXPECT_CALL(client_callback_, OnDone(net::OK)) |
| 138 | .WillOnce(QuitThreadOnCounter(&callback_counter)); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 139 | if (expected_fail) { |
[email protected] | de0c4f5 | 2013-02-05 00:41:37 | [diff] [blame] | 140 | EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED)) |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 141 | .WillOnce(QuitThreadOnCounter(&callback_counter)); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 142 | } else { |
[email protected] | de0c4f5 | 2013-02-05 00:41:37 | [diff] [blame] | 143 | EXPECT_CALL(host_callback_, OnDone(net::OK)) |
| 144 | .WillOnce(QuitThreadOnCounter(&callback_counter)); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 147 | // Ensure that .Run() does not run unbounded if the callbacks are never |
| 148 | // called. |
tzik | 9cd62f9d | 2018-07-11 02:27:25 | [diff] [blame] | 149 | base::OneShotTimer shutdown_timer; |
ki.stfu | 659c758f | 2015-10-12 20:10:06 | [diff] [blame] | 150 | shutdown_timer.Start(FROM_HERE, TestTimeouts::action_timeout(), |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 151 | base::RunLoop::QuitCurrentWhenIdleClosureDeprecated()); |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 152 | base::RunLoop().Run(); |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 153 | shutdown_timer.Stop(); |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 154 | |
| 155 | testing::Mock::VerifyAndClearExpectations(&client_callback_); |
| 156 | testing::Mock::VerifyAndClearExpectations(&host_callback_); |
| 157 | |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 158 | if (!expected_fail) { |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 159 | ASSERT_TRUE(client_socket_.get() != nullptr); |
| 160 | ASSERT_TRUE(host_socket_.get() != nullptr); |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 161 | } |
[email protected] | 85488f2 | 2012-01-25 04:34:01 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | d7c6cc2 | 2012-02-05 05:19:27 | [diff] [blame] | 164 | void AuthenticatorTestBase::OnHostConnected( |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 165 | int error, |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 166 | std::unique_ptr<P2PStreamSocket> socket) { |
[email protected] | de0c4f5 | 2013-02-05 00:41:37 | [diff] [blame] | 167 | host_callback_.OnDone(error); |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 168 | host_socket_ = std::move(socket); |
[email protected] | d7c6cc2 | 2012-02-05 05:19:27 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void AuthenticatorTestBase::OnClientConnected( |
sergeyu | 9cb142f | 2014-09-12 20:43:01 | [diff] [blame] | 172 | int error, |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 173 | std::unique_ptr<P2PStreamSocket> socket) { |
[email protected] | de0c4f5 | 2013-02-05 00:41:37 | [diff] [blame] | 174 | client_callback_.OnDone(error); |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 175 | client_socket_ = std::move(socket); |
[email protected] | d7c6cc2 | 2012-02-05 05:19:27 | [diff] [blame] | 176 | } |
| 177 | |
Joe Downing | 39d710e | 2022-08-25 20:11:45 | [diff] [blame] | 178 | } // namespace remoting::protocol |