db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From be...@apache.org
Subject svn commit: r546861 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/derbynet/ junit/
Date Wed, 13 Jun 2007 13:19:16 GMT
Author: bernt
Date: Wed Jun 13 06:19:15 2007
New Revision: 546861

URL: http://svn.apache.org/viewvc?view=rev&rev=546861
Log:
DERBY-2274 Implement testing for SSL communication

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTest.java
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTestServerKey.key
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeSSLSetup.java   (with
props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/build.xml
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTest.java?view=auto&rev=546861
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTest.java
Wed Jun 13 06:19:15 2007
@@ -0,0 +1,200 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.SSLTest
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+
+package org.apache.derbyTesting.functionTests.tests.derbynet;
+
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Properties;
+import java.sql.DriverManager;
+import java.sql.Connection;
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.derbyTesting.junit.BaseTestCase;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.Derby;
+import org.apache.derbyTesting.junit.NetworkServerTestSetup;
+import org.apache.derbyTesting.junit.SecurityManagerSetup;
+import org.apache.derbyTesting.junit.ServerSetup;
+import org.apache.derbyTesting.junit.SupportFilesSetup;
+import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+import org.apache.derbyTesting.junit.JDBCDataSource;
+
+import org.apache.derby.drda.NetworkServerControl;
+
+/**
+ * Tests connects to an SSL server
+ */
+
+public class SSLTest extends BaseJDBCTestCase
+{
+    // helper state for intercepting server error messages
+    private InputStream[]  _inputStreamHolder;
+    /** Reference to the enclosing NetworkServerTestSetup. */
+    private NetworkServerTestSetup nsTestSetup;
+
+    // Constructors
+
+    public SSLTest(String testName)
+    {
+        super(testName);
+        _inputStreamHolder = new InputStream[1];
+    }
+    
+    // JUnit machinery
+    
+    /**
+     * Tests to run.
+     */
+    public static Test suite()
+    {
+        //NetworkServerTestSetup.setWaitTime(10000L);
+        
+        TestSuite suite = new TestSuite("SSLTest");
+        
+        // Server booting requires that we run from the jar files
+        if (!TestConfiguration.loadingFromJars()) { return suite; }
+        
+        // Need derbynet.jar in the classpath!
+        if (!Derby.hasServer())
+            return suite;
+        
+        suite.addTest(decorateTest("testSSLBasicDSConnect"));
+        suite.addTest(decorateTest("testSSLBasicDSPlainConnect"));
+        return suite;
+    }
+    
+    /**
+     * Release resources.
+     */
+
+    protected void tearDown() throws Exception
+    {
+        _inputStreamHolder = null;
+    }
+    
+
+    // Test decoration
+    
+    /**
+     * <p>
+     * Compose the required decorators to bring up the server in the correct
+     * configuration.
+     * </p>
+     */
+    private static Test decorateTest(String testName)
+    {
+        SSLTest sslTest = 
+            new SSLTest(testName);
+        
+        String[] startupProperties = 
+            getStartupProperties();
+
+        String[] startupArgs = new String[]{};
+        
+        NetworkServerTestSetup networkServerTestSetup =
+            new NetworkServerTestSetup(sslTest,
+                                       startupProperties,
+                                       startupArgs,
+                                       true,
+                                       true, 
+                                       sslTest._inputStreamHolder);
+        
+        sslTest.nsTestSetup = networkServerTestSetup;
+        
+        Test testSetup =
+            SecurityManagerSetup.noSecurityManager(networkServerTestSetup);
+        
+        testSetup = 
+            new SupportFilesSetup(testSetup,
+                                  null,
+                                  new String[] 
+                                  {"functionTests/tests/derbynet/SSLTestServerKey.key"},
+                                  null,
+                                  new String[] 
+                                  {"SSLTestServerKey.key"}
+                                  );
+        Test test = TestConfiguration.defaultServerDecorator(testSetup);
+
+        test = TestConfiguration.changeSSLDecorator(test, "basic");
+
+        return test;
+    }
+    
+    /**
+     * <p>
+     * Return a set of startup properties suitable for SSLTest.
+     * </p>
+     */
+    private static  String[]  getStartupProperties()
+    {
+        ArrayList list = new ArrayList();
+        list.add("javax.net.ssl.keyStore=extinout/SSLTestServerKey.key");
+        list.add("javax.net.ssl.keyStorePassword=qwerty");
+        String[] result = new String[ list.size()];
+        list.toArray(result);
+        return result;
+    }
+    
+    // JUnit Tests
+    
+    /**
+     * Test that a basic SSL connect succeeds.
+     **/
+
+    public void testSSLBasicDSConnect()
+        throws Exception
+    {   
+        DataSource ds = JDBCDataSource.getDataSource();
+        JDBCDataSource.setBeanProperty(ds,"createDatabase","create");
+        JDBCDataSource.setBeanProperty(ds,"ssl","basic");
+        Connection c1 = ds.getConnection();
+        c1.close();
+    }
+
+    /**
+     * Test that a plaintext connect will fail.
+     **/
+
+    public void testSSLBasicDSPlainConnect()
+        throws Exception
+    {   
+        DataSource ds = JDBCDataSource.getDataSource();
+        JDBCDataSource.setBeanProperty(ds,"createDatabase","create");
+        
+        try {
+            Connection c2 = ds.getConnection();
+            c2.close();
+            fail();
+        } catch (SQLException e) {
+            assertSQLState("58009", e);
+        }
+    }
+}
+

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTestServerKey.key
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTestServerKey.key?view=auto&rev=546861
==============================================================================
Binary file - no diff available.

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SSLTestServerKey.key
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java?view=diff&rev=546861&r1=546860&r2=546861
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
Wed Jun 13 06:19:15 2007
@@ -53,6 +53,7 @@
         suite.addTest(BadConnectionTest.suite());
         suite.addTest(NetHarnessJavaTest.suite());
         suite.addTest(SecureServerTest.suite());
+        suite.addTest(SSLTest.suite());
         suite.addTest(NetIjTest.suite());
         
         // Disabled due to "java.sql.SQLSyntaxErrorException: The class

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/build.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/build.xml?view=diff&rev=546861&r1=546860&r2=546861
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/build.xml
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/build.xml
Wed Jun 13 06:19:15 2007
@@ -115,7 +115,7 @@
   <target name="copyfiles">
     <copy todir="${out.dir}/${derby.testing.functest.dir}/tests/derbynet">
       <fileset dir="${derby.testing.src.dir}/${derby.testing.functest.dir}/tests/derbynet"

-        includes="*.inc,*.policy,*.properties,*.sql,*.tests"/>
+        includes="*.inc,*.policy,*.properties,*.sql,*.tests,*.key"/>
     </copy>
   </target> 
 

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeSSLSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeSSLSetup.java?view=auto&rev=546861
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeSSLSetup.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeSSLSetup.java Wed
Jun 13 06:19:15 2007
@@ -0,0 +1,44 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.junit.ChageSSLSetup
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+ * either express or implied. See the License for the specific 
+ * language governing permissions and limitations under the License.
+ */
+package org.apache.derbyTesting.junit;
+
+import junit.framework.Test;
+
+/**
+ * A decorator that changes the default ssl mode
+ * for the current configuration. Its tearDown method restores
+ * the previous configuration.
+ * 
+ */
+final class ChangeSSLSetup extends ChangeConfigurationSetup {
+
+    private final String ssl;
+
+    ChangeSSLSetup(Test test, String ssl)
+    {
+        super(test);
+        this.ssl = ssl;
+    }
+    
+    TestConfiguration getNewConfiguration(TestConfiguration old)
+    {
+        return new TestConfiguration(old, ssl);
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeSSLSetup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java?view=diff&rev=546861&r1=546860&r2=546861
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java
Wed Jun 13 06:19:15 2007
@@ -266,13 +266,25 @@
     {
         TestConfiguration config = TestConfiguration.getCurrent();
         
-        return new String[] {
-            "start",
-            "-h",
-            config.getHostName(),
-            "-p",
-            Integer.toString(config.getPort())
-        };
+        if (config.getSsl() == null) {
+            return new String[] {
+                "start",
+                "-h",
+                config.getHostName(),
+                "-p",
+                Integer.toString(config.getPort())
+            };
+        } else {
+            return new String[] {
+                "start",
+                "-h",
+                config.getHostName(),
+                "-p",
+                Integer.toString(config.getPort()),
+                "-ssl",
+                config.getSsl()
+            };
+        }
     }
     
     /* Network Server Control */
@@ -289,8 +301,27 @@
         throws Exception
     {
         TestConfiguration config = TestConfiguration.getCurrent();
-        return new NetworkServerControl
-        (InetAddress.getByName(config.getHostName()), config.getPort());
+        if (config.getSsl() == null) {
+            return new NetworkServerControl
+                (InetAddress.getByName(config.getHostName()), 
+                 config.getPort());
+        } else {
+            // This is a hack. A NetworkServerControl constructor with
+            // the needed interface to control sslMode (and possibly
+            // more) would be better.
+            String oldValue = 
+                System.setProperty("derby.drda.sslMode", config.getSsl());
+            NetworkServerControl control = new NetworkServerControl
+                (InetAddress.getByName(config.getHostName()), 
+                 config.getPort());
+            if (oldValue == null) {
+                // JDK 1.4 does not have clearProperty....
+                System.getProperties().remove("derby.drda.sslMode");
+            } else {
+                System.setProperty("derby.drda.sslMode", oldValue);
+            }
+            return control;
+        }
     }
     
     /**

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java?view=diff&rev=546861&r1=546860&r2=546861
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
Wed Jun 13 06:19:15 2007
@@ -69,6 +69,7 @@
     public final static int    DEFAULT_PORT = 1527;
     private final static String DEFAULT_FRAMEWORK = "embedded";
     public final static String DEFAULT_HOSTNAME = "localhost";
+    public final static String DEFAULT_SSL = "off";
 
     public  final   static  String  TEST_DBO = "TEST_DBO";
             
@@ -82,6 +83,7 @@
     private final static String KEY_HOSTNAME = "hostName";
     private final static String KEY_PORT = "port";
     private final static String KEY_VERBOSE = "derby.tests.debug";    
+    private final static String KEY_SSL = "ssl";
     
     /**
      * Simple count to provide a unique number for database
@@ -615,6 +617,20 @@
     }
     
     /**
+     * Decorate a test changing the default ssl mode.
+     * The tearDown method resets the default user and password value to
+     * their previous settings.
+     * 
+     * @param test Test to decorate
+     * @param ssl New ssl mode
+     * @return decorated test
+     */
+    public static Test changeSSLDecorator(Test test, String ssl)
+    {
+        return new ChangeSSLSetup(test, ssl);
+    }   
+    
+    /**
      * Default embedded configuration
      *
      */
@@ -631,6 +647,7 @@
             booleanValue();
         
         this.jdbcClient = JDBCClient.getDefaultEmbedded();
+        this.ssl = null;
         url = createJDBCUrlWithDatabaseName(defaultDbName);
         initConnector(null);
  
@@ -653,6 +670,8 @@
         this.jdbcClient = copy.jdbcClient;
         this.hostName = copy.hostName;
         
+        this.ssl = copy.ssl;
+
         this.url = copy.url;
         initConnector(copy.connector);
     }
@@ -671,6 +690,8 @@
         
         this.jdbcClient = client;
         this.hostName = hostName;
+
+        this.ssl = copy.ssl;
         
         this.url = createJDBCUrlWithDatabaseName(defaultDbName);
         initConnector(copy.connector);
@@ -699,6 +720,8 @@
         
         this.jdbcClient = copy.jdbcClient;
         this.hostName = copy.hostName;
+
+        this.ssl = copy.ssl;
         
         this.url = copy.url;
         initConnector(copy.connector);
@@ -706,6 +729,18 @@
 
     /**
      * Obtain a new configuration identical to the passed in
+     * one except for the default user and password.
+     * @param copy Configuration to copy.
+     * @param ssl New default ssl mode
+     */
+    TestConfiguration(TestConfiguration copy, String ssl)
+    {
+        this(copy);
+        this.ssl = ssl;
+    }
+
+    /**
+     * Obtain a new configuration identical to the passed in
      * one except for the database name. The passed database name
      * is added at the end of the list of used databases.
      * If the <code>defaulDb</code> parameter is <code>true</code>
@@ -742,6 +777,8 @@
         
         this.jdbcClient = copy.jdbcClient;
         this.hostName = copy.hostName;
+
+        this.ssl = copy.ssl;
         
         this.url = createJDBCUrlWithDatabaseName(this.defaultDbName);
         initConnector(copy.connector);
@@ -775,6 +812,8 @@
         } else {
             port = DEFAULT_PORT;
         }
+
+        ssl = props.getProperty(KEY_SSL);
         
         String framework = props.getProperty(KEY_FRAMEWORK, DEFAULT_FRAMEWORK);
         
@@ -944,6 +983,16 @@
     public int getPort() {
         return port;
     }
+
+    /**
+     * Get ssl mode for network server
+     * 
+     * @return ssl mode
+     */
+    public String getSsl() {
+        return ssl;
+    }
+
     
     /**
      * Open connection to the default database.
@@ -1131,6 +1180,7 @@
     private final String hostName;
     private final JDBCClient jdbcClient;
     private boolean isVerbose;
+    private String ssl;
     
     /**
      * Password token used by the builtin authentication decorators.



Mime
View raw message