Test fallback SHA1PRNG uses full width seeds
(cherry picked from commit 82575d5df1d80691a9bc96ec1af2c6c32e3426f8)
Bug: 6642616
Change-Id: I922316853eca6fc80c4c5e329864f3a93fc0af4f
diff --git a/luni/src/test/java/org/apache/harmony/security/tests/provider/crypto/SHA1PRNG_SecureRandomTest.java b/luni/src/test/java/org/apache/harmony/security/tests/provider/crypto/SHA1PRNG_SecureRandomTest.java
index ffdbe92..0db5ee1 100644
--- a/luni/src/test/java/org/apache/harmony/security/tests/provider/crypto/SHA1PRNG_SecureRandomTest.java
+++ b/luni/src/test/java/org/apache/harmony/security/tests/provider/crypto/SHA1PRNG_SecureRandomTest.java
@@ -17,6 +17,8 @@
package org.apache.harmony.security.tests.provider.crypto;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
@@ -400,4 +402,31 @@
assertFalse("sequences are equal i=" + i, b);
}
}
+
+ public void testSeedIsFullLength() throws Exception {
+ Class<?> srClass = Class.forName(
+ "org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl");
+ Field seedField = srClass.getDeclaredField("seed");
+ seedField.setAccessible(true);
+
+ Method nextBytesMethod = srClass.getDeclaredMethod("engineNextBytes", byte[].class);
+ nextBytesMethod.setAccessible(true);
+
+ byte[] bytes = new byte[1];
+
+ // Iterate 8 times to make sure the probability of a false positive is
+ // extremely rare.
+ for (int i = 0; i < 8; i++) {
+ Object sr = srClass.newInstance();
+ nextBytesMethod.invoke(sr, bytes);
+ int[] seed = (int[]) seedField.get(sr);
+
+ // If the first integer is not zero, it is fixed.
+ if (seed[0] != 0) {
+ return; // Success
+ }
+ }
+
+ fail("Fallback SHA1PRNG_SecureRandomImpl should not clobber seed internally");
+ }
}