You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by sg...@apache.org on 2005/03/01 11:51:09 UTC
cvs commit: jakarta-turbine-fulcrum/yaafi/src/test/org/apache/fulcrum/jce/crypto CryptoUtilTest.java SmartDecryptingInputStreamTest.java
sgoeschl 2005/03/01 02:51:09
Added: yaafi/src/test/org/apache/fulcrum/jce/crypto
CryptoUtilTest.java
SmartDecryptingInputStreamTest.java
Log:
Synchronizing my development CVS with Fulrum
Revision Changes Path
1.1 jakarta-turbine-fulcrum/yaafi/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilTest.java
Index: CryptoUtilTest.java
===================================================================
package org.apache.fulcrum.jce.crypto;
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed 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.
*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import org.apache.fulcrum.jce.crypto.CryptoParameters;
import org.apache.fulcrum.jce.crypto.CryptoStreamFactoryImpl;
import org.apache.fulcrum.jce.crypto.CryptoUtil;
import org.apache.fulcrum.jce.crypto.HexConverter;
import org.apache.fulcrum.jce.crypto.PasswordFactory;
import junit.framework.TestCase;
/**
* Test suite for crypto functionality
*
* @author <a href="mailto:siegfried.goeschl@drei.com">Siegfried Goeschl</a>
* @version $Id: CryptoUtilTest.java,v 1.1 2005/03/01 10:51:08 sgoeschl Exp $
*/
public class CryptoUtilTest extends TestCase
{
/** the password to be used */
private String password;
/** the test data directory */
private File testDataDirectory;
/** the temp data director */
private File tempDataDirectory;
/**
* Constructor
* @param name the name of the test case
*/
public CryptoUtilTest( String name )
{
super(name);
this.password = "mysecret";
this.testDataDirectory = new File( "./src/test/data" );
this.tempDataDirectory = new File( "./temp" );
}
/**
* @see junit.framework.TestCase#setUp()
* byte[] salt,
int count,
String algorithm,
String providerName )
*/
protected void setUp() throws Exception
{
CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(
CryptoParameters.SALT,
CryptoParameters.COUNT,
"PBEWithMD5AndDES",
CryptoParameters.PROVIDERNAME
);
CryptoStreamFactoryImpl.setInstance( factory );
}
/**
* @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 */
public void testTextEncryption() throws Exception
{
File sourceFile = new File( this.getTestDataDirectory(), "plain.txt" );
File targetFile = new File( this.getTempDataDirectory(), "plain.enc.txt" );
CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
}
/** Decrypt a text file */
public void testTextDecryption() throws Exception
{
testTextEncryption();
File sourceFile = new File( this.getTempDataDirectory(), "plain.enc.txt" );
File targetFile = new File( this.getTempDataDirectory(), "plain.dec.txt" );
CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
}
/** Encrypt an empty text file */
public void testEmptyTextEncryption() throws Exception
{
File sourceFile = new File( this.getTestDataDirectory(), "empty.txt" );
File targetFile = new File( this.getTempDataDirectory(), "empty.enc.txt" );
CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
}
/** Decrypt a text file */
public void testEmptyTextDecryption() throws Exception
{
testEmptyTextEncryption();
File sourceFile = new File( this.getTempDataDirectory(), "empty.enc.txt" );
File targetFile = new File( this.getTempDataDirectory(), "empty.dec.txt" );
CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
}
/** Encrypt a PDF file */
public void testPdfEncryption() throws Exception
{
File sourceFile = new File( this.getTestDataDirectory(), "plain.pdf" );
File targetFile = new File( this.getTempDataDirectory(), "plain.enc.pdf" );
CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
}
/** Decrypt a PDF file */
public void testPdfDecryption() throws Exception
{
testPdfEncryption();
File sourceFile = new File( this.getTempDataDirectory(), "plain.enc.pdf" );
File targetFile = new File( this.getTempDataDirectory(), "plain.dec.pdf" );
CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
}
/** Encrypt a ZIP file */
public void testZipEncryption() throws Exception
{
File sourceFile = new File( this.getTestDataDirectory(), "plain.zip" );
File targetFile = new File( this.getTempDataDirectory(), "plain.enc.zip" );
CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
}
/** Decrypt a ZIP file */
public void testZipDecryption() throws Exception
{
testZipEncryption();
File sourceFile = new File( this.getTempDataDirectory(), "plain.enc.zip" );
File targetFile = new File( this.getTempDataDirectory(), "plain.dec.zip" );
CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
}
/** Encrypt a UTF-16 XML file */
public void testXmlUTF16Encryption() throws Exception
{
File sourceFile = new File( this.getTestDataDirectory(), "plain-utf16.xml" );
File targetFile = new File( this.getTempDataDirectory(), "plain-utf16.enc.xml" );
CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
}
/** Decrypt a UTF-16 XML file */
public void testXMLUTF16Decryption() throws Exception
{
testXmlUTF16Encryption();
File sourceFile = new File( this.getTempDataDirectory(), "plain-utf16.enc.xml" );
File targetFile = new File( this.getTempDataDirectory(), "plain-utf16.dec.xml" );
CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
}
/** Encrypt a UTF-8 XML file */
public void testXmlUTF8Encryption() throws Exception
{
File sourceFile = new File( this.getTestDataDirectory(), "plain-utf8.xml" );
File targetFile = new File( this.getTempDataDirectory(), "plain-utf8.enc.xml" );
CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
}
/** Decrypt a UTF-8 XML file */
public void testXMLUTF8Decryption() throws Exception
{
testXmlUTF8Encryption();
File sourceFile = new File( this.getTempDataDirectory(), "plain-utf8.enc.xml" );
File targetFile = new File( this.getTempDataDirectory(), "plain-utf8.dec.xml" );
CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
}
/** Encrypt a ISO-8859-1 XML file */
public void testXmlISO88591Encryption() throws Exception
{
File sourceFile = new File( this.getTestDataDirectory(), "plain-iso-8859-1.xml" );
File targetFile = new File( this.getTempDataDirectory(), "plain-iso-8859-1.enc.xml" );
CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
}
/** Decrypt a UTF-8 XML file */
public void testXmlISO88591Decryption() throws Exception
{
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.decrypt( sourceFile, targetFile, this.getPassword() );
}
/** Test encryption and decryption of Strings */
public void testStringEncryption() throws Exception
{
char[] testVector = new char[513];
for( int i=0; i<testVector.length; i++ )
{
testVector[i] = (char) i;
}
String source = new String( testVector );
String cipherText = CryptoUtil.encryptString( source, this.getPassword() );
String plainText = CryptoUtil.decryptString( cipherText, this.getPassword() );
assertEquals( source, plainText );
}
/** Test encryption and decryption of Strings */
public void testStringHandling() throws Exception
{
String source = "Nobody knows the toubles I have seen ...";
String cipherText = CryptoUtil.encryptString( source, this.getPassword() );
String plainText = CryptoUtil.decryptString( cipherText, this.getPassword() );
assertEquals( source, plainText );
}
/** Test encryption and decryption of binary data */
public void testBinaryHandling() throws Exception
{
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();
CryptoUtil.encrypt( source, cipherText, this.getPassword() );
CryptoUtil.decrypt( cipherText, plainText, this.getPassword() );
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 creating a password */
public void testPasswordFactory() throws Exception
{
char[] result = null;
result = PasswordFactory.create();
result = PasswordFactory.create( this.getPassword() );
return;
}
public void testHexConverter() throws Exception
{
String source = "DceuATAABWSaVTSIK";
String hexString = HexConverter.toString( source.getBytes() );
String result = new String( HexConverter.toBytes( hexString ) );
assertEquals( source, result );
}
}
1.1 jakarta-turbine-fulcrum/yaafi/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java
Index: SmartDecryptingInputStreamTest.java
===================================================================
package org.apache.fulcrum.jce.crypto;
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed 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.
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import org.apache.fulcrum.jce.crypto.CryptoParameters;
import org.apache.fulcrum.jce.crypto.CryptoStreamFactoryImpl;
import org.apache.fulcrum.jce.crypto.CryptoUtil;
import junit.framework.TestCase;
/**
* Test suite for SmartDecryptingInputStream
*
* @author <a href="mailto:siegfried.goeschl@drei.com">Siegfried Goeschl</a>
* @version $Id: SmartDecryptingInputStreamTest.java,v 1.1 2005/03/01 10:51:08 sgoeschl Exp $
*/
public class SmartDecryptingInputStreamTest extends TestCase
{
/** the password to be used */
private String password;
/** the test data directory */
private File testDataDirectory;
/** the temp data director */
private File tempDataDirectory;
/**
* Constructor
* @param name the name of the test case
*/
public SmartDecryptingInputStreamTest( String name )
{
super(name);
this.password = "mysecret";
this.testDataDirectory = new File( "./src/test/data" );
this.tempDataDirectory = new File( "./temp" );
}
/**
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception
{
CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(
CryptoParameters.SALT,
CryptoParameters.COUNT,
"PBEWithMD5AndDES",
CryptoParameters.PROVIDERNAME
);
CryptoStreamFactoryImpl.setInstance( factory );
}
/**
* @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;
}
public void testSmartEmtpyDecryption() throws Exception
{
this.testSmartDecryption("empty.txt","ISO-8859-1");
}
public void testSmartTextDecryption() throws Exception
{
this.testSmartDecryption("plain.txt","ISO-8859-1");
}
public void testSmartGroovyDecryption() throws Exception
{
this.testSmartDecryption("plain.groovy","ISO-8859-1");
}
public void testSmartXmlIso8859Utf8Decryption() throws Exception
{
this.testSmartDecryption("plain-iso-8859-1.xml","ISO-8859-1");
}
public void testSmartXmlUtf8Decryption() throws Exception
{
this.testSmartDecryption("plain-utf8.xml","UTF-8");
}
public void testSmartXmlUtf16Decryption() throws Exception
{
this.testSmartDecryption("plain-utf16.xml","UTF-16");
}
public void testPDFDecryption() throws Exception
{
this.testSmartDecryption("plain.pdf","ISO-8859-1");
}
public void testZIPDecryption() throws Exception
{
this.testSmartDecryption("plain.zip","ISO-8859-1");
}
/** Test smart decryption for a given file */
private void testSmartDecryption( String fileName, String enc ) throws Exception
{
File sourceFile = new File( this.getTestDataDirectory(), fileName );
String plainText = this.loadTextFile(sourceFile,enc);
String smartText = this.smartDecrypt(sourceFile,enc);
byte[] cipherText = this.encryptTextFile(sourceFile);
String decryptedText = this.smartDecrypt(cipherText,enc);
assertTrue( plainText.length() == smartText.length() );
assertTrue( plainText.length() == decryptedText.length() );
assertEquals( plainText, smartText );
assertEquals( plainText, decryptedText );
}
/**
* Loads a plain text file.
* @param file the file to load
*/
private String loadTextFile( File file, String enc ) throws Exception
{
String result = null;
FileInputStream fis = new FileInputStream( file );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CryptoUtil.copy(fis,baos);
fis.close();
result = new String( baos.toByteArray(), enc );
return result;
}
/**
* Encrypt a plain text file.
* @param file the file to encrypt
*/
private byte[] encryptTextFile( File file ) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream( file );
CryptoUtil.encrypt(
CryptoStreamFactoryImpl.getInstance(),
fis,
baos,
this.getPassword()
);
fis.close();
return baos.toByteArray();
}
/**
* Use smart decryption on a cipherText.
*
* @param cipherText the encrypted text
* @return the decrypeted content
*/
private String smartDecrypt( byte[] cipherText, String enc ) throws Exception
{
ByteArrayInputStream bais = new ByteArrayInputStream(cipherText);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SmartDecryptingInputStream sdis = new SmartDecryptingInputStream(
CryptoStreamFactoryImpl.getInstance(),
bais,
this.getPassword()
);
CryptoUtil.copy(sdis,baos);
return new String( baos.toByteArray(), enc );
}
/**
* Use smart decryption on a plain text file.
*
* @param file the file to load
* @return the content
*/
private String smartDecrypt( File file, String enc ) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream( file );
SmartDecryptingInputStream sdis = new SmartDecryptingInputStream(
CryptoStreamFactoryImpl.getInstance(),
fis,
this.getPassword()
);
CryptoUtil.copy(sdis,baos);
return new String( baos.toByteArray(), enc );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org