You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by gk...@apache.org on 2020/07/23 11:58:44 UTC
svn commit: r1880199 [2/2] - in /turbine/fulcrum/trunk/yaafi-crypto: ./
src/changes/ src/filters/ src/java/org/apache/fulcrum/jce/crypto/
src/java/org/apache/fulcrum/jce/crypto/algo/
src/java/org/apache/fulcrum/jce/crypto/cli/ src/java/org/apache/fulcr...
Modified: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java?rev=1880199&r1=1880198&r2=1880199&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java Thu Jul 23 11:58:44 2020
@@ -1,5 +1,10 @@
package org.apache.fulcrum.jce.crypto;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -21,12 +26,14 @@ package org.apache.fulcrum.jce.crypto;
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.IOException;
+import java.security.GeneralSecurityException;
-import org.apache.fulcrum.jce.crypto.extended.CryptoUtilJ8ParameterizedTest;
+import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-
-import junit.framework.TestCase;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
/**
* Test suite for crypto functionality
@@ -34,7 +41,7 @@ import junit.framework.TestCase;
* @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
*/
-public class CryptoUtilTest extends TestCase {
+public class CryptoUtilTest {
/** the password to be used */
private String password;
@@ -48,12 +55,8 @@ public class CryptoUtilTest extends Test
/**
* Constructor
- *
- * @param name the name of the test case
*/
- public CryptoUtilTest(String name) {
- super(name);
-
+ public CryptoUtilTest() {
this.password = "mysecret";
this.testDataDirectory = new File("./src/test/data");
this.tempDataDirectory = new File("./target/temp");
@@ -61,15 +64,16 @@ public class CryptoUtilTest extends Test
}
/**
- * @see junit.framework.TestCase#setUp() byte[] salt, int count, String
- * algorithm, String providerName )
*
* @throws Exception Generic exception
*/
- protected void setUp() throws Exception {
+ @BeforeAll
+ protected static void setUp() throws Exception {
CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(CryptoParameters.Salt(), CryptoParameters.COUNT);
CryptoStreamFactoryImpl.setInstance(factory);
+
+
}
/**
@@ -94,159 +98,205 @@ public class CryptoUtilTest extends Test
}
/** Encrypt a text file
- * @throws Exception Generic exception
*/
- public void testTextEncryption() throws Exception {
+ @Test
+ public void testTextEncryption() {
File sourceFile = new File(this.getTestDataDirectory(), "plain.txt");
File targetFile = new File(this.getTempDataDirectory(), "plain.enc.txt");
- CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException e) {
+ fail(e);
+ } catch (IOException e) {
+ fail(e);
+ }
}
/** Decrypt a text file
- * @throws Exception Generic exception
*/
- public void testTextDecryption() throws Exception {
+ @Test
+ public void testTextDecryption() {
testTextEncryption();
File sourceFile = new File(this.getTempDataDirectory(), "plain.enc.txt");
File targetFile = new File(this.getTempDataDirectory(), "plain.dec.txt");
- CryptoUtil.getInstance().decrypt(sourceFile, targetFile.getAbsolutePath(), this.getPassword());
+ try {
+ CryptoUtil.getInstance().decrypt(sourceFile, targetFile.getAbsolutePath(), this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/** Encrypt an empty text file
- *
- * @throws Exception Generic exception
*/
- public void testEmptyTextEncryption() throws Exception {
+ @Test
+ public void testEmptyTextEncryption() {
File sourceFile = new File(this.getTestDataDirectory(), "empty.txt");
File targetFile = new File(this.getTempDataDirectory(), "empty.enc.txt");
- CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/** Decrypt a text file
- * @throws Exception Generic exception
*/
- public void testEmptyTextDecryption() throws Exception {
+ @Test
+ public void testEmptyTextDecryption() {
testEmptyTextEncryption();
File sourceFile = new File(this.getTempDataDirectory(), "empty.enc.txt");
File targetFile = new File(this.getTempDataDirectory(), "empty.dec.txt");
- CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/** Encrypt a PDF file
- *
- * @throws Exception Generic exception
*/
- public void testPdfEncryption() throws Exception {
+ @Test
+ public void testPdfEncryption() {
File sourceFile = new File(this.getTestDataDirectory(), "plain.pdf");
File targetFile = new File(this.getTempDataDirectory(), "plain.enc.pdf");
- CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/** Decrypt a PDF file
- *
- * @throws Exception Generic exception
*/
- public void testPdfDecryption() throws Exception {
+ @Test
+ public void testPdfDecryption() {
testPdfEncryption();
File sourceFile = new File(this.getTempDataDirectory(), "plain.enc.pdf");
File targetFile = new File(this.getTempDataDirectory(), "plain.dec.pdf");
- CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/** Encrypt a ZIP file
- *
- * @throws Exception Generic exception
*/
- public void testZipEncryption() throws Exception {
+ @Test
+ public void testZipEncryption() {
File sourceFile = new File(this.getTestDataDirectory(), "plain.zip");
File targetFile = new File(this.getTempDataDirectory(), "plain.enc.zip");
- CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/** Decrypt a ZIP file
- *
- * @throws Exception Generic exception
*/
- public void testZipDecryption() throws Exception {
+ @Test
+ public void testZipDecryption() {
testZipEncryption();
File sourceFile = new File(this.getTempDataDirectory(), "plain.enc.zip");
File targetFile = new File(this.getTempDataDirectory(), "plain.dec.zip");
- CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/** Encrypt a UTF-16 XML file
- *
- * @throws Exception Generic exception
*/
- public void testXmlUTF16Encryption() throws Exception {
+ @Test
+ public void testXmlUTF16Encryption() {
File sourceFile = new File(this.getTestDataDirectory(), "plain-utf16.xml");
File targetFile = new File(this.getTempDataDirectory(), "plain-utf16.enc.xml");
- CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/**
* Decrypt a UTF-16 XML file
- *
- * @throws Exception Generic exception
*/
- public void testXMLUTF16Decryption() throws Exception {
+ @Test
+ public void testXMLUTF16Decryption() {
testXmlUTF16Encryption();
File sourceFile = new File(this.getTempDataDirectory(), "plain-utf16.enc.xml");
File targetFile = new File(this.getTempDataDirectory(), "plain-utf16.dec.xml");
- CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/**
* Encrypt a UTF-8 XML file
- *
- * @throws Exception Generic exception
*/
- public void testXmlUTF8Encryption() throws Exception {
+ @Test
+ public void testXmlUTF8Encryption() {
File sourceFile = new File(this.getTestDataDirectory(), "plain-utf8.xml");
File targetFile = new File(this.getTempDataDirectory(), "plain-utf8.enc.xml");
- CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/**
* Decrypt a UTF-8 XML file
- *
- * @throws Exception Generic exception
*/
- public void testXMLUTF8Decryption() throws Exception {
+ @Test
+ public void testXMLUTF8Decryption() {
testXmlUTF8Encryption();
File sourceFile = new File(this.getTempDataDirectory(), "plain-utf8.enc.xml");
File targetFile = new File(this.getTempDataDirectory(), "plain-utf8.dec.xml");
- CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/**
* Encrypt a ISO-8859-1 XML file
- *
- * @throws Exception Generic exception
*/
- public void testXmlISO88591Encryption() throws Exception {
+ @Test
+ public void testXmlISO88591Encryption() {
File sourceFile = new File(this.getTestDataDirectory(), "plain-iso-8859-1.xml");
File targetFile = new File(this.getTempDataDirectory(), "plain-iso-8859-1.enc.xml");
- CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/**
* Decrypt a ISO-8859-1 XML file
- *
- * @throws Exception Generic exception
*/
- public void testXmlISO88591Decryption() throws Exception {
+ @Test
+ public void testXmlISO88591Decryption() {
testXmlISO88591Encryption();
File sourceFile = new File(this.getTempDataDirectory(), "plain-iso-8859-1.enc.xml");
File targetFile = new File(this.getTempDataDirectory(), "plain-iso-8859-1.dec.xml");
- CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ try {
+ CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
}
/** Test encryption and decryption of Strings
- *
- * @throws Exception Generic exception
*/
- public void testStringEncryption() throws Exception {
+ @Test
+ public void testStringEncryption() {
char[] testVector = new char[513];
for (int i = 0; i < testVector.length; i++) {
@@ -254,24 +304,70 @@ public class CryptoUtilTest extends Test
}
String source = new String(testVector);
- String cipherText = CryptoUtil.getInstance().encryptString(source, this.getPassword());
- String plainText = CryptoUtil.getInstance().decryptString(cipherText, this.getPassword());
- assertEquals(source, plainText);
+ try {
+ String cipherText = CryptoUtil.getInstance().encryptString(source, this.getPassword());
+ String plainText = CryptoUtil.getInstance().decryptString(cipherText, this.getPassword());
+ assertEquals(source, plainText);
+ } catch (GeneralSecurityException | IOException e) {
+ fail(e);
+ }
+
+ }
+
+ @Test
+ public void testStringEncryptionWithType() {
+ CryptoUtil cu = CryptoUtil.getInstance();
+ char[] testVector = new char[513];
+
+ for (int i = 0; i < testVector.length; i++) {
+ testVector[i] = (char) i;
+ }
+
+ String source = new String(testVector);
+ String cipherText = null;
+ String plainText = null;
+ try {
+ log.info("Test without clearTextHeader");
+ cipherText = cu.encryptString(source, this.getPassword());
+ log.trace(cipherText);
+ plainText = cu.decryptString(cipherText, this.getPassword());
+ assertEquals(source, plainText, source + " is not equal with " + plainText);
+
+ log.info(String.format("Test with clearTextHeader %s in encrypted string.",
+ CryptoParametersJ8.CLEAR_CODE_DEFAULT));
+ String cipherText2 = cu.encryptStringWithClearCode(source, this.getPassword());
+ log.trace(cipherText2);
+ // old style
+ assertTrue(cipherText2.startsWith(CryptoParametersJ8.CLEAR_CODE_DEFAULT),
+ String.format("%s does not start with '%s'", cipherText2, CryptoParametersJ8.CLEAR_CODE_DEFAULT));
+ String plainText2 = cu.decryptStringWithClearCode(cipherText2, this.getPassword());
+ assertEquals(source, plainText2, String.format("%s is not equal with %s", source, plainText));
+
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
}
/** Test encryption and decryption of Strings
- * @throws Exception Generic exception
*/
- public void testStringHandling() throws Exception {
+ @Test
+ public void testStringHandling() {
String source = "Nobody knows the toubles I have seen ...";
- String cipherText = CryptoUtil.getInstance().encryptString(source, this.getPassword());
- String plainText = CryptoUtil.getInstance().decryptString(cipherText, this.getPassword());
+ try {
+ String cipherText = CryptoUtil.getInstance().encryptString(source, this.getPassword());
+ String plainText = CryptoUtil.getInstance().decryptString(cipherText, this.getPassword());
assertEquals(source, plainText);
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
}
/** Test encryption and decryption of binary data
* @throws Exception Generic exception
*/
+ @Test
public void testBinaryHandling() throws Exception {
byte[] source = new byte[256];
byte[] result = null;
@@ -298,6 +394,7 @@ public class CryptoUtilTest extends Test
/** Test creating a password
* @throws Exception Generic exception
*/
+ @Test
public void testPasswordFactory() throws Exception {
char[] result = null;
result = PasswordFactory.getInstance().create();
@@ -305,12 +402,12 @@ public class CryptoUtilTest extends Test
result = PasswordFactory.getInstance().create(this.getPassword());
log.info(new String(result));
assertNotNull(result);
- return;
}
/** Test the hex converter
* @throws Exception Generic exception
*/
+ @Test
public void testHexConverter() throws Exception {
String source = "DceuATAABWSaVTSIK";
String hexString = HexConverter.toString(source.getBytes());
@@ -321,6 +418,7 @@ public class CryptoUtilTest extends Test
/** Test encryption and decryption of Strings
* @throws Exception Generic exception
*/
+ @Test
public void testPasswordEncryption() throws Exception {
char[] password = "57cb-4a23-d838-45222".toCharArray();
String source = "e02c-3b76-ff1e-5d9a1";
Added: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8ExplicitParamsTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8ExplicitParamsTest.java?rev=1880199&view=auto
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8ExplicitParamsTest.java (added)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8ExplicitParamsTest.java Thu Jul 23 11:58:44 2020
@@ -0,0 +1,371 @@
+package org.apache.fulcrum.jce.crypto.extended;
+
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.GeneralSecurityException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.fulcrum.jce.crypto.PasswordFactory;
+import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
+import org.apache.fulcrum.jce.junit5.extension.SupportedTypeArguments;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+/*
+ * 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.
+ */
+
+
+/**
+ * Test suite for crypto functionality
+ *
+ * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
+ */
+public class CryptoUtilJ8ExplicitParamsTest {
+ /** the password to be used */
+ private String password;
+
+ /** the test data directory */
+ private File testDataDirectory;
+
+ /** the temp data director */
+ private File tempDataDirectory;
+
+
+ private static byte[] SALT = generateSalt();
+
+ private static int COUNT = 12345;
+
+ private static List<CryptoUtilJ8> cryptoUtilJ8s = new ArrayList<>();
+
+ private static Logger log = LogManager.getLogger(CryptoUtilJ8ExplicitParamsTest.class);
+
+
+ protected static byte[] generateSalt() {
+ SecureRandom random;
+ try {
+ random = SecureRandom.getInstanceStrong();
+ byte[] salt = new byte[ 16 ];
+ random.nextBytes(salt);
+ return salt;
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ /**
+ * Constructor
+ */
+ public CryptoUtilJ8ExplicitParamsTest() {
+
+ this.password = "mysecret";
+ this.testDataDirectory = new File("./src/test/data");
+ this.tempDataDirectory = new File("./target/temp");
+ this.tempDataDirectory.mkdirs();
+ }
+
+
+ @BeforeAll
+ public static void setUp() throws Exception {
+ cryptoUtilJ8s.clear();
+ SupportedTypeArguments.init();
+ for (TYPES type : CryptoParametersJ8.TYPES.values()) {
+ if (SupportedTypeArguments.SUPPORTED_TYPES.contains(type.toString())) {
+ cryptoUtilJ8s.add(CryptoUtilJ8.getInstance(type, SALT, COUNT));
+ }
+ }
+ for (CryptoUtilJ8 cryptoUtilJ8 : cryptoUtilJ8s) {
+ log.debug("registered {}", cryptoUtilJ8.getClass().getSimpleName() );
+ CryptoStreamFactoryJ8Template crt = ((CryptoStreamFactoryJ8Template)cryptoUtilJ8.getCryptoStreamFactory());
+ log.debug(String.format("created default crypto factory instance %s for algo %s with salt length: %s",
+ crt.getClass().getSimpleName(),
+ crt.getAlgorithm(), crt.getSalt().length));
+ }
+ }
+
+ @AfterAll
+ public static void destroy() {
+ cryptoUtilJ8s.clear();
+ }
+
+
+ /**
+ * @return Returns the password.
+ */
+ protected char[] getPassword() {
+ return password.toCharArray();
+ }
+
+ /**
+ * @return Returns the tempDataDirectory.
+ */
+ protected File getTempDataDirectory() {
+ return tempDataDirectory;
+ }
+
+ /**
+ * @return Returns the testDataDirectory.
+ */
+ protected File getTestDataDirectory() {
+ return testDataDirectory;
+ }
+
+ /** Encrypt a text file
+ *
+ */
+ @Test
+ public void testTextEncryption() {
+
+ File sourceFile = new File(this.getTestDataDirectory(), "plain.txt");
+ File targetFile = new File(this.getTempDataDirectory(), "plain.j8.enc.txt");
+
+ cryptoUtilJ8s.forEach(cuj8 -> {
+ try {
+ cuj8.encrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException e) {
+ e.printStackTrace();
+ fail();
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+ } );
+ }
+
+ /** Decrypt a text file
+ */
+ @Test
+ public void testTextDecryption() {
+ cryptoUtilJ8s.forEach(cuj8 -> {
+ log.info("start en-/decrypting with {}",cuj8);
+ try {
+ File sourceFile = new File(this.getTestDataDirectory(), "plain.txt");
+ File targetFile = new File(this.getTempDataDirectory(), "plain.j8.enc.txt");
+ cuj8.encrypt(sourceFile, targetFile, this.getPassword());
+
+ File sourceFile2 = new File(this.getTempDataDirectory(), "plain.j8.enc.txt");;
+ File targetFile2 = new File(this.getTempDataDirectory(), "plain.j8.dec.txt");
+ cuj8.decrypt(sourceFile2, targetFile2.getAbsolutePath(), this.getPassword());
+ assertEquals(
+ new String(Files.readAllBytes( Paths.get(sourceFile.toURI())) ),
+ new String(Files.readAllBytes( Paths.get(targetFile2.toURI())) )
+ );
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+ });
+ }
+
+ /** Encrypt a PDF file
+ *
+ */
+ @Test
+ public void testPdfEncryption() {
+ File sourceFile = new File(this.getTestDataDirectory(), "plain.pdf");
+ File targetFile = new File(this.getTempDataDirectory(), "plain.j8.enc.pdf");
+ cryptoUtilJ8s.forEach(cuj8 -> {
+ try {
+ cuj8.encrypt(sourceFile, targetFile, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+ });
+ }
+
+ /** Decrypt a PDF file
+ *
+ */
+ @Test
+ public void testPdfDecryption() {
+ //testPdfEncryption();
+ cryptoUtilJ8s.forEach(cuj8 -> {
+ try {
+ File sourceFile = new File(this.getTestDataDirectory(), "plain.pdf");
+ File targetFile = new File(this.getTempDataDirectory(), "plain.j8.enc.pdf");
+ cuj8.encrypt(sourceFile, targetFile, this.getPassword());
+
+ File sourceFile2 = new File(this.getTempDataDirectory(), "plain.j8.enc.pdf");
+ File targetFile2 = new File(this.getTempDataDirectory(), "plain.j8.dec.pdf");
+ cuj8.decrypt(sourceFile2, targetFile2, this.getPassword());
+
+ assertEquals(
+ new String(Files.readAllBytes( Paths.get(sourceFile.toURI())) ),
+ new String(Files.readAllBytes( Paths.get(targetFile2.toURI())) )
+ );
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+ });
+
+ }
+
+ /** Test encryption and decryption of Strings
+ *
+ */
+ @Test
+ public void testStringEncryption() {
+ char[] testVector = new char[513];
+
+ for (int i = 0; i < testVector.length; i++) {
+ testVector[i] = (char) i;
+ }
+
+ String source = new String(testVector);
+ cryptoUtilJ8s.forEach(cuj8 -> {
+ String cipherText;
+ String plainText;
+ try {
+ cipherText = cuj8.encryptString(source, this.getPassword());
+ plainText = cuj8.decryptString(cipherText, this.getPassword());
+ assertEquals(source, plainText, source +" is not equal with " + plainText);
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+
+ });
+
+
+ }
+
+ /** Test encryption and decryption of Strings
+ */
+ @Test
+ public void testStringHandling() {
+ String source = "Nobody knows the toubles I have seen ...";
+ cryptoUtilJ8s.forEach(cuj8 -> {
+ String cipherText;
+ try {
+ cipherText = cuj8.encryptString(source, this.getPassword());
+ String plainText = cuj8.decryptString(cipherText, this.getPassword());
+ assertEquals(source, plainText);
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+
+ });
+
+ }
+
+ /**
+ * Test creating a password
+ * @throws Exception Generic exception
+ */
+ @Test
+ public void testPasswordFactory() throws Exception {
+ char[] result = null;
+ result = PasswordFactory.getInstance("SHA-256").create();
+ log.debug("random pw: {}", new String(result));
+ result = PasswordFactory.getInstance("SHA-256",10_000).create(this.getPassword());
+ log.debug("password pw with seed: {}", new String(result));
+ assertNotNull(result);
+ return;
+ }
+
+ /** Test encryption and decryption of binary data
+ * @throws Exception Generic exception
+ */
+ @Test
+ public void testBinaryHandling() throws Exception {
+
+ cryptoUtilJ8s.forEach(cuj8 -> {
+ byte[] source = new byte[256];
+ byte[] result = null;
+
+ for (int i = 0; i < source.length; i++) {
+ source[i] = (byte) i;
+ }
+
+ ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
+ ByteArrayOutputStream plainText = new ByteArrayOutputStream();
+ try {
+ cuj8.encrypt(source, cipherText, this.getPassword());
+ cuj8.decrypt(cipherText, plainText, this.getPassword());
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+ result = plainText.toByteArray();
+
+ for (int i = 0; i < source.length; i++) {
+ if (source[i] != result[i]) {
+ fail("Binary data are different at position " + i);
+ }
+ }
+ });
+
+
+
+ }
+
+ /**
+ * Test encryption and decryption of Strings
+ */
+ @Test
+ public void testStringWithPasswordEncryption() {
+ char[] password = "57cb-4a23-d838-45222".toCharArray();
+ String source = "e02c-3b76-ff1e-5d9a1";
+
+ cryptoUtilJ8s.forEach(cuj8 -> {
+ String cipherText = null;
+ try {
+ cipherText = cuj8.encryptString(source, password);
+ log.debug(cipherText);// about 128
+
+ log.debug("registered {}: {}", cuj8.getClass().getSimpleName());
+ CryptoStreamFactoryJ8Template crt = ((CryptoStreamFactoryJ8Template)cuj8.getCryptoStreamFactory());
+ log.debug(String.format("created default crypto factory instance %s for algo %s with salt (optional): %s",
+ crt.getClass().getSimpleName(),
+ crt.getAlgorithm(), crt.getSalt()));
+
+ log.debug("length for {} is: {}", crt.getType(), cipherText.length());// about 128
+ if (crt.getType() == TYPES.PBE) {
+ assertEquals(128, cipherText.length()); // 128bytes + 10 bytes for cleartext
+ }
+ CryptoStreamFactoryJ8Template.resetInstances();
+ String plainText = cuj8.decryptString(cipherText, password);
+ assertEquals(source, plainText);
+ } catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ fail();
+ }
+
+ });
+
+ }
+
+}
Modified: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8ParameterizedTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8ParameterizedTest.java?rev=1880199&r1=1880198&r2=1880199&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8ParameterizedTest.java (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8ParameterizedTest.java Thu Jul 23 11:58:44 2020
@@ -16,18 +16,25 @@ import java.util.List;
import org.apache.fulcrum.jce.crypto.PasswordFactory;
import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
+import org.apache.fulcrum.jce.junit5.extension.SupportedTypeArguments;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.EnumSource;
+import org.junit.jupiter.params.provider.ArgumentsSource;
/**
* Test suite for crypto functionality
+ *
+ * Could still not access arguments of parameterized tests in lifecycle callback methods
+ *
+ * - https://github.com/junit-team/junit5/issues/944
+ * - https://github.com/junit-team/junit5/issues/1139#issuecomment-341683075
*
- * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
- */
+ * e.g. with ExtendWith(SupportedTypeArguments.class)
+ * */
public class CryptoUtilJ8ParameterizedTest {
/** the password to be used */
private String password;
@@ -46,19 +53,12 @@ public class CryptoUtilJ8ParameterizedTe
* Constructor
*/
public CryptoUtilJ8ParameterizedTest() {
-
this.password = "mysecret";
this.testDataDirectory = new File("./src/test/data");
this.tempDataDirectory = new File("./target/temp");
this.tempDataDirectory.mkdirs();
}
-// @ParameterizedTest
-// @EnumSource( TYPES.class )
-// public void setUp(TYPES type) throws Exception {
-// cryptoUtilJ8 = CryptoUtilJ8.getInstance(type); // (TYPES.PBE);
-// }
-
/**
* @return Returns the password.
*/
@@ -80,9 +80,16 @@ public class CryptoUtilJ8ParameterizedTe
return testDataDirectory;
}
- @AfterEach
+ @BeforeEach
public void setup() {
cryptoUtilJ8s.clear();
+ SupportedTypeArguments.init();
+ }
+
+
+ @AfterEach
+ public void clean() {
+ cryptoUtilJ8s.clear();
}
/**
@@ -94,7 +101,7 @@ public class CryptoUtilJ8ParameterizedTe
*
*/
@ParameterizedTest
- @EnumSource(TYPES.class)
+ @ArgumentsSource(SupportedTypeArguments.class)
public void testTextEncryption(TYPES type) {
cryptoUtilJ8s.add(CryptoUtilJ8.getInstance(type));
@@ -103,7 +110,11 @@ public class CryptoUtilJ8ParameterizedTe
cryptoUtilJ8s.forEach(cuj8 -> {
try {
- log.info("checking " + cuj8.getType());
+ log.debug("registered {}: {}", cuj8.getClass().getSimpleName());
+ CryptoStreamFactoryJ8Template crt = ((CryptoStreamFactoryJ8Template)cuj8.getCryptoStreamFactory());
+ log.debug(String.format("created default crypto factory instance %s for algo %s with salt (optional): %s",
+ crt.getClass().getSimpleName(),
+ crt.getAlgorithm(), crt.getSalt()));
cuj8.encrypt(sourceFile, targetFile, this.getPassword());
} catch (GeneralSecurityException e) {
e.printStackTrace();
@@ -122,11 +133,15 @@ public class CryptoUtilJ8ParameterizedTe
*
*/
@ParameterizedTest
- @EnumSource(TYPES.class)
+ @ArgumentsSource(SupportedTypeArguments.class)
public void testTextDecryption(TYPES type) {
cryptoUtilJ8s.add(CryptoUtilJ8.getInstance(type));
cryptoUtilJ8s.forEach(cuj8 -> {
- log.info("checking " + cuj8.getType());
+ log.debug("registered {}: {}", cuj8.getClass().getSimpleName());
+ CryptoStreamFactoryJ8Template crt = ((CryptoStreamFactoryJ8Template)cuj8.getCryptoStreamFactory());
+ log.debug(String.format("created default crypto factory instance %s for algo %s with salt length: %s",
+ crt.getClass().getSimpleName(),
+ crt.getAlgorithm(), crt.getSalt().length));
try {
File sourceFile = new File(this.getTestDataDirectory(), "plain.txt");
File targetFile = new File(this.getTempDataDirectory(), "plain.j8.enc.txt");
@@ -153,7 +168,7 @@ public class CryptoUtilJ8ParameterizedTe
*
*/
@ParameterizedTest
- @EnumSource(TYPES.class)
+ @ArgumentsSource(SupportedTypeArguments.class)
public void testPdfEncryption(TYPES type) {
cryptoUtilJ8s.add(CryptoUtilJ8.getInstance(type));
File sourceFile = new File(this.getTestDataDirectory(), "plain.pdf");
@@ -175,7 +190,7 @@ public class CryptoUtilJ8ParameterizedTe
*
*/
@ParameterizedTest
- @EnumSource(TYPES.class)
+ @ArgumentsSource(SupportedTypeArguments.class)
public void testPdfDecryption(TYPES type) {
cryptoUtilJ8s.add(CryptoUtilJ8.getInstance(type));
// testPdfEncryption();
@@ -206,9 +221,15 @@ public class CryptoUtilJ8ParameterizedTe
*
*/
@ParameterizedTest
- @EnumSource(TYPES.class)
+ @ArgumentsSource(SupportedTypeArguments.class)
public void testStringEncryption(TYPES type) {
CryptoUtilJ8 cuj8 = CryptoUtilJ8.getInstance(type);
+
+ log.debug("registered {} and called for {}", cuj8.getClass().getSimpleName(), type);
+ CryptoStreamFactoryJ8Template crt = ((CryptoStreamFactoryJ8Template)cuj8.getCryptoStreamFactory());
+ log.debug(String.format("created default crypto factory instance %s for algo %s with salt length: %s",
+ crt.getClass().getSimpleName(),
+ crt.getAlgorithm(), crt.getSalt().length));
char[] testVector = new char[513];
for (int i = 0; i < testVector.length; i++) {
@@ -219,18 +240,20 @@ public class CryptoUtilJ8ParameterizedTe
String cipherText = null;
String plainText = null;
try {
- log.info("Test without clearTextHeader");
+ log.info("Test without clearTextHeader in type {}", type);
cipherText = cuj8.encryptString(source, this.getPassword());
- // log.debug(cipherText);
+ log.trace(cipherText);
plainText = cuj8.decryptString(cipherText, this.getPassword());
assertEquals(source, plainText, source + " is not equal with " + plainText);
+
+ String clearCode = type.equals(TYPES.PBE)? CryptoParametersJ8.TYPES.PBE.getClearCode():
+ CryptoParametersJ8.TYPES.GCM.getClearCode() ;
- log.info(String.format("Test with clearTextHeader %s in encrypted string.",
- CryptoParametersJ8.CLEAR_CODE_J8));
+ log.info(String.format("Test with clearTextHeader %s in encrypted string.",clearCode) );
String cipherText2 = cuj8.encryptStringWithClearCode(source, this.getPassword());
- // log.debug(cipherText2);
- assertTrue(cipherText2.startsWith(CryptoParametersJ8.CLEAR_CODE_J8),
- String.format("%s does not start with '%s'", cipherText2, CryptoParametersJ8.CLEAR_CODE_J8));
+ log.trace(cipherText2);
+ assertTrue(cipherText2.startsWith(clearCode),
+ String.format("%s does not start with '%s'", cipherText2, clearCode));
String plainText2 = cuj8.decryptStringWithClearCode(cipherText2, this.getPassword());
assertEquals(source, plainText2, String.format("%s is not equal with %s", source, plainText));
@@ -247,7 +270,7 @@ public class CryptoUtilJ8ParameterizedTe
*
*/
@ParameterizedTest
- @EnumSource(TYPES.class)
+ @ArgumentsSource(SupportedTypeArguments.class)
public void testStringHandling(TYPES type) {
cryptoUtilJ8s.add(CryptoUtilJ8.getInstance(type));
String source = "Nobody knows the toubles I have seen ...";
@@ -292,7 +315,7 @@ public class CryptoUtilJ8ParameterizedTe
* @param type the type to be tested based on {@link TYPES}
*/
@ParameterizedTest
- @EnumSource(TYPES.class)
+ @ArgumentsSource(SupportedTypeArguments.class)
public void testBinaryHandling(TYPES type) throws Exception {
cryptoUtilJ8s.add(CryptoUtilJ8.getInstance(type));
cryptoUtilJ8s.forEach(cuj8 -> {
@@ -332,23 +355,28 @@ public class CryptoUtilJ8ParameterizedTe
*
*/
@ParameterizedTest
- @EnumSource(TYPES.class)
+ @ArgumentsSource(SupportedTypeArguments.class)
public void testStringWithPasswordEncryption(TYPES type) {
char[] password = "57cb-4a23-d838-45222".toCharArray();
String source = "e02c-3b76-ff1e-5d9a1";
CryptoUtilJ8 cuj8 = CryptoUtilJ8.getInstance(type);
- log.debug("checking type " + cuj8.getType());
+ log.debug("registered {}: {}", cuj8.getClass().getSimpleName());
+ CryptoStreamFactoryJ8Template crt = ((CryptoStreamFactoryJ8Template)cuj8.getCryptoStreamFactory());
+ log.debug(String.format("created default crypto factory instance %s for algo %s with salt (optional): %s",
+ crt.getClass().getSimpleName(),
+ crt.getAlgorithm(), crt.getSalt()));
String cipherText = null;
try {
cipherText = cuj8.encryptString(source, password);
log.info(cipherText);// about 128
- log.info(String.format("length for %s is %d", cuj8.getType(), cipherText.length()));// about 128
- if (cuj8.type == TYPES.PBE) {
- assertEquals(128, cipherText.length()); // 128bytes + 10 bytes for cleartext
- }
- if (cuj8.type == TYPES.GCM) {
- assertEquals(128, cipherText.length());
- }
+ log.info(String.format("length for %s is %d", cuj8, cipherText.length()));// about 128
+ assertEquals(128, cipherText.length());
+// if (cuj8.type == TYPES.PBE) {
+// assertEquals(128, cipherText.length()); // 128bytes + 10 bytes for cleartext
+// }
+// if (cuj8.type == TYPES.GCM) {
+// assertEquals(128, cipherText.length());
+// }
String plainText = cuj8.decryptString(cipherText, password);
assertEquals(source, plainText);
} catch (GeneralSecurityException | IOException e) {
Modified: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8Test.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8Test.java?rev=1880199&r1=1880198&r2=1880199&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8Test.java (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/CryptoUtilJ8Test.java Thu Jul 23 11:58:44 2020
@@ -11,6 +11,8 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
@@ -18,6 +20,7 @@ import org.apache.fulcrum.jce.crypto.Pas
import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8;
import org.apache.fulcrum.jce.crypto.extended.CryptoStreamFactoryJ8Template;
import org.apache.fulcrum.jce.crypto.extended.CryptoUtilJ8;
+import org.apache.fulcrum.jce.junit5.extension.SupportedTypeArguments;
import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -25,6 +28,24 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+/*
+ * 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.
+ */
/**
* Test suite for crypto functionality
@@ -60,17 +81,21 @@ public class CryptoUtilJ8Test {
@BeforeAll
public static void setUp() throws Exception {
cryptoUtilJ8s.clear();
+ SupportedTypeArguments.init();
for (TYPES type : CryptoParametersJ8.TYPES.values()) {
- cryptoUtilJ8s.add(CryptoUtilJ8.getInstance(type));
+ if (SupportedTypeArguments.SUPPORTED_TYPES.contains(type.toString())) {
+ cryptoUtilJ8s.add(CryptoUtilJ8.getInstance(type));
+ }
}
for (CryptoUtilJ8 cryptoUtilJ8 : cryptoUtilJ8s) {
- log.debug("registered {}: {}", cryptoUtilJ8.getClass().getSimpleName(), cryptoUtilJ8.getType() );
+ log.debug("registered {}: {}", cryptoUtilJ8.getClass().getSimpleName());
CryptoStreamFactoryJ8Template crt = ((CryptoStreamFactoryJ8Template)cryptoUtilJ8.getCryptoStreamFactory());
- log.debug(String.format("created default crypto factory instance %s for algo %s", crt.getClass().getSimpleName(),
- crt.getAlgorithm()));
+ log.debug(String.format("created default crypto factory instance %s for algo %s with salt length: %s",
+ crt.getClass().getSimpleName(),
+ crt.getAlgorithm(), crt.getSalt().length));
}
-
}
+
@AfterAll
public static void destroy() {
cryptoUtilJ8s.clear();
@@ -124,8 +149,13 @@ public class CryptoUtilJ8Test {
*/
@Test
public void testTextDecryption() {
- cryptoUtilJ8s.forEach(cuj8 -> {
- try {
+ cryptoUtilJ8s.forEach(cuj8 -> {
+ log.debug("registered {}: {}", cuj8.getClass().getSimpleName());
+ CryptoStreamFactoryJ8Template crt = ((CryptoStreamFactoryJ8Template)cuj8.getCryptoStreamFactory());
+ log.debug(String.format("created default crypto factory instance %s for algo %s with salt length: %s",
+ crt.getClass().getSimpleName(),
+ crt.getAlgorithm(), crt.getSalt().length));
+ try {
File sourceFile = new File(this.getTestDataDirectory(), "plain.txt");
File targetFile = new File(this.getTempDataDirectory(), "plain.j8.enc.txt");
cuj8.encrypt(sourceFile, targetFile, this.getPassword());
@@ -302,9 +332,14 @@ public class CryptoUtilJ8Test {
try {
cipherText = cuj8.encryptString(source, password);
log.debug(cipherText);// about 128
+
+ CryptoStreamFactoryJ8Template crt = ((CryptoStreamFactoryJ8Template)cuj8.getCryptoStreamFactory());
+ log.debug(String.format("created default crypto factory instance %s for algo %s and type %s with salt (optional): %s",
+ crt.getClass().getSimpleName(), crt.getType(),
+ crt.getAlgorithm(), crt.getSalt()));
- log.debug("length for {} is: {}", cuj8.getType(), cipherText.length());// about 128
- if (cuj8.type == TYPES.PBE) {
+ log.debug("length for {} is: {}", crt.getType(), cipherText.length());// about 128
+ if (crt.getType() == TYPES.PBE) {
assertEquals(128, cipherText.length()); // 128bytes + 10 bytes for cleartext
}
CryptoStreamFactoryJ8Template.resetInstances();
Modified: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java?rev=1880199&r1=1880198&r2=1880199&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java Thu Jul 23 11:58:44 2020
@@ -1,23 +1,25 @@
package org.apache.fulcrum.jce.crypto.extended;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;
-import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.fulcrum.jce.crypto.cli.CLI2;
import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
+import org.apache.fulcrum.jce.junit5.extension.SupportedTypeArguments;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
-
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/*
@@ -39,177 +41,198 @@ import org.junit.jupiter.api.Test;
* under the License.
*/
-
/**
* Test suite for crypto functionality
*
* @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
*/
-public class Main8Test
-{
- /** the password to be used */
- private String password;
-
- /**
- * Constructor
- */
- public Main8Test() {
-
- this.password = "foobar";
- ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
- builder.setStatusLevel(Level.DEBUG);
- }
-
-
- /**
- * @return Returns the password.
- */
- protected char[] getPassword()
- {
- return password.toCharArray();
- }
-
-
- /** Encrypt a string on the command line */
- @Test
- public void testStringEncryption()
- {
- String[] encryptionArgs = { "string", "enc", this.password, "mysecretpassword"};
- CLI2.main(encryptionArgs);
- String[] decryptionArgs = { "string", "dec", this.password, "ce3bf02da8a57c94b4f42c084230d1bedcd856c49a3fd23ec59835ca46a3d37ee02d470394691353478c905e7b342316d1fcc3e1b98837bf0595ef50853922df"};
- CLI2.main(decryptionArgs);
- }
- @Test
- public void testAnotherStringEncryption()
- {
- String[] encryptionArgs = { "string", "enc", this.password, "secret"};
- CLI2.main(encryptionArgs);
- String[] decryptionArgs = { "string", "dec", this.password, "8626904c9e64fddfa64add56472c4796429b0adb7c8039424adef7434be6bc255ce092011e8c560965814e806dd68117"};
- CLI2.main(decryptionArgs);
- }
- @Test
- /** Encrypt a text file on the command line */
- public void testFileEncryption1()
- {
- String[] encryptionArgs = { "file", "enc", this.password, "./src/test/data/plain.txt", "./target/main8/plain.enc.txt" };
- String[] decryptionArgs = { "file", "dec", this.password, "./target/main8/plain.enc.txt", "./target/main8/plain.dec.txt" };
- CLI2.main(encryptionArgs);
- CLI2.main(decryptionArgs);
- try {
- assertTrue(
- FileUtils.contentEquals(new File("./src/test/data/plain.txt"), new File("./target/main8/plain.dec.txt"))
- );
- } catch (IOException e) {
- fail();
- }
- }
- @Test
- /** Encrypt a text file in-place on the command line */
- public void testFileEncryption2()
- {
- String[] encryptionArgs = { "file", "enc", this.password, "./src/test/data/plain.txt", "./target/main8/plain.txt" };
- // caution decrypting into source file!
- String[] decryptionArgs = { "file", "dec", this.password, "./target/main8/plain.txt" };
- CLI2.main(encryptionArgs);
- CLI2.main(decryptionArgs);
- }
-
- @Test
- public void testYetAnotherStringEncryption()
- {
- try {
- // assumptions
- String topSecret= "mysecretpassword";
- assertTrue(FileUtils.readFileToString(
- new File("./src/test/data/plain-simple.txt"), StandardCharsets.UTF_8).
- equals(topSecret));
- // test
- // encode from string = hexdecimal file
- String[] encryptionArgs = { "string", "enc"+TYPES.GCM, this.password, topSecret,
- "./target/main8/another-plain16chars.enc.txt"};
- CLI2.main(encryptionArgs);
- // shows hex dec in stderr + file
-
- String[] decryptionArgs = { "string", "dec"+TYPES.GCM, this.password,
- "c9fa3e7d3c49d379ee8ff2dff6e6effbafee264794a03d0ffd895caac2b3c9b4558087f5b12e72a92475f1ed638b7911389234b443d4ebcf351c86cb",
- "./target/main8/another-plain16chars.dec.txt"};
- CLI2.main(decryptionArgs);
- // shows clear password in stdout + file
-
- assertTrue(
- FileUtils.readFileToString(
- new File("./target/main8/another-plain16chars.dec.txt"), StandardCharsets.UTF_8).
- equals(topSecret));
-
- String[] decryptionArgs2 = { "string", "dec"+TYPES.GCM, this.password,
- "605efd3009a7242a9c9cab23aa712d6d116e8686732194d3306416cda2a416df1e63aeffcdc1910af1e1100b382b24fc628d9c413ebf7e1b2885c0ec"};
- CLI2.main(decryptionArgs2);
- // shows clear password in stdout (offline decoded)
-
- // file mode commands do show nothing on stdout, except Decrypting / Encrypting
-
- // should not fail, if converted from hex
- String[] decryptionArgs3 = { "file", "dec"+TYPES.GCM, this.password, "./target/main8/another-plain16chars.enc.txt", "./target/main8/another-plain16chars2.dec.txt"};
- CLI2.main(decryptionArgs3);
- assertTrue(
- FileUtils.readFileToString(
- new File("./target/main8/another-plain16chars2.dec.txt"), StandardCharsets.UTF_8).
- equals(topSecret));
-
- String[] encryptionArgs4 = { "file", "enc"+TYPES.GCM, this.password,
- "./src/test/data/plain-simple.txt", "./target/main8/plain-simple.enc.txt" };
- CLI2.main(encryptionArgs4);
-
- String[] decryptionArgs4 = { "file", "dec"+TYPES.GCM, this.password,
- "./target/main8/plain-simple.enc.txt", "./target/main8/plain-simple.dec.txt"};
- CLI2.main(decryptionArgs4);
-
- try {
- assertTrue(
- FileUtils.contentEquals(
- new File("./src/test/data/plain-simple.txt"),
- new File("./target/main8/plain-simple.dec.txt"))
- );
- } catch (IOException e) {
- fail();
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- fail();
+public class Main8Test {
+ /** the password to be used */
+ private String password;
+
+ /**
+ * Constructor
+ */
+ public Main8Test() {
+
+ this.password = "foobar";
+ ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
+ builder.setStatusLevel(Level.DEBUG);
+ }
+
+ @BeforeAll
+ public static void setup() {
+ SupportedTypeArguments.init();
+ }
+
+ /**
+ * @return Returns the password.
+ */
+ protected char[] getPassword() {
+ return password.toCharArray();
+ }
+
+ /** Encrypt a string on the command line */
+ @Test
+ public void testStringEncryption() {
+ String[] encryptionArgs = { "string", "enc", this.password, "mysecretpassword" };
+ CLI2.main(encryptionArgs);
+ String[] decryptionArgs = { "string", "dec", this.password,
+ "ce3bf02da8a57c94b4f42c084230d1bedcd856c49a3fd23ec59835ca46a3d37ee02d470394691353478c905e7b342316d1fcc3e1b98837bf0595ef50853922df" };
+ CLI2.main(decryptionArgs);
+ }
+
+ @Test
+ public void testAnotherStringEncryption() {
+ String[] encryptionArgs = { "string", "enc", this.password, "secret" };
+ CLI2.main(encryptionArgs);
+ String[] decryptionArgs = { "string", "dec", this.password,
+ "8626904c9e64fddfa64add56472c4796429b0adb7c8039424adef7434be6bc255ce092011e8c560965814e806dd68117" };
+ CLI2.main(decryptionArgs);
+ }
+
+ @Test
+ /** Encrypt a text file on the command line */
+ public void testFileEncryption1() {
+ String[] encryptionArgs = { "file", "enc", this.password, "./src/test/data/plain.txt",
+ "./target/main8/plain.enc.txt" };
+ String[] decryptionArgs = { "file", "dec", this.password, "./target/main8/plain.enc.txt",
+ "./target/main8/plain.dec.txt" };
+ CLI2.main(encryptionArgs);
+ CLI2.main(decryptionArgs);
+ try {
+ assertTrue(FileUtils.contentEquals(new File("./src/test/data/plain.txt"),
+ new File("./target/main8/plain.dec.txt")));
+ } catch (IOException e) {
+ fail();
+ }
+ }
+
+ @Test
+ /** Encrypt a text file in-place on the command line */
+ public void testFileEncryption2() {
+ String[] encryptionArgs = { "file", "enc", this.password, "./src/test/data/plain.txt",
+ "./target/main8/plain.txt" };
+ // caution decrypting into source file!
+ String[] decryptionArgs = { "file", "dec", this.password, "./target/main8/plain.txt" };
+ CLI2.main(encryptionArgs);
+ CLI2.main(decryptionArgs);
+ }
+
+ @Test
+ public void testYetAnotherStringEncryption() {
+ try {
+ // assumptions
+ String topSecret = "mysecretpassword";
+ assertTrue(FileUtils.readFileToString(new File("./src/test/data/plain-simple.txt"), StandardCharsets.UTF_8)
+ .equals(topSecret));
+
+ if (SupportedTypeArguments.SUPPORTED_TYPES.contains(TYPES.GCM.toString())) {
+
+ // test
+ // encode from string = hexdecimal file
+ String[] encryptionArgs = { "string", "enc" + TYPES.GCM, this.password, topSecret,
+ "./target/main8/another-plain16chars.enc.txt" };
+ CLI2.main(encryptionArgs);
+ // shows hex dec in stderr + file
+
+ String[] decryptionArgs = { "string", "dec" + TYPES.GCM, this.password,
+ "c9fa3e7d3c49d379ee8ff2dff6e6effbafee264794a03d0ffd895caac2b3c9b4558087f5b12e72a92475f1ed638b7911389234b443d4ebcf351c86cb",
+ "./target/main8/another-plain16chars.dec.txt" };
+ CLI2.main(decryptionArgs);
+ // shows clear password in stdout + file
+
+ assertTrue(FileUtils.readFileToString(new File("./target/main8/another-plain16chars.dec.txt"),
+ StandardCharsets.UTF_8).equals(topSecret));
+
+ String[] decryptionArgs2 = { "string", "dec" + TYPES.GCM, this.password,
+ "605efd3009a7242a9c9cab23aa712d6d116e8686732194d3306416cda2a416df1e63aeffcdc1910af1e1100b382b24fc628d9c413ebf7e1b2885c0ec" };
+ CLI2.main(decryptionArgs2);
+ // shows clear password in stdout (offline decoded)
+
+ // file mode commands do show nothing on stdout, except Decrypting / Encrypting
+
+ // should not fail, if converted from hex
+ String[] decryptionArgs3 = { "file", "dec" + TYPES.GCM, this.password,
+ "./target/main8/another-plain16chars.enc.txt", "./target/main8/another-plain16chars2.dec.txt" };
+ CLI2.main(decryptionArgs3);
+ assertTrue(FileUtils.readFileToString(new File("./target/main8/another-plain16chars2.dec.txt"),
+ StandardCharsets.UTF_8).equals(topSecret));
+
+ String[] encryptionArgs4 = { "file", "enc" + TYPES.GCM, this.password,
+ "./src/test/data/plain-simple.txt", "./target/main8/plain-simple.enc.txt" };
+ CLI2.main(encryptionArgs4);
+
+ String[] decryptionArgs4 = { "file", "dec" + TYPES.GCM, this.password,
+ "./target/main8/plain-simple.enc.txt", "./target/main8/plain-simple.dec.txt" };
+ CLI2.main(decryptionArgs4);
+
+ try {
+ assertTrue(FileUtils.contentEquals(new File("./src/test/data/plain-simple.txt"),
+ new File("./target/main8/plain-simple.dec.txt")));
+ } catch (IOException e) {
+ fail();
+ }
+
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ @Test
+ public void testIntegratedStringEncryption() {
+ try {
+ // assumptions
+ String topSecret = "myX!_secretp@ssword?~,2";
+
+ Map<String, Object> conf = new HashMap<>();
+ conf.put("enc", TYPES.GCM);
+
+ // encode as string to stderr and file in hexdecimal format
+ String[] encryptionArgs = { "string", "enc" + conf.get("enc"), this.password, topSecret,
+ "./target/main8/integrated-plain16chars.enc.txt" };
+ CLI2.main(encryptionArgs);
+ // shows encoded hexdec in stdout + file
+
+ String encodedEncrypted = FileUtils.readFileToString(
+ new File("./target/main8/integrated-plain16chars.enc.txt"), StandardCharsets.UTF_8);
+
+ conf.put("pw", encodedEncrypted);
+
+ // this should be done without output to console
+ String result = CLI2.processString("dec" + conf.get("enc"), this.password.toCharArray(),
+ (String) conf.get("pw"));
+
+ assertTrue(result.equals(topSecret));
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ @Test
+ public void testDefaultStringEncryption() {
+ char[] testVector = new char[513];
+
+ for (int i = 0; i < testVector.length; i++) {
+ testVector[i] = (char) i;
}
- }
-
- @Test
- public void testIntegratedStringEncryption()
- {
+ String source = new String(testVector);
+
+ CryptoUtilJ8.getInstances().clear();
+ CryptoUtilJ8 cuj8 = CryptoUtilJ8.getInstance();
+ String cipherText;
+ String plainText;
try {
- // assumptions
- String topSecret= "myX!_secretp@ssword?~,2";
-
- Map<String,Object> conf = new HashMap<>();
- conf.put("enc",TYPES.GCM);
-
- // encode as string to stderr and file in hexdecimal format
- String[] encryptionArgs = {
- "string", "enc"+ conf.get("enc"), this.password,
- topSecret,
- "./target/main8/integrated-plain16chars.enc.txt"};
- CLI2.main(encryptionArgs);
- // shows encoded hexdec in stdout + file
-
- String encodedEncrypted = FileUtils.readFileToString(new File("./target/main8/integrated-plain16chars.enc.txt"), StandardCharsets.UTF_8);
-
- conf.put("pw", encodedEncrypted);
-
- // this should be done without output to console
- String result = CLI2.processString( "dec"+ conf.get("enc"), this.password.toCharArray(), (String) conf.get("pw"));
-
- assertTrue(
- result.equals(topSecret));
-
- } catch (Exception e) {
+ cipherText = cuj8.encryptString(source, this.getPassword());
+ plainText = cuj8.decryptString(cipherText, this.getPassword());
+ assertEquals(source, plainText, source +" is not equal with " + plainText);
+ } catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
fail();
}
Added: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/junit5/extension/SupportedTypeArguments.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/junit5/extension/SupportedTypeArguments.java?rev=1880199&view=auto
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/junit5/extension/SupportedTypeArguments.java (added)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/junit5/extension/SupportedTypeArguments.java Thu Jul 23 11:58:44 2020
@@ -0,0 +1,71 @@
+package org.apache.fulcrum.jce.junit5.extension;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8;
+import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.ArgumentsProvider;
+
+/*
+ * 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.
+ */
+
+/**
+ * Using {@link CryptoParametersJ8#init()} and ArgumentsProvider to filter parameters.
+ *
+ * Could still not access arguments of parameterized tests in lifecycle callback methods
+ *
+ * - https://github.com/junit-team/junit5/issues/944
+ * - https://github.com/junit-team/junit5/issues/1139#issuecomment-341683075
+ *
+ * @author gkallidis
+ *
+ */
+public class SupportedTypeArguments implements ArgumentsProvider {
+
+ public static Logger log = LogManager.getLogger();
+
+ public static List<String> SUPPORTED_TYPES = null;
+
+ public static void init() {
+ if (SUPPORTED_TYPES == null) {
+ SUPPORTED_TYPES = CryptoParametersJ8.init();
+ }
+ log.warn("SUPPORTED_TYPES: {}",SupportedTypeArguments.SUPPORTED_TYPES);
+ }
+
+ @Override
+ public Stream<? extends Arguments> provideArguments(ExtensionContext arg0) throws Exception {
+ if (SUPPORTED_TYPES == null) {
+ init();
+ }
+ return SUPPORTED_TYPES.stream().map(Arguments::of);
+ }
+
+ public static List<String> getSUPPORTED_TYPES() {
+ return SUPPORTED_TYPES;
+ }
+
+}
Modified: turbine/fulcrum/trunk/yaafi-crypto/xdocs/index.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/xdocs/index.xml?rev=1880199&r1=1880198&r2=1880199&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/xdocs/index.xml (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/xdocs/index.xml Thu Jul 23 11:58:44 2020
@@ -120,7 +120,7 @@
</tr>
</table>
</subsection>
- <subsection name="Availabe Algorithms">
+ <subsection name="Availabe Cipher Algorithms (Algorithm Modes)">
<table>
<tr>
<th>Provider Version</th>
@@ -134,10 +134,14 @@
<td>SunJCE 1.42</td>
<td>PBEWithMD5AndDES</td>
</tr>
- <tr>
+ <tr>
<td>SunJCE (Java 8)</td>
<td>PBEWithHmacSHA256AndAES_256</td>
</tr>
+ <tr>
+ <td>SunJCE (Java 8)</td>
+ <td>AES_128/GCM/NoPadding</td>
+ </tr>
</table>
</subsection>
</section>