blob: 2c1c13caa1d4161874567cd82be5090a0c7079fd [file] [log] [blame]
mlumishb892a272014-12-09 16:28:23 -08001<?php
Craig Tiller2e498aa2015-02-16 12:09:31 -08002/*
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 */
murgatroid99b2a6d012015-03-27 13:15:58 -070034require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php');
mlumishb892a272014-12-09 16:28:23 -080035require 'DrSlump/Protobuf.php';
36\DrSlump\Protobuf::autoload();
37require 'empty.php';
38require 'message_set.php';
39require 'messages.php';
40require '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 */
47function hardAssert($value, $error_message) {
Stanley Cheung0e08aed2015-04-30 09:25:45 -070048 if (!$value) {
mlumishb892a272014-12-09 16:28:23 -080049 echo $error_message . "\n";
50 exit(1);
51 }
52}
53
54/**
55 * Run the empty_unary test.
Stanley Cheung0e08aed2015-04-30 09:25:45 -070056 * Passes when run against the Node server as of 2015-04-30
mlumishb892a272014-12-09 16:28:23 -080057 * @param $stub Stub object that has service methods
58 */
59function emptyUnary($stub) {
murgatroid99f21eb252015-01-30 13:47:41 -080060 list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait();
murgatroid9925e5f672015-02-02 11:05:01 -080061 hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
62 hardAssert($result !== null, 'Call completed with a null response');
mlumishb892a272014-12-09 16:28:23 -080063}
64
65/**
66 * Run the large_unary test.
Stanley Cheung0e08aed2015-04-30 09:25:45 -070067 * Passes when run against the C++/Node server as of 2015-04-30
mlumishb892a272014-12-09 16:28:23 -080068 * @param $stub Stub object that has service methods
69 */
70function largeUnary($stub) {
Stanley Cheung0e08aed2015-04-30 09:25:45 -070071 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 */
80function performLargeUnary($stub, $fillUsername = false, $fillOauthScope = false) {
mlumishb892a272014-12-09 16:28:23 -080081 $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 Cheung0e08aed2015-04-30 09:25:45 -070091 $request->setFillUsername($fillUsername);
92 $request->setFillOauthScope($fillOauthScope);
mlumishb892a272014-12-09 16:28:23 -080093
94 list($result, $status) = $stub->UnaryCall($request)->wait();
murgatroid9925e5f672015-02-02 11:05:01 -080095 hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
96 hardAssert($result !== null, 'Call returned a null response');
mlumishb892a272014-12-09 16:28:23 -080097 $payload = $result->getPayload();
murgatroid9925e5f672015-02-02 11:05:01 -080098 hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
mlumishb892a272014-12-09 16:28:23 -080099 'Payload had the wrong type');
murgatroid9925e5f672015-02-02 11:05:01 -0800100 hardAssert(strlen($payload->getBody()) === $response_len,
mlumishb892a272014-12-09 16:28:23 -0800101 'Payload had the wrong length');
murgatroid9925e5f672015-02-02 11:05:01 -0800102 hardAssert($payload->getBody() === str_repeat("\0", $response_len),
mlumishb892a272014-12-09 16:28:23 -0800103 'Payload had the wrong content');
Stanley Cheung0e08aed2015-04-30 09:25:45 -0700104 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 */
113function 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');
mlumishb892a272014-12-09 16:28:23 -0800125}
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 */
132function 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();
murgatroid9925e5f672015-02-02 11:05:01 -0800145 hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
146 hardAssert($result->getAggregatedPayloadSize() === 74922,
mlumishb892a272014-12-09 16:28:23 -0800147 '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 */
155function 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);
mlumishb892a272014-12-09 16:28:23 -0800167 $i = 0;
168 foreach($call->responses() as $value) {
169 hardAssert($i < 4, 'Too many responses');
170 $payload = $value->getPayload();
murgatroid9925e5f672015-02-02 11:05:01 -0800171 hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
mlumishb892a272014-12-09 16:28:23 -0800172 'Payload ' . $i . ' had the wrong type');
murgatroid9925e5f672015-02-02 11:05:01 -0800173 hardAssert(strlen($payload->getBody()) === $sizes[$i],
mlumishb892a272014-12-09 16:28:23 -0800174 'Response ' . $i . ' had the wrong length');
murgatroid99c1da8f22015-03-25 11:33:05 -0700175 $i += 1;
mlumishb892a272014-12-09 16:28:23 -0800176 }
murgatroid99c1da8f22015-03-25 11:33:05 -0700177 hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
178 'Call did not complete successfully');
mlumishb892a272014-12-09 16:28:23 -0800179}
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 */
186function 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
murgatroid9925e5f672015-02-02 11:05:01 -0800204 hardAssert($response !== null, 'Server returned too few responses');
mlumishb892a272014-12-09 16:28:23 -0800205 $payload = $response->getPayload();
murgatroid9925e5f672015-02-02 11:05:01 -0800206 hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
mlumishb892a272014-12-09 16:28:23 -0800207 'Payload ' . $i . ' had the wrong type');
murgatroid9925e5f672015-02-02 11:05:01 -0800208 hardAssert(strlen($payload->getBody()) === $response_lengths[$i],
mlumishb892a272014-12-09 16:28:23 -0800209 'Payload ' . $i . ' had the wrong length');
210 }
211 $call->writesDone();
murgatroid9925e5f672015-02-02 11:05:01 -0800212 hardAssert($call->read() === null, 'Server returned too many responses');
213 hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
mlumishb892a272014-12-09 16:28:23 -0800214 'Call did not complete successfully');
215}
216
murgatroid9975c9d2f2015-02-03 18:16:23 -0800217function cancelAfterFirstResponse($stub) {
murgatroid99554fe352015-02-03 18:18:28 -0800218 $call = $stub->FullDuplexCall();
murgatroid9975c9d2f2015-02-03 18:16:23 -0800219 $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 Cheung0e08aed2015-04-30 09:25:45 -0700236$args = getopt('', array('server_host:', 'server_port:', 'test_case:',
237 'server_host_override:', 'oauth_scope:'));
mlumishb892a272014-12-09 16:28:23 -0800238if (!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 Cheung0e08aed2015-04-30 09:25:45 -0700246if (!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');
251if (!$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
262if (array_key_exists('oauth_scope', $args)) {
263 $auth = Google\Auth\ApplicationDefaultCredentials::getCredentials(
264 $args['oauth_scope']);
265 $opts['update_metadata'] = $auth->getUpdateMetadataFunc();
266}
267
mlumishb892a272014-12-09 16:28:23 -0800268$stub = new grpc\testing\TestServiceClient(
murgatroid9914d2ce22015-01-30 15:36:23 -0800269 new Grpc\BaseStub(
270 $server_address,
Stanley Cheung0e08aed2015-04-30 09:25:45 -0700271 $opts));
mlumishb892a272014-12-09 16:28:23 -0800272
273echo "Connecting to $server_address\n";
274echo "Running test case $args[test_case]\n";
275
Stanley Cheung0e08aed2015-04-30 09:25:45 -0700276switch ($args['test_case']) {
mlumishb892a272014-12-09 16:28:23 -0800277 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;
murgatroid9975c9d2f2015-02-03 18:16:23 -0800292 case 'cancel_after_first_response':
293 cancelAfterFirstResponse($stub);
murgatroid99abd364d2015-03-30 14:29:14 -0700294 break;
Stanley Cheung0e08aed2015-04-30 09:25:45 -0700295 case 'service_account_creds':
296 serviceAccountCreds($stub, $args);
297 break;
murgatroid99c1da8f22015-03-25 11:33:05 -0700298 default:
299 exit(1);
Craig Tiller2e498aa2015-02-16 12:09:31 -0800300}