Skip to content

Commit

Permalink
Merge branch 'release/1.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
overheadhunter committed Jun 8, 2017
2 parents 480d27f + 358ba17 commit 239cd7b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.cryptomator</groupId>
<artifactId>cryptofs</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
<name>Cryptomator Crypto Filesystem</name>
<description>This library provides the Java filesystem provider used by Cryptomator.</description>
<url>https://github.com/cryptomator/cryptofs</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -204,7 +205,11 @@ public Builder withPassphrase(CharSequence passphrase) {
return this;
}

public Builder withFlags(Set<FileSystemFlags> flags) {
public Builder withFlags(FileSystemFlags... flags) {
return withFlags(asList(flags));
}

public Builder withFlags(Collection<FileSystemFlags> flags) {
this.flags.clear();
this.flags.addAll(flags);
return this;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/cryptomator/cryptofs/OpenCryptoFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public synchronized int read(ByteBuffer dst, long position) throws IOException {
int payloadSize = cryptor.fileContentCryptor().cleartextChunkSize();
while (dst.hasRemaining()) {
long pos = position + read;
long chunkIndex = pos / payloadSize;
int offset = (int) pos % payloadSize;
int len = min(dst.remaining(), payloadSize - offset);
long chunkIndex = pos / payloadSize; // floor by int-truncation
int offsetInChunk = (int) (pos % payloadSize); // known to fit in int, because payloadSize is int
int len = min(dst.remaining(), payloadSize - offsetInChunk); // known to fit in int, because second argument is int
final ChunkData chunkData = chunkCache.get(chunkIndex);
chunkData.copyDataStartingAt(offset).to(dst);
chunkData.copyDataStartingAt(offsetInChunk).to(dst);
read += len;
}
dst.limit(origLimit);
Expand Down Expand Up @@ -138,7 +138,7 @@ public synchronized void truncate(long size) throws IOException {
if (originalSize > size) {
int cleartextChunkSize = cryptor.fileContentCryptor().cleartextChunkSize();
long indexOfLastChunk = (size + cleartextChunkSize - 1) / cleartextChunkSize - 1;
int sizeOfIncompleteChunk = (int) (size % cleartextChunkSize);
int sizeOfIncompleteChunk = (int) (size % cleartextChunkSize); // known to fit in int, because cleartextChunkSize is int
if (sizeOfIncompleteChunk > 0) {
chunkCache.get(indexOfLastChunk).truncate(sizeOfIncompleteChunk);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.spi.FileSystemProvider;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -206,7 +205,7 @@ public void testNoImplicitInitialization() throws IOException {
URI uri = CryptoFileSystemUri.create(pathToVault);

CryptoFileSystemProperties properties = cryptoFileSystemProperties() //
.withFlags(EnumSet.noneOf(FileSystemFlags.class)) //
.withFlags() //
.withMasterkeyFilename("masterkey.cryptomator") //
.withPassphrase("asd") //
.build();
Expand All @@ -229,7 +228,7 @@ public void testImplicitInitialization() throws IOException {
URI uri = CryptoFileSystemUri.create(pathToVault);

CryptoFileSystemProperties properties = cryptoFileSystemProperties() //
.withFlags(EnumSet.of(FileSystemFlags.INIT_IMPLICITLY)) //
.withFlags(FileSystemFlags.INIT_IMPLICITLY) //
.withMasterkeyFilename("masterkey.cryptomator") //
.withPassphrase("asd") //
.build();
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/org/cryptomator/cryptofs/OpenCryptoFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;
import java.util.concurrent.atomic.AtomicLong;

import org.cryptomator.cryptofs.OpenCounter.OpenState;
import org.cryptomator.cryptolib.api.Cryptor;
import org.cryptomator.cryptolib.api.FileContentCryptor;
import org.cryptomator.cryptolib.api.FileHeader;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

Expand Down Expand Up @@ -108,4 +114,68 @@ public void testCloseDelegatesToCryptoFileChannelFactory() throws IOException {
verify(cryptoFileChannelFactory).close();
}

@Test
public void testRead() throws IOException {
int cleartextChunkSize = 1000; // 1 kb per chunk
ByteBuffer buf = ByteBuffer.allocate(10);
size.set(10_000_000_000l); // 10 gb total file size

FileContentCryptor fileContentCryptor = Mockito.mock(FileContentCryptor.class);
when(cryptor.fileContentCryptor()).thenReturn(fileContentCryptor);
when(fileContentCryptor.cleartextChunkSize()).thenReturn(cleartextChunkSize);
when(chunkCache.get(Mockito.anyLong())).then(invocation -> {
return ChunkData.wrap(ByteBuffer.allocate(cleartextChunkSize));
});

// A read from frist chunk:
buf.clear();
inTest.read(buf, 0);

// B read from second and third chunk:
buf.clear();
inTest.read(buf, 1999);

// C read from position > maxint
buf.clear();
inTest.read(buf, 5_000_000_000l);

InOrder inOrder = Mockito.inOrder(chunkCache, chunkCache, chunkCache, chunkCache);
inOrder.verify(chunkCache).get(0l); // A
inOrder.verify(chunkCache).get(1l); // B
inOrder.verify(chunkCache).get(2l); // B
inOrder.verify(chunkCache).get(5_000_000l); // C
inOrder.verifyNoMoreInteractions();
}

@Test
public void testWrite() throws IOException {
int cleartextChunkSize = 1000; // 1 kb per chunk
size.set(10_000_000_000l); // 10 gb total file size

FileContentCryptor fileContentCryptor = Mockito.mock(FileContentCryptor.class);
when(cryptor.fileContentCryptor()).thenReturn(fileContentCryptor);
when(fileContentCryptor.cleartextChunkSize()).thenReturn(cleartextChunkSize);
when(chunkCache.get(Mockito.anyLong())).then(invocation -> {
return ChunkData.wrap(ByteBuffer.allocate(cleartextChunkSize));
});

// A change 10 bytes inside first chunk:
ByteBuffer buf1 = ByteBuffer.allocate(10);
inTest.write(EffectiveOpenOptions.from(EnumSet.of(StandardOpenOption.WRITE)), buf1, 0);

// B change complete second chunk:
ByteBuffer buf2 = ByteBuffer.allocate(1000);
inTest.write(EffectiveOpenOptions.from(EnumSet.of(StandardOpenOption.WRITE)), buf2, 1000);

// C change complete chunk at position > maxint:
ByteBuffer buf3 = ByteBuffer.allocate(1000);
inTest.write(EffectiveOpenOptions.from(EnumSet.of(StandardOpenOption.WRITE)), buf3, 5_000_000_000l);

InOrder inOrder = Mockito.inOrder(chunkCache, chunkCache, chunkCache);
inOrder.verify(chunkCache).get(0l); // A
inOrder.verify(chunkCache).set(Mockito.eq(1l), Mockito.any()); // B
inOrder.verify(chunkCache).set(Mockito.eq(5_000_000l), Mockito.any()); // C
inOrder.verifyNoMoreInteractions();
}

}

0 comments on commit 239cd7b

Please sign in to comment.