|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Psl\Tests\Unit\TLS; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Psl\Async; |
| 9 | +use Psl\TCP; |
| 10 | +use Psl\TLS; |
| 11 | + |
| 12 | +final class TCPConnectorTest extends TestCase |
| 13 | +{ |
| 14 | + private const string CERT_FILE = __DIR__ . '/../../fixture/certs/server.crt'; |
| 15 | + private const string KEY_FILE = __DIR__ . '/../../fixture/certs/server.key'; |
| 16 | + |
| 17 | + public function testTCPConnectorImplementsConnectorInterface(): void |
| 18 | + { |
| 19 | + $connector = new TLS\TCPConnector(new TCP\Connector(), TLS\Connector::default()); |
| 20 | + |
| 21 | + static::assertInstanceOf(TCP\ConnectorInterface::class, $connector); |
| 22 | + } |
| 23 | + |
| 24 | + public function testTCPConnectorConnectsAndUpgradesToTls(): void |
| 25 | + { |
| 26 | + $cert = TLS\Certificate::create(self::CERT_FILE, self::KEY_FILE); |
| 27 | + $server_config = TLS\ServerConfig::create($cert); |
| 28 | + $acceptor = new TLS\Acceptor($server_config); |
| 29 | + |
| 30 | + $listener = TCP\listen('127.0.0.1', 0); |
| 31 | + $port = $listener->getLocalAddress()->port; |
| 32 | + |
| 33 | + Async\concurrently([ |
| 34 | + 'server' => static function () use ($listener, $acceptor): void { |
| 35 | + $connection = $listener->accept(); |
| 36 | + $tls = $acceptor->accept($connection); |
| 37 | + $data = $tls->read(); |
| 38 | + static::assertSame('hello from tcp-connector', $data); |
| 39 | + $tls->writeAll('hello back'); |
| 40 | + $tls->close(); |
| 41 | + $listener->close(); |
| 42 | + }, |
| 43 | + 'client' => static function () use ($port): void { |
| 44 | + $config = TLS\ClientConfig::default()->withPeerVerification(false)->withAllowSelfSigned(true); |
| 45 | + |
| 46 | + $connector = new TLS\TCPConnector(new TCP\Connector(), new TLS\Connector($config)); |
| 47 | + |
| 48 | + $stream = $connector->connect('127.0.0.1', $port); |
| 49 | + |
| 50 | + static::assertInstanceOf(TCP\StreamInterface::class, $stream); |
| 51 | + static::assertInstanceOf(TLS\StreamInterface::class, $stream); |
| 52 | + |
| 53 | + $stream->writeAll('hello from tcp-connector'); |
| 54 | + $response = $stream->readAll(); |
| 55 | + static::assertSame('hello back', $response); |
| 56 | + $stream->close(); |
| 57 | + }, |
| 58 | + ]); |
| 59 | + } |
| 60 | + |
| 61 | + public function testTCPConnectorWorksWithSocketPool(): void |
| 62 | + { |
| 63 | + $cert = TLS\Certificate::create(self::CERT_FILE, self::KEY_FILE); |
| 64 | + $server_config = TLS\ServerConfig::create($cert); |
| 65 | + $acceptor = new TLS\Acceptor($server_config); |
| 66 | + |
| 67 | + $listener = TCP\listen('127.0.0.1', 0); |
| 68 | + $port = $listener->getLocalAddress()->port; |
| 69 | + |
| 70 | + Async\concurrently([ |
| 71 | + 'server' => static function () use ($listener, $acceptor): void { |
| 72 | + $connection = $listener->accept(); |
| 73 | + $tls = $acceptor->accept($connection); |
| 74 | + $data = $tls->read(); |
| 75 | + static::assertSame('pooled-request', $data); |
| 76 | + $tls->writeAll('pooled-response'); |
| 77 | + $tls->close(); |
| 78 | + $listener->close(); |
| 79 | + }, |
| 80 | + 'client' => static function () use ($port): void { |
| 81 | + $config = TLS\ClientConfig::default()->withPeerVerification(false)->withAllowSelfSigned(true); |
| 82 | + |
| 83 | + $pool = new TCP\SocketPool(new TLS\TCPConnector(new TCP\Connector(), new TLS\Connector($config))); |
| 84 | + |
| 85 | + $stream = $pool->checkout('127.0.0.1', $port); |
| 86 | + static::assertInstanceOf(TLS\StreamInterface::class, $stream); |
| 87 | + |
| 88 | + $stream->writeAll('pooled-request'); |
| 89 | + $response = $stream->readAll(); |
| 90 | + static::assertSame('pooled-response', $response); |
| 91 | + |
| 92 | + $pool->clear($stream); |
| 93 | + $pool->close(); |
| 94 | + }, |
| 95 | + ]); |
| 96 | + } |
| 97 | +} |
0 commit comments