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 | */ |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 34 | require_once realpath(dirname(__FILE__) . '/../../lib/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) { |
| 48 | if(!$value) { |
| 49 | echo $error_message . "\n"; |
| 50 | exit(1); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Run the empty_unary test. |
| 56 | * Currently not tested against any server as of 2014-12-04 |
| 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. |
| 67 | * Passes when run against the C++ server as of 2014-12-04 |
| 68 | * Not tested against any other server as of 2014-12-04 |
| 69 | * @param $stub Stub object that has service methods |
| 70 | */ |
| 71 | function largeUnary($stub) { |
| 72 | $request_len = 271828; |
| 73 | $response_len = 314159; |
| 74 | |
| 75 | $request = new grpc\testing\SimpleRequest(); |
| 76 | $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
| 77 | $request->setResponseSize($response_len); |
| 78 | $payload = new grpc\testing\Payload(); |
| 79 | $payload->setType(grpc\testing\PayloadType::COMPRESSABLE); |
| 80 | $payload->setBody(str_repeat("\0", $request_len)); |
| 81 | $request->setPayload($payload); |
| 82 | |
| 83 | list($result, $status) = $stub->UnaryCall($request)->wait(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 84 | hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); |
| 85 | hardAssert($result !== null, 'Call returned a null response'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 86 | $payload = $result->getPayload(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 87 | hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 88 | 'Payload had the wrong type'); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 89 | hardAssert(strlen($payload->getBody()) === $response_len, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 90 | 'Payload had the wrong length'); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 91 | hardAssert($payload->getBody() === str_repeat("\0", $response_len), |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 92 | 'Payload had the wrong content'); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Run the client_streaming test. |
| 97 | * Not tested against any server as of 2014-12-04. |
| 98 | * @param $stub Stub object that has service methods |
| 99 | */ |
| 100 | function clientStreaming($stub) { |
| 101 | $request_lengths = array(27182, 8, 1828, 45904); |
| 102 | |
| 103 | $requests = array_map( |
| 104 | function($length) { |
| 105 | $request = new grpc\testing\StreamingInputCallRequest(); |
| 106 | $payload = new grpc\testing\Payload(); |
| 107 | $payload->setBody(str_repeat("\0", $length)); |
| 108 | $request->setPayload($payload); |
| 109 | return $request; |
| 110 | }, $request_lengths); |
| 111 | |
| 112 | list($result, $status) = $stub->StreamingInputCall($requests)->wait(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 113 | hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); |
| 114 | hardAssert($result->getAggregatedPayloadSize() === 74922, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 115 | 'aggregated_payload_size was incorrect'); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Run the server_streaming test. |
| 120 | * Not tested against any server as of 2014-12-04. |
| 121 | * @param $stub Stub object that has service methods. |
| 122 | */ |
| 123 | function serverStreaming($stub) { |
| 124 | $sizes = array(31415, 9, 2653, 58979); |
| 125 | |
| 126 | $request = new grpc\testing\StreamingOutputCallRequest(); |
| 127 | $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
| 128 | foreach($sizes as $size) { |
| 129 | $response_parameters = new grpc\testing\ResponseParameters(); |
| 130 | $response_parameters->setSize($size); |
| 131 | $request->addResponseParameters($response_parameters); |
| 132 | } |
| 133 | |
| 134 | $call = $stub->StreamingOutputCall($request); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 135 | $i = 0; |
| 136 | foreach($call->responses() as $value) { |
| 137 | hardAssert($i < 4, 'Too many responses'); |
| 138 | $payload = $value->getPayload(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 139 | hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 140 | 'Payload ' . $i . ' had the wrong type'); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 141 | hardAssert(strlen($payload->getBody()) === $sizes[$i], |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 142 | 'Response ' . $i . ' had the wrong length'); |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame^] | 143 | $i += 1; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 144 | } |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame^] | 145 | hardAssert($call->getStatus()->code === Grpc\STATUS_OK, |
| 146 | 'Call did not complete successfully'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Run the ping_pong test. |
| 151 | * Not tested against any server as of 2014-12-04. |
| 152 | * @param $stub Stub object that has service methods. |
| 153 | */ |
| 154 | function pingPong($stub) { |
| 155 | $request_lengths = array(27182, 8, 1828, 45904); |
| 156 | $response_lengths = array(31415, 9, 2653, 58979); |
| 157 | |
| 158 | $call = $stub->FullDuplexCall(); |
| 159 | for($i = 0; $i < 4; $i++) { |
| 160 | $request = new grpc\testing\StreamingOutputCallRequest(); |
| 161 | $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
| 162 | $response_parameters = new grpc\testing\ResponseParameters(); |
| 163 | $response_parameters->setSize($response_lengths[$i]); |
| 164 | $request->addResponseParameters($response_parameters); |
| 165 | $payload = new grpc\testing\Payload(); |
| 166 | $payload->setBody(str_repeat("\0", $request_lengths[$i])); |
| 167 | $request->setPayload($payload); |
| 168 | |
| 169 | $call->write($request); |
| 170 | $response = $call->read(); |
| 171 | |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 172 | hardAssert($response !== null, 'Server returned too few responses'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 173 | $payload = $response->getPayload(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 174 | hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 175 | 'Payload ' . $i . ' had the wrong type'); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 176 | hardAssert(strlen($payload->getBody()) === $response_lengths[$i], |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 177 | 'Payload ' . $i . ' had the wrong length'); |
| 178 | } |
| 179 | $call->writesDone(); |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 180 | hardAssert($call->read() === null, 'Server returned too many responses'); |
| 181 | hardAssert($call->getStatus()->code === Grpc\STATUS_OK, |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 182 | 'Call did not complete successfully'); |
| 183 | } |
| 184 | |
murgatroid99 | 75c9d2f | 2015-02-03 18:16:23 -0800 | [diff] [blame] | 185 | function cancelAfterFirstResponse($stub) { |
murgatroid99 | 554fe35 | 2015-02-03 18:18:28 -0800 | [diff] [blame] | 186 | $call = $stub->FullDuplexCall(); |
murgatroid99 | 75c9d2f | 2015-02-03 18:16:23 -0800 | [diff] [blame] | 187 | $request = new grpc\testing\StreamingOutputCallRequest(); |
| 188 | $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); |
| 189 | $response_parameters = new grpc\testing\ResponseParameters(); |
| 190 | $response_parameters->setSize(31415); |
| 191 | $request->addResponseParameters($response_parameters); |
| 192 | $payload = new grpc\testing\Payload(); |
| 193 | $payload->setBody(str_repeat("\0", 27182)); |
| 194 | $request->setPayload($payload); |
| 195 | |
| 196 | $call->write($request); |
| 197 | $response = $call->read(); |
| 198 | |
| 199 | $call->cancel(); |
| 200 | hardAssert($call->getStatus()->code === Grpc\STATUS_CANCELLED, |
| 201 | 'Call status was not CANCELLED'); |
| 202 | } |
| 203 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 204 | $args = getopt('', array('server_host:', 'server_port:', 'test_case:')); |
| 205 | if (!array_key_exists('server_host', $args) || |
| 206 | !array_key_exists('server_port', $args) || |
| 207 | !array_key_exists('test_case', $args)) { |
| 208 | throw new Exception('Missing argument'); |
| 209 | } |
| 210 | |
| 211 | $server_address = $args['server_host'] . ':' . $args['server_port']; |
| 212 | |
| 213 | $credentials = Grpc\Credentials::createSsl( |
| 214 | file_get_contents(dirname(__FILE__) . '/../data/ca.pem')); |
| 215 | $stub = new grpc\testing\TestServiceClient( |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 216 | new Grpc\BaseStub( |
| 217 | $server_address, |
| 218 | [ |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 219 | 'grpc.ssl_target_name_override' => 'foo.test.google.fr', |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 220 | 'credentials' => $credentials |
| 221 | ])); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 222 | |
| 223 | echo "Connecting to $server_address\n"; |
| 224 | echo "Running test case $args[test_case]\n"; |
| 225 | |
| 226 | switch($args['test_case']) { |
| 227 | case 'empty_unary': |
| 228 | emptyUnary($stub); |
| 229 | break; |
| 230 | case 'large_unary': |
| 231 | largeUnary($stub); |
| 232 | break; |
| 233 | case 'client_streaming': |
| 234 | clientStreaming($stub); |
| 235 | break; |
| 236 | case 'server_streaming': |
| 237 | serverStreaming($stub); |
| 238 | break; |
| 239 | case 'ping_pong': |
| 240 | pingPong($stub); |
| 241 | break; |
murgatroid99 | 75c9d2f | 2015-02-03 18:16:23 -0800 | [diff] [blame] | 242 | case 'cancel_after_first_response': |
| 243 | cancelAfterFirstResponse($stub); |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame^] | 244 | default: |
| 245 | exit(1); |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 246 | } |