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/chromoting_test_driver_environment.h"
7 #include "base/files/file_path.h"
8 #include "remoting/test/fake_access_token_fetcher.h"
9 #include "remoting/test/fake_host_list_fetcher.h"
10 #include "remoting/test/fake_refresh_token_store.h"
11 #include "testing/gtest/include/gtest/gtest.h"
14 const char kAuthCodeValue
[] = "4/892379827345jkefvkdfbv";
15 const char kUserNameValue
[] = "remoting_user@gmail.com";
16 const char kHostNameValue
[] = "fake_host_name";
17 const char kFakeHostNameValue
[] = "fake_host_name";
18 const char kFakeHostIdValue
[] = "fake_host_id";
19 const char kFakeHostJidValue
[] = "fake_host_jid";
20 const char kFakeHostOfflineReasonValue
[] = "fake_offline_reason";
21 const char kFakeHostPublicKeyValue
[] = "fake_public_key";
22 const char kFakeHostFirstTokenUrlValue
[] = "token_url_1";
23 const char kFakeHostSecondTokenUrlValue
[] = "token_url_2";
24 const char kFakeHostThirdTokenUrlValue
[] = "token_url_3";
25 const unsigned int kExpectedHostListSize
= 1;
31 class ChromotingTestDriverEnvironmentTest
: public ::testing::Test
{
33 ChromotingTestDriverEnvironmentTest();
34 ~ChromotingTestDriverEnvironmentTest() override
;
37 // testing::Test interface.
38 void SetUp() override
;
40 FakeAccessTokenFetcher fake_access_token_fetcher_
;
41 FakeRefreshTokenStore fake_token_store_
;
42 FakeHostListFetcher fake_host_list_fetcher_
;
44 scoped_ptr
<ChromotingTestDriverEnvironment
> environment_object_
;
47 DISALLOW_COPY_AND_ASSIGN(ChromotingTestDriverEnvironmentTest
);
50 ChromotingTestDriverEnvironmentTest::ChromotingTestDriverEnvironmentTest() {
53 ChromotingTestDriverEnvironmentTest::~ChromotingTestDriverEnvironmentTest() {
56 void ChromotingTestDriverEnvironmentTest::SetUp() {
57 ChromotingTestDriverEnvironment::EnvironmentOptions options
;
58 options
.user_name
= kUserNameValue
;
59 options
.host_name
= kHostNameValue
;
61 environment_object_
.reset(new ChromotingTestDriverEnvironment(options
));
63 std::vector
<HostInfo
> fake_host_list
;
65 fake_host
.host_id
= kFakeHostIdValue
;
66 fake_host
.host_jid
= kFakeHostJidValue
;
67 fake_host
.host_name
= kFakeHostNameValue
;
68 fake_host
.offline_reason
= kFakeHostOfflineReasonValue
;
69 fake_host
.public_key
= kFakeHostPublicKeyValue
;
70 fake_host
.status
= kHostStatusOnline
;
71 fake_host
.token_url_patterns
.push_back(kFakeHostFirstTokenUrlValue
);
72 fake_host
.token_url_patterns
.push_back(kFakeHostSecondTokenUrlValue
);
73 fake_host
.token_url_patterns
.push_back(kFakeHostThirdTokenUrlValue
);
74 fake_host_list
.push_back(fake_host
);
76 fake_host_list_fetcher_
.set_retrieved_host_list(fake_host_list
);
78 environment_object_
->SetAccessTokenFetcherForTest(
79 &fake_access_token_fetcher_
);
80 environment_object_
->SetRefreshTokenStoreForTest(&fake_token_store_
);
81 environment_object_
->SetHostListFetcherForTest(&fake_host_list_fetcher_
);
84 TEST_F(ChromotingTestDriverEnvironmentTest
, InitializeObjectWithAuthCode
) {
85 // Pass in an auth code to initialize the environment.
86 EXPECT_TRUE(environment_object_
->Initialize(kAuthCodeValue
));
88 EXPECT_TRUE(fake_token_store_
.refresh_token_write_attempted());
89 EXPECT_EQ(fake_token_store_
.stored_refresh_token_value(),
90 kFakeAccessTokenFetcherRefreshTokenValue
);
92 EXPECT_EQ(environment_object_
->user_name(), kUserNameValue
);
93 EXPECT_EQ(environment_object_
->host_name(), kHostNameValue
);
94 EXPECT_EQ(environment_object_
->access_token(),
95 kFakeAccessTokenFetcherAccessTokenValue
);
97 // Should only have one host in the list.
98 EXPECT_EQ(environment_object_
->host_list().size(), kExpectedHostListSize
);
99 HostInfo fake_host
= environment_object_
->host_list().at(0);
100 EXPECT_EQ(fake_host
.host_id
, kFakeHostIdValue
);
101 EXPECT_EQ(fake_host
.host_jid
, kFakeHostJidValue
);
102 EXPECT_EQ(fake_host
.host_name
, kFakeHostNameValue
);
103 EXPECT_EQ(fake_host
.public_key
, kFakeHostPublicKeyValue
);
104 EXPECT_EQ(fake_host
.token_url_patterns
.at(0), kFakeHostFirstTokenUrlValue
);
105 EXPECT_EQ(fake_host
.token_url_patterns
.at(1), kFakeHostSecondTokenUrlValue
);
106 EXPECT_EQ(fake_host
.token_url_patterns
.at(2), kFakeHostThirdTokenUrlValue
);
109 TEST_F(ChromotingTestDriverEnvironmentTest
,
110 InitializeObjectWithAuthCodeFailed
) {
111 fake_access_token_fetcher_
.set_fail_access_token_from_auth_code(true);
113 EXPECT_FALSE(environment_object_
->Initialize(kAuthCodeValue
));
114 EXPECT_FALSE(fake_token_store_
.refresh_token_write_attempted());
117 TEST_F(ChromotingTestDriverEnvironmentTest
, InitializeObjectWithRefreshToken
) {
118 // Pass in an empty auth code since we are using a refresh token.
119 EXPECT_TRUE(environment_object_
->Initialize(std::string()));
121 // We should not write the refresh token a second time if we read from the
123 EXPECT_FALSE(fake_token_store_
.refresh_token_write_attempted());
125 // Verify the object was initialized correctly.
126 EXPECT_EQ(environment_object_
->user_name(), kUserNameValue
);
127 EXPECT_EQ(environment_object_
->host_name(), kHostNameValue
);
128 EXPECT_EQ(environment_object_
->access_token(),
129 kFakeAccessTokenFetcherAccessTokenValue
);
131 // Should only have one host in the list.
132 EXPECT_EQ(environment_object_
->host_list().size(), kExpectedHostListSize
);
133 HostInfo fake_host
= environment_object_
->host_list().at(0);
134 EXPECT_EQ(fake_host
.host_id
, kFakeHostIdValue
);
135 EXPECT_EQ(fake_host
.host_jid
, kFakeHostJidValue
);
136 EXPECT_EQ(fake_host
.host_name
, kFakeHostNameValue
);
137 EXPECT_EQ(fake_host
.public_key
, kFakeHostPublicKeyValue
);
138 EXPECT_EQ(fake_host
.token_url_patterns
.at(0), kFakeHostFirstTokenUrlValue
);
139 EXPECT_EQ(fake_host
.token_url_patterns
.at(1), kFakeHostSecondTokenUrlValue
);
140 EXPECT_EQ(fake_host
.token_url_patterns
.at(2), kFakeHostThirdTokenUrlValue
);
143 TEST_F(ChromotingTestDriverEnvironmentTest
,
144 InitializeObjectWithRefreshTokenFailed
) {
145 fake_access_token_fetcher_
.set_fail_access_token_from_refresh_token(true);
147 // Pass in an empty auth code since we are using a refresh token.
148 EXPECT_FALSE(environment_object_
->Initialize(std::string()));
149 EXPECT_FALSE(fake_token_store_
.refresh_token_write_attempted());
152 TEST_F(ChromotingTestDriverEnvironmentTest
, TearDownAfterInitializeSucceeds
) {
153 // Pass in an empty auth code since we are using a refresh token.
154 EXPECT_TRUE(environment_object_
->Initialize(std::string()));
156 // Note: We are using a static cast here because the TearDown() method is
157 // private as it is an interface method that we only want to call
158 // directly in tests or by the GTEST framework.
159 static_cast<testing::Environment
*>(environment_object_
.get())->TearDown();
162 TEST_F(ChromotingTestDriverEnvironmentTest
,
163 InitializeObjectNoAuthCodeOrRefreshToken
) {
164 // Clear out the 'stored' refresh token value.
165 fake_token_store_
.set_refresh_token_value(std::string());
167 // With no auth code or refresh token, then the initialization should fail.
168 EXPECT_FALSE(environment_object_
->Initialize(std::string()));
169 EXPECT_FALSE(fake_token_store_
.refresh_token_write_attempted());
172 TEST_F(ChromotingTestDriverEnvironmentTest
,
173 InitializeObjectWithAuthCodeWriteFailed
) {
174 // Simulate a failure writing the token to the disk.
175 fake_token_store_
.set_refresh_token_write_succeeded(false);
177 EXPECT_FALSE(environment_object_
->Initialize(kAuthCodeValue
));
178 EXPECT_TRUE(fake_token_store_
.refresh_token_write_attempted());
181 TEST_F(ChromotingTestDriverEnvironmentTest
, HostListEmptyFromDirectory
) {
182 // Set the host list fetcher to return an empty list.
183 fake_host_list_fetcher_
.set_retrieved_host_list(std::vector
<HostInfo
>());
185 EXPECT_FALSE(environment_object_
->Initialize(kAuthCodeValue
));
186 EXPECT_TRUE(fake_token_store_
.refresh_token_write_attempted());
190 } // namespace remoting