You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2016/06/27 22:11:07 UTC

commons-crypto git commit: More tests

Repository: commons-crypto
Updated Branches:
  refs/heads/master 5b78fca11 -> 896db5955


More tests

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

Branch: refs/heads/master
Commit: 896db59550516d4147290bc98a0efc305a5df270
Parents: 5b78fca
Author: Sebb <se...@apache.org>
Authored: Mon Jun 27 23:11:04 2016 +0100
Committer: Sebb <se...@apache.org>
Committed: Mon Jun 27 23:11:04 2016 +0100

----------------------------------------------------------------------
 .../commons/crypto/random/AbstractRandom.java   |  25 ++++
 .../commons/crypto/random/DummyRandom.java      |  32 +++++
 .../commons/crypto/random/TestCryptoRandom.java | 131 +++++++++++++++++++
 3 files changed, 188 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/896db595/src/test/java/org/apache/commons/crypto/random/AbstractRandom.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/crypto/random/AbstractRandom.java b/src/test/java/org/apache/commons/crypto/random/AbstractRandom.java
new file mode 100644
index 0000000..97d8c75
--- /dev/null
+++ b/src/test/java/org/apache/commons/crypto/random/AbstractRandom.java
@@ -0,0 +1,25 @@
+/*
+ * 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;
+
+/**
+ * For testing class creation
+ */
+abstract class AbstractRandom implements CryptoRandom {
+
+}

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/896db595/src/test/java/org/apache/commons/crypto/random/DummyRandom.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/crypto/random/DummyRandom.java b/src/test/java/org/apache/commons/crypto/random/DummyRandom.java
new file mode 100644
index 0000000..551ab36
--- /dev/null
+++ b/src/test/java/org/apache/commons/crypto/random/DummyRandom.java
@@ -0,0 +1,32 @@
+/*
+ * 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 java.io.IOException;
+
+class DummyRandom implements CryptoRandom {
+
+    @Override
+    public void close() throws IOException {
+    }
+
+    @Override
+    public void nextBytes(byte[] bytes) {
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/896db595/src/test/java/org/apache/commons/crypto/random/TestCryptoRandom.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/crypto/random/TestCryptoRandom.java b/src/test/java/org/apache/commons/crypto/random/TestCryptoRandom.java
new file mode 100644
index 0000000..d2b1c4c
--- /dev/null
+++ b/src/test/java/org/apache/commons/crypto/random/TestCryptoRandom.java
@@ -0,0 +1,131 @@
+/**
+ * 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.Assert.assertTrue;
+
+import java.util.Properties;
+
+import org.apache.commons.crypto.conf.ConfigurationKeys;
+import org.junit.Test;
+
+public class TestCryptoRandom {
+
+    @Test(expected=NullPointerException.class)
+    public void testNull() throws Exception {
+        CryptoRandomFactory.getCryptoRandom(null);
+    }
+
+    @Test
+    public void testEmpty() throws Exception {
+        final Properties props = new Properties();
+        props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "");
+        CryptoRandomFactory.getCryptoRandom(props);
+    }
+
+    @Test
+    public void testEmptyFallback() throws Exception {
+        final Properties props = new Properties();
+        props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "");
+        props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "");
+        CryptoRandomFactory.getCryptoRandom(props);
+    }
+
+    @Test
+    public void testTrueFallback() throws Exception {
+        final Properties props = new Properties();
+        props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "");
+        props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "true");
+        CryptoRandomFactory.getCryptoRandom(props);
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testFalseFallbackEmpty() throws Exception {
+        final Properties props = new Properties();
+        props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "");
+        props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
+        CryptoRandomFactory.getCryptoRandom(props);
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testFalseFallbackNoNames() throws Exception {
+        final Properties props = new Properties();
+        props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, ",,,,");
+        props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
+        CryptoRandomFactory.getCryptoRandom(props);
+    }
+
+    @Test
+    public void testNoSuchClass() throws Exception {
+        final Properties props = new Properties();
+        props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "noSuchClass");
+        props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
+        try {
+            CryptoRandomFactory.getCryptoRandom(props);
+        } catch (Exception e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("not found"));
+            assertTrue(message, message.contains("noSuchClass"));
+        }
+    }
+
+    @Test
+    public void testWrongClass() throws Exception {
+        final Properties props = new Properties();
+        props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, "java.util.Properties"); // Use a class that accepts a Properties object
+        props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
+        try {
+            CryptoRandomFactory.getCryptoRandom(props);
+        } catch (Exception e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("java.util.Properties"));
+            assertTrue(message, message.contains("not a CryptoRandom"));
+        }
+    }
+
+    @Test
+    public void testWrongClassBadCtor() throws Exception {
+        final Properties props = new Properties();
+        final String canonicalName = DummyRandom.class.getCanonicalName();
+        props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, canonicalName);
+        props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
+        try {
+            CryptoRandomFactory.getCryptoRandom(props);
+        } catch (Exception e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains(canonicalName));
+            assertTrue(message, message.contains("NoSuchMethodException"));
+        }
+    }
+
+    @Test
+    public void testAbstractClass() throws Exception {
+        final Properties props = new Properties();
+        final String canonicalName = AbstractRandom.class.getCanonicalName();
+        props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, canonicalName);
+        props.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "notTrue");
+        try {
+            CryptoRandomFactory.getCryptoRandom(props);
+        } catch (Exception e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains(canonicalName));
+            assertTrue(message, message.contains("NoSuchMethodException"));
+        }
+    }
+
+}