|
10 | 10 |
|
11 | 11 | import com.google.common.base.Strings; |
12 | 12 | import org.cryptomator.cryptofs.common.CiphertextFileType; |
| 13 | +import org.cryptomator.cryptofs.event.BrokenFileNodeEvent; |
13 | 14 | import org.cryptomator.cryptofs.event.FilesystemEvent; |
14 | 15 | import org.cryptomator.cryptolib.api.Cryptor; |
15 | 16 | import org.cryptomator.cryptolib.api.FileNameCryptor; |
|
18 | 19 | import org.junit.jupiter.api.DisplayName; |
19 | 20 | import org.junit.jupiter.api.Nested; |
20 | 21 | import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.CsvSource; |
| 24 | +import org.mockito.ArgumentMatcher; |
21 | 25 | import org.mockito.ArgumentMatchers; |
22 | 26 | import org.mockito.Mockito; |
23 | 27 |
|
|
33 | 37 | import java.util.stream.IntStream; |
34 | 38 |
|
35 | 39 | import static org.mockito.Mockito.mock; |
| 40 | +import static org.mockito.Mockito.verify; |
36 | 41 |
|
37 | 42 | public class CryptoPathMapperTest { |
38 | 43 |
|
@@ -339,7 +344,70 @@ public void testGetCiphertextFileTypeForShortenedFile() throws IOException { |
339 | 344 | Assertions.assertEquals(CiphertextFileType.FILE, type); |
340 | 345 | } |
341 | 346 |
|
342 | | - //TODO: Tests for determining filetype order and failure including event emit |
| 347 | + @DisplayName("Test ciphertextFileType detection priority") |
| 348 | + @ParameterizedTest |
| 349 | + @CsvSource(value = {"true, true, true", "true, false, true", "true, true, false", "false, true, true"}) |
| 350 | + public void testDetectionPriority(boolean dirFileExists, boolean symlinkFileExists, boolean contentsFileExists) throws IOException { |
| 351 | + Mockito.when(underlyingFileSystemProvider.readAttributes(c9rPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenReturn(c9rAttrs); |
| 352 | + Mockito.when(c9rAttrs.isDirectory()).thenReturn(true); |
| 353 | + var dirFileAttr = Mockito.mock(BasicFileAttributes.class); |
| 354 | + var symlinkFileAttr = Mockito.mock(BasicFileAttributes.class); |
| 355 | + var contentsFileAttr = Mockito.mock(BasicFileAttributes.class); |
| 356 | + if (dirFileExists) { |
| 357 | + Mockito.when(underlyingFileSystemProvider.readAttributes(dirFilePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenReturn(dirFileAttr); |
| 358 | + } else { |
| 359 | + Mockito.when(underlyingFileSystemProvider.readAttributes(dirFilePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenThrow(NoSuchFileException.class); |
| 360 | + } |
| 361 | + if (symlinkFileExists) { |
| 362 | + Mockito.when(underlyingFileSystemProvider.readAttributes(symlinkFilePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenReturn(symlinkFileAttr); |
| 363 | + } else { |
| 364 | + Mockito.when(underlyingFileSystemProvider.readAttributes(symlinkFilePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenThrow(NoSuchFileException.class); |
| 365 | + } |
| 366 | + if (contentsFileExists) { |
| 367 | + Mockito.when(underlyingFileSystemProvider.readAttributes(contentsFilePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenReturn(contentsFileAttr); |
| 368 | + } else { |
| 369 | + Mockito.when(underlyingFileSystemProvider.readAttributes(contentsFilePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenThrow(NoSuchFileException.class); |
| 370 | + } |
| 371 | + |
| 372 | + CiphertextFileType expectedType; |
| 373 | + if (dirFileExists) { |
| 374 | + expectedType = CiphertextFileType.DIRECTORY; |
| 375 | + } else if (symlinkFileExists) { |
| 376 | + expectedType = CiphertextFileType.SYMLINK; |
| 377 | + } else { |
| 378 | + expectedType = CiphertextFileType.FILE; |
| 379 | + } |
| 380 | + |
| 381 | + Mockito.when(underlyingFileSystemProvider.exists(dirFilePath, LinkOption.NOFOLLOW_LINKS)).thenReturn(dirFileExists); |
| 382 | + Mockito.when(underlyingFileSystemProvider.exists(symlinkFilePath, LinkOption.NOFOLLOW_LINKS)).thenReturn(symlinkFileExists); |
| 383 | + Mockito.when(underlyingFileSystemProvider.exists(contentsFilePath, LinkOption.NOFOLLOW_LINKS)).thenReturn(contentsFileExists); |
| 384 | + |
| 385 | + CryptoPathMapper mapper = new CryptoPathMapper(pathToVault, cryptor, dirIdProvider, longFileNameProvider, vaultConfig, eventConsumer); |
| 386 | + |
| 387 | + CryptoPath path = fileSystem.getPath("/CLEAR"); |
| 388 | + CiphertextFileType type = mapper.getCiphertextFileType(path); |
| 389 | + Assertions.assertEquals(expectedType, type); |
| 390 | + } |
| 391 | + |
| 392 | + @Test |
| 393 | + @DisplayName("Throw NoSuchFileException if no known file exists") |
| 394 | + public void testNoKnownFileExists() throws IOException { |
| 395 | + Mockito.when(underlyingFileSystemProvider.readAttributes(c9rPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenReturn(c9rAttrs); |
| 396 | + Mockito.when(c9rAttrs.isDirectory()).thenReturn(true); |
| 397 | + Mockito.when(underlyingFileSystemProvider.readAttributes(dirFilePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenThrow(NoSuchFileException.class); |
| 398 | + Mockito.when(underlyingFileSystemProvider.readAttributes(symlinkFilePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenThrow(NoSuchFileException.class); |
| 399 | + Mockito.when(underlyingFileSystemProvider.readAttributes(contentsFilePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenThrow(NoSuchFileException.class); |
| 400 | + Mockito.when(underlyingFileSystemProvider.exists(dirFilePath, LinkOption.NOFOLLOW_LINKS)).thenReturn(false); |
| 401 | + Mockito.when(underlyingFileSystemProvider.exists(symlinkFilePath, LinkOption.NOFOLLOW_LINKS)).thenReturn(false); |
| 402 | + Mockito.when(underlyingFileSystemProvider.exists(contentsFilePath, LinkOption.NOFOLLOW_LINKS)).thenReturn(false); |
| 403 | + |
| 404 | + CryptoPathMapper mapper = new CryptoPathMapper(pathToVault, cryptor, dirIdProvider, longFileNameProvider, vaultConfig, eventConsumer); |
| 405 | + |
| 406 | + CryptoPath path = fileSystem.getPath("/CLEAR"); |
| 407 | + Assertions.assertThrows(NoSuchFileException.class, () -> mapper.getCiphertextFileType(path)); |
| 408 | + var isBrokenFileNodeEvent = (ArgumentMatcher<FilesystemEvent>) ev -> ev instanceof BrokenFileNodeEvent; |
| 409 | + verify(eventConsumer).accept(ArgumentMatchers.argThat(isBrokenFileNodeEvent)); |
| 410 | + } |
343 | 411 |
|
344 | 412 | } |
345 | 413 |
|
|
0 commit comments