Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make some better use of mocks during unit tests
- Loading branch information
Sebastian Stenzel
committed
Dec 7, 2015
1 parent
0697e19
commit 97a72ec
Showing
15 changed files
with
252 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
main/core/src/test/java/org/cryptomator/webdav/jackrabbit/CryptorMock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package org.cryptomator.webdav.jackrabbit; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.SeekableByteChannel; | ||
|
||
import org.cryptomator.crypto.Cryptor; | ||
import org.cryptomator.crypto.exceptions.DecryptFailedException; | ||
import org.cryptomator.crypto.exceptions.EncryptFailedException; | ||
import org.cryptomator.crypto.exceptions.MacAuthenticationFailedException; | ||
import org.cryptomator.crypto.exceptions.UnsupportedKeyLengthException; | ||
import org.cryptomator.crypto.exceptions.UnsupportedVaultException; | ||
import org.cryptomator.crypto.exceptions.WrongPasswordException; | ||
|
||
class CryptorMock implements Cryptor { | ||
|
||
private static final int BUFSIZE = 32768; | ||
|
||
@Override | ||
public void randomizeMasterKey() { | ||
// noop | ||
} | ||
|
||
@Override | ||
public void encryptMasterKey(OutputStream out, CharSequence password) throws IOException { | ||
// noop | ||
} | ||
|
||
@Override | ||
public void decryptMasterKey(InputStream in, CharSequence password) throws DecryptFailedException, WrongPasswordException, UnsupportedKeyLengthException, IOException, UnsupportedVaultException { | ||
// noop | ||
} | ||
|
||
@Override | ||
public String encryptDirectoryPath(String cleartextDirectoryId, String nativePathSep) { | ||
return cleartextDirectoryId; | ||
} | ||
|
||
@Override | ||
public String encryptFilename(String cleartextName) { | ||
return cleartextName; | ||
} | ||
|
||
@Override | ||
public String decryptFilename(String ciphertextName) throws DecryptFailedException { | ||
return ciphertextName; | ||
} | ||
|
||
@Override | ||
public Long decryptedContentLength(SeekableByteChannel encryptedFile) throws IOException, MacAuthenticationFailedException { | ||
return encryptedFile.size(); | ||
} | ||
|
||
@Override | ||
public Long decryptFile(SeekableByteChannel encryptedFile, OutputStream plaintextFile, boolean authenticate) throws IOException, DecryptFailedException { | ||
ByteBuffer buf = ByteBuffer.allocate(BUFSIZE); | ||
long numReadTotal = 0; | ||
int numRead = 0; | ||
while ((numRead = encryptedFile.read(buf)) != -1) { | ||
numReadTotal += numRead; | ||
buf.flip(); | ||
byte[] bytes = new byte[numRead]; | ||
buf.get(bytes); | ||
plaintextFile.write(bytes); | ||
buf.rewind(); | ||
} | ||
return numReadTotal; | ||
} | ||
|
||
@Override | ||
public Long decryptRange(SeekableByteChannel encryptedFile, OutputStream plaintextFile, long pos, long length, boolean authenticate) throws IOException, DecryptFailedException { | ||
encryptedFile.position(pos); | ||
|
||
ByteBuffer buf = ByteBuffer.allocate(BUFSIZE); | ||
long numReadTotal = 0; | ||
int numRead = 0; | ||
while ((numRead = encryptedFile.read(buf)) != -1 && numReadTotal < length) { | ||
int len = (int) Math.min(Math.min(numRead, BUFSIZE), length - numReadTotal); // known to fit into integer | ||
numReadTotal += len; | ||
buf.flip(); | ||
byte[] bytes = new byte[len]; | ||
buf.get(bytes); | ||
plaintextFile.write(bytes); | ||
buf.rewind(); | ||
} | ||
return numReadTotal; | ||
} | ||
|
||
@Override | ||
public Long encryptFile(InputStream plaintextFile, SeekableByteChannel encryptedFile) throws IOException, EncryptFailedException { | ||
byte[] buf = new byte[BUFSIZE]; | ||
long numWrittenTotal = 0; | ||
int numRead = 0; | ||
while ((numRead = plaintextFile.read(buf)) != -1) { | ||
numWrittenTotal += numRead; | ||
encryptedFile.write(ByteBuffer.wrap(buf, 0, numRead)); | ||
} | ||
return numWrittenTotal; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
main/crypto-aes/src/main/java/org/cryptomator/crypto/aes256/CryptoComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.cryptomator.crypto.aes256; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import org.cryptomator.crypto.Cryptor; | ||
|
||
import dagger.Component; | ||
|
||
@Singleton | ||
@Component(modules = CryptoModule.class) | ||
interface CryptoComponent { | ||
|
||
Cryptor cryptor(); | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
main/crypto-aes/src/main/java/org/cryptomator/crypto/aes256/CryptoModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.cryptomator.crypto.aes256; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
|
||
import org.cryptomator.crypto.Cryptor; | ||
|
||
import dagger.Module; | ||
import dagger.Provides; | ||
|
||
@Module | ||
public class CryptoModule { | ||
|
||
@Provides | ||
SecureRandom provideRandomNumberGenerator() { | ||
try { | ||
return SecureRandom.getInstanceStrong(); | ||
} catch (NoSuchAlgorithmException e) { | ||
// quote "Every implementation of the Java platform is required to support at least one strong SecureRandom implementation." | ||
throw new AssertionError("No SecureRandom implementation available."); | ||
} | ||
} | ||
|
||
@Provides | ||
public Cryptor provideCryptor(SecureRandom secureRandom) { | ||
return new Aes256Cryptor(secureRandom); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
main/crypto-aes/src/test/java/org/cryptomator/crypto/aes256/CryptoTestComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.cryptomator.crypto.aes256; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import org.cryptomator.crypto.Cryptor; | ||
|
||
import dagger.Component; | ||
|
||
@Singleton | ||
@Component(modules = CryptoTestModule.class) | ||
public interface CryptoTestComponent { | ||
|
||
Cryptor cryptor(); | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
main/crypto-aes/src/test/java/org/cryptomator/crypto/aes256/CryptoTestModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.cryptomator.crypto.aes256; | ||
|
||
import java.security.SecureRandom; | ||
|
||
import org.cryptomator.crypto.Cryptor; | ||
|
||
import dagger.Module; | ||
import dagger.Provides; | ||
|
||
@Module | ||
public class CryptoTestModule { | ||
|
||
@Provides | ||
@SuppressWarnings("deprecation") | ||
SecureRandom provideRandomNumberGenerator() { | ||
// we use this class for testing only, as unit tests on CI servers tend to stall, if they rely on true randomness. | ||
return new InsecureRandomMock(); | ||
} | ||
|
||
@Provides | ||
Cryptor provideCryptor(SecureRandom secureRandom) { | ||
return new Aes256Cryptor(secureRandom); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
main/crypto-aes/src/test/java/org/cryptomator/crypto/aes256/InsecureRandomMock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.cryptomator.crypto.aes256; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Random; | ||
|
||
/** | ||
* <b>DO NOT USE</b> | ||
* | ||
* This class is for testing only. | ||
*/ | ||
@Deprecated // marked as deprecated and made package-private inside /src/test/java to avoid accidential use. | ||
class InsecureRandomMock extends SecureRandom { | ||
|
||
private static final long serialVersionUID = 1505563778398085504L; | ||
private final Random random = new Random(); | ||
|
||
@Override | ||
public void nextBytes(byte[] bytes) { | ||
// let the deterministic RNG do the work: | ||
this.random.nextBytes(bytes); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.