Skip to content

Commit

Permalink
add reproducer test
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Nov 20, 2023
1 parent 4444bd6 commit 0e46ad5
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/test/java/org/cryptomator/cryptofs/CryptoPathMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.cryptomator.cryptolib.api.FileNameCryptor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
Expand All @@ -26,6 +27,8 @@
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class CryptoPathMapperTest {

Expand Down Expand Up @@ -133,6 +136,44 @@ public void testPathEncryptionForFooBar() throws IOException {
Assertions.assertEquals(d0002, path);
}

@DisplayName("Decrypts a deeply nested filepath")
@Test
public void testPathEncryptionDEEP() throws IOException {
Path d00 = Mockito.mock(Path.class, "d/00/");
Mockito.when(dataRoot.resolve("00")).thenReturn(d00);
Mockito.when(fileNameCryptor.hashDirectoryId("")).thenReturn("0000");

int maxNestingDepth = 9;

for (int depth=0; depth < maxNestingDepth; depth++) {
var dirId = "%02d".formatted(depth);
var nextDirId = "%02d".formatted(depth+1);
String pathPrefix = "d/00/"+dirId;
Path contentDir = Mockito.mock(Path.class, pathPrefix);
Path dirLinkDir = Mockito.mock(Path.class, pathPrefix+"/cipherDir"+dirId+".c9r");
Path dirLinkFile = Mockito.mock(Path.class, pathPrefix + "/cipherDir"+dirId+".c9r/dir.c9r");
Mockito.when(d00.resolve(dirId)).thenReturn(contentDir);
Mockito.when(contentDir.resolve("cipherDir"+dirId+".c9r")).thenReturn(dirLinkDir);
Mockito.when(dirLinkDir.resolve("dir.c9r")).thenReturn(dirLinkFile);
Mockito.when(fileNameCryptor.encryptFilename(Mockito.any(), Mockito.eq(dirId), Mockito.any())).thenReturn("cipherDir"+dirId);
Mockito.when(dirIdProvider.load(dirLinkFile)).thenReturn(nextDirId);
Mockito.when(fileNameCryptor.hashDirectoryId(nextDirId)).thenReturn("%04d".formatted(depth+1));
}

var finalDirId = "%02d".formatted(maxNestingDepth);
Path finalContentDir = Mockito.mock(Path.class, "d/00/"+finalDirId);
Path finalEncryptedFile = Mockito.mock(Path.class, "d/00/"+finalDirId+"/file.c9r");
Mockito.when(d00.resolve(finalDirId)).thenReturn(finalContentDir);
Mockito.when(finalContentDir.resolve("file.c9r")).thenReturn(finalEncryptedFile);
Mockito.when(fileNameCryptor.encryptFilename(Mockito.any(), Mockito.eq("cleartextFile"), Mockito.any())).thenReturn("file");

var testPath ="/"+ IntStream.range(0,maxNestingDepth).mapToObj("%02d"::formatted).collect(Collectors.joining("/"))+"/cleartextFile";

CryptoPathMapper mapper = new CryptoPathMapper(pathToVault, cryptor, dirIdProvider, longFileNameProvider, vaultConfig);
Path path = mapper.getCiphertextFilePath(fileSystem.getPath(testPath)).getRawPath();
Assertions.assertEquals(finalEncryptedFile, path);
}

@Test
public void testPathEncryptionForFooBarBaz() throws IOException {
Path d00 = Mockito.mock(Path.class, "d/00/");
Expand Down Expand Up @@ -169,7 +210,6 @@ public void testPathEncryptionForFooBarBaz() throws IOException {
Path path = mapper.getCiphertextFilePath(fileSystem.getPath("/foo/bar/baz")).getRawPath();
Assertions.assertEquals(d0002zab, path);
}

@Nested
public class GetCiphertextFileType {

Expand Down

0 comments on commit 0e46ad5

Please sign in to comment.