From cccf9295e6e6d2da8c8c283b5c1ee0001975e525 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Fri, 12 Feb 2016 16:37:19 -0800 Subject: [PATCH 1/3] php: add more unit tests to improve code coverage --- src/php/ext/grpc/call.c | 12 +- src/php/ext/grpc/channel.c | 52 ++- .../tests/unit_tests/CallCredentials3Test.php | 136 +++++++ .../tests/unit_tests/CallCredentialsTest.php | 51 ++- src/php/tests/unit_tests/CallTest.php | 28 ++ .../unit_tests/ChannelCredentialsTest.php | 73 ++++ src/php/tests/unit_tests/ChannelTest.php | 82 +++++ src/php/tests/unit_tests/EndToEndTest.php | 344 ++++++++++++++++++ src/php/tests/unit_tests/ServerTest.php | 71 ++++ src/php/tests/unit_tests/TimevalTest.php | 65 ++++ 10 files changed, 870 insertions(+), 44 deletions(-) create mode 100644 src/php/tests/unit_tests/CallCredentials3Test.php create mode 100644 src/php/tests/unit_tests/ChannelCredentialsTest.php create mode 100644 src/php/tests/unit_tests/ChannelTest.php create mode 100644 src/php/tests/unit_tests/ServerTest.php diff --git a/src/php/ext/grpc/call.c b/src/php/ext/grpc/call.c index 7ba14a38d8e..4b4336666cb 100644 --- a/src/php/ext/grpc/call.c +++ b/src/php/ext/grpc/call.c @@ -125,15 +125,9 @@ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array) { memcpy(str_val, elem->value, elem->value_length); if (zend_hash_find(array_hash, str_key, key_len, (void **)data) == SUCCESS) { - if (Z_TYPE_P(*data) != IS_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; - } - add_next_index_stringl(*data, str_val, elem->value_length, false); + zend_throw_exception(zend_exception_get_default(), + "Metadata hash somehow contains wrong types.", + 1 TSRMLS_CC); } else { MAKE_STD_ZVAL(inner_array); array_init(inner_array); diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index 60c94412dce..97e8cc1a63d 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -148,37 +148,33 @@ PHP_METHOD(Channel, __construct) { "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 00000000000..6d98815d16c --- /dev/null +++ b/src/php/tests/unit_tests/CallCredentials3Test.php @@ -0,0 +1,136 @@ +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 09184127816..5fec06cd133 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 3b697b50c3b..1170a440fa9 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 00000000000..1a42d69428b --- /dev/null +++ b/src/php/tests/unit_tests/ChannelCredentialsTest.php @@ -0,0 +1,73 @@ +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 00000000000..b6eac3109a8 --- /dev/null +++ b/src/php/tests/unit_tests/ChannelTest.php @@ -0,0 +1,82 @@ +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 5a38262451b..3fa92c950ba 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 00000000000..d18f9abe9bf --- /dev/null +++ b/src/php/tests/unit_tests/ServerTest.php @@ -0,0 +1,71 @@ +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 1d2a8d303e7..43abba126ab 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); + } + } From b91f0f27c5cfd92b5f9358fe54f556fec6005d22 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Tue, 16 Feb 2016 09:36:36 -0800 Subject: [PATCH 2/3] revert unnecessary change --- src/php/ext/grpc/call.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/php/ext/grpc/call.c b/src/php/ext/grpc/call.c index 4b4336666cb..024ab70571b 100644 --- a/src/php/ext/grpc/call.c +++ b/src/php/ext/grpc/call.c @@ -125,9 +125,15 @@ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array) { memcpy(str_val, elem->value, elem->value_length); if (zend_hash_find(array_hash, str_key, key_len, (void **)data) == SUCCESS) { - zend_throw_exception(zend_exception_get_default(), - "Metadata hash somehow contains wrong types.", - 1 TSRMLS_CC); + if (Z_TYPE_P(*data) != IS_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; + } + add_next_index_stringl(*data, str_val, elem->value_length, false); } else { MAKE_STD_ZVAL(inner_array); array_init(inner_array); From f77a4ad9f7d0e84ee418165c4c9a604ea89780f9 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Tue, 16 Feb 2016 09:45:51 -0800 Subject: [PATCH 3/3] channel constructor the array is required --- src/php/ext/grpc/channel.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index 97e8cc1a63d..2c64b9122c3 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -141,8 +141,8 @@ 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);