Skip to content

Commit d5e238c

Browse files
committed
add Tests
1 parent b2ed90d commit d5e238c

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.google.common.base.Strings;
1212
import org.cryptomator.cryptofs.common.CiphertextFileType;
13+
import org.cryptomator.cryptofs.event.BrokenFileNodeEvent;
1314
import org.cryptomator.cryptofs.event.FilesystemEvent;
1415
import org.cryptomator.cryptolib.api.Cryptor;
1516
import org.cryptomator.cryptolib.api.FileNameCryptor;
@@ -18,6 +19,9 @@
1819
import org.junit.jupiter.api.DisplayName;
1920
import org.junit.jupiter.api.Nested;
2021
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;
2125
import org.mockito.ArgumentMatchers;
2226
import org.mockito.Mockito;
2327

@@ -33,6 +37,7 @@
3337
import java.util.stream.IntStream;
3438

3539
import static org.mockito.Mockito.mock;
40+
import static org.mockito.Mockito.verify;
3641

3742
public class CryptoPathMapperTest {
3843

@@ -339,7 +344,70 @@ public void testGetCiphertextFileTypeForShortenedFile() throws IOException {
339344
Assertions.assertEquals(CiphertextFileType.FILE, type);
340345
}
341346

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+
}
343411

344412
}
345413

0 commit comments

Comments
 (0)