Skip to content

Commit 52ae98f

Browse files
Merge branch 'develop' into release/1.9.0
2 parents 93aed7a + 72ae360 commit 52ae98f

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
@@ -364,7 +364,9 @@ private FileChannel newFileChannel(CryptoPath cleartextFilePath, EffectiveOpenOp
364364
Files.createDirectories(ciphertextPath.getRawPath()); // suppresses FileAlreadyExists
365365
}
366366
FileChannel ch = openCryptoFiles.getOrCreate(ciphertextFilePath).newFileChannel(options); // might throw FileAlreadyExists
367-
ciphertextPath.persistLongFileName();
367+
if (options.writable()) {
368+
ciphertextPath.persistLongFileName();
369+
}
368370
return ch;
369371
}
370372
}

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;
@@ -339,6 +341,55 @@ public void testNewWatchServiceThrowsUnsupportedOperationException() throws IOEx
339341
});
340342
}
341343

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

0 commit comments

Comments
 (0)