Skip to content

Commit 971c1a8

Browse files
committed
Add unit tests and update doc
1 parent 56073b8 commit 971c1a8

2 files changed

Lines changed: 90 additions & 6 deletions

File tree

src/main/java/org/cryptomator/cryptofs/CryptoFileSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public abstract class CryptoFileSystem extends FileSystem {
3333
/**
3434
* Provides the {@link Path} to the (data) ciphertext from a given cleartext path.
3535
*
36-
* @param cleartextPath path to the cleartext file or folder belonging to this {@link CryptoFileSystem}. Internally the path must be an instance of {@link CryptoPath}
36+
* @param cleartextPath absolute path to the cleartext file or folder belonging to this {@link CryptoFileSystem}. Internally the path must be an instance of {@link CryptoPath}
3737
* @return the {@link Path} to ciphertext file or folder containing teh actual encrypted data
3838
* @throws IOException
3939
*/

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

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.nio.file.NoSuchFileException;
4343
import java.nio.file.Path;
4444
import java.nio.file.PathMatcher;
45+
import java.nio.file.ProviderMismatchException;
4546
import java.nio.file.StandardCopyOption;
4647
import java.nio.file.StandardOpenOption;
4748
import java.nio.file.attribute.BasicFileAttributeView;
@@ -119,11 +120,11 @@ public void setup() {
119120

120121
when(fileSystemProperties.maxCleartextNameLength()).thenReturn(32768);
121122

122-
inTest = new CryptoFileSystemImpl(provider, cryptoFileSystems, pathToVault, cryptor,
123-
fileStore, stats, cryptoPathMapper, cryptoPathFactory,
124-
pathMatcherFactory, directoryStreamFactory, dirIdProvider, dirIdBackup,
125-
fileAttributeProvider, fileAttributeByNameProvider, fileAttributeViewProvider,
126-
openCryptoFiles, symlinks, finallyUtil, ciphertextDirDeleter, readonlyFlag,
123+
inTest = new CryptoFileSystemImpl(provider, cryptoFileSystems, pathToVault, cryptor, //
124+
fileStore, stats, cryptoPathMapper, cryptoPathFactory, //
125+
pathMatcherFactory, directoryStreamFactory, dirIdProvider, dirIdBackup, //
126+
fileAttributeProvider, fileAttributeByNameProvider, fileAttributeViewProvider, //
127+
openCryptoFiles, symlinks, finallyUtil, ciphertextDirDeleter, readonlyFlag, //
127128
fileSystemProperties);
128129
}
129130

@@ -188,6 +189,89 @@ public void testGetFileStoresReturnsFileStore() {
188189
Assertions.assertSame(fileStore, inTest.getFileStore());
189190
}
190191

192+
@Nested
193+
public class PathToDataCiphertext {
194+
195+
@Test
196+
@DisplayName("Getting data ciphertext path of directory returns ciphertext content dir")
197+
public void testCleartextDirectory() throws IOException {
198+
Path ciphertext = Mockito.mock(Path.class, "/d/AB/CD...XYZ/");
199+
Path cleartext = inTest.getPath("/");
200+
try (var cryptoPathMock = Mockito.mockStatic(CryptoPath.class)) {
201+
cryptoPathMock.when(() -> CryptoPath.castAndAssertAbsolute(any())).thenReturn(cleartext);
202+
when(cryptoPathMapper.getCiphertextFileType(any())).thenReturn(CiphertextFileType.DIRECTORY);
203+
when(cryptoPathMapper.getCiphertextDir(any())).thenReturn(new CiphertextDirectory("foo", ciphertext));
204+
205+
Path result = inTest.getPathToDataCiphertext(cleartext);
206+
Assertions.assertEquals(ciphertext, result);
207+
}
208+
}
209+
210+
@Test
211+
@DisplayName("Getting data ciphertext path of file returns ciphertext file")
212+
public void testCleartextFile() throws IOException {
213+
Path ciphertext = Mockito.mock(Path.class, "/d/AB/CD..XYZ/foo.c9r");
214+
Path cleartext = inTest.getPath("/foo.bar");
215+
try (var cryptoPathMock = Mockito.mockStatic(CryptoPath.class)) {
216+
CiphertextFilePath p = Mockito.mock(CiphertextFilePath.class);
217+
cryptoPathMock.when(() -> CryptoPath.castAndAssertAbsolute(any())).thenReturn(cleartext);
218+
when(cryptoPathMapper.getCiphertextFileType(any())).thenReturn(CiphertextFileType.FILE);
219+
when(cryptoPathMapper.getCiphertextFilePath(any())).thenReturn(p);
220+
when(p.getFilePath()).thenReturn(ciphertext);
221+
222+
Path result = inTest.getPathToDataCiphertext(cleartext);
223+
Assertions.assertEquals(ciphertext, result);
224+
}
225+
}
226+
227+
@Test
228+
@DisplayName("Getting data ciphertext path of symlink returns ciphertext symlink.c9r")
229+
public void testCleartextSymlink() throws IOException {
230+
Path ciphertext = Mockito.mock(Path.class, "/d/AB/CD..XYZ/foo.c9s/symlink.c9r");
231+
Path cleartext = inTest.getPath("/foo.bar");
232+
try (var cryptoPathMock = Mockito.mockStatic(CryptoPath.class)) {
233+
CiphertextFilePath p = Mockito.mock(CiphertextFilePath.class);
234+
cryptoPathMock.when(() -> CryptoPath.castAndAssertAbsolute(any())).thenReturn(cleartext);
235+
when(cryptoPathMapper.getCiphertextFileType(any())).thenReturn(CiphertextFileType.SYMLINK);
236+
when(cryptoPathMapper.getCiphertextFilePath(any())).thenReturn(p);
237+
when(p.getSymlinkFilePath()).thenReturn(ciphertext);
238+
239+
Path result = inTest.getPathToDataCiphertext(cleartext);
240+
Assertions.assertEquals(ciphertext, result);
241+
}
242+
}
243+
244+
@Test
245+
@DisplayName("Path not pointing into the vault throws exception")
246+
public void testForeignPathThrows() throws IOException {
247+
Path cleartext = Mockito.mock(Path.class, "/some.file");
248+
Assertions.assertThrows(ProviderMismatchException.class, () -> inTest.getPathToDataCiphertext(cleartext));
249+
}
250+
251+
@Test
252+
@DisplayName("Not existing resource throws NoSuchFileException")
253+
public void testNoSuchFile() throws IOException {
254+
Path cleartext = inTest.getPath("/i-do-not-exist");
255+
try (var cryptoPathMock = Mockito.mockStatic(CryptoPath.class)) {
256+
cryptoPathMock.when(() -> CryptoPath.castAndAssertAbsolute(any())).thenReturn(cleartext);
257+
when(cryptoPathMapper.getCiphertextFileType(any())).thenThrow(new NoSuchFileException("no such file"));
258+
259+
Assertions.assertThrows(NoSuchFileException.class, () -> inTest.getPathToDataCiphertext(cleartext));
260+
}
261+
}
262+
263+
@Test
264+
@DisplayName("Relative cleartext path throws exception")
265+
public void testRelativePathException() throws IOException {
266+
Path cleartext = inTest.getPath("relative/path");
267+
try (var cryptoPathMock = Mockito.mockStatic(CryptoPath.class)) {
268+
cryptoPathMock.when(() -> CryptoPath.castAndAssertAbsolute(any())).thenThrow(new IllegalArgumentException());
269+
270+
Assertions.assertThrows(IllegalArgumentException.class, () -> inTest.getPathToDataCiphertext(cleartext));
271+
}
272+
}
273+
}
274+
191275
@Nested
192276
public class CloseAndIsOpen {
193277

0 commit comments

Comments
 (0)