| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 5 |
* or more contributor license agreements. See the NOTICE file |
| 6 |
* distributed with this work for additional information |
| 7 |
* regarding copyright ownership. The ASF licenses this file |
| 8 |
* to you under the Apache License, Version 2.0 (the |
| 9 |
* "License"); you may not use this file except in compliance |
| 10 |
* with the License. You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, |
| 15 |
* software distributed under the License is distributed on an |
| 16 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 |
* KIND, either express or implied. See the License for the |
| 18 |
* specific language governing permissions and limitations |
| 19 |
* under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Test\Thrift\Unit\Lib\Server; |
| 23 |
|
| 24 |
use phpmock\phpunit\PHPMock; |
| 25 |
use PHPUnit\Framework\TestCase; |
| 26 |
use Test\Thrift\Unit\Lib\ReflectionHelper; |
| 27 |
use Thrift\Exception\TTransportException; |
| 28 |
use Thrift\Server\TSSLServerSocket; |
| 29 |
use Thrift\Transport\TSocket; |
| 30 |
|
| 31 |
class TSSLServerSocketTest extends TestCase |
| 32 |
{ |
| 33 |
use PHPMock; |
| 34 |
use ReflectionHelper; |
| 35 |
|
| 36 |
|
| 37 |
public function testGetSSLHost() |
| 38 |
{ |
| 39 |
$socket = new TSSLServerSocket(); |
| 40 |
|
| 41 |
$this->assertEquals('ssl://localhost', $socket->getSSLHost('localhost')); |
| 42 |
$this->assertEquals('ssl://localhost', $socket->getSSLHost('ssl://localhost')); |
| 43 |
$this->assertEquals('tcp://localhost', $socket->getSSLHost('tcp://localhost')); |
| 44 |
} |
| 45 |
|
| 46 |
public function testListenAndClose(): void |
| 47 |
{ |
| 48 |
$options = [ |
| 49 |
'ssl' => [ |
| 50 |
'verify_peer' => true, |
| 51 |
'verify_peer_name' => true, |
| 52 |
'allow_self_signed' => true, |
| 53 |
], |
| 54 |
]; |
| 55 |
$context = stream_context_create($options); |
| 56 |
$socket = new TSSLServerSocket('somehost', 999, $context); |
| 57 |
|
| 58 |
$listener = tmpfile(); |
| 59 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_server') |
| 60 |
->expects($this->once()) |
| 61 |
->with( |
| 62 |
'ssl://somehost:999', #$address |
| 63 |
$this->anything(), #&$error_code |
| 64 |
$this->anything(), #&$error_string |
| 65 |
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, #int $flags |
| 66 |
$this->callback(function ($context) use ($options) { |
| 67 |
$contextOptions = stream_context_get_options($context); |
| 68 |
|
| 69 |
return is_resource($context) && $options === $contextOptions; |
| 70 |
})#resource $context |
| 71 |
)->willReturn($listener); |
| 72 |
|
| 73 |
$socket->listen(); |
| 74 |
|
| 75 |
$this->assertIsResource($this->getPropertyValue($socket, 'listener_')); |
| 76 |
|
| 77 |
$this->getFunctionMock('Thrift\Server', 'fclose') |
| 78 |
->expects($this->once()) |
| 79 |
->with($this->equalTo($listener)) |
| 80 |
->willReturn(true); |
| 81 |
|
| 82 |
$socket->close(); |
| 83 |
$this->assertNull($this->getPropertyValue($socket, 'listener_')); |
| 84 |
} |
| 85 |
|
| 86 |
public function testAccept() |
| 87 |
{ |
| 88 |
$socket = new TSSLServerSocket('somehost', 999); |
| 89 |
$socket->setAcceptTimeout(1000); |
| 90 |
|
| 91 |
$listener = tmpfile(); |
| 92 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_server') |
| 93 |
->expects($this->once()) |
| 94 |
->with( |
| 95 |
'ssl://somehost:999', #$address |
| 96 |
$this->anything(), #&$error_code |
| 97 |
$this->anything(), #&$error_string |
| 98 |
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, #int $flags |
| 99 |
$this->callback(function ($context) { |
| 100 |
$contextOptions = stream_context_get_options($context); |
| 101 |
|
| 102 |
return is_resource($context) && $contextOptions === []; |
| 103 |
}) #resource $context |
| 104 |
)->willReturn($listener); |
| 105 |
|
| 106 |
$transportHandle = tmpfile(); |
| 107 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_accept') |
| 108 |
->expects($this->once()) |
| 109 |
->with( |
| 110 |
$this->equalTo($listener), |
| 111 |
1 |
| 112 |
)->willReturn($transportHandle); |
| 113 |
|
| 114 |
$socket->listen(); |
| 115 |
$result = $socket->accept(); |
| 116 |
$this->assertInstanceOf(TSocket::class, $result); |
| 117 |
|
| 118 |
$this->assertEquals($transportHandle, $this->getPropertyValue($result, 'handle_')); |
| 119 |
} |
| 120 |
|
| 121 |
public function testAcceptFailed() |
| 122 |
{ |
| 123 |
$socket = new TSSLServerSocket('somehost', 999); |
| 124 |
$socket->setAcceptTimeout(1000); |
| 125 |
|
| 126 |
$listener = tmpfile(); |
| 127 |
|
| 128 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_server') |
| 129 |
->expects($this->once()) |
| 130 |
->with('ssl://somehost:999') |
| 131 |
->willReturn($listener); |
| 132 |
|
| 133 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_accept') |
| 134 |
->expects($this->once()) |
| 135 |
->with( |
| 136 |
$this->equalTo($listener), |
| 137 |
1 |
| 138 |
)->willReturn(null); |
| 139 |
|
| 140 |
$this->expectException(TTransportException::class); |
| 141 |
$this->expectExceptionMessage('accept() may not return NULL'); |
| 142 |
|
| 143 |
$socket->listen(); |
| 144 |
$socket->accept(); |
| 145 |
} |
| 146 |
} |