make some better use of mocks during unit tests

This commit is contained in:
Sebastian Stenzel
2015-12-07 14:59:12 +01:00
parent 0697e19b01
commit 97a72ecbf7
15 changed files with 252 additions and 28 deletions
-5
View File
@@ -27,11 +27,6 @@
<groupId>org.cryptomator</groupId>
<artifactId>crypto-api</artifactId>
</dependency>
<dependency>
<groupId>org.cryptomator</groupId>
<artifactId>crypto-aes</artifactId>
<scope>test</scope>
</dependency>
<!-- Jetty (Servlet Container) -->
<dependency>
@@ -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;
}
}
@@ -26,7 +26,7 @@ import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.cryptomator.crypto.aes256.Aes256Cryptor;
import org.cryptomator.crypto.Cryptor;
import org.cryptomator.webdav.WebDavServer;
import org.cryptomator.webdav.WebDavServer.ServletLifeCycleAdapter;
import org.junit.AfterClass;
@@ -41,7 +41,7 @@ import com.google.common.io.Files;
public class RangeRequestTest {
private static final Logger LOG = LoggerFactory.getLogger(RangeRequestTest.class);
private static final Aes256Cryptor CRYPTOR = new Aes256Cryptor();
private static final Cryptor CRYPTOR = new CryptorMock();
private static final WebDavServer SERVER = new WebDavServer();
private static final File TMP_VAULT = Files.createTempDir();
private static ServletLifeCycleAdapter SERVLET;