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/gtest/include/gtest/gtest.h"
14 const char kAuthCodeValue
[] = "4/892379827345jkefvkdfbv";
15 const char kRefreshTokenValue
[] = "1/lkjalseLKJlsiJgr45jbv";
16 const char kUserNameValue
[] = "remoting_user@gmail.com";
17 const char kTestApplicationId
[] = "sadlkjlsjgadjfgoajdfgagb";
25 // Stubs out the file API and returns fake data so we can remove
26 // file system dependencies when testing the TestDriverEnvironment.
27 class FakeRefreshTokenStore
: public RefreshTokenStore
{
29 FakeRefreshTokenStore()
30 : refresh_token_value(kRefreshTokenValue
),
31 refresh_token_write_succeeded(true),
32 refresh_token_write_attempted(false) {}
33 ~FakeRefreshTokenStore() override
{}
35 std::string
FetchRefreshToken() override
{ return refresh_token_value
; };
37 bool StoreRefreshToken(const std::string
& refresh_token
) override
{
38 // Record the information passed to us to write.
39 refresh_token_write_attempted
= true;
40 refresh_token_value_written
= refresh_token
;
42 return refresh_token_write_succeeded
;
45 // Control members used to return specific data to the caller.
46 std::string refresh_token_value
;
47 bool refresh_token_write_succeeded
;
49 // Verification members to observe the value of the data being written.
50 bool refresh_token_write_attempted
;
51 std::string refresh_token_value_written
;
54 DISALLOW_COPY_AND_ASSIGN(FakeRefreshTokenStore
);
57 TEST(AppRemotingTestDriverEnvironmentTest
, InitializeObjectWithAuthCode
) {
58 MockAccessTokenFetcher mock_access_token_fetcher
;
60 mock_access_token_fetcher
.SetAccessTokenFetcher(
61 make_scoped_ptr(new FakeAccessTokenFetcher()));
63 FakeRefreshTokenStore fake_token_store
;
65 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromAuthCode(_
, _
))
68 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromRefreshToken(_
, _
))
71 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
72 kDeveloperEnvironment
);
74 environment_object
.SetAccessTokenFetcherForTest(&mock_access_token_fetcher
);
76 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
78 bool init_result
= environment_object
.Initialize(kAuthCodeValue
);
80 EXPECT_TRUE(init_result
);
81 EXPECT_TRUE(fake_token_store
.refresh_token_write_attempted
);
82 EXPECT_EQ(fake_token_store
.refresh_token_value_written
.compare(
83 kFakeAccessTokenFetcherRefreshTokenValue
),
85 EXPECT_EQ(environment_object
.user_name().compare(kUserNameValue
), 0);
86 EXPECT_EQ(environment_object
.access_token().compare(
87 kFakeAccessTokenFetcherAccessTokenValue
),
90 // Attempt to init again, we should not see any additional calls or errors.
91 init_result
= environment_object
.Initialize(kAuthCodeValue
);
92 EXPECT_TRUE(init_result
);
95 TEST(AppRemotingTestDriverEnvironmentTest
, InitializeObjectWithAuthCodeFailed
) {
96 MockAccessTokenFetcher mock_access_token_fetcher
;
98 scoped_ptr
<FakeAccessTokenFetcher
> fake_access_token_fetcher(
99 new FakeAccessTokenFetcher());
101 fake_access_token_fetcher
->set_fail_access_token_from_auth_code(true);
103 mock_access_token_fetcher
.SetAccessTokenFetcher(
104 fake_access_token_fetcher
.Pass());
106 FakeRefreshTokenStore fake_token_store
;
108 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromAuthCode(_
, _
))
111 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromRefreshToken(_
, _
))
114 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
115 kDeveloperEnvironment
);
117 environment_object
.SetAccessTokenFetcherForTest(&mock_access_token_fetcher
);
119 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
121 bool init_result
= environment_object
.Initialize(kAuthCodeValue
);
123 EXPECT_FALSE(init_result
);
124 EXPECT_FALSE(fake_token_store
.refresh_token_write_attempted
);
127 TEST(AppRemotingTestDriverEnvironmentTest
, InitializeObjectWithRefreshToken
) {
128 MockAccessTokenFetcher mock_access_token_fetcher
;
130 mock_access_token_fetcher
.SetAccessTokenFetcher(
131 make_scoped_ptr(new FakeAccessTokenFetcher()));
133 FakeRefreshTokenStore fake_token_store
;
135 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromRefreshToken(_
, _
))
138 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromAuthCode(_
, _
))
141 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
142 kDeveloperEnvironment
);
144 environment_object
.SetAccessTokenFetcherForTest(&mock_access_token_fetcher
);
146 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
148 // Pass in an empty auth code since we are using a refresh token.
149 bool init_result
= environment_object
.Initialize(std::string());
151 EXPECT_TRUE(init_result
);
153 // We should not write the refresh token a second time if we read from the
155 EXPECT_FALSE(fake_token_store
.refresh_token_write_attempted
);
157 // Verify the object was initialized correctly.
158 EXPECT_EQ(environment_object
.user_name().compare(kUserNameValue
), 0);
159 EXPECT_EQ(environment_object
.access_token().compare(
160 kFakeAccessTokenFetcherAccessTokenValue
),
163 // Attempt to init again, we should not see any additional calls or errors.
164 init_result
= environment_object
.Initialize(std::string());
165 EXPECT_TRUE(init_result
);
168 TEST(AppRemotingTestDriverEnvironmentTest
,
169 InitializeObjectWithRefreshTokenFailed
) {
170 MockAccessTokenFetcher mock_access_token_fetcher
;
172 scoped_ptr
<FakeAccessTokenFetcher
> fake_access_token_fetcher(
173 new FakeAccessTokenFetcher());
175 fake_access_token_fetcher
->set_fail_access_token_from_refresh_token(true);
177 mock_access_token_fetcher
.SetAccessTokenFetcher(
178 fake_access_token_fetcher
.Pass());
180 FakeRefreshTokenStore fake_token_store
;
182 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromRefreshToken(_
, _
))
185 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromAuthCode(_
, _
))
188 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
189 kDeveloperEnvironment
);
191 environment_object
.SetAccessTokenFetcherForTest(&mock_access_token_fetcher
);
193 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
195 // Pass in an empty auth code since we are using a refresh token.
196 bool init_result
= environment_object
.Initialize(std::string());
198 EXPECT_FALSE(init_result
);
199 EXPECT_FALSE(fake_token_store
.refresh_token_write_attempted
);
202 TEST(AppRemotingTestDriverEnvironmentTest
,
203 InitializeObjectNoAuthCodeOrRefreshToken
) {
204 MockAccessTokenFetcher mock_access_token_fetcher
;
206 mock_access_token_fetcher
.SetAccessTokenFetcher(
207 make_scoped_ptr(new FakeAccessTokenFetcher()));
209 FakeRefreshTokenStore fake_token_store
;
211 // Neither method should be called in this scenario.
212 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromAuthCode(_
, _
))
215 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromRefreshToken(_
, _
))
218 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
219 kDeveloperEnvironment
);
221 environment_object
.SetAccessTokenFetcherForTest(&mock_access_token_fetcher
);
223 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
225 // Clear out the 'stored' refresh token value.
226 fake_token_store
.refresh_token_value
= "";
228 // Pass in an empty auth code.
229 bool init_result
= environment_object
.Initialize(std::string());
231 // With no auth code or refresh token, then the initialization should fail.
232 EXPECT_FALSE(init_result
);
233 EXPECT_FALSE(fake_token_store
.refresh_token_write_attempted
);
236 TEST(AppRemotingTestDriverEnvironmentTest
,
237 InitializeObjectWithAuthCodeWriteFailed
) {
238 MockAccessTokenFetcher mock_access_token_fetcher
;
240 mock_access_token_fetcher
.SetAccessTokenFetcher(
241 make_scoped_ptr(new FakeAccessTokenFetcher()));
243 FakeRefreshTokenStore fake_token_store
;
245 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromAuthCode(_
, _
))
248 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromRefreshToken(_
, _
))
251 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
252 kDeveloperEnvironment
);
254 environment_object
.SetAccessTokenFetcherForTest(&mock_access_token_fetcher
);
256 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
258 // Simulate a failure writing the token to the disk.
259 fake_token_store
.refresh_token_write_succeeded
= false;
261 bool init_result
= environment_object
.Initialize(kAuthCodeValue
);
263 EXPECT_FALSE(init_result
);
264 EXPECT_TRUE(fake_token_store
.refresh_token_write_attempted
);
267 TEST(AppRemotingTestDriverEnvironmentTest
,
268 RefreshAccessTokenAfterUsingAuthCode
) {
269 MockAccessTokenFetcher mock_access_token_fetcher
;
271 mock_access_token_fetcher
.SetAccessTokenFetcher(
272 make_scoped_ptr(new FakeAccessTokenFetcher()));
274 FakeRefreshTokenStore fake_token_store
;
277 testing::Sequence call_sequence
;
279 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromAuthCode(_
, _
))
282 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromRefreshToken(_
, _
))
286 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
287 kDeveloperEnvironment
);
289 environment_object
.SetAccessTokenFetcherForTest(&mock_access_token_fetcher
);
291 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
293 bool init_result
= environment_object
.Initialize(kAuthCodeValue
);
295 EXPECT_TRUE(init_result
);
296 EXPECT_TRUE(fake_token_store
.refresh_token_write_attempted
);
297 EXPECT_EQ(fake_token_store
.refresh_token_value_written
.compare(
298 kFakeAccessTokenFetcherRefreshTokenValue
),
300 EXPECT_EQ(environment_object
.user_name().compare(kUserNameValue
), 0);
301 EXPECT_EQ(environment_object
.access_token().compare(
302 kFakeAccessTokenFetcherAccessTokenValue
),
305 // Attempt to init again, we should not see any additional calls or errors.
306 bool refresh_result
= environment_object
.RefreshAccessToken();
307 EXPECT_TRUE(refresh_result
);
310 TEST(AppRemotingTestDriverEnvironmentTest
, RefreshAccessTokenFailure
) {
311 MockAccessTokenFetcher mock_access_token_fetcher
;
313 // Use a raw pointer as we want to adjust behavior after we've handed off the
315 FakeAccessTokenFetcher
* fake_access_token_fetcher
=
316 new FakeAccessTokenFetcher();
317 mock_access_token_fetcher
.SetAccessTokenFetcher(
318 make_scoped_ptr(fake_access_token_fetcher
));
320 FakeRefreshTokenStore fake_token_store
;
323 testing::Sequence call_sequence
;
325 // Mock is set up for this call to succeed.
326 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromAuthCode(_
, _
))
329 // Mock is set up for this call to fail.
330 EXPECT_CALL(mock_access_token_fetcher
, GetAccessTokenFromRefreshToken(_
, _
))
334 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
335 kDeveloperEnvironment
);
337 environment_object
.SetAccessTokenFetcherForTest(&mock_access_token_fetcher
);
339 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
341 bool init_result
= environment_object
.Initialize(kAuthCodeValue
);
343 EXPECT_TRUE(init_result
);
344 EXPECT_TRUE(fake_token_store
.refresh_token_write_attempted
);
345 EXPECT_EQ(fake_token_store
.refresh_token_value_written
.compare(
346 kFakeAccessTokenFetcherRefreshTokenValue
),
348 EXPECT_EQ(environment_object
.user_name().compare(kUserNameValue
), 0);
349 EXPECT_EQ(environment_object
.access_token().compare(
350 kFakeAccessTokenFetcherAccessTokenValue
),
353 fake_access_token_fetcher
->set_fail_access_token_from_refresh_token(true);
355 bool refresh_result
= environment_object
.RefreshAccessToken();
357 // We expect the refresh to have failed, the user name to remain valid,
358 // and the access token to have been cleared.
359 EXPECT_FALSE(refresh_result
);
360 EXPECT_TRUE(environment_object
.access_token().empty());
361 EXPECT_EQ(environment_object
.user_name().compare(kUserNameValue
), 0);
364 TEST(AppRemotingTestDriverEnvironmentTest
, GetRemoteHostInfoSuccess
) {
365 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
366 kDeveloperEnvironment
);
368 FakeAccessTokenFetcher fake_access_token_fetcher
;
369 environment_object
.SetAccessTokenFetcherForTest(&fake_access_token_fetcher
);
371 FakeRefreshTokenStore fake_token_store
;
372 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
374 // Pass in an empty auth code since we are using a refresh token.
375 bool init_result
= environment_object
.Initialize(std::string());
377 EXPECT_TRUE(init_result
);
379 FakeRemoteHostInfoFetcher fake_remote_host_info_fetcher
;
380 environment_object
.SetRemoteHostInfoFetcherForTest(
381 &fake_remote_host_info_fetcher
);
383 RemoteHostInfo remote_host_info
;
384 bool request_result
= environment_object
.GetRemoteHostInfoForApplicationId(
385 kTestApplicationId
, &remote_host_info
);
387 EXPECT_TRUE(request_result
);
388 EXPECT_TRUE(remote_host_info
.IsReadyForConnection());
391 TEST(AppRemotingTestDriverEnvironmentTest
, GetRemoteHostInfoFailure
) {
392 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
393 kDeveloperEnvironment
);
395 FakeAccessTokenFetcher fake_access_token_fetcher
;
396 environment_object
.SetAccessTokenFetcherForTest(&fake_access_token_fetcher
);
398 FakeRefreshTokenStore fake_token_store
;
399 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
401 // Pass in an empty auth code since we are using a refresh token.
402 bool init_result
= environment_object
.Initialize(std::string());
404 EXPECT_TRUE(init_result
);
406 FakeRemoteHostInfoFetcher fake_remote_host_info_fetcher
;
407 fake_remote_host_info_fetcher
.set_fail_retrieve_remote_host_info(true);
408 environment_object
.SetRemoteHostInfoFetcherForTest(
409 &fake_remote_host_info_fetcher
);
411 RemoteHostInfo remote_host_info
;
412 bool request_result
= environment_object
.GetRemoteHostInfoForApplicationId(
413 kTestApplicationId
, &remote_host_info
);
415 EXPECT_FALSE(request_result
);
418 TEST(AppRemotingTestDriverEnvironmentTest
,
419 GetRemoteHostInfoWithoutInitializing
) {
420 AppRemotingTestDriverEnvironment
environment_object(kUserNameValue
,
421 kDeveloperEnvironment
);
423 FakeAccessTokenFetcher fake_access_token_fetcher
;
424 environment_object
.SetAccessTokenFetcherForTest(&fake_access_token_fetcher
);
426 FakeRefreshTokenStore fake_token_store
;
427 environment_object
.SetRefreshTokenStoreForTest(&fake_token_store
);
429 FakeRemoteHostInfoFetcher fake_remote_host_info_fetcher
;
430 environment_object
.SetRemoteHostInfoFetcherForTest(
431 &fake_remote_host_info_fetcher
);
433 RemoteHostInfo remote_host_info
;
434 bool request_result
= environment_object
.GetRemoteHostInfoForApplicationId(
435 kTestApplicationId
, &remote_host_info
);
437 EXPECT_FALSE(request_result
);
441 } // namespace remoting