-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeFileReaderTest.java
More file actions
68 lines (57 loc) · 2.59 KB
/
Copy pathSafeFileReaderTest.java
File metadata and controls
68 lines (57 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package io.github.randomcodespace.iq.api;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class SafeFileReaderTest {
@Test
void readsWholeFileUnderCap(@TempDir Path tempDir) throws IOException {
Path f = tempDir.resolve("small.txt");
Files.writeString(f, "hello", StandardCharsets.UTF_8);
assertEquals("hello", SafeFileReader.read(f, null, null, 1024L));
}
@Test
void rejectsWholeFileExceedingCap(@TempDir Path tempDir) throws IOException {
Path f = tempDir.resolve("big.txt");
byte[] payload = new byte[2048];
java.util.Arrays.fill(payload, (byte) 'x');
Files.write(f, payload);
SafeFileReader.FileTooLargeException e = assertThrows(
SafeFileReader.FileTooLargeException.class,
() -> SafeFileReader.read(f, null, null, 1024L));
assertEquals(2048L, e.size());
assertEquals(1024L, e.max());
}
@Test
void streamsLineRangeWithoutLoadingWholeFile(@TempDir Path tempDir) throws IOException {
Path f = tempDir.resolve("ranged.txt");
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 100; i++) sb.append("line").append(i).append('\n');
Files.writeString(f, sb.toString(), StandardCharsets.UTF_8);
// Whole-file readString with cap=64 would fail, but the 3-line range fits.
assertEquals("line2\nline3\nline4",
SafeFileReader.read(f, 2, 4, 64L));
}
@Test
void rejectsLineRangeExceedingCap(@TempDir Path tempDir) throws IOException {
Path f = tempDir.resolve("ranged.txt");
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 100; i++) sb.append("line").append(i).append('\n');
Files.writeString(f, sb.toString(), StandardCharsets.UTF_8);
SafeFileReader.FileTooLargeException e = assertThrows(
SafeFileReader.FileTooLargeException.class,
() -> SafeFileReader.read(f, 1, 100, 32L));
assertTrue(e.max() == 32L);
}
@Test
void clampsStartLineToOneWhenNegative(@TempDir Path tempDir) throws IOException {
Path f = tempDir.resolve("clamp.txt");
Files.writeString(f, "a\nb\nc\n", StandardCharsets.UTF_8);
assertEquals("a\nb", SafeFileReader.read(f, -3, 2, 1024L));
}
}