| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/test/chromoting_test_driver_environment.h" | 5 #include "remoting/test/chromoting_test_driver_environment.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 ChromotingTestDriverEnvironment::EnvironmentOptions::EnvironmentOptions() { | 24 ChromotingTestDriverEnvironment::EnvironmentOptions::EnvironmentOptions() { |
| 25 } | 25 } |
| 26 | 26 |
| 27 ChromotingTestDriverEnvironment::EnvironmentOptions::~EnvironmentOptions() { | 27 ChromotingTestDriverEnvironment::EnvironmentOptions::~EnvironmentOptions() { |
| 28 } | 28 } |
| 29 | 29 |
| 30 ChromotingTestDriverEnvironment::ChromotingTestDriverEnvironment( | 30 ChromotingTestDriverEnvironment::ChromotingTestDriverEnvironment( |
| 31 const EnvironmentOptions& options) | 31 const EnvironmentOptions& options) |
| 32 : host_name_(options.host_name), | 32 : host_name_(options.host_name), |
| 33 host_jid_(options.host_jid), |
| 33 user_name_(options.user_name), | 34 user_name_(options.user_name), |
| 34 pin_(options.pin), | 35 pin_(options.pin), |
| 35 refresh_token_file_path_(options.refresh_token_file_path), | 36 refresh_token_file_path_(options.refresh_token_file_path), |
| 36 test_access_token_fetcher_(nullptr), | 37 test_access_token_fetcher_(nullptr), |
| 37 test_refresh_token_store_(nullptr), | 38 test_refresh_token_store_(nullptr), |
| 38 test_host_list_fetcher_(nullptr) { | 39 test_host_list_fetcher_(nullptr) { |
| 39 DCHECK(!user_name_.empty()); | 40 DCHECK(!user_name_.empty()); |
| 40 DCHECK(!host_name_.empty()); | 41 DCHECK(!host_name_.empty()); |
| 41 } | 42 } |
| 42 | 43 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 70 VLOG(2) << "No refresh token stored for " << user_name_; | 71 VLOG(2) << "No refresh token stored for " << user_name_; |
| 71 | 72 |
| 72 if (auth_code.empty()) { | 73 if (auth_code.empty()) { |
| 73 // No token and no Auth code means no service connectivity, bail! | 74 // No token and no Auth code means no service connectivity, bail! |
| 74 LOG(ERROR) << "Cannot retrieve an access token without a stored refresh" | 75 LOG(ERROR) << "Cannot retrieve an access token without a stored refresh" |
| 75 << " token on disk or an auth_code passed into the tool"; | 76 << " token on disk or an auth_code passed into the tool"; |
| 76 return false; | 77 return false; |
| 77 } | 78 } |
| 78 } | 79 } |
| 79 | 80 |
| 80 if (!RetrieveAccessToken(auth_code) || !RetrieveHostList()) { | 81 if (!RetrieveAccessToken(auth_code)) { |
| 81 // If we cannot retrieve an access token or a host list, then nothing is | 82 // If we cannot retrieve an access token, then nothing is going to work. |
| 82 // going to work. We should let the caller know that our object is not ready | 83 // Let the caller know that our object is not ready to be used. |
| 83 // to be used. | |
| 84 return false; | 84 return false; |
| 85 } | 85 } |
| 86 | 86 |
| 87 return true; | 87 return true; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void ChromotingTestDriverEnvironment::DisplayHostList() { | 90 void ChromotingTestDriverEnvironment::DisplayHostList() { |
| 91 const char kHostAvailabilityFormatString[] = "%-45s%-15s%-35s"; | 91 const char kHostAvailabilityFormatString[] = "%-45s%-15s%-35s"; |
| 92 | 92 |
| 93 LOG(INFO) << base::StringPrintf(kHostAvailabilityFormatString, | 93 LOG(INFO) << base::StringPrintf(kHostAvailabilityFormatString, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 108 | 108 |
| 109 LOG(INFO) << base::StringPrintf( | 109 LOG(INFO) << base::StringPrintf( |
| 110 kHostAvailabilityFormatString, host_info.host_name.c_str(), | 110 kHostAvailabilityFormatString, host_info.host_name.c_str(), |
| 111 status.c_str(), host_info.host_jid.c_str()); | 111 status.c_str(), host_info.host_jid.c_str()); |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 | 114 |
| 115 bool ChromotingTestDriverEnvironment::WaitForHostOnline( | 115 bool ChromotingTestDriverEnvironment::WaitForHostOnline( |
| 116 const std::string& host_jid, | 116 const std::string& host_jid, |
| 117 const std::string& host_name) { | 117 const std::string& host_name) { |
| 118 if (host_list_.empty()) { |
| 119 RetrieveHostList(); |
| 120 } |
| 121 |
| 118 // Refresh the |host_list_| periodically to check if expected JID is online. | 122 // Refresh the |host_list_| periodically to check if expected JID is online. |
| 119 const int kMaxIterations = 12; | 123 const base::TimeDelta kTotalTimeInSeconds = base::TimeDelta::FromSeconds(60); |
| 120 const base::TimeDelta kSleepTimeInSeconds = base::TimeDelta::FromSeconds(5); | 124 const base::TimeDelta kSleepTimeInSeconds = base::TimeDelta::FromSeconds(5); |
| 125 const int kMaxIterations = kTotalTimeInSeconds / kSleepTimeInSeconds; |
| 126 |
| 121 int num_iterations = 0; | 127 int num_iterations = 0; |
| 122 while (num_iterations < kMaxIterations) { | 128 while (num_iterations < kMaxIterations) { |
| 123 if (IsHostOnline(host_jid, host_name)) { | 129 if (host_info_.IsReadyForConnection()) { |
| 124 if (num_iterations > 0) { | 130 if (num_iterations > 0) { |
| 125 VLOG(0) << "Host came online after " | 131 VLOG(0) << "Host online after: " |
| 126 << num_iterations * kSleepTimeInSeconds.InSeconds() | 132 << num_iterations * kSleepTimeInSeconds.InSeconds() |
| 127 << " seconds."; | 133 << " seconds."; |
| 128 } | 134 } |
| 129 return true; | 135 return true; |
| 130 } | 136 } |
| 137 |
| 131 // Wait a while before refreshing host list. | 138 // Wait a while before refreshing host list. |
| 132 base::PlatformThread::Sleep(kSleepTimeInSeconds); | 139 base::PlatformThread::Sleep(kSleepTimeInSeconds); |
| 133 RefreshHostList(); | 140 RefreshHostList(); |
| 134 ++num_iterations; | 141 ++num_iterations; |
| 135 } | 142 } |
| 143 |
| 136 LOG(ERROR) << "Host with JID '" << host_jid << "' still not online after " | 144 LOG(ERROR) << "Host with JID '" << host_jid << "' still not online after " |
| 137 << num_iterations * kSleepTimeInSeconds.InSeconds() << " seconds."; | 145 << num_iterations * kSleepTimeInSeconds.InSeconds() << " seconds."; |
| 138 return false; | 146 return false; |
| 139 } | 147 } |
| 140 | 148 |
| 141 bool ChromotingTestDriverEnvironment::IsHostOnline( | |
| 142 const std::string host_jid, | |
| 143 const std::string host_name) const { | |
| 144 for (const HostInfo& host_info : host_list_) { | |
| 145 if (host_jid == host_info.host_jid && host_name == host_info.host_name) { | |
| 146 if (host_info.status == kHostStatusOnline) { | |
| 147 return true; | |
| 148 } else { | |
| 149 LOG(WARNING) << "Host '" << host_name << "' with JID '" << host_jid | |
| 150 << "' not online."; | |
| 151 return false; | |
| 152 } | |
| 153 } | |
| 154 } | |
| 155 return false; | |
| 156 } | |
| 157 | |
| 158 void ChromotingTestDriverEnvironment::SetAccessTokenFetcherForTest( | 149 void ChromotingTestDriverEnvironment::SetAccessTokenFetcherForTest( |
| 159 AccessTokenFetcher* access_token_fetcher) { | 150 AccessTokenFetcher* access_token_fetcher) { |
| 160 DCHECK(access_token_fetcher); | 151 DCHECK(access_token_fetcher); |
| 161 | 152 |
| 162 test_access_token_fetcher_ = access_token_fetcher; | 153 test_access_token_fetcher_ = access_token_fetcher; |
| 163 } | 154 } |
| 164 | 155 |
| 165 void ChromotingTestDriverEnvironment::SetRefreshTokenStoreForTest( | 156 void ChromotingTestDriverEnvironment::SetRefreshTokenStoreForTest( |
| 166 RefreshTokenStore* refresh_token_store) { | 157 RefreshTokenStore* refresh_token_store) { |
| 167 DCHECK(refresh_token_store); | 158 DCHECK(refresh_token_store); |
| 168 | 159 |
| 169 test_refresh_token_store_ = refresh_token_store; | 160 test_refresh_token_store_ = refresh_token_store; |
| 170 } | 161 } |
| 171 | 162 |
| 172 void ChromotingTestDriverEnvironment::SetHostListFetcherForTest( | 163 void ChromotingTestDriverEnvironment::SetHostListFetcherForTest( |
| 173 HostListFetcher* host_list_fetcher) { | 164 HostListFetcher* host_list_fetcher) { |
| 174 DCHECK(host_list_fetcher); | 165 DCHECK(host_list_fetcher); |
| 175 | 166 |
| 176 test_host_list_fetcher_ = host_list_fetcher; | 167 test_host_list_fetcher_ = host_list_fetcher; |
| 177 } | 168 } |
| 178 | 169 |
| 170 void ChromotingTestDriverEnvironment::SetHostNameForTest( |
| 171 const std::string& host_name) { |
| 172 host_name_ = host_name; |
| 173 } |
| 174 |
| 175 void ChromotingTestDriverEnvironment::SetHostJidForTest( |
| 176 const std::string& host_jid) { |
| 177 host_jid_ = host_jid; |
| 178 } |
| 179 |
| 179 void ChromotingTestDriverEnvironment::TearDown() { | 180 void ChromotingTestDriverEnvironment::TearDown() { |
| 180 // Letting the MessageLoop tear down during the test destructor results in | 181 // Letting the MessageLoop tear down during the test destructor results in |
| 181 // errors after test completion, when the MessageLoop dtor touches the | 182 // errors after test completion, when the MessageLoop dtor touches the |
| 182 // registered AtExitManager. The AtExitManager is torn down before the test | 183 // registered AtExitManager. The AtExitManager is torn down before the test |
| 183 // destructor is executed, so we tear down the MessageLoop here, while it is | 184 // destructor is executed, so we tear down the MessageLoop here, while it is |
| 184 // still valid. | 185 // still valid. |
| 185 message_loop_.reset(); | 186 message_loop_.reset(); |
| 186 } | 187 } |
| 187 | 188 |
| 188 bool ChromotingTestDriverEnvironment::RetrieveAccessToken( | 189 bool ChromotingTestDriverEnvironment::RetrieveAccessToken( |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 | 270 |
| 270 bool ChromotingTestDriverEnvironment::RefreshHostList() { | 271 bool ChromotingTestDriverEnvironment::RefreshHostList() { |
| 271 host_list_.clear(); | 272 host_list_.clear(); |
| 272 | 273 |
| 273 return RetrieveHostList(); | 274 return RetrieveHostList(); |
| 274 } | 275 } |
| 275 | 276 |
| 276 bool ChromotingTestDriverEnvironment::RetrieveHostList() { | 277 bool ChromotingTestDriverEnvironment::RetrieveHostList() { |
| 277 base::RunLoop run_loop; | 278 base::RunLoop run_loop; |
| 278 | 279 |
| 280 // Clear the previous host info. |
| 279 host_info_ = HostInfo(); | 281 host_info_ = HostInfo(); |
| 280 | 282 |
| 281 // If a unit test has set |test_host_list_fetcher_| then we should use it | 283 // If a unit test has set |test_host_list_fetcher_| then we should use it |
| 282 // below. Note that we do not want to destroy the test object at the end of | 284 // below. Note that we do not want to destroy the test object at the end of |
| 283 // the function which is why we have the dance below. | 285 // the function which is why we have the dance below. |
| 284 scoped_ptr<HostListFetcher> temporary_host_list_fetcher; | 286 scoped_ptr<HostListFetcher> temporary_host_list_fetcher; |
| 285 HostListFetcher* host_list_fetcher = test_host_list_fetcher_; | 287 HostListFetcher* host_list_fetcher = test_host_list_fetcher_; |
| 286 if (!host_list_fetcher) { | 288 if (!host_list_fetcher) { |
| 287 temporary_host_list_fetcher.reset(new HostListFetcher()); | 289 temporary_host_list_fetcher.reset(new HostListFetcher()); |
| 288 host_list_fetcher = temporary_host_list_fetcher.get(); | 290 host_list_fetcher = temporary_host_list_fetcher.get(); |
| 289 } | 291 } |
| 290 | 292 |
| 291 remoting::test::HostListFetcher::HostlistCallback host_list_callback = | 293 remoting::test::HostListFetcher::HostlistCallback host_list_callback = |
| 292 base::Bind(&ChromotingTestDriverEnvironment::OnHostListRetrieved, | 294 base::Bind(&ChromotingTestDriverEnvironment::OnHostListRetrieved, |
| 293 base::Unretained(this), run_loop.QuitClosure()); | 295 base::Unretained(this), run_loop.QuitClosure()); |
| 294 | 296 |
| 295 host_list_fetcher->RetrieveHostlist(access_token_, host_list_callback); | 297 host_list_fetcher->RetrieveHostlist(access_token_, host_list_callback); |
| 296 | 298 |
| 297 run_loop.Run(); | 299 run_loop.Run(); |
| 298 | 300 |
| 299 if (host_list_.empty()) { | 301 if (host_list_.empty()) { |
| 300 // Note: Access token may have expired, but it is unlikely. | 302 // Note: Access token may have expired, but it is unlikely. |
| 301 LOG(ERROR) << "Retrieved host list is empty.\n" | 303 LOG(ERROR) << "Retrieved host list is empty.\n" |
| 302 << "Does the account have hosts set up?"; | 304 << "Does the account have hosts set up?"; |
| 303 return false; | 305 return false; |
| 304 } | 306 } |
| 305 | 307 |
| 306 // If the host or command line parameters are not setup correctly, we want to | 308 DisplayHostList(); |
| 307 // let the user fix the issue before continuing. | 309 for (HostInfo& host_info : host_list_) { |
| 308 bool found_host_name = false; | 310 // The JID is optional so we consider an empty string to be a '*' match. |
| 309 auto host_info_iter = std::find_if(host_list_.begin(), host_list_.end(), | 311 bool host_jid_match = |
| 310 [this, &found_host_name](const remoting::test::HostInfo& host_info) { | 312 host_jid_.empty() || (host_jid_ == host_info.host_jid); |
| 311 if (host_info.host_name == host_name_) { | 313 bool host_name_match = host_name_ == host_info.host_name; |
| 312 found_host_name = true; | 314 |
| 313 return host_info.IsReadyForConnection(); | 315 if (host_name_match && host_jid_match) { |
| 314 } | 316 host_info_ = host_info; |
| 315 return false; | 317 |
| 316 }); | 318 if (host_info.IsReadyForConnection()) { |
| 317 if (host_info_iter == host_list_.end()) { | 319 return true; |
| 318 if (found_host_name) { | 320 } else { |
| 319 LOG(ERROR) << this->host_name_ << " is not ready to connect."; | 321 LOG(WARNING) << "Host '" << host_name_ << "' with JID '" << host_jid_ |
| 320 } else { | 322 << "' not online."; |
| 321 LOG(ERROR) << this->host_name_ << " was not found in the host list."; | 323 return false; |
| 324 } |
| 322 } | 325 } |
| 323 DisplayHostList(); | |
| 324 return false; | |
| 325 } | 326 } |
| 326 | 327 LOG(WARNING) << "Host '" << host_name_ << "' with JID '" << host_jid_ |
| 327 host_info_ = *host_info_iter; | 328 << "' not found in host list."; |
| 328 | 329 return false; |
| 329 return true; | |
| 330 } | 330 } |
| 331 | 331 |
| 332 void ChromotingTestDriverEnvironment::OnHostListRetrieved( | 332 void ChromotingTestDriverEnvironment::OnHostListRetrieved( |
| 333 base::Closure done_closure, | 333 base::Closure done_closure, |
| 334 const std::vector<HostInfo>& retrieved_host_list) { | 334 const std::vector<HostInfo>& retrieved_host_list) { |
| 335 VLOG(1) << "OnHostListRetrieved() Called"; | 335 VLOG(1) << "OnHostListRetrieved() Called"; |
| 336 | 336 |
| 337 host_list_ = retrieved_host_list; | 337 host_list_ = retrieved_host_list; |
| 338 | 338 |
| 339 done_closure.Run(); | 339 done_closure.Run(); |
| 340 } | 340 } |
| 341 | 341 |
| 342 } // namespace test | 342 } // namespace test |
| 343 } // namespace remoting | 343 } // namespace remoting |
| OLD | NEW |