package org.springframework.security.crypto.encrypt;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class EncryptorsTests {

	@Test
	public void stronger() throws Exception {
		BytesEncryptor encryptor = Encryptors.stronger("password", "5c0744940b5c369b");
		byte[] result = encryptor.encrypt("text".getBytes("UTF-8"));
		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()))));
	}

	@Test
	public void standard() throws Exception {
		BytesEncryptor encryptor = Encryptors.standard("password", "5c0744940b5c369b");
		byte[] result = encryptor.encrypt("text".getBytes("UTF-8"));
		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()))));
	}

	@Test
	public void preferred() {
		TextEncryptor encryptor = Encryptors.delux("password", "5c0744940b5c369b");
		String result = encryptor.encrypt("text");
		assertNotNull(result);
		assertFalse(result.equals("text"));
		assertEquals("text", encryptor.decrypt(result));
		assertFalse(result.equals(encryptor.encrypt("text")));
	}

	@Test
	public void text() {
		TextEncryptor encryptor = Encryptors.text("password", "5c0744940b5c369b");
		String result = encryptor.encrypt("text");
		assertNotNull(result);
		assertFalse(result.equals("text"));
		assertEquals("text", encryptor.decrypt(result));
		assertFalse(result.equals(encryptor.encrypt("text")));
	}

	@Test
	public void queryableText() {
		TextEncryptor encryptor = Encryptors
				.queryableText("password", "5c0744940b5c369b");
		String result = encryptor.encrypt("text");
		assertNotNull(result);
		assertFalse(result.equals("text"));
		assertEquals("text", encryptor.decrypt(result));
		assertTrue(result.equals(encryptor.encrypt("text")));
	}

	@Test
	public void noOpText() {
		TextEncryptor encryptor = Encryptors.noOpText();
		assertEquals("text", encryptor.encrypt("text"));
		assertEquals("text", encryptor.decrypt("text"));
	}
}

@@ -226,16 +226,118 @@
 tTrue;%0A%0A
+import java.security.GeneralSecurityException;%0A%0Aimport javax.crypto.Cipher;%0A%0Aimport org.junit.Assume;%0A
 import o
@@ -351,16 +351,16 @@
 t.Test;%0A
-
 %0Apublic 
@@ -426,32 +426,114 @@
 ows Exception %7B%0A
+%09%09Assume.assumeTrue(%22GCM must be available for this test%22, isAesGcmAvailable());%0A%0A
 %09%09BytesEncryptor
@@ -1364,24 +1364,106 @@
 eferred() %7B%0A
+%09%09Assume.assumeTrue(%22GCM must be available for this test%22, isAesGcmAvailable());%0A%0A
 %09%09TextEncryp
@@ -2549,32 +2549,32 @@
 crypt(%22text%22));%0A
-
 %09%09assertEquals(%22
@@ -2610,11 +2610,184 @@
 t%22));%0A%09%7D
+%0A%0A%09private boolean isAesGcmAvailable() %7B%0A%09%09try %7B%0A%09%09%09Cipher.getInstance(%22AES/GCM/NoPadding%22);%0A%09%09%09return true;%0A%09%09%7D catch (GeneralSecurityException e) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Add JUnit Assume to GCM encryption tests