You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2011/09/24 19:20:21 UTC

svn commit: r1175201 - in /tomcat/trunk/test/org/apache/catalina/startup: FastNonSecureRandom.java TomcatBaseTest.java

Author: markt
Date: Sat Sep 24 17:20:21 2011
New Revision: 1175201

URL: http://svn.apache.org/viewvc?rev=1175201&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51887
Use an insecure random source for session ID generation during tests for speed.
Based on kkolinko's suggestion.

Added:
    tomcat/trunk/test/org/apache/catalina/startup/FastNonSecureRandom.java   (with props)
Modified:
    tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java

Added: tomcat/trunk/test/org/apache/catalina/startup/FastNonSecureRandom.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/FastNonSecureRandom.java?rev=1175201&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/FastNonSecureRandom.java (added)
+++ tomcat/trunk/test/org/apache/catalina/startup/FastNonSecureRandom.java Sat Sep 24 17:20:21 2011
@@ -0,0 +1,60 @@
+/*
+ * 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.catalina.startup;
+
+import java.security.SecureRandom;
+import java.util.Random;
+
+public class FastNonSecureRandom extends SecureRandom {
+
+    private static final long serialVersionUID = 1L;
+
+    private final Random random = new Random();
+
+    @Override
+    public String getAlgorithm() {
+        return "INSECURE";
+    }
+
+    @Override
+    public synchronized void setSeed(byte[] seed) {
+        // Not implemented
+    }
+
+    @Override
+    public synchronized void setSeed(long seed) {
+        // The super class constructor calls this method earlier than our
+        // fields are initialized. Ignore the call.
+        if (random == null) {
+            return;
+        }
+        random.setSeed(seed);
+    }
+
+    @Override
+    public synchronized void nextBytes(byte[] bytes) {
+        random.nextBytes(bytes);
+    }
+
+    @Override
+    public byte[] generateSeed(int numBytes) {
+        byte[] value = new byte[numBytes];
+        nextBytes(value);
+        return value;
+    }
+
+}
\ No newline at end of file

Propchange: tomcat/trunk/test/org/apache/catalina/startup/FastNonSecureRandom.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?rev=1175201&r1=1175200&r2=1175201&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java (original)
+++ tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java Sat Sep 24 17:20:21 2011
@@ -37,10 +37,15 @@ import static org.junit.Assert.fail;
 import org.junit.After;
 import org.junit.Before;
 
+import org.apache.catalina.Container;
+import org.apache.catalina.LifecycleException;
 import org.apache.catalina.LifecycleState;
+import org.apache.catalina.Server;
+import org.apache.catalina.Service;
 import org.apache.catalina.connector.Connector;
 import org.apache.catalina.core.AprLifecycleListener;
 import org.apache.catalina.core.StandardServer;
+import org.apache.catalina.session.StandardManager;
 import org.apache.catalina.valves.AccessLogValve;
 import org.apache.tomcat.util.buf.ByteChunk;
 
@@ -126,7 +131,7 @@ public abstract class TomcatBaseTest {
             fail("Unable to create appBase for test");
         }
         
-        tomcat = new Tomcat();
+        tomcat = new TomcatWithFastSessionIDs();
 
         String protocol = getProtocol();
         Connector connector = new Connector(protocol);
@@ -362,4 +367,27 @@ public abstract class TomcatBaseTest {
         return rc;
     }
 
+    private static class TomcatWithFastSessionIDs extends Tomcat {
+
+        @Override
+        public void start() throws LifecycleException {
+            // Use fats, insecure session ID generation for all tests
+            Server server = getServer();
+            for (Service service : server.findServices()) {
+                Container e = service.getContainer();
+                for (Container h : e.findChildren()) {
+                    for (Container c : h.findChildren()) {
+                        StandardManager m = (StandardManager) c.getManager();
+                        if (m == null) {
+                            m = new StandardManager();
+                            m.setSecureRandomClass(
+                                    "org.apache.catalina.startup.FastNonSecureRandom");
+                            c.setManager(m);
+                        }
+                    }
+                }
+            }
+            super.start();
+        }
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org