changing ChannelCredentials::createInsecure() to return grpc_insecure_credentials_create() instead of NULL

pull/25303/head
root 4 years ago
parent a043be5d5c
commit 185e80373a
  1. 28
      src/php/ext/grpc/channel_credentials.c
  2. 2
      src/php/tests/unit_tests/ChannelCredentialsTest.php
  3. 10
      src/php/tests/unit_tests/ChannelTest.php

@ -233,12 +233,15 @@ PHP_METHOD(ChannelCredentials, createComposite) {
* @return null * @return null
*/ */
PHP_METHOD(ChannelCredentials, createInsecure) { 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 * 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 * 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 * control plane does not provide information on how to fetch credentials
* dynamically. * dynamically.
@ -248,25 +251,20 @@ PHP_METHOD(ChannelCredentials, createXds) {
grpc_channel_credentials* xds_creds = NULL; grpc_channel_credentials* xds_creds = NULL;
zval* fallback_creds = NULL; zval* fallback_creds = NULL;
if (zend_parse_parameters_ex(0, // ZEND_PARSE_PARAMS_QUIET, 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) { grpc_ce_channel_credentials) != SUCCESS) {
zend_throw_exception(spl_ce_InvalidArgumentException, zend_throw_exception(spl_ce_InvalidArgumentException,
"createXds expects a fallback credentials", "createXds expects a fallback credentials",
1 TSRMLS_CC); 1 TSRMLS_CC);
return; return;
} }
char* fallback_creds_hash_str = "";
if (!fallback_creds) { wrapped_grpc_channel_credentials* wrapped_fallback_creds =
grpc_channel_credentials* insecure_creds = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel_credentials,
grpc_insecure_credentials_create(); fallback_creds);
xds_creds = grpc_xds_credentials_create(insecure_creds); xds_creds = grpc_xds_credentials_create(wrapped_fallback_creds->wrapped);
} else { const char* fallback_creds_hash_str =
wrapped_grpc_channel_credentials* wrapped_fallback_creds = wrapped_fallback_creds->hashstr ? wrapped_fallback_creds->hashstr : "";
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;
}
// prefix "XDS:" as the hash of the xDS channel // prefix "XDS:" as the hash of the xDS channel
char* hash_str = malloc(strlen(fallback_creds_hash_str) + strlen("XDS:") + 1); char* hash_str = malloc(strlen(fallback_creds_hash_str) + strlen("XDS:") + 1);

@ -43,7 +43,7 @@ class ChanellCredentialsTest extends \PHPUnit\Framework\TestCase
public function testCreateInsecure() public function testCreateInsecure()
{ {
$channel_credentials = Grpc\ChannelCredentials::createInsecure(); $channel_credentials = Grpc\ChannelCredentials::createInsecure();
$this->assertNull($channel_credentials); $this->assertNotNull($channel_credentials);
} }
public function testDefaultRootsPem() public function testDefaultRootsPem()

@ -59,6 +59,16 @@ class ChannelTest extends \PHPUnit\Framework\TestCase
$this->assertNotNull($xdsCreds); $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() public function testGetConnectivityState()
{ {
$this->channel = new Grpc\Channel('localhost:50001', $this->channel = new Grpc\Channel('localhost:50001',

Loading…
Cancel
Save