Skip to content

Commit

Permalink
Add test for illegal memory access detection
Browse files Browse the repository at this point in the history
Crashes were detected in production tests when Hyphenator is used with
the version 5.14 of JNA. See
daisy/pipeline-modules#102.
  • Loading branch information
NPavie authored and bertfrees committed Aug 1, 2024
1 parent 87d28ee commit bb6f120
Show file tree
Hide file tree
Showing 2 changed files with 897 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/test/java/ch/sbs/jhyphen/HyphenatorTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package ch.sbs.jhyphen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;

import static org.apache.commons.io.filefilter.FileFilterUtils.asFileFilter;
Expand All @@ -14,9 +17,10 @@

public class HyphenatorTest {

private static File tablesDir; {
private static File tablesDir, textFile; {
File testRootDir = new File(this.getClass().getResource("/").getPath());
tablesDir = new File(testRootDir, "tables");
textFile = new File(testRootDir, "test.txt");
Hyphen.setLibraryPath(((Collection<File>)FileUtils.listFiles(
new File(testRootDir, "../dependency"),
asFileFilter(new FilenameFilter() {
Expand Down Expand Up @@ -68,4 +72,32 @@ public void testNonStandardHyphenation() throws Exception {
hyphenator.close();
}
}

/**
* Stability test over many runs on a simple text document.
* (Introduced after crashes were detected when using JNA 5.14, that were
* caused by early garbage collection of pointer.)
*
* @throws CompilationException
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void testManyRunsHyphenation() throws CompilationException, FileNotFoundException, IOException {
Hyphenator hyphenator = new Hyphenator(new File(tablesDir, "standard.dic"));
try{
for(int i = 0; i < 10; ++i){
BufferedReader testFileReader = new BufferedReader(new FileReader(textFile));
String line;
do {
line = testFileReader.readLine();
if(line != null){
hyphenator.hyphenate(line);
}
} while(line != null);
}
} finally {
hyphenator.close();
}
}
}
Loading

0 comments on commit bb6f120

Please sign in to comment.