commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s..@apache.org
Subject [2/2] commons-crypto git commit: CRYPTO-62: Add multithreaded related tests and javadoc comments
Date Mon, 06 Jun 2016 03:53:18 GMT
CRYPTO-62: Add multithreaded related tests and javadoc comments


Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/0fa9f0a2
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/0fa9f0a2
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/0fa9f0a2

Branch: refs/heads/master
Commit: 0fa9f0a202f187af1c7bfa8058d64cafd91dd2c6
Parents: 46f2dfc
Author: Hendrik Saly <hendrikdev22@gmail.com>
Authored: Mon Jun 6 11:42:53 2016 +0800
Committer: Sun Dapeng <sdp@apache.org>
Committed: Mon Jun 6 11:43:19 2016 +0800

----------------------------------------------------------------------
 .../commons/crypto/cipher/CryptoCipher.java     |  4 +++
 .../commons/crypto/cipher/OpensslNative.java    |  2 +-
 .../crypto/random/AbstractRandomTest.java       | 32 +++++++++++++++++++-
 3 files changed, 36 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/0fa9f0a2/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java
index 9781ab2..3b31a57 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipher.java
@@ -32,6 +32,10 @@ import javax.crypto.ShortBufferException;
 
 /**
  * The interface of cryptographic cipher for encryption and decryption.
+ *
+ * <p><strong>Note that this implementation is not synchronized.</strong>
+ * Multiple threads must not access a crypto cipher instance concurrently.
+ * Use one crypto cipher instance per thread to circumvent this.
  */
 public interface CryptoCipher extends Closeable {
 

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/0fa9f0a2/src/main/java/org/apache/commons/crypto/cipher/OpensslNative.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/cipher/OpensslNative.java b/src/main/java/org/apache/commons/crypto/cipher/OpensslNative.java
index 15060c9..9993098 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/OpensslNative.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/OpensslNative.java
@@ -21,7 +21,7 @@ import java.nio.ByteBuffer;
 
 /**
  * JNI interface of {@link Openssl} implementation. The native method in this
- * class is defined in OpensslNative.h(genereted by javah).
+ * class is defined in OpensslNative.h (generated by javah).
  */
 public class OpensslNative {
 

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/0fa9f0a2/src/test/java/org/apache/commons/crypto/random/AbstractRandomTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/crypto/random/AbstractRandomTest.java b/src/test/java/org/apache/commons/crypto/random/AbstractRandomTest.java
index 2ccd163..1c1bab2 100644
--- a/src/test/java/org/apache/commons/crypto/random/AbstractRandomTest.java
+++ b/src/test/java/org/apache/commons/crypto/random/AbstractRandomTest.java
@@ -17,8 +17,11 @@
  */
 package org.apache.commons.crypto.random;
 
+import java.lang.Thread.State;
 import java.security.GeneralSecurityException;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 
 import org.junit.Test;
 
@@ -41,6 +44,33 @@ public abstract class AbstractRandomTest {
         random.close();
     }
 
+    @Test(timeout = 120000)
+    public void testRandomBytesMultiThreaded() throws Exception {
+        final int threadCount = 100;
+        final CryptoRandom random = getCryptoRandom();
+        final List<Thread> threads = new ArrayList<Thread>(threadCount);
+
+        for(int i=0; i< threadCount; i++) {
+            Thread t = new Thread(new Runnable() {
+                public void run() {
+                    checkRandomBytes(random, 10);
+                    checkRandomBytes(random, 1000);
+                    checkRandomBytes(random, 100000);
+                }
+            });
+            t.start();
+            threads.add(t);
+        }
+
+        for(Thread t: threads) {
+            if(!t.getState().equals(State.NEW)) {
+                t.join();
+            }
+        }
+
+        random.close();
+    }
+
     /**
      * Test will timeout if secure random implementation always returns a
      * constant value.
@@ -51,7 +81,7 @@ public abstract class AbstractRandomTest {
         random.nextBytes(bytes);
         random.nextBytes(bytes1);
 
-        while (Arrays.equals(bytes, bytes1)) {
+        while (Arrays.equals(bytes1, new byte[len]) || Arrays.equals(bytes, bytes1)) {
             random.nextBytes(bytes1);
         }
     }


Mime
View raw message