OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "remoting/protocol/third_party_authenticator.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "net/base/net_errors.h" |
| 9 #include "remoting/protocol/authenticator_test_base.h" |
| 10 #include "remoting/protocol/channel_authenticator.h" |
| 11 #include "remoting/protocol/connection_tester.h" |
| 12 #include "remoting/protocol/fake_authenticator.h" |
| 13 #include "remoting/protocol/key_pair.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 17 |
| 18 using testing::_; |
| 19 using testing::DeleteArg; |
| 20 using testing::SaveArg; |
| 21 |
| 22 namespace remoting { |
| 23 namespace protocol { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const int kMessageSize = 100; |
| 28 const int kMessages = 1; |
| 29 |
| 30 const char kTokenIssueUrl[] = "https://example.com/Issue"; |
| 31 const char kTokenVerificationUrl[] = "https://example.com/Verify"; |
| 32 const char kTokenScope[] = "host:a@b.com/1 client:a@b.com/2"; |
| 33 const char kToken[] = "abc123456xyz789"; |
| 34 const char kSharedSecret[] = "1234-1234-5678"; |
| 35 const char kSharedSecretBad[] = "0000-0000-0001"; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 class ThirdPartyAuthenticatorTest : public AuthenticatorTestBase { |
| 40 class FakeTokenFetcher |
| 41 : public protocol::ThirdPartyAuthenticator::TokenFetcher { |
| 42 public: |
| 43 virtual void FetchThirdPartyToken( |
| 44 const std::string& token_url, |
| 45 const std::string& host_public_key, |
| 46 const std::string& scope, |
| 47 const base::Callback<void( |
| 48 const std::string& token, |
| 49 const std::string& shared_secret)>&on_token_fetched) { |
| 50 on_token_fetched_ = on_token_fetched; |
| 51 } |
| 52 |
| 53 void OnTokenFetched(const std::string& token, |
| 54 const std::string& shared_secret) { |
| 55 on_token_fetched_.Run(token, shared_secret); |
| 56 on_token_fetched_.Reset(); |
| 57 } |
| 58 |
| 59 base::Callback<void(const std::string& token, |
| 60 const std::string& shared_secret)> on_token_fetched_; |
| 61 }; |
| 62 |
| 63 class FakeTokenValidator |
| 64 : public protocol::ThirdPartyAuthenticator::TokenValidator { |
| 65 public: |
| 66 virtual void ValidateThirdPartyToken( |
| 67 const std::string& token_validation_url, |
| 68 const std::string& token, |
| 69 const std::string& host_public_key, |
| 70 const std::string& token_signature, |
| 71 const std::string& scope, |
| 72 const base::Callback<void( |
| 73 const std::string& shared_secret)>& on_token_validated) { |
| 74 on_token_validated_ = on_token_validated; |
| 75 } |
| 76 |
| 77 void OnTokenValidated(const std::string& shared_secret) { |
| 78 on_token_validated_.Run(shared_secret); |
| 79 on_token_validated_.Reset(); |
| 80 } |
| 81 virtual ~FakeTokenValidator() {} |
| 82 base::Callback<void(const std::string& shared_secret)> on_token_validated_; |
| 83 }; |
| 84 |
| 85 public: |
| 86 ThirdPartyAuthenticatorTest() { |
| 87 } |
| 88 virtual ~ThirdPartyAuthenticatorTest() { |
| 89 } |
| 90 |
| 91 protected: |
| 92 void InitAuthenticators() { |
| 93 scoped_ptr<protocol::ThirdPartyAuthenticator::TokenValidator> |
| 94 token_validator(new FakeTokenValidator()); |
| 95 token_validator_ = static_cast<FakeTokenValidator*>(token_validator.get()); |
| 96 host_ = ThirdPartyAuthenticator::CreateForHost( |
| 97 host_cert_, key_pair_->Copy(), |
| 98 kTokenIssueUrl, kTokenVerificationUrl, kTokenScope, |
| 99 token_validator.Pass(), Authenticator::WAITING_MESSAGE); |
| 100 client_ = ThirdPartyAuthenticator::CreateForClient( |
| 101 host_public_key_, &token_fetcher_, Authenticator::MESSAGE_READY); |
| 102 } |
| 103 |
| 104 FakeTokenFetcher token_fetcher_; |
| 105 FakeTokenValidator* token_validator_; |
| 106 |
| 107 private: |
| 108 DISALLOW_COPY_AND_ASSIGN(ThirdPartyAuthenticatorTest); |
| 109 }; |
| 110 |
| 111 TEST_F(ThirdPartyAuthenticatorTest, SuccessfulShortAuth) { |
| 112 ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 113 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); |
| 114 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, client_->state()); |
| 115 ASSERT_NO_FATAL_FAILURE(token_fetcher_.OnTokenFetched(kToken, kSharedSecret)); |
| 116 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, host_->state()); |
| 117 ASSERT_NO_FATAL_FAILURE( |
| 118 token_validator_->OnTokenValidated(kSharedSecret)); |
| 119 // Both sides have finished. |
| 120 ASSERT_EQ(Authenticator::ACCEPTED, host_->state()); |
| 121 ASSERT_EQ(Authenticator::ACCEPTED, client_->state()); |
| 122 |
| 123 // An authenticated channel can be created after the authentication. |
| 124 client_auth_ = client_->CreateChannelAuthenticator(); |
| 125 host_auth_ = host_->CreateChannelAuthenticator(); |
| 126 RunChannelAuth(false); |
| 127 |
| 128 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 129 kMessageSize, kMessages); |
| 130 |
| 131 tester.Start(); |
| 132 message_loop_.Run(); |
| 133 tester.CheckResults(); |
| 134 } |
| 135 |
| 136 TEST_F(ThirdPartyAuthenticatorTest, ClientNoSecret) { |
| 137 ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 138 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); |
| 139 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, client_->state()); |
| 140 ASSERT_NO_FATAL_FAILURE(token_fetcher_.OnTokenFetched(kToken, "")); |
| 141 // The end result is that the client rejected the connection, since it |
| 142 // couldn't fetch the secret. |
| 143 ASSERT_EQ(Authenticator::REJECTED, client_->state()); |
| 144 } |
| 145 |
| 146 TEST_F(ThirdPartyAuthenticatorTest, InvalidToken) { |
| 147 ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 148 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); |
| 149 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, client_->state()); |
| 150 ASSERT_NO_FATAL_FAILURE(token_fetcher_.OnTokenFetched(kToken, kSharedSecret)); |
| 151 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, host_->state()); |
| 152 ASSERT_NO_FATAL_FAILURE(token_validator_->OnTokenValidated("")); |
| 153 |
| 154 // The end result is that the host rejected the token. |
| 155 ASSERT_EQ(Authenticator::REJECTED, host_->state()); |
| 156 } |
| 157 |
| 158 TEST_F(ThirdPartyAuthenticatorTest, CannotFetchToken) { |
| 159 ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 160 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); |
| 161 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, client_->state()); |
| 162 ASSERT_NO_FATAL_FAILURE(token_fetcher_.OnTokenFetched("", "")); |
| 163 |
| 164 // The end result is that the client rejected the connection, since it |
| 165 // couldn't fetch the token. |
| 166 ASSERT_EQ(Authenticator::REJECTED, client_->state()); |
| 167 } |
| 168 |
| 169 // Test that negotiation stops when the fake authentication is rejected. |
| 170 TEST_F(ThirdPartyAuthenticatorTest, HostBadSecret) { |
| 171 ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 172 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); |
| 173 |
| 174 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, client_->state()); |
| 175 ASSERT_NO_FATAL_FAILURE(token_fetcher_.OnTokenFetched(kToken, kSharedSecret)); |
| 176 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, host_->state()); |
| 177 ASSERT_NO_FATAL_FAILURE( |
| 178 token_validator_->OnTokenValidated(kSharedSecretBad)); |
| 179 |
| 180 // The end result is that the host rejected the fake authentication. |
| 181 ASSERT_EQ(Authenticator::REJECTED, client_->state()); |
| 182 } |
| 183 |
| 184 TEST_F(ThirdPartyAuthenticatorTest, ClientBadSecret) { |
| 185 ASSERT_NO_FATAL_FAILURE(InitAuthenticators()); |
| 186 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); |
| 187 |
| 188 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, client_->state()); |
| 189 ASSERT_NO_FATAL_FAILURE( |
| 190 token_fetcher_.OnTokenFetched(kToken, kSharedSecretBad)); |
| 191 ASSERT_EQ(Authenticator::WAITING_EXTERNAL, host_->state()); |
| 192 ASSERT_NO_FATAL_FAILURE( |
| 193 token_validator_->OnTokenValidated(kSharedSecret)); |
| 194 |
| 195 // The end result is that the host rejected the fake authentication. |
| 196 ASSERT_EQ(Authenticator::REJECTED, client_->state()); |
| 197 } |
| 198 |
| 199 } // namespace protocol |
| 200 } // namespace remoting |
OLD | NEW |