Skip to content

Commit 91fe9f2

Browse files
Fixes #63
1 parent 46eadee commit 91fe9f2

File tree

2 files changed

+54
-71
lines changed

2 files changed

+54
-71
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,15 @@ public ReadonlyFlag(CryptoFileSystemProperties properties, @PathToVault Path pat
2222
if (properties.readonly()) {
2323
LOG.info("Vault opened readonly.");
2424
readonly = true;
25-
} else if (targetFileStoreIsReadonly(pathToVault)) {
26-
LOG.warn("Vault on readonly filesystem.");
25+
} else if (!Files.isWritable(pathToVault)) {
26+
LOG.warn("Vault directory is write-protected.");
2727
readonly = true;
2828
} else {
2929
LOG.debug("Vault opened for read and write.");
3030
readonly = false;
3131
}
3232
}
3333

34-
private boolean targetFileStoreIsReadonly(Path pathToVault) {
35-
try {
36-
return Files.getFileStore(pathToVault).isReadOnly();
37-
} catch (IOException e) {
38-
throw new UncheckedIOException(e);
39-
}
40-
}
41-
4234
public void assertWritable() throws ReadOnlyFileSystemException {
4335
if (readonly) {
4436
throw new ReadOnlyFileSystemException();
Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package org.cryptomator.cryptofs;
22

3+
import org.hamcrest.CoreMatchers;
4+
import org.hamcrest.MatcherAssert;
35
import org.junit.jupiter.api.Assertions;
46
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.DisplayName;
58
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.CsvSource;
11+
import org.mockito.Mockito;
612

713
import java.io.IOException;
14+
import java.nio.file.AccessDeniedException;
15+
import java.nio.file.AccessMode;
816
import java.nio.file.FileStore;
917
import java.nio.file.FileSystem;
1018
import java.nio.file.Path;
@@ -19,79 +27,62 @@ public class ReadonlyFlagTest {
1927
private FileStore fileStore = mock(FileStore.class);
2028
private FileSystemProvider provider = mock(FileSystemProvider.class);
2129
private FileSystem fileSystem = mock(FileSystem.class);
22-
private Path path = mock(Path.class);
30+
private Path path = mock(Path.class, "test-path");
2331

2432
private CryptoFileSystemProperties properties = mock(CryptoFileSystemProperties.class);
2533

26-
private ReadonlyFlag inTest;
27-
2834
@BeforeEach
2935
public void setup() throws IOException {
3036
when(path.getFileSystem()).thenReturn(fileSystem);
3137
when(fileSystem.provider()).thenReturn(provider);
3238
when(provider.getFileStore(path)).thenReturn(fileStore);
3339
}
3440

35-
@Test
36-
public void testReadonlyFlagIsSetIfReadonlyIsSetOnProperties() throws IOException {
37-
when(properties.readonly()).thenReturn(true);
38-
39-
inTest = new ReadonlyFlag(properties, path);
40-
41-
Assertions.assertTrue(inTest.isSet());
42-
}
43-
44-
@Test
45-
public void testReadonlyFlagIsSetIfReadonlyIsNotSetOnPropertiesAndFilestoreOfVaultIsReadonly() throws IOException {
46-
when(properties.readonly()).thenReturn(false);
47-
when(fileStore.isReadOnly()).thenReturn(true);
48-
49-
inTest = new ReadonlyFlag(properties, path);
50-
51-
Assertions.assertTrue(inTest.isSet());
52-
}
53-
54-
@Test
55-
public void testReadonlyFlagIsNotSetIfReadonlyIsNotSetOnPropertiesAndFilestoreOfVaultIsNotReadonly() throws IOException {
56-
when(properties.readonly()).thenReturn(false);
57-
when(fileStore.isReadOnly()).thenReturn(false);
58-
59-
inTest = new ReadonlyFlag(properties, path);
60-
61-
Assertions.assertFalse(inTest.isSet());
41+
@DisplayName("isSet()")
42+
@ParameterizedTest(name = "readonlyFlag: {0}, writeProtected: {1} -> mounted readonly {2}")
43+
@CsvSource({
44+
"false, false, false",
45+
"true, false, true",
46+
"false, true, true",
47+
"true, true, true",
48+
})
49+
public void testIsSet(boolean readonlyFlag, boolean writeProtected, boolean expectedResult) throws IOException {
50+
when(properties.readonly()).thenReturn(readonlyFlag);
51+
if (writeProtected) {
52+
Mockito.doThrow(new AccessDeniedException(path.toString())).when(provider).checkAccess(path, AccessMode.WRITE);
53+
}
54+
ReadonlyFlag inTest = new ReadonlyFlag(properties, path);
55+
56+
boolean result = inTest.isSet();
57+
58+
MatcherAssert.assertThat(result, CoreMatchers.is(expectedResult));
59+
Assertions.assertEquals(expectedResult, result);
6260
}
6361

64-
@Test
65-
public void testAssertWritableThrowsIOExceptionIfReadonlyIsSetOnProperties() throws IOException {
66-
when(properties.readonly()).thenReturn(true);
67-
68-
inTest = new ReadonlyFlag(properties, path);
69-
70-
Assertions.assertThrows(ReadOnlyFileSystemException.class, () -> {
71-
inTest.assertWritable();
72-
});
73-
}
74-
75-
@Test
76-
public void testAssertWritableThrowsIOExceptionIfReadonlyIsNotSetOnPropertiesAndFilestoreOfVaultIsReadonly() throws IOException {
77-
when(properties.readonly()).thenReturn(false);
78-
when(fileStore.isReadOnly()).thenReturn(true);
79-
80-
inTest = new ReadonlyFlag(properties, path);
81-
82-
Assertions.assertThrows(ReadOnlyFileSystemException.class, () -> {
83-
inTest.assertWritable();
84-
});
85-
}
86-
87-
@Test
88-
public void testAssertWritableDoesNotThrowIOExceptionIfReadonlyIsNotSetOnPropertiesAndFilestoreOfVaultIsNotReadonly() throws IOException {
89-
when(properties.readonly()).thenReturn(false);
90-
when(fileStore.isReadOnly()).thenReturn(false);
91-
92-
inTest = new ReadonlyFlag(properties, path);
93-
94-
inTest.assertWritable();
62+
@DisplayName("assertWritable()")
63+
@ParameterizedTest(name = "readonlyFlag: {0}, writeProtected: {1} -> mounted readonly {2}")
64+
@CsvSource({
65+
"false, false, false",
66+
"true, false, true",
67+
"false, true, true",
68+
"true, true, true",
69+
})
70+
public void testAssertWritable(boolean readonlyFlag, boolean writeProtected, boolean expectedResult) throws IOException {
71+
when(properties.readonly()).thenReturn(readonlyFlag);
72+
if (writeProtected) {
73+
Mockito.doThrow(new AccessDeniedException(path.toString())).when(provider).checkAccess(path, AccessMode.WRITE);
74+
}
75+
ReadonlyFlag inTest = new ReadonlyFlag(properties, path);
76+
77+
if (expectedResult) {
78+
Assertions.assertThrows(ReadOnlyFileSystemException.class, () -> {
79+
inTest.assertWritable();
80+
});
81+
} else {
82+
Assertions.assertDoesNotThrow(() -> {
83+
inTest.assertWritable();
84+
});
85+
}
9586
}
9687

9788
}

0 commit comments

Comments
 (0)