From ffe6e12a8adc58ff880f546eedd5338686d6e3fe Mon Sep 17 00:00:00 2001 From: freakolowsky Date: Sun, 26 May 2013 09:20:38 +0200 Subject: [PATCH] Fixed EZConnect string regex in OracleInstaller class. The Oracle connect string was valid only if it contained alphanumerics, underscore and dot. Some new schmemes makes uses of slashes and EZConnect has the concept of server types (pooled, dedicated, shared) which we now validate. The long regex is now in OracleInstaller::checkConnectStringFormat() (flagged with @since 1.22). The patch provides a bunch of very basic tests to test out the regex. Change-Id: Ie3a0af9801bfdbc9129298be07e1676145a1607a --- includes/installer/Installer.i18n.php | 2 +- includes/installer/OracleInstaller.php | 22 ++++++++- .../installer/OracleInstallerTest.php | 47 +++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/includes/installer/OracleInstallerTest.php diff --git a/includes/installer/Installer.i18n.php b/includes/installer/Installer.i18n.php index 95cfff72fd..868e4da312 100644 --- a/includes/installer/Installer.i18n.php +++ b/includes/installer/Installer.i18n.php @@ -261,7 +261,7 @@ If you do not see the database system you are trying to use listed below, then f 'config-missing-db-host' => 'You must enter a value for "Database host"', 'config-missing-db-server-oracle' => 'You must enter a value for "Database TNS"', 'config-invalid-db-server-oracle' => 'Invalid database TNS "$1". -Use only ASCII letters (a-z, A-Z), numbers (0-9), underscores (_) and dots (.).', +Use either "TNS Name" or an "Easy Connect" string ([http://docs.oracle.com/cd/E11882_01/network.112/e10836/naming.htm Oracle Naming Methods])', 'config-invalid-db-name' => 'Invalid database name "$1". Use only ASCII letters (a-z, A-Z), numbers (0-9), underscores (_) and hyphens (-).', 'config-invalid-db-prefix' => 'Invalid database prefix "$1". diff --git a/includes/installer/OracleInstaller.php b/includes/installer/OracleInstaller.php index e85d07f2bf..e34bed3757 100644 --- a/includes/installer/OracleInstaller.php +++ b/includes/installer/OracleInstaller.php @@ -86,7 +86,7 @@ class OracleInstaller extends DatabaseInstaller { $status = Status::newGood(); if ( !strlen( $newValues['wgDBserver'] ) ) { $status->fatal( 'config-missing-db-server-oracle' ); - } elseif ( !preg_match( '/^[a-zA-Z0-9_\.]+$/', $newValues['wgDBserver'] ) ) { + } elseif ( !self::checkConnectStringFormat( $newValues['wgDBserver'] ) ) { $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] ); } if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) { @@ -296,4 +296,24 @@ class OracleInstaller extends DatabaseInstaller { "; } + /** + * Function checks the format of Oracle connect string + * The actual validity of the string is checked by attempting to connect + * + * Regex should be able to validate all connect string formats + * [//](host|tns_name)[:port][/service_name][:POOLED] + * http://www.orafaq.com/wiki/EZCONNECT + * + * @since 1.22 + * + * @param string $connect_string + * + * @return bool Whether the connection string is valid. + */ + public static function checkConnectStringFormat( $connect_string ) { + $isValid = preg_match( '/^[[:alpha:]][\w\-]*(?:\.[[:alpha:]][\w\-]*){0,2}$/', $connect_string ); // TNS name + $isValid |= preg_match( '/^(?:\/\/)?[\w\-\.]+(?::[\d]+)?(?:\/(?:[\w\-\.]+(?::(pooled|dedicated|shared))?)?(?:\/[\w\-\.]+)?)?$/', $connect_string ); // EZConnect + return (bool)$isValid; + } + } diff --git a/tests/phpunit/includes/installer/OracleInstallerTest.php b/tests/phpunit/includes/installer/OracleInstallerTest.php new file mode 100644 index 0000000000..7c37f98fe5 --- /dev/null +++ b/tests/phpunit/includes/installer/OracleInstallerTest.php @@ -0,0 +1,47 @@ +assertEquals( $expected, + OracleInstaller::checkConnectStringFormat( $connectString ), + $msg + ); + } + + /** + * Provider to test OracleInstaller::checkConnectStringFormat() + */ + function provideOracleConnectStrings() { + // expected result, connectString[, message] + return array( + array( true, 'simple_01', 'Simple TNS name' ), + array( true, 'simple_01.world', 'TNS name with domain' ), + array( true, 'simple_01.domain.net', 'TNS name with domain' ), + array( true, 'host123', 'Host only' ), + array( true, 'host123.domain.net', 'FQDN only' ), + array( true, '//host123.domain.net', 'FQDN URL only' ), + array( true, '123.223.213.132', 'Host IP only' ), + array( true, 'host:1521', 'Host and port' ), + array( true, 'host:1521/service', 'Host, port and service' ), + array( true, 'host:1521/service:shared', 'Host, port, service and shared server type' ), + array( true, 'host:1521/service:dedicated', 'Host, port, service and dedicated server type' ), + array( true, 'host:1521/service:pooled', 'Host, port, service and pooled server type' ), + array( true, 'host:1521/service:shared/instance1', 'Host, port, service, server type and instance' ), + array( true, 'host:1521//instance1', 'Host, port and instance' ), + ); + } + +} -- 2.20.1