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