interop_client.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <?php
  2. /*
  3. *
  4. * Copyright 2015-2016 gRPC authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. require_once realpath(dirname(__FILE__).'/../../vendor/autoload.php');
  20. // The following includes are needed when using protobuf 3.1.0
  21. // and will suppress warnings when using protobuf 3.2.0+
  22. @include_once 'src/proto/grpc/testing/test.pb.php';
  23. @include_once 'src/proto/grpc/testing/test_grpc_pb.php';
  24. use Google\Auth\CredentialsLoader;
  25. use Google\Auth\ApplicationDefaultCredentials;
  26. use GuzzleHttp\ClientInterface;
  27. /**
  28. * Assertion function that always exits with an error code if the assertion is
  29. * falsy.
  30. *
  31. * @param $value Assertion value. Should be true.
  32. * @param $error_message Message to display if the assertion is false
  33. */
  34. function hardAssert($value, $error_message)
  35. {
  36. if (!$value) {
  37. echo $error_message."\n";
  38. exit(1);
  39. }
  40. }
  41. function hardAssertIfStatusOk($status)
  42. {
  43. if ($status->code !== Grpc\STATUS_OK) {
  44. echo "Call did not complete successfully. Status object:\n";
  45. var_dump($status);
  46. exit(1);
  47. }
  48. }
  49. /**
  50. * Run the empty_unary test.
  51. *
  52. * @param $stub Stub object that has service methods
  53. */
  54. function emptyUnary($stub)
  55. {
  56. list($result, $status) =
  57. $stub->EmptyCall(new Grpc\Testing\EmptyMessage())->wait();
  58. hardAssertIfStatusOk($status);
  59. hardAssert($result !== null, 'Call completed with a null response');
  60. }
  61. /**
  62. * Run the large_unary test.
  63. *
  64. * @param $stub Stub object that has service methods
  65. */
  66. function largeUnary($stub)
  67. {
  68. performLargeUnary($stub);
  69. }
  70. /**
  71. * Shared code between large unary test and auth test.
  72. *
  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,
  78. $fillOauthScope = false, $callback = false)
  79. {
  80. $request_len = 271828;
  81. $response_len = 314159;
  82. $request = new Grpc\Testing\SimpleRequest();
  83. $request->setResponseType(Grpc\Testing\PayloadType::COMPRESSABLE);
  84. $request->setResponseSize($response_len);
  85. $payload = new Grpc\Testing\Payload();
  86. $payload->setType(Grpc\Testing\PayloadType::COMPRESSABLE);
  87. $payload->setBody(str_repeat("\0", $request_len));
  88. $request->setPayload($payload);
  89. $request->setFillUsername($fillUsername);
  90. $request->setFillOauthScope($fillOauthScope);
  91. $options = [];
  92. if ($callback) {
  93. $options['call_credentials_callback'] = $callback;
  94. }
  95. list($result, $status) = $stub->UnaryCall($request, [], $options)->wait();
  96. hardAssertIfStatusOk($status);
  97. hardAssert($result !== null, 'Call returned a null response');
  98. $payload = $result->getPayload();
  99. hardAssert($payload->getType() === Grpc\Testing\PayloadType::COMPRESSABLE,
  100. 'Payload had the wrong type');
  101. hardAssert(strlen($payload->getBody()) === $response_len,
  102. 'Payload had the wrong length');
  103. hardAssert($payload->getBody() === str_repeat("\0", $response_len),
  104. 'Payload had the wrong content');
  105. return $result;
  106. }
  107. /**
  108. * Run the service account credentials auth test.
  109. *
  110. * @param $stub Stub object that has service methods
  111. * @param $args array command line args
  112. */
  113. function serviceAccountCreds($stub, $args)
  114. {
  115. if (!array_key_exists('oauth_scope', $args)) {
  116. throw new Exception('Missing oauth scope');
  117. }
  118. $jsonKey = json_decode(
  119. file_get_contents(getenv(CredentialsLoader::ENV_VAR)),
  120. true);
  121. $result = performLargeUnary($stub, $fillUsername = true,
  122. $fillOauthScope = true);
  123. hardAssert($result->getUsername() === $jsonKey['client_email'],
  124. 'invalid email returned');
  125. hardAssert(strpos($args['oauth_scope'], $result->getOauthScope()) !== false,
  126. 'invalid oauth scope returned');
  127. }
  128. /**
  129. * Run the compute engine credentials auth test.
  130. * Has not been run from gcloud as of 2015-05-05.
  131. *
  132. * @param $stub Stub object that has service methods
  133. * @param $args array command line args
  134. */
  135. function computeEngineCreds($stub, $args)
  136. {
  137. if (!array_key_exists('oauth_scope', $args)) {
  138. throw new Exception('Missing oauth scope');
  139. }
  140. if (!array_key_exists('default_service_account', $args)) {
  141. throw new Exception('Missing default_service_account');
  142. }
  143. $result = performLargeUnary($stub, $fillUsername = true,
  144. $fillOauthScope = true);
  145. hardAssert($args['default_service_account'] === $result->getUsername(),
  146. 'invalid email returned');
  147. }
  148. /**
  149. * Run the jwt token credentials auth test.
  150. *
  151. * @param $stub Stub object that has service methods
  152. * @param $args array command line args
  153. */
  154. function jwtTokenCreds($stub, $args)
  155. {
  156. $jsonKey = json_decode(
  157. file_get_contents(getenv(CredentialsLoader::ENV_VAR)),
  158. true);
  159. $result = performLargeUnary($stub, $fillUsername = true,
  160. $fillOauthScope = true);
  161. hardAssert($result->getUsername() === $jsonKey['client_email'],
  162. 'invalid email returned');
  163. }
  164. /**
  165. * Run the oauth2_auth_token auth test.
  166. *
  167. * @param $stub Stub object that has service methods
  168. * @param $args array command line args
  169. */
  170. function oauth2AuthToken($stub, $args)
  171. {
  172. $jsonKey = json_decode(
  173. file_get_contents(getenv(CredentialsLoader::ENV_VAR)),
  174. true);
  175. $result = performLargeUnary($stub, $fillUsername = true,
  176. $fillOauthScope = true);
  177. hardAssert($result->getUsername() === $jsonKey['client_email'],
  178. 'invalid email returned');
  179. }
  180. function updateAuthMetadataCallback($context)
  181. {
  182. $authUri = $context->service_url;
  183. $methodName = $context->method_name;
  184. $auth_credentials = ApplicationDefaultCredentials::getCredentials();
  185. $metadata = [];
  186. $result = $auth_credentials->updateMetadata([], $authUri);
  187. foreach ($result as $key => $value) {
  188. $metadata[strtolower($key)] = $value;
  189. }
  190. return $metadata;
  191. }
  192. /**
  193. * Run the per_rpc_creds auth test.
  194. *
  195. * @param $stub Stub object that has service methods
  196. * @param $args array command line args
  197. */
  198. function perRpcCreds($stub, $args)
  199. {
  200. $jsonKey = json_decode(
  201. file_get_contents(getenv(CredentialsLoader::ENV_VAR)),
  202. true);
  203. $result = performLargeUnary($stub, $fillUsername = true,
  204. $fillOauthScope = true,
  205. 'updateAuthMetadataCallback');
  206. hardAssert($result->getUsername() === $jsonKey['client_email'],
  207. 'invalid email returned');
  208. }
  209. /**
  210. * Run the client_streaming test.
  211. *
  212. * @param $stub Stub object that has service methods
  213. */
  214. function clientStreaming($stub)
  215. {
  216. $request_lengths = [27182, 8, 1828, 45904];
  217. $requests = array_map(
  218. function ($length) {
  219. $request = new Grpc\Testing\StreamingInputCallRequest();
  220. $payload = new Grpc\Testing\Payload();
  221. $payload->setBody(str_repeat("\0", $length));
  222. $request->setPayload($payload);
  223. return $request;
  224. }, $request_lengths);
  225. $call = $stub->StreamingInputCall();
  226. foreach ($requests as $request) {
  227. $call->write($request);
  228. }
  229. list($result, $status) = $call->wait();
  230. hardAssertIfStatusOk($status);
  231. hardAssert($result->getAggregatedPayloadSize() === 74922,
  232. 'aggregated_payload_size was incorrect');
  233. }
  234. /**
  235. * Run the server_streaming test.
  236. *
  237. * @param $stub Stub object that has service methods.
  238. */
  239. function serverStreaming($stub)
  240. {
  241. $sizes = [31415, 9, 2653, 58979];
  242. $request = new Grpc\Testing\StreamingOutputCallRequest();
  243. $request->setResponseType(Grpc\Testing\PayloadType::COMPRESSABLE);
  244. foreach ($sizes as $size) {
  245. $response_parameters = new Grpc\Testing\ResponseParameters();
  246. $response_parameters->setSize($size);
  247. $request->getResponseParameters()[] = $response_parameters;
  248. }
  249. $call = $stub->StreamingOutputCall($request);
  250. $i = 0;
  251. foreach ($call->responses() as $value) {
  252. hardAssert($i < 4, 'Too many responses');
  253. $payload = $value->getPayload();
  254. hardAssert(
  255. $payload->getType() === Grpc\Testing\PayloadType::COMPRESSABLE,
  256. 'Payload '.$i.' had the wrong type');
  257. hardAssert(strlen($payload->getBody()) === $sizes[$i],
  258. 'Response '.$i.' had the wrong length');
  259. $i += 1;
  260. }
  261. hardAssertIfStatusOk($call->getStatus());
  262. }
  263. /**
  264. * Run the ping_pong test.
  265. *
  266. * @param $stub Stub object that has service methods.
  267. */
  268. function pingPong($stub)
  269. {
  270. $request_lengths = [27182, 8, 1828, 45904];
  271. $response_lengths = [31415, 9, 2653, 58979];
  272. $call = $stub->FullDuplexCall();
  273. for ($i = 0; $i < 4; ++$i) {
  274. $request = new Grpc\Testing\StreamingOutputCallRequest();
  275. $request->setResponseType(Grpc\Testing\PayloadType::COMPRESSABLE);
  276. $response_parameters = new Grpc\Testing\ResponseParameters();
  277. $response_parameters->setSize($response_lengths[$i]);
  278. $request->getResponseParameters()[] = $response_parameters;
  279. $payload = new Grpc\Testing\Payload();
  280. $payload->setBody(str_repeat("\0", $request_lengths[$i]));
  281. $request->setPayload($payload);
  282. $call->write($request);
  283. $response = $call->read();
  284. hardAssert($response !== null, 'Server returned too few responses');
  285. $payload = $response->getPayload();
  286. hardAssert(
  287. $payload->getType() === Grpc\Testing\PayloadType::COMPRESSABLE,
  288. 'Payload '.$i.' had the wrong type');
  289. hardAssert(strlen($payload->getBody()) === $response_lengths[$i],
  290. 'Payload '.$i.' had the wrong length');
  291. }
  292. $call->writesDone();
  293. hardAssert($call->read() === null, 'Server returned too many responses');
  294. hardAssertIfStatusOk($call->getStatus());
  295. }
  296. /**
  297. * Run the empty_stream test.
  298. *
  299. * @param $stub Stub object that has service methods.
  300. */
  301. function emptyStream($stub)
  302. {
  303. $call = $stub->FullDuplexCall();
  304. $call->writesDone();
  305. hardAssert($call->read() === null, 'Server returned too many responses');
  306. hardAssertIfStatusOk($call->getStatus());
  307. }
  308. /**
  309. * Run the cancel_after_begin test.
  310. *
  311. * @param $stub Stub object that has service methods.
  312. */
  313. function cancelAfterBegin($stub)
  314. {
  315. $call = $stub->StreamingInputCall();
  316. $call->cancel();
  317. list($result, $status) = $call->wait();
  318. hardAssert($status->code === Grpc\STATUS_CANCELLED,
  319. 'Call status was not CANCELLED');
  320. }
  321. /**
  322. * Run the cancel_after_first_response test.
  323. *
  324. * @param $stub Stub object that has service methods.
  325. */
  326. function cancelAfterFirstResponse($stub)
  327. {
  328. $call = $stub->FullDuplexCall();
  329. $request = new Grpc\Testing\StreamingOutputCallRequest();
  330. $request->setResponseType(Grpc\Testing\PayloadType::COMPRESSABLE);
  331. $response_parameters = new Grpc\Testing\ResponseParameters();
  332. $response_parameters->setSize(31415);
  333. $request->getResponseParameters()[] = $response_parameters;
  334. $payload = new Grpc\Testing\Payload();
  335. $payload->setBody(str_repeat("\0", 27182));
  336. $request->setPayload($payload);
  337. $call->write($request);
  338. $response = $call->read();
  339. $call->cancel();
  340. hardAssert($call->getStatus()->code === Grpc\STATUS_CANCELLED,
  341. 'Call status was not CANCELLED');
  342. }
  343. function timeoutOnSleepingServer($stub)
  344. {
  345. $call = $stub->FullDuplexCall([], ['timeout' => 1000]);
  346. $request = new Grpc\Testing\StreamingOutputCallRequest();
  347. $request->setResponseType(Grpc\Testing\PayloadType::COMPRESSABLE);
  348. $response_parameters = new Grpc\Testing\ResponseParameters();
  349. $response_parameters->setSize(8);
  350. $request->getResponseParameters()[] = $response_parameters;
  351. $payload = new Grpc\Testing\Payload();
  352. $payload->setBody(str_repeat("\0", 9));
  353. $request->setPayload($payload);
  354. $call->write($request);
  355. $response = $call->read();
  356. hardAssert($call->getStatus()->code === Grpc\STATUS_DEADLINE_EXCEEDED,
  357. 'Call status was not DEADLINE_EXCEEDED');
  358. }
  359. function customMetadata($stub)
  360. {
  361. $ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial';
  362. $ECHO_INITIAL_VALUE = 'test_initial_metadata_value';
  363. $ECHO_TRAILING_KEY = 'x-grpc-test-echo-trailing-bin';
  364. $ECHO_TRAILING_VALUE = 'ababab';
  365. $request_len = 271828;
  366. $response_len = 314159;
  367. $request = new Grpc\Testing\SimpleRequest();
  368. $request->setResponseType(Grpc\Testing\PayloadType::COMPRESSABLE);
  369. $request->setResponseSize($response_len);
  370. $payload = new Grpc\Testing\Payload();
  371. $payload->setType(Grpc\Testing\PayloadType::COMPRESSABLE);
  372. $payload->setBody(str_repeat("\0", $request_len));
  373. $request->setPayload($payload);
  374. $metadata = [
  375. $ECHO_INITIAL_KEY => [$ECHO_INITIAL_VALUE],
  376. $ECHO_TRAILING_KEY => [$ECHO_TRAILING_VALUE],
  377. ];
  378. $call = $stub->UnaryCall($request, $metadata);
  379. $initial_metadata = $call->getMetadata();
  380. hardAssert(array_key_exists($ECHO_INITIAL_KEY, $initial_metadata),
  381. 'Initial metadata does not contain expected key');
  382. hardAssert(
  383. $initial_metadata[$ECHO_INITIAL_KEY][0] === $ECHO_INITIAL_VALUE,
  384. 'Incorrect initial metadata value');
  385. list($result, $status) = $call->wait();
  386. hardAssertIfStatusOk($status);
  387. $trailing_metadata = $call->getTrailingMetadata();
  388. hardAssert(array_key_exists($ECHO_TRAILING_KEY, $trailing_metadata),
  389. 'Trailing metadata does not contain expected key');
  390. hardAssert(
  391. $trailing_metadata[$ECHO_TRAILING_KEY][0] === $ECHO_TRAILING_VALUE,
  392. 'Incorrect trailing metadata value');
  393. $streaming_call = $stub->FullDuplexCall($metadata);
  394. $streaming_request = new Grpc\Testing\StreamingOutputCallRequest();
  395. $streaming_request->setPayload($payload);
  396. $response_parameters = new Grpc\Testing\ResponseParameters();
  397. $response_parameters->setSize($response_len);
  398. $streaming_request->getResponseParameters()[] = $response_parameters;
  399. $streaming_call->write($streaming_request);
  400. $streaming_call->writesDone();
  401. $result = $streaming_call->read();
  402. hardAssertIfStatusOk($streaming_call->getStatus());
  403. $streaming_initial_metadata = $streaming_call->getMetadata();
  404. hardAssert(array_key_exists($ECHO_INITIAL_KEY, $streaming_initial_metadata),
  405. 'Initial metadata does not contain expected key');
  406. hardAssert(
  407. $streaming_initial_metadata[$ECHO_INITIAL_KEY][0] === $ECHO_INITIAL_VALUE,
  408. 'Incorrect initial metadata value');
  409. $streaming_trailing_metadata = $streaming_call->getTrailingMetadata();
  410. hardAssert(array_key_exists($ECHO_TRAILING_KEY,
  411. $streaming_trailing_metadata),
  412. 'Trailing metadata does not contain expected key');
  413. hardAssert($streaming_trailing_metadata[$ECHO_TRAILING_KEY][0] ===
  414. $ECHO_TRAILING_VALUE, 'Incorrect trailing metadata value');
  415. }
  416. function statusCodeAndMessage($stub)
  417. {
  418. $echo_status = new Grpc\Testing\EchoStatus();
  419. $echo_status->setCode(2);
  420. $echo_status->setMessage('test status message');
  421. $request = new Grpc\Testing\SimpleRequest();
  422. $request->setResponseStatus($echo_status);
  423. $call = $stub->UnaryCall($request);
  424. list($result, $status) = $call->wait();
  425. hardAssert($status->code === 2,
  426. 'Received unexpected UnaryCall status code: '.
  427. $status->code);
  428. hardAssert($status->details === 'test status message',
  429. 'Received unexpected UnaryCall status details: '.
  430. $status->details);
  431. $streaming_call = $stub->FullDuplexCall();
  432. $streaming_request = new Grpc\Testing\StreamingOutputCallRequest();
  433. $streaming_request->setResponseStatus($echo_status);
  434. $streaming_call->write($streaming_request);
  435. $streaming_call->writesDone();
  436. $result = $streaming_call->read();
  437. $status = $streaming_call->getStatus();
  438. hardAssert($status->code === 2,
  439. 'Received unexpected FullDuplexCall status code: '.
  440. $status->code);
  441. hardAssert($status->details === 'test status message',
  442. 'Received unexpected FullDuplexCall status details: '.
  443. $status->details);
  444. }
  445. # NOTE: the stub input to this function is from UnimplementedService
  446. function unimplementedService($stub)
  447. {
  448. $call = $stub->UnimplementedCall(new Grpc\Testing\EmptyMessage());
  449. list($result, $status) = $call->wait();
  450. hardAssert($status->code === Grpc\STATUS_UNIMPLEMENTED,
  451. 'Received unexpected status code');
  452. }
  453. # NOTE: the stub input to this function is from TestService
  454. function unimplementedMethod($stub)
  455. {
  456. $call = $stub->UnimplementedCall(new Grpc\Testing\EmptyMessage());
  457. list($result, $status) = $call->wait();
  458. hardAssert($status->code === Grpc\STATUS_UNIMPLEMENTED,
  459. 'Received unexpected status code');
  460. }
  461. function _makeStub($args)
  462. {
  463. if (!array_key_exists('server_host', $args)) {
  464. throw new Exception('Missing argument: --server_host is required');
  465. }
  466. if (!array_key_exists('server_port', $args)) {
  467. throw new Exception('Missing argument: --server_port is required');
  468. }
  469. if (!array_key_exists('test_case', $args)) {
  470. throw new Exception('Missing argument: --test_case is required');
  471. }
  472. if ($args['server_port'] === 443) {
  473. $server_address = $args['server_host'];
  474. } else {
  475. $server_address = $args['server_host'].':'.$args['server_port'];
  476. }
  477. $test_case = $args['test_case'];
  478. $host_override = 'foo.test.google.fr';
  479. if (array_key_exists('server_host_override', $args)) {
  480. $host_override = $args['server_host_override'];
  481. }
  482. $use_tls = false;
  483. if (array_key_exists('use_tls', $args) &&
  484. $args['use_tls'] != 'false') {
  485. $use_tls = true;
  486. }
  487. $use_test_ca = false;
  488. if (array_key_exists('use_test_ca', $args) &&
  489. $args['use_test_ca'] != 'false') {
  490. $use_test_ca = true;
  491. }
  492. $opts = [];
  493. if ($use_tls) {
  494. if ($use_test_ca) {
  495. $ssl_credentials = Grpc\ChannelCredentials::createSsl(
  496. file_get_contents(dirname(__FILE__).'/../data/ca.pem'));
  497. } else {
  498. $ssl_credentials = Grpc\ChannelCredentials::createSsl();
  499. }
  500. $opts['credentials'] = $ssl_credentials;
  501. $opts['grpc.ssl_target_name_override'] = $host_override;
  502. } else {
  503. $opts['credentials'] = Grpc\ChannelCredentials::createInsecure();
  504. }
  505. if (in_array($test_case, ['service_account_creds',
  506. 'compute_engine_creds', 'jwt_token_creds', ])) {
  507. if ($test_case === 'jwt_token_creds') {
  508. $auth_credentials = ApplicationDefaultCredentials::getCredentials();
  509. } else {
  510. $auth_credentials = ApplicationDefaultCredentials::getCredentials(
  511. $args['oauth_scope']
  512. );
  513. }
  514. $opts['update_metadata'] = $auth_credentials->getUpdateMetadataFunc();
  515. }
  516. if ($test_case === 'oauth2_auth_token') {
  517. $auth_credentials = ApplicationDefaultCredentials::getCredentials(
  518. $args['oauth_scope']
  519. );
  520. $token = $auth_credentials->fetchAuthToken();
  521. $update_metadata =
  522. function ($metadata,
  523. $authUri = null,
  524. ClientInterface $client = null) use ($token) {
  525. $metadata_copy = $metadata;
  526. $metadata_copy[CredentialsLoader::AUTH_METADATA_KEY] =
  527. [sprintf('%s %s',
  528. $token['token_type'],
  529. $token['access_token'])];
  530. return $metadata_copy;
  531. };
  532. $opts['update_metadata'] = $update_metadata;
  533. }
  534. if ($test_case === 'unimplemented_service') {
  535. $stub = new Grpc\Testing\UnimplementedServiceClient($server_address,
  536. $opts);
  537. } else {
  538. $stub = new Grpc\Testing\TestServiceClient($server_address, $opts);
  539. }
  540. return $stub;
  541. }
  542. function interop_main($args, $stub = false)
  543. {
  544. if (!$stub) {
  545. $stub = _makeStub($args);
  546. }
  547. $test_case = $args['test_case'];
  548. echo "Running test case $test_case\n";
  549. switch ($test_case) {
  550. case 'empty_unary':
  551. emptyUnary($stub);
  552. break;
  553. case 'large_unary':
  554. largeUnary($stub);
  555. break;
  556. case 'client_streaming':
  557. clientStreaming($stub);
  558. break;
  559. case 'server_streaming':
  560. serverStreaming($stub);
  561. break;
  562. case 'ping_pong':
  563. pingPong($stub);
  564. break;
  565. case 'empty_stream':
  566. emptyStream($stub);
  567. break;
  568. case 'cancel_after_begin':
  569. cancelAfterBegin($stub);
  570. break;
  571. case 'cancel_after_first_response':
  572. cancelAfterFirstResponse($stub);
  573. break;
  574. case 'timeout_on_sleeping_server':
  575. timeoutOnSleepingServer($stub);
  576. break;
  577. case 'custom_metadata':
  578. customMetadata($stub);
  579. break;
  580. case 'status_code_and_message':
  581. statusCodeAndMessage($stub);
  582. break;
  583. case 'unimplemented_service':
  584. unimplementedService($stub);
  585. break;
  586. case 'unimplemented_method':
  587. unimplementedMethod($stub);
  588. break;
  589. case 'service_account_creds':
  590. serviceAccountCreds($stub, $args);
  591. break;
  592. case 'compute_engine_creds':
  593. computeEngineCreds($stub, $args);
  594. break;
  595. case 'jwt_token_creds':
  596. jwtTokenCreds($stub, $args);
  597. break;
  598. case 'oauth2_auth_token':
  599. oauth2AuthToken($stub, $args);
  600. break;
  601. case 'per_rpc_creds':
  602. perRpcCreds($stub, $args);
  603. break;
  604. default:
  605. echo "Unsupported test case $test_case\n";
  606. exit(1);
  607. }
  608. return $stub;
  609. }
  610. if (isset($_SERVER['PHP_SELF']) &&
  611. preg_match('/interop_client/', $_SERVER['PHP_SELF'])) {
  612. $args = getopt('', ['server_host:', 'server_port:', 'test_case:',
  613. 'use_tls::', 'use_test_ca::',
  614. 'server_host_override:', 'oauth_scope:',
  615. 'default_service_account:', ]);
  616. interop_main($args);
  617. }