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