Skip to content

Commit b65c0ea

Browse files
committed
add more tests to RealUseToken
1 parent fe9bbc7 commit b65c0ea

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/test/java/org/cryptomator/cryptofs/inuse/RealUseTokenTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
import org.junit.jupiter.api.Test;
1212
import org.junit.jupiter.api.io.TempDir;
1313

14+
import java.io.ByteArrayInputStream;
1415
import java.io.IOException;
1516
import java.nio.file.Files;
1617
import java.nio.file.Path;
1718
import java.nio.file.StandardOpenOption;
1819
import java.nio.file.StandardWatchEventKinds;
1920
import java.nio.file.WatchService;
2021
import java.time.Duration;
22+
import java.time.Instant;
23+
import java.util.Properties;
2124
import java.util.concurrent.CompletableFuture;
2225
import java.util.concurrent.ConcurrentHashMap;
2326
import java.util.concurrent.ConcurrentMap;
@@ -29,6 +32,8 @@
2932
import static org.mockito.ArgumentMatchers.eq;
3033
import static org.mockito.Mockito.doAnswer;
3134
import static org.mockito.Mockito.mock;
35+
import static org.mockito.Mockito.never;
36+
import static org.mockito.Mockito.verify;
3237

3338
public class RealUseTokenTest {
3439

@@ -75,6 +80,21 @@ public void testFileCreation() {
7580
Assertions.assertTrue(Files.notExists(filePath));
7681
}
7782

83+
@Test
84+
@DisplayName("The properties file contains required keys with valid content")
85+
public void testFileContent() throws IOException {
86+
var filePath = tmpDir.resolve("inUse.file");
87+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, StandardOpenOption.CREATE_NEW, encWrapper)) {
88+
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(() -> Files.exists(filePath));
89+
90+
var props = new Properties();
91+
var rawProps = Files.readAllBytes(filePath);
92+
props.load(new ByteArrayInputStream(rawProps));
93+
Assertions.assertEquals("test3000", props.getProperty(UseToken.OWNER_KEY));
94+
Assertions.assertDoesNotThrow(() -> Instant.parse(props.getProperty(UseToken.LASTUPDATED_KEY)));
95+
}
96+
}
97+
7898
@Test
7999
@DisplayName("After X seconds of token creation, a file is updated")
80100
public void testFileSteal() throws IOException {
@@ -198,4 +218,40 @@ public void testMoveToClosed() throws IOException {
198218
Assertions.assertNull(useTokens.get(targetPath));
199219
}
200220
}
221+
222+
@Test
223+
@DisplayName("After token persisting, refreshing a token modifies content")
224+
public void testFileRefresh() throws IOException {
225+
var filePath = tmpDir.resolve("inUse.file");
226+
227+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, StandardOpenOption.CREATE_NEW, encWrapper)) {
228+
Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(() -> Files.exists(filePath));
229+
230+
var props = new Properties();
231+
var rawProps = Files.readAllBytes(filePath);
232+
props.load(new ByteArrayInputStream(rawProps));
233+
var oldLastUpdated = Instant.parse(props.getProperty(UseToken.LASTUPDATED_KEY));
234+
235+
token.refresh();
236+
237+
var props2 = new Properties();
238+
rawProps = Files.readAllBytes(filePath);
239+
props2.load(new ByteArrayInputStream(rawProps));
240+
241+
Assertions.assertEquals("test3000", props2.getProperty(UseToken.OWNER_KEY));
242+
var newLastUpdated = Instant.parse(props2.getProperty(UseToken.LASTUPDATED_KEY));
243+
Assertions.assertTrue(newLastUpdated.isAfter(oldLastUpdated));
244+
}
245+
}
246+
247+
@Test
248+
@DisplayName("Before token persisting, refreshing a token does nothing")
249+
public void testFileRefreshSkip() throws IOException {
250+
var filePath = tmpDir.resolve("inUse.file");
251+
252+
try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, StandardOpenOption.CREATE_NEW, encWrapper)) {
253+
token.refresh();
254+
verify(encWrapper, never()).wrapWithEncryption(any(), eq(cryptor));
255+
}
256+
}
201257
}

0 commit comments

Comments
 (0)