This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-crypto.git
The following commit(s) were added to refs/heads/master by this push: new 8e6015e Use try-with-resources 8e6015e is described below commit 8e6015e3f085f098c9b62b746e6e5b651401288a Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Dec 12 07:03:02 2022 -0500 Use try-with-resources Format Javadoc --- .../apache/commons/crypto/random/CryptoRandom.java | 3 +- .../crypto/random/CryptoRandomFactoryTest.java | 108 ++++++++++----------- .../apache/commons/crypto/random/DummyRandom.java | 6 +- .../commons/crypto/random/FailingRandom.java | 4 +- .../crypto/random/JavaCryptoRandomTest.java | 66 ++++++------- .../crypto/random/OpenSslCryptoRandomTest.java | 43 ++++---- .../commons/crypto/random/OsCryptoRandomTest.java | 54 +++++------ 7 files changed, 135 insertions(+), 149 deletions(-) diff --git a/src/main/java/org/apache/commons/crypto/random/CryptoRandom.java b/src/main/java/org/apache/commons/crypto/random/CryptoRandom.java index e0d956f..a45a89d 100644 --- a/src/main/java/org/apache/commons/crypto/random/CryptoRandom.java +++ b/src/main/java/org/apache/commons/crypto/random/CryptoRandom.java @@ -22,7 +22,8 @@ import java.io.Closeable; /** * The interface for CryptoRandom. * <p> - * Note that implementations must provide a constructor that takes a Properties instance + * Note that implementations must provide a constructor that takes a Properties instance. + * </p> */ public interface CryptoRandom extends Closeable { diff --git a/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java b/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java index c27341c..dbaa567 100644 --- a/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java +++ b/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java @@ -1,20 +1,20 @@ - /* - * 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. - */ +/* +* 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.commons.crypto.random; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -22,92 +22,82 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.security.GeneralSecurityException; import java.util.Properties; import org.junit.jupiter.api.Test; - public class CryptoRandomFactoryTest { @Test public void testNull() { - assertThrows(NullPointerException.class, - () -> CryptoRandomFactory.getCryptoRandom(null)); + assertThrows(NullPointerException.class, () -> CryptoRandomFactory.getCryptoRandom(null)); } @Test public void testEmpty() throws Exception { final Properties props = new Properties(); props.setProperty(CryptoRandomFactory.CLASSES_KEY, ""); - CryptoRandomFactory.getCryptoRandom(props); + CryptoRandomFactory.getCryptoRandom(props).close(); } - @Test - public void testDefaultRandom() throws GeneralSecurityException { + public void testDefaultRandom() throws GeneralSecurityException, IOException { final Properties props = new Properties(); - final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); - final String name = random.getClass().getName(); - if (OpenSslCryptoRandom.isNativeCodeEnabled()) { - assertEquals(OpenSslCryptoRandom.class.getName(), name); - } else { - assertEquals(JavaCryptoRandom.class.getName(), name); + try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props)) { + final String name = random.getClass().getName(); + if (OpenSslCryptoRandom.isNativeCodeEnabled()) { + assertEquals(OpenSslCryptoRandom.class.getName(), name); + } else { + assertEquals(JavaCryptoRandom.class.getName(), name); + } } } @Test - public void testGetOSRandom() throws GeneralSecurityException { + public void testGetOSRandom() throws GeneralSecurityException, IOException { // Windows does not have a /dev/random device assumeTrue(!System.getProperty("os.name").contains("Windows")); final Properties props = new Properties(); - props.setProperty( - CryptoRandomFactory.CLASSES_KEY, - CryptoRandomFactory.RandomProvider.OS.getClassName()); - final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); - - assertEquals(OsCryptoRandom.class.getName(), random.getClass() - .getName()); + props.setProperty(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OS.getClassName()); + try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props)) { + assertEquals(OsCryptoRandom.class.getName(), random.getClass().getName()); + } } @Test - public void testFullClassName() throws GeneralSecurityException { + public void testFullClassName() throws GeneralSecurityException, IOException { final Properties props = new Properties(); - props.setProperty( - CryptoRandomFactory.CLASSES_KEY, - JavaCryptoRandom.class.getName()); - final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); - - assertEquals(JavaCryptoRandom.class.getName(), random.getClass() - .getName()); + props.setProperty(CryptoRandomFactory.CLASSES_KEY, JavaCryptoRandom.class.getName()); + try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props)) { + assertEquals(JavaCryptoRandom.class.getName(), random.getClass().getName()); + } } @Test public void testInvalidRandom() { final Properties properties = new Properties(); - properties.setProperty( - CryptoRandomFactory.CLASSES_KEY, - "InvalidCipherName"); + properties.setProperty(CryptoRandomFactory.CLASSES_KEY, "InvalidCipherName"); - assertThrows(GeneralSecurityException.class, - () -> CryptoRandomFactory.getCryptoRandom(properties)); + assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(properties)); } @Test - public void testInvalidRandomClass() throws GeneralSecurityException { + public void testInvalidRandomClass() throws GeneralSecurityException, IOException { final Properties properties = new Properties(); - properties.setProperty( - "org.apache.commons.crypto.cipher", - "OpenSsl"); - final CryptoRandom rand = CryptoRandomFactory.getCryptoRandom(properties); - assertEquals(OpenSslCryptoRandom.class.getName(), rand.getClass().getName()); + properties.setProperty("org.apache.commons.crypto.cipher", "OpenSsl"); + try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties)) { + assertEquals(OpenSslCryptoRandom.class.getName(), random.getClass().getName()); + } } @Test - public void testDefaultRandomClass() throws GeneralSecurityException { - final CryptoRandom rand = CryptoRandomFactory.getCryptoRandom(); - assertEquals(OpenSslCryptoRandom.class.getName(), rand.getClass().getName()); + public void testDefaultRandomClass() throws GeneralSecurityException, IOException { + try (final CryptoRandom random = CryptoRandomFactory.getCryptoRandom()) { + assertEquals(OpenSslCryptoRandom.class.getName(), random.getClass().getName()); + } } @Test diff --git a/src/test/java/org/apache/commons/crypto/random/DummyRandom.java b/src/test/java/org/apache/commons/crypto/random/DummyRandom.java index 4d1b4ac..a240841 100644 --- a/src/test/java/org/apache/commons/crypto/random/DummyRandom.java +++ b/src/test/java/org/apache/commons/crypto/random/DummyRandom.java @@ -20,17 +20,19 @@ package org.apache.commons.crypto.random; class DummyRandom implements CryptoRandom { - // Should fail with NoSuchMethodException + /** Should fail with NoSuchMethodException. */ DummyRandom() { - + // empty } @Override public void close() { + // empty } @Override public void nextBytes(final byte[] bytes) { + // empty } } diff --git a/src/test/java/org/apache/commons/crypto/random/FailingRandom.java b/src/test/java/org/apache/commons/crypto/random/FailingRandom.java index 6600bc7..3f73709 100644 --- a/src/test/java/org/apache/commons/crypto/random/FailingRandom.java +++ b/src/test/java/org/apache/commons/crypto/random/FailingRandom.java @@ -21,17 +21,19 @@ import java.util.Properties; class FailingRandom implements CryptoRandom { - // Should fail with NoSuchMethodException + /** Should fail with NoSuchMethodException. */ FailingRandom(final Properties props) { NoSuchMethod(); } @Override public void close() { + // empty } @Override public void nextBytes(final byte[] bytes) { + // empty } public static native void NoSuchMethod(); diff --git a/src/test/java/org/apache/commons/crypto/random/JavaCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/random/JavaCryptoRandomTest.java index 881fbe3..e4eb5bd 100644 --- a/src/test/java/org/apache/commons/crypto/random/JavaCryptoRandomTest.java +++ b/src/test/java/org/apache/commons/crypto/random/JavaCryptoRandomTest.java @@ -1,20 +1,20 @@ - /* - * 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. - */ +/* +* 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.commons.crypto.random; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -31,29 +31,25 @@ public class JavaCryptoRandomTest extends AbstractRandomTest { @Override public CryptoRandom getCryptoRandom() throws GeneralSecurityException { final Properties props = new Properties(); - props.setProperty( - CryptoRandomFactory.CLASSES_KEY, - JavaCryptoRandom.class.getName()); + props.setProperty(CryptoRandomFactory.CLASSES_KEY, JavaCryptoRandom.class.getName()); final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); - assertTrue( - random instanceof JavaCryptoRandom, - "The CryptoRandom should be: " + JavaCryptoRandom.class.getName()); + assertTrue(random instanceof JavaCryptoRandom, "The CryptoRandom should be: " + JavaCryptoRandom.class.getName()); return random; } @Test public void testNextIntIsntActuallyRandomNextInt() throws Exception { - final CryptoRandom cr = getCryptoRandom(); - final Random r = (Random) cr; - final long seed = 1654421930011L; // System.getCurrentMillis() on 2022-June-05, 11:39 - final Random otherRandom = new Random(seed); - final Random otherRandom2 = new Random(); - otherRandom2.setSeed(seed); - r.setSeed(seed); - final long l1 = r.nextLong(); - final long l2 = otherRandom.nextLong(); - final long l3 = otherRandom2.nextLong(); - assertEquals(l2, l3); - assertNotEquals(l1, l2); + final CryptoRandom cr = getCryptoRandom(); + final Random r = (Random) cr; + final long seed = 1654421930011L; // System.getCurrentMillis() on 2022-June-05, 11:39 + final Random otherRandom = new Random(seed); + final Random otherRandom2 = new Random(); + otherRandom2.setSeed(seed); + r.setSeed(seed); + final long l1 = r.nextLong(); + final long l2 = otherRandom.nextLong(); + final long l3 = otherRandom2.nextLong(); + assertEquals(l2, l3); + assertNotEquals(l1, l2); } } diff --git a/src/test/java/org/apache/commons/crypto/random/OpenSslCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/random/OpenSslCryptoRandomTest.java index a99ffdf..a9f86e2 100644 --- a/src/test/java/org/apache/commons/crypto/random/OpenSslCryptoRandomTest.java +++ b/src/test/java/org/apache/commons/crypto/random/OpenSslCryptoRandomTest.java @@ -1,23 +1,22 @@ - /* - * 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. - */ +/* +* 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.commons.crypto.random; - import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -32,13 +31,9 @@ public class OpenSslCryptoRandomTest extends AbstractRandomTest { public CryptoRandom getCryptoRandom() throws GeneralSecurityException { assumeTrue(Crypto.isNativeCodeLoaded()); final Properties props = new Properties(); - props.setProperty( - CryptoRandomFactory.CLASSES_KEY, - OpenSslCryptoRandom.class.getName()); + props.setProperty(CryptoRandomFactory.CLASSES_KEY, OpenSslCryptoRandom.class.getName()); final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); - assertTrue( - random instanceof OpenSslCryptoRandom, - "The CryptoRandom should be: " + OpenSslCryptoRandom.class.getName()); + assertTrue(random instanceof OpenSslCryptoRandom, "The CryptoRandom should be: " + OpenSslCryptoRandom.class.getName()); return random; } diff --git a/src/test/java/org/apache/commons/crypto/random/OsCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/random/OsCryptoRandomTest.java index b3b4044..4cb742f 100644 --- a/src/test/java/org/apache/commons/crypto/random/OsCryptoRandomTest.java +++ b/src/test/java/org/apache/commons/crypto/random/OsCryptoRandomTest.java @@ -32,33 +32,33 @@ import org.junit.jupiter.api.Test; public class OsCryptoRandomTest extends AbstractRandomTest { - @Override - public CryptoRandom getCryptoRandom() throws GeneralSecurityException { - // Windows does not have a /dev/random device - assumeTrue(!System.getProperty("os.name").contains("Windows")); - final Properties props = new Properties(); - props.setProperty(CryptoRandomFactory.CLASSES_KEY, OsCryptoRandom.class.getName()); - final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); - assertTrue(random instanceof OsCryptoRandom, "The CryptoRandom should be: " + OsCryptoRandom.class.getName()); - return random; - } + @Override + public CryptoRandom getCryptoRandom() throws GeneralSecurityException { + // Windows does not have a /dev/random device + assumeTrue(!System.getProperty("os.name").contains("Windows")); + final Properties props = new Properties(); + props.setProperty(CryptoRandomFactory.CLASSES_KEY, OsCryptoRandom.class.getName()); + final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); + assertTrue(random instanceof OsCryptoRandom, "The CryptoRandom should be: " + OsCryptoRandom.class.getName()); + return random; + } - @Test - public void testInvalidRandom() { - final Properties props = new Properties(); - props.setProperty(CryptoRandomFactory.CLASSES_KEY, OsCryptoRandom.class.getName()); - // Invalid device - props.setProperty(CryptoRandomFactory.DEVICE_FILE_PATH_KEY, ""); - final Exception e = assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(props)); - Throwable cause; - cause = e.getCause(); - assertEquals(IllegalArgumentException.class, cause.getClass()); - cause = cause.getCause(); - assertEquals(InvocationTargetException.class, cause.getClass()); - cause = cause.getCause(); - assertEquals(IllegalArgumentException.class, cause.getClass()); - cause = cause.getCause(); - assertEquals(FileNotFoundException.class, cause.getClass()); + @Test + public void testInvalidRandom() { + final Properties props = new Properties(); + props.setProperty(CryptoRandomFactory.CLASSES_KEY, OsCryptoRandom.class.getName()); + // Invalid device + props.setProperty(CryptoRandomFactory.DEVICE_FILE_PATH_KEY, ""); + final Exception e = assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(props)); + Throwable cause; + cause = e.getCause(); + assertEquals(IllegalArgumentException.class, cause.getClass()); + cause = cause.getCause(); + assertEquals(InvocationTargetException.class, cause.getClass()); + cause = cause.getCause(); + assertEquals(IllegalArgumentException.class, cause.getClass()); + cause = cause.getCause(); + assertEquals(FileNotFoundException.class, cause.getClass()); - } + } }