OLD | NEW |
(Empty) | |
| 1 <?php |
| 2 /* |
| 3 * |
| 4 * Copyright 2015-2016, Google Inc. |
| 5 * All rights reserved. |
| 6 * |
| 7 * Redistribution and use in source and binary forms, with or without |
| 8 * modification, are permitted provided that the following conditions are |
| 9 * met: |
| 10 * |
| 11 * * Redistributions of source code must retain the above copyright |
| 12 * notice, this list of conditions and the following disclaimer. |
| 13 * * Redistributions in binary form must reproduce the above |
| 14 * copyright notice, this list of conditions and the following disclaimer |
| 15 * in the documentation and/or other materials provided with the |
| 16 * distribution. |
| 17 * * Neither the name of Google Inc. nor the names of its |
| 18 * contributors may be used to endorse or promote products derived from |
| 19 * this software without specific prior written permission. |
| 20 * |
| 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 * |
| 33 */ |
| 34 |
| 35 class CallCredentialsTest extends PHPUnit_Framework_TestCase |
| 36 { |
| 37 public function setUp() |
| 38 { |
| 39 $this->credentials = Grpc\ChannelCredentials::createSsl( |
| 40 file_get_contents(dirname(__FILE__).'/../data/ca.pem')); |
| 41 $this->call_credentials = Grpc\CallCredentials::createFromPlugin( |
| 42 [$this, 'callbackFunc']); |
| 43 $this->credentials = Grpc\ChannelCredentials::createComposite( |
| 44 $this->credentials, |
| 45 $this->call_credentials |
| 46 ); |
| 47 $server_credentials = Grpc\ServerCredentials::createSsl( |
| 48 null, |
| 49 file_get_contents(dirname(__FILE__).'/../data/server1.key'), |
| 50 file_get_contents(dirname(__FILE__).'/../data/server1.pem')); |
| 51 $this->server = new Grpc\Server(); |
| 52 $this->port = $this->server->addSecureHttp2Port('0.0.0.0:0', |
| 53 $server_credentials); |
| 54 $this->server->start(); |
| 55 $this->host_override = 'foo.test.google.fr'; |
| 56 $this->channel = new Grpc\Channel( |
| 57 'localhost:'.$this->port, |
| 58 [ |
| 59 'grpc.ssl_target_name_override' => $this->host_override, |
| 60 'grpc.default_authority' => $this->host_override, |
| 61 'credentials' => $this->credentials, |
| 62 ] |
| 63 ); |
| 64 } |
| 65 |
| 66 public function tearDown() |
| 67 { |
| 68 unset($this->channel); |
| 69 unset($this->server); |
| 70 } |
| 71 |
| 72 public function callbackFunc($context) |
| 73 { |
| 74 $this->assertTrue(is_string($context->service_url)); |
| 75 $this->assertTrue(is_string($context->method_name)); |
| 76 |
| 77 return ['k1' => ['v1'], 'k2' => ['v2']]; |
| 78 } |
| 79 |
| 80 public function testCreateFromPlugin() |
| 81 { |
| 82 $deadline = Grpc\Timeval::infFuture(); |
| 83 $status_text = 'xyz'; |
| 84 $call = new Grpc\Call($this->channel, |
| 85 '/abc/dummy_method', |
| 86 $deadline, |
| 87 $this->host_override); |
| 88 |
| 89 $event = $call->startBatch([ |
| 90 Grpc\OP_SEND_INITIAL_METADATA => [], |
| 91 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, |
| 92 ]); |
| 93 |
| 94 $this->assertTrue($event->send_metadata); |
| 95 $this->assertTrue($event->send_close); |
| 96 |
| 97 $event = $this->server->requestCall(); |
| 98 |
| 99 $this->assertTrue(is_array($event->metadata)); |
| 100 $metadata = $event->metadata; |
| 101 $this->assertTrue(array_key_exists('k1', $metadata)); |
| 102 $this->assertTrue(array_key_exists('k2', $metadata)); |
| 103 $this->assertSame($metadata['k1'], ['v1']); |
| 104 $this->assertSame($metadata['k2'], ['v2']); |
| 105 |
| 106 $this->assertSame('/abc/dummy_method', $event->method); |
| 107 $server_call = $event->call; |
| 108 |
| 109 $event = $server_call->startBatch([ |
| 110 Grpc\OP_SEND_INITIAL_METADATA => [], |
| 111 Grpc\OP_SEND_STATUS_FROM_SERVER => [ |
| 112 'metadata' => [], |
| 113 'code' => Grpc\STATUS_OK, |
| 114 'details' => $status_text, |
| 115 ], |
| 116 Grpc\OP_RECV_CLOSE_ON_SERVER => true, |
| 117 ]); |
| 118 |
| 119 $this->assertTrue($event->send_metadata); |
| 120 $this->assertTrue($event->send_status); |
| 121 $this->assertFalse($event->cancelled); |
| 122 |
| 123 $event = $call->startBatch([ |
| 124 Grpc\OP_RECV_INITIAL_METADATA => true, |
| 125 Grpc\OP_RECV_STATUS_ON_CLIENT => true, |
| 126 ]); |
| 127 |
| 128 $this->assertSame([], $event->metadata); |
| 129 $status = $event->status; |
| 130 $this->assertSame([], $status->metadata); |
| 131 $this->assertSame(Grpc\STATUS_OK, $status->code); |
| 132 $this->assertSame($status_text, $status->details); |
| 133 |
| 134 unset($call); |
| 135 unset($server_call); |
| 136 } |
| 137 |
| 138 public function callbackFunc2($context) |
| 139 { |
| 140 return []; |
| 141 } |
| 142 |
| 143 public function testCreateComposite() |
| 144 { |
| 145 $call_credentials2 = Grpc\CallCredentials::createFromPlugin( |
| 146 [$this, 'callbackFunc2']); |
| 147 $call_credentials3 = Grpc\CallCredentials::createComposite( |
| 148 $this->call_credentials, |
| 149 $call_credentials2 |
| 150 ); |
| 151 $this->assertSame('Grpc\CallCredentials', get_class($call_credentials3))
; |
| 152 } |
| 153 |
| 154 /** |
| 155 * @expectedException InvalidArgumentException |
| 156 */ |
| 157 public function testCreateFromPluginInvalidParam() |
| 158 { |
| 159 $call_credentials = Grpc\CallCredentials::createFromPlugin( |
| 160 'callbackFunc' |
| 161 ); |
| 162 } |
| 163 |
| 164 /** |
| 165 * @expectedException InvalidArgumentException |
| 166 */ |
| 167 public function testCreateCompositeInvalidParam() |
| 168 { |
| 169 $call_credentials3 = Grpc\CallCredentials::createComposite( |
| 170 $this->call_credentials, |
| 171 $this->credentials |
| 172 ); |
| 173 } |
| 174 } |
OLD | NEW |