Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / test / app_remoting_test_driver_environment_unittest.cc
blobcf9a4ccbc3e55689ebefbf10d1129b31f8d897c6
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 <algorithm>
9 #include "base/files/file_path.h"
10 #include "remoting/test/fake_access_token_fetcher.h"
11 #include "remoting/test/fake_app_remoting_report_issue_request.h"
12 #include "remoting/test/fake_refresh_token_store.h"
13 #include "remoting/test/fake_remote_host_info_fetcher.h"
14 #include "remoting/test/mock_access_token_fetcher.h"
15 #include "remoting/test/refresh_token_store.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace {
20 const char kAuthCodeValue[] = "4/892379827345jkefvkdfbv";
21 const char kUserNameValue[] = "remoting_user@gmail.com";
22 const char kTestApplicationId[] = "sadlkjlsjgadjfgoajdfgagb";
23 const char kAnotherTestApplicationId[] = "waklgoisdhfnvjkdsfbljn";
24 const char kTestHostId1[] = "awesome_test_host_id";
25 const char kTestHostId2[] = "super_awesome_test_host_id";
26 const char kTestHostId3[] = "uber_awesome_test_host_id";
29 namespace remoting {
30 namespace test {
32 using testing::_;
34 class AppRemotingTestDriverEnvironmentTest : public ::testing::Test {
35 public:
36 AppRemotingTestDriverEnvironmentTest();
37 ~AppRemotingTestDriverEnvironmentTest() override;
39 FakeAccessTokenFetcher* fake_access_token_fetcher() const {
40 return fake_access_token_fetcher_;
43 protected:
44 void Initialize();
45 void Initialize(
46 const AppRemotingTestDriverEnvironment::EnvironmentOptions& options);
48 FakeAccessTokenFetcher* fake_access_token_fetcher_;
49 FakeAppRemotingReportIssueRequest fake_report_issue_request_;
50 FakeRefreshTokenStore fake_token_store_;
51 FakeRemoteHostInfoFetcher fake_remote_host_info_fetcher_;
52 MockAccessTokenFetcher mock_access_token_fetcher_;
54 scoped_ptr<AppRemotingTestDriverEnvironment> environment_object_;
56 private:
57 DISALLOW_COPY_AND_ASSIGN(AppRemotingTestDriverEnvironmentTest);
60 AppRemotingTestDriverEnvironmentTest::AppRemotingTestDriverEnvironmentTest()
61 : fake_access_token_fetcher_(nullptr) {
64 AppRemotingTestDriverEnvironmentTest::~AppRemotingTestDriverEnvironmentTest() {
67 void AppRemotingTestDriverEnvironmentTest::Initialize() {
68 AppRemotingTestDriverEnvironment::EnvironmentOptions options;
69 options.user_name = kUserNameValue;
70 options.service_environment = kDeveloperEnvironment;
72 Initialize(options);
75 void AppRemotingTestDriverEnvironmentTest::Initialize(
76 const AppRemotingTestDriverEnvironment::EnvironmentOptions& options) {
77 environment_object_.reset(new AppRemotingTestDriverEnvironment(options));
79 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher(
80 new FakeAccessTokenFetcher());
81 fake_access_token_fetcher_ = fake_access_token_fetcher.get();
82 mock_access_token_fetcher_.SetAccessTokenFetcher(
83 fake_access_token_fetcher.Pass());
85 environment_object_->SetAccessTokenFetcherForTest(
86 &mock_access_token_fetcher_);
87 environment_object_->SetAppRemotingReportIssueRequestForTest(
88 &fake_report_issue_request_);
89 environment_object_->SetRefreshTokenStoreForTest(&fake_token_store_);
90 environment_object_->SetRemoteHostInfoFetcherForTest(
91 &fake_remote_host_info_fetcher_);
94 TEST_F(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCode) {
95 Initialize();
97 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromAuthCode(_, _));
99 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromRefreshToken(_, _))
100 .Times(0);
102 EXPECT_TRUE(environment_object_->Initialize(kAuthCodeValue));
103 EXPECT_TRUE(fake_token_store_.refresh_token_write_attempted());
104 EXPECT_EQ(fake_token_store_.stored_refresh_token_value(),
105 kFakeAccessTokenFetcherRefreshTokenValue);
106 EXPECT_EQ(environment_object_->user_name(), kUserNameValue);
107 EXPECT_EQ(environment_object_->access_token(),
108 kFakeAccessTokenFetcherAccessTokenValue);
110 // Attempt to init again, we should not see any additional calls or errors.
111 EXPECT_TRUE(environment_object_->Initialize(kAuthCodeValue));
114 TEST_F(AppRemotingTestDriverEnvironmentTest,
115 InitializeObjectWithAuthCodeFailed) {
116 Initialize();
118 fake_access_token_fetcher()->set_fail_access_token_from_auth_code(true);
120 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromAuthCode(_, _));
122 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromRefreshToken(_, _))
123 .Times(0);
125 EXPECT_FALSE(environment_object_->Initialize(kAuthCodeValue));
126 EXPECT_FALSE(fake_token_store_.refresh_token_write_attempted());
129 TEST_F(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithRefreshToken) {
130 Initialize();
132 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromRefreshToken(_, _));
134 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromAuthCode(_, _))
135 .Times(0);
137 // Pass in an empty auth code since we are using a refresh token.
138 EXPECT_TRUE(environment_object_->Initialize(std::string()));
140 // We should not write the refresh token a second time if we read from the
141 // disk originally.
142 EXPECT_FALSE(fake_token_store_.refresh_token_write_attempted());
144 // Verify the object was initialized correctly.
145 EXPECT_EQ(environment_object_->user_name(), kUserNameValue);
146 EXPECT_EQ(environment_object_->access_token(),
147 kFakeAccessTokenFetcherAccessTokenValue);
149 // Attempt to init again, we should not see any additional calls or errors.
150 EXPECT_TRUE(environment_object_->Initialize(std::string()));
153 TEST_F(AppRemotingTestDriverEnvironmentTest, TearDownAfterInitializeSucceeds) {
154 Initialize();
156 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromRefreshToken(_, _));
158 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromAuthCode(_, _))
159 .Times(0);
161 // Pass in an empty auth code since we are using a refresh token.
162 EXPECT_TRUE(environment_object_->Initialize(std::string()));
164 // Note: We are using a static cast here because the TearDown() method is
165 // private as it is an interface method that we only want to call
166 // directly in tests or by the GTEST framework.
167 static_cast<testing::Environment*>(environment_object_.get())->TearDown();
170 TEST_F(AppRemotingTestDriverEnvironmentTest,
171 InitializeObjectWithRefreshTokenFailed) {
172 Initialize();
174 fake_access_token_fetcher()->set_fail_access_token_from_refresh_token(true);
176 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromRefreshToken(_, _));
178 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromAuthCode(_, _))
179 .Times(0);
181 // Pass in an empty auth code since we are using a refresh token.
182 EXPECT_FALSE(environment_object_->Initialize(std::string()));
183 EXPECT_FALSE(fake_token_store_.refresh_token_write_attempted());
186 TEST_F(AppRemotingTestDriverEnvironmentTest,
187 InitializeObjectNoAuthCodeOrRefreshToken) {
188 Initialize();
190 // Neither method should be called in this scenario.
191 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromAuthCode(_, _))
192 .Times(0);
194 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromRefreshToken(_, _))
195 .Times(0);
197 // Clear out the 'stored' refresh token value.
198 fake_token_store_.set_refresh_token_value(std::string());
200 // With no auth code or refresh token, then the initialization should fail.
201 EXPECT_FALSE(environment_object_->Initialize(std::string()));
202 EXPECT_FALSE(fake_token_store_.refresh_token_write_attempted());
205 TEST_F(AppRemotingTestDriverEnvironmentTest,
206 InitializeObjectWithAuthCodeWriteFailed) {
207 Initialize();
209 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromAuthCode(_, _));
211 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromRefreshToken(_, _))
212 .Times(0);
214 // Simulate a failure writing the token to the disk.
215 fake_token_store_.set_refresh_token_write_succeeded(false);
217 EXPECT_FALSE(environment_object_->Initialize(kAuthCodeValue));
218 EXPECT_TRUE(fake_token_store_.refresh_token_write_attempted());
221 TEST_F(AppRemotingTestDriverEnvironmentTest,
222 RefreshAccessTokenAfterUsingAuthCode) {
223 Initialize();
226 testing::InSequence call_sequence;
228 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromAuthCode(_, _));
230 EXPECT_CALL(mock_access_token_fetcher_,
231 GetAccessTokenFromRefreshToken(_, _));
234 EXPECT_TRUE(environment_object_->Initialize(kAuthCodeValue));
235 EXPECT_TRUE(fake_token_store_.refresh_token_write_attempted());
236 EXPECT_EQ(fake_token_store_.stored_refresh_token_value(),
237 kFakeAccessTokenFetcherRefreshTokenValue);
238 EXPECT_EQ(environment_object_->user_name(), kUserNameValue);
239 EXPECT_EQ(environment_object_->access_token(),
240 kFakeAccessTokenFetcherAccessTokenValue);
242 // Attempt to init again, we should not see any additional calls or errors.
243 EXPECT_TRUE(environment_object_->RefreshAccessToken());
246 TEST_F(AppRemotingTestDriverEnvironmentTest, RefreshAccessTokenFailure) {
247 Initialize();
250 testing::InSequence call_sequence;
252 // Mock is set up for this call to succeed.
253 EXPECT_CALL(mock_access_token_fetcher_, GetAccessTokenFromAuthCode(_, _));
255 // Mock is set up for this call to fail.
256 EXPECT_CALL(mock_access_token_fetcher_,
257 GetAccessTokenFromRefreshToken(_, _));
260 EXPECT_TRUE(environment_object_->Initialize(kAuthCodeValue));
261 EXPECT_TRUE(fake_token_store_.refresh_token_write_attempted());
262 EXPECT_EQ(fake_token_store_.stored_refresh_token_value(),
263 kFakeAccessTokenFetcherRefreshTokenValue);
264 EXPECT_EQ(environment_object_->user_name(), kUserNameValue);
265 EXPECT_EQ(environment_object_->access_token(),
266 kFakeAccessTokenFetcherAccessTokenValue);
268 fake_access_token_fetcher()->set_fail_access_token_from_refresh_token(true);
270 // We expect the refresh to have failed, the user name to remain valid,
271 // and the access token to have been cleared.
272 EXPECT_FALSE(environment_object_->RefreshAccessToken());
273 EXPECT_TRUE(environment_object_->access_token().empty());
274 EXPECT_EQ(environment_object_->user_name(), kUserNameValue);
277 TEST_F(AppRemotingTestDriverEnvironmentTest, GetRemoteHostInfoSuccess) {
278 Initialize();
280 // Pass in an empty auth code since we are using a refresh token.
281 EXPECT_TRUE(environment_object_->Initialize(std::string()));
283 RemoteHostInfo remote_host_info;
284 EXPECT_TRUE(environment_object_->GetRemoteHostInfoForApplicationId(
285 kTestApplicationId, &remote_host_info));
286 EXPECT_TRUE(remote_host_info.IsReadyForConnection());
289 TEST_F(AppRemotingTestDriverEnvironmentTest, GetRemoteHostInfoFailure) {
290 Initialize();
292 // Pass in an empty auth code since we are using a refresh token.
293 EXPECT_TRUE(environment_object_->Initialize(std::string()));
295 fake_remote_host_info_fetcher_.set_fail_retrieve_remote_host_info(true);
297 RemoteHostInfo remote_host_info;
298 EXPECT_FALSE(environment_object_->GetRemoteHostInfoForApplicationId(
299 kTestApplicationId, &remote_host_info));
302 TEST_F(AppRemotingTestDriverEnvironmentTest,
303 GetRemoteHostInfoWithoutInitializing) {
304 Initialize();
306 RemoteHostInfo remote_host_info;
307 EXPECT_FALSE(environment_object_->GetRemoteHostInfoForApplicationId(
308 kTestApplicationId, &remote_host_info));
311 TEST_F(AppRemotingTestDriverEnvironmentTest, NoRemoteHostsReleasedOnTearDown) {
312 // Use the default options as the flag to release the remote hosts is not
313 // enabled by default.
314 Initialize();
316 // Pass in an empty auth code since we are using a refresh token.
317 EXPECT_TRUE(environment_object_->Initialize(std::string()));
319 RemoteHostInfo remote_host_info;
320 EXPECT_TRUE(environment_object_->GetRemoteHostInfoForApplicationId(
321 kTestApplicationId, &remote_host_info));
322 EXPECT_TRUE(remote_host_info.IsReadyForConnection());
324 EXPECT_EQ(fake_report_issue_request_.get_host_ids_released().size(), 0UL);
326 environment_object_->AddHostToReleaseList(kTestApplicationId, kTestHostId1);
328 // Note: We are using a static cast here because the TearDown() method is
329 // private as it is an interface method that we only want to call
330 // directly in tests or by the GTEST framework.
331 static_cast<testing::Environment*>(environment_object_.get())->TearDown();
333 // Verify no hosts were released via a report issue request.
334 EXPECT_EQ(fake_report_issue_request_.get_host_ids_released().size(), 0UL);
337 TEST_F(AppRemotingTestDriverEnvironmentTest, OneRemoteHostReleasedOnTearDown) {
338 AppRemotingTestDriverEnvironment::EnvironmentOptions options;
339 options.user_name = kUserNameValue;
340 options.release_hosts_when_done = true;
341 options.service_environment = kDeveloperEnvironment;
342 Initialize(options);
344 // Pass in an empty auth code since we are using a refresh token.
345 EXPECT_TRUE(environment_object_->Initialize(std::string()));
347 RemoteHostInfo remote_host_info;
348 EXPECT_TRUE(environment_object_->GetRemoteHostInfoForApplicationId(
349 kTestApplicationId, &remote_host_info));
350 EXPECT_TRUE(remote_host_info.IsReadyForConnection());
352 EXPECT_EQ(fake_report_issue_request_.get_host_ids_released().size(), 0UL);
354 environment_object_->AddHostToReleaseList(kTestApplicationId, kTestHostId1);
356 // Note: We are using a static cast here because the TearDown() method is
357 // private as it is an interface method that we only want to call
358 // directly in tests or by the GTEST framework.
359 static_cast<testing::Environment*>(environment_object_.get())->TearDown();
361 std::string expected_host(
362 MakeFormattedStringForReleasedHost(kTestApplicationId, kTestHostId1));
363 std::vector<std::string> actual_host_list =
364 fake_report_issue_request_.get_host_ids_released();
366 EXPECT_EQ(actual_host_list.size(), 1UL);
367 EXPECT_EQ(actual_host_list[0], expected_host);
370 TEST_F(AppRemotingTestDriverEnvironmentTest, RemoteHostsReleasedOnTearDown) {
371 AppRemotingTestDriverEnvironment::EnvironmentOptions options;
372 options.user_name = kUserNameValue;
373 options.release_hosts_when_done = true;
374 options.service_environment = kDeveloperEnvironment;
375 Initialize(options);
377 // Pass in an empty auth code since we are using a refresh token.
378 EXPECT_TRUE(environment_object_->Initialize(std::string()));
380 RemoteHostInfo remote_host_info;
381 EXPECT_TRUE(environment_object_->GetRemoteHostInfoForApplicationId(
382 kTestApplicationId, &remote_host_info));
383 EXPECT_TRUE(remote_host_info.IsReadyForConnection());
385 std::vector<std::string> expected_host_list;
386 environment_object_->AddHostToReleaseList(kTestApplicationId, kTestHostId1);
387 expected_host_list.push_back(
388 MakeFormattedStringForReleasedHost(kTestApplicationId, kTestHostId1));
390 environment_object_->AddHostToReleaseList(kAnotherTestApplicationId,
391 kTestHostId2);
392 expected_host_list.push_back(MakeFormattedStringForReleasedHost(
393 kAnotherTestApplicationId, kTestHostId2));
395 environment_object_->AddHostToReleaseList(kTestApplicationId, kTestHostId3);
396 expected_host_list.push_back(
397 MakeFormattedStringForReleasedHost(kTestApplicationId, kTestHostId3));
399 // Note: We are using a static cast here because the TearDown() method is
400 // private as it is an interface method that we only want to call
401 // directly in tests or by the GTEST framework.
402 static_cast<testing::Environment*>(environment_object_.get())->TearDown();
404 std::vector<std::string> actual_host_list =
405 fake_report_issue_request_.get_host_ids_released();
407 std::sort(actual_host_list.begin(), actual_host_list.end());
408 std::sort(expected_host_list.begin(), expected_host_list.end());
410 EXPECT_EQ(actual_host_list.size(), expected_host_list.size());
411 for (size_t i = 0; i < actual_host_list.size(); ++i) {
412 EXPECT_EQ(actual_host_list[i], expected_host_list[i]);
416 TEST_F(AppRemotingTestDriverEnvironmentTest, RemoteHostsReleasedOnce) {
417 AppRemotingTestDriverEnvironment::EnvironmentOptions options;
418 options.user_name = kUserNameValue;
419 options.release_hosts_when_done = true;
420 options.service_environment = kDeveloperEnvironment;
421 Initialize(options);
423 // Pass in an empty auth code since we are using a refresh token.
424 EXPECT_TRUE(environment_object_->Initialize(std::string()));
426 RemoteHostInfo remote_host_info;
427 EXPECT_TRUE(environment_object_->GetRemoteHostInfoForApplicationId(
428 kTestApplicationId, &remote_host_info));
429 EXPECT_TRUE(remote_host_info.IsReadyForConnection());
431 std::vector<std::string> expected_host_list;
432 environment_object_->AddHostToReleaseList(kTestApplicationId, kTestHostId1);
433 expected_host_list.push_back(
434 MakeFormattedStringForReleasedHost(kTestApplicationId, kTestHostId1));
436 environment_object_->AddHostToReleaseList(kAnotherTestApplicationId,
437 kTestHostId2);
438 expected_host_list.push_back(MakeFormattedStringForReleasedHost(
439 kAnotherTestApplicationId, kTestHostId2));
441 environment_object_->AddHostToReleaseList(kTestApplicationId, kTestHostId3);
442 expected_host_list.push_back(
443 MakeFormattedStringForReleasedHost(kTestApplicationId, kTestHostId3));
445 // Attempt to add the previous hosts again, they should not be added since
446 // they will already exist in the list of hosts to release.
447 environment_object_->AddHostToReleaseList(kTestApplicationId, kTestHostId1);
448 environment_object_->AddHostToReleaseList(kAnotherTestApplicationId,
449 kTestHostId2);
450 environment_object_->AddHostToReleaseList(kTestApplicationId, kTestHostId3);
452 // Note: We are using a static cast here because the TearDown() method is
453 // private as it is an interface method that we only want to call
454 // directly in tests or by the GTEST framework.
455 static_cast<testing::Environment*>(environment_object_.get())->TearDown();
457 std::vector<std::string> actual_host_list =
458 fake_report_issue_request_.get_host_ids_released();
460 std::sort(actual_host_list.begin(), actual_host_list.end());
461 std::sort(expected_host_list.begin(), expected_host_list.end());
463 EXPECT_EQ(actual_host_list.size(), expected_host_list.size());
464 for (size_t i = 0; i < actual_host_list.size(); ++i) {
465 EXPECT_EQ(actual_host_list[i], expected_host_list[i]);
469 } // namespace test
470 } // namespace remoting