Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit 412cd82

Browse files
restructured some tests
1 parent 670817f commit 412cd82

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

src/test/java/org/cryptomator/cloudaccess/vaultformat8/VaultFormat8IntegrationTest.java

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import org.cryptomator.cloudaccess.api.exceptions.VaultVerificationFailedException;
1313
import org.cryptomator.cloudaccess.api.exceptions.VaultVersionVerificationFailedException;
1414
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.Assumptions;
1516
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.DisplayName;
18+
import org.junit.jupiter.api.Nested;
1619
import org.junit.jupiter.api.Test;
1720
import org.junit.jupiter.api.io.TempDir;
1821

@@ -31,47 +34,63 @@ public class VaultFormat8IntegrationTest {
3134
private static final Duration TIMEOUT = Duration.ofMillis(100);
3235

3336
private CloudProvider localProvider;
34-
private CloudProvider encryptedProvider;
3537

3638
@BeforeEach
3739
public void setup(@TempDir Path tmpDir) throws IOException {
3840
this.localProvider = CloudAccess.toLocalFileSystem(tmpDir);
39-
var in = getClass().getResourceAsStream("/vaultconfig.jwt");
40-
localProvider.write(CloudPath.of("/vaultconfig.jwt"), false, in, in.available(), Optional.empty(), ProgressListener.NO_PROGRESS_AWARE).toCompletableFuture().join();
41-
this.encryptedProvider = CloudAccess.vaultFormat8GCMCloudAccess(localProvider, CloudPath.of("/"), new byte[64]);
4241
}
4342

44-
@Test
45-
public void testWriteThenReadFile() throws IOException {
46-
var path = CloudPath.of("/file.txt");
47-
var content = new byte[100_000];
48-
new Random(42l).nextBytes(content);
49-
50-
// write 100k
51-
var futureMetadata = encryptedProvider.write(path, true, new ByteArrayInputStream(content), content.length, Optional.empty(), ProgressListener.NO_PROGRESS_AWARE);
52-
Assertions.assertTimeoutPreemptively(TIMEOUT, () -> futureMetadata.toCompletableFuture().get());
53-
54-
// read all bytes
55-
var futureInputStream1 = encryptedProvider.read(path, ProgressListener.NO_PROGRESS_AWARE);
56-
var inputStream1 = Assertions.assertTimeoutPreemptively(TIMEOUT, () -> futureInputStream1.toCompletableFuture().get());
57-
Assertions.assertArrayEquals(content, inputStream1.readAllBytes());
58-
59-
// read partially
60-
var futureInputStream2 = encryptedProvider.read(path, 2000, 15000, ProgressListener.NO_PROGRESS_AWARE);
61-
var inputStream2 = Assertions.assertTimeoutPreemptively(TIMEOUT, () -> futureInputStream2.toCompletableFuture().get());
62-
Assertions.assertArrayEquals(Arrays.copyOfRange(content, 2000, 17000), inputStream2.readAllBytes());
43+
@Nested
44+
@DisplayName("with valid /vaultconfig.jwt")
45+
public class WithInitializedVaultConfig {
46+
47+
private CloudProvider encryptedProvider;
48+
49+
@BeforeEach
50+
public void setup() throws IOException {
51+
var in = getClass().getResourceAsStream("/vaultconfig.jwt");
52+
localProvider.write(CloudPath.of("/vaultconfig.jwt"), false, in, in.available(), Optional.empty(), ProgressListener.NO_PROGRESS_AWARE).toCompletableFuture().join();
53+
this.encryptedProvider = CloudAccess.vaultFormat8GCMCloudAccess(localProvider, CloudPath.of("/"), new byte[64]);
54+
}
55+
56+
@Test
57+
@DisplayName("read and write through encryption decorator")
58+
public void testWriteThenReadFile() throws IOException {
59+
var path = CloudPath.of("/file.txt");
60+
var content = new byte[100_000];
61+
new Random(42l).nextBytes(content);
62+
63+
// write 100k
64+
var futureMetadata = encryptedProvider.write(path, true, new ByteArrayInputStream(content), content.length, Optional.empty(), ProgressListener.NO_PROGRESS_AWARE);
65+
Assertions.assertTimeoutPreemptively(TIMEOUT, () -> futureMetadata.toCompletableFuture().get());
66+
67+
// read all bytes
68+
var futureInputStream1 = encryptedProvider.read(path, ProgressListener.NO_PROGRESS_AWARE);
69+
var inputStream1 = Assertions.assertTimeoutPreemptively(TIMEOUT, () -> futureInputStream1.toCompletableFuture().get());
70+
Assertions.assertArrayEquals(content, inputStream1.readAllBytes());
71+
72+
// read partially
73+
var futureInputStream2 = encryptedProvider.read(path, 2000, 15000, ProgressListener.NO_PROGRESS_AWARE);
74+
var inputStream2 = Assertions.assertTimeoutPreemptively(TIMEOUT, () -> futureInputStream2.toCompletableFuture().get());
75+
Assertions.assertArrayEquals(Arrays.copyOfRange(content, 2000, 17000), inputStream2.readAllBytes());
76+
}
77+
6378
}
6479

6580
@Test
81+
@DisplayName("init with missing /vaultconfig.jwt fails")
6682
public void testInstantiateFormat8GCMCloudAccessWithoutVaultConfigFile() {
67-
localProvider.deleteFile(CloudPath.of("/vaultconfig.jwt"));
83+
Assumptions.assumeFalse(localProvider.exists(CloudPath.of("/vaultconfig.jwt")).toCompletableFuture().join());
84+
6885
var exception = Assertions.assertThrows(CloudProviderException.class, () -> CloudAccess.vaultFormat8GCMCloudAccess(localProvider, CloudPath.of("/"), new byte[64]));
6986
Assertions.assertTrue(exception.getCause() instanceof NotFoundException);
7087
}
7188

7289
@Test
90+
@DisplayName("init with wrong format")
7391
public void testInstantiateFormat8GCMCloudAccessWithWrongVaultVersion() {
74-
localProvider.deleteFile(CloudPath.of("/vaultconfig.jwt"));
92+
Assumptions.assumeFalse(localProvider.exists(CloudPath.of("/vaultconfig.jwt")).toCompletableFuture().join());
93+
7594
byte[] masterkey = new byte[64];
7695
Algorithm algorithm = Algorithm.HMAC256(masterkey);
7796
var token = JWT.create()
@@ -86,8 +105,10 @@ public void testInstantiateFormat8GCMCloudAccessWithWrongVaultVersion() {
86105
}
87106

88107
@Test
108+
@DisplayName("init with invalid cipherCombo fails")
89109
public void testInstantiateFormat8GCMCloudAccessWithWrongCiphermode() {
90-
localProvider.deleteFile(CloudPath.of("/vaultconfig.jwt"));
110+
Assumptions.assumeFalse(localProvider.exists(CloudPath.of("/vaultconfig.jwt")).toCompletableFuture().join());
111+
91112
byte[] masterkey = new byte[64];
92113
Algorithm algorithm = Algorithm.HMAC256(masterkey);
93114
var token = JWT.create()
@@ -102,8 +123,10 @@ public void testInstantiateFormat8GCMCloudAccessWithWrongCiphermode() {
102123
}
103124

104125
@Test
126+
@DisplayName("init with wrong key")
105127
public void testInstantiateFormat8GCMCloudAccessWithWrongKey() {
106-
localProvider.deleteFile(CloudPath.of("/vaultconfig.jwt"));
128+
Assumptions.assumeFalse(localProvider.exists(CloudPath.of("/vaultconfig.jwt")).toCompletableFuture().join());
129+
107130
byte[] masterkey = new byte[64];
108131
Arrays.fill(masterkey, (byte) 15);
109132
Algorithm algorithm = Algorithm.HMAC256(masterkey);

0 commit comments

Comments
 (0)