Skip to content

Commit

Permalink
Merge pull request #9006 from kenjis/fix-oci8-easy-connect-string-val…
Browse files Browse the repository at this point in the history
…idation

fix: [OCI8] Easy Connect string validation
  • Loading branch information
kenjis authored Jun 30, 2024
2 parents d14dce8 + 6c61938 commit 1638990
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
22 changes: 18 additions & 4 deletions system/Database/OCI8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

/**
Expand Down
74 changes: 74 additions & 0 deletions tests/system/Database/Live/OCI8/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* 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<string, mixed> 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<string, list<string>>
*/
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'],
];
}
}

0 comments on commit 1638990

Please sign in to comment.