Author: markt
Date: Tue Mar 12 22:35:06 2013
New Revision: 1455737

URL: http://svn.apache.org/r1455737
Log:
Make SSL optional
Try to write data in one large chunk.
Unexpected values for written are now observable.

Modified:
    tomcat/trunk/test/org/apache/tomcat/jni/TestSocket.java

Modified: tomcat/trunk/test/org/apache/tomcat/jni/TestSocket.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/jni/TestSocket.java?rev=1455737&r1=1455736&r2=1455737&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/jni/TestSocket.java (original)
+++ tomcat/trunk/test/org/apache/tomcat/jni/TestSocket.java Tue Mar 12 22:35:06 
2013
@@ -23,7 +23,7 @@ import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.security.SecureRandom;
 
-import javax.net.ssl.SSLSocketFactory;
+import javax.net.SocketFactory;
 import javax.net.ssl.TrustManager;
 
 import org.junit.Assert;
@@ -35,6 +35,29 @@ public class TestSocket {
 
     private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
 
+    // 1,000        Writes all the data in the first write
+    // 100,000      Shows strange return values for written
+    // 10,000,000   Shows strange return values for written more clearly
+    private static final int LIMIT = 100000;
+    private static final byte[] DATA;
+    private static final boolean ENABLE_SSL = true;
+
+    static {
+        int pos = 0;
+        int size = 0;
+        for (int i = 0; i < LIMIT; i++) {
+            String s = Integer.toString(i);
+            size += s.length();
+        }
+        DATA = new byte[size];
+        for (int i = 0; i < LIMIT; i++) {
+            String s = Integer.toString(i);
+            byte[] b = s.getBytes(ISO_8859_1);
+            System.arraycopy(b, 0, DATA, pos, b.length);
+            pos += b.length;
+        }
+    }
+
     @Test
     public void testNonBlockingEagain() throws Exception {
 
@@ -57,11 +80,16 @@ public class TestSocket {
 
         // Open a connection to the server
         String host = null;
-        javax.net.ssl.SSLContext sc =
-                javax.net.ssl.SSLContext.getInstance("SSL");
-        sc.init(null, new TrustManager[] {new TesterSupport.TrustAllCerts()} ,
-                new SecureRandom());
-        SSLSocketFactory factory = sc.getSocketFactory();
+        SocketFactory factory;
+        if (ENABLE_SSL) {
+            javax.net.ssl.SSLContext sc =
+                    javax.net.ssl.SSLContext.getInstance("SSL");
+            sc.init(null, new TrustManager[] {new 
TesterSupport.TrustAllCerts()} ,
+                    new SecureRandom());
+             factory = sc.getSocketFactory();
+        } else {
+            factory = SocketFactory.getDefault();
+        }
         java.net.Socket socket = factory.createSocket(host, port);
 
         // Infinite timeout
@@ -74,24 +102,26 @@ public class TestSocket {
 
         // Read the first 20 digits.
         data = clientRead(is, bb, data, 20);
+        System.out.println("Client read first 20 digits");
 
         // Block until the server fills up it's send buffers and then sleep for
         // 5 seconds
         int count = 0;
-        while (count < 5) {
+        while (count < 3) {
             java.lang.Thread.sleep(1000);
             if (s.isPolling()) {
                 count ++;
             }
         }
 
-        // Read the next 10000 digits
-        data = clientRead(is, bb, data, 10000);
+        // Read 30% of the data
+        data = clientRead(is, bb, data, (int) (LIMIT * 0.3));
+        System.out.println("Client read first 30% of digits");
 
         // Block until the server fills up it's send buffers and then sleep for
         // 5 seconds
         count = 0;
-        while (count < 5) {
+        while (count < 3) {
             java.lang.Thread.sleep(1000);
             if (s.isPolling()) {
                 count ++;
@@ -99,7 +129,8 @@ public class TestSocket {
         }
 
         // Read to the end
-        clientRead(is, bb, data, 100000);
+        clientRead(is, bb, data, LIMIT);
+        System.out.println("Client read all digits");
 
         socket.close();
     }
@@ -128,6 +159,9 @@ public class TestSocket {
                 expected = Integer.toString(data);
                 len = expected.length();
                 bb.compact();
+                if (data%1000 == 0) {
+                    System.out.println("Client read to  [" + data + "]");
+                }
             }
         }
         return data;
@@ -168,18 +202,22 @@ public class TestSocket {
             }
 
             // Setup SSL
-            SSL.randSet("builtin");
-            SSL.initialize(null);
-            sslContext = SSLContext.make(
-                    rootPool, SSL.SSL_PROTOCOL_ALL, SSL.SSL_MODE_SERVER);
-            SSLContext.setCipherSuite(sslContext, "ALL");
-            File certFile = new File(
-                    "test/org/apache/tomcat/util/net/localhost-cert.pem");
-            File keyFile = new File(
-                    "test/org/apache/tomcat/util/net/localhost-key.pem");
-            SSLContext.setCertificate(sslContext, certFile.getAbsolutePath(),
-                    keyFile.getAbsolutePath(), null, SSL.SSL_AIDX_RSA);
-            SSLContext.setVerify(sslContext, SSL.SSL_CVERIFY_NONE, 10);
+            if (ENABLE_SSL) {
+                SSL.randSet("builtin");
+                SSL.initialize(null);
+                sslContext = SSLContext.make(
+                        rootPool, SSL.SSL_PROTOCOL_ALL, SSL.SSL_MODE_SERVER);
+                SSLContext.setCipherSuite(sslContext, "ALL");
+                File certFile = new File(
+                        "test/org/apache/tomcat/util/net/localhost-cert.pem");
+                File keyFile = new File(
+                        "test/org/apache/tomcat/util/net/localhost-key.pem");
+                SSLContext.setCertificate(sslContext, 
certFile.getAbsolutePath(),
+                        keyFile.getAbsolutePath(), null, SSL.SSL_AIDX_RSA);
+                SSLContext.setVerify(sslContext, SSL.SSL_CVERIFY_NONE, 10);
+            } else {
+                sslContext = 0;
+            }
 
             // Poller
             serverSocketPool = Pool.create(rootPool);
@@ -206,73 +244,82 @@ public class TestSocket {
                 long socket = Socket.accept(serverSocket);
 
                 // Configure SSL
-                SSLSocket.attach(sslContext, socket);
-                if (SSLSocket.handshake(socket) != 0) {
-                    System.err.println("SSL handshake failed");
+                if (sslContext != 0) {
+                    SSLSocket.attach(sslContext, socket);
+                    if (SSLSocket.handshake(socket) != 0) {
+                        System.err.println("SSL handshake failed");
+                    }
                 }
 
                 // Make socket non-blocking
                 Socket.timeoutSet(socket, 0);
 
+                int start = 0;
+                int left = DATA.length;
+                int written;
 
-                int data = 0;
+                System.out.println("To write [" + left + "]");
                 do {
-                    String s = Integer.toString(data);
-                    byte[] b = s.getBytes(ISO_8859_1);
+                    written = Socket.send(socket, DATA, start, left);
+                    if (written < 0) {
+                        if (Status.APR_STATUS_IS_EAGAIN(-written)) {
+                            System.out.println("EAGAIN");
+                            written = 0;
+                        } else {
+                            System.out.println("Error code [" + -written + 
"]");
+                            throw new RuntimeException();
+                        }
+                    }
 
-                    int start = 0;
-                    int len = b.length;
-                    int written;
-
-                    do {
-                        written = Socket.send(socket, b, start, len);
-                        if (written < 0) {
-                            if (Status.APR_STATUS_IS_EAGAIN(-written)) {
-                                // System.out.println("EAGAIN");
-                                written = 0;
+                    start += written;
+                    left -= written;
+                    System.out.println(
+                            "Written: [" +written + "], Left [" + left + "]");
+
+                    if (written == 0 && left > 0) {
+                        // Write buffer is full. Poll until there is space
+                        Poll.add(poller, socket, Poll.APR_POLLOUT);
+                        polling = true;
+                        int pollCount = 0;
+
+                        int rv = 0;
+                        long[] desc = new long[2];
+
+                        while (rv == 0) {
+                            System.out.println("Poll. Left [" + left + "]");
+                            rv = Poll.poll(poller, 1000000, desc, true);
+                            pollCount++;
+
+                            if (rv > 0) {
+                                // There is space. Continue to write.
+                            } else if (-rv == Status.TIMEUP) {
+                                rv = 0;
+                                // Poll timed out. Poll again.
+                            } else if (rv < 0) {
+                                // Something went wrong
+                                System.err.println(
+                                        "Poller failure [" + -rv + "]");
                             } else {
-                                System.out.println("Error code [" + -written + 
"]");
-                                throw new RuntimeException();
+                                // rv == 0. Poll again.
                             }
-                        }
 
-                        start += written;
-                        len -= written;
-
-                        if (written == 0 && len > 0) {
-                            // Write buffer is full. Poll until there is space
-                            Poll.add(poller, socket, Poll.APR_POLLOUT);
-                            polling = true;
-
-                            int rv = 0;
-                            long[] desc = new long[2];
-
-                            while (rv == 0) {
-                                System.out.println("Poll");
-                                rv = Poll.poll(poller, 1000000, desc, true);
-                                if (rv > 0) {
-                                    // There is space. Continue to write.
-                                } else if (-rv == Status.TIMEUP) {
-                                    // Poll timed out. Poll again.
-                                } else if (rv < 0) {
-                                    // Something went wrong
-                                    System.err.println(
-                                            "Poller failure [" + -rv + "]");
-                                } else {
-                                    // rv == 0. Poll again.
-                                }
+                            if (pollCount > 10) {
+                                // Should never get stuck in a polling loop
+                                // for this long.
+                                System.err.println(
+                                        "Polling loop [" + pollCount + "]");
                             }
-                            polling = false;
                         }
-                    } while (len > 0);
-
-                    data++;
-                } while (data < 100000);
-
+                        polling = false;
+                    }
+                } while (left > 0);
             } catch (Exception e) {
                 // May need to do something here
                 e.printStackTrace();
             }
+            // If this finishes early, let the client think it is polling so 
the
+            // client can complete
+            polling = true;
         }
     }
 }



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

Reply via email to