Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: remoting/test/chromoting_test_driver_environment.cc

Issue 1237883002: Added chromoting test environment for the tests to share data. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added files to BUILD.gn and synced with connect to localhost changes. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "remoting/test/chromoting_test_driver_environment.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/strings/stringprintf.h"
15 #include "remoting/test/access_token_fetcher.h"
16 #include "remoting/test/host_info.h"
17 #include "remoting/test/host_list_fetcher.h"
18 #include "remoting/test/refresh_token_store.h"
19
20 namespace remoting {
21 namespace test {
22
23 ChromotingTestDriverEnvironment::EnvironmentOptions::EnvironmentOptions()
24 : refresh_token_file_path(base::FilePath()) {
Sergey Ulanov 2015/07/23 19:12:43 Don't need this initializer. The default construct
tonychun 2015/07/24 16:16:47 Done.
25 }
26
27 ChromotingTestDriverEnvironment::EnvironmentOptions::~EnvironmentOptions() {
28 }
29
30 ChromotingTestDriverEnvironment::ChromotingTestDriverEnvironment(
31 const EnvironmentOptions& options)
32 : host_name_(options.host_name),
33 user_name_(options.user_name),
34 refresh_token_file_path_(options.refresh_token_file_path),
35 test_access_token_fetcher_(nullptr),
36 test_refresh_token_store_(nullptr),
37 test_host_list_fetcher_(nullptr) {
38 DCHECK(!user_name_.empty());
39 DCHECK(!host_name_.empty());
40 }
41
42 ChromotingTestDriverEnvironment::~ChromotingTestDriverEnvironment() {
43 }
44
45 bool ChromotingTestDriverEnvironment::Initialize(
46 const std::string& auth_code) {
47 if (!access_token_.empty()) {
48 return true;
49 }
50
51 if (!base::MessageLoop::current()) {
52 message_loop_.reset(new base::MessageLoopForIO);
53 }
54
55 // If a unit test has set |test_refresh_token_store_| then we should use it
56 // below. Note that we do not want to destroy the test object.
57 scoped_ptr<RefreshTokenStore> temporary_refresh_token_store;
58 RefreshTokenStore* refresh_token_store = test_refresh_token_store_;
59 if (!refresh_token_store) {
60 temporary_refresh_token_store =
61 RefreshTokenStore::OnDisk(user_name_, refresh_token_file_path_);
62 refresh_token_store = temporary_refresh_token_store.get();
63 }
64
65 // Check to see if we have a refresh token stored for this user.
66 refresh_token_ = refresh_token_store->FetchRefreshToken();
67 if (refresh_token_.empty()) {
68 // This isn't necessarily an error as this might be a first run scenario.
69 VLOG(2) << "No refresh token stored for " << user_name_;
70
71 if (auth_code.empty()) {
72 // No token and no Auth code means no service connectivity, bail!
73 LOG(ERROR) << "Cannot retrieve an access token without a stored refresh"
74 << " token on disk or an auth_code passed into the tool";
75 return false;
76 }
77 }
78
79 if (!RetrieveAccessToken(auth_code) || !RetrieveHostList()) {
80 // If we cannot retrieve an access token or a host list, then nothing is
81 // going to work. We should let the caller know that our object is not ready
82 // to be used.
83 return false;
84 }
85
86 return true;
87 }
88
89 void ChromotingTestDriverEnvironment::DisplayHostList() {
90 const char kHostAvailabilityFormatString[] = "%-45s%-15s%-35s";
91
92 LOG(INFO) << base::StringPrintf(kHostAvailabilityFormatString,
93 "Host Name", "Host Status", "Host JID");
94 LOG(INFO) << base::StringPrintf(kHostAvailabilityFormatString,
95 "---------", "-----------", "--------");
96
97 std::string status;
98 for (const HostInfo& host_info : host_list_) {
99 HostStatus host_status = host_info.status;
100 if (host_status == kHostStatusOnline) {
101 status = "ONLINE";
102 } else if (host_status == kHostStatusOffline) {
103 status = "OFFLINE";
104 } else {
105 status = "UNKNOWN";
106 }
107
108 LOG(INFO) << base::StringPrintf(
109 kHostAvailabilityFormatString, host_info.host_name.c_str(),
110 status.c_str(), host_info.host_jid.c_str());
111 }
112 }
113
114 void ChromotingTestDriverEnvironment::SetAccessTokenFetcherForTest(
115 AccessTokenFetcher* access_token_fetcher) {
116 DCHECK(access_token_fetcher);
117
118 test_access_token_fetcher_ = access_token_fetcher;
119 }
120
121 void ChromotingTestDriverEnvironment::SetRefreshTokenStoreForTest(
122 RefreshTokenStore* refresh_token_store) {
123 DCHECK(refresh_token_store);
124
125 test_refresh_token_store_ = refresh_token_store;
126 }
127
128 void ChromotingTestDriverEnvironment::SetHostListFetcherForTest(
129 HostListFetcher* host_list_fetcher) {
130 DCHECK(host_list_fetcher);
131
132 test_host_list_fetcher_ = host_list_fetcher;
133 }
134
135 void ChromotingTestDriverEnvironment::TearDown() {
136 // Letting the MessageLoop tear down during the test destructor results in
137 // errors after test completion, when the MessageLoop dtor touches the
138 // registered AtExitManager. The AtExitManager is torn down before the test
139 // destructor is executed, so we tear down the MessageLoop here, while it is
140 // still valid.
141 message_loop_.reset();
142 }
143
144 bool ChromotingTestDriverEnvironment::RetrieveAccessToken(
145 const std::string& auth_code) {
146 base::RunLoop run_loop;
147
148 access_token_.clear();
149
150 AccessTokenCallback access_token_callback =
151 base::Bind(&ChromotingTestDriverEnvironment::OnAccessTokenRetrieved,
152 base::Unretained(this), run_loop.QuitClosure());
153
154 // If a unit test has set |test_access_token_fetcher_| then we should use it
155 // below. Note that we do not want to destroy the test object at the end of
156 // the function which is why we have the dance below.
157 scoped_ptr<AccessTokenFetcher> temporary_access_token_fetcher;
158 AccessTokenFetcher* access_token_fetcher = test_access_token_fetcher_;
159 if (!access_token_fetcher) {
160 temporary_access_token_fetcher.reset(new AccessTokenFetcher());
161 access_token_fetcher = temporary_access_token_fetcher.get();
162 }
163
164 if (!auth_code.empty()) {
165 // If the user passed in an authcode, then use it to retrieve an
166 // updated access/refresh token.
167 access_token_fetcher->GetAccessTokenFromAuthCode(auth_code,
168 access_token_callback);
169 } else {
170 DCHECK(!refresh_token_.empty());
171
172 access_token_fetcher->GetAccessTokenFromRefreshToken(refresh_token_,
173 access_token_callback);
174 }
175
176 run_loop.Run();
177
178 // If we were using an auth_code and received a valid refresh token,
179 // then we want to store it locally. If we had an auth code and did not
180 // receive a refresh token, then we should let the user know and exit.
181 if (!auth_code.empty()) {
182 if (!refresh_token_.empty()) {
183 // If a unit test has set |test_refresh_token_store_| then we should use
184 // it below. Note that we do not want to destroy the test object.
185 scoped_ptr<RefreshTokenStore> temporary_refresh_token_store;
186 RefreshTokenStore* refresh_token_store = test_refresh_token_store_;
187 if (!refresh_token_store) {
188 temporary_refresh_token_store =
189 RefreshTokenStore::OnDisk(user_name_, refresh_token_file_path_);
190 refresh_token_store = temporary_refresh_token_store.get();
191 }
192
193 if (!refresh_token_store->StoreRefreshToken(refresh_token_)) {
194 // If we failed to persist the refresh token, then we should let the
195 // user sort out the issue before continuing.
196 return false;
197 }
198 } else {
199 LOG(ERROR) << "Failed to use AUTH CODE to retrieve a refresh token.\n"
200 << "Was the one-time use AUTH CODE used more than once?";
201 return false;
202 }
203 }
204
205 if (access_token_.empty()) {
206 LOG(ERROR) << "Failed to retrieve access token.";
207 return false;
208 }
209
210 return true;
211 }
212
213 void ChromotingTestDriverEnvironment::OnAccessTokenRetrieved(
214 base::Closure done_closure,
215 const std::string& retrieved_access_token,
216 const std::string& retrieved_refresh_token) {
217 VLOG(1) << "OnAccessTokenRetrieved() Called";
218 VLOG(1) << "Access Token: " << retrieved_access_token;
219
220 access_token_ = retrieved_access_token;
221 refresh_token_ = retrieved_refresh_token;
222
223 done_closure.Run();
224 }
225
226 bool ChromotingTestDriverEnvironment::RetrieveHostList() {
227 base::RunLoop run_loop;
228
229 host_list_.clear();
230
231 // If a unit test has set |test_host_list_fetcher_| then we should use it
232 // below. Note that we do not want to destroy the test object at the end of
233 // the function which is why we have the dance below.
234 scoped_ptr<HostListFetcher> temporary_host_list_fetcher;
235 HostListFetcher* host_list_fetcher = test_host_list_fetcher_;
236 if (!host_list_fetcher) {
237 temporary_host_list_fetcher.reset(new HostListFetcher());
238 host_list_fetcher = temporary_host_list_fetcher.get();
239 }
240
241 remoting::test::HostListFetcher::HostlistCallback host_list_callback =
242 base::Bind(&ChromotingTestDriverEnvironment::OnHostListRetrieved,
243 base::Unretained(this), run_loop.QuitClosure());
244
245 host_list_fetcher->RetrieveHostlist(access_token_, host_list_callback);
246
247 run_loop.Run();
248
249 if (host_list_.empty()) {
250 // Note: Access token may have expired, but it is unlikely.
251 LOG(ERROR) << "Retrieved host list is empty.\n"
252 << "Does the account have hosts set up?";
253 return false;
254 }
255
256 return true;
257 }
258
259 void ChromotingTestDriverEnvironment::OnHostListRetrieved(
260 base::Closure done_closure,
261 const std::vector<HostInfo>& retrieved_host_list) {
262 VLOG(1) << "OnHostListRetrieved() Called";
263
264 host_list_ = retrieved_host_list;
265
266 done_closure.Run();
267 }
268
269 } // namespace test
270 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698