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 
     20 class CallCredentialsTest extends PHPUnit_Framework_TestCase
     21 {
     22     public function setUp()
     23     {
     24         $this->credentials = Grpc\ChannelCredentials::createSsl(
     25             file_get_contents(dirname(__FILE__).'/../data/ca.pem'));
     26         $this->call_credentials = Grpc\CallCredentials::createFromPlugin(
     27             [$this, 'callbackFunc']);
     28         $this->credentials = Grpc\ChannelCredentials::createComposite(
     29             $this->credentials,
     30             $this->call_credentials
     31         );
     32         $server_credentials = Grpc\ServerCredentials::createSsl(
     33             null,
     34             file_get_contents(dirname(__FILE__).'/../data/server1.key'),
     35             file_get_contents(dirname(__FILE__).'/../data/server1.pem'));
     36         $this->server = new Grpc\Server();
     37         $this->port = $this->server->addSecureHttp2Port('0.0.0.0:0',
     38                                               $server_credentials);
     39         $this->server->start();
     40         $this->host_override = 'foo.test.google.fr';
     41         $this->channel = new Grpc\Channel(
     42             'localhost:'.$this->port,
     43             [
     44             'force_new' => true,
     45             'grpc.ssl_target_name_override' => $this->host_override,
     46             'grpc.default_authority' => $this->host_override,
     47             'credentials' => $this->credentials,
     48             ]
     49         );
     50     }
     51 
     52     public function tearDown()
     53     {
     54         unset($this->channel);
     55         unset($this->server);
     56     }
     57 
     58     public function callbackFunc($context)
     59     {
     60         $this->assertTrue(is_string($context->service_url));
     61         $this->assertTrue(is_string($context->method_name));
     62 
     63         return ['k1' => ['v1'], 'k2' => ['v2']];
     64     }
     65 
     66     public function testCreateFromPlugin()
     67     {
     68         $deadline = Grpc\Timeval::infFuture();
     69         $status_text = 'xyz';
     70         $call = new Grpc\Call($this->channel,
     71                               '/abc/dummy_method',
     72                               $deadline,
     73                               $this->host_override);
     74 
     75         $event = $call->startBatch([
     76             Grpc\OP_SEND_INITIAL_METADATA => [],
     77             Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
     78         ]);
     79 
     80         $this->assertTrue($event->send_metadata);
     81         $this->assertTrue($event->send_close);
     82 
     83         $event = $this->server->requestCall();
     84 
     85         $this->assertTrue(is_array($event->metadata));
     86         $metadata = $event->metadata;
     87         $this->assertTrue(array_key_exists('k1', $metadata));
     88         $this->assertTrue(array_key_exists('k2', $metadata));
     89         $this->assertSame($metadata['k1'], ['v1']);
     90         $this->assertSame($metadata['k2'], ['v2']);
     91 
     92         $this->assertSame('/abc/dummy_method', $event->method);
     93         $server_call = $event->call;
     94 
     95         $event = $server_call->startBatch([
     96             Grpc\OP_SEND_INITIAL_METADATA => [],
     97             Grpc\OP_SEND_STATUS_FROM_SERVER => [
     98                 'metadata' => [],
     99                 'code' => Grpc\STATUS_OK,
    100                 'details' => $status_text,
    101             ],
    102             Grpc\OP_RECV_CLOSE_ON_SERVER => true,
    103         ]);
    104 
    105         $this->assertTrue($event->send_metadata);
    106         $this->assertTrue($event->send_status);
    107         $this->assertFalse($event->cancelled);
    108 
    109         $event = $call->startBatch([
    110             Grpc\OP_RECV_INITIAL_METADATA => true,
    111             Grpc\OP_RECV_STATUS_ON_CLIENT => true,
    112         ]);
    113 
    114         $this->assertSame([], $event->metadata);
    115         $status = $event->status;
    116         $this->assertSame([], $status->metadata);
    117         $this->assertSame(Grpc\STATUS_OK, $status->code);
    118         $this->assertSame($status_text, $status->details);
    119 
    120         unset($call);
    121         unset($server_call);
    122     }
    123 
    124     public function callbackFunc2($context)
    125     {
    126         return [];
    127     }
    128 
    129     public function testCreateComposite()
    130     {
    131         $call_credentials2 = Grpc\CallCredentials::createFromPlugin(
    132             [$this, 'callbackFunc2']);
    133         $call_credentials3 = Grpc\CallCredentials::createComposite(
    134             $this->call_credentials,
    135             $call_credentials2
    136         );
    137         $this->assertSame('Grpc\CallCredentials',
    138                           get_class($call_credentials3));
    139     }
    140 
    141     /**
    142      * @expectedException InvalidArgumentException
    143      */
    144     public function testCreateFromPluginInvalidParam()
    145     {
    146         $call_credentials = Grpc\CallCredentials::createFromPlugin(
    147             'callbackFunc'
    148         );
    149     }
    150 
    151     /**
    152      * @expectedException InvalidArgumentException
    153      */
    154     public function testCreateCompositeInvalidParam()
    155     {
    156         $call_credentials3 = Grpc\CallCredentials::createComposite(
    157             $this->call_credentials,
    158             $this->credentials
    159         );
    160     }
    161 }
    162