|
@@ -18,12 +18,17 @@
|
|
|
package org.apache.hadoop.crypto;
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
+import org.apache.hadoop.crypto.random.OsSecureRandom;
|
|
|
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
|
|
|
import org.apache.hadoop.test.GenericTestUtils;
|
|
|
import org.junit.BeforeClass;
|
|
|
+import org.junit.Test;
|
|
|
+import org.mockito.internal.util.reflection.Whitebox;
|
|
|
|
|
|
+import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_CODEC_CLASSES_AES_CTR_NOPADDING_KEY;
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
+import static org.junit.Assert.assertNull;
|
|
|
|
|
|
public class TestCryptoStreamsWithOpensslAesCtrCryptoCodec
|
|
|
extends TestCryptoStreams {
|
|
@@ -32,8 +37,7 @@ public class TestCryptoStreamsWithOpensslAesCtrCryptoCodec
|
|
|
public static void init() throws Exception {
|
|
|
GenericTestUtils.assumeInNativeProfile();
|
|
|
Configuration conf = new Configuration();
|
|
|
- conf.set(
|
|
|
- CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_CODEC_CLASSES_AES_CTR_NOPADDING_KEY,
|
|
|
+ conf.set(HADOOP_SECURITY_CRYPTO_CODEC_CLASSES_AES_CTR_NOPADDING_KEY,
|
|
|
OpensslAesCtrCryptoCodec.class.getName());
|
|
|
codec = CryptoCodec.getInstance(conf);
|
|
|
assertNotNull("Unable to instantiate codec " +
|
|
@@ -42,4 +46,28 @@ public class TestCryptoStreamsWithOpensslAesCtrCryptoCodec
|
|
|
assertEquals(OpensslAesCtrCryptoCodec.class.getCanonicalName(),
|
|
|
codec.getClass().getCanonicalName());
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCodecClosesRandom() throws Exception {
|
|
|
+ GenericTestUtils.assumeInNativeProfile();
|
|
|
+ Configuration conf = new Configuration();
|
|
|
+ conf.set(HADOOP_SECURITY_CRYPTO_CODEC_CLASSES_AES_CTR_NOPADDING_KEY,
|
|
|
+ OpensslAesCtrCryptoCodec.class.getName());
|
|
|
+ conf.set(
|
|
|
+ CommonConfigurationKeysPublic.HADOOP_SECURITY_SECURE_RANDOM_IMPL_KEY,
|
|
|
+ OsSecureRandom.class.getName());
|
|
|
+ CryptoCodec codecWithRandom = CryptoCodec.getInstance(conf);
|
|
|
+ assertNotNull(
|
|
|
+ "Unable to instantiate codec " + OpensslAesCtrCryptoCodec.class
|
|
|
+ .getName() + ", is the required " + "version of OpenSSL installed?",
|
|
|
+ codecWithRandom);
|
|
|
+ OsSecureRandom random =
|
|
|
+ (OsSecureRandom) Whitebox.getInternalState(codecWithRandom, "random");
|
|
|
+ // trigger the OsSecureRandom to create an internal FileInputStream
|
|
|
+ random.nextBytes(new byte[10]);
|
|
|
+ assertNotNull(Whitebox.getInternalState(random, "stream"));
|
|
|
+ // verify closing the codec closes the codec's random's stream.
|
|
|
+ codecWithRandom.close();
|
|
|
+ assertNull(Whitebox.getInternalState(random, "stream"));
|
|
|
+ }
|
|
|
}
|