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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/test/test_suite.h" | 15 #include "base/test/test_suite.h" |
16 #include "base/test/test_switches.h" | 16 #include "base/test/test_switches.h" |
17 #include "google_apis/google_api_keys.h" | 17 #include "google_apis/google_api_keys.h" |
18 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
19 #include "remoting/test/access_token_fetcher.h" | 19 #include "remoting/test/access_token_fetcher.h" |
20 #include "remoting/test/host_info.h" | 20 #include "remoting/test/host_info.h" |
21 #include "remoting/test/host_list_fetcher.h" | 21 #include "remoting/test/host_list_fetcher.h" |
22 #include "remoting/test/refresh_token_store.h" | 22 #include "remoting/test/refresh_token_store.h" |
23 #include "remoting/test/test_chromoting_client.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 namespace switches { | 26 namespace switches { |
26 const char kAuthCodeSwitchName[] = "authcode"; | 27 const char kAuthCodeSwitchName[] = "authcode"; |
28 const char kPinSwitchName[] = "pin"; | |
27 const char kHelpSwitchName[] = "help"; | 29 const char kHelpSwitchName[] = "help"; |
28 const char kHostNameSwitchName[] = "hostname"; | 30 const char kHostNameSwitchName[] = "hostname"; |
29 const char kLoggingLevelSwitchName[] = "verbosity"; | 31 const char kLoggingLevelSwitchName[] = "verbosity"; |
30 const char kRefreshTokenPathSwitchName[] = "refresh-token-path"; | 32 const char kRefreshTokenPathSwitchName[] = "refresh-token-path"; |
31 const char kSingleProcessTestsSwitchName[] = "single-process-tests"; | 33 const char kSingleProcessTestsSwitchName[] = "single-process-tests"; |
32 const char kUserNameSwitchName[] = "username"; | 34 const char kUserNameSwitchName[] = "username"; |
33 } | 35 } |
34 | 36 |
35 namespace { | 37 namespace { |
36 const char kChromotingAuthScopeValues[] = | 38 const char kChromotingAuthScopeValues[] = |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 switches::kUserNameSwitchName); | 71 switches::kUserNameSwitchName); |
70 printf(" %s: Specifies which host to connect to when running tests\n", | 72 printf(" %s: Specifies which host to connect to when running tests\n", |
71 switches::kHostNameSwitchName); | 73 switches::kHostNameSwitchName); |
72 printf("\nOptional Parameters:\n"); | 74 printf("\nOptional Parameters:\n"); |
73 printf(" %s: Exchanged for a refresh and access token for authentication\n", | 75 printf(" %s: Exchanged for a refresh and access token for authentication\n", |
74 switches::kAuthCodeSwitchName); | 76 switches::kAuthCodeSwitchName); |
75 printf(" %s: Displays additional usage information\n", | 77 printf(" %s: Displays additional usage information\n", |
76 switches::kHelpSwitchName); | 78 switches::kHelpSwitchName); |
77 printf(" %s: Path to a JSON file containing username/refresh_token KVPs\n", | 79 printf(" %s: Path to a JSON file containing username/refresh_token KVPs\n", |
78 switches::kRefreshTokenPathSwitchName); | 80 switches::kRefreshTokenPathSwitchName); |
81 printf(" %s: Used to authenticate a chromoting connection with the host\n", | |
82 switches::kPinSwitchName); | |
79 printf(" %s: Specifies the optional logging level of the tool (0-3)." | 83 printf(" %s: Specifies the optional logging level of the tool (0-3)." |
80 " [default: off]\n", | 84 " [default: off]\n", |
81 switches::kLoggingLevelSwitchName); | 85 switches::kLoggingLevelSwitchName); |
82 } | 86 } |
83 | 87 |
84 void PrintAuthCodeInfo() { | 88 void PrintAuthCodeInfo() { |
85 printf("\n*******************************\n"); | 89 printf("\n*******************************\n"); |
86 printf("*** Auth Code Example Usage ***\n"); | 90 printf("*** Auth Code Example Usage ***\n"); |
87 printf("*******************************\n\n"); | 91 printf("*******************************\n\n"); |
88 | 92 |
89 printf("If this is the first time you are running the tool,\n"); | 93 printf("If this is the first time you are running the tool,\n"); |
90 printf("you will need to provide an authorization code.\n"); | 94 printf("you will need to provide an authorization code.\n"); |
91 printf("This code will be exchanged for a long term refresh token which\n"); | 95 printf("This code will be exchanged for a long term refresh token which\n"); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 // Turn on logging for the test_driver and remoting components. | 202 // Turn on logging for the test_driver and remoting components. |
199 // This switch is parsed during logging::InitLogging. | 203 // This switch is parsed during logging::InitLogging. |
200 command_line->AppendSwitchASCII("vmodule", | 204 command_line->AppendSwitchASCII("vmodule", |
201 "*/remoting/*=" + verbosity_level); | 205 "*/remoting/*=" + verbosity_level); |
202 logging::LoggingSettings logging_settings; | 206 logging::LoggingSettings logging_settings; |
203 logging::InitLogging(logging_settings); | 207 logging::InitLogging(logging_settings); |
204 } | 208 } |
205 | 209 |
206 // The username is used to run the tests and determines which refresh token to | 210 // The username is used to run the tests and determines which refresh token to |
207 // select in the refresh token file. | 211 // select in the refresh token file. |
208 std::string username = | 212 const std::string username = |
209 command_line->GetSwitchValueASCII(switches::kUserNameSwitchName); | 213 command_line->GetSwitchValueASCII(switches::kUserNameSwitchName); |
210 | |
211 if (username.empty()) { | 214 if (username.empty()) { |
212 LOG(ERROR) << "No username passed in, can't authenticate or run tests!"; | 215 LOG(ERROR) << "No username passed in, can't authenticate or run tests!"; |
213 return -1; | 216 return -1; |
214 } | 217 } |
215 VLOG(1) << "Running chromoting tests as: " << username; | 218 VLOG(1) << "Running chromoting tests as: " << username; |
216 | 219 |
217 // Check to see if the user passed in a one time use auth_code for | 220 // Check to see if the user passed in a one time use auth_code for |
218 // refreshing their credentials. | 221 // refreshing their credentials. |
219 std::string auth_code = | 222 std::string auth_code = |
220 command_line->GetSwitchValueASCII(switches::kAuthCodeSwitchName); | 223 command_line->GetSwitchValueASCII(switches::kAuthCodeSwitchName); |
221 | 224 |
222 base::FilePath refresh_token_path = | 225 const base::FilePath refresh_token_path = |
223 command_line->GetSwitchValuePath(switches::kRefreshTokenPathSwitchName); | 226 command_line->GetSwitchValuePath(switches::kRefreshTokenPathSwitchName); |
224 | 227 |
225 // The hostname determines which host to initiate a session with from the list | 228 // The hostname determines which host to initiate a session with from the list |
226 // returned from the directory service. | 229 // returned from the directory service. |
227 std::string hostname = | 230 const std::string hostname = |
228 command_line->GetSwitchValueASCII(switches::kHostNameSwitchName); | 231 command_line->GetSwitchValueASCII(switches::kHostNameSwitchName); |
229 | |
230 if (hostname.empty()) { | 232 if (hostname.empty()) { |
231 LOG(ERROR) << "No hostname passed in, connect to host requires hostname!"; | 233 LOG(ERROR) << "No hostname passed in, finding the host requires hostname!"; |
232 return -1; | 234 return -1; |
233 } | 235 } |
234 VLOG(1) << "Chromoting tests will connect to: " << hostname; | 236 VLOG(1) << "Chromoting tests will connect to: " << hostname; |
235 | 237 |
238 const std::string pin = | |
239 command_line->GetSwitchValueASCII(switches::kPinSwitchName); | |
240 | |
236 // TODO(TonyChun): Move this logic into a shared environment class. | 241 // TODO(TonyChun): Move this logic into a shared environment class. |
237 scoped_ptr<remoting::test::RefreshTokenStore> refresh_token_store = | 242 scoped_ptr<remoting::test::RefreshTokenStore> refresh_token_store = |
238 remoting::test::RefreshTokenStore::OnDisk(username, refresh_token_path); | 243 remoting::test::RefreshTokenStore::OnDisk(username, refresh_token_path); |
239 | 244 |
240 std::string refresh_token = refresh_token_store->FetchRefreshToken(); | 245 std::string refresh_token = refresh_token_store->FetchRefreshToken(); |
241 if (auth_code.empty() && refresh_token.empty()) { | 246 if (auth_code.empty() && refresh_token.empty()) { |
242 // RefreshTokenStore already logs which specific error occured. | 247 // RefreshTokenStore already logs which specific error occured. |
243 return -1; | 248 return -1; |
244 } | 249 } |
245 | 250 |
(...skipping 20 matching lines...) Expand all Loading... | |
266 access_token_fetcher.GetAccessTokenFromAuthCode(auth_code, | 271 access_token_fetcher.GetAccessTokenFromAuthCode(auth_code, |
267 access_token_callback); | 272 access_token_callback); |
268 } else { | 273 } else { |
269 DCHECK(!refresh_token.empty()); | 274 DCHECK(!refresh_token.empty()); |
270 access_token_fetcher.GetAccessTokenFromRefreshToken(refresh_token, | 275 access_token_fetcher.GetAccessTokenFromRefreshToken(refresh_token, |
271 access_token_callback); | 276 access_token_callback); |
272 } | 277 } |
273 | 278 |
274 run_loop->Run(); | 279 run_loop->Run(); |
275 | 280 |
276 // RunLoop to handle callback from directory service. | 281 // Reset runloop to use it to retrieve hostlist from directory service. |
277 run_loop.reset(new base::RunLoop()); | 282 run_loop.reset(new base::RunLoop()); |
278 | 283 |
279 std::vector<remoting::test::HostInfo> hostlist; | 284 std::vector<remoting::test::HostInfo> hostlist; |
280 remoting::test::HostListFetcher::HostlistCallback hostlist_request_callback = | 285 remoting::test::HostListFetcher::HostlistCallback hostlist_request_callback = |
281 base::Bind(&OnHostlistRetrieved, run_loop->QuitClosure(), &hostlist); | 286 base::Bind(&OnHostlistRetrieved, run_loop->QuitClosure(), &hostlist); |
282 | 287 |
283 // Uses the access token to get the hostlist from the directory service. | 288 // Uses the access token to get the hostlist from the directory service. |
284 remoting::test::HostListFetcher hostlist_fetcher; | 289 remoting::test::HostListFetcher hostlist_fetcher; |
285 hostlist_fetcher.RetrieveHostlist(access_token, hostlist_request_callback); | 290 hostlist_fetcher.RetrieveHostlist(access_token, hostlist_request_callback); |
286 | 291 |
287 run_loop->Run(); | 292 run_loop->Run(); |
288 | 293 |
294 run_loop.reset(new base::RunLoop()); | |
295 | |
296 scoped_ptr<base::Timer> timer = | |
297 make_scoped_ptr<base::Timer>(new base::Timer(true, false)); | |
298 | |
299 remoting::test::TestChromotingClient test_chromoting_client; | |
300 | |
301 // Find the requested online host info and initiate a session. | |
anandc
2015/07/15 21:12:29
"Check if requested host is online and ready to re
tonychun
2015/07/16 23:12:08
Done.
| |
302 auto it = std::find_if(hostlist.begin(), hostlist.end(), | |
303 [&hostname](const remoting::test::HostInfo& host_info) { | |
304 return host_info.host_name == hostname && | |
305 host_info.IsReadyForConnection(); | |
306 }); | |
307 if (it != hostlist.end()) { | |
308 // Total time to connect to a remote host before aborting the connection. | |
anandc
2015/07/15 21:12:29
"Host is online and ready, initiate a remote sessi
tonychun
2015/07/16 23:12:08
Done.
| |
309 timer->Start(FROM_HERE, base::TimeDelta::FromSeconds(10), | |
anandc
2015/07/15 21:12:29
Make it a constant? Or explain the reason for usin
tonychun
2015/07/16 23:12:08
Done.
| |
310 run_loop->QuitClosure()); | |
311 test_chromoting_client.StartConnection( | |
312 it->GenerateConnectionInfo(access_token, username, pin)); | |
313 run_loop->Run(); | |
314 timer->Stop(); | |
315 test_chromoting_client.EndConnection(); | |
316 } | |
317 | |
289 return 0; | 318 return 0; |
290 } | 319 } |
OLD | NEW |