Chromium Code Reviews| 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 "base/bind.h" | |
| 6 #include "base/command_line.h" | |
| 7 #include "base/logging.h" | |
| 8 #include "base/strings/stringprintf.h" | |
| 9 #include "base/test/launcher/unit_test_launcher.h" | |
| 10 #include "base/test/test_suite.h" | |
| 11 #include "base/test/test_switches.h" | |
| 12 #include "google_apis/google_api_keys.h" | |
| 13 #include "net/base/escape.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace switches { | |
| 17 const char kUserNameSwitchName[] = "username"; | |
| 18 const char kAuthCodeSwitchName[] = "authcode"; | |
| 19 const char kServiceEnvironmentSwitchName[] = "environment"; | |
| 20 const char kHelpSwitchName[] = "help"; | |
| 21 } | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Requested permissions needed for App Remoting tests. The spaces in between | |
| 26 // scope fragments are necessary and will be escaped properly before use. | |
|
Jamie
2015/02/05 00:38:40
Optional: I usually put the spaces at the start of
joedow
2015/02/05 01:19:48
Done.
| |
| 27 const char kAppRemotingAuthScopeValues[] = | |
| 28 "https://www.googleapis.com/auth/appremoting.runapplication " | |
| 29 "https://www.googleapis.com/auth/googletalk " | |
| 30 "https://www.googleapis.com/auth/userinfo.email " | |
| 31 "https://docs.google.com/feeds " | |
| 32 "https://www.googleapis.com/auth/drive"; | |
| 33 | |
| 34 // Used to generate an authorization code request string. | |
|
Jamie
2015/02/05 00:38:40
There's no need for this to be declared as a globa
joedow
2015/02/05 01:19:48
Done.
| |
| 35 const char kAuthorizationCodeFormat[] = | |
| 36 "https://accounts.google.com/o/oauth2/auth" | |
| 37 "?scope=%s" | |
| 38 "&redirect_uri=https://chromoting-oauth.talkgadget.google.com/talkgadget/" | |
| 39 "oauth/chrome-remote-desktop/dev" | |
| 40 "&response_type=code" | |
| 41 "&client_id=%s" | |
| 42 "&access_type=offline" | |
| 43 "&approval_prompt=force"; | |
| 44 | |
| 45 std::string GetAuthorizationCodeUri() { | |
| 46 // Replace space characters with a '+' sign when formatting. | |
| 47 bool use_plus = true; | |
| 48 return base::StringPrintf( | |
| 49 kAuthorizationCodeFormat, | |
| 50 net::EscapeUrlEncodedData(kAppRemotingAuthScopeValues, use_plus).c_str(), | |
| 51 net::EscapeUrlEncodedData(google_apis::GetOAuth2ClientID( | |
| 52 google_apis::CLIENT_REMOTING), use_plus).c_str()); | |
| 53 } | |
| 54 | |
| 55 // Color values used to make the tool usage output text easier to read. | |
| 56 const char kAnsiColorGreen[] = "\x1b[32m"; | |
| 57 const char kAnsiColorCyan[] = "\x1b[36m"; | |
| 58 const char kAnsiColorOrange[] = "\x1b[38;5;208m"; | |
| 59 const char kAnsiColorReset[] = "\x1b[0m"; | |
|
Jamie
2015/02/05 00:38:40
These don't work for all terminal types. You shoul
joedow
2015/02/05 01:19:48
Done.
| |
| 60 | |
| 61 void PrintUsage() { | |
| 62 printf(kAnsiColorCyan); | |
| 63 printf("\n**************************************\n"); | |
| 64 printf("*** App Remoting Test Driver Usage ***\n"); | |
| 65 printf("**************************************\n"); | |
| 66 printf(kAnsiColorReset); | |
| 67 | |
| 68 printf("\nUsage:\n"); | |
| 69 printf(" ar_test_driver --username=<example@gmail.com> [options]\n"); | |
| 70 printf("\nRequired Parameters:\n"); | |
| 71 printf(" %s: Specifies which account to use when running tests\n", | |
| 72 switches::kUserNameSwitchName); | |
| 73 printf("\nOptional Parameters:\n"); | |
| 74 printf(" %s: Exchanged for a refresh and access token for authentication\n", | |
| 75 switches::kAuthCodeSwitchName); | |
| 76 printf(" %s: Displays additional usage information\n", | |
| 77 switches::kHelpSwitchName); | |
| 78 printf(" %s: Specifies the service api to use (dev|test) [default: dev]\n", | |
| 79 switches::kServiceEnvironmentSwitchName); | |
| 80 } | |
| 81 | |
| 82 void PrintAuthCodeInfo() { | |
| 83 printf(kAnsiColorCyan); | |
| 84 printf("\n*******************************\n"); | |
| 85 printf("*** Auth Code Example Usage ***\n"); | |
| 86 printf("*******************************\n"); | |
| 87 printf(kAnsiColorReset); | |
| 88 | |
| 89 printf("\nIf this is the first time you are running the tool, you will need" | |
| 90 " to provide an authorization code.\n" | |
| 91 "This code will be exchanged for a long term refresh token which will" | |
| 92 " be stored locally and used to acquire a short lived access token to" | |
| 93 " connect to the remoting service apis and establish a" | |
| 94 " remote host connection.\n"); | |
|
Jamie
2015/02/05 00:38:40
Word-wrap these to 80-characters (or fewer, if you
joedow
2015/02/05 01:19:48
Done.
| |
| 95 | |
| 96 printf("\nNote: You may need to repeat this step if the stored refresh token" | |
| 97 " has been revoked or expired.\n"); | |
| 98 printf(" Passing in the same auth code twice will result in an error\n"); | |
| 99 | |
| 100 printf("\nFollow these steps to produce an auth code:\n" | |
| 101 " - Open the Authorization URL link shown below in your browser\n" | |
| 102 " - Approve the requested permissions for the tool\n" | |
| 103 " - Copy the highlighted code in the redirected URL\n" | |
| 104 " - Run the tool and pass in copied auth code as a parameter\n"); | |
| 105 | |
| 106 printf("\nAuthorization URL:\n"); | |
| 107 printf(kAnsiColorGreen); | |
| 108 printf("%s\n", GetAuthorizationCodeUri().c_str()); | |
| 109 printf(kAnsiColorReset); | |
| 110 | |
| 111 printf("\nRedirected URL Example:\n"); | |
| 112 printf("https://chromoting-oauth.talkgadget.google.com/talkgadget/oauth/" | |
| 113 "chrome-remote-desktop/dev?code="); | |
| 114 printf(kAnsiColorOrange); | |
| 115 printf("4/AKtf...\n"); | |
| 116 printf(kAnsiColorReset); | |
| 117 | |
| 118 printf("\nTool usage example with the newly created auth code:\n"); | |
| 119 printf("ar_test_driver --%s=example@gmail.com --%s=", | |
| 120 switches::kUserNameSwitchName, | |
| 121 switches::kAuthCodeSwitchName); | |
| 122 printf(kAnsiColorOrange); | |
| 123 printf("4/AKtf...\n\n"); | |
| 124 printf(kAnsiColorReset); | |
| 125 } | |
| 126 | |
| 127 } // namespace | |
| 128 | |
| 129 int main(int argc, char** argv) { | |
| 130 testing::InitGoogleTest(&argc, argv); | |
| 131 TestSuite test_suite(argc, argv); | |
| 132 | |
| 133 // The pointer returned here refers to a singleton, since we don't own the | |
| 134 // lifetime of the object, don't wrap in a scoped_ptr construct or release it. | |
| 135 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 136 DCHECK(command_line); | |
| 137 | |
| 138 // We do not want to retry failures as a failed test should signify an error | |
| 139 // to be investigated. | |
| 140 command_line->AppendSwitchASCII(switches::kTestLauncherRetryLimit, "0"); | |
|
Jamie
2015/02/05 00:38:40
Blank line here, I think.
joedow
2015/02/05 01:19:48
Done.
| |
| 141 // We do not want to run the tests in parallel and we do not | |
|
Jamie
2015/02/05 00:38:40
The word-wrap is a bit funky here.
joedow
2015/02/05 01:19:48
Done.
| |
| 142 // want to retry failures. The reason for running in a single process is that | |
| 143 // some tests may share the same remoting host and they cannot be run | |
| 144 // concurrently, also the test output gets spammed with test launcher messages | |
| 145 // which reduces the readability of the results. | |
| 146 command_line->AppendSwitch("single-process-tests"); | |
|
Jamie
2015/02/05 00:38:40
Maybe add this to the switches namespace for consi
joedow
2015/02/05 01:19:48
Done.
| |
| 147 | |
| 148 // If the user passed in the help flag, then show the help info for this tool | |
| 149 // and 'run' the tests which will print the gtest specific help and then exit. | |
| 150 // NOTE: We do this check after updating the switches as otherwise the gtest | |
| 151 // help is written in parallel with our text and can appear interleaved. | |
| 152 if (command_line->HasSwitch(switches::kHelpSwitchName)) { | |
| 153 PrintUsage(); | |
| 154 PrintAuthCodeInfo(); | |
| 155 return base::LaunchUnitTestsSerially( | |
| 156 argc, | |
| 157 argv, | |
| 158 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); | |
| 159 } | |
| 160 | |
| 161 // Verify we received the required input from the command line. | |
| 162 if (!command_line->HasSwitch(switches::kUserNameSwitchName)) { | |
| 163 LOG(ERROR) << "No user name passed in, can't authenticate without that!"; | |
| 164 PrintUsage(); | |
| 165 return -1; | |
| 166 } | |
| 167 | |
| 168 // Because many tests may access the same remoting host(s), we need to run | |
| 169 // the tests sequentially so they do not interfere with each other. | |
| 170 return base::LaunchUnitTestsSerially( | |
| 171 argc, | |
| 172 argv, | |
| 173 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); | |
| 174 } | |
| OLD | NEW |