1 // Copyright 2015 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.
5 #include "remoting/test/app_remoting_test_driver_environment.h"
7 #include "remoting/test/fake_access_token_fetcher.h"
8 #include "remoting/test/fake_remote_host_info_fetcher.h"
9 #include "remoting/test/mock_access_token_fetcher.h"
10 #include "remoting/test/refresh_token_store.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 const char kAuthCodeValue
[] = "4/892379827345jkefvkdfbv";
16 const char kRefreshTokenValue
[] = "1/lkjalseLKJlsiJgr45jbv";
17 const char kUserNameValue
[] = "remoting_user@gmail.com";
18 const char kTestApplicationId
[] = "sadlkjlsjgadjfgoajdfgagb";
26 // Stubs out the file API and returns fake data so we can remove
27 // file system dependencies when testing the TestDriverEnvironment.
28 class FakeRefreshTokenStore
: public RefreshTokenStore
{
30 FakeRefreshTokenStore()
31 : refresh_token_value_(kRefreshTokenValue
),
32 refresh_token_write_succeeded_(true),
33 refresh_token_write_attempted_(false) {}
34 ~FakeRefreshTokenStore() override
{}
36 std::string
FetchRefreshToken() override
{ return refresh_token_value_
; }
38 bool StoreRefreshToken(const std::string
& refresh_token
) override
{
39 // Record the information passed to us to write.
40 refresh_token_write_attempted_
= true;
41 stored_refresh_token_value_
= refresh_token
;
43 return refresh_token_write_succeeded_
;
46 bool refresh_token_write_attempted() const {
47 return refresh_token_write_attempted_
;
50 const std::string
& stored_refresh_token_value() const {
51 return stored_refresh_token_value_
;
54 void set_refresh_token_value(const std::string
& new_token_value
) {
55 refresh_token_value_
= new_token_value
;
58 void set_refresh_token_write_succeeded(bool write_succeeded
) {
59 refresh_token_write_succeeded_
= write_succeeded
;
63 // Control members used to return specific data to the caller.
64 std::string refresh_token_value_
;
65 bool refresh_token_write_succeeded_
;
67 // Verification members to observe the value of the data being written.
68 bool refresh_token_write_attempted_
;
69 std::string stored_refresh_token_value_
;
71 DISALLOW_COPY_AND_ASSIGN(FakeRefreshTokenStore
);
74 class AppRemotingTestDriverEnvironmentTest
: public ::testing::Test
{
76 AppRemotingTestDriverEnvironmentTest()
77 : fake_access_token_fetcher_(nullptr),
78 environment_object_(kUserNameValue
, kDeveloperEnvironment
) {}
79 ~AppRemotingTestDriverEnvironmentTest() override
{}
81 FakeAccessTokenFetcher
* fake_access_token_fetcher() const {
82 return fake_access_token_fetcher_
;
86 // testing::Test interface.
87 void SetUp() override
{
88 scoped_ptr
<FakeAccessTokenFetcher
> fake_access_token_fetcher(
89 new FakeAccessTokenFetcher());
90 fake_access_token_fetcher_
= fake_access_token_fetcher
.get();
91 mock_access_token_fetcher_
.SetAccessTokenFetcher(
92 fake_access_token_fetcher
.Pass());
94 environment_object_
.SetAccessTokenFetcherForTest(
95 &mock_access_token_fetcher_
);
96 environment_object_
.SetRefreshTokenStoreForTest(&fake_token_store_
);
97 environment_object_
.SetRemoteHostInfoFetcherForTest(
98 &fake_remote_host_info_fetcher_
);
101 FakeRefreshTokenStore fake_token_store_
;
102 FakeRemoteHostInfoFetcher fake_remote_host_info_fetcher_
;
103 FakeAccessTokenFetcher
* fake_access_token_fetcher_
;
104 MockAccessTokenFetcher mock_access_token_fetcher_
;
106 AppRemotingTestDriverEnvironment environment_object_
;
109 DISALLOW_COPY_AND_ASSIGN(AppRemotingTestDriverEnvironmentTest
);
112 TEST_F(AppRemotingTestDriverEnvironmentTest
, InitializeObjectWithAuthCode
) {
113 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromAuthCode(_
, _
));
115 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromRefreshToken(_
, _
))
118 EXPECT_TRUE(environment_object_
.Initialize(kAuthCodeValue
));
119 EXPECT_TRUE(fake_token_store_
.refresh_token_write_attempted());
120 EXPECT_EQ(fake_token_store_
.stored_refresh_token_value(),
121 kFakeAccessTokenFetcherRefreshTokenValue
);
122 EXPECT_EQ(environment_object_
.user_name(), kUserNameValue
);
123 EXPECT_EQ(environment_object_
.access_token(),
124 kFakeAccessTokenFetcherAccessTokenValue
);
126 // Attempt to init again, we should not see any additional calls or errors.
127 EXPECT_TRUE(environment_object_
.Initialize(kAuthCodeValue
));
130 TEST_F(AppRemotingTestDriverEnvironmentTest
,
131 InitializeObjectWithAuthCodeFailed
) {
132 fake_access_token_fetcher()->set_fail_access_token_from_auth_code(true);
134 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromAuthCode(_
, _
));
136 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromRefreshToken(_
, _
))
139 EXPECT_FALSE(environment_object_
.Initialize(kAuthCodeValue
));
140 EXPECT_FALSE(fake_token_store_
.refresh_token_write_attempted());
143 TEST_F(AppRemotingTestDriverEnvironmentTest
, InitializeObjectWithRefreshToken
) {
144 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromRefreshToken(_
, _
));
146 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromAuthCode(_
, _
))
149 // Pass in an empty auth code since we are using a refresh token.
150 EXPECT_TRUE(environment_object_
.Initialize(std::string()));
152 // We should not write the refresh token a second time if we read from the
154 EXPECT_FALSE(fake_token_store_
.refresh_token_write_attempted());
156 // Verify the object was initialized correctly.
157 EXPECT_EQ(environment_object_
.user_name(), kUserNameValue
);
158 EXPECT_EQ(environment_object_
.access_token(),
159 kFakeAccessTokenFetcherAccessTokenValue
);
161 // Attempt to init again, we should not see any additional calls or errors.
162 EXPECT_TRUE(environment_object_
.Initialize(std::string()));
165 TEST_F(AppRemotingTestDriverEnvironmentTest
,
166 InitializeObjectWithRefreshTokenFailed
) {
167 fake_access_token_fetcher()->set_fail_access_token_from_refresh_token(true);
169 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromRefreshToken(_
, _
));
171 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromAuthCode(_
, _
))
174 // Pass in an empty auth code since we are using a refresh token.
175 EXPECT_FALSE(environment_object_
.Initialize(std::string()));
176 EXPECT_FALSE(fake_token_store_
.refresh_token_write_attempted());
179 TEST_F(AppRemotingTestDriverEnvironmentTest
,
180 InitializeObjectNoAuthCodeOrRefreshToken
) {
181 // Neither method should be called in this scenario.
182 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromAuthCode(_
, _
))
185 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromRefreshToken(_
, _
))
188 // Clear out the 'stored' refresh token value.
189 fake_token_store_
.set_refresh_token_value(std::string());
191 // With no auth code or refresh token, then the initialization should fail.
192 EXPECT_FALSE(environment_object_
.Initialize(std::string()));
193 EXPECT_FALSE(fake_token_store_
.refresh_token_write_attempted());
196 TEST_F(AppRemotingTestDriverEnvironmentTest
,
197 InitializeObjectWithAuthCodeWriteFailed
) {
198 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromAuthCode(_
, _
));
200 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromRefreshToken(_
, _
))
203 // Simulate a failure writing the token to the disk.
204 fake_token_store_
.set_refresh_token_write_succeeded(false);
206 EXPECT_FALSE(environment_object_
.Initialize(kAuthCodeValue
));
207 EXPECT_TRUE(fake_token_store_
.refresh_token_write_attempted());
210 TEST_F(AppRemotingTestDriverEnvironmentTest
,
211 RefreshAccessTokenAfterUsingAuthCode
) {
213 testing::InSequence call_sequence
;
215 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromAuthCode(_
, _
));
217 EXPECT_CALL(mock_access_token_fetcher_
,
218 GetAccessTokenFromRefreshToken(_
, _
));
221 EXPECT_TRUE(environment_object_
.Initialize(kAuthCodeValue
));
222 EXPECT_TRUE(fake_token_store_
.refresh_token_write_attempted());
223 EXPECT_EQ(fake_token_store_
.stored_refresh_token_value(),
224 kFakeAccessTokenFetcherRefreshTokenValue
);
225 EXPECT_EQ(environment_object_
.user_name(), kUserNameValue
);
226 EXPECT_EQ(environment_object_
.access_token(),
227 kFakeAccessTokenFetcherAccessTokenValue
);
229 // Attempt to init again, we should not see any additional calls or errors.
230 EXPECT_TRUE(environment_object_
.RefreshAccessToken());
233 TEST_F(AppRemotingTestDriverEnvironmentTest
, RefreshAccessTokenFailure
) {
235 testing::InSequence call_sequence
;
237 // Mock is set up for this call to succeed.
238 EXPECT_CALL(mock_access_token_fetcher_
, GetAccessTokenFromAuthCode(_
, _
));
240 // Mock is set up for this call to fail.
241 EXPECT_CALL(mock_access_token_fetcher_
,
242 GetAccessTokenFromRefreshToken(_
, _
));
245 EXPECT_TRUE(environment_object_
.Initialize(kAuthCodeValue
));
246 EXPECT_TRUE(fake_token_store_
.refresh_token_write_attempted());
247 EXPECT_EQ(fake_token_store_
.stored_refresh_token_value(),
248 kFakeAccessTokenFetcherRefreshTokenValue
);
249 EXPECT_EQ(environment_object_
.user_name(), kUserNameValue
);
250 EXPECT_EQ(environment_object_
.access_token(),
251 kFakeAccessTokenFetcherAccessTokenValue
);
253 fake_access_token_fetcher()->set_fail_access_token_from_refresh_token(true);
255 // We expect the refresh to have failed, the user name to remain valid,
256 // and the access token to have been cleared.
257 EXPECT_FALSE(environment_object_
.RefreshAccessToken());
258 EXPECT_TRUE(environment_object_
.access_token().empty());
259 EXPECT_EQ(environment_object_
.user_name(), kUserNameValue
);
262 TEST_F(AppRemotingTestDriverEnvironmentTest
, GetRemoteHostInfoSuccess
) {
263 // Pass in an empty auth code since we are using a refresh token.
264 EXPECT_TRUE(environment_object_
.Initialize(std::string()));
266 RemoteHostInfo remote_host_info
;
267 EXPECT_TRUE(environment_object_
.GetRemoteHostInfoForApplicationId(
268 kTestApplicationId
, &remote_host_info
));
269 EXPECT_TRUE(remote_host_info
.IsReadyForConnection());
272 TEST_F(AppRemotingTestDriverEnvironmentTest
, GetRemoteHostInfoFailure
) {
273 // Pass in an empty auth code since we are using a refresh token.
274 EXPECT_TRUE(environment_object_
.Initialize(std::string()));
276 fake_remote_host_info_fetcher_
.set_fail_retrieve_remote_host_info(true);
278 RemoteHostInfo remote_host_info
;
279 EXPECT_FALSE(environment_object_
.GetRemoteHostInfoForApplicationId(
280 kTestApplicationId
, &remote_host_info
));
283 TEST_F(AppRemotingTestDriverEnvironmentTest
,
284 GetRemoteHostInfoWithoutInitializing
) {
285 RemoteHostInfo remote_host_info
;
286 EXPECT_FALSE(environment_object_
.GetRemoteHostInfoForApplicationId(
287 kTestApplicationId
, &remote_host_info
));
291 } // namespace remoting