Skip to content

Commit afa6a88

Browse files
add integration test for issue #129
1 parent ecd825d commit afa6a88

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/test/java/org/cryptomator/cryptofs/CryptoFileChannelWriteReadIntegrationTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.cryptomator.cryptofs;
1010

1111
import com.google.common.jimfs.Jimfs;
12+
import org.cryptomator.cryptofs.util.ByteBuffers;
1213
import org.cryptomator.cryptolib.api.Masterkey;
1314
import org.cryptomator.cryptolib.api.MasterkeyLoader;
1415
import org.cryptomator.cryptolib.api.MasterkeyLoadingFailedException;
@@ -24,12 +25,14 @@
2425
import org.junit.jupiter.api.io.TempDir;
2526
import org.junit.jupiter.params.ParameterizedTest;
2627
import org.junit.jupiter.params.provider.CsvSource;
28+
import org.junit.jupiter.params.provider.MethodSource;
2729
import org.junit.jupiter.params.provider.ValueSource;
2830
import org.mockito.Mockito;
2931

3032
import java.io.IOException;
3133
import java.net.URI;
3234
import java.nio.ByteBuffer;
35+
import java.nio.channels.Channels;
3336
import java.nio.channels.FileChannel;
3437
import java.nio.channels.FileLock;
3538
import java.nio.charset.StandardCharsets;
@@ -39,6 +42,8 @@
3942
import java.nio.file.attribute.FileTime;
4043
import java.time.Instant;
4144
import java.time.temporal.ChronoUnit;
45+
import java.util.List;
46+
import java.util.stream.Stream;
4247

4348
import static java.lang.Math.min;
4449
import static java.lang.String.format;
@@ -49,7 +54,6 @@
4954
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
5055
import static java.nio.file.StandardOpenOption.WRITE;
5156
import static org.cryptomator.cryptofs.CryptoFileSystemProperties.cryptoFileSystemProperties;
52-
import static org.cryptomator.cryptofs.CryptoFileSystemUri.create;
5357
import static org.cryptomator.cryptofs.util.ByteBuffers.repeat;
5458

5559
public class CryptoFileChannelWriteReadIntegrationTest {
@@ -260,6 +264,54 @@ public void testFileSizeIsTenAfterWritingTenBytes() throws IOException {
260264
Assertions.assertEquals(10, Files.size(file));
261265
}
262266

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+
263315
@Test
264316
public void testWriteAndReadNothing() throws IOException {
265317
try (FileChannel channel = FileChannel.open(file, CREATE, WRITE)) {

0 commit comments

Comments
 (0)