JSON testing:

+ run testcase from file
+ run the full suite from the url
This commit is contained in:
romanman 2014-07-10 11:31:46 +03:00
parent 0bcf41be5b
commit 4d56d76ef1
11 changed files with 343 additions and 26 deletions

View File

@ -128,13 +128,19 @@ public class Repository {
public ContractDetails getContractDetails(byte[] addr) {
byte[] accountDetailsRLP = contractDetailsDB.get(addr);
if (logger.isInfoEnabled())
logger.info("Account: [ {} ]", Hex.toHexString(addr));
if (accountDetailsRLP == null) {
if (logger.isInfoEnabled())
logger.info("No account: [ {} ]", Hex.toHexString(addr));
byte[] accountDetailsRLP = contractDetailsDB.get(addr);
if (accountDetailsRLP == null) {
return null;
}
if (logger.isInfoEnabled())
logger.info("Contract details RLP: [ {} ]", Hex.toHexString(accountDetailsRLP));
ContractDetails details = new ContractDetails(accountDetailsRLP);
return details;
}
@ -226,6 +232,11 @@ public class Repository {
public void saveCode(byte[] address, byte[] code) {
if (logger.isDebugEnabled())
logger.debug("saveCode: \n address: [ {} ], \n code: [ {} ]",
Hex.toHexString(address),
Hex.toHexString(code));
if (code == null) return;
AccountState state = getAccountState(address);
@ -245,7 +256,12 @@ public class Repository {
accountStateDB.update(address, state.getEncoded());
contractDetailsDB.put(address, details.getEncoded());
}
if (logger.isDebugEnabled())
logger.debug("saveCode: \n accountState: [ {} ], \n contractDetails: [ {} ]",
Hex.toHexString(state.getEncoded()),
Hex.toHexString(details.getEncoded()));
}
public byte[] getRootHash() {
return this.worldState.getRootHash();

View File

@ -19,6 +19,8 @@ import java.util.*;
public class TestCase {
private String name = "";
// "env": { ... },
private Env env;
@ -40,6 +42,12 @@ public class TestCase {
// "callcreates": { ... }
private List<CallCreate> callCreateList = new ArrayList<>();
public TestCase(String name, JSONObject testCaseJSONObj) throws ParseException{
this(testCaseJSONObj);
this.name = name;
}
public TestCase(JSONObject testCaseJSONObj) throws ParseException{
@ -116,6 +124,10 @@ public class TestCase {
return callCreateList;
}
public String getName(){
return name;
}
@Override
public String toString() {
return "TestCase{" +

View File

@ -10,10 +10,7 @@ import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* www.ethereumJ.com
@ -26,13 +23,31 @@ public class TestRunner {
private Logger logger = LoggerFactory.getLogger("JSONTest");
public List<String> runTestSuite(TestSuite testSuite){
Iterator<TestCase> testIterator = testSuite.iterator();
List<String> resultCollector = new ArrayList<>();
while (testIterator.hasNext()){
TestCase testCase = testIterator.next();
logger.info("Running: [ {} ]", testCase.getName());
TestRunner runner = new TestRunner();
List<String> result = runner.runTestCase(testCase);
resultCollector.addAll(result);
}
return resultCollector;
}
public List<String> runTestCase(TestCase testCase){
List<String> results = new ArrayList<>();
Repository repository = new Repository();
/* 1. Store pre-exist accounts - Pre */
for (ByteArrayWrapper key : testCase.getPre().keySet()){

View File

@ -0,0 +1,33 @@
package org.ethereum.jsontestsuite;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import java.util.*;
/**
* www.ethereumJ.com
*
* @author: Roman Mandeleil
* Created on: 10/07/2014 09:46
*/
public class TestSuite {
List<TestCase> testList = new ArrayList<>();
public TestSuite(JSONObject testCaseJSONObj) throws ParseException {
for (Object key: testCaseJSONObj.keySet()){
Object testCaseJSON = testCaseJSONObj.get(key);
TestCase testCase = new TestCase(key.toString(), (JSONObject) testCaseJSON);
testList.add(testCase);
}
}
public Iterator<TestCase> iterator(){
return testList.iterator();
}
}

View File

@ -101,7 +101,7 @@ public class Utils {
}
public static String getHTML(String urlToRead) {
public static String getFromUrl(String urlToRead) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
@ -111,6 +111,7 @@ public class Utils {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
@ -118,7 +119,7 @@ public class Utils {
rd.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
} catch (Throwable e) {
e.printStackTrace();
}
return result;

View File

@ -6,6 +6,8 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
@ -14,8 +16,8 @@ import java.io.IOException;
import java.math.BigInteger;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.util.Iterator;
import java.util.List;
/**
@ -182,8 +184,8 @@ public class JSONTestSuiteTest {
}
@Test // TestCase parsing //
public void test7() throws ParseException, IOException, URISyntaxException {
@Test // TestCase file: vmtest-1.json //
public void test6() throws ParseException, IOException, URISyntaxException {
URL vmtest = ClassLoader
.getSystemResource("jsontestsuite/vmtest-1.json");
@ -195,24 +197,102 @@ public class JSONTestSuiteTest {
JSONParser parser = new JSONParser();
JSONObject testCaseJSONObj = (JSONObject)parser.parse(testSrc);
TestCase testCase = new TestCase(testCaseJSONObj);
int ccList = testCase.getCallCreateList().size();
Assert.assertEquals(1, ccList);
TestSuite testSuite = new TestSuite(testCaseJSONObj);
TestRunner runner = new TestRunner();
List<String> result = runner.runTestCase(testCase);
List<String> result = runner.runTestSuite(testSuite);
Assert.assertTrue(result.size() == 0);
}
@Test // TestCase file: vmtest-2.json //
public void test7() throws ParseException, IOException, URISyntaxException {
URL vmtest = ClassLoader
.getSystemResource("jsontestsuite/vmtest-2.json");
File vmTestFile = new File(vmtest.toURI());
byte[] testData = Files.readAllBytes(vmTestFile.toPath());
String testSrc = new String(testData);
JSONParser parser = new JSONParser();
JSONObject testCaseJSONObj = (JSONObject)parser.parse(testSrc);
TestSuite testSuite = new TestSuite(testCaseJSONObj);
TestRunner runner = new TestRunner();
List<String> result = runner.runTestSuite(testSuite);
Assert.assertTrue(result.size() == 0);
}
@Test // TestCase file: vmtest-3.json //
public void test8() throws ParseException, IOException, URISyntaxException {
URL vmtest = ClassLoader
.getSystemResource("jsontestsuite/vmtest-3.json");
File vmTestFile = new File(vmtest.toURI());
byte[] testData = Files.readAllBytes(vmTestFile.toPath());
String testSrc = new String(testData);
JSONParser parser = new JSONParser();
JSONObject testCaseJSONObj = (JSONObject)parser.parse(testSrc);
TestSuite testSuite = new TestSuite(testCaseJSONObj);
TestRunner runner = new TestRunner();
List<String> result = runner.runTestSuite(testSuite);
Assert.assertTrue(result.size() == 0);
}
@Ignore //TODO: suicide test should fixed
@Test // TestCase file: vmtest-4.json //
public void test9() throws ParseException, IOException, URISyntaxException {
URL vmtest = ClassLoader
.getSystemResource("jsontestsuite/vmtest-4.json");
File vmTestFile = new File(vmtest.toURI());
byte[] testData = Files.readAllBytes(vmTestFile.toPath());
String testSrc = new String(testData);
JSONParser parser = new JSONParser();
JSONObject testCaseJSONObj = (JSONObject)parser.parse(testSrc);
TestSuite testSuite = new TestSuite(testCaseJSONObj);
TestRunner runner = new TestRunner();
List<String> result = runner.runTestSuite(testSuite);
Assert.assertTrue(result.size() == 0);
}
@Ignore //TODO: suicide test should fixed
@Test // testing full suite
public void testDirectFromGitHub(){
public void testDirectFromGitHub() throws ParseException {
String json = Utils.getHTML("https://raw.githubusercontent.com/ethereum/tests/develop/vmtests.json");
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/develop/vmtests.json");
Assume.assumeFalse("Online test suite is no available", json.equals(""));
JSONParser parser = new JSONParser();
JSONObject testSuiteObj = (JSONObject)parser.parse(json);
TestSuite testSuite = new TestSuite(testSuiteObj);
Iterator<TestCase> testIterator = testSuite.iterator();
while (testIterator.hasNext()){
TestCase testCase = testIterator.next();
System.out.println("Running: " + testCase.getName());
TestRunner runner = new TestRunner();
List<String> result = runner.runTestCase(testCase);
Assert.assertTrue(result.isEmpty());
}
}

View File

@ -1,4 +1,4 @@
{
{"arithm" : {
"callcreates": [
{
"data": "0x",
@ -46,4 +46,4 @@
}
}
}
}
}}

View File

@ -0,0 +1,67 @@
{"boolean": {
"callcreates": [
{
"data": "0x",
"destination": "cd1722f3947def4cf144679da39c4c32bdc35681",
"gasLimit": 9786,
"value": 2
},
{
"data": "0x",
"destination": "cd1722f3947def4cf144679da39c4c32bdc35681",
"gasLimit": 9732,
"value": 12
},
{
"data": "0x",
"destination": "cd1722f3947def4cf144679da39c4c32bdc35681",
"gasLimit": 9696,
"value": 13
},
{
"data": "0x",
"destination": "cd1722f3947def4cf144679da39c4c32bdc35681",
"gasLimit": 9660,
"value": 14
}
],
"env": {
"currentCoinbase": "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentDifficulty": "256",
"currentGasLimit": "1000000",
"currentNumber": "0",
"currentTimestamp": 1,
"previousHash": "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
},
"exec": {
"address": "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"caller": "cd1722f3947def4cf144679da39c4c32bdc35681",
"code": "0x60016001100f601a59600060006000600060023360c85c03f15060006001100f603459600060006000600060033360c85c03f15060016000100f604e59600060006000600060043360c85c03f15060006000100f606859600060006000600060053360c85c03f15060016001110f6082596000600060006000600c3360c85c03f15060006001110f609c596000600060006000600d3360c85c03f15060016000110f60b6596000600060006000600e3360c85c03f15060006000110f60d0596000600060006000600f3360c85c03f150",
"data": "0x",
"gas": 10000,
"gasPrice": 100000000000000,
"origin": "cd1722f3947def4cf144679da39c4c32bdc35681",
"value": 1000000000000000000
},
"gas": 9832,
"out": [
],
"post": {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6": {
"balance": 999999999999999959,
"code": "0x60016001100f601a59600060006000600060023360c85c03f15060006001100f603459600060006000600060033360c85c03f15060016000100f604e59600060006000600060043360c85c03f15060006000100f606859600060006000600060053360c85c03f15060016001110f6082596000600060006000600c3360c85c03f15060006001110f609c596000600060006000600d3360c85c03f15060016000110f60b6596000600060006000600e3360c85c03f15060006000110f60d0596000600060006000600f3360c85c03f150",
"nonce": 0,
"storage": {
}
}
},
"pre": {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6": {
"balance": 1000000000000000000,
"code": "0x60016001100f601a59600060006000600060023360c85c03f15060006001100f603459600060006000600060033360c85c03f15060016000100f604e59600060006000600060043360c85c03f15060006000100f606859600060006000600060053360c85c03f15060016001110f6082596000600060006000600c3360c85c03f15060006001110f609c596000600060006000600d3360c85c03f15060016000110f60b6596000600060006000600e3360c85c03f15060006000110f60d0596000600060006000600f3360c85c03f150",
"nonce": 0,
"storage": {
}
}
}
}}

View File

@ -0,0 +1,49 @@
{"mktx": {
"callcreates": [
{
"data": "0x",
"destination": "cd1722f3947def4cf144679da39c4c32bdc35681",
"gasLimit": 9792,
"value": 500000000000000000
}
],
"env": {
"currentCoinbase": "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentDifficulty": "256",
"currentGasLimit": "1000000",
"currentNumber": "0",
"currentTimestamp": 1,
"previousHash": "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
},
"exec": {
"address": "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"caller": "cd1722f3947def4cf144679da39c4c32bdc35681",
"code": "0x60006000600060006706f05b59d3b200003360c85c03f1",
"data": "0x",
"gas": 10000,
"gasPrice": 100000000000000,
"origin": "cd1722f3947def4cf144679da39c4c32bdc35681",
"value": 1000000000000000000
},
"gas": 9971,
"out": [
],
"post": {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6": {
"balance": 500000000000000000,
"code": "0x60006000600060006706f05b59d3b200003360c85c03f1",
"nonce": 0,
"storage": {
}
}
},
"pre": {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6": {
"balance": 1000000000000000000,
"code": "0x60006000600060006706f05b59d3b200003360c85c03f1",
"nonce": 0,
"storage": {
}
}
}
}}

View File

@ -0,0 +1,44 @@
{"suicide": {
"callcreates": [
],
"env": {
"currentCoinbase": "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentDifficulty": "256",
"currentGasLimit": "1000000",
"currentNumber": "0",
"currentTimestamp": 1,
"previousHash": "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
},
"exec": {
"address": "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"caller": "cd1722f3947def4cf144679da39c4c32bdc35681",
"code": "0x10f9",
"data": "0x",
"gas": 10000,
"gasPrice": 100000000000000,
"origin": "cd1722f3947def4cf144679da39c4c32bdc35681",
"value": 1000000000000000000
},
"gas": 9999,
"out": [
],
"post": {
"cd1722f3947def4cf144679da39c4c32bdc35681": {
"balance": 1000000000000000000,
"code": "0x",
"nonce": 0,
"storage": {
}
}
},
"pre": {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6": {
"balance": 1000000000000000000,
"code": "0x33ff",
"nonce": 0,
"storage": {
}
}
}
}
}

File diff suppressed because one or more lines are too long