Chromium Code Reviews| Index: remoting/test/app_remoting_test_driver_environment_unittest.cc |
| diff --git a/remoting/test/app_remoting_test_driver_environment_unittest.cc b/remoting/test/app_remoting_test_driver_environment_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6869572ff982974b406e2355ff68cbda6ddeacd4 |
| --- /dev/null |
| +++ b/remoting/test/app_remoting_test_driver_environment_unittest.cc |
| @@ -0,0 +1,418 @@ |
| +// 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/test/app_remoting_test_driver_environment.h" |
| + |
| +#include "remoting/test/fake_access_token_fetcher.h" |
| +#include "remoting/test/mock_access_token_fetcher.h" |
| +#include "remoting/test/refresh_token_store.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| +const char kAuthCodeValue[] = "4/892379827345jkefvkdfbv"; |
| +const char kRefreshTokenValue[] = "1/lkjalseLKJlsiJgr45jbv"; |
| +const char kUserNameValue[] = "remoting_user@gmail.com"; |
| +const char kDevEnvironmentValue[] = "dev"; |
| +} |
| + |
| +namespace remoting { |
| +namespace test { |
| + |
| +using testing::_; |
| + |
| +// Mocks out the file API and returns fake data so we can remove |
| +// file system dependencies in this unit test. |
| +class FakeRefreshTokenStore : public RefreshTokenStoreInterface { |
| + public: |
| + FakeRefreshTokenStore() { |
| + // Set some success defaults. |
| + refresh_token_value = kRefreshTokenValue; |
| + refresh_token_write_attempted = false; |
| + refresh_token_write_succeeded = true; |
| + } |
| + ~FakeRefreshTokenStore() override {} |
| + |
| + std::string ReadRefreshTokenFromDisk(const std::string& user_name) override { |
| + return refresh_token_value; |
| + }; |
| + |
| + bool WriteRefreshTokenToDisk( |
| + const std::string& user_name, |
| + const std::string& refresh_token) override { |
| + // Record the information passed to us to write. |
| + refresh_token_write_attempted = true; |
| + refresh_token_value_written = refresh_token; |
| + user_name_written = user_name; |
| + |
| + return refresh_token_write_succeeded; |
| + }; |
| + |
| + // Control members used to return specific data to the caller. |
| + std::string refresh_token_value; |
| + bool refresh_token_write_succeeded; |
| + |
| + // Verification members to observe the value of the data being written. |
| + bool refresh_token_write_attempted; |
| + std::string refresh_token_value_written; |
| + std::string user_name_written; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FakeRefreshTokenStore); |
| +}; |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCode) { |
| + scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( |
| + new MockAccessTokenFetcher()); |
| + |
| + mock_access_token_fetcher->SetFakeAccessTokenFetcher( |
| + make_scoped_ptr(new FakeAccessTokenFetcher())); |
| + |
| + FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| + .Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| + .Times(0); |
| + |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, |
| + kDevEnvironmentValue); |
| + |
| + environment_object.SetAccessTokenFetcherForTest( |
| + mock_access_token_fetcher.Pass()); |
| + |
| + // We will continue to use the token_store object as it will be alive until |
| + // the environment object is destructed, but we do not need to free it. |
|
Wez
2015/02/19 22:00:24
I don't understand this comment - there's no "toke
joedow
2015/02/20 02:58:36
Done.
|
| + environment_object.SetRefreshTokenStoreForTest( |
| + make_scoped_ptr(fake_token_store)); |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_TRUE(init_result); |
| + EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); |
| + EXPECT_STREQ(kUserNameValue, |
| + fake_token_store->user_name_written.c_str()); |
| + EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, |
| + fake_token_store->refresh_token_value_written.c_str()); |
| + EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| + EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, |
| + environment_object.access_token().c_str()); |
| + |
| + // Attempt to init again, we should not see any additional calls or errors. |
| + init_result = environment_object.Initialize(kAuthCodeValue); |
| + EXPECT_TRUE(init_result); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCodeFailed) { |
| + scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( |
| + new MockAccessTokenFetcher()); |
| + |
| + scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( |
| + new FakeAccessTokenFetcher()); |
| + |
| + fake_access_token_fetcher->SetAuthCodeError(true); |
| + |
| + mock_access_token_fetcher->SetFakeAccessTokenFetcher( |
| + fake_access_token_fetcher.Pass()); |
| + |
| + FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| + .Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| + .Times(0); |
| + |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, |
| + kDevEnvironmentValue); |
| + |
| + environment_object.SetAccessTokenFetcherForTest( |
| + mock_access_token_fetcher.Pass()); |
| + |
| + // We will continue to use the token_store object as it will be alive until |
| + // the environment object is destructed, but we do not need to free it. |
| + environment_object.SetRefreshTokenStoreForTest( |
| + make_scoped_ptr(fake_token_store)); |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_FALSE(init_result); |
| + EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithRefreshToken) { |
| + scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( |
| + new MockAccessTokenFetcher()); |
| + |
| + mock_access_token_fetcher->SetFakeAccessTokenFetcher( |
| + make_scoped_ptr(new FakeAccessTokenFetcher())); |
| + |
| + FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| + .Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| + .Times(0); |
| + |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, |
| + kDevEnvironmentValue); |
| + |
| + environment_object.SetAccessTokenFetcherForTest( |
| + mock_access_token_fetcher.Pass()); |
| + |
| + // We will continue to use the token_store object as it will be alive until |
| + // the environment object is destructed, but we do not need to free it. |
| + environment_object.SetRefreshTokenStoreForTest( |
| + make_scoped_ptr(fake_token_store)); |
| + |
| + // Pass in an empty auth code since we are using a refresh token. |
| + bool init_result = environment_object.Initialize(std::string()); |
| + |
| + EXPECT_TRUE(init_result); |
| + |
| + // We should not write the refresh token a second time if we read from the |
| + // disk originally. |
| + EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); |
| + |
| + // Verify the object was initialized correctly. |
| + EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| + EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, |
| + environment_object.access_token().c_str()); |
| + |
| + // Attempt to init again, we should not see any additional calls or errors. |
| + init_result = environment_object.Initialize(std::string()); |
| + EXPECT_TRUE(init_result); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, |
| + InitializeObjectWithRefreshTokenFailed) { |
| + scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( |
| + new MockAccessTokenFetcher()); |
| + |
| + scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( |
| + new FakeAccessTokenFetcher()); |
| + |
| + fake_access_token_fetcher->SetRefreshTokenError(true); |
| + |
| + mock_access_token_fetcher->SetFakeAccessTokenFetcher( |
| + fake_access_token_fetcher.Pass()); |
| + |
| + FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| + .Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| + .Times(0); |
| + |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, |
| + kDevEnvironmentValue); |
| + |
| + environment_object.SetAccessTokenFetcherForTest( |
| + mock_access_token_fetcher.Pass()); |
| + |
| + // We will continue to use the token_store object as it will be alive until |
| + // the environment object is destructed, but we do not need to free it. |
| + environment_object.SetRefreshTokenStoreForTest( |
| + make_scoped_ptr(fake_token_store)); |
| + |
| + // Pass in an empty auth code since we are using a refresh token. |
| + bool init_result = environment_object.Initialize(std::string()); |
| + |
| + EXPECT_FALSE(init_result); |
| + EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, |
| + InitializeObjectNoAuthCodeOrRefreshToken) { |
| + scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( |
| + new MockAccessTokenFetcher()); |
| + |
| + mock_access_token_fetcher->SetFakeAccessTokenFetcher( |
| + make_scoped_ptr(new FakeAccessTokenFetcher())); |
| + |
| + FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); |
| + |
| + // Neither method should be called in this scenario. |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| + .Times(0); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| + .Times(0); |
| + |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, |
| + kDevEnvironmentValue); |
| + |
| + environment_object.SetAccessTokenFetcherForTest( |
| + mock_access_token_fetcher.Pass()); |
| + |
| + // We will continue to use the token_store object as it will be alive until |
| + // the environment object is destructed, but we do not need to free it. |
| + environment_object.SetRefreshTokenStoreForTest( |
| + make_scoped_ptr(fake_token_store)); |
| + |
| + // Clear out the 'stored' refresh token value. |
| + fake_token_store->refresh_token_value = ""; |
| + |
| + // Pass in an empty auth code. |
| + bool init_result = environment_object.Initialize(std::string()); |
| + |
| + // With no auth code or refresh token, then the initialization should fail. |
| + EXPECT_FALSE(init_result); |
| + EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, |
| + InitializeObjectWithAuthCodeWriteFailed) { |
| + scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( |
| + new MockAccessTokenFetcher()); |
| + |
| + mock_access_token_fetcher->SetFakeAccessTokenFetcher( |
| + make_scoped_ptr(new FakeAccessTokenFetcher())); |
| + |
| + FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| + .Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| + .Times(0); |
| + |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, |
| + kDevEnvironmentValue); |
| + |
| + environment_object.SetAccessTokenFetcherForTest( |
| + mock_access_token_fetcher.Pass()); |
| + |
| + // We will continue to use the token_store object as it will be alive until |
| + // the environment object is destructed, but we do not need to free it. |
| + environment_object.SetRefreshTokenStoreForTest( |
| + make_scoped_ptr(fake_token_store)); |
| + |
| + // Simulate a failure writing the token to the disk. |
| + fake_token_store->refresh_token_write_succeeded = false; |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_FALSE(init_result); |
| + EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, |
| + RefreshAccessTokenAfterUsingAuthCode) { |
| + scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( |
| + new MockAccessTokenFetcher()); |
| + |
| + mock_access_token_fetcher->SetFakeAccessTokenFetcher( |
| + make_scoped_ptr(new FakeAccessTokenFetcher())); |
| + |
| + FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); |
| + |
| + { |
| + testing::Sequence call_sequence; |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| + .Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromRefreshToken(_, _)).Times(1); |
| + } |
| + |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, |
| + kDevEnvironmentValue); |
| + |
| + environment_object.SetAccessTokenFetcherForTest( |
| + mock_access_token_fetcher.Pass()); |
| + |
| + // We will continue to use the token_store object as it will be alive until |
| + // the environment object is destructed, but we do not need to free it. |
| + environment_object.SetRefreshTokenStoreForTest( |
| + make_scoped_ptr(fake_token_store)); |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_TRUE(init_result); |
| + EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); |
| + EXPECT_STREQ(kUserNameValue, |
| + fake_token_store->user_name_written.c_str()); |
| + EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, |
| + fake_token_store->refresh_token_value_written.c_str()); |
| + EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| + EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, |
| + environment_object.access_token().c_str()); |
| + |
| + // Attempt to init again, we should not see any additional calls or errors. |
| + bool refresh_result = environment_object.RefreshAccessToken(); |
| + EXPECT_TRUE(refresh_result); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, RefreshAccessTokenFailure) { |
| + scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( |
| + new MockAccessTokenFetcher()); |
| + |
| + // Use a raw pointer as we want to adjust behavior after we've handed off the |
| + // mock class. |
| + FakeAccessTokenFetcher* fake_access_token_fetcher = |
| + new FakeAccessTokenFetcher(); |
| + mock_access_token_fetcher->SetFakeAccessTokenFetcher( |
| + make_scoped_ptr(fake_access_token_fetcher)); |
| + |
| + FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); |
| + |
| + { |
| + testing::Sequence call_sequence; |
| + |
| + // Mock is set up for this call to succeed. |
| + EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| + .Times(1); |
| + |
| + // Mock is set up for this call to fail. |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromRefreshToken(_, _)).Times(1); |
| + } |
| + |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, |
| + kDevEnvironmentValue); |
| + |
| + environment_object.SetAccessTokenFetcherForTest( |
| + mock_access_token_fetcher.Pass()); |
| + |
| + // We will continue to use the token_store object as it will be alive until |
| + // the environment object is destructed, but we do not need to free it. |
| + environment_object.SetRefreshTokenStoreForTest( |
| + make_scoped_ptr(fake_token_store)); |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_TRUE(init_result); |
| + EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); |
| + EXPECT_STREQ(kUserNameValue, |
| + fake_token_store->user_name_written.c_str()); |
| + EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, |
| + fake_token_store->refresh_token_value_written.c_str()); |
| + EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| + EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, |
| + environment_object.access_token().c_str()); |
| + |
| + fake_access_token_fetcher->SetRefreshTokenError(true); |
| + |
| + bool refresh_result = environment_object.RefreshAccessToken(); |
| + |
| + // We expect the refresh to have failed, the user name to remain valid, |
| + // and the access token to have been cleared. |
| + EXPECT_FALSE(refresh_result); |
| + EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| + EXPECT_STREQ("", environment_object.access_token().c_str()); |
| +} |
| + |
| +} // namespace test |
| +} // namespace remoting |