|
14 | 14 | import org.junit.jupiter.api.io.TempDir; |
15 | 15 | import org.junit.jupiter.params.ParameterizedTest; |
16 | 16 | import org.junit.jupiter.params.provider.ValueSource; |
| 17 | +import org.mockito.ArgumentCaptor; |
17 | 18 | import org.mockito.ArgumentMatchers; |
18 | 19 | import org.mockito.Mockito; |
19 | 20 |
|
|
22 | 23 | import java.io.IOException; |
23 | 24 | import java.io.InputStream; |
24 | 25 | import java.io.StringWriter; |
| 26 | +import java.nio.file.Files; |
25 | 27 | import java.nio.file.Path; |
26 | 28 | import java.security.SecureRandom; |
27 | 29 |
|
@@ -124,6 +126,80 @@ public void testLoadMalformed() { |
124 | 126 | }); |
125 | 127 | } |
126 | 128 |
|
| 129 | + @Nested |
| 130 | + @DisplayName("load() with validator") |
| 131 | + class LoadWithValidator { |
| 132 | + |
| 133 | + @Test |
| 134 | + @DisplayName("all-passing validator allows loading the key") |
| 135 | + public void testLoadWithPassingValidator() throws IOException { |
| 136 | + InputStream in = new ByteArrayInputStream(serializedKeyFile); |
| 137 | + |
| 138 | + Masterkey loaded = masterkeyFileAccess.load(in, "asd", file -> { |
| 139 | + }); |
| 140 | + |
| 141 | + Assertions.assertArrayEquals(key.getEncoded(), loaded.getEncoded()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + @DisplayName("all-rejecting validator throws exception, no key derivation happens") |
| 146 | + public void testLoadWithRejectingValidator() throws InvalidPassphraseException { |
| 147 | + InputStream in = new ByteArrayInputStream(serializedKeyFile); |
| 148 | + IOException rejection = new IOException("scrypt parameters exceed memory limit"); |
| 149 | + |
| 150 | + IOException thrown = Assertions.assertThrows(IOException.class, () -> { |
| 151 | + masterkeyFileAccess.load(in, "asd", file -> { |
| 152 | + throw rejection; |
| 153 | + }); |
| 154 | + }); |
| 155 | + Assertions.assertSame(rejection, thrown); |
| 156 | + Mockito.verify(masterkeyFileAccess, Mockito.never()).unlock(ArgumentMatchers.any(), ArgumentMatchers.any()); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + @DisplayName("validator receives the parsed file") |
| 161 | + public void testValidatorReceivesParsedFile() throws IOException { |
| 162 | + InputStream in = new ByteArrayInputStream(serializedKeyFile); |
| 163 | + MasterkeyFileValidator validator = Mockito.mock(MasterkeyFileValidator.class); |
| 164 | + |
| 165 | + masterkeyFileAccess.load(in, "asd", validator); |
| 166 | + |
| 167 | + ArgumentCaptor<MasterkeyFile> captor = ArgumentCaptor.forClass(MasterkeyFile.class); |
| 168 | + Mockito.verify(validator).validate(captor.capture()); |
| 169 | + Assertions.assertEquals(999, captor.getValue().version); |
| 170 | + Assertions.assertEquals(2, captor.getValue().scryptCostParam); |
| 171 | + Assertions.assertEquals(8, captor.getValue().scryptBlockSize); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + @DisplayName("validator is not invoked for structurally invalid files") |
| 176 | + public void testValidatorNotInvokedForInvalidFile() { |
| 177 | + InputStream in = new ByteArrayInputStream("{\"foo\": 42}".getBytes(UTF_8)); |
| 178 | + MasterkeyFileValidator validator = Mockito.mock(MasterkeyFileValidator.class); |
| 179 | + |
| 180 | + Assertions.assertThrows(IOException.class, () -> { |
| 181 | + masterkeyFileAccess.load(in, "asd", validator); |
| 182 | + }); |
| 183 | + Mockito.verifyNoInteractions(validator); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + @DisplayName("load(Path, ...) with rejecting validator -> MasterkeyLoadingFailedException caused by validator's exception") |
| 188 | + public void testLoadPathWithRejectingValidator(@TempDir Path tmpDir) throws IOException { |
| 189 | + Path masterkeyFile = tmpDir.resolve("masterkey.cryptomator"); |
| 190 | + Files.write(masterkeyFile, serializedKeyFile); |
| 191 | + IOException rejection = new IOException("scrypt parameters exceed memory limit"); |
| 192 | + |
| 193 | + MasterkeyLoadingFailedException thrown = Assertions.assertThrows(MasterkeyLoadingFailedException.class, () -> { |
| 194 | + masterkeyFileAccess.load(masterkeyFile, "asd", file -> { |
| 195 | + throw rejection; |
| 196 | + }); |
| 197 | + }); |
| 198 | + Assertions.assertSame(rejection, thrown.getCause()); |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
127 | 203 | } |
128 | 204 |
|
129 | 205 | /** |
@@ -173,12 +249,12 @@ public void testLoadWithOversizedMemoryRequirement() throws IOException, Invalid |
173 | 249 | Mockito.verify(masterkeyFileAccess, Mockito.never()).unlock(ArgumentMatchers.any(), ArgumentMatchers.any()); |
174 | 250 | } |
175 | 251 |
|
176 | | - private byte[] serialize(MasterkeyFile keyFile) throws IOException { |
177 | | - StringWriter writer = new StringWriter(); |
178 | | - keyFile.write(writer); |
179 | | - return writer.toString().getBytes(UTF_8); |
180 | | - } |
| 252 | + } |
181 | 253 |
|
| 254 | + private static byte[] serialize(MasterkeyFile keyFile) throws IOException { |
| 255 | + StringWriter writer = new StringWriter(); |
| 256 | + keyFile.write(writer); |
| 257 | + return writer.toString().getBytes(UTF_8); |
182 | 258 | } |
183 | 259 |
|
184 | 260 | @Nested |
|
0 commit comments