From 41f13153acadffadd483e371394b831e2ed08852 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 12 Apr 2017 07:59:41 +0700 Subject: [PATCH 1/3] cast to (int) for Curl adapter timeout config --- src/Client/Adapter/Curl.php | 4 ++-- test/Client/CurlTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Client/Adapter/Curl.php b/src/Client/Adapter/Curl.php index 9d830e4619..5cf9a1e48e 100644 --- a/src/Client/Adapter/Curl.php +++ b/src/Client/Adapter/Curl.php @@ -202,9 +202,9 @@ public function connect($host, $port = 80, $secure = false) } if (isset($this->config['connecttimeout'])) { - $connectTimeout = $this->config['connecttimeout']; + $connectTimeout = (int) $this->config['connecttimeout']; } elseif (isset($this->config['timeout'])) { - $connectTimeout = $this->config['timeout']; + $connectTimeout = (int) $this->config['timeout']; } else { $connectTimeout = null; } diff --git a/test/Client/CurlTest.php b/test/Client/CurlTest.php index 0e6c75fc92..1c005b1726 100644 --- a/test/Client/CurlTest.php +++ b/test/Client/CurlTest.php @@ -96,6 +96,22 @@ public function testConfigSetAsZendConfig() $this->assertEquals($config->nested->item, $hasConfig['nested']['item']); } + /** + * Test not integer timeout casted to int + * + */ + public function testCastTimeOutConfig() + { + $config = new Config([ + 'timeout' => "timeout", + ]); + + $this->_adapter->setOptions($config); + + $hasConfig = $this->_adapter->getConfig(); + $this->assertEquals((int) $config->timeout, $hasConfig['timeout']); + } + /** * Check that an exception is thrown when trying to set invalid config * From 5b36ca3daf51a02763c409a291f0e7c79b9828cc Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 3 Oct 2018 14:58:10 +0700 Subject: [PATCH 2/3] apply integer and numeric check --- src/Client/Adapter/Curl.php | 16 ++++++++++++++-- test/Client/CurlTest.php | 32 +++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/Client/Adapter/Curl.php b/src/Client/Adapter/Curl.php index 5cf9a1e48e..1dac31df3d 100644 --- a/src/Client/Adapter/Curl.php +++ b/src/Client/Adapter/Curl.php @@ -202,12 +202,24 @@ public function connect($host, $port = 80, $secure = false) } if (isset($this->config['connecttimeout'])) { - $connectTimeout = (int) $this->config['connecttimeout']; + $connectTimeout = $this->config['connecttimeout']; } elseif (isset($this->config['timeout'])) { - $connectTimeout = (int) $this->config['timeout']; + $connectTimeout = $this->config['timeout']; } else { $connectTimeout = null; } + + if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) { + throw new AdapterException\InvalidArgumentException(sprintf( + 'integer or numeric string expected, got %s', + gettype($connectTimeout) + )); + } + + if ($connectTimeout !== null) { + $connectTimeout = (int) $connectTimeout; + } + if ($connectTimeout !== null) { if (defined('CURLOPT_CONNECTTIMEOUT_MS')) { curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT_MS, $connectTimeout * 1000); diff --git a/test/Client/CurlTest.php b/test/Client/CurlTest.php index 1c005b1726..6dc81a3fc2 100644 --- a/test/Client/CurlTest.php +++ b/test/Client/CurlTest.php @@ -96,20 +96,34 @@ public function testConfigSetAsZendConfig() $this->assertEquals($config->nested->item, $hasConfig['nested']['item']); } + public function provideValidTimeoutConfig() + { + return [ + 'integer' => [10], + 'numeric' => ['10'], + ]; + } + /** - * Test not integer timeout casted to int - * + * @dataProvider provideValidTimeoutConfig */ - public function testCastTimeOutConfig() + public function testPassValidTimeout($timeout) { - $config = new Config([ - 'timeout' => "timeout", - ]); + $adapter = new Adapter\Curl(); + $adapter->setOptions(['timeout' => $timeout]); - $this->_adapter->setOptions($config); + $adapter->connect('http://framework.zend.com'); + } - $hasConfig = $this->_adapter->getConfig(); - $this->assertEquals((int) $config->timeout, $hasConfig['timeout']); + public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout() + { + $adapter = new Adapter\Curl(); + $adapter->setOptions(['timeout' => 'timeout']); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('integer or numeric string expected, got string'); + + $adapter->connect('http://framework.zend.com'); } /** From 4dc2c305864986624ec7156962015ed76ba54f48 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 3 Oct 2018 15:05:44 +0700 Subject: [PATCH 3/3] apply integer and numeric check in Socket adapter --- src/Client/Adapter/Socket.php | 8 ++++++++ test/Client/SocketTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Client/Adapter/Socket.php b/src/Client/Adapter/Socket.php index 2319a4785f..26a17217a8 100644 --- a/src/Client/Adapter/Socket.php +++ b/src/Client/Adapter/Socket.php @@ -262,6 +262,14 @@ public function connect($host, $port = 80, $secure = false) } else { $connectTimeout = $this->config['timeout']; } + + if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) { + throw new AdapterException\InvalidArgumentException(sprintf( + 'integer or numeric string expected, got %s', + gettype($connectTimeout) + )); + } + ErrorHandler::start(); $this->socket = stream_socket_client( $host . ':' . $port, diff --git a/test/Client/SocketTest.php b/test/Client/SocketTest.php index 1610c571f2..e1504041c8 100644 --- a/test/Client/SocketTest.php +++ b/test/Client/SocketTest.php @@ -171,6 +171,36 @@ public function testSetConfigInvalidConfig($config) $this->_adapter->setOptions($config); } + public function provideValidTimeoutConfig() + { + return [ + 'integer' => [10], + 'numeric' => ['10'], + ]; + } + + /** + * @dataProvider provideValidTimeoutConfig + */ + public function testPassValidTimeout($timeout) + { + $adapter = new Adapter\Socket(); + $adapter->setOptions(['timeout' => $timeout]); + + $adapter->connect('http://framework.zend.com'); + } + + public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout() + { + $adapter = new Adapter\Socket(); + $adapter->setOptions(['timeout' => 'timeout']); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('integer or numeric string expected, got string'); + + $adapter->connect('http://framework.zend.com'); + } + /** * Stream context related tests */