Skip to content

Commit acb8c18

Browse files
committed
test: fix legacy TLS expectations and socket cleanup
Account for PHP 7.1 and 7.2 negotiating TLS 1.3 with combined crypto methods when OpenSSL supports it, including their UNKNOWN protocol metadata. Always close client and server sockets in finally blocks so failed assertions cannot leave later event-loop tests hanging.
1 parent 13cb13d commit acb8c18

1 file changed

Lines changed: 44 additions & 28 deletions

File tree

‎tests/FunctionalSecureServerTest.php‎

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testClientUsesTls13ByDefaultWhenSupportedByOpenSSL()
8282
}
8383

8484
/** @dataProvider provideClientCryptoMethods */
85-
public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClient($method)
85+
public function testClientUsesExpectedTlsVersionWhenCryptoMethodIsExplicitlyConfiguredByClient($method, $expectedProtocol)
8686
{
8787
$server = new TcpServer(0);
8888
$server = new SecureServer($server, null, [
@@ -95,30 +95,38 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClien
9595
]);
9696
$promise = $connector->connect($server->getAddress());
9797

98-
/* @var ConnectionInterface $client */
99-
$client = await(timeout($promise, self::TIMEOUT));
100-
101-
$this->assertInstanceOf(Connection::class, $client);
102-
$this->assertTrue(isset($client->stream));
103-
104-
$meta = stream_get_meta_data($client->stream);
105-
$this->assertTrue(isset($meta['crypto']['protocol']));
106-
$this->assertEquals('TLSv1.2', $meta['crypto']['protocol']);
98+
try {
99+
/* @var ConnectionInterface $client */
100+
$client = await(timeout($promise, self::TIMEOUT));
107101

108-
$client->close();
109-
$server->close();
102+
$this->assertInstanceOf(Connection::class, $client);
103+
$this->assertTrue(isset($client->stream));
104+
105+
$meta = stream_get_meta_data($client->stream);
106+
$this->assertTrue(isset($meta['crypto']['protocol']));
107+
// Older PHP versions expose TLS 1.3 only through the cipher version.
108+
$this->assertEquals($expectedProtocol, $meta['crypto']['protocol'] === 'UNKNOWN' ? $meta['crypto']['cipher_version'] : $meta['crypto']['protocol']);
109+
} finally {
110+
if (isset($client)) {
111+
$client->close();
112+
}
113+
$server->close();
114+
}
110115
}
111116

112117
public function provideClientCryptoMethods()
113118
{
119+
// PHP < 7.3 does not cap combined protocol flags at TLS 1.2 with OpenSSL 1.1.1+.
120+
$combinedProtocol = PHP_VERSION_ID < 70300 && $this->supportsTls13() ? 'TLSv1.3' : 'TLSv1.2';
121+
114122
return [
115-
'single method' => [STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT],
116-
'combined methods' => [STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT]
123+
'single method' => [STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, 'TLSv1.2'],
124+
'combined methods' => [STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, $combinedProtocol]
117125
];
118126
}
119127

120128
/** @dataProvider provideServerCryptoMethods */
121-
public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServer($method)
129+
public function testClientUsesExpectedTlsVersionWhenCryptoMethodIsExplicitlyConfiguredByServer($method, $expectedProtocol)
122130
{
123131
$server = new TcpServer(0);
124132
$server = new SecureServer($server, null, [
@@ -131,25 +139,33 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServe
131139
]);
132140
$promise = $connector->connect($server->getAddress());
133141

134-
/* @var ConnectionInterface $client */
135-
$client = await(timeout($promise, self::TIMEOUT));
136-
137-
$this->assertInstanceOf(Connection::class, $client);
138-
$this->assertTrue(isset($client->stream));
139-
140-
$meta = stream_get_meta_data($client->stream);
141-
$this->assertTrue(isset($meta['crypto']['protocol']));
142-
$this->assertEquals('TLSv1.2', $meta['crypto']['protocol']);
142+
try {
143+
/* @var ConnectionInterface $client */
144+
$client = await(timeout($promise, self::TIMEOUT));
143145

144-
$client->close();
145-
$server->close();
146+
$this->assertInstanceOf(Connection::class, $client);
147+
$this->assertTrue(isset($client->stream));
148+
149+
$meta = stream_get_meta_data($client->stream);
150+
$this->assertTrue(isset($meta['crypto']['protocol']));
151+
// Older PHP versions expose TLS 1.3 only through the cipher version.
152+
$this->assertEquals($expectedProtocol, $meta['crypto']['protocol'] === 'UNKNOWN' ? $meta['crypto']['cipher_version'] : $meta['crypto']['protocol']);
153+
} finally {
154+
if (isset($client)) {
155+
$client->close();
156+
}
157+
$server->close();
158+
}
146159
}
147160

148161
public function provideServerCryptoMethods()
149162
{
163+
// PHP < 7.3 does not cap combined protocol flags at TLS 1.2 with OpenSSL 1.1.1+.
164+
$combinedProtocol = PHP_VERSION_ID < 70300 && $this->supportsTls13() ? 'TLSv1.3' : 'TLSv1.2';
165+
150166
return [
151-
'single method' => [STREAM_CRYPTO_METHOD_TLSv1_2_SERVER],
152-
'combined methods' => [STREAM_CRYPTO_METHOD_TLSv1_1_SERVER | STREAM_CRYPTO_METHOD_TLSv1_2_SERVER]
167+
'single method' => [STREAM_CRYPTO_METHOD_TLSv1_2_SERVER, 'TLSv1.2'],
168+
'combined methods' => [STREAM_CRYPTO_METHOD_TLSv1_1_SERVER | STREAM_CRYPTO_METHOD_TLSv1_2_SERVER, $combinedProtocol]
153169
];
154170
}
155171

0 commit comments

Comments
 (0)