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