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 */ 19class SecureEndToEndTest extends \PHPUnit\Framework\TestCase 20{ 21 private $server; 22 private $port; 23 private $host_override; 24 private $channel; 25 26 public function setUp(): void 27 { 28 $credentials = Grpc\ChannelCredentials::createSsl( 29 file_get_contents(dirname(__FILE__).'/../data/ca.pem')); 30 $server_credentials = Grpc\ServerCredentials::createSsl( 31 null, 32 file_get_contents(dirname(__FILE__).'/../data/server1.key'), 33 file_get_contents(dirname(__FILE__).'/../data/server1.pem')); 34 $this->server = new Grpc\Server(); 35 $this->port = $this->server->addSecureHttp2Port('0.0.0.0:0', 36 $server_credentials); 37 $this->server->start(); 38 $this->host_override = 'foo.test.google.fr'; 39 $this->channel = new Grpc\Channel( 40 'localhost:'.$this->port, 41 [ 42 'force_new' => true, 43 'grpc.ssl_target_name_override' => $this->host_override, 44 'grpc.default_authority' => $this->host_override, 45 'credentials' => $credentials, 46 ] 47 ); 48 } 49 50 public function tearDown(): void 51 { 52 $this->channel->close(); 53 unset($this->server); 54 } 55 56 public function testSimpleRequestBody() 57 { 58 $deadline = Grpc\Timeval::infFuture(); 59 $status_text = 'xyz'; 60 $call = new Grpc\Call($this->channel, 61 'phony_method', 62 $deadline, 63 $this->host_override); 64 65 $event = $call->startBatch([ 66 Grpc\OP_SEND_INITIAL_METADATA => [], 67 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, 68 ]); 69 70 $this->assertTrue($event->send_metadata); 71 $this->assertTrue($event->send_close); 72 73 $event = $this->server->requestCall(); 74 $this->assertSame('phony_method', $event->method); 75 $server_call = $event->call; 76 77 $event = $server_call->startBatch([ 78 Grpc\OP_SEND_INITIAL_METADATA => [], 79 Grpc\OP_SEND_STATUS_FROM_SERVER => [ 80 'metadata' => [], 81 'code' => Grpc\STATUS_OK, 82 'details' => $status_text, 83 ], 84 Grpc\OP_RECV_CLOSE_ON_SERVER => true, 85 ]); 86 87 $this->assertTrue($event->send_metadata); 88 $this->assertTrue($event->send_status); 89 $this->assertFalse($event->cancelled); 90 91 $event = $call->startBatch([ 92 Grpc\OP_RECV_INITIAL_METADATA => true, 93 Grpc\OP_RECV_STATUS_ON_CLIENT => true, 94 ]); 95 96 $this->assertSame([], $event->metadata); 97 $status = $event->status; 98 $this->assertSame([], $status->metadata); 99 $this->assertSame(Grpc\STATUS_OK, $status->code); 100 $this->assertSame($status_text, $status->details); 101 102 unset($call); 103 unset($server_call); 104 } 105 106 public function testMessageWriteFlags() 107 { 108 $deadline = Grpc\Timeval::infFuture(); 109 $req_text = 'message_write_flags_test'; 110 $status_text = 'xyz'; 111 $call = new Grpc\Call($this->channel, 112 'phony_method', 113 $deadline, 114 $this->host_override); 115 116 $event = $call->startBatch([ 117 Grpc\OP_SEND_INITIAL_METADATA => [], 118 Grpc\OP_SEND_MESSAGE => ['message' => $req_text, 119 'flags' => Grpc\WRITE_NO_COMPRESS, ], 120 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, 121 ]); 122 123 $this->assertTrue($event->send_metadata); 124 $this->assertTrue($event->send_close); 125 126 $event = $this->server->requestCall(); 127 $this->assertSame('phony_method', $event->method); 128 $server_call = $event->call; 129 130 $event = $server_call->startBatch([ 131 Grpc\OP_SEND_INITIAL_METADATA => [], 132 Grpc\OP_SEND_STATUS_FROM_SERVER => [ 133 'metadata' => [], 134 'code' => Grpc\STATUS_OK, 135 'details' => $status_text, 136 ], 137 ]); 138 139 $event = $call->startBatch([ 140 Grpc\OP_RECV_INITIAL_METADATA => true, 141 Grpc\OP_RECV_STATUS_ON_CLIENT => true, 142 ]); 143 144 $this->assertSame([], $event->metadata); 145 $status = $event->status; 146 $this->assertSame([], $status->metadata); 147 $this->assertSame(Grpc\STATUS_OK, $status->code); 148 $this->assertSame($status_text, $status->details); 149 150 unset($call); 151 unset($server_call); 152 } 153 154 public function testClientServerFullRequestResponse() 155 { 156 $deadline = Grpc\Timeval::infFuture(); 157 $req_text = 'client_server_full_request_response'; 158 $reply_text = 'reply:client_server_full_request_response'; 159 $status_text = 'status:client_server_full_response_text'; 160 161 $call = new Grpc\Call($this->channel, 162 'phony_method', 163 $deadline, 164 $this->host_override); 165 166 $event = $call->startBatch([ 167 Grpc\OP_SEND_INITIAL_METADATA => [], 168 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, 169 Grpc\OP_SEND_MESSAGE => ['message' => $req_text], 170 ]); 171 172 $this->assertTrue($event->send_metadata); 173 $this->assertTrue($event->send_close); 174 $this->assertTrue($event->send_message); 175 176 $event = $this->server->requestCall(); 177 $this->assertSame('phony_method', $event->method); 178 $server_call = $event->call; 179 180 $event = $server_call->startBatch([ 181 Grpc\OP_RECV_MESSAGE => true, 182 ]); 183 184 $this->assertSame($req_text, $event->message); 185 186 $event = $server_call->startBatch([ 187 Grpc\OP_SEND_INITIAL_METADATA => [], 188 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], 189 Grpc\OP_SEND_STATUS_FROM_SERVER => [ 190 'metadata' => [], 191 'code' => Grpc\STATUS_OK, 192 'details' => $status_text, 193 ], 194 Grpc\OP_RECV_CLOSE_ON_SERVER => true, 195 ]); 196 197 $this->assertTrue($event->send_metadata); 198 $this->assertTrue($event->send_status); 199 $this->assertTrue($event->send_message); 200 $this->assertFalse($event->cancelled); 201 202 $event = $call->startBatch([ 203 Grpc\OP_RECV_INITIAL_METADATA => true, 204 Grpc\OP_RECV_MESSAGE => true, 205 Grpc\OP_RECV_STATUS_ON_CLIENT => true, 206 ]); 207 208 $this->assertSame([], $event->metadata); 209 $this->assertSame($reply_text, $event->message); 210 $status = $event->status; 211 $this->assertSame([], $status->metadata); 212 $this->assertSame(Grpc\STATUS_OK, $status->code); 213 $this->assertSame($status_text, $status->details); 214 215 unset($call); 216 unset($server_call); 217 } 218} 219