|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | +use WP_MySQL_Proxy\Adapter\Adapter; |
| 5 | +use WP_MySQL_Proxy\MySQL_Authentication; |
| 6 | +use WP_MySQL_Proxy\MySQL_Proxy; |
| 7 | +use WP_MySQL_Proxy\MySQL_Session; |
| 8 | + |
| 9 | +class WP_MySQL_Proxy_Limits_Test extends TestCase { |
| 10 | + /** @dataProvider pending_logins */ |
| 11 | + public function test_login_deadline( int $deadline_offset, bool $authenticated, bool $expected_open, ?int $expected_timeout ): void { |
| 12 | + $proxy = new MySQL_Proxy( |
| 13 | + $this->createMock( Adapter::class ), |
| 14 | + array( 'authentication' => new MySQL_Authentication( 'user', 'password' ) ) |
| 15 | + ); |
| 16 | + $clients = $this->property( 'clients' ); |
| 17 | + $clients->setValue( |
| 18 | + $proxy, |
| 19 | + array( |
| 20 | + 1 => array( |
| 21 | + 'socket' => $this->socket(), |
| 22 | + 'session' => $this->session( $authenticated ), |
| 23 | + 'login_deadline' => time() + $deadline_offset, |
| 24 | + ), |
| 25 | + ) |
| 26 | + ); |
| 27 | + |
| 28 | + $this->assertSame( $expected_timeout, $this->call( $proxy, 'get_select_timeout' ) ); |
| 29 | + $this->call( $proxy, 'close_timed_out_logins' ); |
| 30 | + $this->assertCount( $expected_open ? 1 : 0, $clients->getValue( $proxy ) ); |
| 31 | + } |
| 32 | + |
| 33 | + public function pending_logins(): array { |
| 34 | + return array( |
| 35 | + 'pending login within the deadline' => array( 5, false, true, 5 ), |
| 36 | + 'pending login past the deadline' => array( -1, false, false, 0 ), |
| 37 | + 'authenticated past the deadline' => array( -1, true, true, null ), |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** @dataProvider connection_counts */ |
| 42 | + public function test_connection_limit( int $open, bool $expected_accepted ): void { |
| 43 | + $port = $this->free_port(); |
| 44 | + $proxy = new MySQL_Proxy( |
| 45 | + $this->createMock( Adapter::class ), |
| 46 | + array( |
| 47 | + 'authentication' => new MySQL_Authentication( 'user', 'password' ), |
| 48 | + 'port' => $port, |
| 49 | + ) |
| 50 | + ); |
| 51 | + |
| 52 | + // Stand in for the listener the proxy creates in start(). |
| 53 | + $listener = socket_create( AF_INET, SOCK_STREAM, SOL_TCP ); |
| 54 | + socket_set_option( $listener, SOL_SOCKET, SO_REUSEADDR, 1 ); |
| 55 | + socket_bind( $listener, '127.0.0.1', $port ); |
| 56 | + socket_listen( $listener ); |
| 57 | + $this->property( 'socket' )->setValue( $proxy, $listener ); |
| 58 | + |
| 59 | + $clients = $this->property( 'clients' ); |
| 60 | + $occupant = array( |
| 61 | + 'socket' => $this->socket(), |
| 62 | + 'session' => $this->session( true ), |
| 63 | + 'login_deadline' => time() + 60, |
| 64 | + ); |
| 65 | + |
| 66 | + // Key the occupants away from any object ID the accepted socket can get. |
| 67 | + $occupants = array(); |
| 68 | + for ( $i = 1; $i <= $open; $i++ ) { |
| 69 | + $occupants[ PHP_INT_MAX - $i ] = $occupant; |
| 70 | + } |
| 71 | + $clients->setValue( $proxy, $occupants ); |
| 72 | + |
| 73 | + $client = stream_socket_client( "tcp://127.0.0.1:$port", $errno, $error, 2 ); |
| 74 | + stream_set_timeout( $client, 2 ); |
| 75 | + $this->call( $proxy, 'handle_new_client' ); |
| 76 | + $response = fread( $client, 256 ); |
| 77 | + |
| 78 | + if ( $expected_accepted ) { |
| 79 | + $this->assertSame( 10, ord( $response[4] ), 'The client gets the handshake packet.' ); |
| 80 | + $this->assertCount( $open + 1, $clients->getValue( $proxy ) ); |
| 81 | + $accepted = $clients->getValue( $proxy ); |
| 82 | + $this->assertGreaterThan( time(), end( $accepted )['login_deadline'] ); |
| 83 | + } else { |
| 84 | + $this->assertSame( 0xff, ord( $response[4] ) ); |
| 85 | + $this->assertSame( 1040, unpack( 'v', substr( $response, 5, 2 ) )[1] ); |
| 86 | + $this->assertSame( 'Too many connections', substr( $response, 13 ) ); |
| 87 | + $this->assertCount( $open, $clients->getValue( $proxy ), 'A refused client is not registered.' ); |
| 88 | + $this->assertSame( '', fread( $client, 1 ), 'A refused connection is closed.' ); |
| 89 | + } |
| 90 | + |
| 91 | + fclose( $client ); |
| 92 | + socket_close( $listener ); |
| 93 | + } |
| 94 | + |
| 95 | + public function connection_counts(): array { |
| 96 | + return array( |
| 97 | + 'below the limit' => array( MySQL_Proxy::MAX_CONNECTIONS - 1, true ), |
| 98 | + 'at the limit' => array( MySQL_Proxy::MAX_CONNECTIONS, false ), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + private function free_port(): int { |
| 103 | + $listener = stream_socket_server( 'tcp://127.0.0.1:0', $errno, $error ); |
| 104 | + $port = (int) substr( strrchr( stream_socket_get_name( $listener, false ), ':' ), 1 ); |
| 105 | + fclose( $listener ); |
| 106 | + return $port; |
| 107 | + } |
| 108 | + |
| 109 | + private function session( bool $authenticated ): MySQL_Session { |
| 110 | + $session = new MySQL_Session( $this->createMock( Adapter::class ), 1, new MySQL_Authentication( 'user', 'password' ) ); |
| 111 | + if ( $authenticated ) { |
| 112 | + $challenge = MySQL_Test_Client::challenge( $session->get_initial_handshake() ); |
| 113 | + $session->receive_bytes( MySQL_Test_Client::handshake_response( $challenge ) ); |
| 114 | + $this->assertTrue( $session->is_authenticated() ); |
| 115 | + } |
| 116 | + return $session; |
| 117 | + } |
| 118 | + |
| 119 | + private function socket() { |
| 120 | + $pair = array(); |
| 121 | + socket_create_pair( AF_UNIX, SOCK_STREAM, 0, $pair ); |
| 122 | + return $pair[0]; |
| 123 | + } |
| 124 | + |
| 125 | + private function property( string $name ): ReflectionProperty { |
| 126 | + $property = new ReflectionProperty( MySQL_Proxy::class, $name ); |
| 127 | + if ( PHP_VERSION_ID < 80100 ) { |
| 128 | + $property->setAccessible( true ); |
| 129 | + } |
| 130 | + return $property; |
| 131 | + } |
| 132 | + |
| 133 | + private function call( MySQL_Proxy $proxy, string $name ) { |
| 134 | + $method = new ReflectionMethod( MySQL_Proxy::class, $name ); |
| 135 | + if ( PHP_VERSION_ID < 80100 ) { |
| 136 | + $method->setAccessible( true ); |
| 137 | + } |
| 138 | + return $method->invoke( $proxy ); |
| 139 | + } |
| 140 | +} |
0 commit comments