Fixed EZConnect string regex in OracleInstaller class.
authorfreakolowsky <freak@drajv.si>
Sun, 26 May 2013 07:20:38 +0000 (09:20 +0200)
committerfreakolowsky <freak@drajv.si>
Fri, 31 May 2013 14:03:58 +0000 (16:03 +0200)
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
includes/installer/OracleInstaller.php
tests/phpunit/includes/installer/OracleInstallerTest.php [new file with mode: 0644]

index 95cfff7..868e4da 100644 (file)
@@ -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".
index e85d07f..e34bed3 100644 (file)
@@ -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 (file)
index 0000000..7c37f98
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+/**
+ * Tests for OracleInstaller
+ *
+ * @group Database
+ * @group Installer
+ */
+
+class OracleInstallerTest extends MediaWikiTestCase {
+
+       /**
+        * @dataProvider provideOracleConnectStrings
+        */
+       function testCheckConnectStringFormat( $expected, $connectString, $msg = '' ) {
+               $validity = $expected ? 'should be valid' : 'should NOT be valid';
+               $msg = "'$connectString' ($msg) $validity.";
+               $this->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' ),
+               );
+       }
+
+}