Search in sources :

Example 1 with FileContentCryptor

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();
}
Also used : InOrder(org.mockito.InOrder) FileContentCryptor(org.cryptomator.cryptolib.api.FileContentCryptor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with FileContentCryptor

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);
}
Also used : Path(java.nio.file.Path) FileHeaderCryptor(org.cryptomator.cryptolib.api.FileHeaderCryptor) FileHeaderCryptor(org.cryptomator.cryptolib.api.FileHeaderCryptor) FileContentCryptor(org.cryptomator.cryptolib.api.FileContentCryptor) Cryptor(org.cryptomator.cryptolib.api.Cryptor) FileSystemProvider(java.nio.file.spi.FileSystemProvider) FileSystem(java.nio.file.FileSystem) FileContentCryptor(org.cryptomator.cryptolib.api.FileContentCryptor) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Before(org.junit.Before)

Example 3 with FileContentCryptor

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();
}
Also used : InOrder(org.mockito.InOrder) FileContentCryptor(org.cryptomator.cryptolib.api.FileContentCryptor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

FileContentCryptor (org.cryptomator.cryptolib.api.FileContentCryptor)3 ByteBuffer (java.nio.ByteBuffer)2 Test (org.junit.Test)2 InOrder (org.mockito.InOrder)2 FileSystem (java.nio.file.FileSystem)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 FileSystemProvider (java.nio.file.spi.FileSystemProvider)1 Cryptor (org.cryptomator.cryptolib.api.Cryptor)1 FileHeaderCryptor (org.cryptomator.cryptolib.api.FileHeaderCryptor)1 Before (org.junit.Before)1