Skip to content

Commit

Permalink
Use Files.list() in fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Jul 23, 2024
1 parent dcf190c commit 2915f88
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
27 changes: 12 additions & 15 deletions src/test/java/org/jsoup/integration/FuzzFixesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -22,40 +23,36 @@
public class FuzzFixesIT {
static int numIters = 50;
static int timeout = 30; // external fuzzer is set to 60 for 100 runs
static File testDir = ParseTest.getFile("/fuzztests/");
static Path testDir = ParseTest.getPath("/fuzztests/");

private static Stream<File> testFiles() {
File[] files = testDir.listFiles();
assertNotNull(files);
assertTrue(files.length > 10);

return Stream.of(files);
private static Stream<Path> testPaths() throws IOException {
return Files.list(testDir);
}

@Disabled // disabled, as these soak up build time and the outcome oughtn't change unless we are refactoring the tree builders. manually execute as desired.
@ParameterizedTest
@MethodSource("testFiles")
void testHtmlParse(File file) throws IOException {
@MethodSource("testPaths")
void testHtmlParse(Path path) throws IOException {
long startTime = System.currentTimeMillis();
long completeBy = startTime + timeout * 1000L;

for (int i = 0; i < numIters; i++) {
Document doc = Jsoup.parse(file, "UTF-8", "https://example.com/");
Document doc = Jsoup.parse(path, "UTF-8", "https://example.com/");
assertNotNull(doc);
if (System.currentTimeMillis() > completeBy)
Assertions.fail(String.format("Timeout: only completed %d iters of [%s] in %d seconds", i, file.getName(), timeout));
Assertions.fail(String.format("Timeout: only completed %d iters of [%s] in %d seconds", i, path.getFileName(), timeout));
}
}

@Disabled // disabled, as these soak up build time and the outcome oughtn't change unless we are refactoring the tree builders. manually execute as desired.
@ParameterizedTest
@MethodSource("testFiles")
void testXmlParse(File file) throws IOException {
@MethodSource("testPaths")
void testXmlParse(Path path) throws IOException {
long startTime = System.currentTimeMillis();
long completeBy = startTime + timeout * 1000L;

for (int i = 0; i < numIters; i++) {
Document doc = Jsoup.parse(file, "UTF-8", "https://example.com/", Parser.xmlParser());
Document doc = Jsoup.parse(path, "UTF-8", "https://example.com/", Parser.xmlParser());
assertNotNull(doc);
if (System.currentTimeMillis() > completeBy)
Assertions.fail(String.format("Timeout: only completed %d iters of [%s] in %d seconds", i, file.getName(), timeout));
Expand Down
23 changes: 10 additions & 13 deletions src/test/java/org/jsoup/integration/FuzzFixesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -20,12 +21,8 @@
*/
public class FuzzFixesTest {

private static Stream<File> testFiles() {
File[] files = FuzzFixesIT.testDir.listFiles();
assertNotNull(files);
assertTrue(files.length > 10);

return Stream.of(files);
private static Stream<Path> testPaths() throws IOException {
return Files.list(FuzzFixesIT.testDir);
}

@Test
Expand All @@ -48,16 +45,16 @@ public void bookmark() {
}

@ParameterizedTest
@MethodSource("testFiles")
void testHtmlParse(File file) throws IOException {
Document doc = Jsoup.parse(file, "UTF-8", "https://example.com/");
@MethodSource("testPaths")
void testHtmlParse(Path path) throws IOException {
Document doc = Jsoup.parse(path, "UTF-8", "https://example.com/");
assertNotNull(doc);
}

@ParameterizedTest
@MethodSource("testFiles")
void testXmlParse(File file) throws IOException {
Document doc = Jsoup.parse(file, "UTF-8", "https://example.com/", Parser.xmlParser());
@MethodSource("testPaths")
void testXmlParse(Path path) throws IOException {
Document doc = Jsoup.parse(path, "UTF-8", "https://example.com/", Parser.xmlParser());
assertNotNull(doc);
}
}

0 comments on commit 2915f88

Please sign in to comment.