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

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

Powered by Google App Engine
This is Rietveld 408576698