|
| 1 | +package org.tron.common.runtime; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | + |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.util.Collections; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.Test; |
| 10 | +import org.mockito.MockedStatic; |
| 11 | +import org.mockito.Mockito; |
| 12 | +import org.tron.common.runtime.vm.LogInfo; |
| 13 | +import org.tron.core.actuator.VMActuator; |
| 14 | +import org.tron.core.db.TransactionContext; |
| 15 | +import org.tron.core.vm.OperationRegistry; |
| 16 | +import org.tron.core.vm.VM; |
| 17 | +import org.tron.core.vm.config.VMConfig; |
| 18 | +import org.tron.core.vm.program.Program; |
| 19 | + |
| 20 | +public class VMActuatorMockTest { |
| 21 | + |
| 22 | + @BeforeClass |
| 23 | + public static void init() { |
| 24 | + // warm up the registry so VM.play(..., OperationRegistry.getTable()) arg eval is safe |
| 25 | + OperationRegistry.init(); |
| 26 | + } |
| 27 | + |
| 28 | + private void runCatchPathTest(Throwable thrownByVm, boolean osakaOn, int expectedLogSize) |
| 29 | + throws Exception { |
| 30 | + boolean prevOsaka = VMConfig.allowTvmOsaka(); |
| 31 | + VMConfig.initAllowTvmOsaka(osakaOn ? 1 : 0); |
| 32 | + try (MockedStatic<VM> vmMock = Mockito.mockStatic(VM.class)) { |
| 33 | + Program program = Mockito.mock(Program.class); |
| 34 | + ProgramResult result = new ProgramResult(); |
| 35 | + result.addLogInfo(new LogInfo(new byte[20], Collections.emptyList(), new byte[0])); |
| 36 | + Mockito.when(program.getResult()).thenReturn(result); |
| 37 | + |
| 38 | + vmMock.when(() -> VM.play(any(), any())).thenThrow(thrownByVm); |
| 39 | + |
| 40 | + VMActuator actuator = new VMActuator(false); |
| 41 | + Field f = VMActuator.class.getDeclaredField("program"); |
| 42 | + f.setAccessible(true); |
| 43 | + f.set(actuator, program); |
| 44 | + |
| 45 | + TransactionContext context = Mockito.mock(TransactionContext.class); |
| 46 | + Mockito.when(context.getProgramResult()).thenReturn(new ProgramResult()); |
| 47 | + |
| 48 | + actuator.execute(context); |
| 49 | + |
| 50 | + Assert.assertEquals(expectedLogSize, result.getLogInfoList().size()); |
| 51 | + } finally { |
| 52 | + VMConfig.initAllowTvmOsaka(prevOsaka ? 1 : 0); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void osakaClearsLogOnOutOfTime() throws Exception { |
| 58 | + runCatchPathTest(new Program.OutOfTimeException("timeout"), true, 0); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void osakaClearsLogOnJvmStackOverflow() throws Exception { |
| 63 | + runCatchPathTest(new Program.JVMStackOverFlowException(), true, 0); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void osakaClearsLogOnThrowable() throws Exception { |
| 68 | + runCatchPathTest(new RuntimeException("boom"), true, 0); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void preOsakaKeepsLogOnOutOfTime() throws Exception { |
| 73 | + runCatchPathTest(new Program.OutOfTimeException("timeout"), false, 1); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void preOsakaKeepsLogOnJvmStackOverflow() throws Exception { |
| 78 | + runCatchPathTest(new Program.JVMStackOverFlowException(), false, 1); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void preOsakaKeepsLogOnThrowable() throws Exception { |
| 83 | + runCatchPathTest(new RuntimeException("boom"), false, 1); |
| 84 | + } |
| 85 | +} |
0 commit comments