commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1167100 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/ssl/Random.java native/modules/openssl/rand.c test/org/apache/commons/runtime/TestOpenSSL.java
Date Fri, 09 Sep 2011 11:19:33 GMT
Author: mturk
Date: Fri Sep  9 11:19:32 2011
New Revision: 1167100

URL: http://svn.apache.org/viewvc?rev=1167100&view=rev
Log:
Allow both standard and direct byte buffers to be used

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java
    commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java?rev=1167100&r1=1167099&r2=1167100&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java
Fri Sep  9 11:19:32 2011
@@ -49,8 +49,8 @@ public final class Random
     private static native String        getdef0();
     private static native void          setdef0(String path);
 
-    private static native int           bytes0(byte[] b, int off, int len);
-    private static native int           bytes1(ByteBuffer b, int off, int len);
+    private static native boolean       bytes0(byte[] b, int off, int len);
+    private static native boolean       bytes1(ByteBuffer b, int off, int len);
     private static native boolean       seteng0(long ep);
     
     public static boolean seed(String path)
@@ -96,7 +96,7 @@ public final class Random
             throw new IndexOutOfBoundsException();
         synchronized(buf) {
             if (siz > 0) {
-                int n = siz > len ? len : pos;
+                int n = siz > len ? len : siz;
                 System.arraycopy(buf, pos, bytes, off, n);
                 off += n;
                 len -= n;
@@ -122,14 +122,32 @@ public final class Random
         synchronized(buf) {
             if (siz > 0) {
                 int s = buffer.remaining();
-                int n = siz > s ? s : pos;
+                int n = siz > s ? s : siz;
                 buffer.put(buf, pos, n);
                 pos += n;
                 siz -= n;
             }
         }
-        if (buffer.remaining() > 0)
-            bytes1(buffer, buffer.position(), buffer.remaining());
+        if (buffer.isDirect()) {
+            int nr = buffer.remaining();
+            if (nr > 0) {
+                int bp = buffer.position();
+                if (bytes1(buffer, bp, nr))
+                    buffer.position(bp + nr);
+            }
+        }
+        else {
+            int s;
+            while ((s = buffer.remaining()) > 0) {
+                bytes0(buf, 0, buf.length);
+                siz = buf.length;
+                pos = 0;
+                int n = siz > s ? s : siz;
+                buffer.put(buf, pos, n);
+                pos += n;
+                siz -= n;        
+            }
+        }
     }
 
     public byte nextByte()

Modified: commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c?rev=1167100&r1=1167099&r2=1167100&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c Fri Sep  9 11:19:32
2011
@@ -157,11 +157,12 @@ ACR_SSL_EXPORT(void, Random, setdef0)(JN
 ACR_SSL_EXPORT(jint, Random, bytes0)(JNI_STDARGS, jbyteArray ba,
                                      jint off, jint len)
 {
-    jint rv = -1;
+    jboolean rv = JNI_FALSE;
     unsigned char *sb = (*env)->GetPrimitiveArrayCritical(env, ba, 0);
 
     if (sb != 0) {
-        rv = RAND_bytes(sb + off, len);
+        if (RAND_bytes(sb + off, len) > 0)
+            rv = JNI_TRUE;
         (*env)->ReleasePrimitiveArrayCritical(env, ba, sb, 0);
     }
     return rv;
@@ -170,13 +171,12 @@ ACR_SSL_EXPORT(jint, Random, bytes0)(JNI
 ACR_SSL_EXPORT(jint, Random, bytes1)(JNI_STDARGS, jobject bb,
                                      jint off, jint len)
 {
-    jint rv = -1;
     unsigned char *sb = (*env)->GetDirectBufferAddress(env, bb);
 
-    if (sb != 0) {
-        rv = RAND_bytes(sb + off, len);
-    }
-    return rv;
+    if (sb != 0 && RAND_bytes(sb + off, len) > 0)
+        return JNI_TRUE;
+    else
+        return JNI_FALSE;
 }
 
 ACR_SSL_EXPORT(jboolean, Random, seteng0)(JNI_STDARGS, jlong ep)

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java?rev=1167100&r1=1167099&r2=1167100&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java
Fri Sep  9 11:19:32 2011
@@ -20,6 +20,7 @@ import org.testng.annotations.*;
 import org.testng.Assert;
 import java.io.IOException;
 import java.io.File;
+import java.nio.ByteBuffer;
 import org.apache.commons.runtime.Native;
 
 public class TestOpenSSL extends Assert
@@ -75,5 +76,32 @@ public class TestOpenSSL extends Assert
         }
     }
 
+    @Test(groups = { "openssl" })
+    public void randomBytes()
+        throws Exception
+    {
+        byte[] b = new byte[1024];
+        Random r = new Random();
+        r.nextBytes(b);
+        r.nextByte();
+        r.nextBytes(b);
+        ByteBuffer bb = ByteBuffer.allocateDirect(1024);
+        r.nextBytes(bb);
+    }
+
+    @Test(groups = { "openssl" })
+    public void randomBuffer()
+        throws Exception
+    {
+        Random r = new Random();
+        r.nextByte();
+        ByteBuffer db = ByteBuffer.allocateDirect(1964);
+        r.nextBytes(db);
+        assertEquals(db.remaining(), 0);
+        ByteBuffer bb = ByteBuffer.allocate(2303);
+        r.nextBytes(bb);
+        assertEquals(bb.remaining(), 0);
+    }
 
+    
 }



Mime
View raw message