From 704b1148425a9a2089fd903f7017f684fd7e99d3 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Mon, 13 Jul 2015 16:22:18 -0500 Subject: [PATCH] SEC-3002: Add JUnit Assume to GCM encryption tests Not all JDKs have GCM installed on them. --- .../crypto/encrypt/EncryptorsTests.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/crypto/src/test/java/org/springframework/security/crypto/encrypt/EncryptorsTests.java b/crypto/src/test/java/org/springframework/security/crypto/encrypt/EncryptorsTests.java index fc97300fbe..a179b8be96 100644 --- a/crypto/src/test/java/org/springframework/security/crypto/encrypt/EncryptorsTests.java +++ b/crypto/src/test/java/org/springframework/security/crypto/encrypt/EncryptorsTests.java @@ -5,12 +5,19 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.security.GeneralSecurityException; + +import javax.crypto.Cipher; + +import org.junit.Assume; import org.junit.Test; public class EncryptorsTests { @Test public void stronger() throws Exception { + Assume.assumeTrue("GCM must be available for this test", isAesGcmAvailable()); + BytesEncryptor encryptor = Encryptors.stronger("password", "5c0744940b5c369b"); byte[] result = encryptor.encrypt("text".getBytes("UTF-8")); assertNotNull(result); @@ -27,11 +34,14 @@ public class EncryptorsTests { assertNotNull(result); assertFalse(new String(result).equals("text")); assertEquals("text", new String(encryptor.decrypt(result))); - assertFalse(new String(result).equals(new String(encryptor.encrypt("text".getBytes())))); + assertFalse(new String(result).equals(new String(encryptor.encrypt("text" + .getBytes())))); } @Test public void preferred() { + Assume.assumeTrue("GCM must be available for this test", isAesGcmAvailable()); + TextEncryptor encryptor = Encryptors.delux("password", "5c0744940b5c369b"); String result = encryptor.encrypt("text"); assertNotNull(result); @@ -52,7 +62,8 @@ public class EncryptorsTests { @Test public void queryableText() { - TextEncryptor encryptor = Encryptors.queryableText("password", "5c0744940b5c369b"); + TextEncryptor encryptor = Encryptors + .queryableText("password", "5c0744940b5c369b"); String result = encryptor.encrypt("text"); assertNotNull(result); assertFalse(result.equals("text")); @@ -66,4 +77,13 @@ public class EncryptorsTests { assertEquals("text", encryptor.encrypt("text")); assertEquals("text", encryptor.decrypt("text")); } + + private boolean isAesGcmAvailable() { + try { + Cipher.getInstance("AES/GCM/NoPadding"); + return true; + } catch (GeneralSecurityException e) { + return false; + } + } }