Skip to content

Commit 6901c32

Browse files
committed
Extracted InMemory-Tests to own class
1 parent fbe79f7 commit 6901c32

File tree

2 files changed

+88
-62
lines changed

2 files changed

+88
-62
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 file")
63+
public void testReplaceExistingShortenedFile() throws IOException {
64+
try (var fs = setupCryptoFs(50, 100, false)) {
65+
var fiftyCharName2 = "/50char2_50char2_50char2_50char2_50char2_50char.txt"; //since filename encryption increases filename length, 50 cleartext chars are sufficient
66+
var source = fs.getPath("/source.txt");
67+
var target = fs.getPath(fiftyCharName2);
68+
Files.createFile(source);
69+
Files.createFile(target);
70+
71+
assertDoesNotThrow(() -> Files.move(source, target, REPLACE_EXISTING));
72+
assertTrue(Files.notExists(source));
73+
assertTrue(Files.exists(target));
74+
}
75+
}
76+
77+
private FileSystem setupCryptoFs(int ciphertextShorteningThreshold, int maxCleartextFilename, boolean readonly) throws IOException {
78+
byte[] key = new byte[64];
79+
Arrays.fill(key, (byte) 0x55);
80+
var keyLoader = Mockito.mock(MasterkeyLoader.class);
81+
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(key));
82+
var properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoader(keyLoader).withShorteningThreshold(ciphertextShorteningThreshold).withMaxCleartextNameLength(maxCleartextFilename).withFlags(readonly ? Set.of(CryptoFileSystemProperties.FileSystemFlags.READONLY) : Set.of()).build();
83+
CryptoFileSystemProvider.initialize(pathToVault, properties, URI.create("test:key"));
84+
URI fsUri = CryptoFileSystemUri.create(pathToVault);
85+
return FileSystems.newFileSystem(fsUri, cryptoFileSystemProperties().withKeyLoader(keyLoader).build());
86+
}
87+
88+
}

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

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -589,68 +589,6 @@ public void testMoveFileFromOneCryptoFileSystemToAnother() throws IOException {
589589

590590
}
591591

592-
593-
@Nested
594-
public class InMemory {
595-
596-
private static FileSystem tmpFs;
597-
private static Path pathToVault;
598-
599-
@BeforeAll
600-
public static void beforeAll() {
601-
tmpFs = Jimfs.newFileSystem(Configuration.unix());
602-
pathToVault = tmpFs.getPath("/vault");
603-
}
604-
605-
@BeforeEach
606-
public void beforeEach() throws IOException {
607-
Files.createDirectory(pathToVault);
608-
}
609-
610-
@AfterEach
611-
public void afterEach() throws IOException {
612-
try (var paths = Files.walk(pathToVault)) {
613-
var nodes = paths.sorted(Comparator.reverseOrder()).toList();
614-
for (var node : nodes) {
615-
Files.delete(node);
616-
}
617-
}
618-
}
619-
620-
@AfterAll
621-
public static void afterAll() throws IOException {
622-
tmpFs.close();
623-
}
624-
625-
@Test
626-
@DisplayName("Replace an existing, shortened file")
627-
public void testReplaceExistingShortenedFile() throws IOException {
628-
try (var fs = setupCryptoFs(50, 100, false)) {
629-
var fiftyCharName2 = "/50char2_50char2_50char2_50char2_50char2_50char.txt"; //since filename encryption increases filename length, 50 cleartext chars are sufficient
630-
var source = fs.getPath("/source.txt");
631-
var target = fs.getPath(fiftyCharName2);
632-
Files.createFile(source);
633-
Files.createFile(target);
634-
635-
assertDoesNotThrow(() -> Files.move(source, target, REPLACE_EXISTING));
636-
assertTrue(Files.notExists(source));
637-
assertTrue(Files.exists(target));
638-
}
639-
}
640-
641-
private FileSystem setupCryptoFs(int ciphertextShorteningThreshold, int maxCleartextFilename, boolean readonly) throws IOException {
642-
byte[] key = new byte[64];
643-
Arrays.fill(key, (byte) 0x55);
644-
var keyLoader = Mockito.mock(MasterkeyLoader.class);
645-
Mockito.when(keyLoader.loadKey(Mockito.any())).thenAnswer(ignored -> new Masterkey(key));
646-
var properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withKeyLoader(keyLoader).withShorteningThreshold(ciphertextShorteningThreshold).withMaxCleartextNameLength(maxCleartextFilename).withFlags(readonly ? Set.of(CryptoFileSystemProperties.FileSystemFlags.READONLY) : Set.of()).build();
647-
CryptoFileSystemProvider.initialize(pathToVault, properties, URI.create("test:key"));
648-
URI fsUri = CryptoFileSystemUri.create(pathToVault);
649-
return FileSystems.newFileSystem(fsUri, cryptoFileSystemProperties().withKeyLoader(keyLoader).build());
650-
}
651-
652-
}
653-
654592
@Nested
655593
@EnabledOnOs({OS.MAC, OS.LINUX})
656594
@TestInstance(TestInstance.Lifecycle.PER_CLASS)

0 commit comments

Comments
 (0)