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.
6 #include "base/command_line.h"
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/test/launcher/unit_test_launcher.h"
11 #include "base/test/test_suite.h"
12 #include "base/test/test_switches.h"
13 #include "google_apis/google_api_keys.h"
14 #include "net/base/escape.h"
15 #include "remoting/test/app_remoting_test_driver_environment.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 const char kAuthCodeSwitchName
[] = "authcode";
20 const char kHelpSwitchName
[] = "help";
21 const char kLoggingLevelSwitchName
[] = "verbosity";
22 const char kRefreshTokenFileSwitchName
[] = "refresh-token-file";
23 const char kServiceEnvironmentSwitchName
[] = "environment";
24 const char kShowHostAvailabilitySwitchName
[] = "show-host-availability";
25 const char kSingleProcessTestsSwitchName
[] = "single-process-tests";
26 const char kUserNameSwitchName
[] = "username";
31 // Requested permissions needed for App Remoting tests. The spaces in between
32 // scope fragments are necessary and will be escaped properly before use.
33 const char kAppRemotingAuthScopeValues
[] =
34 "https://www.googleapis.com/auth/appremoting.runapplication"
35 " https://www.googleapis.com/auth/googletalk"
36 " https://www.googleapis.com/auth/userinfo.email"
37 " https://docs.google.com/feeds"
38 " https://www.googleapis.com/auth/drive";
40 std::string
GetAuthorizationCodeUri() {
41 // Replace space characters with a '+' sign when formatting.
43 return base::StringPrintf(
44 "https://accounts.google.com/o/oauth2/auth"
46 "&redirect_uri=https://chromoting-oauth.talkgadget.google.com/"
47 "talkgadget/oauth/chrome-remote-desktop/dev"
50 "&access_type=offline"
51 "&approval_prompt=force",
52 net::EscapeUrlEncodedData(kAppRemotingAuthScopeValues
, use_plus
).c_str(),
53 net::EscapeUrlEncodedData(
54 google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING
),
59 printf("\n**************************************\n");
60 printf("*** App Remoting Test Driver Usage ***\n");
61 printf("**************************************\n");
64 printf(" ar_test_driver --username=<example@gmail.com> [options]\n");
65 printf("\nRequired Parameters:\n");
66 printf(" %s: Specifies which account to use when running tests\n",
67 switches::kUserNameSwitchName
);
68 printf("\nOptional Parameters:\n");
69 printf(" %s: Exchanged for a refresh and access token for authentication\n",
70 switches::kAuthCodeSwitchName
);
71 printf(" %s: Path to a JSON file containing username/refresh_token KVPs\n",
72 switches::kRefreshTokenFileSwitchName
);
73 printf(" %s: Displays additional usage information\n",
74 switches::kHelpSwitchName
);
75 printf(" %s: Specifies the service api to use (dev|test) [default: dev]\n",
76 switches::kServiceEnvironmentSwitchName
);
78 " %s: Retrieves and displays the connection status for all known "
79 "hosts, no tests will be run\n",
80 switches::kShowHostAvailabilitySwitchName
);
82 " %s: Specifies the optional logging level of the tool (0-3)."
84 switches::kLoggingLevelSwitchName
);
87 void PrintAuthCodeInfo() {
88 printf("\n*******************************\n");
89 printf("*** Auth Code Example Usage ***\n");
90 printf("*******************************\n\n");
92 printf("If this is the first time you are running the tool,\n");
93 printf("you will need to provide an authorization code.\n");
94 printf("This code will be exchanged for a long term refresh token which\n");
95 printf("will be stored locally and used to acquire a short lived access\n");
96 printf("token to connect to the remoting service apis and establish a\n");
97 printf("remote host connection.\n\n");
99 printf("Note: You may need to repeat this step if the stored refresh token");
100 printf("\n has been revoked or expired.\n");
101 printf(" Passing in the same auth code twice will result in an error\n");
104 "\nFollow these steps to produce an auth code:\n"
105 " - Open the Authorization URL link shown below in your browser\n"
106 " - Approve the requested permissions for the tool\n"
107 " - Copy the 'code' value in the redirected URL\n"
108 " - Run the tool and pass in copied auth code as a parameter\n");
110 printf("\nAuthorization URL:\n");
111 printf("%s\n", GetAuthorizationCodeUri().c_str());
113 printf("\nRedirected URL Example:\n");
115 "https://chromoting-oauth.talkgadget.google.com/talkgadget/oauth/"
116 "chrome-remote-desktop/dev?code=4/AKtf...\n");
118 printf("\nTool usage example with the newly created auth code:\n");
119 printf("ar_test_driver --%s=example@gmail.com --%s=4/AKtf...\n\n",
120 switches::kUserNameSwitchName
, switches::kAuthCodeSwitchName
);
123 void PrintJsonFileInfo() {
124 printf("\n*****************************************\n");
125 printf("*** Refresh Token File Example Usage ***\n");
126 printf("****************************************\n\n");
128 printf("In order to use this option, a valid JSON file must exist, be\n");
129 printf("properly formatted, and contain a username/token KVP.\n");
130 printf("Contents of example_file.json\n");
132 printf(" \"username1@fauxdomain.com\": \"1/3798Gsdf898shksdvfyi8sshad\",\n");
133 printf(" \"username2@fauxdomain.com\": \"1/8974sdf87asdgadfgaerhfRsAa\",\n");
136 printf("\nTool usage example:\n");
137 printf("ar_test_driver --%s=%s --%s=./example_file.json\n\n",
138 switches::kUserNameSwitchName
, "username1@fauxdomain.com",
139 switches::kRefreshTokenFileSwitchName
);
144 int main(int argc
, char** argv
) {
145 testing::InitGoogleTest(&argc
, argv
);
146 base::TestSuite
test_suite(argc
, argv
);
148 // The pointer returned here refers to a singleton, since we don't own the
149 // lifetime of the object, don't wrap in a scoped_ptr construct or release it.
150 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
151 DCHECK(command_line
);
153 // We do not want to retry failures as a failed test should signify an error
154 // to be investigated.
155 command_line
->AppendSwitchASCII(switches::kTestLauncherRetryLimit
, "0");
157 // We do not want to run the tests in parallel and we do not want to retry
158 // failures. The reason for running in a single process is that some tests
159 // may share the same remoting host and they cannot be run concurrently, also
160 // the test output gets spammed with test launcher messages which reduces the
161 // readability of the results.
162 command_line
->AppendSwitch(switches::kSingleProcessTestsSwitchName
);
164 // If the user passed in the help flag, then show the help info for this tool
165 // and 'run' the tests which will print the gtest specific help and then exit.
166 // NOTE: We do this check after updating the switches as otherwise the gtest
167 // help is written in parallel with our text and can appear interleaved.
168 if (command_line
->HasSwitch(switches::kHelpSwitchName
)) {
172 return base::LaunchUnitTestsSerially(
174 base::Bind(&base::TestSuite::Run
, base::Unretained(&test_suite
)));
177 // Verify we received the required input from the command line.
178 std::string
user_name(
179 command_line
->GetSwitchValueASCII(switches::kUserNameSwitchName
));
180 if (user_name
.empty()) {
181 LOG(ERROR
) << "No user name passed in, can't authenticate without that!";
185 DVLOG(1) << "Running tests as: " << user_name
;
187 // Check to see if the user passed in a one time use auth_code for
188 // refreshing their credentials.
189 std::string
auth_code(
190 command_line
->GetSwitchValueASCII(switches::kAuthCodeSwitchName
));
192 base::FilePath
refresh_token_file_path(
193 command_line
->GetSwitchValuePath(switches::kRefreshTokenFileSwitchName
));
195 // If the user passed in a service environment, use it, otherwise set a
197 remoting::test::ServiceEnvironment service_environment
;
198 std::string
service_environment_switch(command_line
->GetSwitchValueASCII(
199 switches::kServiceEnvironmentSwitchName
));
200 if (service_environment_switch
.empty() ||
201 service_environment_switch
== "dev") {
202 service_environment
=
203 remoting::test::ServiceEnvironment::kDeveloperEnvironment
;
204 } else if (service_environment_switch
== "test") {
205 service_environment
=
206 remoting::test::ServiceEnvironment::kTestingEnvironment
;
207 } else if (service_environment_switch
== "staging") {
208 service_environment
=
209 remoting::test::ServiceEnvironment::kStagingEnvironment
;
211 LOG(ERROR
) << "Invalid " << switches::kServiceEnvironmentSwitchName
212 << " argument passed in.";
217 // Update the logging verbosity level is user specified one.
218 std::string
verbosity_level(
219 command_line
->GetSwitchValueASCII(switches::kLoggingLevelSwitchName
));
220 if (!verbosity_level
.empty()) {
221 // Turn on logging for the test_driver and remoting components.
222 // This switch is parsed during logging::InitLogging.
223 command_line
->AppendSwitchASCII("vmodule",
224 "*/remoting/*=" + verbosity_level
);
225 logging::LoggingSettings logging_settings
;
226 logging::InitLogging(logging_settings
);
229 // Create and register our global test data object. It will handle
230 // retrieving an access token for the user and spinning up VMs.
231 // The GTest framework will own the lifetime of this object once
232 // it is registered below.
233 scoped_ptr
<remoting::test::AppRemotingTestDriverEnvironment
> shared_data(
234 new remoting::test::AppRemotingTestDriverEnvironment(
235 user_name
, refresh_token_file_path
, service_environment
));
237 if (!shared_data
->Initialize(auth_code
)) {
238 // If we failed to initialize our shared data object, then bail.
242 if (command_line
->HasSwitch(switches::kShowHostAvailabilitySwitchName
)) {
243 // When this flag is specified, we will retrieve connection information
244 // for all known applications and report the status. No tests will be run.
245 shared_data
->ShowHostAvailability();
249 // Since we've successfully set up our shared_data object, we'll assign the
250 // value to our global* and transfer ownership to the framework.
251 remoting::test::AppRemotingSharedData
= shared_data
.release();
252 testing::AddGlobalTestEnvironment(remoting::test::AppRemotingSharedData
);
254 // Because many tests may access the same remoting host(s), we need to run
255 // the tests sequentially so they do not interfere with each other.
256 return base::LaunchUnitTestsSerially(
258 base::Bind(&base::TestSuite::Run
, base::Unretained(&test_suite
)));