mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 1 | <?php |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Copyright 2015, Google Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following disclaimer |
| 15 | * in the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * * Neither the name of Google Inc. nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived from |
| 19 | * this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | */ |
murgatroid99 | b2a6d01 | 2015-03-27 13:15:58 -0700 | [diff] [blame] | 34 | require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 35 | require 'DrSlump/Protobuf.php'; |
| 36 | \DrSlump\Protobuf::autoload(); |
| 37 | require 'empty.php'; |
| 38 | require 'message_set.php'; |
| 39 | require 'messages.php'; |
| 40 | require 'test.php'; |
| 41 | /** |
| 42 | * Assertion function that always exits with an error code if the assertion is |
| 43 | * falsy |
| 44 | * @param $value Assertion value. Should be true. |
| 45 | * @param $error_message Message to display if the assertion is false |
| 46 | */ |
| 47 | function hardAssert($value, $error_message) { |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 48 | if (!$value) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 49 | echo $error_message . "\n"; |
| 50 | exit(1); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Run the empty_unary test. |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 56 | * Passes when run against the Node server as of 2015-04-30 |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 57 | * @param $stub Stub object that has service methods |
| 58 | */ |
| 59 | function emptyUnary($stub) { |
murgatroid99 | f21eb25 | 2015-01-30 13:47:41 -0800 | [diff] [blame] | 60 | list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 61 | hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); |
| 62 | hardAssert($result !== null, 'Call completed with a null response'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Run the large_unary test. |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 67 | * Passes when run against the C++/Node server as of 2015-04-30 |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 68 | * @param $stub Stub object that has service methods |
| 69 | */ |
| 70 | function largeUnary($stub) { |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 71 | performLargeUnary($stub); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Shared code between large unary test and auth test |
| 76 | * @param $stub Stub object that has service methods |
| 77 | * @param $fillUsername boolean whether to fill result with username |
| 78 | * @param $fillOauthScope boolean whether to fill result with oauth scope |
| 79 | */ |
| 80 | function performLargeUnary($stub, $fillUsername = false, $fillOauthScope = false) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 81 | $request_len = 271828; |
| 82 | $response_len = 314159; |
| 83 | |
| 84 | $request = new grpc\testing\SimpleRequest(); |
| 85 | $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
| 86 | $request->setResponseSize($response_len); |
| 87 | $payload = new grpc\testing\Payload(); |
| 88 | $payload->setType(grpc\testing\PayloadType::COMPRESSABLE); |
| 89 | $payload->setBody(str_repeat("\0", $request_len)); |
| 90 | $request->setPayload($payload); |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 91 | $request->setFillUsername($fillUsername); |
| 92 | $request->setFillOauthScope($fillOauthScope); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 93 | |
| 94 | list($result, $status) = $stub->UnaryCall($request)->wait(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 95 | hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); |
| 96 | hardAssert($result !== null, 'Call returned a null response'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 97 | $payload = $result->getPayload(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 98 | hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 99 | 'Payload had the wrong type'); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 100 | hardAssert(strlen($payload->getBody()) === $response_len, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 101 | 'Payload had the wrong length'); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 102 | hardAssert($payload->getBody() === str_repeat("\0", $response_len), |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 103 | 'Payload had the wrong content'); |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 104 | return $result; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Run the service account credentials auth test. |
| 109 | * Passes when run against the cloud server as of 2015-04-30 |
| 110 | * @param $stub Stub object that has service methods |
| 111 | * @param $args array command line args |
| 112 | */ |
| 113 | function serviceAccountCreds($stub, $args) { |
| 114 | if (!array_key_exists('oauth_scope', $args)) { |
| 115 | throw new Exception('Missing oauth scope'); |
| 116 | } |
| 117 | $jsonKey = json_decode( |
| 118 | file_get_contents(getenv(Google\Auth\CredentialsLoader::ENV_VAR)), |
| 119 | true); |
| 120 | $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true); |
| 121 | hardAssert($result->getUsername() == $jsonKey['client_email'], |
| 122 | 'invalid email returned'); |
| 123 | hardAssert(strpos($args['oauth_scope'], $result->getOauthScope()) !== false, |
| 124 | 'invalid oauth scope returned'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Run the client_streaming test. |
| 129 | * Not tested against any server as of 2014-12-04. |
| 130 | * @param $stub Stub object that has service methods |
| 131 | */ |
| 132 | function clientStreaming($stub) { |
| 133 | $request_lengths = array(27182, 8, 1828, 45904); |
| 134 | |
| 135 | $requests = array_map( |
| 136 | function($length) { |
| 137 | $request = new grpc\testing\StreamingInputCallRequest(); |
| 138 | $payload = new grpc\testing\Payload(); |
| 139 | $payload->setBody(str_repeat("\0", $length)); |
| 140 | $request->setPayload($payload); |
| 141 | return $request; |
| 142 | }, $request_lengths); |
| 143 | |
| 144 | list($result, $status) = $stub->StreamingInputCall($requests)->wait(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 145 | hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); |
| 146 | hardAssert($result->getAggregatedPayloadSize() === 74922, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 147 | 'aggregated_payload_size was incorrect'); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Run the server_streaming test. |
| 152 | * Not tested against any server as of 2014-12-04. |
| 153 | * @param $stub Stub object that has service methods. |
| 154 | */ |
| 155 | function serverStreaming($stub) { |
| 156 | $sizes = array(31415, 9, 2653, 58979); |
| 157 | |
| 158 | $request = new grpc\testing\StreamingOutputCallRequest(); |
| 159 | $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
| 160 | foreach($sizes as $size) { |
| 161 | $response_parameters = new grpc\testing\ResponseParameters(); |
| 162 | $response_parameters->setSize($size); |
| 163 | $request->addResponseParameters($response_parameters); |
| 164 | } |
| 165 | |
| 166 | $call = $stub->StreamingOutputCall($request); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 167 | $i = 0; |
| 168 | foreach($call->responses() as $value) { |
| 169 | hardAssert($i < 4, 'Too many responses'); |
| 170 | $payload = $value->getPayload(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 171 | hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 172 | 'Payload ' . $i . ' had the wrong type'); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 173 | hardAssert(strlen($payload->getBody()) === $sizes[$i], |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 174 | 'Response ' . $i . ' had the wrong length'); |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 175 | $i += 1; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 176 | } |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 177 | hardAssert($call->getStatus()->code === Grpc\STATUS_OK, |
| 178 | 'Call did not complete successfully'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Run the ping_pong test. |
| 183 | * Not tested against any server as of 2014-12-04. |
| 184 | * @param $stub Stub object that has service methods. |
| 185 | */ |
| 186 | function pingPong($stub) { |
| 187 | $request_lengths = array(27182, 8, 1828, 45904); |
| 188 | $response_lengths = array(31415, 9, 2653, 58979); |
| 189 | |
| 190 | $call = $stub->FullDuplexCall(); |
| 191 | for($i = 0; $i < 4; $i++) { |
| 192 | $request = new grpc\testing\StreamingOutputCallRequest(); |
| 193 | $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
| 194 | $response_parameters = new grpc\testing\ResponseParameters(); |
| 195 | $response_parameters->setSize($response_lengths[$i]); |
| 196 | $request->addResponseParameters($response_parameters); |
| 197 | $payload = new grpc\testing\Payload(); |
| 198 | $payload->setBody(str_repeat("\0", $request_lengths[$i])); |
| 199 | $request->setPayload($payload); |
| 200 | |
| 201 | $call->write($request); |
| 202 | $response = $call->read(); |
| 203 | |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 204 | hardAssert($response !== null, 'Server returned too few responses'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 205 | $payload = $response->getPayload(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 206 | hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 207 | 'Payload ' . $i . ' had the wrong type'); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 208 | hardAssert(strlen($payload->getBody()) === $response_lengths[$i], |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 209 | 'Payload ' . $i . ' had the wrong length'); |
| 210 | } |
| 211 | $call->writesDone(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 212 | hardAssert($call->read() === null, 'Server returned too many responses'); |
| 213 | hardAssert($call->getStatus()->code === Grpc\STATUS_OK, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 214 | 'Call did not complete successfully'); |
| 215 | } |
| 216 | |
murgatroid99 | 75c9d2f | 2015-02-03 18:16:23 -0800 | [diff] [blame] | 217 | function cancelAfterFirstResponse($stub) { |
murgatroid99 | 554fe35 | 2015-02-03 18:18:28 -0800 | [diff] [blame] | 218 | $call = $stub->FullDuplexCall(); |
murgatroid99 | 75c9d2f | 2015-02-03 18:16:23 -0800 | [diff] [blame] | 219 | $request = new grpc\testing\StreamingOutputCallRequest(); |
| 220 | $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
| 221 | $response_parameters = new grpc\testing\ResponseParameters(); |
| 222 | $response_parameters->setSize(31415); |
| 223 | $request->addResponseParameters($response_parameters); |
| 224 | $payload = new grpc\testing\Payload(); |
| 225 | $payload->setBody(str_repeat("\0", 27182)); |
| 226 | $request->setPayload($payload); |
| 227 | |
| 228 | $call->write($request); |
| 229 | $response = $call->read(); |
| 230 | |
| 231 | $call->cancel(); |
| 232 | hardAssert($call->getStatus()->code === Grpc\STATUS_CANCELLED, |
| 233 | 'Call status was not CANCELLED'); |
| 234 | } |
| 235 | |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 236 | $args = getopt('', array('server_host:', 'server_port:', 'test_case:', |
| 237 | 'server_host_override:', 'oauth_scope:')); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 238 | if (!array_key_exists('server_host', $args) || |
| 239 | !array_key_exists('server_port', $args) || |
| 240 | !array_key_exists('test_case', $args)) { |
| 241 | throw new Exception('Missing argument'); |
| 242 | } |
| 243 | |
| 244 | $server_address = $args['server_host'] . ':' . $args['server_port']; |
| 245 | |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 246 | if (!array_key_exists('server_host_override', $args)) { |
| 247 | $args['server_host_override'] = 'foo.test.google.fr'; |
| 248 | } |
| 249 | |
| 250 | $ssl_cert_file = getenv('SSL_CERT_FILE'); |
| 251 | if (!$ssl_cert_file) { |
| 252 | $ssl_cert_file = dirname(__FILE__) . '/../data/ca.pem'; |
| 253 | } |
| 254 | |
| 255 | $credentials = Grpc\Credentials::createSsl(file_get_contents($ssl_cert_file)); |
| 256 | |
| 257 | $opts = [ |
| 258 | 'grpc.ssl_target_name_override' => $args['server_host_override'], |
| 259 | 'credentials' => $credentials, |
| 260 | ]; |
| 261 | |
| 262 | if (array_key_exists('oauth_scope', $args)) { |
| 263 | $auth = Google\Auth\ApplicationDefaultCredentials::getCredentials( |
| 264 | $args['oauth_scope']); |
| 265 | $opts['update_metadata'] = $auth->getUpdateMetadataFunc(); |
| 266 | } |
| 267 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 268 | $stub = new grpc\testing\TestServiceClient( |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 269 | new Grpc\BaseStub( |
| 270 | $server_address, |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 271 | $opts)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 272 | |
| 273 | echo "Connecting to $server_address\n"; |
| 274 | echo "Running test case $args[test_case]\n"; |
| 275 | |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 276 | switch ($args['test_case']) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 277 | case 'empty_unary': |
| 278 | emptyUnary($stub); |
| 279 | break; |
| 280 | case 'large_unary': |
| 281 | largeUnary($stub); |
| 282 | break; |
| 283 | case 'client_streaming': |
| 284 | clientStreaming($stub); |
| 285 | break; |
| 286 | case 'server_streaming': |
| 287 | serverStreaming($stub); |
| 288 | break; |
| 289 | case 'ping_pong': |
| 290 | pingPong($stub); |
| 291 | break; |
murgatroid99 | 75c9d2f | 2015-02-03 18:16:23 -0800 | [diff] [blame] | 292 | case 'cancel_after_first_response': |
| 293 | cancelAfterFirstResponse($stub); |
murgatroid99 | abd364d | 2015-03-30 14:29:14 -0700 | [diff] [blame] | 294 | break; |
Stanley Cheung | 0e08aed | 2015-04-30 09:25:45 -0700 | [diff] [blame^] | 295 | case 'service_account_creds': |
| 296 | serviceAccountCreds($stub, $args); |
| 297 | break; |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 298 | default: |
| 299 | exit(1); |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 300 | } |