- faster unit test using insecure PRNG - test only ;)

This commit is contained in:
Sebastian Stenzel
2014-12-16 12:13:01 +01:00
parent 7d6d061d95
commit 38a0cfb2eb
3 changed files with 25 additions and 59 deletions
@@ -26,6 +26,7 @@ import java.security.spec.KeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.zip.CRC32;
@@ -102,11 +103,24 @@ public class Aes256Cryptor extends AbstractCryptor implements AesCryptographicCo
}
}
/**
* Creates a new Cryptor with a newly initialized PRNG.
*/
public Aes256Cryptor() {
SECURE_PRNG.setSeed(SECURE_PRNG.generateSeed(PRNG_SEED_LENGTH));
SECURE_PRNG.nextBytes(this.masterKey);
}
/**
* Creates a new Cryptor with the given PRNG.<br/>
* <strong>DO NOT USE IN PRODUCTION</strong>. This constructor must only be used in in unit tests. Do not change method visibility.
*
* @param prng Fast, possibly insecure PRNG.
*/
Aes256Cryptor(Random prng) {
prng.nextBytes(this.masterKey);
}
/**
* Encrypts the current masterKey with the given password and writes the result to the given output stream.
*/
@@ -19,6 +19,7 @@ import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.apache.commons.io.FileUtils;
import org.cryptomator.crypto.CryptorIOSupport;
@@ -32,6 +33,8 @@ import org.junit.Test;
public class Aes256CryptorTest {
private static final Random TEST_PRNG = new Random();
private Path tmpDir;
private Path masterKey;
@@ -57,12 +60,12 @@ public class Aes256CryptorTest {
@Test
public void testCorrectPassword() throws IOException, WrongPasswordException, DecryptFailedException, UnsupportedKeyLengthException {
final String pw = "asd";
final Aes256Cryptor cryptor = new Aes256Cryptor();
final Aes256Cryptor cryptor = new Aes256Cryptor(TEST_PRNG);
final OutputStream out = Files.newOutputStream(masterKey, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
cryptor.encryptMasterKey(out, pw);
cryptor.swipeSensitiveData();
final Aes256Cryptor decryptor = new Aes256Cryptor();
final Aes256Cryptor decryptor = new Aes256Cryptor(TEST_PRNG);
final InputStream in = Files.newInputStream(masterKey, StandardOpenOption.READ);
decryptor.decryptMasterKey(in, pw);
}
@@ -70,13 +73,13 @@ public class Aes256CryptorTest {
@Test(expected = WrongPasswordException.class)
public void testWrongPassword() throws IOException, DecryptFailedException, WrongPasswordException, UnsupportedKeyLengthException {
final String pw = "asd";
final Aes256Cryptor cryptor = new Aes256Cryptor();
final Aes256Cryptor cryptor = new Aes256Cryptor(TEST_PRNG);
final OutputStream out = Files.newOutputStream(masterKey, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
cryptor.encryptMasterKey(out, pw);
cryptor.swipeSensitiveData();
final String wrongPw = "foo";
final Aes256Cryptor decryptor = new Aes256Cryptor();
final Aes256Cryptor decryptor = new Aes256Cryptor(TEST_PRNG);
final InputStream in = Files.newInputStream(masterKey, StandardOpenOption.READ);
decryptor.decryptMasterKey(in, wrongPw);
}
@@ -84,13 +87,13 @@ public class Aes256CryptorTest {
@Test(expected = NoSuchFileException.class)
public void testWrongLocation() throws IOException, DecryptFailedException, WrongPasswordException, UnsupportedKeyLengthException {
final String pw = "asd";
final Aes256Cryptor cryptor = new Aes256Cryptor();
final Aes256Cryptor cryptor = new Aes256Cryptor(TEST_PRNG);
final OutputStream out = Files.newOutputStream(masterKey, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
cryptor.encryptMasterKey(out, pw);
cryptor.swipeSensitiveData();
final Path wrongMasterKey = tmpDir.resolve("notExistingMasterKey.json");
final Aes256Cryptor decryptor = new Aes256Cryptor();
final Aes256Cryptor decryptor = new Aes256Cryptor(TEST_PRNG);
final InputStream in = Files.newInputStream(wrongMasterKey, StandardOpenOption.READ);
decryptor.decryptMasterKey(in, pw);
}
@@ -98,7 +101,7 @@ public class Aes256CryptorTest {
@Test(expected = FileAlreadyExistsException.class)
public void testReInitialization() throws IOException {
final String pw = "asd";
final Aes256Cryptor cryptor = new Aes256Cryptor();
final Aes256Cryptor cryptor = new Aes256Cryptor(TEST_PRNG);
final OutputStream out = Files.newOutputStream(masterKey, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
cryptor.encryptMasterKey(out, pw);
cryptor.swipeSensitiveData();
@@ -111,7 +114,7 @@ public class Aes256CryptorTest {
@Test
public void testEncryptionOfFilenames() throws IOException {
final CryptorIOSupport ioSupportMock = new CryptoIOSupportMock();
final Aes256Cryptor cryptor = new Aes256Cryptor();
final Aes256Cryptor cryptor = new Aes256Cryptor(TEST_PRNG);
// short path components
final String originalPath1 = "foo/bar/baz";