diff options
author | Stanley Cheung <stanleycheung@google.com> | 2015-04-30 09:25:45 -0700 |
---|---|---|
committer | Stanley Cheung <stanleycheung@google.com> | 2015-04-30 09:26:19 -0700 |
commit | 0e08aed61c022f84ab80306565bf71b8e8613ff0 (patch) | |
tree | 3fcc55df6f6199bdf964a4c8bccc0738e18a9696 /src/php/tests/interop | |
parent | 6d42a73bb984e19eb1bf84e2a952eec861cea464 (diff) |
add php service account credentials auth interop test, tested against cloud server
Diffstat (limited to 'src/php/tests/interop')
-rwxr-xr-x | src/php/tests/interop/interop_client.php | 77 |
1 files changed, 65 insertions, 12 deletions
diff --git a/src/php/tests/interop/interop_client.php b/src/php/tests/interop/interop_client.php index 6f81bfa6cd..2c1c13caa1 100755 --- a/src/php/tests/interop/interop_client.php +++ b/src/php/tests/interop/interop_client.php @@ -45,7 +45,7 @@ require 'test.php'; * @param $error_message Message to display if the assertion is false */ function hardAssert($value, $error_message) { - if(!$value) { + if (!$value) { echo $error_message . "\n"; exit(1); } @@ -53,7 +53,7 @@ function hardAssert($value, $error_message) { /** * Run the empty_unary test. - * Currently not tested against any server as of 2014-12-04 + * Passes when run against the Node server as of 2015-04-30 * @param $stub Stub object that has service methods */ function emptyUnary($stub) { @@ -64,11 +64,20 @@ function emptyUnary($stub) { /** * Run the large_unary test. - * Passes when run against the C++ server as of 2014-12-04 - * Not tested against any other server as of 2014-12-04 + * Passes when run against the C++/Node server as of 2015-04-30 * @param $stub Stub object that has service methods */ function largeUnary($stub) { + performLargeUnary($stub); +} + +/** + * Shared code between large unary test and auth test + * @param $stub Stub object that has service methods + * @param $fillUsername boolean whether to fill result with username + * @param $fillOauthScope boolean whether to fill result with oauth scope + */ +function performLargeUnary($stub, $fillUsername = false, $fillOauthScope = false) { $request_len = 271828; $response_len = 314159; @@ -79,6 +88,8 @@ function largeUnary($stub) { $payload->setType(grpc\testing\PayloadType::COMPRESSABLE); $payload->setBody(str_repeat("\0", $request_len)); $request->setPayload($payload); + $request->setFillUsername($fillUsername); + $request->setFillOauthScope($fillOauthScope); list($result, $status) = $stub->UnaryCall($request)->wait(); hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); @@ -90,6 +101,27 @@ function largeUnary($stub) { 'Payload had the wrong length'); hardAssert($payload->getBody() === str_repeat("\0", $response_len), 'Payload had the wrong content'); + return $result; +} + +/** + * Run the service account credentials auth test. + * Passes when run against the cloud server as of 2015-04-30 + * @param $stub Stub object that has service methods + * @param $args array command line args + */ +function serviceAccountCreds($stub, $args) { + if (!array_key_exists('oauth_scope', $args)) { + throw new Exception('Missing oauth scope'); + } + $jsonKey = json_decode( + file_get_contents(getenv(Google\Auth\CredentialsLoader::ENV_VAR)), + true); + $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true); + hardAssert($result->getUsername() == $jsonKey['client_email'], + 'invalid email returned'); + hardAssert(strpos($args['oauth_scope'], $result->getOauthScope()) !== false, + 'invalid oauth scope returned'); } /** @@ -201,7 +233,8 @@ function cancelAfterFirstResponse($stub) { 'Call status was not CANCELLED'); } -$args = getopt('', array('server_host:', 'server_port:', 'test_case:')); +$args = getopt('', array('server_host:', 'server_port:', 'test_case:', + 'server_host_override:', 'oauth_scope:')); if (!array_key_exists('server_host', $args) || !array_key_exists('server_port', $args) || !array_key_exists('test_case', $args)) { @@ -210,20 +243,37 @@ if (!array_key_exists('server_host', $args) || $server_address = $args['server_host'] . ':' . $args['server_port']; -$credentials = Grpc\Credentials::createSsl( - file_get_contents(dirname(__FILE__) . '/../data/ca.pem')); +if (!array_key_exists('server_host_override', $args)) { + $args['server_host_override'] = 'foo.test.google.fr'; +} + +$ssl_cert_file = getenv('SSL_CERT_FILE'); +if (!$ssl_cert_file) { + $ssl_cert_file = dirname(__FILE__) . '/../data/ca.pem'; +} + +$credentials = Grpc\Credentials::createSsl(file_get_contents($ssl_cert_file)); + +$opts = [ + 'grpc.ssl_target_name_override' => $args['server_host_override'], + 'credentials' => $credentials, + ]; + +if (array_key_exists('oauth_scope', $args)) { + $auth = Google\Auth\ApplicationDefaultCredentials::getCredentials( + $args['oauth_scope']); + $opts['update_metadata'] = $auth->getUpdateMetadataFunc(); +} + $stub = new grpc\testing\TestServiceClient( new Grpc\BaseStub( $server_address, - [ - 'grpc.ssl_target_name_override' => 'foo.test.google.fr', - 'credentials' => $credentials - ])); + $opts)); echo "Connecting to $server_address\n"; echo "Running test case $args[test_case]\n"; -switch($args['test_case']) { +switch ($args['test_case']) { case 'empty_unary': emptyUnary($stub); break; @@ -242,6 +292,9 @@ switch($args['test_case']) { case 'cancel_after_first_response': cancelAfterFirstResponse($stub); break; + case 'service_account_creds': + serviceAccountCreds($stub, $args); + break; default: exit(1); } |