|
| 1 | +package org.evomaster.client.java.instrumentation.dynamosa.graphs; |
| 2 | + |
| 3 | +import org.evomaster.client.java.instrumentation.dynamosa.graphs.cfg.ActualControlFlowGraph; |
| 4 | +import org.evomaster.client.java.instrumentation.dynamosa.graphs.cfg.BytecodeInstruction; |
| 5 | +import org.evomaster.client.java.instrumentation.dynamosa.graphs.cfg.RawControlFlowGraph; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.objectweb.asm.Opcodes; |
| 9 | +import org.objectweb.asm.tree.InsnNode; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.*; |
| 12 | + |
| 13 | +class GraphPoolTest { |
| 14 | + |
| 15 | + private static final ClassLoader TEST_LOADER = GraphPoolTest.class.getClassLoader(); |
| 16 | + private static final String CLASS_NAME = "com.example.GraphPoolFixture"; |
| 17 | + private static final String METHOD_NAME = "sample()V"; |
| 18 | + |
| 19 | + // Custom class loader that intentionally throws an exception when the ExecutionTracer class is loaded. |
| 20 | + private ClassLoader blockingLoader; |
| 21 | + |
| 22 | + @AfterEach |
| 23 | + void resetPools() { |
| 24 | + GraphPool.resetForTesting(TEST_LOADER); |
| 25 | + if (blockingLoader != null) { |
| 26 | + GraphPool.resetForTesting(blockingLoader); |
| 27 | + blockingLoader = null; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void getInstanceReturnsSingletonPerLoader() { |
| 33 | + GraphPool first = GraphPool.getInstance(TEST_LOADER); |
| 34 | + GraphPool second = GraphPool.getInstance(TEST_LOADER); |
| 35 | + assertSame(first, second); |
| 36 | + |
| 37 | + ClassLoader otherLoader = new SimpleDelegatingClassLoader(TEST_LOADER); |
| 38 | + GraphPool third = GraphPool.getInstance(otherLoader); |
| 39 | + assertNotSame(first, third); |
| 40 | + GraphPool.resetForTesting(otherLoader); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void getRawCFGReturnsNullWhenClassIsUnknown() { |
| 45 | + GraphPool pool = GraphPool.getInstance(TEST_LOADER); |
| 46 | + assertNull(pool.getRawCFG("unknown.class", METHOD_NAME)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void registerRawCFGStoresGraphUntilCleared() { |
| 51 | + GraphPool pool = GraphPool.getInstance(TEST_LOADER); |
| 52 | + RawControlFlowGraph raw = simpleRawGraph(TEST_LOADER, CLASS_NAME, METHOD_NAME); |
| 53 | + |
| 54 | + // Test that registerRawCFG and GetRawCFG works correctly |
| 55 | + pool.registerRawCFG(raw); |
| 56 | + assertSame(raw, pool.getRawCFG(CLASS_NAME, METHOD_NAME)); |
| 57 | + |
| 58 | + // Test that clear works correctly |
| 59 | + pool.clear(CLASS_NAME, METHOD_NAME); |
| 60 | + assertNull(pool.getRawCFG(CLASS_NAME, METHOD_NAME)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void registerActualCFGSkipsCdgWhenInstrumentationDisabled() { |
| 65 | + blockingLoader = new NoExecutionTracerClassLoader(GraphPoolTest.class.getClassLoader()); |
| 66 | + GraphPool pool = GraphPool.getInstance(blockingLoader); |
| 67 | + RawControlFlowGraph raw = simpleRawGraph(blockingLoader, CLASS_NAME, METHOD_NAME); |
| 68 | + ActualControlFlowGraph cfg = new ActualControlFlowGraph(raw); |
| 69 | + |
| 70 | + pool.registerActualCFG(cfg); |
| 71 | + |
| 72 | + assertSame(cfg, pool.getActualCFG(CLASS_NAME, METHOD_NAME)); |
| 73 | + assertNull(pool.getCDG(CLASS_NAME, METHOD_NAME)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void resetForTestingDropsCachedInstancesAndGraphs() { |
| 78 | + GraphPool pool = GraphPool.getInstance(TEST_LOADER); |
| 79 | + RawControlFlowGraph raw = simpleRawGraph(TEST_LOADER, CLASS_NAME, METHOD_NAME); |
| 80 | + pool.registerRawCFG(raw); |
| 81 | + |
| 82 | + GraphPool.resetForTesting(TEST_LOADER); |
| 83 | + |
| 84 | + GraphPool fresh = GraphPool.getInstance(TEST_LOADER); |
| 85 | + assertNotSame(pool, fresh); |
| 86 | + assertNull(fresh.getRawCFG(CLASS_NAME, METHOD_NAME)); |
| 87 | + } |
| 88 | + |
| 89 | + private static RawControlFlowGraph simpleRawGraph(ClassLoader loader, |
| 90 | + String className, |
| 91 | + String methodName) { |
| 92 | + TestRawControlFlowGraph raw = new TestRawControlFlowGraph(loader, className, methodName); |
| 93 | + |
| 94 | + BytecodeInstruction entry = instruction(loader, className, methodName, 0, Opcodes.NOP); |
| 95 | + BytecodeInstruction body = instruction(loader, className, methodName, 1, Opcodes.NOP); |
| 96 | + BytecodeInstruction exit = instruction(loader, className, methodName, 2, Opcodes.RETURN); |
| 97 | + |
| 98 | + raw.addVertex(entry); |
| 99 | + raw.addVertex(body); |
| 100 | + raw.addVertex(exit); |
| 101 | + |
| 102 | + raw.connect(entry, body); |
| 103 | + raw.connect(body, exit); |
| 104 | + |
| 105 | + return raw; |
| 106 | + } |
| 107 | + |
| 108 | + private static BytecodeInstruction instruction(ClassLoader loader, |
| 109 | + String className, |
| 110 | + String methodName, |
| 111 | + int instructionId, |
| 112 | + int opcode) { |
| 113 | + BytecodeInstruction instruction = new BytecodeInstruction( |
| 114 | + loader, |
| 115 | + className, |
| 116 | + methodName, |
| 117 | + instructionId, |
| 118 | + instructionId, |
| 119 | + new InsnNode(opcode) |
| 120 | + ); |
| 121 | + instruction.setLineNumber(100 + instructionId); |
| 122 | + return instruction; |
| 123 | + } |
| 124 | + |
| 125 | + private static final class TestRawControlFlowGraph extends RawControlFlowGraph { |
| 126 | + private TestRawControlFlowGraph(ClassLoader loader, String className, String methodName) { |
| 127 | + super(loader, className, methodName, Opcodes.ACC_PUBLIC); |
| 128 | + } |
| 129 | + |
| 130 | + private void connect(BytecodeInstruction src, BytecodeInstruction dst) { |
| 131 | + super.addEdge(src, dst, false); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static class SimpleDelegatingClassLoader extends ClassLoader { |
| 136 | + private SimpleDelegatingClassLoader(ClassLoader parent) { |
| 137 | + super(parent); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static class NoExecutionTracerClassLoader extends ClassLoader { |
| 142 | + private NoExecutionTracerClassLoader(ClassLoader parent) { |
| 143 | + super(parent); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { |
| 148 | + if ("org.evomaster.client.java.instrumentation.staticstate.ExecutionTracer".equals(name)) { |
| 149 | + throw new ClassNotFoundException(name); |
| 150 | + } |
| 151 | + return super.loadClass(name, resolve); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
0 commit comments