diff options
author | Michael Lumish <mlumish@google.com> | 2016-02-16 11:43:35 -0800 |
---|---|---|
committer | Michael Lumish <mlumish@google.com> | 2016-02-16 11:43:35 -0800 |
commit | 221bb1dae0feb7801dacd4f2c32045a2e15651d9 (patch) | |
tree | 6cf0281717b129a91ad385f44e7359ef952f0aec /src/php | |
parent | e683814416a724d1e110180ddf13848a028f1a34 (diff) | |
parent | f77a4ad9f7d0e84ee418165c4c9a604ea89780f9 (diff) |
Merge pull request #5225 from stanley-cheung/php-ext-code-coverage
PHP: add unit test to increase PHP C extension code coverage
Diffstat (limited to 'src/php')
-rw-r--r-- | src/php/ext/grpc/call.c | 6 | ||||
-rw-r--r-- | src/php/ext/grpc/channel.c | 56 | ||||
-rw-r--r-- | src/php/tests/unit_tests/CallCredentials3Test.php | 136 | ||||
-rw-r--r-- | src/php/tests/unit_tests/CallCredentialsTest.php | 51 | ||||
-rwxr-xr-x | src/php/tests/unit_tests/CallTest.php | 28 | ||||
-rw-r--r-- | src/php/tests/unit_tests/ChannelCredentialsTest.php | 73 | ||||
-rw-r--r-- | src/php/tests/unit_tests/ChannelTest.php | 82 | ||||
-rwxr-xr-x | src/php/tests/unit_tests/EndToEndTest.php | 344 | ||||
-rw-r--r-- | src/php/tests/unit_tests/ServerTest.php | 71 | ||||
-rwxr-xr-x | src/php/tests/unit_tests/TimevalTest.php | 65 |
10 files changed, 872 insertions, 40 deletions
diff --git a/src/php/ext/grpc/call.c b/src/php/ext/grpc/call.c index 7ba14a38d8..024ab70571 100644 --- a/src/php/ext/grpc/call.c +++ b/src/php/ext/grpc/call.c @@ -129,9 +129,9 @@ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array) { zend_throw_exception(zend_exception_get_default(), "Metadata hash somehow contains wrong types.", 1 TSRMLS_CC); - efree(str_key); - efree(str_val); - return NULL; + efree(str_key); + efree(str_val); + return NULL; } add_next_index_stringl(*data, str_val, elem->value_length, false); } else { diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index 60c94412dc..2c64b9122c 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -141,44 +141,40 @@ PHP_METHOD(Channel, __construct) { HashTable *array_hash; zval **creds_obj = NULL; wrapped_grpc_channel_credentials *creds = NULL; - /* "s|a" == 1 string, 1 optional array */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &target, + /* "sa" == 1 string, 1 array */ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &target, &target_length, &args_array) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "Channel expects a string and an array", 1 TSRMLS_CC); return; } - if (args_array == NULL) { - channel->wrapped = grpc_insecure_channel_create(target, NULL, NULL); - } else { - array_hash = Z_ARRVAL_P(args_array); - if (zend_hash_find(array_hash, "credentials", sizeof("credentials"), - (void **)&creds_obj) == SUCCESS) { - if (Z_TYPE_P(*creds_obj) == IS_NULL) { - creds = NULL; - zend_hash_del(array_hash, "credentials", 12); - } else if (zend_get_class_entry(*creds_obj TSRMLS_CC) != - grpc_ce_channel_credentials) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "credentials must be a ChannelCredentials object", - 1 TSRMLS_CC); - return; - } else { - creds = (wrapped_grpc_channel_credentials *)zend_object_store_get_object( - *creds_obj TSRMLS_CC); - zend_hash_del(array_hash, "credentials", 12); - } - } - php_grpc_read_args_array(args_array, &args); - if (creds == NULL) { - channel->wrapped = grpc_insecure_channel_create(target, &args, NULL); + array_hash = Z_ARRVAL_P(args_array); + if (zend_hash_find(array_hash, "credentials", sizeof("credentials"), + (void **)&creds_obj) == SUCCESS) { + if (Z_TYPE_P(*creds_obj) == IS_NULL) { + creds = NULL; + zend_hash_del(array_hash, "credentials", 12); + } else if (zend_get_class_entry(*creds_obj TSRMLS_CC) != + grpc_ce_channel_credentials) { + zend_throw_exception(spl_ce_InvalidArgumentException, + "credentials must be a ChannelCredentials object", + 1 TSRMLS_CC); + return; } else { - gpr_log(GPR_DEBUG, "Initialized secure channel"); - channel->wrapped = - grpc_secure_channel_create(creds->wrapped, target, &args, NULL); + creds = (wrapped_grpc_channel_credentials *)zend_object_store_get_object( + *creds_obj TSRMLS_CC); + zend_hash_del(array_hash, "credentials", 12); } - efree(args.args); } + php_grpc_read_args_array(args_array, &args); + if (creds == NULL) { + channel->wrapped = grpc_insecure_channel_create(target, &args, NULL); + } else { + gpr_log(GPR_DEBUG, "Initialized secure channel"); + channel->wrapped = + grpc_secure_channel_create(creds->wrapped, target, &args, NULL); + } + efree(args.args); } /** diff --git a/src/php/tests/unit_tests/CallCredentials3Test.php b/src/php/tests/unit_tests/CallCredentials3Test.php new file mode 100644 index 0000000000..6d98815d16 --- /dev/null +++ b/src/php/tests/unit_tests/CallCredentials3Test.php @@ -0,0 +1,136 @@ +<?php +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +class CallCredentials3Test extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + $this->credentials = Grpc\ChannelCredentials::createSsl( + file_get_contents(dirname(__FILE__).'/../data/ca.pem')); + $server_credentials = Grpc\ServerCredentials::createSsl( + null, + file_get_contents(dirname(__FILE__).'/../data/server1.key'), + file_get_contents(dirname(__FILE__).'/../data/server1.pem')); + $this->server = new Grpc\Server(); + $this->port = $this->server->addSecureHttp2Port('0.0.0.0:0', + $server_credentials); + $this->server->start(); + $this->host_override = 'foo.test.google.fr'; + $this->channel = new Grpc\Channel( + 'localhost:'.$this->port, + [ + 'grpc.ssl_target_name_override' => $this->host_override, + 'grpc.default_authority' => $this->host_override, + 'credentials' => $this->credentials, + ] + ); + } + + public function tearDown() + { + unset($this->channel); + unset($this->server); + } + + public function callbackFunc($context) + { + $this->assertTrue(is_string($context->service_url)); + $this->assertTrue(is_string($context->method_name)); + + return ['k1' => ['v1'], 'k2' => ['v2']]; + } + + public function testCreateFromPlugin() + { + $deadline = Grpc\Timeval::infFuture(); + $status_text = 'xyz'; + $call = new Grpc\Call($this->channel, + '/abc/dummy_method', + $deadline, + $this->host_override); + + $call_credentials = Grpc\CallCredentials::createFromPlugin( + [$this, 'callbackFunc']); + $call->setCredentials($call_credentials); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + + $event = $this->server->requestCall(); + + $this->assertTrue(is_array($event->metadata)); + $metadata = $event->metadata; + $this->assertTrue(array_key_exists('k1', $metadata)); + $this->assertTrue(array_key_exists('k2', $metadata)); + $this->assertSame($metadata['k1'], ['v1']); + $this->assertSame($metadata['k2'], ['v2']); + + $this->assertSame('/abc/dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text, + ], + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_status); + $this->assertFalse($event->cancelled); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true, + ]); + + $this->assertSame([], $event->metadata); + $status = $event->status; + $this->assertSame([], $status->metadata); + $this->assertSame(Grpc\STATUS_OK, $status->code); + $this->assertSame($status_text, $status->details); + + unset($call); + unset($server_call); + } + +} diff --git a/src/php/tests/unit_tests/CallCredentialsTest.php b/src/php/tests/unit_tests/CallCredentialsTest.php index 0918412781..5fec06cd13 100644 --- a/src/php/tests/unit_tests/CallCredentialsTest.php +++ b/src/php/tests/unit_tests/CallCredentialsTest.php @@ -36,13 +36,13 @@ class CallCredentialsTest extends PHPUnit_Framework_TestCase { public function setUp() { - $credentials = Grpc\ChannelCredentials::createSsl( + $this->credentials = Grpc\ChannelCredentials::createSsl( file_get_contents(dirname(__FILE__).'/../data/ca.pem')); - $call_credentials = Grpc\CallCredentials::createFromPlugin( - array($this, 'callbackFunc')); - $credentials = Grpc\ChannelCredentials::createComposite( - $credentials, - $call_credentials + $this->call_credentials = Grpc\CallCredentials::createFromPlugin( + [$this, 'callbackFunc']); + $this->credentials = Grpc\ChannelCredentials::createComposite( + $this->credentials, + $this->call_credentials ); $server_credentials = Grpc\ServerCredentials::createSsl( null, @@ -58,7 +58,7 @@ class CallCredentialsTest extends PHPUnit_Framework_TestCase [ 'grpc.ssl_target_name_override' => $this->host_override, 'grpc.default_authority' => $this->host_override, - 'credentials' => $credentials, + 'credentials' => $this->credentials, ] ); } @@ -134,4 +134,41 @@ class CallCredentialsTest extends PHPUnit_Framework_TestCase unset($call); unset($server_call); } + + public function callbackFunc2($context) + { + return []; + } + + public function testCreateComposite() + { + $call_credentials2 = Grpc\CallCredentials::createFromPlugin( + [$this, 'callbackFunc2']); + $call_credentials3 = Grpc\CallCredentials::createComposite( + $this->call_credentials, + $call_credentials2 + ); + $this->assertSame('Grpc\CallCredentials', get_class($call_credentials3)); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testCreateFromPluginInvalidParam() + { + $call_credentials = Grpc\CallCredentials::createFromPlugin( + 'callbackFunc' + ); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testCreateCompositeInvalidParam() + { + $call_credentials3 = Grpc\CallCredentials::createComposite( + $this->call_credentials, + $this->credentials + ); + } } diff --git a/src/php/tests/unit_tests/CallTest.php b/src/php/tests/unit_tests/CallTest.php index 3b697b50c3..1170a440fa 100755 --- a/src/php/tests/unit_tests/CallTest.php +++ b/src/php/tests/unit_tests/CallTest.php @@ -91,4 +91,32 @@ class CallTest extends PHPUnit_Framework_TestCase { $this->assertTrue(is_string($this->call->getPeer())); } + + public function testCancel() + { + $this->assertNull($this->call->cancel()); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidMetadataKey() + { + $batch = [ + 'invalid' => ['key1' => 'value1'], + ]; + $result = $this->call->startBatch($batch); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidMetadataInnerValue() + { + $batch = [ + Grpc\OP_SEND_INITIAL_METADATA => ['key1' => 'value1'], + ]; + $result = $this->call->startBatch($batch); + } + } diff --git a/src/php/tests/unit_tests/ChannelCredentialsTest.php b/src/php/tests/unit_tests/ChannelCredentialsTest.php new file mode 100644 index 0000000000..1a42d69428 --- /dev/null +++ b/src/php/tests/unit_tests/ChannelCredentialsTest.php @@ -0,0 +1,73 @@ +<?php +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +class ChanellCredentialsTest extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + } + + public function tearDown() + { + } + + public function testCreateDefault() + { + $channel_credentials = Grpc\ChannelCredentials::createDefault(); + $this->assertSame('Grpc\ChannelCredentials', get_class($channel_credentials)); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidCreateSsl() + { + $channel_credentials = Grpc\ChannelCredentials::createSsl([]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidCreateComposite() + { + $channel_credentials = Grpc\ChannelCredentials::createComposite( + 'something', 'something'); + } + + public function testCreateInsecure() + { + $channel_credentials = Grpc\ChannelCredentials::createInsecure(); + $this->assertNull($channel_credentials); + } +}
\ No newline at end of file diff --git a/src/php/tests/unit_tests/ChannelTest.php b/src/php/tests/unit_tests/ChannelTest.php new file mode 100644 index 0000000000..b6eac3109a --- /dev/null +++ b/src/php/tests/unit_tests/ChannelTest.php @@ -0,0 +1,82 @@ +<?php +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +class ChannelTest extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + } + + public function tearDown() + { + } + + public function testInsecureCredentials() + { + $this->channel = new Grpc\Channel( + 'localhost:0', + [ + 'credentials' => Grpc\ChannelCredentials::createInsecure(), + ] + ); + $this->assertSame('Grpc\Channel', get_class($this->channel)); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidCredentials() + { + $this->channel = new Grpc\Channel( + 'localhost:0', + [ + 'credentials' => new Grpc\Timeval(100), + ] + ); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidOptionsArray() + { + $this->channel = new Grpc\Channel( + 'localhost:0', + [ + 'abc' => [], + ] + ); + } + +}
\ No newline at end of file diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php index 5a38262451..3fa92c950b 100755 --- a/src/php/tests/unit_tests/EndToEndTest.php +++ b/src/php/tests/unit_tests/EndToEndTest.php @@ -201,6 +201,318 @@ class EndToEndTest extends PHPUnit_Framework_TestCase unset($server_call); } + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidClientMessageArray() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => 'invalid', + ]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidClientMessageString() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => 0], + ]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidClientMessageFlags() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => 'abc', + 'flags' => 'invalid'], + ]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidServerStatusMetadata() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => $req_text], + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + $this->assertTrue($event->send_message); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => 'invalid', + 'code' => Grpc\STATUS_OK, + 'details' => $status_text, + ], + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidServerStatusCode() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => $req_text], + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + $this->assertTrue($event->send_message); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => 'invalid', + 'details' => $status_text, + ], + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testMissingServerStatusCode() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => $req_text], + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + $this->assertTrue($event->send_message); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'details' => $status_text, + ], + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidServerStatusDetails() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => $req_text], + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + $this->assertTrue($event->send_message); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => 0, + ], + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testMissingServerStatusDetails() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => $req_text], + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + $this->assertTrue($event->send_message); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + ], + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidStartBatchKey() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + 9999999 => [], + ]); + } + + /** + * @expectedException LogicException + */ + public function testInvalidStartBatch() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => $req_text], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => 'abc', + ], + ]); + } + public function testGetTarget() { $this->assertTrue(is_string($this->channel->getTarget())); @@ -255,4 +567,36 @@ class EndToEndTest extends PHPUnit_Framework_TestCase $new_state = $this->channel->getConnectivityState(); $this->assertTrue($new_state == Grpc\CHANNEL_IDLE); } + + /** + * @expectedException InvalidArgumentException + */ + public function testGetConnectivityStateInvalidParam() + { + $this->assertTrue($this->channel->getConnectivityState( + new Grpc\Timeval)); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testWatchConnectivityStateInvalidParam() + { + $this->assertTrue($this->channel->watchConnectivityState( + 0, 1000)); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testChannelConstructorInvalidParam() + { + $this->channel = new Grpc\Channel('localhost:'.$this->port, NULL); + } + + public function testClose() + { + $this->assertNull($this->channel->close()); + } + } diff --git a/src/php/tests/unit_tests/ServerTest.php b/src/php/tests/unit_tests/ServerTest.php new file mode 100644 index 0000000000..d18f9abe9b --- /dev/null +++ b/src/php/tests/unit_tests/ServerTest.php @@ -0,0 +1,71 @@ +<?php +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +class ServerTest extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + } + + public function tearDown() + { + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidConstructor() + { + $server = new Grpc\Server('invalid_host'); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidAddHttp2Port() + { + $this->server = new Grpc\Server([]); + $this->port = $this->server->addHttp2Port(['0.0.0.0:0']); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidAddSecureHttp2Port() + { + $this->server = new Grpc\Server([]); + $this->port = $this->server->addSecureHttp2Port(['0.0.0.0:0']); + } + +}
\ No newline at end of file diff --git a/src/php/tests/unit_tests/TimevalTest.php b/src/php/tests/unit_tests/TimevalTest.php index 1d2a8d303e..43abba126a 100755 --- a/src/php/tests/unit_tests/TimevalTest.php +++ b/src/php/tests/unit_tests/TimevalTest.php @@ -91,4 +91,69 @@ class TimevalTest extends PHPUnit_Framework_TestCase $back_to_now = $deadline->subtract($delta); $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); } + + public function testSimilar() + { + $a = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(1000); + $b = $a->add($delta); + $thresh = new Grpc\Timeval(1100); + $this->assertTrue(Grpc\Timeval::similar($a, $b, $thresh)); + $thresh = new Grpc\Timeval(900); + $this->assertFalse(Grpc\Timeval::similar($a, $b, $thresh)); + } + + public function testSleepUntil() + { + $curr_microtime = microtime(true); + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(1000); + $deadline = $now->add($delta); + $deadline->sleepUntil(); + $done_microtime = microtime(true); + $this->assertTrue(($done_microtime - $curr_microtime) > 0.0009); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testConstructorInvalidParam() + { + $delta = new Grpc\Timeval('abc'); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testAddInvalidParam() + { + $a = Grpc\Timeval::now(); + $a->add(1000); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testSubtractInvalidParam() + { + $a = Grpc\Timeval::now(); + $a->subtract(1000); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testCompareInvalidParam() + { + $a = Grpc\Timeval::compare(1000, 1100); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testSimilarInvalidParam() + { + $a = Grpc\Timeval::similar(1000, 1100, 1200); + } + } |