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

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

Powered by Google App Engine
This is Rietveld 408576698