Skip to content

Commit 72ae360

Browse files
Merge branch 'master' into develop
# Conflicts: # src/main/java/org/cryptomator/cryptofs/CryptoFileSystemImpl.java # src/test/java/org/cryptomator/cryptofs/CryptoFileSystemImplTest.java
2 parents fb67c46 + 1f5fcf3 commit 72ae360

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/main/java/org/cryptomator/cryptofs/CryptoFileSystemImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ private FileChannel newFileChannel(CryptoPath cleartextFilePath, EffectiveOpenOp
368368
Files.createDirectories(ciphertextPath.getRawPath()); // suppresses FileAlreadyExists
369369
}
370370
FileChannel ch = openCryptoFiles.getOrCreate(ciphertextFilePath).newFileChannel(options); // might throw FileAlreadyExists
371-
ciphertextPath.persistLongFileName();
371+
if (options.writable()) {
372+
ciphertextPath.persistLongFileName();
373+
}
372374
return ch;
373375
}
374376
}

src/test/java/org/cryptomator/cryptofs/CryptoFileSystemImplTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.cryptomator.cryptofs.common.RunnableThrowingException;
1111
import org.cryptomator.cryptofs.dir.CiphertextDirectoryDeleter;
1212
import org.cryptomator.cryptofs.dir.DirectoryStreamFactory;
13+
import org.cryptomator.cryptofs.fh.OpenCryptoFile;
1314
import org.cryptomator.cryptofs.fh.OpenCryptoFiles;
1415
import org.cryptomator.cryptofs.fh.OpenCryptoFiles.TwoPhaseMove;
1516
import org.cryptomator.cryptofs.mocks.FileChannelMock;
@@ -18,6 +19,7 @@
1819
import org.hamcrest.MatcherAssert;
1920
import org.junit.jupiter.api.Assertions;
2021
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.DisplayName;
2123
import org.junit.jupiter.api.Nested;
2224
import org.junit.jupiter.api.Test;
2325
import org.mockito.Mockito;
@@ -340,6 +342,55 @@ public void testNewWatchServiceThrowsUnsupportedOperationException() throws IOEx
340342
});
341343
}
342344

345+
@Nested
346+
public class NewFileChannel {
347+
348+
private final CryptoPath cleartextPath = mock(CryptoPath.class, "cleartext");
349+
private final CryptoPath ciphertextFilePath = mock(CryptoPath.class, "ciphertext");
350+
private final CiphertextFilePath ciphertextPath = mock(CiphertextFilePath.class);
351+
private final OpenCryptoFile openCryptoFile = mock(OpenCryptoFile.class);
352+
private final FileChannel fileChannel = mock(FileChannel.class);
353+
354+
@BeforeEach
355+
public void setup() throws IOException {
356+
when(cryptoPathMapper.getCiphertextFileType(cleartextPath)).thenReturn(CiphertextFileType.FILE);
357+
when(cryptoPathMapper.getCiphertextFilePath(cleartextPath)).thenReturn(ciphertextPath);
358+
when(ciphertextPath.getFilePath()).thenReturn(ciphertextFilePath);
359+
when(openCryptoFiles.getOrCreate(ciphertextFilePath)).thenReturn(openCryptoFile);
360+
when(openCryptoFile.newFileChannel(any())).thenReturn(fileChannel);
361+
}
362+
363+
@Test
364+
@DisplayName("newFileChannel read-only")
365+
public void testNewFileChannelReadOnly() throws IOException {
366+
FileChannel ch = inTest.newFileChannel(cleartextPath, EnumSet.of(StandardOpenOption.READ));
367+
368+
Assertions.assertSame(fileChannel, ch);
369+
verify(readonlyFlag, Mockito.never()).assertWritable();
370+
}
371+
372+
@Test
373+
@DisplayName("newFileChannel read-only with long filename")
374+
public void testNewFileChannelReadOnlyShortened() throws IOException {
375+
FileChannel ch = inTest.newFileChannel(cleartextPath, EnumSet.of(StandardOpenOption.READ));
376+
377+
Assertions.assertSame(fileChannel, ch);
378+
verify(readonlyFlag, Mockito.never()).assertWritable();
379+
verify(ciphertextPath, Mockito.never()).persistLongFileName();
380+
}
381+
382+
@Test
383+
@DisplayName("newFileChannel read-write with long filename")
384+
public void testNewFileChannelReadWriteShortened() throws IOException {
385+
FileChannel ch = inTest.newFileChannel(cleartextPath, EnumSet.of(StandardOpenOption.WRITE));
386+
387+
Assertions.assertSame(fileChannel, ch);
388+
verify(readonlyFlag, Mockito.atLeastOnce()).assertWritable();
389+
verify(ciphertextPath).persistLongFileName();
390+
}
391+
392+
}
393+
343394
@Nested
344395
public class Delete {
345396

0 commit comments

Comments
 (0)