diff options
author | jrw <jrw@chromium.org> | 2015-04-27 14:45:26 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-27 21:48:50 +0000 |
commit | 8544c7042687e2ede68ee00ea6b599072cfc1768 (patch) | |
tree | 3e787d8e0d9637553065cfd51bd940a336cb8c6f /remoting | |
parent | e2de4734b01f37454f939bc493a8a5cb59ce3c42 (diff) | |
download | chromium_src-8544c7042687e2ede68ee00ea6b599072cfc1768.zip chromium_src-8544c7042687e2ede68ee00ea6b599072cfc1768.tar.gz chromium_src-8544c7042687e2ede68ee00ea6b599072cfc1768.tar.bz2 |
Added unit tests for getCredentialsFromAuthCode.
BUG=471928
Review URL: https://codereview.chromium.org/1099553003
Cr-Commit-Position: refs/heads/master@{#327144}
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/host/BUILD.gn | 1 | ||||
-rw-r--r-- | remoting/host/setup/gaia_oauth_client.cc (renamed from remoting/host/setup/oauth_client.cc) | 42 | ||||
-rw-r--r-- | remoting/host/setup/gaia_oauth_client.h | 83 | ||||
-rw-r--r-- | remoting/host/setup/me2me_native_messaging_host_main.cc | 3 | ||||
-rw-r--r-- | remoting/host/setup/me2me_native_messaging_host_unittest.cc | 54 | ||||
-rw-r--r-- | remoting/host/setup/mock_oauth_client.cc | 25 | ||||
-rw-r--r-- | remoting/host/setup/mock_oauth_client.h | 32 | ||||
-rw-r--r-- | remoting/host/setup/oauth_client.h | 62 | ||||
-rw-r--r-- | remoting/remoting_host_srcs.gypi | 3 | ||||
-rw-r--r-- | remoting/remoting_test.gypi | 2 |
10 files changed, 216 insertions, 91 deletions
diff --git a/remoting/host/BUILD.gn b/remoting/host/BUILD.gn index 8e71b25..97065eb 100644 --- a/remoting/host/BUILD.gn +++ b/remoting/host/BUILD.gn @@ -235,6 +235,7 @@ if (is_mac) { # TODO(GYP) Mac build of remoting host. "screen_resolution_unittest.cc", "server_log_entry_host_unittest.cc", "setup/me2me_native_messaging_host_unittest.cc", + "setup/mock_oauth_client.cc", "setup/oauth_helper_unittest.cc", "setup/pin_validator_unittest.cc", "shaped_desktop_capturer_unittest.cc", diff --git a/remoting/host/setup/oauth_client.cc b/remoting/host/setup/gaia_oauth_client.cc index fae8021..9855dd6 100644 --- a/remoting/host/setup/oauth_client.cc +++ b/remoting/host/setup/gaia_oauth_client.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "remoting/host/setup/oauth_client.h" +#include "remoting/host/setup/gaia_oauth_client.h" #include "base/callback_helpers.h" #include "base/logging.h" @@ -13,20 +13,19 @@ const int kMaxGaiaRetries = 3; namespace remoting { -OAuthClient::OAuthClient( +GaiaOAuthClient::GaiaOAuthClient( scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) : gaia_oauth_client_(url_request_context_getter.get()) { } -OAuthClient::~OAuthClient() { +GaiaOAuthClient::~GaiaOAuthClient() { } -void OAuthClient::GetCredentialsFromAuthCode( +void GaiaOAuthClient::GetCredentialsFromAuthCode( const gaia::OAuthClientInfo& oauth_client_info, const std::string& auth_code, bool need_user_email, CompletionCallback on_done) { - if (!on_done_.is_null()) { pending_requests_.push( Request(oauth_client_info, auth_code, need_user_email, on_done)); @@ -40,10 +39,9 @@ void OAuthClient::GetCredentialsFromAuthCode( kMaxGaiaRetries, this); } -void OAuthClient::OnGetTokensResponse( - const std::string& refresh_token, - const std::string& access_token, - int expires_in_seconds) { +void GaiaOAuthClient::OnGetTokensResponse(const std::string& refresh_token, + const std::string& access_token, + int expires_in_seconds) { refresh_token_ = refresh_token; if (need_user_email_) { // Get the email corresponding to the access token. @@ -53,15 +51,14 @@ void OAuthClient::OnGetTokensResponse( } } -void OAuthClient::OnRefreshTokenResponse( - const std::string& access_token, - int expires_in_seconds) { +void GaiaOAuthClient::OnRefreshTokenResponse(const std::string& access_token, + int expires_in_seconds) { // We never request a refresh token, so this call is not expected. NOTREACHED(); } -void OAuthClient::SendResponse(const std::string& user_email, - const std::string& refresh_token) { +void GaiaOAuthClient::SendResponse(const std::string& user_email, + const std::string& refresh_token) { base::ResetAndReturn(&on_done_).Run(user_email, refresh_token); // Process the next request in the queue. @@ -69,27 +66,24 @@ void OAuthClient::SendResponse(const std::string& user_email, Request request = pending_requests_.front(); pending_requests_.pop(); // GetCredentialsFromAuthCode is asynchronous, so it's safe to call it here. - GetCredentialsFromAuthCode( - request.oauth_client_info, - request.auth_code, - request.need_user_email, - request.on_done); + GetCredentialsFromAuthCode(request.oauth_client_info, request.auth_code, + request.need_user_email, request.on_done); } } -void OAuthClient::OnGetUserEmailResponse(const std::string& user_email) { +void GaiaOAuthClient::OnGetUserEmailResponse(const std::string& user_email) { SendResponse(user_email, refresh_token_); } -void OAuthClient::OnOAuthError() { +void GaiaOAuthClient::OnOAuthError() { SendResponse("", ""); } -void OAuthClient::OnNetworkError(int response_code) { +void GaiaOAuthClient::OnNetworkError(int response_code) { SendResponse("", ""); } -OAuthClient::Request::Request( +GaiaOAuthClient::Request::Request( const gaia::OAuthClientInfo& oauth_client_info, const std::string& auth_code, bool need_user_email, @@ -100,7 +94,7 @@ OAuthClient::Request::Request( this->on_done = on_done; } -OAuthClient::Request::~Request() { +GaiaOAuthClient::Request::~Request() { } } // namespace remoting diff --git a/remoting/host/setup/gaia_oauth_client.h b/remoting/host/setup/gaia_oauth_client.h new file mode 100644 index 0000000..d6bb72c --- /dev/null +++ b/remoting/host/setup/gaia_oauth_client.h @@ -0,0 +1,83 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef REMOTING_HOST_SETUP_GAIA_OAUTH_CLIENT_H_ +#define REMOTING_HOST_SETUP_GAIA_OAUTH_CLIENT_H_ + +#include <queue> + +#include "base/memory/ref_counted.h" +#include "google_apis/gaia/gaia_oauth_client.h" +#include "net/url_request/url_request_context_getter.h" + +#include "remoting/host/setup/oauth_client.h" + +namespace net { +class URLRequestContext; +} + +namespace remoting { + +// A wrapper around gaia::GaiaOAuthClient that provides a more +// convenient interface, with queueing of requests and a callback +// rather than a delegate. +class GaiaOAuthClient : public OAuthClient, + public gaia::GaiaOAuthClient::Delegate { + public: + GaiaOAuthClient( + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); + + ~GaiaOAuthClient() override; + + // Redeems |auth_code| using |oauth_client_info| to obtain |refresh_token| and + // |access_token|, then uses the userinfo endpoint to obtain |user_email|. + // Calls CompletionCallback with |user_email| and |refresh_token| when done, + // or with empty strings on error. + // If a request is received while another one is being processed, it is + // enqueued and processed after the first one is finished. + void GetCredentialsFromAuthCode( + const gaia::OAuthClientInfo& oauth_client_info, + const std::string& auth_code, + bool need_user_email, + CompletionCallback on_done) override; + + // gaia::GaiaOAuthClient::Delegate + void OnGetTokensResponse(const std::string& refresh_token, + const std::string& access_token, + int expires_in_seconds) override; + void OnRefreshTokenResponse(const std::string& access_token, + int expires_in_seconds) override; + void OnGetUserEmailResponse(const std::string& user_email) override; + + void OnOAuthError() override; + void OnNetworkError(int response_code) override; + + private: + struct Request { + Request(const gaia::OAuthClientInfo& oauth_client_info, + const std::string& auth_code, + bool need_user_email, + CompletionCallback on_done); + virtual ~Request(); + gaia::OAuthClientInfo oauth_client_info; + std::string auth_code; + bool need_user_email; + CompletionCallback on_done; + }; + + void SendResponse(const std::string& user_email, + const std::string& refresh_token); + + std::queue<Request> pending_requests_; + gaia::GaiaOAuthClient gaia_oauth_client_; + std::string refresh_token_; + bool need_user_email_; + CompletionCallback on_done_; + + DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClient); +}; + +} // namespace remoting + +#endif // REMOTING_HOST_SETUP_GAIA_OAUTH_CLIENT_H_ diff --git a/remoting/host/setup/me2me_native_messaging_host_main.cc b/remoting/host/setup/me2me_native_messaging_host_main.cc index 87aec46..bfc943e5 100644 --- a/remoting/host/setup/me2me_native_messaging_host_main.cc +++ b/remoting/host/setup/me2me_native_messaging_host_main.cc @@ -19,6 +19,7 @@ #include "remoting/host/logging.h" #include "remoting/host/native_messaging/pipe_messaging_channel.h" #include "remoting/host/pairing_registry_delegate.h" +#include "remoting/host/setup/gaia_oauth_client.h" #include "remoting/host/setup/me2me_native_messaging_host.h" #include "remoting/host/usage_stats_consent.h" @@ -197,7 +198,7 @@ int StartMe2MeNativeMessagingHost() { new URLRequestContextGetter(io_thread.task_runner(), file_thread.task_runner())); scoped_ptr<OAuthClient> oauth_client( - new OAuthClient(url_request_context_getter)); + new GaiaOAuthClient(url_request_context_getter)); net::URLFetcher::SetIgnoreCertificateRequests(true); diff --git a/remoting/host/setup/me2me_native_messaging_host_unittest.cc b/remoting/host/setup/me2me_native_messaging_host_unittest.cc index c9a0ae0..bdd6daf 100644 --- a/remoting/host/setup/me2me_native_messaging_host_unittest.cc +++ b/remoting/host/setup/me2me_native_messaging_host_unittest.cc @@ -19,6 +19,7 @@ #include "remoting/base/auto_thread_task_runner.h" #include "remoting/host/native_messaging/pipe_messaging_channel.h" #include "remoting/host/pin_hash.h" +#include "remoting/host/setup/mock_oauth_client.h" #include "remoting/host/setup/test_util.h" #include "remoting/protocol/pairing_registry.h" #include "remoting/protocol/protocol_mock_objects.h" @@ -134,6 +135,18 @@ void VerifyStartDaemonResponse(scoped_ptr<base::DictionaryValue> response) { EXPECT_EQ("OK", value); } +void VerifyGetCredentialsFromAuthCodeResponse( + scoped_ptr<base::DictionaryValue> response) { + ASSERT_TRUE(response); + std::string value; + EXPECT_TRUE(response->GetString("type", &value)); + EXPECT_EQ("getCredentialsFromAuthCodeResponse", value); + EXPECT_TRUE(response->GetString("userEmail", &value)); + EXPECT_EQ("fake_user_email", value); + EXPECT_TRUE(response->GetString("refreshToken", &value)); + EXPECT_EQ("fake_refresh_token", value); +} + } // namespace namespace remoting { @@ -313,8 +326,12 @@ void Me2MeNativeMessagingHostTest::StartHost() { new PipeMessagingChannel(input_read_file.Pass(), output_write_file.Pass())); - host_.reset(new Me2MeNativeMessagingHost( - false, 0, channel.Pass(), daemon_controller, pairing_registry, nullptr)); + scoped_ptr<OAuthClient> oauth_client( + new MockOAuthClient("fake_user_email", "fake_refresh_token")); + + host_.reset(new Me2MeNativeMessagingHost(false, 0, channel.Pass(), + daemon_controller, pairing_registry, + oauth_client.Pass())); host_->Start(base::Bind(&Me2MeNativeMessagingHostTest::StopHost, base::Unretained(this))); @@ -474,17 +491,23 @@ TEST_F(Me2MeNativeMessagingHostTest, All) { message.SetString("type", "startDaemon"); WriteMessageToInputPipe(message); + message.SetInteger("id", next_id++); + message.SetString("type", "getCredentialsFromAuthCode"); + message.SetString("authorizationCode", "fake_auth_code"); + WriteMessageToInputPipe(message); + void (*verify_routines[])(scoped_ptr<base::DictionaryValue>) = { - &VerifyHelloResponse, - &VerifyGetHostNameResponse, - &VerifyGetPinHashResponse, - &VerifyGenerateKeyPairResponse, - &VerifyGetDaemonConfigResponse, - &VerifyGetUsageStatsConsentResponse, - &VerifyStopDaemonResponse, - &VerifyGetDaemonStateResponse, - &VerifyUpdateDaemonConfigResponse, - &VerifyStartDaemonResponse, + &VerifyHelloResponse, + &VerifyGetHostNameResponse, + &VerifyGetPinHashResponse, + &VerifyGenerateKeyPairResponse, + &VerifyGetDaemonConfigResponse, + &VerifyGetUsageStatsConsentResponse, + &VerifyStopDaemonResponse, + &VerifyGetDaemonStateResponse, + &VerifyUpdateDaemonConfigResponse, + &VerifyStartDaemonResponse, + &VerifyGetCredentialsFromAuthCodeResponse, }; ASSERT_EQ(arraysize(verify_routines), static_cast<size_t>(next_id)); @@ -585,4 +608,11 @@ TEST_F(Me2MeNativeMessagingHostTest, StartDaemonNoConsent) { TestBadRequest(message); } +// Verify rejection if getCredentialsFromAuthCode has no auth code. +TEST_F(Me2MeNativeMessagingHostTest, GetCredentialsFromAuthCodeNoAuthCode) { + base::DictionaryValue message; + message.SetString("type", "getCredentialsFromAuthCode"); + TestBadRequest(message); +} + } // namespace remoting diff --git a/remoting/host/setup/mock_oauth_client.cc b/remoting/host/setup/mock_oauth_client.cc new file mode 100644 index 0000000..bd78aa6 --- /dev/null +++ b/remoting/host/setup/mock_oauth_client.cc @@ -0,0 +1,25 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "remoting/host/setup/mock_oauth_client.h" + +namespace remoting { + +MockOAuthClient::MockOAuthClient(const std::string& user_email, + const std::string& refresh_token) + : user_email_(user_email), refresh_token_(refresh_token) { +} + +MockOAuthClient::~MockOAuthClient() { +} + +void MockOAuthClient::GetCredentialsFromAuthCode( + const gaia::OAuthClientInfo& oauth_client_info, + const std::string& auth_code, + bool need_user_email, + CompletionCallback on_done) { + on_done.Run(need_user_email ? user_email_ : "", refresh_token_); +} + +} // namespace remoting diff --git a/remoting/host/setup/mock_oauth_client.h b/remoting/host/setup/mock_oauth_client.h new file mode 100644 index 0000000..db235a5 --- /dev/null +++ b/remoting/host/setup/mock_oauth_client.h @@ -0,0 +1,32 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef REMOTING_HOST_SETUP_MOCK_OAUTH_CLIENT_H_ +#define REMOTING_HOST_SETUP_MOCK_OAUTH_CLIENT_H_ + +#include "remoting/host/setup/oauth_client.h" + +namespace remoting { + +class MockOAuthClient : public OAuthClient { + public: + MockOAuthClient(const std::string& user_email, + const std::string& refresh_token); + + ~MockOAuthClient() override; + + void GetCredentialsFromAuthCode( + const gaia::OAuthClientInfo& oauth_client_info, + const std::string& auth_code, + bool need_user_email, + CompletionCallback on_done) override; + + private: + std::string user_email_; + std::string refresh_token_; +}; + +} // namespace remoting + +#endif // REMOTING_HOST_SETUP_MOCK_OAUTH_CLIENT_H_ diff --git a/remoting/host/setup/oauth_client.h b/remoting/host/setup/oauth_client.h index 79d1d9a..8da652b 100644 --- a/remoting/host/setup/oauth_client.h +++ b/remoting/host/setup/oauth_client.h @@ -2,26 +2,20 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef REMOTING_HOST_SETUP_OAUTH_CLIENT -#define REMOTING_HOST_SETUP_OAUTH_CLIENT +#ifndef REMOTING_HOST_SETUP_OAUTH_CLIENT_H_ +#define REMOTING_HOST_SETUP_OAUTH_CLIENT_H_ -#include <queue> #include <string> #include "base/callback.h" -#include "base/memory/ref_counted.h" -#include "google_apis/gaia/gaia_oauth_client.h" -#include "net/url_request/url_request_context_getter.h" -namespace net { -class URLRequestContext; +namespace gaia { +struct OAuthClientInfo; } namespace remoting { -// A wrapper around GaiaOAuthClient that provides a more convenient interface, -// with queueing of requests and a callback rather than a delegate. -class OAuthClient : public gaia::GaiaOAuthClient::Delegate { +class OAuthClient { public: // Called when GetCredentialsFromAuthCode is completed, with the |user_email| // and |refresh_token| that correspond to the given |auth_code|, or with empty @@ -30,10 +24,7 @@ class OAuthClient : public gaia::GaiaOAuthClient::Delegate { const std::string& user_email, const std::string& refresh_token)> CompletionCallback; - OAuthClient( - scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); - - ~OAuthClient() override; + virtual ~OAuthClient() {} // Redeems |auth_code| using |oauth_client_info| to obtain // |refresh_token| and |access_token|, then, if |need_user_email| is @@ -42,48 +33,13 @@ class OAuthClient : public gaia::GaiaOAuthClient::Delegate { // done, or with empty strings on error. If a request is received // while another one is being processed, it is enqueued and // processed after the first one is finished. - void GetCredentialsFromAuthCode( + virtual void GetCredentialsFromAuthCode( const gaia::OAuthClientInfo& oauth_client_info, const std::string& auth_code, bool need_user_email, - CompletionCallback on_done); - - // gaia::GaiaOAuthClient::Delegate - void OnGetTokensResponse(const std::string& refresh_token, - const std::string& access_token, - int expires_in_seconds) override; - void OnRefreshTokenResponse(const std::string& access_token, - int expires_in_seconds) override; - void OnGetUserEmailResponse(const std::string& user_email) override; - - void OnOAuthError() override; - void OnNetworkError(int response_code) override; - - private: - struct Request { - Request(const gaia::OAuthClientInfo& oauth_client_info, - const std::string& auth_code, - bool need_user_email, - CompletionCallback on_done); - virtual ~Request(); - gaia::OAuthClientInfo oauth_client_info; - std::string auth_code; - bool need_user_email; - CompletionCallback on_done; - }; - - void SendResponse(const std::string& user_email, - const std::string& refresh_token); - - std::queue<Request> pending_requests_; - gaia::GaiaOAuthClient gaia_oauth_client_; - std::string refresh_token_; - bool need_user_email_; - CompletionCallback on_done_; - - DISALLOW_COPY_AND_ASSIGN(OAuthClient); + CompletionCallback on_done) = 0; }; } // namespace remoting -#endif // REMOTING_HOST_SETUP_OAUTH_CLIENT +#endif // REMOTING_HOST_SETUP_OAUTH_CLIENT_H_ diff --git a/remoting/remoting_host_srcs.gypi b/remoting/remoting_host_srcs.gypi index b260c87..de3eb37 100644 --- a/remoting/remoting_host_srcs.gypi +++ b/remoting/remoting_host_srcs.gypi @@ -270,9 +270,10 @@ 'host/setup/daemon_controller_delegate_mac.mm', 'host/setup/daemon_controller_delegate_win.cc', 'host/setup/daemon_controller_delegate_win.h', + 'host/setup/gaia_oauth_client.cc', + 'host/setup/gaia_oauth_client.h', 'host/setup/me2me_native_messaging_host.cc', 'host/setup/me2me_native_messaging_host.h', - 'host/setup/oauth_client.cc', 'host/setup/oauth_client.h', 'host/setup/oauth_helper.cc', 'host/setup/oauth_helper.h', diff --git a/remoting/remoting_test.gypi b/remoting/remoting_test.gypi index 206eb65..ac1f3d5 100644 --- a/remoting/remoting_test.gypi +++ b/remoting/remoting_test.gypi @@ -208,6 +208,8 @@ 'host/resizing_host_observer_unittest.cc', 'host/screen_resolution_unittest.cc', 'host/server_log_entry_host_unittest.cc', + 'host/setup/mock_oauth_client.cc', + 'host/setup/mock_oauth_client.h', 'host/setup/me2me_native_messaging_host.cc', 'host/setup/me2me_native_messaging_host.h', 'host/setup/me2me_native_messaging_host_unittest.cc', |