Debian Codegraph @

lib/php/test/Unit/Lib/Transport/TSSLSocketTest.php

raw
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\Transport;
23
24 use phpmock\phpunit\PHPMock;
25 use PHPUnit\Framework\TestCase;
26 use Thrift\Exception\TException;
27 use Thrift\Exception\TTransportException;
28 use Thrift\Transport\TSSLSocket;
29
30 class TSSLSocketTest extends TestCase
31 {
32 use PHPMock;
33
34 /**
35 * @dataProvider openExceptionDataProvider
36 */
37 public function testOpenException(
38 $host,
39 $port,
40 $context,
41 $debugHandler,
42 $streamSocketClientCallCount,
43 $expectedException,
44 $expectedMessage,
45 $expectedCode
46 ) {
47 $this->expectException($expectedException);
48 $this->expectExceptionMessage($expectedMessage);
49 $this->expectExceptionCode($expectedCode);
50
51 $this->getFunctionMock('Thrift\Transport', 'stream_socket_client')
52 ->expects($this->exactly($streamSocketClientCallCount))
53 ->with(
54 'ssl://' . $host . ':' . $port,
55 $this->anything(), #$errno,
56 $this->anything(), #$errstr,
57 $this->anything(), #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
58 STREAM_CLIENT_CONNECT,
59 $this->anything() #$context
60 )
61 ->willReturn(false);
62
63 $socket = new TSSLSocket(
64 $host,
65 $port,
66 $context,
67 $debugHandler
68 );
69 $socket->open();
70 }
71
72 public function openExceptionDataProvider()
73 {
74 yield 'host is empty' => [
75 'host' => '',
76 'port' => 9090,
77 'context' => null,
78 'debugHandler' => null,
79 'streamSocketClientCallCount' => 0,
80 'expectedException' => TTransportException::class,
81 'expectedMessage' => 'Cannot open null host',
82 'expectedCode' => TTransportException::NOT_OPEN,
83 ];
84 yield 'port is not positive' => [
85 'host' => 'localhost',
86 'port' => 0,
87 'context' => null,
88 'debugHandler' => null,
89 'streamSocketClientCallCount' => 0,
90 'expectedException' => TTransportException::class,
91 'expectedMessage' => 'Cannot open without port',
92 'expectedCode' => TTransportException::NOT_OPEN,
93 ];
94 yield 'connection failure' => [
95 'host' => 'nonexistent-host',
96 'port' => 9090,
97 'context' => null,
98 'debugHandler' => null,
99 'streamSocketClientCallCount' => 1,
100 'expectedException' => TException::class,
101 'expectedMessage' => 'TSocket: Could not connect to',
102 'expectedCode' => TTransportException::UNKNOWN,
103 ];
104 }
105
106 public function testDoubleConnect(): void
107 {
108 $host = 'localhost';
109 $port = 9090;
110 $context = null;
111 $debugHandler = null;
112
113 $this->getFunctionMock('Thrift\Transport', 'stream_socket_client')
114 ->expects($this->once())
115 ->with(
116 'ssl://' . $host . ':' . $port,
117 $this->anything(), #$errno,
118 $this->anything(), #$errstr,
119 $this->anything(), #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
120 STREAM_CLIENT_CONNECT,
121 $this->anything() #$context
122 )
123 ->willReturn(fopen('php://memory', 'r+'));
124
125 $transport = new TSSLSocket(
126 $host,
127 $port,
128 $context,
129 $debugHandler
130 );
131
132 $transport->open();
133 $this->expectException(TTransportException::class);
134 $this->expectExceptionMessage('Socket already connected');
135 $this->expectExceptionCode(TTransportException::ALREADY_OPEN);
136 $transport->open();
137 }
138
139 public function testDebugHandler()
140 {
141 $host = 'nonexistent-host';
142 $port = 9090;
143 $context = null;
144
145 $debugHandler = function ($error) {
146 $this->assertEquals(
147 'TSocket: Could not connect to ssl://nonexistent-host:9090 (Connection refused [999])',
148 $error
149 );
150 };
151
152 $this->getFunctionMock('Thrift\Transport', 'stream_socket_client')
153 ->expects($this->once())
154 ->with(
155 'ssl://' . $host . ':' . $port,
156 $this->anything(), #$errno,
157 $this->anything(), #$errstr,
158 $this->anything(), #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
159 STREAM_CLIENT_CONNECT,
160 $this->anything() #$context
161 )
162 ->willReturnCallback(
163 function ($host, &$error_code, &$error_message, $timeout, $flags, $context) {
164 $error_code = 999;
165 $error_message = 'Connection refused';
166
167 return false;
168 }
169 );
170
171 $this->expectException(\Exception::class);
172 $this->expectExceptionMessage('TSocket: Could not connect to');
173 $this->expectExceptionCode(0);
174
175 $transport = new TSSLSocket(
176 $host,
177 $port,
178 $context,
179 $debugHandler
180 );
181 $transport->setDebug(true);
182 $transport->open();
183 }
184
185 public function testOpenWithContext()
186 {
187 $host = 'self-signed-localhost';
188 $port = 9090;
189 $context = stream_context_create(
190 [
191 'ssl' => [
192 'verify_peer' => true,
193 'verify_peer_name' => true,
194 'allow_self_signed' => true,
195 ],
196 ]
197 );
198 $debugHandler = null;
199
200 $this->getFunctionMock('Thrift\Transport', 'stream_socket_client')
201 ->expects($this->once())
202 ->with(
203 'ssl://' . $host . ':' . $port,
204 $this->anything(), #$errno,
205 $this->anything(), #$errstr,
206 $this->anything(), #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
207 STREAM_CLIENT_CONNECT,
208 $context #$context
209 )
210 ->willReturn(fopen('php://memory', 'r+'));
211
212 $transport = new TSSLSocket(
213 $host,
214 $port,
215 $context,
216 $debugHandler
217 );
218
219
220 $transport->open();
221 $this->assertTrue($transport->isOpen());
222 }
223
224 /**
225 * @dataProvider hostDataProvider
226 */
227 public function testGetHost($host, $expected)
228 {
229 $port = 9090;
230 $context = null;
231 $debugHandler = null;
232 $transport = new TSSLSocket(
233 $host,
234 $port,
235 $context,
236 $debugHandler
237 );
238 $this->assertEquals($expected, $transport->getHost());
239 }
240
241 public function hostDataProvider()
242 {
243 yield 'localhost' => ['localhost', 'ssl://localhost'];
244 yield 'ssl_localhost' => ['ssl://localhost', 'ssl://localhost'];
245 yield 'http_localhost' => ['http://localhost', 'http://localhost'];
246 }
247 }