|
10 | 10 | import org.cryptomator.cryptofs.common.RunnableThrowingException; |
11 | 11 | import org.cryptomator.cryptofs.dir.CiphertextDirectoryDeleter; |
12 | 12 | import org.cryptomator.cryptofs.dir.DirectoryStreamFactory; |
| 13 | +import org.cryptomator.cryptofs.fh.OpenCryptoFile; |
13 | 14 | import org.cryptomator.cryptofs.fh.OpenCryptoFiles; |
14 | 15 | import org.cryptomator.cryptofs.fh.OpenCryptoFiles.TwoPhaseMove; |
15 | 16 | import org.cryptomator.cryptofs.mocks.FileChannelMock; |
|
18 | 19 | import org.hamcrest.MatcherAssert; |
19 | 20 | import org.junit.jupiter.api.Assertions; |
20 | 21 | import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.DisplayName; |
21 | 23 | import org.junit.jupiter.api.Nested; |
22 | 24 | import org.junit.jupiter.api.Test; |
23 | 25 | import org.mockito.Mockito; |
@@ -339,6 +341,55 @@ public void testNewWatchServiceThrowsUnsupportedOperationException() throws IOEx |
339 | 341 | }); |
340 | 342 | } |
341 | 343 |
|
| 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 | + |
342 | 393 | @Nested |
343 | 394 | public class Delete { |
344 | 395 |
|
|
0 commit comments