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 | 7 |
7 #include "base/bind.h" | 8 #include "base/bind.h" |
8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
14 #include "base/test/test_suite.h" | 15 #include "base/test/test_suite.h" |
15 #include "base/test/test_switches.h" | 16 #include "base/test/test_switches.h" |
16 #include "google_apis/google_api_keys.h" | 17 #include "google_apis/google_api_keys.h" |
17 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
18 #include "remoting/test/access_token_fetcher.h" | 19 #include "remoting/test/access_token_fetcher.h" |
20 #include "remoting/test/host_info.h" | |
21 #include "remoting/test/host_list_fetcher.h" | |
19 #include "remoting/test/refresh_token_store.h" | 22 #include "remoting/test/refresh_token_store.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
21 | 24 |
22 namespace switches { | 25 namespace switches { |
23 const char kAuthCodeSwitchName[] = "authcode"; | 26 const char kAuthCodeSwitchName[] = "authcode"; |
24 const char kHelpSwitchName[] = "help"; | 27 const char kHelpSwitchName[] = "help"; |
25 const char kHostNameSwitchName[] = "hostname"; | 28 const char kHostNameSwitchName[] = "hostname"; |
26 const char kLoggingLevelSwitchName[] = "verbosity"; | 29 const char kLoggingLevelSwitchName[] = "verbosity"; |
27 const char kRefreshTokenPathSwitchName[] = "refresh-token-path"; | 30 const char kRefreshTokenPathSwitchName[] = "refresh-token-path"; |
28 const char kSingleProcessTestsSwitchName[] = "single-process-tests"; | 31 const char kSingleProcessTestsSwitchName[] = "single-process-tests"; |
(...skipping 23 matching lines...) Expand all Loading... | |
52 google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING), | 55 google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING), |
53 use_plus).c_str()); | 56 use_plus).c_str()); |
54 } | 57 } |
55 | 58 |
56 void PrintUsage() { | 59 void PrintUsage() { |
57 printf("\n************************************\n"); | 60 printf("\n************************************\n"); |
58 printf("*** Chromoting Test Driver Usage ***\n"); | 61 printf("*** Chromoting Test Driver Usage ***\n"); |
59 printf("************************************\n"); | 62 printf("************************************\n"); |
60 | 63 |
61 printf("\nUsage:\n"); | 64 printf("\nUsage:\n"); |
62 printf(" chromoting_test_driver --username=<example@gmail.com> [options]\n"); | 65 printf(" chromoting_test_driver --username=<example@gmail.com> [options]" |
66 " --hostname=<example hostname>\n"); | |
63 printf("\nRequired Parameters:\n"); | 67 printf("\nRequired Parameters:\n"); |
64 printf(" %s: Specifies which account to use when running tests\n", | 68 printf(" %s: Specifies which account to use when running tests\n", |
65 switches::kUserNameSwitchName); | 69 switches::kUserNameSwitchName); |
70 printf(" %s: Specifies which host to connect to when running tests\n", | |
71 switches::kHostNameSwitchName); | |
66 printf("\nOptional Parameters:\n"); | 72 printf("\nOptional Parameters:\n"); |
67 printf(" %s: Exchanged for a refresh and access token for authentication\n", | 73 printf(" %s: Exchanged for a refresh and access token for authentication\n", |
68 switches::kAuthCodeSwitchName); | 74 switches::kAuthCodeSwitchName); |
75 printf(" %s: Displays additional usage information\n", | |
76 switches::kHelpSwitchName); | |
69 printf(" %s: Path to a JSON file containing username/refresh_token KVPs\n", | 77 printf(" %s: Path to a JSON file containing username/refresh_token KVPs\n", |
70 switches::kRefreshTokenPathSwitchName); | 78 switches::kRefreshTokenPathSwitchName); |
71 printf(" %s: Displays additional usage information\n", | 79 printf(" %s: Specifies the optional logging level of the tool (0-3)." |
72 switches::kHelpSwitchName); | 80 " [default: off]\n", |
81 switches::kLoggingLevelSwitchName); | |
73 } | 82 } |
74 | 83 |
75 void PrintAuthCodeInfo() { | 84 void PrintAuthCodeInfo() { |
76 printf("\n*******************************\n"); | 85 printf("\n*******************************\n"); |
77 printf("*** Auth Code Example Usage ***\n"); | 86 printf("*** Auth Code Example Usage ***\n"); |
78 printf("*******************************\n\n"); | 87 printf("*******************************\n\n"); |
79 | 88 |
80 printf("If this is the first time you are running the tool,\n"); | 89 printf("If this is the first time you are running the tool,\n"); |
81 printf("you will need to provide an authorization code.\n"); | 90 printf("you will need to provide an authorization code.\n"); |
82 printf("This code will be exchanged for a long term refresh token which\n"); | 91 printf("This code will be exchanged for a long term refresh token which\n"); |
83 printf("will be stored locally and used to acquire a short lived access\n"); | 92 printf("will be stored locally and used to acquire a short lived access\n"); |
84 printf("token to connect to the remoting service apis and establish a\n"); | 93 printf("token to connect to the remoting service apis and establish a\n"); |
85 printf("remote host connection.\n\n"); | 94 printf("remote host connection.\n\n"); |
86 | 95 |
87 printf("Note: You may need to repeat this step if the stored refresh token"); | 96 printf("Note: You may need to repeat this step if the stored refresh token"); |
88 printf("\n has been revoked or expired.\n"); | 97 printf("\n has been revoked or expired.\n"); |
89 printf(" Passing in the same auth code twice will result in an error\n"); | 98 printf(" Passing in the same auth code twice will result in an error\n"); |
90 | 99 |
91 printf( | 100 printf("\nFollow these steps to produce an auth code:\n" |
92 "\nFollow these steps to produce an auth code:\n" | 101 " - Open the Authorization URL link shown below in your browser\n" |
93 " - Open the Authorization URL link shown below in your browser\n" | 102 " - Approve the requested permissions for the tool\n" |
94 " - Approve the requested permissions for the tool\n" | 103 " - Copy the 'code' value in the redirected URL\n" |
95 " - Copy the 'code' value in the redirected URL\n" | 104 " - Run the tool and pass in copied auth code as a parameter\n"); |
96 " - Run the tool and pass in copied auth code as a parameter\n"); | |
97 | 105 |
98 printf("\nAuthorization URL:\n"); | 106 printf("\nAuthorization URL:\n"); |
99 printf("%s\n", GetAuthorizationCodeUri().c_str()); | 107 printf("%s\n", GetAuthorizationCodeUri().c_str()); |
100 | 108 |
101 printf("\nRedirected URL Example:\n"); | 109 printf("\nRedirected URL Example:\n"); |
102 printf( | 110 printf("https://chromoting-oauth.talkgadget.google.com/talkgadget/oauth/" |
103 "https://chromoting-oauth.talkgadget.google.com/talkgadget/oauth/" | 111 "chrome-remote-desktop/dev?code=4/AKtf...\n"); |
104 "chrome-remote-desktop/dev?code=4/AKtf...\n"); | |
105 | 112 |
106 printf("\nTool usage example with the newly created auth code:\n"); | 113 printf("\nTool usage example with the newly created auth code:\n"); |
107 printf("chromoting_test_driver --%s=example@gmail.com --%s=4/AKtf...\n\n", | 114 printf("chromoting_test_driver --%s=example@gmail.com --%s=example_host_name" |
108 switches::kUserNameSwitchName, switches::kAuthCodeSwitchName); | 115 " --%s=4/AKtf...\n\n", |
116 switches::kUserNameSwitchName, | |
117 switches::kHostNameSwitchName, | |
118 switches::kAuthCodeSwitchName); | |
109 } | 119 } |
110 | 120 |
111 void PrintJsonFileInfo() { | 121 void PrintJsonFileInfo() { |
112 printf("\n****************************************\n"); | 122 printf("\n****************************************\n"); |
113 printf("*** Refresh Token File Example Usage ***\n"); | 123 printf("*** Refresh Token File Example Usage ***\n"); |
114 printf("****************************************\n\n"); | 124 printf("****************************************\n\n"); |
115 | 125 |
116 printf("In order to use this option, a valid JSON file must exist, be\n"); | 126 printf("In order to use this option, a valid JSON file must exist, be\n"); |
117 printf("properly formatted, and contain a username/token KVP.\n"); | 127 printf("properly formatted, and contain a username/token KVP.\n"); |
118 printf("Contents of example_file.json\n"); | 128 printf("Contents of example_file.json\n"); |
119 printf("{\n"); | 129 printf("{\n"); |
120 printf(" \"username1@fauxdomain.com\": \"1/3798Gsdf898shksdvfyi8sshad\",\n"); | 130 printf(" \"username1@fauxdomain.com\": \"1/3798Gsdf898shksdvfyi8sshad\",\n"); |
121 printf(" \"username2@fauxdomain.com\": \"1/8974sdf87asdgadfgaerhfRsAa\",\n"); | 131 printf(" \"username2@fauxdomain.com\": \"1/8974sdf87asdgadfgaerhfRsAa\",\n"); |
122 printf("}\n\n"); | 132 printf("}\n\n"); |
123 | 133 |
124 printf("\nTool usage example:\n"); | 134 printf("\nTool usage example:\n"); |
125 printf("chromoting_test_driver --%s=%s --%s=./example_file.json\n\n", | 135 printf("chromoting_test_driver --%s=%s --%s=example_host_name" |
136 " --%s=./example_file.json\n\n", | |
126 switches::kUserNameSwitchName, "username1@fauxdomain.com", | 137 switches::kUserNameSwitchName, "username1@fauxdomain.com", |
127 switches::kRefreshTokenPathSwitchName); | 138 switches::kHostNameSwitchName, switches::kRefreshTokenPathSwitchName); |
128 } | 139 } |
129 | 140 |
130 } // namespace | 141 } // namespace |
131 | 142 |
132 void OnAccessTokenRetrieved( | 143 void OnAccessTokenRetrieved( |
133 base::Closure done_closure, | 144 base::Closure done_closure, |
134 const std::string& access_token, | 145 std::string* access_token, |
135 const std::string& refresh_token) { | 146 const std::string& retrieved_access_token, |
147 const std::string& retrieved_refresh_token) { | |
136 | 148 |
137 DVLOG(1) << "OnAccessTokenRetrieved() Called"; | 149 VLOG(1) << "OnAccessTokenRetrieved() Called"; |
150 DVLOG(1) << "Access Token: " << retrieved_access_token; | |
joedow
2015/07/09 02:41:18
Can you use VLOG consistently? DVLOG won't show u
tonychun
2015/07/09 03:02:46
Done.
| |
138 | 151 |
139 DVLOG(1) << "Access Token: " << access_token; | 152 *access_token = retrieved_access_token; |
140 | 153 |
141 done_closure.Run(); | 154 done_closure.Run(); |
142 } | 155 } |
143 | 156 |
157 void OnHostlistRetrieved( | |
158 base::Closure done_closure, | |
159 std::vector<remoting::test::HostInfo>* hostlist, | |
Sergey Ulanov
2015/07/09 01:25:29
Why do you need to use this parameter. It's not us
tonychun
2015/07/09 03:02:46
I will use it in my next CL, which will connect to
| |
160 const std::vector<remoting::test::HostInfo>& retrieved_hostlist) { | |
161 | |
162 VLOG(1) << "OnHostlistRetrieved() Called"; | |
163 | |
164 DCHECK(hostlist); | |
165 | |
166 *hostlist = retrieved_hostlist; | |
167 | |
168 DVLOG(1) << "There are " << hostlist->size() << " hosts in the hostlist"; | |
169 done_closure.Run(); | |
170 } | |
171 | |
144 int main(int argc, char* argv[]) { | 172 int main(int argc, char* argv[]) { |
145 testing::InitGoogleTest(&argc, argv); | 173 testing::InitGoogleTest(&argc, argv); |
146 base::TestSuite test_suite(argc, argv); | 174 base::TestSuite test_suite(argc, argv); |
147 | 175 |
148 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 176 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
149 DCHECK(command_line); | 177 DCHECK(command_line); |
150 | 178 |
151 // Do not retry if tests fails. | 179 // Do not retry if tests fails. |
152 command_line->AppendSwitchASCII(switches::kTestLauncherRetryLimit, "0"); | 180 command_line->AppendSwitchASCII(switches::kTestLauncherRetryLimit, "0"); |
153 | 181 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 | 215 |
188 // Check to see if the user passed in a one time use auth_code for | 216 // Check to see if the user passed in a one time use auth_code for |
189 // refreshing their credentials. | 217 // refreshing their credentials. |
190 std::string auth_code = | 218 std::string auth_code = |
191 command_line->GetSwitchValueASCII(switches::kAuthCodeSwitchName); | 219 command_line->GetSwitchValueASCII(switches::kAuthCodeSwitchName); |
192 | 220 |
193 base::FilePath refresh_token_path = | 221 base::FilePath refresh_token_path = |
194 command_line->GetSwitchValuePath(switches::kRefreshTokenPathSwitchName); | 222 command_line->GetSwitchValuePath(switches::kRefreshTokenPathSwitchName); |
195 | 223 |
196 // The hostname determines which host to initiate a session with from the list | 224 // The hostname determines which host to initiate a session with from the list |
197 // returned from the Directory service. | 225 // returned from the directory service. |
198 std::string hostname = | 226 std::string hostname = |
199 command_line->GetSwitchValueASCII(switches::kHostNameSwitchName); | 227 command_line->GetSwitchValueASCII(switches::kHostNameSwitchName); |
200 | 228 |
201 if (hostname.empty()) { | 229 if (hostname.empty()) { |
202 LOG(ERROR) << "No hostname passed in, connect to host requires hostname!"; | 230 LOG(ERROR) << "No hostname passed in, connect to host requires hostname!"; |
203 return -1; | 231 return -1; |
204 } | 232 } |
205 DVLOG(1) << "Chromoting tests will connect to: " << hostname; | 233 DVLOG(1) << "Chromoting tests will connect to: " << hostname; |
206 | 234 |
207 // TODO(TonyChun): Move this logic into a shared environment class. | 235 // TODO(TonyChun): Move this logic into a shared environment class. |
208 scoped_ptr<remoting::test::RefreshTokenStore> refresh_token_store = | 236 scoped_ptr<remoting::test::RefreshTokenStore> refresh_token_store = |
209 remoting::test::RefreshTokenStore::OnDisk(username, refresh_token_path); | 237 remoting::test::RefreshTokenStore::OnDisk(username, refresh_token_path); |
210 | 238 |
211 std::string refresh_token = refresh_token_store->FetchRefreshToken(); | 239 std::string refresh_token = refresh_token_store->FetchRefreshToken(); |
212 if (auth_code.empty() && refresh_token.empty()) { | 240 if (auth_code.empty() && refresh_token.empty()) { |
213 // RefreshTokenStore already logs which specific error occured. | 241 // RefreshTokenStore already logs which specific error occured. |
214 return -1; | 242 return -1; |
215 } | 243 } |
216 | 244 |
217 // Used for running network request tasks. | 245 // Used for running network request tasks. |
218 // TODO(TonyChun): Move this logic into a shared environment class. | 246 // TODO(TonyChun): Move this logic into a shared environment class. |
219 base::MessageLoopForIO message_loop; | 247 base::MessageLoopForIO message_loop; |
220 | 248 |
221 // Uses the refresh token to get the access token from GAIA. | 249 // Uses the refresh token to get the access token from GAIA. |
222 remoting::test::AccessTokenFetcher access_token_fetcher; | 250 remoting::test::AccessTokenFetcher access_token_fetcher; |
223 | 251 |
224 base::RunLoop run_loop; | 252 // A RunLoop that yields to the thread's MessageLoop. |
253 scoped_ptr<base::RunLoop> run_loop; | |
225 | 254 |
255 // RunLoop to handle callback from GAIA. | |
256 run_loop.reset(new base::RunLoop()); | |
257 | |
258 std::string access_token; | |
226 remoting::test::AccessTokenCallback access_token_callback = | 259 remoting::test::AccessTokenCallback access_token_callback = |
227 base::Bind(&OnAccessTokenRetrieved, run_loop.QuitClosure()); | 260 base::Bind(&OnAccessTokenRetrieved, |
261 run_loop->QuitClosure(), | |
262 &access_token); | |
228 | 263 |
229 if (!auth_code.empty()) { | 264 if (!auth_code.empty()) { |
230 access_token_fetcher.GetAccessTokenFromAuthCode(auth_code, | 265 access_token_fetcher.GetAccessTokenFromAuthCode(auth_code, |
231 access_token_callback); | 266 access_token_callback); |
232 } else { | 267 } else { |
233 DCHECK(!refresh_token.empty()); | 268 DCHECK(!refresh_token.empty()); |
234 access_token_fetcher.GetAccessTokenFromRefreshToken(refresh_token, | 269 access_token_fetcher.GetAccessTokenFromRefreshToken(refresh_token, |
235 access_token_callback); | 270 access_token_callback); |
236 } | 271 } |
237 | 272 |
238 run_loop.Run(); | 273 run_loop->Run(); |
274 | |
275 // Reset runloop to use it to retrieve hostlist from directory service. | |
276 run_loop.reset(new base::RunLoop()); | |
277 | |
278 std::vector<remoting::test::HostInfo> hostlist; | |
279 remoting::test::HostListFetcher::HostlistCallback hostlist_request_callback = | |
280 base::Bind(&OnHostlistRetrieved, run_loop->QuitClosure(), &hostlist); | |
281 | |
282 // Uses the access token to get the hostlist from the directory service. | |
283 remoting::test::HostListFetcher hostlist_fetcher; | |
284 hostlist_fetcher.RetrieveHostlist(access_token, hostlist_request_callback); | |
285 | |
286 run_loop->Run(); | |
Sergey Ulanov
2015/07/09 01:25:29
I think it would be cleaner to run message loop on
tonychun
2015/07/09 03:02:46
I have to wait for the response from GAIA for the
Sergey Ulanov
2015/07/09 20:15:14
Right. That doesn't mean you need to quit the loop
tonychun
2015/07/09 20:32:34
Done.
| |
239 | 287 |
240 return 0; | 288 return 0; |
241 } | 289 } |
OLD | NEW |