Skip to content

Commit c04f87a

Browse files
committed
tests function listing script
1 parent 86152af commit c04f87a

2 files changed

Lines changed: 83 additions & 6 deletions

File tree

scripts/ghidra/PatchestryListFunctions.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@
4444
import java.util.Collections;
4545

4646
public class PatchestryListFunctions extends GhidraScript {
47-
private List<Function> getAllFunctions() {
47+
/* For test setup purposes, we don't want to control this script from the command line. */
48+
protected void setProgram(Program program) throws RuntimeException {
49+
if (program != null && getCurrentProgram() == null) {
50+
currentProgram = program;
51+
} else {
52+
currentProgram = getCurrentProgram();
53+
}
54+
}
55+
56+
protected List<Function> getAllFunctions() {
4857
if (currentProgram == null || currentProgram.getFunctionManager() == null) {
4958
return Collections.emptyList();
5059
}
@@ -56,16 +65,21 @@ private List<Function> getAllFunctions() {
5665
return functions;
5766
}
5867

59-
private void serializeToFile(Path file, List<Function> functions) throws Exception {
60-
if (file == null || functions == null || functions.isEmpty()) {
61-
throw new IllegalArgumentException("Invalid file path or empty function list");
68+
protected void serializeToFile(Path file, List<Function> functions) throws Exception {
69+
if (file == null) {
70+
throw new IllegalArgumentException("Invalid file writer");
71+
}
72+
73+
if (functions == null || functions.isEmpty()) {
74+
throw new IllegalArgumentException("Empty function list");
6275
}
6376

6477
final var serializer = new FunctionSerializer(Files.newBufferedWriter(file), currentProgram);
65-
serializer.serialize(functions).close();
78+
serializer.serialize(functions);
79+
serializer.close();
6680
}
6781

68-
private void runHeadless() throws Exception {
82+
protected void runHeadless() throws Exception {
6983
if (getScriptArgs().length != 1) {
7084
throw new IllegalArgumentException("Output file is not specified");
7185
}

test/scripts/ghidra/PatchestryListFunctionsTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,70 @@
99

1010
import org.junit.jupiter.api.*;
1111

12+
import ghidra.program.model.listing.Function;
13+
14+
import java.lang.reflect.Field;
15+
16+
import java.nio.file.Files;
17+
import java.nio.file.Path;
18+
19+
import java.util.Collections;
20+
import java.util.List;
21+
1222
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1323
public class PatchestryListFunctionsTest extends BaseTest {
1424
private PatchestryListFunctions listingScript = new PatchestryListFunctions();
25+
private Path outputFile;
26+
27+
@BeforeAll
28+
public void setUp() throws Exception {
29+
super.setUp();
30+
31+
Field monitorField = findFieldInHierarchy(listingScript.getClass(), "monitor");
32+
monitorField.setAccessible(true);
33+
monitorField.set(listingScript, fakeMonitor);
34+
}
35+
36+
@BeforeEach
37+
protected void finalizeSetUp() throws Exception {
38+
outputFile = Files.createTempFile("test-list-and-serialize", ".json");
39+
String[] args = {
40+
outputFile.toString()
41+
};
42+
listingScript.setScriptArgs(args);
43+
listingScript.setProgram(program);
44+
assertEquals(listingScript.getScriptArgs(), args);
45+
assertNotNull(listingScript.getCurrentProgram());
46+
assertFalse(listingScript.getCurrentProgram().isClosed());
47+
}
48+
49+
@Test
50+
public void testGetAllFunctions() throws Exception {
51+
List<Function> functions = listingScript.getAllFunctions();
52+
assertNotNull(functions);
53+
assertFalse(functions.isEmpty());
54+
55+
// should match with testGetAllFunctions in the PatchestryDecompileFunctionsTest.
56+
assertEquals(functions.size(), 262);
57+
}
58+
59+
@Test
60+
public void testSerializeToFile() throws Exception {
61+
List<Function> functions = listingScript.getAllFunctions();
62+
assertEquals(functions.size(), 262);
63+
64+
assertThrows(IllegalArgumentException.class, () -> {
65+
listingScript.serializeToFile((Path) null, functions);
66+
});
67+
68+
assertThrows(IllegalArgumentException.class, () -> {
69+
listingScript.serializeToFile(outputFile, (List<Function>) null);
70+
});
71+
72+
assertThrows(IllegalArgumentException.class, () -> {
73+
listingScript.serializeToFile(outputFile, Collections.emptyList());
74+
});
75+
76+
listingScript.serializeToFile(outputFile, functions);
77+
}
1578
}

0 commit comments

Comments
 (0)