You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/12/12 01:07:04 UTC
[commons-crypto] branch master updated: New test, formatting
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 385f15e New test, formatting
385f15e is described below
commit 385f15e87edf1fec47268b60badd4a954ebfa98f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 11 20:07:00 2022 -0500
New test, formatting
---
.../java/org/apache/commons/crypto/Crypto.java | 11 +--
.../java/org/apache/commons/crypto/OsInfo.java | 99 +++++++++++-----------
.../commons/crypto/jna/OpenSslJnaCryptoRandom.java | 16 ++--
.../java/org/apache/commons/crypto/CryptoTest.java | 69 ++++++++-------
.../java/org/apache/commons/crypto/OsInfoTest.java | 30 +++++++
.../crypto/jna/OpenSslJnaCryptoRandomTest.java | 43 +++++-----
6 files changed, 145 insertions(+), 123 deletions(-)
diff --git a/src/main/java/org/apache/commons/crypto/Crypto.java b/src/main/java/org/apache/commons/crypto/Crypto.java
index 44c2191..5780129 100644
--- a/src/main/java/org/apache/commons/crypto/Crypto.java
+++ b/src/main/java/org/apache/commons/crypto/Crypto.java
@@ -30,7 +30,7 @@ import org.apache.commons.crypto.utils.AES;
/**
* Provides diagnostic information about Commons Crypto and keys for native
- * class loading
+ * class loading.
*/
public final class Crypto {
@@ -49,7 +49,6 @@ public final class Crypto {
if (url != null) {
try (InputStream inputStream = url.openStream()) {
versionData.load(inputStream);
- return versionData;
} catch (final IOException e) { // NOPMD
}
}
@@ -151,7 +150,7 @@ public final class Crypto {
* @throws Exception if getCryptoRandom or getCryptoCipher get error.
*/
public static void main(final String[] args) throws Exception {
- quiet = args.length ==1 && args[0].equals("-q");
+ quiet = args.length == 1 && args[0].equals("-q");
info("%s %s", getComponentName(), getComponentVersion());
if (isNativeCodeLoaded()) {
info("Native code loaded OK: %s", OpenSslInfoNative.NativeVersion());
@@ -163,16 +162,14 @@ public final class Crypto {
info("DLL path: %s", OpenSslInfoNative.DLLPath());
{ // CryptoRandom
final Properties props = new Properties();
- props.setProperty(CryptoRandomFactory.CLASSES_KEY,
- CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());
+ props.setProperty(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());
try (CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(props)) {
info("Random instance created OK: %s", cryptoRandom);
}
}
{ // CryptoCipher
final Properties props = new Properties();
- props.setProperty(CryptoCipherFactory.CLASSES_KEY,
- CryptoCipherFactory.CipherProvider.OPENSSL.getClassName());
+ props.setProperty(CryptoCipherFactory.CLASSES_KEY, CryptoCipherFactory.CipherProvider.OPENSSL.getClassName());
try (CryptoCipher cryptoCipher = CryptoCipherFactory.getCryptoCipher(AES.CTR_NO_PADDING, props)) {
info("Cipher %s instance created OK: %s", AES.CTR_NO_PADDING, cryptoCipher);
}
diff --git a/src/main/java/org/apache/commons/crypto/OsInfo.java b/src/main/java/org/apache/commons/crypto/OsInfo.java
index 01cca69..5ec55a6 100644
--- a/src/main/java/org/apache/commons/crypto/OsInfo.java
+++ b/src/main/java/org/apache/commons/crypto/OsInfo.java
@@ -65,12 +65,6 @@ final class OsInfo {
*/
static final String PPC64 = "ppc64";
- /**
- * The private constructor of {@link OsInfo}.
- */
- private OsInfo() {
- }
-
static {
// x86 mappings
archMapping.put(X86, X86);
@@ -110,23 +104,33 @@ final class OsInfo {
}
/**
- * The main method. This is used by the JNI make processing in Makefile.common
+ * Gets the architecture name.
*
- * @param args the argv.
+ * @return the architecture name.
*/
- public static void main(final String[] args) {
- if (args.length >= 1) {
- if ("--os".equals(args[0])) {
- System.out.print(getOSName());
- return;
+ static String getArchName() {
+ // if running Linux on ARM, need to determine ABI of JVM
+ final String osArch = System.getProperty("os.arch");
+ if (osArch.startsWith("arm") && getOsNameProperty().contains("Linux")) {
+ final String javaHome = System.getProperty("java.home");
+ try {
+ // determine if first JVM found uses ARM hard-float ABI
+ final String[] cmdarray = { "/bin/sh", "-c",
+ "find '" + javaHome + "' -name 'libjvm.so' | head -1 | xargs readelf -A | grep 'Tag_ABI_VFP_args: VFP registers'" };
+ final int exitCode = Runtime.getRuntime().exec(cmdarray).waitFor();
+ if (exitCode == 0) {
+ return "armhf";
+ }
+ } catch (final IOException | InterruptedException e) { // NOPMD
+ // ignored: fall back to "arm" arch (soft-float ABI)
}
- if ("--arch".equals(args[0])) {
- System.out.print(getArchName());
- return;
+ } else {
+ final String string = archMapping.get(osArch.toLowerCase(Locale.US));
+ if (string != null) {
+ return string;
}
}
-
- System.out.print(getNativeLibFolderPathForCurrentOS());
+ return translateArchNameToFolderName(osArch);
}
/**
@@ -144,38 +148,41 @@ final class OsInfo {
* @return the OS name.
*/
static String getOSName() {
- return translateOSNameToFolderName(System.getProperty("os.name"));
+ return translateOSNameToFolderName(getOsNameProperty());
+ }
+
+ static String getOsNameProperty() {
+ return System.getProperty("os.name");
}
/**
- * Gets the architecture name.
+ * The main method. This is used by the JNI make processing in Makefile.common
*
- * @return the architecture name.
+ * @param args the argv.
*/
- static String getArchName() {
- // if running Linux on ARM, need to determine ABI of JVM
- final String osArch = System.getProperty("os.arch");
- if (osArch.startsWith("arm") && System.getProperty("os.name").contains("Linux")) {
- final String javaHome = System.getProperty("java.home");
- try {
- // determine if first JVM found uses ARM hard-float ABI
- final String[] cmdarray = { "/bin/sh", "-c",
- "find '" + javaHome + "' -name 'libjvm.so' | head -1 | xargs readelf -A | "
- + "grep 'Tag_ABI_VFP_args: VFP registers'" };
- final int exitCode = Runtime.getRuntime().exec(cmdarray).waitFor();
- if (exitCode == 0) {
- return "armhf";
- }
- } catch (final IOException | InterruptedException e) { // NOPMD
- // ignored: fall back to "arm" arch (soft-float ABI)
+ public static void main(final String[] args) {
+ if (args.length >= 1) {
+ if ("--os".equals(args[0])) {
+ System.out.print(getOSName());
+ return;
}
- } else {
- final String string = archMapping.get(osArch.toLowerCase(Locale.US));
- if (string != null) {
- return string;
+ if ("--arch".equals(args[0])) {
+ System.out.print(getArchName());
+ return;
}
}
- return translateArchNameToFolderName(osArch);
+
+ System.out.println(getNativeLibFolderPathForCurrentOS());
+ }
+
+ /**
+ * Translates the architecture name to folder name.
+ *
+ * @param archName the architecture name.
+ * @return the folder name.
+ */
+ private static String translateArchNameToFolderName(final String archName) {
+ return archName.replaceAll("\\W", "");
}
/**
@@ -201,12 +208,8 @@ final class OsInfo {
}
/**
- * Translates the architecture name to folder name.
- *
- * @param archName the architecture name.
- * @return the folder name.
+ * The private constructor of {@link OsInfo}.
*/
- private static String translateArchNameToFolderName(final String archName) {
- return archName.replaceAll("\\W", "");
+ private OsInfo() {
}
}
diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java
index 75e5b92..5a8d842 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java
@@ -69,12 +69,12 @@ class OpenSslJnaCryptoRandom extends Random implements CryptoRandom {
OpenSslNativeJna.ENGINE_load_rdrand();
rdrandEngine = OpenSslNativeJna.ENGINE_by_id("rdrand");
final int ENGINE_METHOD_RAND = 0x0008;
- if(rdrandEngine != null) {
+ if (rdrandEngine != null) {
final int rc = OpenSslNativeJna.ENGINE_init(rdrandEngine);
- if(rc != 0) {
+ if (rc != 0) {
final int rc2 = OpenSslNativeJna.ENGINE_set_default(rdrandEngine, ENGINE_METHOD_RAND);
- if(rc2 != 0) {
+ if (rc2 != 0) {
rdrandLoaded = true;
}
}
@@ -86,7 +86,7 @@ class OpenSslJnaCryptoRandom extends Random implements CryptoRandom {
rdrandEnabled = rdrandLoaded;
- if(!rdrandLoaded) {
+ if (!rdrandLoaded) {
closeRdrandEngine();
}
}
@@ -100,10 +100,10 @@ class OpenSslJnaCryptoRandom extends Random implements CryptoRandom {
public void nextBytes(final byte[] bytes) {
synchronized (OpenSslJnaCryptoRandom.class) {
- //this method is synchronized for now
- //to support multithreading https://wiki.openssl.org/index.php/Manual:Threads(3) needs to be done
+ // this method is synchronized for now
+ // to support multithreading https://wiki.openssl.org/index.php/Manual:Threads(3) needs to be done
- if(rdrandEnabled && OpenSslNativeJna.RAND_get_rand_method().equals(OpenSslNativeJna.RAND_SSLeay())) {
+ if (rdrandEnabled && OpenSslNativeJna.RAND_get_rand_method().equals(OpenSslNativeJna.RAND_SSLeay())) {
close();
throw new IllegalStateException("rdrand should be used but default is detected");
}
@@ -113,7 +113,7 @@ class OpenSslJnaCryptoRandom extends Random implements CryptoRandom {
final int retVal = OpenSslNativeJna.RAND_bytes(buf, byteLength);
throwOnError(retVal);
buf.rewind();
- buf.get(bytes,0, byteLength);
+ buf.get(bytes, 0, byteLength);
}
}
diff --git a/src/test/java/org/apache/commons/crypto/CryptoTest.java b/src/test/java/org/apache/commons/crypto/CryptoTest.java
index c94f4d6..eeb1217 100644
--- a/src/test/java/org/apache/commons/crypto/CryptoTest.java
+++ b/src/test/java/org/apache/commons/crypto/CryptoTest.java
@@ -17,7 +17,6 @@
*/
package org.apache.commons.crypto;
-
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -25,43 +24,41 @@ import org.junit.jupiter.api.Test;
public class CryptoTest {
- /**
- * This test may fail unless the code was built by Maven, as it relies on the
- * VERSION file being set up correctly
- */
- @Test
- public void testGetComponentName() {
- final String version = Crypto.getComponentName();
- assertNotNull("Should not be null", version);
- assertTrue(version.matches("^Apache Commons Crypto.*"), version);
- }
+ /**
+ * This test may fail unless the code was built by Maven, as it relies on the VERSION file being set up correctly
+ */
+ @Test
+ public void testGetComponentName() {
+ final String version = Crypto.getComponentName();
+ assertNotNull("Should not be null", version);
+ assertTrue(version.matches("^Apache Commons Crypto.*"), version);
+ }
- /**
- * This test may fail unless the code was built by Maven, as it relies on the
- * VERSION file being set up correctly.
- */
- @Test
- public void testGetComponentVersion() {
- final String version = Crypto.getComponentVersion();
- assertNotNull("Should not be null", version);
- assertTrue(version.matches("^\\d+\\.\\d+.*"), version);
- }
+ /**
+ * This test may fail unless the code was built by Maven, as it relies on the VERSION file being set up correctly.
+ */
+ @Test
+ public void testGetComponentVersion() {
+ final String version = Crypto.getComponentVersion();
+ assertNotNull("Should not be null", version);
+ assertTrue(version.matches("^\\d+\\.\\d+.*"), version);
+ }
- @Test
- public void testMain() throws Throwable {
- // Check that Crypt.main will actually run tests
- assertTrue(Crypto.isNativeCodeLoaded(), "Native code loaded OK");
- Crypto.main(new String[]{"-q"}); // output causes issues for testing
- assertTrue(Crypto.isNativeCodeLoaded(), "Completed OK");
- }
+ @Test
+ public void testMain() throws Throwable {
+ // Check that Crypt.main will actually run tests
+ assertTrue(Crypto.isNativeCodeLoaded(), "Native code loaded OK");
+ Crypto.main(new String[] { "-q" }); // output causes issues for testing
+ assertTrue(Crypto.isNativeCodeLoaded(), "Completed OK");
+ }
- @Test
- public void testLoadingError() throws Throwable {
- final Throwable loadingError = Crypto.getLoadingError();
- if (loadingError != null) {
- throw loadingError;
- }
- assertTrue(true, "Completed OK");
- }
+ @Test
+ public void testLoadingError() throws Throwable {
+ final Throwable loadingError = Crypto.getLoadingError();
+ if (loadingError != null) {
+ throw loadingError;
+ }
+ assertTrue(true, "Completed OK");
+ }
}
diff --git a/src/test/java/org/apache/commons/crypto/OsInfoTest.java b/src/test/java/org/apache/commons/crypto/OsInfoTest.java
new file mode 100644
index 0000000..a1c9e74
--- /dev/null
+++ b/src/test/java/org/apache/commons/crypto/OsInfoTest.java
@@ -0,0 +1,30 @@
+/*
+ * 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;
+
+import org.junit.jupiter.api.Test;
+
+public class OsInfoTest {
+
+ @Test
+ public void testMain() {
+ OsInfo.main(new String[0]);
+ OsInfo.main(new String[] { "--os" });
+ OsInfo.main(new String[] { "--arch" });
+ }
+}
diff --git a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java
index 48234fa..4678400 100644
--- a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.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.jna;
-
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.security.GeneralSecurityException;
@@ -39,13 +38,9 @@ public class OpenSslJnaCryptoRandomTest extends AbstractRandomTest {
@Override
public CryptoRandom getCryptoRandom() throws GeneralSecurityException {
final Properties props = new Properties();
- props.setProperty(
- CryptoRandomFactory.CLASSES_KEY,
- OpenSslJnaCryptoRandom.class.getName());
+ props.setProperty(CryptoRandomFactory.CLASSES_KEY, OpenSslJnaCryptoRandom.class.getName());
final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
- assertTrue(
- random instanceof OpenSslJnaCryptoRandom,
- "The CryptoRandom should be: " + OpenSslJnaCryptoRandom.class.getName());
+ assertTrue(random instanceof OpenSslJnaCryptoRandom, "The CryptoRandom should be: " + OpenSslJnaCryptoRandom.class.getName());
return random;
}