Set up testing infrastructure ("make test")

This commit is contained in:
Robert Xiao
2023-05-05 16:24:45 -07:00
parent 4b471e16e9
commit 66c8965f96
14 changed files with 151 additions and 19 deletions

View File

@@ -11,6 +11,7 @@ JAVA_PLATFORM_INC := $(shell dirname `find $(JAVA_INC) -name jni_md.h`)
UNICORN_INC=../../include
SAMPLES := $(shell ls samples/*.java)
TESTS := $(shell ls tests/*.java)
SRC := $(shell ls unicorn/*.java)
OS := $(shell uname)
@@ -33,6 +34,9 @@ CLASSPATH=./
.SUFFIXES: .java .class
tests/%.class: tests/%.java
$(JC) -classpath .:unicorn.jar:testdep/junit-4.13.2.jar $(JFLAGS) $<
%.class: %.java
$(JC) -classpath .:unicorn.jar $(JFLAGS) $<
@@ -55,11 +59,15 @@ lib: libunicorn_java$(LIB_EXT) unicorn_Unicorn.h
$(CC) -o $< $(LDFLAGS) $(OBJS) $(LIBDIR) $(LIBS)
samples: $(SAMPLES:.java=.class)
tests: $(TESTS:.java=.class)
jarfiles: $(SRC:.java=.class)
jar: jarfiles
jar cf $(JARFILE) unicorn/*.class
test: lib samples tests
java -cp .:testdep/hamcrest-2.2.jar:testdep/junit-4.13.2.jar org.junit.runner.JUnitCore $(subst /,.,$(TESTS:.java=))
install: lib jar
cp libunicorn_java$(LIB_EXT) /usr/lib
cp $(JARFILE) /usr/share/java

View File

@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Nguyen Tan Cong <shenlongbk@gmail.com>
*/
package samples;
import unicorn.*;
import java.util.*;
@@ -430,7 +432,7 @@ public class SampleNetworkAuditing {
}
// end utilities
static void test_i386(byte[] code) {
public static void test_i386(byte[] code) {
fd_chains.clean();
System.out.printf("Emulate i386 code\n");
try {

View File

@@ -3,6 +3,8 @@
/* Sample code to demonstrate how to emulate ARM code */
package samples;
import unicorn.*;
public class Sample_arm {
@@ -41,7 +43,7 @@ public class Sample_arm {
}
}
static void test_arm() {
public static void test_arm() {
Long r0 = 0x1234L; // R0 register
Long r2 = 0x6789L; // R1 register
@@ -85,7 +87,7 @@ public class Sample_arm {
u.close();
}
static void test_thumb() {
public static void test_thumb() {
Long sp = 0x1234L; // R0 register

View File

@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/* Sample code to demonstrate how to emulate ARM64 code */
package samples;
import unicorn.*;
public class Sample_arm64 {
@@ -70,7 +72,7 @@ public class Sample_arm64 {
}
}
static void test_arm64() {
public static void test_arm64() {
Long x11 = 0x1234L; // X11 register
Long x13 = 0x6789L; // X13 register

View File

@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/* Sample code to demonstrate how to emulate m68k code */
package samples;
import unicorn.*;
public class Sample_m68k {
@@ -70,7 +72,7 @@ public class Sample_m68k {
}
}
static void test_m68k() {
public static void test_m68k() {
Long d0 = 0x0000L; // d0 data register
Long d1 = 0x0000L; // d1 data register
Long d2 = 0x0000L; // d2 data register

View File

@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/* Sample code to demonstrate how to emulate Mips code (big endian) */
package samples;
import unicorn.*;
public class Sample_mips {
@@ -71,7 +73,7 @@ public class Sample_mips {
}
}
static void test_mips_eb() {
public static void test_mips_eb() {
Long r1 = 0x6789L; // R1 register
@@ -109,7 +111,7 @@ public class Sample_mips {
u.close();
}
static void test_mips_el() {
public static void test_mips_el() {
Long r1 = 0x6789L; // R1 register
System.out.print("===========================\n");

View File

@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/* Sample code to demonstrate how to emulate Sparc code */
package samples;
import unicorn.*;
public class Sample_sparc {
@@ -71,7 +73,7 @@ public class Sample_sparc {
}
}
static void test_sparc() {
public static void test_sparc() {
Long g1 = 0x1230L; // G1 register
Long g2 = 0x6789L; // G2 register
Long g3 = 0x5555L; // G3 register

View File

@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/* Sample code to demonstrate how to emulate X86 code */
package samples;
import unicorn.*;
public class Sample_x86 {
@@ -200,7 +202,7 @@ public class Sample_x86 {
}
}
static void test_i386() {
public static void test_i386() {
Long r_ecx = 0x1234L; // ECX register
Long r_edx = 0x7890L; // EDX register
@@ -266,7 +268,7 @@ public class Sample_x86 {
uc.close();
}
static void test_i386_inout() {
public static void test_i386_inout() {
Long r_eax = 0x1234L; // ECX register
Long r_ecx = 0x6789L; // EDX register
@@ -311,7 +313,7 @@ public class Sample_x86 {
u.close();
}
static void test_i386_jump() {
public static void test_i386_jump() {
System.out.print("===================================\n");
System.out.print("Emulate i386 code with jump\n");
@@ -339,7 +341,7 @@ public class Sample_x86 {
}
// emulate code that loop forever
static void test_i386_loop() {
public static void test_i386_loop() {
Long r_ecx = 0x1234L; // ECX register
Long r_edx = 0x7890L; // EDX register
@@ -376,7 +378,7 @@ public class Sample_x86 {
}
// emulate code that read invalid memory
static void test_i386_invalid_mem_read() {
public static void test_i386_invalid_mem_read() {
Long r_ecx = 0x1234L; // ECX register
Long r_edx = 0x7890L; // EDX register
@@ -424,7 +426,7 @@ public class Sample_x86 {
}
// emulate code that read invalid memory
static void test_i386_invalid_mem_write() {
public static void test_i386_invalid_mem_write() {
Long r_ecx = 0x1234L; // ECX register
Long r_edx = 0x7890L; // EDX register
@@ -489,7 +491,7 @@ public class Sample_x86 {
}
// emulate code that jump to invalid memory
static void test_i386_jump_invalid() {
public static void test_i386_jump_invalid() {
Long r_ecx = 0x1234L; // ECX register
Long r_edx = 0x7890L; // EDX register
@@ -535,7 +537,7 @@ public class Sample_x86 {
u.close();
}
static void test_x86_64() {
public static void test_x86_64() {
long rax = 0x71f3029efd49d41dL;
long rbx = 0xd87b45277f133ddbL;
long rcx = 0xab40d1ffd8afc461L;
@@ -634,7 +636,7 @@ public class Sample_x86 {
u.close();
}
static void test_x86_16() {
public static void test_x86_16() {
Long eax = 7L;
Long ebx = 5L;
Long esi = 6L;

View File

@@ -21,11 +21,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/* Sample code to demonstrate how to register read/write API */
package samples;
import unicorn.*;
public class Sample_x86_mmr {
static void test_x86_mmr() {
public static void test_x86_mmr() {
// Initialize emulator in X86-32bit mode
Unicorn uc;
try {

View File

@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/* Sample code to trace code with Linux code with syscall */
package samples;
import unicorn.*;
public class Shellcode {
@@ -125,7 +127,7 @@ public class Shellcode {
}
}
static void test_i386() {
public static void test_i386() {
Long r_esp = ADDRESS + 0x200000L; // ESP register
System.out.print("Emulate i386 code\n");

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,67 @@
package tests;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import unicorn.Unicorn;
import unicorn.UnicornException;
import unicorn.CodeHook;
public class RegressionTests {
/** Test for GH #1539: Unable to read ARM64 v or q register using java binding */
@Test
public void testARM64VReg() {
Unicorn uc = new Unicorn(Unicorn.UC_ARCH_ARM64, Unicorn.UC_MODE_ARM);
uc.reg_write(Unicorn.UC_ARM64_REG_X0, 0x1);
uc.reg_write(Unicorn.UC_ARM64_REG_V0, 0x2);
uc.reg_read(Unicorn.UC_ARM64_REG_X0);
uc.reg_read(Unicorn.UC_ARM64_REG_V0); // should not crash
uc.close();
}
/** Test for GH #1164: Java binding use CodeHook on Windows, will invoke callback before every instruction */
@Test
public void testCodeHookRunsOnce() {
byte[] ARM_CODE =
{ 55, 0, (byte) 0xa0, (byte) 0xe3, 3, 16, 66, (byte) 0xe0 }; // mov r0, #0x37; sub r1, r2, r3
int ADDRESS = 0x10000;
final int[] hook_count = { 0 };
Unicorn u = new Unicorn(Unicorn.UC_ARCH_ARM, Unicorn.UC_MODE_ARM);
u.mem_map(ADDRESS, 2 * 1024 * 1024, Unicorn.UC_PROT_ALL);
u.mem_write(ADDRESS, ARM_CODE);
u.hook_add((CodeHook) (uc, address, size, user) -> hook_count[0] += 1,
ADDRESS, ADDRESS, null);
u.emu_start(ADDRESS, ADDRESS + ARM_CODE.length, 0, 0);
assertEquals("Hook should only be called once", 1, hook_count[0]);
u.close();
}
/** Test that close() can be called multiple times without crashing */
@Test
public void testCloseIdempotent() {
Unicorn u = new Unicorn(Unicorn.UC_ARCH_ARM, Unicorn.UC_MODE_ARM);
u.close();
u.close();
}
/** Test that close() can be called multiple times without crashing */
@Test
public void testUnicornsWillGC() {
final boolean[] close_called = { false };
new Unicorn(Unicorn.UC_ARCH_ARM, Unicorn.UC_MODE_ARM) {
@Override
public void close() throws UnicornException {
close_called[0] = true;
super.close();
}
};
System.gc();
System.runFinalization();
assertEquals("close() was called", true, close_called[0]);
}
}

View File

@@ -0,0 +1,39 @@
package tests;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
public class TestSamples {
private final ByteArrayOutputStream outContent =
new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
@Before
public void setUpStreams() {
outContent.reset();
System.setOut(new PrintStream(outContent));
}
@After
public void restoreStreams() {
System.setOut(originalOut);
}
@Test
public void testArm() {
samples.Sample_arm.test_arm();
assertEquals("testArm",
"Emulate ARM code\n" +
">>> Tracing basic block at 0x10000, block size = 0x8\n" +
">>> Tracing instruction at 0x10000, instruction size = 0x4\n" +
">>> Emulation done. Below is the CPU context\n" +
">>> R0 = 0x37\n" +
">>> R1 = 0x3456\n",
outContent.toString());
}
}