From 89d28e7dc9179e6508b3117509bd94810b267e91 Mon Sep 17 00:00:00 2001 From: Hannah Shi Date: Fri, 5 Feb 2021 18:49:40 +0000 Subject: [PATCH] test both insecure&secure channel --- src/php/bin/run_gen_code_test.sh | 2 ++ .../AbstractGeneratedCodeTest.php | 26 +++++++++++-------- .../generated_code/GeneratedCodeTest.php | 5 +++- .../GeneratedCodeWithCallbackTest.php | 4 ++- src/php/tests/generated_code/math_server.js | 7 ++++- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/php/bin/run_gen_code_test.sh b/src/php/bin/run_gen_code_test.sh index e98ce364717d7..93308a8609b19 100755 --- a/src/php/bin/run_gen_code_test.sh +++ b/src/php/bin/run_gen_code_test.sh @@ -17,6 +17,8 @@ set -e cd $(dirname $0) source ./determine_extension_dir.sh export GRPC_TEST_HOST=localhost:50051 +export GRPC_TEST_INSECURE_HOST=localhost:50052 + php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug \ ../tests/generated_code/GeneratedCodeTest.php php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug \ diff --git a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php index 7419e64f2a96d..f8a1b53da56ab 100644 --- a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php +++ b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php @@ -134,29 +134,33 @@ public function testCancel() public function testCallCredentialsCallback() { $div_arg = new Math\DivArgs(); + $div_arg->setDividend(7); + $div_arg->setDivisor(4); $call = self::$client->Div($div_arg, array(), array( 'call_credentials_callback' => function ($context) { return array(); }, )); - $call->cancel(); list($response, $status) = $call->wait(); - $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code); + $this->assertSame(\Grpc\STATUS_OK, $status->code); } - public function testCallCredentialsCallback2() + public function testInsecureChannelCallCredentialsCallback() { $div_arg = new Math\DivArgs(); - $call = self::$client->Div($div_arg); - $call_credentials = Grpc\CallCredentials::createFromPlugin( - function ($context) { + $div_arg->setDividend(7); + $div_arg->setDivisor(4); + $client = new Math\MathClient( + getenv('GRPC_TEST_INSECURE_HOST'), [ + 'credentials' => Grpc\ChannelCredentials::createInsecure(), + ]); + $call = $client->Div($div_arg, array(), array( + 'call_credentials_callback' => function ($context) { return array(); - } - ); - $call->setCallCredentials($call_credentials); - $call->cancel(); + }, + )); list($response, $status) = $call->wait(); - $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code); + $this->assertSame(\Grpc\STATUS_UNAUTHENTICATED, $status->code); } public function testInvalidMethodName() diff --git a/src/php/tests/generated_code/GeneratedCodeTest.php b/src/php/tests/generated_code/GeneratedCodeTest.php index f9fa573bf3e58..f2e5c8c56859c 100755 --- a/src/php/tests/generated_code/GeneratedCodeTest.php +++ b/src/php/tests/generated_code/GeneratedCodeTest.php @@ -24,7 +24,10 @@ public function setUp(): void { self::$client = new Math\MathClient( getenv('GRPC_TEST_HOST'), [ - 'credentials' => Grpc\ChannelCredentials::createInsecure(), + 'credentials' => Grpc\ChannelCredentials::createSsl( + file_get_contents(dirname(__FILE__).'/../data/ca.pem')), + 'grpc.ssl_target_name_override' => 'foo.test.google.fr', + ]); } diff --git a/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php b/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php index fb55d1659cdaa..98f293a3c2a65 100644 --- a/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php +++ b/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php @@ -24,7 +24,9 @@ public function setUp(): void { self::$client = new Math\MathClient( getenv('GRPC_TEST_HOST'), - ['credentials' => Grpc\ChannelCredentials::createInsecure(), + ['credentials' => Grpc\ChannelCredentials::createSsl( + file_get_contents(dirname(__FILE__).'/../data/ca.pem')), + 'grpc.ssl_target_name_override' => 'foo.test.google.fr', 'update_metadata' => function ($a_hash, $client = []) { $a_copy = $a_hash; diff --git a/src/php/tests/generated_code/math_server.js b/src/php/tests/generated_code/math_server.js index 1492c936e13fd..6b37561099ff5 100644 --- a/src/php/tests/generated_code/math_server.js +++ b/src/php/tests/generated_code/math_server.js @@ -120,7 +120,12 @@ function main() { Sum: Sum, DivMany: DivMany, }); - server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure()); + server.bind('0.0.0.0:50052', grpc.ServerCredentials.createInsecure()); + var fs = require('fs'); + var key_data = fs.readFileSync(__dirname + '/../data/server1.key'); + var pem_data = fs.readFileSync(__dirname + '/../data/server1.pem'); + server.bind('0.0.0.0:50051', grpc.ServerCredentials.createSsl(null, [{private_key: key_data, + cert_chain: pem_data}])); server.start(); }