use of org.cryptomator.cryptolib.api.FileContentCryptor in project cryptofs by cryptomator.
the class OpenCryptoFileTest method testRead.
@Test
public void testRead() throws IOException {
// 1 kb per chunk
int cleartextChunkSize = 1000;
ByteBuffer buf = ByteBuffer.allocate(10);
// 10 gb total file size
size.set(10_000_000_000l);
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);
// A
inOrder.verify(chunkCache).get(0l);
// B
inOrder.verify(chunkCache).get(1l);
// B
inOrder.verify(chunkCache).get(2l);
// C
inOrder.verify(chunkCache).get(5_000_000l);
inOrder.verifyNoMoreInteractions();
}
use of org.cryptomator.cryptolib.api.FileContentCryptor in project cryptofs by cryptomator.
the class CryptoBasicFileAttributesTest method setup.
@Before
public void setup() throws IOException {
cryptor = Mockito.mock(Cryptor.class);
FileHeaderCryptor headerCryptor = Mockito.mock(FileHeaderCryptor.class);
FileContentCryptor contentCryptor = Mockito.mock(FileContentCryptor.class);
Mockito.when(cryptor.fileHeaderCryptor()).thenReturn(headerCryptor);
Mockito.when(headerCryptor.headerSize()).thenReturn(88);
Mockito.when(cryptor.fileContentCryptor()).thenReturn(contentCryptor);
Mockito.when(contentCryptor.cleartextChunkSize()).thenReturn(32 * 1024);
Mockito.when(contentCryptor.ciphertextChunkSize()).thenReturn(16 + 32 * 1024 + 32);
ciphertextFilePath = Mockito.mock(Path.class);
FileSystem fs = Mockito.mock(FileSystem.class);
Mockito.when(ciphertextFilePath.getFileSystem()).thenReturn(fs);
FileSystemProvider fsProvider = Mockito.mock(FileSystemProvider.class);
Mockito.when(fs.provider()).thenReturn(fsProvider);
delegateAttr = Mockito.mock(BasicFileAttributes.class);
}
use of org.cryptomator.cryptolib.api.FileContentCryptor in project cryptofs by cryptomator.
the class OpenCryptoFileTest method testWrite.
@Test
public void testWrite() throws IOException {
// 1 kb per chunk
int cleartextChunkSize = 1000;
// 10 gb total file size
size.set(10_000_000_000l);
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);
// A
inOrder.verify(chunkCache).get(0l);
// B
inOrder.verify(chunkCache).set(Mockito.eq(1l), Mockito.any());
// C
inOrder.verify(chunkCache).set(Mockito.eq(5_000_000l), Mockito.any());
inOrder.verifyNoMoreInteractions();
}
Aggregations