Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id CCE0B200B3B for ; Mon, 6 Jun 2016 05:53:19 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id CB922160A28; Mon, 6 Jun 2016 03:53:19 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 1A0CE160A53 for ; Mon, 6 Jun 2016 05:53:18 +0200 (CEST) Received: (qmail 4772 invoked by uid 500); 6 Jun 2016 03:53:18 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 4641 invoked by uid 99); 6 Jun 2016 03:53:18 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 06 Jun 2016 03:53:18 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E5387DFFD9; Mon, 6 Jun 2016 03:53:17 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sdp@apache.org To: commits@commons.apache.org Date: Mon, 06 Jun 2016 03:53:18 -0000 Message-Id: In-Reply-To: <04412368698446e3bfd6a87da0eafe4b@git.apache.org> References: <04412368698446e3bfd6a87da0eafe4b@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/2] commons-crypto git commit: CRYPTO-62: Add multithreaded related tests and javadoc comments archived-at: Mon, 06 Jun 2016 03:53:20 -0000 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 Authored: Mon Jun 6 11:42:53 2016 +0800 Committer: Sun Dapeng 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. + * + *

Note that this implementation is not synchronized. + * 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 threads = new ArrayList(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); } }