|
9 | 9 | package org.cryptomator.cryptofs;
|
10 | 10 |
|
11 | 11 | import com.google.common.jimfs.Jimfs;
|
| 12 | +import org.cryptomator.cryptofs.util.ByteBuffers; |
12 | 13 | import org.cryptomator.cryptolib.api.Masterkey;
|
13 | 14 | import org.cryptomator.cryptolib.api.MasterkeyLoader;
|
14 | 15 | import org.cryptomator.cryptolib.api.MasterkeyLoadingFailedException;
|
|
24 | 25 | import org.junit.jupiter.api.io.TempDir;
|
25 | 26 | import org.junit.jupiter.params.ParameterizedTest;
|
26 | 27 | import org.junit.jupiter.params.provider.CsvSource;
|
| 28 | +import org.junit.jupiter.params.provider.MethodSource; |
27 | 29 | import org.junit.jupiter.params.provider.ValueSource;
|
28 | 30 | import org.mockito.Mockito;
|
29 | 31 |
|
30 | 32 | import java.io.IOException;
|
31 | 33 | import java.net.URI;
|
32 | 34 | import java.nio.ByteBuffer;
|
| 35 | +import java.nio.channels.Channels; |
33 | 36 | import java.nio.channels.FileChannel;
|
34 | 37 | import java.nio.channels.FileLock;
|
35 | 38 | import java.nio.charset.StandardCharsets;
|
|
39 | 42 | import java.nio.file.attribute.FileTime;
|
40 | 43 | import java.time.Instant;
|
41 | 44 | import java.time.temporal.ChronoUnit;
|
| 45 | +import java.util.List; |
| 46 | +import java.util.stream.Stream; |
42 | 47 |
|
43 | 48 | import static java.lang.Math.min;
|
44 | 49 | import static java.lang.String.format;
|
|
49 | 54 | import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
|
50 | 55 | import static java.nio.file.StandardOpenOption.WRITE;
|
51 | 56 | import static org.cryptomator.cryptofs.CryptoFileSystemProperties.cryptoFileSystemProperties;
|
52 |
| -import static org.cryptomator.cryptofs.CryptoFileSystemUri.create; |
53 | 57 | import static org.cryptomator.cryptofs.util.ByteBuffers.repeat;
|
54 | 58 |
|
55 | 59 | public class CryptoFileChannelWriteReadIntegrationTest {
|
@@ -260,6 +264,54 @@ public void testFileSizeIsTenAfterWritingTenBytes() throws IOException {
|
260 | 264 | Assertions.assertEquals(10, Files.size(file));
|
261 | 265 | }
|
262 | 266 |
|
| 267 | + |
| 268 | + // tests https://github.com/cryptomator/cryptofs/issues/129 |
| 269 | + @ParameterizedTest |
| 270 | + @MethodSource |
| 271 | + public void testSkipBeyondEofAndWrite(List<SparseContent> contentRanges) throws IOException { |
| 272 | + // write sparse file |
| 273 | + try (var ch = FileChannel.open(file, CREATE, WRITE)) { |
| 274 | + for (var range : contentRanges) { |
| 275 | + var buf = ByteBuffers.repeat(range.pattern).times(range.len).asByteBuffer(); |
| 276 | + ch.write(buf, range.pos); |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + // read and compare to expected |
| 281 | + try (var ch = FileChannel.open(file, READ); var in = Channels.newInputStream(ch)) { |
| 282 | + int pos = 0; |
| 283 | + for (var range : contentRanges) { |
| 284 | + // verify gaps are zeroes: |
| 285 | + for (int p = pos; p < range.pos; p++) { |
| 286 | + if (in.read() != 0) { |
| 287 | + Assertions.fail("Expected NIL byte at pos " + p); |
| 288 | + } |
| 289 | + } |
| 290 | + // verify expected values |
| 291 | + for (int p = 0; p < range.len; p++) { |
| 292 | + if (in.read() != range.pattern) { |
| 293 | + Assertions.fail("Expected byte at pos " + (pos + p) + " to be " + range.pattern); |
| 294 | + } |
| 295 | + } |
| 296 | + pos = range.pos + range.len; |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + private record SparseContent(byte pattern, int pos, int len) { |
| 302 | + |
| 303 | + } |
| 304 | + |
| 305 | + public Stream<List<SparseContent>> testSkipBeyondEofAndWrite() { |
| 306 | + return Stream.of( // |
| 307 | + List.of(new SparseContent((byte) 0x01, 50, 100)), // |
| 308 | + List.of(new SparseContent((byte) 0x01, 0, 1000), new SparseContent((byte) 0x02, 20_000, 1000)), // |
| 309 | + List.of(new SparseContent((byte) 0x01, 0, 1000), new SparseContent((byte) 0x02, 36_000, 1000)), // |
| 310 | + List.of(new SparseContent((byte) 0x01, 2_000_000, 84_000), new SparseContent((byte) 0x02, 3_000_000, 10_000)), // |
| 311 | + List.of(new SparseContent((byte) 0x01, 50, 100), new SparseContent((byte) 0x02, 250, 100), new SparseContent((byte) 0x03, 450, 100), new SparseContent((byte) 0x04, 20_000, 1000), new SparseContent((byte) 0x05, 3_000_000, 10_000)) // |
| 312 | + ); |
| 313 | + } |
| 314 | + |
263 | 315 | @Test
|
264 | 316 | public void testWriteAndReadNothing() throws IOException {
|
265 | 317 | try (FileChannel channel = FileChannel.open(file, CREATE, WRITE)) {
|
|
0 commit comments