Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
changing ChannelCredentials::createInsecure() to return grpc_insecure…
Browse files Browse the repository at this point in the history
…_credentials_create() instead of NULL
  • Loading branch information
HannahShiSFB committed Feb 2, 2021
1 parent a043be5 commit 185e803
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
28 changes: 13 additions & 15 deletions src/php/ext/grpc/channel_credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/php/tests/unit_tests/ChannelCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testCreateSslWith3NullString()
public function testCreateInsecure()
{
$channel_credentials = Grpc\ChannelCredentials::createInsecure();
$this->assertNull($channel_credentials);
$this->assertNotNull($channel_credentials);
}

public function testDefaultRootsPem()
Expand Down
10 changes: 10 additions & 0 deletions src/php/tests/unit_tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public function testCreateXdsWithInsecure() {
$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',
Expand Down

0 comments on commit 185e803

Please sign in to comment.