|
|
|
@ -41,11 +41,13 @@ use GuzzleHttp\ClientInterface; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Assertion function that always exits with an error code if the assertion is |
|
|
|
|
* falsy |
|
|
|
|
* falsy. |
|
|
|
|
* |
|
|
|
|
* @param $value Assertion value. Should be true. |
|
|
|
|
* @param $error_message Message to display if the assertion is false |
|
|
|
|
*/ |
|
|
|
|
function hardAssert($value, $error_message) { |
|
|
|
|
function hardAssert($value, $error_message) |
|
|
|
|
{ |
|
|
|
|
if (!$value) { |
|
|
|
|
echo $error_message."\n"; |
|
|
|
|
exit(1); |
|
|
|
@ -54,9 +56,11 @@ function hardAssert($value, $error_message) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the empty_unary test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods |
|
|
|
|
*/ |
|
|
|
|
function emptyUnary($stub) { |
|
|
|
|
function emptyUnary($stub) |
|
|
|
|
{ |
|
|
|
|
list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait(); |
|
|
|
|
hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); |
|
|
|
|
hardAssert($result !== null, 'Call completed with a null response'); |
|
|
|
@ -64,20 +68,24 @@ function emptyUnary($stub) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the large_unary test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods |
|
|
|
|
*/ |
|
|
|
|
function largeUnary($stub) { |
|
|
|
|
function largeUnary($stub) |
|
|
|
|
{ |
|
|
|
|
performLargeUnary($stub); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Shared code between large unary test and auth test |
|
|
|
|
* 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, |
|
|
|
|
$metadata = array()) { |
|
|
|
|
$metadata = []) |
|
|
|
|
{ |
|
|
|
|
$request_len = 271828; |
|
|
|
|
$response_len = 314159; |
|
|
|
|
|
|
|
|
@ -101,15 +109,18 @@ function performLargeUnary($stub, $fillUsername = false, $fillOauthScope = false |
|
|
|
|
'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. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods |
|
|
|
|
* @param $args array command line args |
|
|
|
|
*/ |
|
|
|
|
function serviceAccountCreds($stub, $args) { |
|
|
|
|
function serviceAccountCreds($stub, $args) |
|
|
|
|
{ |
|
|
|
|
if (!array_key_exists('oauth_scope', $args)) { |
|
|
|
|
throw new Exception('Missing oauth scope'); |
|
|
|
|
} |
|
|
|
@ -125,11 +136,13 @@ function serviceAccountCreds($stub, $args) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the compute engine credentials auth test. |
|
|
|
|
* Has not been run from gcloud as of 2015-05-05 |
|
|
|
|
* Has not been run from gcloud as of 2015-05-05. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods |
|
|
|
|
* @param $args array command line args |
|
|
|
|
*/ |
|
|
|
|
function computeEngineCreds($stub, $args) { |
|
|
|
|
function computeEngineCreds($stub, $args) |
|
|
|
|
{ |
|
|
|
|
if (!array_key_exists('oauth_scope', $args)) { |
|
|
|
|
throw new Exception('Missing oauth scope'); |
|
|
|
|
} |
|
|
|
@ -143,10 +156,12 @@ function computeEngineCreds($stub, $args) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the jwt token credentials auth test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods |
|
|
|
|
* @param $args array command line args |
|
|
|
|
*/ |
|
|
|
|
function jwtTokenCreds($stub, $args) { |
|
|
|
|
function jwtTokenCreds($stub, $args) |
|
|
|
|
{ |
|
|
|
|
$jsonKey = json_decode( |
|
|
|
|
file_get_contents(getenv(CredentialsLoader::ENV_VAR)), |
|
|
|
|
true); |
|
|
|
@ -157,10 +172,12 @@ function jwtTokenCreds($stub, $args) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the oauth2_auth_token auth test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods |
|
|
|
|
* @param $args array command line args |
|
|
|
|
*/ |
|
|
|
|
function oauth2AuthToken($stub, $args) { |
|
|
|
|
function oauth2AuthToken($stub, $args) |
|
|
|
|
{ |
|
|
|
|
$jsonKey = json_decode( |
|
|
|
|
file_get_contents(getenv(CredentialsLoader::ENV_VAR)), |
|
|
|
|
true); |
|
|
|
@ -171,10 +188,12 @@ function oauth2AuthToken($stub, $args) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the per_rpc_creds auth test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods |
|
|
|
|
* @param $args array command line args |
|
|
|
|
*/ |
|
|
|
|
function perRpcCreds($stub, $args) { |
|
|
|
|
function perRpcCreds($stub, $args) |
|
|
|
|
{ |
|
|
|
|
$jsonKey = json_decode( |
|
|
|
|
file_get_contents(getenv(CredentialsLoader::ENV_VAR)), |
|
|
|
|
true); |
|
|
|
@ -182,10 +201,9 @@ function perRpcCreds($stub, $args) { |
|
|
|
|
$args['oauth_scope'] |
|
|
|
|
); |
|
|
|
|
$token = $auth_credentials->fetchAuthToken(); |
|
|
|
|
$metadata = array(CredentialsLoader::AUTH_METADATA_KEY => |
|
|
|
|
array(sprintf("%s %s", |
|
|
|
|
$metadata = [CredentialsLoader::AUTH_METADATA_KEY => [sprintf('%s %s', |
|
|
|
|
$token['token_type'], |
|
|
|
|
$token['access_token']))); |
|
|
|
|
$token['access_token'])]]; |
|
|
|
|
$result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true, |
|
|
|
|
$metadata); |
|
|
|
|
hardAssert($result->getUsername() == $jsonKey['client_email'], |
|
|
|
@ -194,10 +212,12 @@ function perRpcCreds($stub, $args) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the client_streaming test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods |
|
|
|
|
*/ |
|
|
|
|
function clientStreaming($stub) { |
|
|
|
|
$request_lengths = array(27182, 8, 1828, 45904); |
|
|
|
|
function clientStreaming($stub) |
|
|
|
|
{ |
|
|
|
|
$request_lengths = [27182, 8, 1828, 45904]; |
|
|
|
|
|
|
|
|
|
$requests = array_map( |
|
|
|
|
function ($length) { |
|
|
|
@ -205,6 +225,7 @@ function clientStreaming($stub) { |
|
|
|
|
$payload = new grpc\testing\Payload(); |
|
|
|
|
$payload->setBody(str_repeat("\0", $length)); |
|
|
|
|
$request->setPayload($payload); |
|
|
|
|
|
|
|
|
|
return $request; |
|
|
|
|
}, $request_lengths); |
|
|
|
|
|
|
|
|
@ -220,10 +241,12 @@ function clientStreaming($stub) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the server_streaming test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods. |
|
|
|
|
*/ |
|
|
|
|
function serverStreaming($stub) { |
|
|
|
|
$sizes = array(31415, 9, 2653, 58979); |
|
|
|
|
function serverStreaming($stub) |
|
|
|
|
{ |
|
|
|
|
$sizes = [31415, 9, 2653, 58979]; |
|
|
|
|
|
|
|
|
|
$request = new grpc\testing\StreamingOutputCallRequest(); |
|
|
|
|
$request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
|
|
|
@ -250,14 +273,16 @@ function serverStreaming($stub) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the ping_pong test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods. |
|
|
|
|
*/ |
|
|
|
|
function pingPong($stub) { |
|
|
|
|
$request_lengths = array(27182, 8, 1828, 45904); |
|
|
|
|
$response_lengths = array(31415, 9, 2653, 58979); |
|
|
|
|
function pingPong($stub) |
|
|
|
|
{ |
|
|
|
|
$request_lengths = [27182, 8, 1828, 45904]; |
|
|
|
|
$response_lengths = [31415, 9, 2653, 58979]; |
|
|
|
|
|
|
|
|
|
$call = $stub->FullDuplexCall(); |
|
|
|
|
for($i = 0; $i < 4; $i++) { |
|
|
|
|
for ($i = 0; $i < 4; ++$i) { |
|
|
|
|
$request = new grpc\testing\StreamingOutputCallRequest(); |
|
|
|
|
$request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
|
|
|
|
$response_parameters = new grpc\testing\ResponseParameters(); |
|
|
|
@ -285,9 +310,11 @@ function pingPong($stub) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the empty_stream test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods. |
|
|
|
|
*/ |
|
|
|
|
function emptyStream($stub) { |
|
|
|
|
function emptyStream($stub) |
|
|
|
|
{ |
|
|
|
|
$call = $stub->FullDuplexCall(); |
|
|
|
|
$call->writesDone(); |
|
|
|
|
hardAssert($call->read() === null, 'Server returned too many responses'); |
|
|
|
@ -297,9 +324,11 @@ function emptyStream($stub) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the cancel_after_begin test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods. |
|
|
|
|
*/ |
|
|
|
|
function cancelAfterBegin($stub) { |
|
|
|
|
function cancelAfterBegin($stub) |
|
|
|
|
{ |
|
|
|
|
$call = $stub->StreamingInputCall(); |
|
|
|
|
$call->cancel(); |
|
|
|
|
list($result, $status) = $call->wait(); |
|
|
|
@ -309,9 +338,11 @@ function cancelAfterBegin($stub) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Run the cancel_after_first_response test. |
|
|
|
|
* |
|
|
|
|
* @param $stub Stub object that has service methods. |
|
|
|
|
*/ |
|
|
|
|
function cancelAfterFirstResponse($stub) { |
|
|
|
|
function cancelAfterFirstResponse($stub) |
|
|
|
|
{ |
|
|
|
|
$call = $stub->FullDuplexCall(); |
|
|
|
|
$request = new grpc\testing\StreamingOutputCallRequest(); |
|
|
|
|
$request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
|
|
|
@ -330,8 +361,9 @@ function cancelAfterFirstResponse($stub) { |
|
|
|
|
'Call status was not CANCELLED'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function timeoutOnSleepingServer($stub) { |
|
|
|
|
$call = $stub->FullDuplexCall(array('timeout' => 1000)); |
|
|
|
|
function timeoutOnSleepingServer($stub) |
|
|
|
|
{ |
|
|
|
|
$call = $stub->FullDuplexCall(['timeout' => 1000]); |
|
|
|
|
$request = new grpc\testing\StreamingOutputCallRequest(); |
|
|
|
|
$request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
|
|
|
|
$response_parameters = new grpc\testing\ResponseParameters(); |
|
|
|
@ -348,10 +380,10 @@ function timeoutOnSleepingServer($stub) { |
|
|
|
|
'Call status was not DEADLINE_EXCEEDED'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$args = getopt('', array('server_host:', 'server_port:', 'test_case:', |
|
|
|
|
$args = getopt('', ['server_host:', 'server_port:', 'test_case:', |
|
|
|
|
'use_tls::', 'use_test_ca::', |
|
|
|
|
'server_host_override:', 'oauth_scope:', |
|
|
|
|
'default_service_account:')); |
|
|
|
|
'default_service_account:', ]); |
|
|
|
|
if (!array_key_exists('server_host', $args)) { |
|
|
|
|
throw new Exception('Missing argument: --server_host is required'); |
|
|
|
|
} |
|
|
|
@ -400,8 +432,8 @@ if ($use_tls) { |
|
|
|
|
$opts['grpc.ssl_target_name_override'] = $host_override; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (in_array($test_case, array('service_account_creds', |
|
|
|
|
'compute_engine_creds', 'jwt_token_creds'))) { |
|
|
|
|
if (in_array($test_case, ['service_account_creds', |
|
|
|
|
'compute_engine_creds', 'jwt_token_creds', ])) { |
|
|
|
|
if ($test_case == 'jwt_token_creds') { |
|
|
|
|
$auth_credentials = ApplicationDefaultCredentials::getCredentials(); |
|
|
|
|
} else { |
|
|
|
@ -423,9 +455,10 @@ if ($test_case == 'oauth2_auth_token') { |
|
|
|
|
ClientInterface $client = null) use ($token) { |
|
|
|
|
$metadata_copy = $metadata; |
|
|
|
|
$metadata_copy[CredentialsLoader::AUTH_METADATA_KEY] = |
|
|
|
|
array(sprintf("%s %s", |
|
|
|
|
[sprintf('%s %s', |
|
|
|
|
$token['token_type'], |
|
|
|
|
$token['access_token'])); |
|
|
|
|
$token['access_token'])]; |
|
|
|
|
|
|
|
|
|
return $metadata_copy; |
|
|
|
|
}; |
|
|
|
|
$opts['update_metadata'] = $update_metadata; |
|
|
|
|