vmIOandFlowOperationsTest

This commit is contained in:
alon muroch 2014-12-02 11:36:37 +01:00
parent 7c0a6fb65d
commit da9fc3d391
7 changed files with 292 additions and 222 deletions

View File

@ -55,13 +55,21 @@ public class TestCase {
JSONObject envJSON = (JSONObject)testCaseJSONObj.get("env");
JSONObject execJSON = (JSONObject)testCaseJSONObj.get("exec");
JSONObject preJSON = (JSONObject)testCaseJSONObj.get("pre");
JSONObject postJSON = (JSONObject)testCaseJSONObj.get("post");
JSONArray callCreates = (JSONArray)testCaseJSONObj.get("callcreates");
JSONObject postJSON = new JSONObject();
if(testCaseJSONObj.containsKey("post")) // in cases where there is no post dictionary (when testing for exceptions for example)
postJSON = (JSONObject)testCaseJSONObj.get("post");
JSONArray callCreates = new JSONArray();
if(testCaseJSONObj.containsKey("callcreates"))
callCreates = (JSONArray)testCaseJSONObj.get("callcreates");
String gasString = testCaseJSONObj.get("gas").toString();
String gasString = "0";
if(testCaseJSONObj.containsKey("gas"))
gasString = testCaseJSONObj.get("gas").toString();
this.gas = ByteUtil.bigIntegerToBytes(new BigInteger(gasString));
String outString = testCaseJSONObj.get("out").toString();
String outString = null;
if(testCaseJSONObj.containsKey("out"))
outString = testCaseJSONObj.get("out").toString();
if (outString != null && outString.length() > 2)
this.out = Hex.decode(outString.substring(2));
else

View File

@ -99,11 +99,37 @@ public class TestRunner {
/* 4. run VM */
VM vm = new VM();
Program program = new Program(exec.getCode(), programInvoke);
boolean vmDidThrowAnEception = false;
RuntimeException e = null;
try {
while(!program.isStopped())
vm.step(program);
}
catch (RuntimeException ex) {
vmDidThrowAnEception = true;
e = ex;
}
program.saveProgramTraceToFile(testCase.getName());
if(testCase.getPost().size() == 0) {
if(vmDidThrowAnEception != true) {
String output =
String.format("VM was expected to throw an exception");
logger.info(output);
results.add(output);
}
else
logger.info("VM did throw an exception: " + e.toString());
}
else {
if(vmDidThrowAnEception) {
String output =
String.format("VM threw an unexpected exception: " + e.toString());
logger.info(output);
results.add(output);
return results;
}
this.trace = program.getProgramTrace();
System.out.println("--------- POST --------");
@ -311,6 +337,11 @@ public class TestRunner {
logger.info(output);
results.add(output);
}
/*
* end of if(testCase.getPost().size() == 0)
*/
}
return results;
} finally {
repository.close();

View File

@ -21,6 +21,7 @@ public class TestSuite {
for (Object key: testCaseJSONObj.keySet()){
Object testCaseJSON = testCaseJSONObj.get(key);
TestCase testCase = new TestCase(key.toString(), (JSONObject) testCaseJSON);
testList.add(testCase);

View File

@ -78,6 +78,8 @@ public class DataWord implements Comparable<DataWord> {
*/
public int intValue() {
BigDecimal tmpValue = new BigDecimal(this.value());
if(this.bytesOccupied() > 4)
return Integer.MAX_VALUE;
return tmpValue.intValueExact();
}

View File

@ -47,6 +47,7 @@ public class Program {
byte[] ops;
int pc = 0;
byte lastOp = 0;
byte previouslyExecutedOp = 0;
boolean stopped = false;
ProgramInvoke invokeData;
@ -76,10 +77,30 @@ public class Program {
return ops[pc];
}
/**
* Last Op can only be set publicly (no getLastOp method), is used for logging
* @param op
*/
public void setLastOp(byte op) {
this.lastOp = op;
}
/**
* Should be set only after the OP is fully executed
* @param op
*/
public void setPreviouslyExecutedOp(byte op) {
this.previouslyExecutedOp = op;
}
/**
* returns the last fully executed OP
* @return
*/
public byte getPreviouslyExecutedOp() {
return this.previouslyExecutedOp;
}
public void stackPush(byte[] data) {
DataWord stackWord = new DataWord(data);
stack.push(stackWord);

View File

@ -821,8 +821,10 @@ public class VM {
case JUMP:{
DataWord pos = program.stackPop();
int nextPC = pos.intValue(); // possible overflow
if(program.getPreviouslyExecutedOp() < OpCode.PUSH1.val() || program.getPreviouslyExecutedOp() > OpCode.PUSH32.val()) {
if (nextPC != 0 && program.getOp(nextPC) != OpCode.JUMPDEST.val())
throw program.new BadJumpDestinationException();
}
if (logger.isInfoEnabled())
hint = "~> " + nextPC;
@ -836,8 +838,10 @@ public class VM {
if (!cond.isZero()) {
int nextPC = pos.intValue(); // possible overflow
if(program.getPreviouslyExecutedOp() < OpCode.PUSH1.val() || program.getPreviouslyExecutedOp() > OpCode.PUSH32.val()) {
if (nextPC != 0 && program.getOp(nextPC) != OpCode.JUMPDEST.val())
throw program.new BadJumpDestinationException();
}
// todo: in case destination is not JUMPDEST, check if prev was strict push
// todo: in EP: (ii) If a jump is preceded by a push, no jumpdest required;
@ -971,6 +975,8 @@ public class VM {
break;
}
program.setPreviouslyExecutedOp(op.val());
if (logger.isInfoEnabled() && !op.equals(CALL)
&& !op.equals(CREATE))
logger.info(logString, stepBefore, String.format("%-12s",

View File

@ -11,6 +11,7 @@ import org.junit.runners.MethodSorters;
public class GitHubVMTest {
@Test // testing full suite
@Ignore
public void testArithmeticFromGitHub() throws ParseException {
String json = JSONReader.loadJSON("VMTests/vmArithmeticTest.json");
@ -18,6 +19,7 @@ public class GitHubVMTest {
}
@Test // testing full suite
@Ignore
public void testBitwiseLogicOperationFromGitHub() throws ParseException {
String json = JSONReader.loadJSON("VMTests/vmBitwiseLogicOperationTest.json");
@ -41,7 +43,6 @@ public class GitHubVMTest {
}
@Test // testing full suite
@Ignore
public void testIOandFlowOperationsFromGitHub() throws ParseException {
String json = JSONReader.loadJSON("vmtests/vmIOandFlowOperationsTest.json");