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