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..ee761d86ef58f0c3433941d42a9d53b2c15a5a1a |
| --- /dev/null |
| +++ b/remoting/test/app_remoting_test_driver_environment_unittest.cc |
| @@ -0,0 +1,407 @@ |
| +// 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/mock_access_token_fetcher.h" |
| +#include "remoting/test/refresh_token_storage.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::_; |
| + |
| +// This class mocks out the file API and returns fake data so we can remove |
| +// file system dependencies in this unit test. |
| +class FakeRefreshTokenStorage : public RefreshTokenStorageInterface { |
| + public: |
| + FakeRefreshTokenStorage() { |
| + // Set some success defaults. |
| + refresh_token_value = kRefreshTokenValue; |
| + refresh_token_write_attempted = false; |
| + refresh_token_write_succeeded = true; |
| + } |
| + ~FakeRefreshTokenStorage() 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; |
| +}; |
|
Wez
2015/02/13 03:01:54
DISALLOW_COPY_AND_ASSIGN
joedow
2015/02/14 02:31:29
Done.
|
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCode) { |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, kDevEnvironmentValue); |
| + |
| + MockAccessTokenFetcher* mock_access_token_fetcher = |
| + new MockAccessTokenFetcher(); |
| + |
| + mock_access_token_fetcher->DelegateToCompleteCallbacks(); |
| + |
| + FakeRefreshTokenStorage* fake_refresh_token_storage = |
| + new FakeRefreshTokenStorage(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromAuthCode(_, _)). |
| + Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromRefreshToken(_, _)). |
| + Times(0); |
| + |
| + // Passing the pointers transfers ownership to the environment class. We |
| + // will continue to use the objects as they will be alive until the |
| + // environment object is destructed, but we do not need to free them. |
| + environment_object.SetAccessTokenFetcherForTesting( |
| + mock_access_token_fetcher); |
| + environment_object.SetRefreshTokenStorageForTesting( |
| + fake_refresh_token_storage); |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_TRUE(init_result); |
| + EXPECT_TRUE(fake_refresh_token_storage->refresh_token_write_attempted); |
| + EXPECT_STREQ(kUserNameValue, |
| + fake_refresh_token_storage->user_name_written.c_str()); |
| + EXPECT_STREQ(kMockAccessTokenFetcherRefreshTokenValue, |
| + fake_refresh_token_storage->refresh_token_value_written.c_str()); |
| + EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| + EXPECT_STREQ(kMockAccessTokenFetcherAccessTokenValue, |
| + 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) { |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, kDevEnvironmentValue); |
| + |
| + MockAccessTokenFetcher* mock_access_token_fetcher = |
| + new MockAccessTokenFetcher(); |
| + |
| + mock_access_token_fetcher->DelegateToFailureCallbacks(); |
| + |
| + FakeRefreshTokenStorage* fake_refresh_token_storage = |
| + new FakeRefreshTokenStorage(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromAuthCode(_, _)). |
| + Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromRefreshToken(_, _)). |
| + Times(0); |
| + |
| + // Passing the pointers transfers ownership to the environment class. We |
| + // will continue to use the objects as they will be alive until the |
| + // environment object is destructed, but we do not need to free them. |
| + environment_object.SetAccessTokenFetcherForTesting( |
| + mock_access_token_fetcher); |
| + environment_object.SetRefreshTokenStorageForTesting( |
| + fake_refresh_token_storage); |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_FALSE(init_result); |
| + EXPECT_FALSE(fake_refresh_token_storage->refresh_token_write_attempted); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithRefreshToken) { |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, kDevEnvironmentValue); |
| + |
| + MockAccessTokenFetcher* mock_access_token_fetcher = |
| + new MockAccessTokenFetcher(); |
| + |
| + mock_access_token_fetcher->DelegateToCompleteCallbacks(); |
| + |
| + FakeRefreshTokenStorage* fake_refresh_token_storage = |
| + new FakeRefreshTokenStorage(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromRefreshToken(_, _)). |
| + Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromAuthCode(_, _)). |
| + Times(0); |
| + |
| + // Passing the pointers transfers ownership to the environment class. We |
| + // will continue to use the objects as they will be alive until the |
| + // environment object is destructed, but we do not need to free them. |
| + environment_object.SetAccessTokenFetcherForTesting( |
| + mock_access_token_fetcher); |
| + environment_object.SetRefreshTokenStorageForTesting( |
| + fake_refresh_token_storage); |
| + |
| + // Pass in an empty auth code since we are using a refresh token. |
| + std::string auth_code; |
| + bool init_result = environment_object.Initialize(auth_code); |
| + |
| + EXPECT_TRUE(init_result); |
| + |
| + // We should not write the refresh token a second time if we read from the |
| + // disk originally. |
| + EXPECT_FALSE(fake_refresh_token_storage->refresh_token_write_attempted); |
| + |
| + // Verify the object was initialized correctly. |
| + EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| + EXPECT_STREQ(kMockAccessTokenFetcherAccessTokenValue, |
| + environment_object.access_token().c_str()); |
| + |
| + // Attempt to init again, we should not see any additional calls or errors. |
| + init_result = environment_object.Initialize(auth_code); |
| + EXPECT_TRUE(init_result); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, |
| + InitializeObjectWithRefreshTokenFailed) { |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, kDevEnvironmentValue); |
| + |
| + MockAccessTokenFetcher* mock_access_token_fetcher = |
| + new MockAccessTokenFetcher(); |
| + |
| + mock_access_token_fetcher->DelegateToFailureCallbacks(); |
| + |
| + FakeRefreshTokenStorage* fake_refresh_token_storage = |
| + new FakeRefreshTokenStorage(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromRefreshToken(_, _)). |
| + Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromAuthCode(_, _)). |
| + Times(0); |
| + |
| + // Passing the pointers transfers ownership to the environment class. We |
| + // will continue to use the objects as they will be alive until the |
| + // environment object is destructed, but we do not need to free them. |
| + environment_object.SetAccessTokenFetcherForTesting( |
| + mock_access_token_fetcher); |
| + environment_object.SetRefreshTokenStorageForTesting( |
| + fake_refresh_token_storage); |
| + |
| + // Pass in an empty auth code since we are using a refresh token. |
| + std::string auth_code; |
| + bool init_result = environment_object.Initialize(auth_code); |
| + |
| + EXPECT_FALSE(init_result); |
| + EXPECT_FALSE(fake_refresh_token_storage->refresh_token_write_attempted); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, |
| + InitializeObjectNoAuthCodeOrRefreshToken) { |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, kDevEnvironmentValue); |
| + |
| + MockAccessTokenFetcher* mock_access_token_fetcher = |
| + new MockAccessTokenFetcher(); |
| + |
| + mock_access_token_fetcher->DelegateToCompleteCallbacks(); |
| + |
| + FakeRefreshTokenStorage* fake_refresh_token_storage = |
| + new FakeRefreshTokenStorage(); |
| + |
| + // 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); |
| + |
| + // Passing the pointers transfers ownership to the environment class. We |
| + // will continue to use the objects as they will be alive until the |
| + // environment object is destructed, but we do not need to free them. |
| + environment_object.SetAccessTokenFetcherForTesting( |
| + mock_access_token_fetcher); |
| + environment_object.SetRefreshTokenStorageForTesting( |
| + fake_refresh_token_storage); |
| + |
| + // Clear out the 'stored' refresh token value. |
| + fake_refresh_token_storage->refresh_token_value = ""; |
| + |
| + // Pass in an empty auth code. |
| + std::string auth_code; |
| + bool init_result = environment_object.Initialize(auth_code); |
| + |
| + // With no auth code or refresh token, then the initialization should fail. |
| + EXPECT_FALSE(init_result); |
| + EXPECT_FALSE(fake_refresh_token_storage->refresh_token_write_attempted); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, |
| + InitializeObjectWithAuthCodeWriteFailed) { |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, kDevEnvironmentValue); |
| + |
| + MockAccessTokenFetcher* mock_access_token_fetcher = |
| + new MockAccessTokenFetcher(); |
| + |
| + mock_access_token_fetcher->DelegateToCompleteCallbacks(); |
| + |
| + FakeRefreshTokenStorage* fake_refresh_token_storage = |
| + new FakeRefreshTokenStorage(); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromAuthCode(_, _)). |
| + Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromRefreshToken(_, _)). |
| + Times(0); |
| + |
| + // Passing the pointers transfers ownership to the environment class. We |
| + // will continue to use the objects as they will be alive until the |
| + // environment object is destructed, but we do not need to free them. |
| + environment_object.SetAccessTokenFetcherForTesting( |
| + mock_access_token_fetcher); |
| + environment_object.SetRefreshTokenStorageForTesting( |
| + fake_refresh_token_storage); |
| + |
| + // Simulate a failure writing the token to the disk. |
| + fake_refresh_token_storage->refresh_token_write_succeeded = false; |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_FALSE(init_result); |
| + EXPECT_TRUE(fake_refresh_token_storage->refresh_token_write_attempted); |
| +} |
| + |
| +TEST(AppRemotingTestDriverEnvironmentTest, |
| + RefreshAccessTokenAfterUsingAuthCode) { |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, kDevEnvironmentValue); |
| + |
| + MockAccessTokenFetcher* mock_access_token_fetcher = |
| + new MockAccessTokenFetcher(); |
| + |
| + mock_access_token_fetcher->DelegateToCompleteCallbacks(); |
| + |
| + FakeRefreshTokenStorage* fake_refresh_token_storage = |
| + new FakeRefreshTokenStorage(); |
| + |
| + { |
| + testing::Sequence call_sequence; |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromAuthCode(_, _)). |
| + Times(1); |
| + |
| + EXPECT_CALL(*mock_access_token_fetcher, |
| + GetAccessTokenFromRefreshToken(_, _)). |
| + Times(1); |
| + } |
| + |
| + // Passing the pointers transfers ownership to the environment class. We |
| + // will continue to use the objects as they will be alive until the |
| + // environment object is destructed, but we do not need to free them. |
| + environment_object.SetAccessTokenFetcherForTesting( |
| + mock_access_token_fetcher); |
| + environment_object.SetRefreshTokenStorageForTesting( |
| + fake_refresh_token_storage); |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_TRUE(init_result); |
| + EXPECT_TRUE(fake_refresh_token_storage->refresh_token_write_attempted); |
| + EXPECT_STREQ(kUserNameValue, |
| + fake_refresh_token_storage->user_name_written.c_str()); |
| + EXPECT_STREQ(kMockAccessTokenFetcherRefreshTokenValue, |
| + fake_refresh_token_storage->refresh_token_value_written.c_str()); |
| + EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| + EXPECT_STREQ(kMockAccessTokenFetcherAccessTokenValue, |
| + 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) { |
| + AppRemotingTestDriverEnvironment environment_object( |
| + kUserNameValue, kDevEnvironmentValue); |
| + |
| + MockAccessTokenFetcher* mock_access_token_fetcher = |
| + new MockAccessTokenFetcher(); |
| + |
| + mock_access_token_fetcher->DelegateToRefreshFailureCallback(); |
| + |
| + FakeRefreshTokenStorage* fake_refresh_token_storage = |
| + new FakeRefreshTokenStorage(); |
| + |
| + { |
| + 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); |
| + } |
| + |
| + // Passing the pointers transfers ownership to the environment class. We |
| + // will continue to use the objects as they will be alive until the |
| + // environment object is destructed, but we do not need to free them. |
| + environment_object.SetAccessTokenFetcherForTesting( |
| + mock_access_token_fetcher); |
| + environment_object.SetRefreshTokenStorageForTesting( |
| + fake_refresh_token_storage); |
| + |
| + bool init_result = environment_object.Initialize(kAuthCodeValue); |
| + |
| + EXPECT_TRUE(init_result); |
| + EXPECT_TRUE(fake_refresh_token_storage->refresh_token_write_attempted); |
| + EXPECT_STREQ(kUserNameValue, |
| + fake_refresh_token_storage->user_name_written.c_str()); |
| + EXPECT_STREQ(kMockAccessTokenFetcherRefreshTokenValue, |
| + fake_refresh_token_storage->refresh_token_value_written.c_str()); |
| + EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| + EXPECT_STREQ(kMockAccessTokenFetcherAccessTokenValue, |
| + 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_FALSE(refresh_result); |
| +} |
| + |
| +} // namespace test |
| +} // namespace remoting |