From 8c603af8a1fcbdb5543ea12f7f2921d2c24a1512 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 27 Jun 2024 09:41:21 +0900 Subject: [PATCH 1/2] test: add tests for isValidDSN() --- .../Database/Live/OCI8/ConnectionTest.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/system/Database/Live/OCI8/ConnectionTest.php diff --git a/tests/system/Database/Live/OCI8/ConnectionTest.php b/tests/system/Database/Live/OCI8/ConnectionTest.php new file mode 100644 index 000000000000..caf97ed632fd --- /dev/null +++ b/tests/system/Database/Live/OCI8/ConnectionTest.php @@ -0,0 +1,74 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Database\Live\OCI8; + +use CodeIgniter\Database\OCI8\Connection; +use CodeIgniter\Test\CIUnitTestCase; +use Config\Database as DbConfig; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('DatabaseLive')] +final class ConnectionTest extends CIUnitTestCase +{ + /** + * @var array Database connection settings + */ + private array $settings = []; + + protected function setUp(): void + { + $dbConfig = config(DbConfig::class); + $this->settings = $dbConfig->{$this->DBGroup}; + + if ($this->settings['DBDriver'] !== 'OCI8') { + $this->markTestSkipped('This test is only for OCI8.'); + } + } + + #[DataProvider('provideIsValidDSN')] + public function testIsValidDSN(string $dsn): void + { + $this->settings['DSN'] = $dsn; + + $db = new Connection($this->settings); + + $isValidDSN = $this->getPrivateMethodInvoker($db, 'isValidDSN'); + + $this->assertTrue($isValidDSN()); + } + + /** + * @return array> + */ + public static function provideIsValidDSN(): iterable + { + yield from [ + // Easy Connect string + // See https://docs.oracle.com/en/database/oracle/oracle-database/23/netag/configuring-naming-methods.html#GUID-36F3A17D-843C-490A-8A23-FB0FE005F8E8 + 'HostOnly' => ['sales-server'], + 'Host:Port' => ['sales-server:3456'], + 'Host/ServiceName' => ['sales-server/sales'], + 'IPv6Address:Port/ServiceName' => ['[2001:0db8:0:0::200C:417A]:80/sales'], + 'Host:Port/ServiceName' => ['sales-server:80/sales'], + 'Host/ServiceName:ServerType/InstanceName' => ['sales-server/sales:dedicated/inst1'], + 'Host:InstanceName' => ['sales-server//inst1'], + 'Host/ServiceNameWithDots' => ['myhost/my.service.name'], + 'Host:Port/ServiceNameWithDots' => ['myhost:1521/my.service.name'], + ]; + } +} From 6c61938fc57786e664068b5303de24ac04a82eba Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 27 Jun 2024 09:43:21 +0900 Subject: [PATCH 2/2] fix: regex pattern for Easy Connect string --- system/Database/OCI8/Connection.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/system/Database/OCI8/Connection.php b/system/Database/OCI8/Connection.php index f7983a695c0c..2b870b755b44 100644 --- a/system/Database/OCI8/Connection.php +++ b/system/Database/OCI8/Connection.php @@ -53,10 +53,24 @@ class Connection extends BaseConnection ]; protected $validDSNs = [ - 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS - // Easy Connect string (Oracle 10g+) - 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^\/])?(\/[a-z0-9$_]+)?$/i', - 'in' => '/^[a-z0-9$_]+$/i', // Instance name (defined in tnsnames.ora) + // TNS + 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', + // Easy Connect string (Oracle 10g+). + // https://docs.oracle.com/en/database/oracle/oracle-database/23/netag/configuring-naming-methods.html#GUID-36F3A17D-843C-490A-8A23-FB0FE005F8E8 + // [//]host[:port][/[service_name][:server_type][/instance_name]] + 'ec' => '/^ + (\/\/)? + (\[)?[a-z0-9.:_-]+(\])? # Host or IP address + (:[1-9][0-9]{0,4})? # Port + ( + (\/) + ([a-z0-9.$_]+)? # Service name + (:[a-z]+)? # Server type + (\/[a-z0-9$_]+)? # Instance name + )? + $/ix', + // Instance name (defined in tnsnames.ora) + 'in' => '/^[a-z0-9$_]+$/i', ]; /**