/*
* This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
* Copyright Jayasoft 2005 - All rights reserved
*
* #SNAPSHOT#
*/
package fr.jayasoft.crypto;
import junit.framework.TestCase;
import fr.jayasoft.crypto.decoder.Decoder;
import fr.jayasoft.crypto.encoder.Encoder;
import fr.jayasoft.crypto.keys.KeyGenerator;
public class SimpleCryptoTest extends TestCase {
protected void setUp() throws Exception {
}
public void testImplementations() {
doTestAlgo(CryptoFactory.NONE);
doTestAlgo(CryptoFactory.DES);
doTestAlgo(CryptoFactory.BLOWFISH);
doTestAlgo(CryptoFactory.RSA);
}
public void doTestAlgo(String algo) {
System.out.println("testing: " + algo);
Crypto c = CryptoFactory.get(algo);
KeyGenerator kg = c.getKeyGenerator();
Encoder e = c.newEncoder(kg.getEncodingKey());
Decoder d = c.newDecoder(kg.getDecodingKey());
System.out.println("--> bytes testing");
encodeBytes(e,d);
System.out.println("--> short string testing");
encodeString(e,d,"test");
System.out.println("--> long string testing");
encodeString(e,d,"a very long string to test a very long encryption to see if encoding/decoding works well on long strings");
}
private void encodeBytes(Encoder e, Decoder d) {
byte[] bytes = "test".getBytes();
byte[] encodedBytes = e.encode(bytes);
byte[] decodedBytes = d.decode(encodedBytes);
assertEquals(bytes.length, decodedBytes.length);
for (int i = 0; i < decodedBytes.length; i++) {
assertEquals(bytes[i], decodedBytes[i]);
}
}
private void encodeString(Encoder e, Decoder d, String str) {
byte[] encodedBytes = e.encode(str);
String decodedStr = d.decodeAsString(encodedBytes);
assertEquals(str, decodedStr);
}
}