interop_client.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  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. */
  34. require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php');
  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. * Run the empty_unary test.
  55. * Passes when run against the Node server as of 2015-04-30
  56. * @param $stub Stub object that has service methods
  57. */
  58. function emptyUnary($stub) {
  59. list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait();
  60. hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
  61. hardAssert($result !== null, 'Call completed with a null response');
  62. }
  63. /**
  64. * Run the large_unary test.
  65. * Passes when run against the C++/Node server as of 2015-04-30
  66. * @param $stub Stub object that has service methods
  67. */
  68. function largeUnary($stub) {
  69. performLargeUnary($stub);
  70. }
  71. /**
  72. * Shared code between large unary test and auth test
  73. * @param $stub Stub object that has service methods
  74. * @param $fillUsername boolean whether to fill result with username
  75. * @param $fillOauthScope boolean whether to fill result with oauth scope
  76. */
  77. function performLargeUnary($stub, $fillUsername = false, $fillOauthScope = false) {
  78. $request_len = 271828;
  79. $response_len = 314159;
  80. $request = new grpc\testing\SimpleRequest();
  81. $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
  82. $request->setResponseSize($response_len);
  83. $payload = new grpc\testing\Payload();
  84. $payload->setType(grpc\testing\PayloadType::COMPRESSABLE);
  85. $payload->setBody(str_repeat("\0", $request_len));
  86. $request->setPayload($payload);
  87. $request->setFillUsername($fillUsername);
  88. $request->setFillOauthScope($fillOauthScope);
  89. list($result, $status) = $stub->UnaryCall($request)->wait();
  90. hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
  91. hardAssert($result !== null, 'Call returned a null response');
  92. $payload = $result->getPayload();
  93. hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
  94. 'Payload had the wrong type');
  95. hardAssert(strlen($payload->getBody()) === $response_len,
  96. 'Payload had the wrong length');
  97. hardAssert($payload->getBody() === str_repeat("\0", $response_len),
  98. 'Payload had the wrong content');
  99. return $result;
  100. }
  101. /**
  102. * Run the service account credentials auth test.
  103. * Passes when run against the cloud server as of 2015-04-30
  104. * @param $stub Stub object that has service methods
  105. * @param $args array command line args
  106. */
  107. function serviceAccountCreds($stub, $args) {
  108. if (!array_key_exists('oauth_scope', $args)) {
  109. throw new Exception('Missing oauth scope');
  110. }
  111. $jsonKey = json_decode(
  112. file_get_contents(getenv(Google\Auth\CredentialsLoader::ENV_VAR)),
  113. true);
  114. $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true);
  115. hardAssert($result->getUsername() == $jsonKey['client_email'],
  116. 'invalid email returned');
  117. hardAssert(strpos($args['oauth_scope'], $result->getOauthScope()) !== false,
  118. 'invalid oauth scope returned');
  119. }
  120. /**
  121. * Run the compute engine credentials auth test.
  122. * Has not been run from gcloud as of 2015-05-05
  123. * @param $stub Stub object that has service methods
  124. * @param $args array command line args
  125. */
  126. function computeEngineCreds($stub, $args) {
  127. if (!array_key_exists('oauth_scope', $args)) {
  128. throw new Exception('Missing oauth scope');
  129. }
  130. if (!array_key_exists('default_service_account', $args)) {
  131. throw new Exception('Missing default_service_account');
  132. }
  133. $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true);
  134. hardAssert($args['default_service_account'] == $result->getUsername(),
  135. 'invalid email returned');
  136. }
  137. /**
  138. * Run the jwt token credentials auth test.
  139. * Passes when run against the cloud server as of 2015-05-12
  140. * @param $stub Stub object that has service methods
  141. * @param $args array command line args
  142. */
  143. function jwtTokenCreds($stub, $args) {
  144. $jsonKey = json_decode(
  145. file_get_contents(getenv(Google\Auth\CredentialsLoader::ENV_VAR)),
  146. true);
  147. $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true);
  148. hardAssert($result->getUsername() == $jsonKey['client_email'],
  149. 'invalid email returned');
  150. }
  151. /**
  152. * Run the client_streaming test.
  153. * Passes when run against the Node server as of 2015-04-30
  154. * @param $stub Stub object that has service methods
  155. */
  156. function clientStreaming($stub) {
  157. $request_lengths = array(27182, 8, 1828, 45904);
  158. $requests = array_map(
  159. function($length) {
  160. $request = new grpc\testing\StreamingInputCallRequest();
  161. $payload = new grpc\testing\Payload();
  162. $payload->setBody(str_repeat("\0", $length));
  163. $request->setPayload($payload);
  164. return $request;
  165. }, $request_lengths);
  166. list($result, $status) = $stub->StreamingInputCall($requests)->wait();
  167. hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
  168. hardAssert($result->getAggregatedPayloadSize() === 74922,
  169. 'aggregated_payload_size was incorrect');
  170. }
  171. /**
  172. * Run the server_streaming test.
  173. * Passes when run against the Node server as of 2015-04-30
  174. * @param $stub Stub object that has service methods.
  175. */
  176. function serverStreaming($stub) {
  177. $sizes = array(31415, 9, 2653, 58979);
  178. $request = new grpc\testing\StreamingOutputCallRequest();
  179. $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
  180. foreach($sizes as $size) {
  181. $response_parameters = new grpc\testing\ResponseParameters();
  182. $response_parameters->setSize($size);
  183. $request->addResponseParameters($response_parameters);
  184. }
  185. $call = $stub->StreamingOutputCall($request);
  186. $i = 0;
  187. foreach($call->responses() as $value) {
  188. hardAssert($i < 4, 'Too many responses');
  189. $payload = $value->getPayload();
  190. hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
  191. 'Payload ' . $i . ' had the wrong type');
  192. hardAssert(strlen($payload->getBody()) === $sizes[$i],
  193. 'Response ' . $i . ' had the wrong length');
  194. $i += 1;
  195. }
  196. hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
  197. 'Call did not complete successfully');
  198. }
  199. /**
  200. * Run the ping_pong test.
  201. * Passes when run against the Node server as of 2015-04-30
  202. * @param $stub Stub object that has service methods.
  203. */
  204. function pingPong($stub) {
  205. $request_lengths = array(27182, 8, 1828, 45904);
  206. $response_lengths = array(31415, 9, 2653, 58979);
  207. $call = $stub->FullDuplexCall();
  208. for($i = 0; $i < 4; $i++) {
  209. $request = new grpc\testing\StreamingOutputCallRequest();
  210. $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
  211. $response_parameters = new grpc\testing\ResponseParameters();
  212. $response_parameters->setSize($response_lengths[$i]);
  213. $request->addResponseParameters($response_parameters);
  214. $payload = new grpc\testing\Payload();
  215. $payload->setBody(str_repeat("\0", $request_lengths[$i]));
  216. $request->setPayload($payload);
  217. $call->write($request);
  218. $response = $call->read();
  219. hardAssert($response !== null, 'Server returned too few responses');
  220. $payload = $response->getPayload();
  221. hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
  222. 'Payload ' . $i . ' had the wrong type');
  223. hardAssert(strlen($payload->getBody()) === $response_lengths[$i],
  224. 'Payload ' . $i . ' had the wrong length');
  225. }
  226. $call->writesDone();
  227. hardAssert($call->read() === null, 'Server returned too many responses');
  228. hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
  229. 'Call did not complete successfully');
  230. }
  231. /**
  232. * Run the cancel_after_first_response test.
  233. * Passes when run against the Node server as of 2015-04-30
  234. * @param $stub Stub object that has service methods.
  235. */
  236. function cancelAfterFirstResponse($stub) {
  237. $call = $stub->FullDuplexCall();
  238. $request = new grpc\testing\StreamingOutputCallRequest();
  239. $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
  240. $response_parameters = new grpc\testing\ResponseParameters();
  241. $response_parameters->setSize(31415);
  242. $request->addResponseParameters($response_parameters);
  243. $payload = new grpc\testing\Payload();
  244. $payload->setBody(str_repeat("\0", 27182));
  245. $request->setPayload($payload);
  246. $call->write($request);
  247. $response = $call->read();
  248. $call->cancel();
  249. hardAssert($call->getStatus()->code === Grpc\STATUS_CANCELLED,
  250. 'Call status was not CANCELLED');
  251. }
  252. $args = getopt('', array('server_host:', 'server_port:', 'test_case:',
  253. 'server_host_override:', 'oauth_scope:',
  254. 'default_service_account:'));
  255. if (!array_key_exists('server_host', $args) ||
  256. !array_key_exists('server_port', $args) ||
  257. !array_key_exists('test_case', $args)) {
  258. throw new Exception('Missing argument');
  259. }
  260. if ($args['server_port'] == 443) {
  261. $server_address = $args['server_host'];
  262. } else {
  263. $server_address = $args['server_host'] . ':' . $args['server_port'];
  264. }
  265. if (!array_key_exists('server_host_override', $args)) {
  266. $args['server_host_override'] = 'foo.test.google.fr';
  267. }
  268. $ssl_cert_file = getenv('SSL_CERT_FILE');
  269. if (!$ssl_cert_file) {
  270. $ssl_cert_file = dirname(__FILE__) . '/../data/ca.pem';
  271. }
  272. $credentials = Grpc\Credentials::createSsl(file_get_contents($ssl_cert_file));
  273. $opts = [
  274. 'grpc.ssl_target_name_override' => $args['server_host_override'],
  275. 'credentials' => $credentials,
  276. ];
  277. if (in_array($args['test_case'], array(
  278. 'service_account_creds',
  279. 'compute_engine_creds',
  280. 'jwt_token_creds'))) {
  281. if ($args['test_case'] == 'jwt_token_creds') {
  282. $auth = Google\Auth\ApplicationDefaultCredentials::getCredentials();
  283. } else {
  284. $auth = Google\Auth\ApplicationDefaultCredentials::getCredentials(
  285. $args['oauth_scope']);
  286. }
  287. $opts['update_metadata'] = $auth->getUpdateMetadataFunc();
  288. }
  289. $stub = new grpc\testing\TestServiceClient(
  290. new Grpc\BaseStub(
  291. $server_address,
  292. $opts));
  293. echo "Connecting to $server_address\n";
  294. echo "Running test case $args[test_case]\n";
  295. switch ($args['test_case']) {
  296. case 'empty_unary':
  297. emptyUnary($stub);
  298. break;
  299. case 'large_unary':
  300. largeUnary($stub);
  301. break;
  302. case 'client_streaming':
  303. clientStreaming($stub);
  304. break;
  305. case 'server_streaming':
  306. serverStreaming($stub);
  307. break;
  308. case 'ping_pong':
  309. pingPong($stub);
  310. break;
  311. case 'cancel_after_first_response':
  312. cancelAfterFirstResponse($stub);
  313. break;
  314. case 'service_account_creds':
  315. serviceAccountCreds($stub, $args);
  316. break;
  317. case 'compute_engine_creds':
  318. computeEngineCreds($stub, $args);
  319. break;
  320. case 'jwt_token_creds':
  321. jwtTokenCreds($stub, $args);
  322. break;
  323. default:
  324. exit(1);
  325. }