|
| 1 | +package org.cryptomator.cryptofs; |
| 2 | + |
| 3 | +import com.google.common.jimfs.Configuration; |
| 4 | +import com.google.common.jimfs.Jimfs; |
| 5 | +import org.cryptomator.cryptolib.api.Masterkey; |
| 6 | +import org.cryptomator.cryptolib.api.MasterkeyLoader; |
| 7 | +import org.junit.jupiter.api.AfterAll; |
| 8 | +import org.junit.jupiter.api.AfterEach; |
| 9 | +import org.junit.jupiter.api.BeforeAll; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.DisplayName; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.mockito.Mockito; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.net.URI; |
| 17 | +import java.nio.file.FileSystem; |
| 18 | +import java.nio.file.FileSystems; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; |
| 26 | +import static org.cryptomator.cryptofs.CryptoFileSystemProperties.cryptoFileSystemProperties; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 29 | + |
| 30 | +public class CryptoFileSystemProviderInMemoryIntegrationTest { |
| 31 | + |
| 32 | + private static FileSystem tmpFs; |
| 33 | + private static Path pathToVault; |
| 34 | + |
| 35 | + @BeforeAll |
| 36 | + public static void beforeAll() { |
| 37 | + tmpFs = Jimfs.newFileSystem(Configuration.unix()); |
| 38 | + pathToVault = tmpFs.getPath("/vault"); |
| 39 | + } |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + public void beforeEach() throws IOException { |
| 43 | + Files.createDirectory(pathToVault); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterEach |
| 47 | + public void afterEach() throws IOException { |
| 48 | + try (var paths = Files.walk(pathToVault)) { |
| 49 | + var nodes = paths.sorted(Comparator.reverseOrder()).toList(); |
| 50 | + for (var node : nodes) { |
| 51 | + Files.delete(node); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @AfterAll |
| 57 | + public static void afterAll() throws IOException { |
| 58 | + tmpFs.close(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @DisplayName("Replace an existing, shortened, empty directory") |
| 63 | + public void testReplaceExistingShortenedDirEmpty() throws IOException { |
| 64 | + try (var fs = setupCryptoFs(50, 100, false)) { |
| 65 | + var dirName50Chars = "/target_89_123456789_123456789_123456789_123456789_"; //since filename encryption increases filename length, 50 cleartext chars are sufficient |
| 66 | + var source = fs.getPath("/sourceDir"); |
| 67 | + var target = fs.getPath(dirName50Chars); |
| 68 | + Files.createDirectory(source); |
| 69 | + Files.createDirectory(target); |
| 70 | + assertDoesNotThrow(() -> Files.move(source, target, REPLACE_EXISTING)); |
| 71 | + assertTrue(Files.notExists(source)); |
| 72 | + assertTrue(Files.exists(target)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + @DisplayName("Replace an existing, shortened file") |
| 78 | + public void testReplaceExistingShortenedFile() throws IOException { |
| 79 | + try (var fs = setupCryptoFs(50, 100, false)) { |
| 80 | + var fiftyCharName2 = "/50char2_50char2_50char2_50char2_50char2_50char.txt"; //since filename encryption increases filename length, 50 cleartext chars are sufficient |
| 81 | + var source = fs.getPath("/source.txt"); |
| 82 | + var target = fs.getPath(fiftyCharName2); |
| 83 | + Files.createFile(source); |
| 84 | + Files.createFile(target); |
| 85 | + |
| 86 | + assertDoesNotThrow(() -> Files.move(source, target, REPLACE_EXISTING)); |
| 87 | + assertTrue(Files.notExists(source)); |
| 88 | + assertTrue(Files.exists(target)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private FileSystem setupCryptoFs(int ciphertextShorteningThreshold, int maxCleartextFilename, boolean readonly) throws IOException { |
| 93 | + byte[] key = new byte[64]; |
| 94 | + Arrays.fill(key, (byte) 0x55); |
| 95 | + var keyLoader = Mockito.mock(MasterkeyLoader.class); |
| 96 | + Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(key)); |
| 97 | + var properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoader(keyLoader).withShorteningThreshold(ciphertextShorteningThreshold).withMaxCleartextNameLength(maxCleartextFilename).withFlags(readonly ? Set.of(CryptoFileSystemProperties.FileSystemFlags.READONLY) : Set.of()).build(); |
| 98 | + CryptoFileSystemProvider.initialize(pathToVault, properties, URI.create("test:key")); |
| 99 | + URI fsUri = CryptoFileSystemUri.create(pathToVault); |
| 100 | + return FileSystems.newFileSystem(fsUri, cryptoFileSystemProperties().withKeyLoader(keyLoader).build()); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments