From 185e80373ad4ae36e31d66b9f6b92b50c4d8841d Mon Sep 17 00:00:00 2001 From: root Date: Tue, 2 Feb 2021 17:11:29 +0000 Subject: [PATCH] changing ChannelCredentials::createInsecure() to return grpc_insecure_credentials_create() instead of NULL --- src/php/ext/grpc/channel_credentials.c | 28 +++++++++---------- .../unit_tests/ChannelCredentialsTest.php | 2 +- src/php/tests/unit_tests/ChannelTest.php | 10 +++++++ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/php/ext/grpc/channel_credentials.c b/src/php/ext/grpc/channel_credentials.c index a254696b6e4..43080661cd9 100644 --- a/src/php/ext/grpc/channel_credentials.c +++ b/src/php/ext/grpc/channel_credentials.c @@ -233,12 +233,15 @@ PHP_METHOD(ChannelCredentials, createComposite) { * @return null */ PHP_METHOD(ChannelCredentials, createInsecure) { - RETURN_NULL(); + grpc_channel_credentials* creds = grpc_insecure_credentials_create(); + zval* creds_object = grpc_php_wrap_channel_credentials( + creds, strdup("INSECURE"), false TSRMLS_CC); + RETURN_DESTROY_ZVAL(creds_object); } /** * Create XDS channel credentials - * @param ChannelCredentials|null $fallback_creds The fallback credentials used + * @param ChannelCredentials $fallback_creds The fallback credentials used * if the channel target does not have the 'xds:///' scheme or if the xDS * control plane does not provide information on how to fetch credentials * dynamically. @@ -248,25 +251,20 @@ PHP_METHOD(ChannelCredentials, createXds) { grpc_channel_credentials* xds_creds = NULL; zval* fallback_creds = NULL; if (zend_parse_parameters_ex(0, // ZEND_PARSE_PARAMS_QUIET, - ZEND_NUM_ARGS() TSRMLS_CC, "O!", &fallback_creds, + ZEND_NUM_ARGS() TSRMLS_CC, "O", &fallback_creds, grpc_ce_channel_credentials) != SUCCESS) { zend_throw_exception(spl_ce_InvalidArgumentException, "createXds expects a fallback credentials", 1 TSRMLS_CC); return; } - char* fallback_creds_hash_str = ""; - if (!fallback_creds) { - grpc_channel_credentials* insecure_creds = - grpc_insecure_credentials_create(); - xds_creds = grpc_xds_credentials_create(insecure_creds); - } else { - wrapped_grpc_channel_credentials* wrapped_fallback_creds = - PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel_credentials, - fallback_creds); - xds_creds = grpc_xds_credentials_create(wrapped_fallback_creds->wrapped); - fallback_creds_hash_str = wrapped_fallback_creds->hashstr; - } + + wrapped_grpc_channel_credentials* wrapped_fallback_creds = + PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel_credentials, + fallback_creds); + xds_creds = grpc_xds_credentials_create(wrapped_fallback_creds->wrapped); + const char* fallback_creds_hash_str = + wrapped_fallback_creds->hashstr ? wrapped_fallback_creds->hashstr : ""; // prefix "XDS:" as the hash of the xDS channel char* hash_str = malloc(strlen(fallback_creds_hash_str) + strlen("XDS:") + 1); diff --git a/src/php/tests/unit_tests/ChannelCredentialsTest.php b/src/php/tests/unit_tests/ChannelCredentialsTest.php index ed61f3e3a43..a10b5b81ef1 100644 --- a/src/php/tests/unit_tests/ChannelCredentialsTest.php +++ b/src/php/tests/unit_tests/ChannelCredentialsTest.php @@ -43,7 +43,7 @@ class ChanellCredentialsTest extends \PHPUnit\Framework\TestCase public function testCreateInsecure() { $channel_credentials = Grpc\ChannelCredentials::createInsecure(); - $this->assertNull($channel_credentials); + $this->assertNotNull($channel_credentials); } public function testDefaultRootsPem() diff --git a/src/php/tests/unit_tests/ChannelTest.php b/src/php/tests/unit_tests/ChannelTest.php index 5d7b5b4a7cc..a9341060d0e 100644 --- a/src/php/tests/unit_tests/ChannelTest.php +++ b/src/php/tests/unit_tests/ChannelTest.php @@ -59,6 +59,16 @@ class ChannelTest extends \PHPUnit\Framework\TestCase $this->assertNotNull($xdsCreds); } + public function testCreateXdsWithNull() { + $this->expectException(\InvalidArgumentException::class); + $xdsCreds = \Grpc\ChannelCredentials::createXds(null); + } + + public function testCreateXdsWithInvalidType() { + $this->expectException(\TypeError::class); + $xdsCreds = \Grpc\ChannelCredentials::createXds("invalid-type"); + } + public function testGetConnectivityState() { $this->channel = new Grpc\Channel('localhost:50001',