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

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: Clarified comments, removed unused headers, and fixed unit tests. 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 message_loop_.reset();
joedow 2015/07/16 21:32:58 The comment was still useful (I think it should be
tonychun 2015/07/16 23:07:43 I see. Got it
140 }
141
142 bool ChromotingTestDriverEnvironment::RetrieveAccessToken(
143 const std::string& auth_code) {
144 base::RunLoop run_loop;
145
146 access_token_.clear();
147
148 AccessTokenCallback access_token_callback =
149 base::Bind(&ChromotingTestDriverEnvironment::OnAccessTokenRetrieved,
150 base::Unretained(this), run_loop.QuitClosure());
151
152 // If a unit test has set |test_access_token_fetcher_| then we should use it
153 // below. Note that we do not want to destroy the test object at the end of
154 // the function which is why we have the dance below.
155 scoped_ptr<AccessTokenFetcher> temporary_access_token_fetcher;
156 AccessTokenFetcher* access_token_fetcher = test_access_token_fetcher_;
157 if (!access_token_fetcher) {
158 temporary_access_token_fetcher.reset(new AccessTokenFetcher());
159 access_token_fetcher = temporary_access_token_fetcher.get();
160 }
161
162 if (!auth_code.empty()) {
163 // If the user passed in an authcode, then use it to retrieve an
164 // updated access/refresh token.
165 access_token_fetcher->GetAccessTokenFromAuthCode(auth_code,
166 access_token_callback);
167 } else {
168 DCHECK(!refresh_token_.empty());
169
170 access_token_fetcher->GetAccessTokenFromRefreshToken(refresh_token_,
171 access_token_callback);
172 }
173
174 run_loop.Run();
175
176 // If we were using an auth_code and received a valid refresh token,
177 // then we want to store it locally. If we had an auth code and did not
178 // receive a refresh token, then we should let the user know and exit.
179 if (!auth_code.empty()) {
180 if (!refresh_token_.empty()) {
181 // If a unit test has set |test_refresh_token_store_| then we should use
182 // it below. Note that we do not want to destroy the test object.
183 scoped_ptr<RefreshTokenStore> temporary_refresh_token_store;
184 RefreshTokenStore* refresh_token_store = test_refresh_token_store_;
185 if (!refresh_token_store) {
186 temporary_refresh_token_store =
187 RefreshTokenStore::OnDisk(user_name_, refresh_token_file_path_);
188 refresh_token_store = temporary_refresh_token_store.get();
189 }
190
191 if (!refresh_token_store->StoreRefreshToken(refresh_token_)) {
192 // If we failed to persist the refresh token, then we should let the
193 // user sort out the issue before continuing.
194 return false;
195 }
196 } else {
197 LOG(ERROR) << "Failed to use AUTH CODE to retrieve a refresh token.\n"
198 << "Was the one-time use AUTH CODE used more than once?";
199 return false;
200 }
201 }
202
203 if (access_token_.empty()) {
204 LOG(ERROR) << "Failed to retrieve access token.";
205 return false;
206 }
207
208 return true;
209 }
210
211 void ChromotingTestDriverEnvironment::OnAccessTokenRetrieved(
212 base::Closure done_closure,
213 const std::string& retrieved_access_token,
214 const std::string& retrieved_refresh_token) {
215 VLOG(1) << "OnAccessTokenRetrieved() Called";
216 VLOG(1) << "Access Token: " << retrieved_access_token;
217
218 access_token_ = retrieved_access_token;
219 refresh_token_ = retrieved_refresh_token;
220
221 done_closure.Run();
222 }
223
224 bool ChromotingTestDriverEnvironment::RetrieveHostList() {
225 base::RunLoop run_loop;
226
227 host_list_.clear();
228
229 // If a unit test has set |test_host_list_fetcher_| then we should use it
230 // below. Note that we do not want to destroy the test object at the end of
231 // the function which is why we have the dance below.
232 scoped_ptr<HostListFetcher> temporary_host_list_fetcher;
233 HostListFetcher* host_list_fetcher = test_host_list_fetcher_;
234 if (!host_list_fetcher) {
235 temporary_host_list_fetcher.reset(new HostListFetcher());
236 host_list_fetcher = temporary_host_list_fetcher.get();
237 }
238
239 remoting::test::HostListFetcher::HostlistCallback host_list_callback =
240 base::Bind(&ChromotingTestDriverEnvironment::OnHostListRetrieved,
241 base::Unretained(this), run_loop.QuitClosure());
242
243 host_list_fetcher->RetrieveHostlist(access_token_, host_list_callback);
244
245 run_loop.Run();
246
247 if (host_list_.empty()) {
248 // Note: Access token may have expired, but it is unlikely.
249 LOG(ERROR) << "Retrieved host list is empty.\n"
250 << "Does the account have hosts set up?";
251 return false;
252 }
253
254 return true;
255 }
256
257 void ChromotingTestDriverEnvironment::OnHostListRetrieved(
258 base::Closure done_closure,
259 const std::vector<HostInfo>& retrieved_host_list) {
260 VLOG(1) << "OnHostListRetrieved() Called";
261
262 host_list_ = retrieved_host_list;
263
264 done_closure.Run();
265 }
266
267 } // namespace test
268 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698