Skip to content

Commit 1638990

Browse files
authored
Merge pull request #9006 from kenjis/fix-oci8-easy-connect-string-validation
fix: [OCI8] Easy Connect string validation
2 parents d14dce8 + 6c61938 commit 1638990

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

system/Database/OCI8/Connection.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,24 @@ class Connection extends BaseConnection
5353
];
5454

5555
protected $validDSNs = [
56-
'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS
57-
// Easy Connect string (Oracle 10g+)
58-
'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^\/])?(\/[a-z0-9$_]+)?$/i',
59-
'in' => '/^[a-z0-9$_]+$/i', // Instance name (defined in tnsnames.ora)
56+
// TNS
57+
'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/',
58+
// Easy Connect string (Oracle 10g+).
59+
// https://docs.oracle.com/en/database/oracle/oracle-database/23/netag/configuring-naming-methods.html#GUID-36F3A17D-843C-490A-8A23-FB0FE005F8E8
60+
// [//]host[:port][/[service_name][:server_type][/instance_name]]
61+
'ec' => '/^
62+
(\/\/)?
63+
(\[)?[a-z0-9.:_-]+(\])? # Host or IP address
64+
(:[1-9][0-9]{0,4})? # Port
65+
(
66+
(\/)
67+
([a-z0-9.$_]+)? # Service name
68+
(:[a-z]+)? # Server type
69+
(\/[a-z0-9$_]+)? # Instance name
70+
)?
71+
$/ix',
72+
// Instance name (defined in tnsnames.ora)
73+
'in' => '/^[a-z0-9$_]+$/i',
6074
];
6175

6276
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Live\OCI8;
15+
16+
use CodeIgniter\Database\OCI8\Connection;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use Config\Database as DbConfig;
19+
use PHPUnit\Framework\Attributes\DataProvider;
20+
use PHPUnit\Framework\Attributes\Group;
21+
22+
/**
23+
* @internal
24+
*/
25+
#[Group('DatabaseLive')]
26+
final class ConnectionTest extends CIUnitTestCase
27+
{
28+
/**
29+
* @var array<string, mixed> Database connection settings
30+
*/
31+
private array $settings = [];
32+
33+
protected function setUp(): void
34+
{
35+
$dbConfig = config(DbConfig::class);
36+
$this->settings = $dbConfig->{$this->DBGroup};
37+
38+
if ($this->settings['DBDriver'] !== 'OCI8') {
39+
$this->markTestSkipped('This test is only for OCI8.');
40+
}
41+
}
42+
43+
#[DataProvider('provideIsValidDSN')]
44+
public function testIsValidDSN(string $dsn): void
45+
{
46+
$this->settings['DSN'] = $dsn;
47+
48+
$db = new Connection($this->settings);
49+
50+
$isValidDSN = $this->getPrivateMethodInvoker($db, 'isValidDSN');
51+
52+
$this->assertTrue($isValidDSN());
53+
}
54+
55+
/**
56+
* @return array<string, list<string>>
57+
*/
58+
public static function provideIsValidDSN(): iterable
59+
{
60+
yield from [
61+
// Easy Connect string
62+
// See https://docs.oracle.com/en/database/oracle/oracle-database/23/netag/configuring-naming-methods.html#GUID-36F3A17D-843C-490A-8A23-FB0FE005F8E8
63+
'HostOnly' => ['sales-server'],
64+
'Host:Port' => ['sales-server:3456'],
65+
'Host/ServiceName' => ['sales-server/sales'],
66+
'IPv6Address:Port/ServiceName' => ['[2001:0db8:0:0::200C:417A]:80/sales'],
67+
'Host:Port/ServiceName' => ['sales-server:80/sales'],
68+
'Host/ServiceName:ServerType/InstanceName' => ['sales-server/sales:dedicated/inst1'],
69+
'Host:InstanceName' => ['sales-server//inst1'],
70+
'Host/ServiceNameWithDots' => ['myhost/my.service.name'],
71+
'Host:Port/ServiceNameWithDots' => ['myhost:1521/my.service.name'],
72+
];
73+
}
74+
}

0 commit comments

Comments
 (0)