1<?php 2/* 3 * 4 * Copyright 2015 gRPC authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 */ 19 20class CallCredentialsTest extends \PHPUnit\Framework\TestCase 21{ 22 private $credentials; 23 private $call_credentials; 24 private $server; 25 private $port; 26 private $host_override; 27 private $channel; 28 29 public function setUp(): void 30 { 31 $this->credentials = Grpc\ChannelCredentials::createSsl( 32 file_get_contents(dirname(__FILE__).'/../data/ca.pem')); 33 $this->call_credentials = Grpc\CallCredentials::createFromPlugin( 34 [$this, 'callbackFunc']); 35 $this->credentials = Grpc\ChannelCredentials::createComposite( 36 $this->credentials, 37 $this->call_credentials 38 ); 39 $server_credentials = Grpc\ServerCredentials::createSsl( 40 null, 41 file_get_contents(dirname(__FILE__).'/../data/server1.key'), 42 file_get_contents(dirname(__FILE__).'/../data/server1.pem')); 43 $this->server = new Grpc\Server(); 44 $this->port = $this->server->addSecureHttp2Port('0.0.0.0:0', 45 $server_credentials); 46 $this->server->start(); 47 $this->host_override = 'foo.test.google.fr'; 48 $this->channel = new Grpc\Channel( 49 'localhost:'.$this->port, 50 [ 51 'force_new' => true, 52 'grpc.ssl_target_name_override' => $this->host_override, 53 'grpc.default_authority' => $this->host_override, 54 'credentials' => $this->credentials, 55 ] 56 ); 57 } 58 59 public function tearDown(): void 60 { 61 unset($this->channel); 62 unset($this->server); 63 } 64 65 public function callbackFunc($context) 66 { 67 $this->assertTrue(is_string($context->service_url)); 68 $this->assertTrue(is_string($context->method_name)); 69 70 return ['k1' => ['v1'], 'k2' => ['v2']]; 71 } 72 73 public function testCreateFromPlugin() 74 { 75 $deadline = Grpc\Timeval::infFuture(); 76 $status_text = 'xyz'; 77 $call = new Grpc\Call($this->channel, 78 '/abc/phony_method', 79 $deadline, 80 $this->host_override); 81 82 $event = $call->startBatch([ 83 Grpc\OP_SEND_INITIAL_METADATA => [], 84 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, 85 ]); 86 87 $this->assertTrue($event->send_metadata); 88 $this->assertTrue($event->send_close); 89 90 $event = $this->server->requestCall(); 91 92 $this->assertTrue(is_array($event->metadata)); 93 $metadata = $event->metadata; 94 $this->assertTrue(array_key_exists('k1', $metadata)); 95 $this->assertTrue(array_key_exists('k2', $metadata)); 96 $this->assertSame($metadata['k1'], ['v1']); 97 $this->assertSame($metadata['k2'], ['v2']); 98 99 $this->assertSame('/abc/phony_method', $event->method); 100 $server_call = $event->call; 101 102 $event = $server_call->startBatch([ 103 Grpc\OP_SEND_INITIAL_METADATA => [], 104 Grpc\OP_SEND_STATUS_FROM_SERVER => [ 105 'metadata' => [], 106 'code' => Grpc\STATUS_OK, 107 'details' => $status_text, 108 ], 109 Grpc\OP_RECV_CLOSE_ON_SERVER => true, 110 ]); 111 112 $this->assertTrue($event->send_metadata); 113 $this->assertTrue($event->send_status); 114 $this->assertFalse($event->cancelled); 115 116 $event = $call->startBatch([ 117 Grpc\OP_RECV_INITIAL_METADATA => true, 118 Grpc\OP_RECV_STATUS_ON_CLIENT => true, 119 ]); 120 121 $this->assertSame([], $event->metadata); 122 $status = $event->status; 123 $this->assertSame([], $status->metadata); 124 $this->assertSame(Grpc\STATUS_OK, $status->code); 125 $this->assertSame($status_text, $status->details); 126 127 unset($call); 128 unset($server_call); 129 } 130 131 public function callbackFunc2($context) 132 { 133 return []; 134 } 135 136 public function testCreateComposite() 137 { 138 $call_credentials2 = Grpc\CallCredentials::createFromPlugin( 139 [$this, 'callbackFunc2']); 140 $call_credentials3 = Grpc\CallCredentials::createComposite( 141 $this->call_credentials, 142 $call_credentials2 143 ); 144 $this->assertSame('Grpc\CallCredentials', 145 get_class($call_credentials3)); 146 } 147 148 public function testCreateFromPluginInvalidParam() 149 { 150 $this->expectException(\InvalidArgumentException::class); 151 $call_credentials = Grpc\CallCredentials::createFromPlugin( 152 'callbackFunc' 153 ); 154 } 155 156 public function testCreateCompositeInvalidParam() 157 { 158 $this->expectException(\InvalidArgumentException::class); 159 $call_credentials3 = Grpc\CallCredentials::createComposite( 160 $this->call_credentials, 161 $this->credentials 162 ); 163 } 164} 165