Commit 8544c704 authored by jrw's avatar jrw Committed by Commit bot

Added unit tests for getCredentialsFromAuthCode.

BUG=471928

Review URL: https://codereview.chromium.org/1099553003

Cr-Commit-Position: refs/heads/master@{#327144}
parent e2de4734
......@@ -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",
......
......@@ -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,8 +39,7 @@ void OAuthClient::GetCredentialsFromAuthCode(
kMaxGaiaRetries, this);
}
void OAuthClient::OnGetTokensResponse(
const std::string& refresh_token,
void GaiaOAuthClient::OnGetTokensResponse(const std::string& refresh_token,
const std::string& access_token,
int expires_in_seconds) {
refresh_token_ = refresh_token;
......@@ -53,14 +51,13 @@ void OAuthClient::OnGetTokensResponse(
}
}
void OAuthClient::OnRefreshTokenResponse(
const std::string& access_token,
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,
void GaiaOAuthClient::SendResponse(const std::string& user_email,
const std::string& refresh_token) {
base::ResetAndReturn(&on_done_).Run(user_email, refresh_token);
......@@ -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
// 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_
......@@ -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);
......
......@@ -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,6 +491,11 @@ 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,
......@@ -485,6 +507,7 @@ TEST_F(Me2MeNativeMessagingHostTest, All) {
&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
// 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
// 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_
......@@ -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_
......@@ -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',
......
......@@ -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',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment