Debian Codegraph @

lib/php/test/Unit/Lib/Transport/TSocketTest.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 Test\Thrift\Unit\Lib\ReflectionHelper;
27 use Thrift\Exception\TException;
28 use Thrift\Exception\TTransportException;
29 use Thrift\Transport\TSocket;
30
31 class TSocketTest extends TestCase
32 {
33 use PHPMock;
34 use ReflectionHelper;
35
36 /**
37 * @dataProvider openExceptionDataProvider
38 */
39 public function testOpenException(
40 $host,
41 $port,
42 $persist,
43 $debugHandler,
44 $fsockopenCallCount,
45 $expectedException,
46 $expectedMessage,
47 $expectedCode
48 ) {
49 $this->expectException($expectedException);
50 $this->expectExceptionMessage($expectedMessage);
51 $this->expectExceptionCode($expectedCode);
52
53 $this->getFunctionMock('Thrift\Transport', 'fsockopen')
54 ->expects($this->exactly($fsockopenCallCount))
55 ->with(
56 $host,
57 $port,
58 $this->anything(), #$errno,
59 $this->anything(), #$errstr,
60 $this->anything() #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
61 )
62 ->willReturn(false);
63
64 $socket = new TSocket(
65 $host,
66 $port,
67 $persist,
68 $debugHandler
69 );
70 $socket->open();
71 }
72
73 public function openExceptionDataProvider()
74 {
75 yield 'host is empty' => [
76 'host' => '',
77 'port' => 9090,
78 'persist' => null,
79 'debugHandler' => false,
80 'fsockopenCallCount' => 0,
81 'expectedException' => TTransportException::class,
82 'expectedMessage' => 'Cannot open null host',
83 'expectedCode' => TTransportException::NOT_OPEN,
84 ];
85 yield 'port is not positive' => [
86 'host' => 'localhost',
87 'port' => 0,
88 'persist' => false,
89 'debugHandler' => null,
90 'fsockopenCallCount' => 0,
91 'expectedException' => TTransportException::class,
92 'expectedMessage' => 'Cannot open without port',
93 'expectedCode' => TTransportException::NOT_OPEN,
94 ];
95 yield 'connection failure' => [
96 'host' => 'nonexistent-host',
97 'port' => 9090,
98 'persist' => false,
99 'debugHandler' => null,
100 'fsockopenCallCount' => 1,
101 'expectedException' => TException::class,
102 'expectedMessage' => 'TSocket: Could not connect to',
103 'expectedCode' => TTransportException::UNKNOWN,
104 ];
105 }
106
107 public function testDoubleConnect(): void
108 {
109 $host = 'localhost';
110 $port = 9090;
111 $persist = false;
112 $debugHandler = null;
113 $handle = fopen('php://memory', 'r+');
114 $this->getFunctionMock('Thrift\Transport', 'fsockopen')
115 ->expects($this->once())
116 ->with(
117 $host,
118 $port,
119 $this->anything(), #$errno,
120 $this->anything(), #$errstr,
121 $this->anything() #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
122 )
123 ->willReturn($handle);
124
125 $this->getFunctionMock('Thrift\Transport', 'socket_import_stream')
126 ->expects($this->once())
127 ->with($handle)
128 ->willReturn(true);
129
130 $this->getFunctionMock('Thrift\Transport', 'socket_set_option')
131 ->expects($this->once())
132 ->with(
133 $this->anything(), #$socket,
134 SOL_TCP, #$level
135 TCP_NODELAY, #$option
136 1 #$value
137 )
138 ->willReturn(true);
139
140 $transport = new TSocket(
141 $host,
142 $port,
143 $persist,
144 $debugHandler
145 );
146
147 $transport->open();
148 $this->expectException(TTransportException::class);
149 $this->expectExceptionMessage('Socket already connected');
150 $this->expectExceptionCode(TTransportException::ALREADY_OPEN);
151 $transport->open();
152 }
153
154 public function testDebugHandler()
155 {
156 $host = 'nonexistent-host';
157 $port = 9090;
158 $false = false;
159
160 $debugHandler = function ($error) {
161 $this->assertEquals(
162 'TSocket: Could not connect to nonexistent-host:9090 (Connection refused [999])',
163 $error
164 );
165 };
166
167 $this->getFunctionMock('Thrift\Transport', 'fsockopen')
168 ->expects($this->once())
169 ->with(
170 $host,
171 $port,
172 $this->anything(), #$errno,
173 $this->anything(), #$errstr,
174 $this->anything() #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
175 )
176 ->willReturnCallback(
177 function (
178 string $hostname,
179 int $port,
180 &$error_code,
181 &$error_message,
182 ?float $timeout
183 ) {
184 $error_code = 999;
185 $error_message = 'Connection refused';
186
187 return false;
188 }
189 );
190
191 $transport = new TSocket(
192 $host,
193 $port,
194 $false,
195 $debugHandler
196 );
197 $transport->setDebug(true);
198
199 $this->expectException(\Exception::class);
200 $this->expectExceptionMessage('TSocket: Could not connect to');
201 $this->expectExceptionCode(0);
202 $transport->open();
203 }
204
205 public function testOpenPersist()
206 {
207 $host = 'persist-localhost';
208 $port = 9090;
209 $persist = true;
210 $debugHandler = null;
211
212 $handle = fopen('php://memory', 'r+');
213
214 $this->getFunctionMock('Thrift\Transport', 'pfsockopen')
215 ->expects($this->once())
216 ->with(
217 $host,
218 $port,
219 $this->anything(), #$errno,
220 $this->anything(), #$errstr,
221 $this->anything() #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
222 )
223 ->willReturn($handle);
224
225 $this->getFunctionMock('Thrift\Transport', 'socket_import_stream')
226 ->expects($this->once())
227 ->with($handle)
228 ->willReturn(true);
229
230 $this->getFunctionMock('Thrift\Transport', 'socket_set_option')
231 ->expects($this->once())
232 ->with(
233 $this->anything(), #$socket,
234 SOL_TCP, #$level
235 TCP_NODELAY, #$option
236 1 #$value
237 )
238 ->willReturn(true);
239
240 $transport = new TSocket(
241 $host,
242 $port,
243 $persist,
244 $debugHandler
245 );
246
247 $transport->open();
248 $this->assertTrue($transport->isOpen());
249 }
250
251 public function testOpenUnixSocket()
252 {
253 $host = 'unix:///tmp/ipc.sock';
254 $port = -1;
255 $persist = false;
256 $debugHandler = null;
257 $handle = fopen('php://memory', 'r+');
258
259 $this->getFunctionMock('Thrift\Transport', 'fsockopen')
260 ->expects($this->once())
261 ->with(
262 $host,
263 $port,
264 $this->anything(), #$errno,
265 $this->anything(), #$errstr,
266 $this->anything() #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
267 )
268 ->willReturn($handle);
269
270 $this->getFunctionMock('Thrift\Transport', 'socket_import_stream')
271 ->expects($this->once())
272 ->with($handle)
273 ->willReturn(true);
274
275 $this->getFunctionMock('Thrift\Transport', 'socket_set_option')
276 ->expects($this->once())
277 ->with(
278 $this->anything(), #$socket,
279 SOL_TCP, #$level
280 TCP_NODELAY, #$option
281 1 #$value
282 )
283 ->willReturn(true);
284
285 $transport = new TSocket(
286 $host,
287 $port,
288 $persist,
289 $debugHandler
290 );
291
292 $transport->open();
293 }
294
295 /**
296 * @dataProvider openThrift5132DataProvider
297 */
298 public function testOpenThrift5132(
299 $socketImportResult
300 ) {
301 $host = 'localhost';
302 $port = 9090;
303 $persist = false;
304 $debugHandler = null;
305
306 $this->getFunctionMock('Thrift\Transport', 'fsockopen')
307 ->expects($this->once())
308 ->with(
309 $host,
310 $port,
311 $this->anything(), #$errno,
312 $this->anything(), #$errstr,
313 $this->anything() #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
314 )
315 ->willReturn(fopen('php://input', 'r+'));
316
317 $this->getFunctionMock('Thrift\Transport', 'socket_import_stream')
318 ->expects($this->once())
319 ->willReturn($socketImportResult);
320
321 $this->getFunctionMock('Thrift\Transport', 'socket_set_option')
322 ->expects($socketImportResult ? $this->once() : $this->never())
323 ->with(
324 $this->anything(), #$socket,
325 SOL_TCP, #$level
326 TCP_NODELAY, #$option
327 1 #$value
328 )
329 ->willReturn(true);
330
331 $transport = new TSocket(
332 $host,
333 $port,
334 $persist,
335 $debugHandler
336 );
337
338 $transport->open();
339 $this->assertTrue($transport->isOpen());
340 }
341
342 public function openThrift5132DataProvider()
343 {
344 yield 'socket_import_stream success' => [
345 'socketImportResult' => true,
346 ];
347 yield 'socket_import_stream fail' => [
348 'socketImportResult' => false,
349 ];
350 }
351
352 public function testSetHandle()
353 {
354 $host = 'localhost';
355 $port = 9090;
356 $persist = false;
357 $debugHandler = null;
358 $transport = new TSocket(
359 $host,
360 $port,
361 $persist,
362 $debugHandler
363 );
364
365 $this->assertFalse($transport->isOpen());
366 $transport->setHandle(fopen('php://memory', 'r+'));
367 $this->assertTrue($transport->isOpen());
368 }
369
370 public function testSetSendTimeout()
371 {
372 $host = 'localhost';
373 $port = 9090;
374 $persist = false;
375 $debugHandler = null;
376 $transport = new TSocket(
377 $host,
378 $port,
379 $persist,
380 $debugHandler
381 );
382
383 $transport->setSendTimeout(9999);
384 $this->assertEquals(9.0, $this->getPropertyValue($transport, 'sendTimeoutSec_'));
385 $this->assertEquals(999000, $this->getPropertyValue($transport, 'sendTimeoutUsec_'));
386 }
387
388 public function testSetRecvTimeout()
389 {
390 $host = 'localhost';
391 $port = 9090;
392 $persist = false;
393 $debugHandler = null;
394 $transport = new TSocket(
395 $host,
396 $port,
397 $persist,
398 $debugHandler
399 );
400
401 $transport->setRecvTimeout(9999);
402 $this->assertEquals(9.0, $this->getPropertyValue($transport, 'recvTimeoutSec_'));
403 $this->assertEquals(999000, $this->getPropertyValue($transport, 'recvTimeoutUsec_'));
404 }
405
406 /**
407 * @dataProvider hostDataProvider
408 */
409 public function testGetHost($host, $expected)
410 {
411 $port = 9090;
412 $persist = false;
413 $debugHandler = null;
414 $transport = new TSocket(
415 $host,
416 $port,
417 $persist,
418 $debugHandler
419 );
420 $this->assertEquals($expected, $transport->getHost());
421 }
422
423 public function hostDataProvider()
424 {
425 yield 'localhost' => ['localhost', 'localhost'];
426 yield 'ssl_localhost' => ['ssl://localhost', 'ssl://localhost'];
427 yield 'http_localhost' => ['http://localhost', 'http://localhost'];
428 }
429
430 public function testGetPort()
431 {
432 $host = 'localhost';
433 $port = 9090;
434 $persist = false;
435 $debugHandler = null;
436 $transport = new TSocket(
437 $host,
438 $port,
439 $persist,
440 $debugHandler
441 );
442 $this->assertEquals($port, $transport->getPort());
443 }
444
445 public function testClose()
446 {
447 $host = 'localhost';
448 $port = 9090;
449 $persist = false;
450 $debugHandler = null;
451 $transport = new TSocket(
452 $host,
453 $port,
454 $persist,
455 $debugHandler
456 );
457 $transport->setHandle(fopen('php://memory', 'r+'));
458 $this->assertNotNull($this->getPropertyValue($transport, 'handle_'));
459
460 $transport->close();
461 $this->assertNull($this->getPropertyValue($transport, 'handle_'));
462 }
463
464 /**
465 * @dataProvider writeFailDataProvider
466 */
467 public function testWriteFail(
468 $streamSelectResult,
469 $fwriteCallCount,
470 $expectedException,
471 $expectedMessage,
472 $expectedCode
473 ) {
474 $host = 'localhost';
475 $port = 9090;
476 $persist = false;
477 $debugHandler = null;
478 $handle = fopen('php://memory', 'r+');
479
480 $this->getFunctionMock('Thrift\Transport', 'stream_select')
481 ->expects($this->once())
482 ->with(
483 $this->anything(), #$null,
484 [$handle],
485 $this->anything(), #$null,
486 $this->anything(), #$this->sendTimeoutSec_,
487 $this->anything() #$this->sendTimeoutUsec_
488 )
489 ->willReturn($streamSelectResult);
490
491 $this->getFunctionMock('Thrift\Transport', 'fwrite')
492 ->expects($this->exactly($fwriteCallCount))
493 ->with(
494 $handle,
495 'test1234456789132456798'
496 )
497 ->willReturn(false);
498
499 $this->expectException($expectedException);
500 $this->expectExceptionMessage($expectedMessage);
501 $this->expectExceptionCode($expectedCode);
502
503 $transport = new TSocket(
504 $host,
505 $port,
506 $persist,
507 $debugHandler
508 );
509 $transport->setHandle($handle);
510
511 $transport->write('test1234456789132456798');
512 }
513
514 public function testWrite()
515 {
516 $host = 'localhost';
517 $port = 9090;
518 $persist = false;
519 $debugHandler = null;
520 $transport = new TSocket(
521 $host,
522 $port,
523 $persist,
524 $debugHandler
525 );
526 $fileName = sys_get_temp_dir() . '/' . md5(mt_rand(0, time()) . time());
527 touch($fileName);
528 $handle = fopen($fileName, 'r+');
529 $transport->setHandle($handle);
530 $transport->write('test1234456789132456798');
531 $this->assertEquals('test1234456789132456798', file_get_contents($fileName));
532
533 register_shutdown_function(function () use ($fileName) {
534 is_file($fileName) && unlink($fileName);
535 });
536 }
537
538 public function writeFailDataProvider()
539 {
540 yield 'stream_select timeout' => [
541 'streamSelectResult' => 0,
542 'fwriteCallCount' => 0,
543 'expectedException' => TTransportException::class,
544 'expectedMessage' => 'TSocket: timed out writing 23 bytes from localhost:9090',
545 'expectedCode' => 0,
546 ];
547 yield 'stream_select fail write' => [
548 'streamSelectResult' => 1,
549 'fwriteCallCount' => 1,
550 'expectedException' => TTransportException::class,
551 'expectedMessage' => 'TSocket: Could not write 23 bytes localhost:9090',
552 'expectedCode' => 0,
553 ];
554 yield 'stream_select fail' => [
555 'streamSelectResult' => false,
556 'fwriteCallCount' => 0,
557 'expectedException' => TTransportException::class,
558 'expectedMessage' => 'TSocket: Could not write 23 bytes localhost:9090',
559 'expectedCode' => 0,
560 ];
561 }
562
563 public function testRead()
564 {
565 $host = 'localhost';
566 $port = 9090;
567 $persist = false;
568 $debugHandler = null;
569 $transport = new TSocket(
570 $host,
571 $port,
572 $persist,
573 $debugHandler
574 );
575 $fileName = sys_get_temp_dir() . '/' . md5(mt_rand(0, time()) . time());
576 file_put_contents($fileName, '12345678901234567890');
577 $handle = fopen($fileName, 'r+');
578 $transport->setHandle($handle);
579 $this->assertEquals('12345', $transport->read(5));
580
581 register_shutdown_function(function () use ($fileName) {
582 is_file($fileName) && unlink($fileName);
583 });
584 }
585
586 /**
587 * @dataProvider readFailDataProvider
588 */
589 public function testReadFail(
590 $streamSelectResult,
591 $freadResult,
592 $feofResult,
593 $expectedException,
594 $expectedMessage,
595 $expectedCode
596 ) {
597 $host = 'localhost';
598 $port = 9090;
599 $persist = false;
600 $debugHandler = null;
601 $handle = fopen('php://memory', 'r+');
602
603 $this->getFunctionMock('Thrift\Transport', 'stream_select')
604 ->expects($this->once())
605 ->with(
606 [$handle],
607 $this->anything(), #$null,
608 $this->anything(), #$null,
609 $this->anything(), #$this->recvTimeoutSec_,
610 $this->anything() #$this->recvTimeoutUsec_
611 )
612 ->willReturn($streamSelectResult);
613
614 $this->getFunctionMock('Thrift\Transport', 'fread')
615 ->expects($this->exactly($streamSelectResult ? 1 : 0))
616 ->with(
617 $handle,
618 5
619 )
620 ->willReturn($freadResult);
621 $this->getFunctionMock('Thrift\Transport', 'feof')
622 ->expects($this->exactly($feofResult ? 1 : 0))
623 ->with($handle)
624 ->willReturn($feofResult);
625
626 $this->expectException($expectedException);
627 $this->expectExceptionMessage($expectedMessage);
628 $this->expectExceptionCode($expectedCode);
629
630 $transport = new TSocket(
631 $host,
632 $port,
633 $persist,
634 $debugHandler
635 );
636 $transport->setHandle($handle);
637
638 $transport->read(5);
639 }
640
641 public function readFailDataProvider()
642 {
643 yield 'stream_select timeout' => [
644 'streamSelectResult' => 0,
645 'freadResult' => '',
646 'feofResult' => false,
647 'expectedException' => TTransportException::class,
648 'expectedMessage' => 'TSocket: timed out reading 5 bytes from localhost:9090',
649 'expectedCode' => 0,
650 ];
651 yield 'stream_select fail read' => [
652 'streamSelectResult' => 1,
653 'freadResult' => '',
654 'feofResult' => true,
655 'expectedException' => TTransportException::class,
656 'expectedMessage' => 'TSocket read 0 bytes',
657 'expectedCode' => 0,
658 ];
659 yield 'stream_select fail' => [
660 'streamSelectResult' => false,
661 'freadResult' => '',
662 'feofResult' => false,
663 'expectedException' => TTransportException::class,
664 'expectedMessage' => 'TSocket: Could not read 5 bytes from localhost:9090',
665 'expectedCode' => 0,
666 ];
667 yield 'fread false' => [
668 'streamSelectResult' => 1,
669 'freadResult' => false,
670 'feofResult' => false,
671 'expectedException' => TTransportException::class,
672 'expectedMessage' => 'TSocket: Could not read 5 bytes from localhost:9090',
673 'expectedCode' => 0,
674 ];
675 yield 'fread empty' => [
676 'streamSelectResult' => 1,
677 'freadResult' => '',
678 'feofResult' => true,
679 'expectedException' => TTransportException::class,
680 'expectedMessage' => 'TSocket read 0 bytes',
681 'expectedCode' => 0,
682 ];
683 }
684
685 public function testFlush()
686 {
687 $host = 'localhost';
688 $port = 9090;
689 $persist = false;
690 $debugHandler = null;
691 $transport = new TSocket(
692 $host,
693 $port,
694 $persist,
695 $debugHandler
696 );
697 $this->assertNUll($transport->flush());
698 }
699 }