You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by rh...@apache.org on 2007/02/20 19:13:39 UTC
svn commit: r509693 - in
/db/derby/code/trunk/java/testing/org/apache/derbyTesting:
functionTests/tests/derbynet/ junit/
Author: rhillegas
Date: Tue Feb 20 10:13:38 2007
New Revision: 509693
URL: http://svn.apache.org/viewvc?view=rev&rev=509693
Log:
DERBY-2196: Commit secure-server tests in derby-2196-03-tests-01.diff.
Added:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SecureServerTest.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/junit/NetworkServerTestSetup.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ServerSetup.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/SecureServerTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SecureServerTest.java?view=auto&rev=509693
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SecureServerTest.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SecureServerTest.java Tue Feb 20 10:13:38 2007
@@ -0,0 +1,365 @@
+/*
+
+ Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.SecureServerTest
+
+ 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 junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.derbyTesting.junit.BaseTestCase;
+import org.apache.derbyTesting.junit.NetworkServerTestSetup;
+import org.apache.derbyTesting.junit.SecurityManagerSetup;
+import org.apache.derbyTesting.junit.ServerSetup;
+import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+import org.apache.derby.iapi.reference.Property;
+import org.apache.derby.iapi.tools.i18n.LocalizedResource;
+import org.apache.derby.drda.NetworkServerControl;
+import org.apache.derby.impl.drda.NetworkServerControlImpl;
+
+/**
+ * This Junit test class tests whether the server comes up under a security
+ * manager as expected.
+ */
+
+public class SecureServerTest extends BaseTestCase
+{
+ ///////////////////////////////////////////////////////////////////////////////////
+ //
+ // CONSTANTS
+ //
+ ///////////////////////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////////////////////
+ //
+ // INNER CLASSES
+ //
+ ///////////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * <p>
+ * Possible outcomes for the experiment of bringing up the server.
+ * </p>
+ */
+ public static final class Outcome
+ {
+ private boolean _serverShouldComeUp;
+ private String _expectedServerOutput;
+
+ public Outcome
+ (
+ boolean serverShouldComeUp,
+ String expectedServerOutput
+ )
+ {
+ _serverShouldComeUp = serverShouldComeUp;
+ _expectedServerOutput = expectedServerOutput;
+ }
+
+ public boolean serverShouldComeUp() { return _serverShouldComeUp; }
+ public String expectedServerOutput() { return _expectedServerOutput; }
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////
+ //
+ // STATE
+ //
+ ///////////////////////////////////////////////////////////////////////////////////
+
+ private static final Outcome FAILED_NO_AUTHENTICATION = new Outcome( false, authenticationFailure() );
+ private static final Outcome RUNNING_SECURITY_NOT_BOOTED = new Outcome( true, "" );
+ private static final Outcome RUNNING_SECURITY_BOOTED = new Outcome( true, getTextMessage( "DRDA_SecurityInstalled.I" ) );
+
+
+ // startup state
+ private boolean _unsecureSet;
+ private boolean _authenticationRequired;
+
+ // expected outcomes
+ private Outcome _outcome;
+
+ // helper state for intercepting server error messages
+ private InputStream[] _inputStreamHolder;
+
+ private static LocalizedResource _messageResolver;
+
+ ///////////////////////////////////////////////////////////////////////////////////
+ //
+ // CONSTRUCTORS
+ //
+ ///////////////////////////////////////////////////////////////////////////////////
+
+ public SecureServerTest
+ (
+ boolean unsecureSet,
+ boolean authenticationRequired,
+
+ Outcome outcome
+ )
+ {
+ super( "testServerStartup" );
+
+ _unsecureSet = unsecureSet;
+ _authenticationRequired = authenticationRequired;
+
+ _outcome = outcome;
+
+ _inputStreamHolder = new InputStream[ 1 ];
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////
+ //
+ // JUnit MACHINERY
+ //
+ ///////////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Tests to run.
+ */
+ public static Test suite()
+ {
+ //NetworkServerTestSetup.setWaitTime( 10000L );
+
+ TestSuite suite = new TestSuite();
+
+ // Server booting requires that we run from the jar files
+ if ( !TestConfiguration.loadingFromJars() ) { return suite; }
+
+ // O = Overriden
+ // A = Authenticated
+ //
+ // .addTest( decorateTest( S, O, A, Outcome ) );
+ //
+
+ suite.addTest( decorateTest( false, false, FAILED_NO_AUTHENTICATION ) );
+ suite.addTest( decorateTest( false, true, RUNNING_SECURITY_BOOTED ) );
+ suite.addTest( decorateTest( true, false, RUNNING_SECURITY_NOT_BOOTED ) );
+ suite.addTest( decorateTest( true, true, RUNNING_SECURITY_NOT_BOOTED ) );
+
+ 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
+ (
+ boolean unsecureSet,
+ boolean authenticationRequired,
+
+ Outcome outcome
+ )
+ {
+ SecureServerTest secureServerTest = new SecureServerTest
+ (
+ unsecureSet,
+ authenticationRequired,
+
+ outcome
+ );
+
+ String[] startupProperties = getStartupProperties( authenticationRequired );
+ String[] startupArgs = getStartupArgs( unsecureSet );
+
+ TestSetup testSetup = new NetworkServerTestSetup
+ (
+ secureServerTest,
+ startupProperties,
+ startupArgs,
+ true,
+ secureServerTest._outcome.serverShouldComeUp(),
+ secureServerTest._inputStreamHolder
+ );
+
+ testSetup = new ServerSetup( testSetup, TestConfiguration.DEFAULT_HOSTNAME, TestConfiguration.DEFAULT_PORT );
+
+ Test test = SecurityManagerSetup.noSecurityManager( testSetup );
+
+ return test;
+ }
+
+ /**
+ * <p>
+ * Return n array of startup args suitable for booting a server.
+ * </p>
+ */
+ private static String[] getStartupArgs( boolean setUnsecureOption )
+ {
+ ArrayList list = new ArrayList();
+
+ if ( setUnsecureOption )
+ {
+ list.add
+ ( '-' + NetworkServerControlImpl.DASHARGS[ NetworkServerControlImpl.DASHARG_UNSECURE ] );
+ }
+
+ String[] result = new String[ list.size() ];
+
+ list.toArray( result );
+
+ return result;
+ }
+
+ /**
+ * <p>
+ * Return a set of startup properties suitable for SystemPropertyTestSetup.
+ * </p>
+ */
+ private static String[] getStartupProperties( boolean authenticationRequired )
+ {
+ ArrayList list = new ArrayList();
+
+ if ( authenticationRequired )
+ {
+ list.add( "derby.connection.requireAuthentication=true" );
+ }
+
+ String[] result = new String[ list.size() ];
+
+ list.toArray( result );
+
+ return result;
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////
+ //
+ // JUnit TESTS
+ //
+ ///////////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Verify if the server came up and if so, was a security manager installed.
+ */
+ public void testServerStartup()
+ throws Exception
+ {
+ String myName = toString();
+ String serverOutput = getServerOutput();
+ boolean serverCameUp = serverCameUp();
+ boolean outputOK = ( serverOutput.indexOf( _outcome.expectedServerOutput() ) >= 0 );
+
+ assertEquals( myName + ": serverCameUp = " + serverCameUp, _outcome.serverShouldComeUp(), serverCameUp );
+
+ assertTrue( myName + "\nExpected: " + _outcome.expectedServerOutput() + "\nBut saw: " + serverOutput , outputOK );
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////
+ //
+ // Object OVERLOADS
+ //
+ ///////////////////////////////////////////////////////////////////////////////////
+
+ public String toString()
+ {
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append( "SecureServerTest( " );
+ buffer.append( "Opened = " ); buffer.append( _unsecureSet); buffer.append( ", " );
+ buffer.append( "Authenticated= " ); buffer.append( _authenticationRequired );
+ buffer.append( " )" );
+
+ return buffer.toString();
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////
+ //
+ // MINIONS
+ //
+ ///////////////////////////////////////////////////////////////////////////////////
+
+ private String getServerOutput()
+ throws Exception
+ {
+ byte[] inputBuffer = new byte[ 1000 ];
+
+ InputStream is = _inputStreamHolder[ 0 ];
+
+ int bytesRead = is.read( inputBuffer );
+
+ return new String( inputBuffer, 0, bytesRead );
+ }
+
+ private static LocalizedResource getMessageResolver()
+ {
+ if ( _messageResolver == null )
+ {
+ _messageResolver = new LocalizedResource( null, null, "org.apache.derby.loc.drda.messages" );
+ }
+
+ return _messageResolver;
+ }
+
+ private static String getTextMessage( String key )
+ {
+ return getTextMessage( key, new String[]{} );
+ }
+ private static String getTextMessage( String key, Object[] args )
+ {
+ return getMessageResolver().getTextMessage( key, args );
+ }
+
+ private static String authenticationFailure()
+ {
+ return getTextMessage
+ (
+ "DRDA_NoAuthentication.S",
+ new String[]
+ {
+ Property.REQUIRE_AUTHENTICATION_PARAMETER,
+ NetworkServerControlImpl.DASHARGS[ NetworkServerControlImpl.DASHARG_UNSECURE ]
+ }
+ );
+ }
+
+ private boolean serverCameUp()
+ throws Exception
+ {
+ return NetworkServerTestSetup.pingForServerStart( NetworkServerTestSetup.getNetworkServerControl() );
+ }
+
+}
+
Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/SecureServerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
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=509693&r1=509692&r2=509693
==============================================================================
--- 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 Tue Feb 20 10:13:38 2007
@@ -52,6 +52,7 @@
suite.addTest(ClientSideSystemPropertiesTest.suite());
suite.addTest(BadConnectionTest.suite());
suite.addTest(NetHarnessJavaTest.suite());
+ suite.addTest(SecureServerTest.suite());
// These tests references a client class directly
// thus causing class not found exceptions if the
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=509693&r1=509692&r2=509693
==============================================================================
--- 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 Tue Feb 20 10:13:38 2007
@@ -24,6 +24,7 @@
import java.net.UnknownHostException;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.InputStream;
import java.io.PrintWriter;
import java.security.AccessController;
import java.security.PrivilegedAction;
@@ -42,8 +43,22 @@
*/
final public class NetworkServerTestSetup extends TestSetup {
+ /** Wait maximum 1 minute for server to start */
+ private static final long WAIT_TIME = 60000;
+
+ /** Sleep for 50 ms before pinging the network server (again) */
+ private static final int SLEEP_TIME = 50;
+
+ private static long waitTime = WAIT_TIME;
+
private FileOutputStream serverOutput;
private final boolean asCommand;
+
+ private final boolean useSeparateProcess;
+ private final boolean serverShouldComeUp;
+ private final InputStream[] inputStreamHolder;
+ private final String[] systemProperties;
+ private final String[] startupArgs;
/**
* Decorator this test with the NetworkServerTestSetup
@@ -51,6 +66,36 @@
public NetworkServerTestSetup(Test test, boolean asCommand) {
super(test);
this.asCommand = asCommand;
+
+ this.systemProperties = null;
+ this.startupArgs = null;
+ this.useSeparateProcess = false;
+ this.serverShouldComeUp = true;
+ this.inputStreamHolder = null;
+}
+
+ /**
+ * Decorator for starting up with specific command args.
+ */
+ public NetworkServerTestSetup
+ (
+ Test test,
+ String[] systemProperties,
+ String[] startupArgs,
+ boolean useSeparateProcess,
+ boolean serverShouldComeUp,
+ InputStream[] inputStreamHolder
+ )
+ {
+ super(test);
+
+ this.asCommand = true;
+
+ this.systemProperties = systemProperties;
+ this.startupArgs = startupArgs;
+ this.useSeparateProcess = true;
+ this.serverShouldComeUp = serverShouldComeUp;
+ this.inputStreamHolder = inputStreamHolder;
}
/**
@@ -60,13 +105,15 @@
BaseTestCase.println("Starting network server:");
networkServerController = getNetworkServerControl();
-
- if (asCommand)
- startWithCommand();
+
+ if (useSeparateProcess)
+ { startSeparateProcess(); }
+ else if (asCommand)
+ { startWithCommand(); }
else
- startWithAPI();
+ { startWithAPI(); }
- waitForServerStart(networkServerController);
+ if ( serverShouldComeUp ) { waitForServerStart(networkServerController); }
}
private void startWithAPI() throws Exception
@@ -89,7 +136,6 @@
});
networkServerController.start(new PrintWriter(serverOutput));
-
}
private void startWithCommand() throws Exception
@@ -101,24 +147,78 @@
new Thread(
new Runnable() {
public void run() {
- org.apache.derby.drda.NetworkServerControl.main(
- new String[] {
- "start",
- "-h",
- config.getHostName(),
- "-p",
- Integer.toString(config.getPort())
- });
+
+ String[] args = getDefaultStartupArgs();
+
+ org.apache.derby.drda.NetworkServerControl.main( args );
}
}, "NetworkServerTestSetup command").start();
}
+ private void startSeparateProcess() throws Exception
+ {
+ StringBuffer buffer = new StringBuffer();
+ String classpath = BaseTestCase.getSystemProperty( "java.class.path" );
+
+ buffer.append( "java -classpath " );
+ buffer.append( classpath );
+ buffer.append( " " );
+
+ int count = systemProperties.length;
+ for ( int i = 0; i < count; i++ )
+ {
+ buffer.append( " -D" );
+ buffer.append( systemProperties[ i ] );
+ }
+
+ buffer.append( " org.apache.derby.drda.NetworkServerControl " );
+
+ String[] defaultArgs = getDefaultStartupArgs();
+
+ count = defaultArgs.length;
+ for ( int i = 0; i < count; i++ )
+ {
+ buffer.append( " " );
+ buffer.append( defaultArgs[ i ] );
+ }
+
+ count = startupArgs.length;
+ for ( int i = 0; i < count; i++ )
+ {
+ buffer.append( " " );
+ buffer.append( startupArgs[ i ] );
+ }
+
+ final String command = buffer.toString();
+
+ Process serverProcess = (Process) AccessController.doPrivileged
+ (
+ new PrivilegedAction()
+ {
+ public Object run()
+ {
+ Process result = null;
+ try {
+ result = Runtime.getRuntime().exec( command );
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+
+ return result;
+ }
+ }
+ );
+
+ inputStreamHolder[ 0 ] = serverProcess.getInputStream();
+ }
+
/**
* Stop the network server if it still
* appears to be running.
*/
protected void tearDown() throws Exception {
+
if (networkServerController != null) {
boolean running = false;
try {
@@ -130,22 +230,31 @@
if (running)
networkServerController.shutdown();
- serverOutput.close();
+ if ( serverOutput != null ) { serverOutput.close(); }
networkServerController = null;
serverOutput = null;
}
}
+ /**
+ * Get the default command arguments for booting the network server.
+ */
+ public static String[] getDefaultStartupArgs()
+ {
+ TestConfiguration config = TestConfiguration.getCurrent();
+
+ return new String[] {
+ "start",
+ "-h",
+ config.getHostName(),
+ "-p",
+ Integer.toString(config.getPort())
+ };
+ }
+
/* Network Server Control */
private NetworkServerControl networkServerController;
-
- /** Wait maximum 1 minute for server to start */
- private static final int WAIT_TIME = 60000;
-
- /** Sleep for 50 ms before pinging the network server (again) */
- private static final int SLEEP_TIME = 50;
-
-
+
/*
* Utility methods related to controlling network server.
*/
@@ -172,6 +281,16 @@
fail("Timed out waiting for network server to start");
}
+ /**
+ * Set the number of milliseconds to wait before declaring server startup
+ * a failure.
+ *
+ */
+ public static void setWaitTime( long newWaitTime )
+ {
+ waitTime = newWaitTime;
+ }
+
/**
* Ping server for upto sixty seconds. If the server responds
* in that time then return true, otherwise return false.
@@ -187,7 +306,7 @@
networkServerController.ping();
return true;
} catch (Exception e) {
- if (System.currentTimeMillis() - startTime > WAIT_TIME) {
+ if (System.currentTimeMillis() - startTime > waitTime) {
return false;
}
}
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java?view=diff&rev=509693&r1=509692&r2=509693
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java Tue Feb 20 10:13:38 2007
@@ -119,6 +119,14 @@
BaseTestCase.setSystemProperty("java.security.policy", "");
}
+ /**
+ * Return the name of the default policy.
+ */
+ public static String getDefaultPolicy()
+ {
+ return "org/apache/derbyTesting/functionTests/util/derby_tests.policy";
+ }
+
/**
* Install a SecurityManager with the default test policy
* file:
@@ -127,9 +135,7 @@
*/
static void installSecurityManager() throws PrivilegedActionException
{
- installSecurityManager(
- "org/apache/derbyTesting/functionTests/util/derby_tests.policy");
-
+ installSecurityManager( getDefaultPolicy() );
}
private static void installSecurityManager(String policyFile)
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ServerSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ServerSetup.java?view=diff&rev=509693&r1=509692&r2=509693
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ServerSetup.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ServerSetup.java Tue Feb 20 10:13:38 2007
@@ -28,7 +28,7 @@
* configuration, it does not start any network server.
*
*/
-final class ServerSetup extends ChangeConfigurationSetup {
+public final class ServerSetup extends ChangeConfigurationSetup {
private final String host;
private final int port;
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=509693&r1=509692&r2=509693
==============================================================================
--- 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 Tue Feb 20 10:13:38 2007
@@ -66,9 +66,9 @@
private final static String DEFAULT_USER_NAME = "APP";
private final static String DEFAULT_USER_PASSWORD = "APP";
- private final static int DEFAULT_PORT = 1527;
+ public final static int DEFAULT_PORT = 1527;
private final static String DEFAULT_FRAMEWORK = "embedded";
- private final static String DEFAULT_HOSTNAME = "localhost";
+ public final static String DEFAULT_HOSTNAME = "localhost";
/**
* Keys to use to look up values in properties files.