Extract GitHub tests into separate class and add tests

This commit is contained in:
nicksavers 2014-10-09 17:49:34 +02:00
parent 75802cebbc
commit 65ca63e037
1 changed files with 55 additions and 322 deletions

View File

@ -1,348 +1,81 @@
package org.ethereum.jsontestsuite;
import org.ethereum.db.ByteArrayWrapper;
import java.util.Iterator;
import java.util.List;
import org.ethereum.util.Utils;
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;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Iterator;
import java.util.List;
/**
* www.ethereumJ.com
* Test file specific for tests maintained in the GitHub repository
* by the Ethereum DEV team. <br/>
*
* @author: Roman Mandeleil
* Created on: 29/06/2014 10:46
* @see <a href="https://github.com/ethereum/tests/">https://github.com/ethereum/tests/</a>
*/
public class JSONTestSuiteTest {
@Test // AccountState parsing //
public void test1() throws ParseException {
String expectedNonce = "01";
String expectedBalance = "0de0b6b3a763ff6c";
String expectedCode = "6000600060006000604a3360c85c03f1";
ByteArrayWrapper expectedKey1 = new ByteArrayWrapper(Hex.decode("ffaa"));
ByteArrayWrapper expectedKey2 = new ByteArrayWrapper(Hex.decode("ffab"));
ByteArrayWrapper expectedVal1 = new ByteArrayWrapper(Hex.decode("c8"));
ByteArrayWrapper expectedVal2 = new ByteArrayWrapper(Hex.decode("b2b2b2"));
JSONParser parser = new JSONParser();
String accountString = "{'balance':999999999999999852,'nonce':1," +
"'code':'0x6000600060006000604a3360c85c03f1'," +
"'storage':{'0xffaa' : '0xc8', '0xffab' : '0xb2b2b2'}}";
accountString = accountString.replace("'", "\"");
JSONObject accountJSONObj = (JSONObject)parser.parse(accountString);
AccountState state =
new AccountState(Hex.decode("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6"),
accountJSONObj);
Assert.assertEquals(expectedNonce, Hex.toHexString(state.nonce));
Assert.assertEquals(expectedBalance, Hex.toHexString(state.balance));
Assert.assertEquals(expectedCode, Hex.toHexString(state.code));
Assert.assertTrue(state.storage.keySet().contains(expectedKey1));
Assert.assertTrue(state.storage.keySet().contains(expectedKey2));
Assert.assertTrue(state.storage.values().contains(expectedVal1));
Assert.assertTrue(state.storage.values().contains(expectedVal2));
}
@Test // Exec parsing //
public void test2() throws ParseException {
String expectedAddress = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
String expectedCaller = "cd1722f3947def4cf144679da39c4c32bdc35681";
String expectedData = "ffaabb";
String expectedCode = "6000600060006000604a3360c85c03f1";
String expectedGas = "2710";
String expectedGasPrice = "5af3107a4000";
String expectedOrigin = "cd1722f3947def4cf144679da39c4c32bdc35681";
String expectedValue = "0de0b6b3a7640000";
JSONParser parser = new JSONParser();
String execString = "{'address' : '0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6'," +
" 'caller' : 'cd1722f3947def4cf144679da39c4c32bdc35681'," +
" 'data' : '0xffaabb'," +
" 'code' : '0x6000600060006000604a3360c85c03f1'," +
" 'gas' : 10000," +
" 'gasPrice' : 100000000000000," +
" 'origin' : 'cd1722f3947def4cf144679da39c4c32bdc35681'," +
" 'value' : 1000000000000000000}";
execString = execString.replace("'", "\"");
JSONObject execJSONObj = (JSONObject)parser.parse(execString);
Exec exec = new Exec(execJSONObj);
Assert.assertEquals(expectedAddress, Hex.toHexString(exec.getAddress()));
Assert.assertEquals(expectedCaller, Hex.toHexString(exec.getCaller()));
Assert.assertEquals(expectedData, Hex.toHexString(exec.getData()));
Assert.assertEquals(expectedCode, Hex.toHexString(exec.getCode()));
Assert.assertEquals(expectedGas, Hex.toHexString(exec.getGas()));
Assert.assertEquals(expectedGasPrice, Hex.toHexString(exec.getGasPrice()));
Assert.assertEquals(expectedOrigin, Hex.toHexString(exec.getOrigin()));
Assert.assertEquals(expectedValue, Hex.toHexString(exec.getValue()));
}
@Test // Env parsing //
public void test3() throws ParseException {
String expectedCurrentCoinbase = "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba";
String expectedCurrentDifficulty = "256";
String expectedCurrentGasLimit = "1000000";
String expectedCurrentNumber = "0";
String expectedCurrentTimestamp = "1";
String expectedPreviousHash = "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6";
JSONParser parser = new JSONParser();
String envString = "{'currentCoinbase' : '2adc25665018aa1fe0e6bc666dac8fc2697ff9ba'," +
"'currentDifficulty' : '256'," +
"'currentGasLimit' : '1000000'," +
"'currentNumber' : '0'," +
"'currentTimestamp' : 1," +
"'previousHash' : '5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6'}";
envString = envString.replace("'", "\"");
JSONObject envJSONObj = (JSONObject)parser.parse(envString);
Env env = new Env(envJSONObj);
Assert.assertEquals(expectedCurrentCoinbase, Hex.toHexString(env.getCurrentCoinbase()));
Assert.assertEquals(expectedCurrentDifficulty, new BigInteger(env.getCurrentDifficlty()).toString());
Assert.assertEquals(expectedCurrentGasLimit, new BigInteger(env.getCurrentGasLimit()).toString());
Assert.assertEquals(expectedCurrentNumber, new BigInteger(env.getCurrentNumber()).toString());
Assert.assertEquals(expectedCurrentTimestamp, new BigInteger(env.getCurrentTimestamp()).toString());
Assert.assertEquals(expectedPreviousHash, Hex.toHexString(env.getPreviousHash()));
}
@Test // CallCreate parsing //
public void test4() throws ParseException {
String expectedData = "";
String expectedDestination = "cd1722f3947def4cf144679da39c4c32bdc35681";
String expectedGasLimit = "9792";
String expectedValue = "74";
JSONParser parser = new JSONParser();
String callCreateString = "{'data' : '','destination' : 'cd1722f3947def4cf144679da39c4c32bdc35681','gasLimit' : 9792,'value' : 74}";
callCreateString = callCreateString.replace("'", "\"");
JSONObject callCreateJSONObj = (JSONObject)parser.parse(callCreateString);
CallCreate callCreate = new CallCreate(callCreateJSONObj);
Assert.assertEquals(expectedData, Hex.toHexString(callCreate.getData()));
Assert.assertEquals(expectedDestination, Hex.toHexString(callCreate.getDestination()));
Assert.assertEquals(expectedGasLimit, new BigInteger( callCreate.getGasLimit()).toString());
Assert.assertEquals(expectedValue, new BigInteger( callCreate.getValue()).toString());
}
@Test // TestCase parsing //
public void test5() throws ParseException {
JSONParser parser = new JSONParser();
// String testCaseString = "{'callcreates':[{'data':[],'destination':'cd1722f3947def4cf144679da39c4c32bdc35681','gasLimit':9786,'value':2},{'data':[],'destination':'cd1722f3947def4cf144679da39c4c32bdc35681','gasLimit':9732,'value':12},{'data':[],'destination':'cd1722f3947def4cf144679da39c4c32bdc35681','gasLimit':9696,'value':13},{'data':[],'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':['0x60006001100f603459600060006000600060033360c85c03f150', '0x60016000100f604e59600060006000600060043360c85c03f150','0x60006000100f606859600060006000600060053360c85c03f150', '0x60016001110f6082596000600060006000600c3360c85c03f150','0x60006001110f609c596000600060006000600d3360c85c03f150','0x60016000110f60b6596000600060006000600e3360c85c03f150','0x60006000110f60d0596000600060006000600f3360c85c03f150'],'data':[],'gas':10000,'gasPrice':100000000000000,'origin':'cd1722f3947def4cf144679da39c4c32bdc35681','value':1000000000000000000},'gas':9832,'out':[]}";
String testCaseString = "{'callcreates':[{'data':'','destination':'cd1722f3947def4cf144679da39c4c32bdc35681','gasLimit':200,'value':74}],'env':{'currentCoinbase':'2adc25665018aa1fe0e6bc666dac8fc2697ff9ba','currentDifficulty':'256','currentGasLimit':'1000000','currentNumber':'0','currentTimestamp':1,'previousHash':'5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6'},'exec':{'address':'0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6','caller':'cd1722f3947def4cf144679da39c4c32bdc35681','code':'0x6000600060006000604a3360c8f1','data':'','gas':10000,'gasPrice':100000000000000,'origin':'cd1722f3947def4cf144679da39c4c32bdc35681','value':1000000000000000000},'gas':9973,'out':[],'post':{'0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6':{'balance':999999999999999926,'code':'','nonce':0,'storage':{}}},'pre':{'0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6':{'balance':1000000000000000000,'code':'','nonce':0,'storage':{}}}}";
testCaseString = testCaseString.replace("'", "\"");
JSONObject testCaseJSONObj = (JSONObject)parser.parse(testCaseString);
TestCase testCase = new TestCase(testCaseJSONObj);
int ccList = testCase.getCallCreateList().size();
Assert.assertEquals(1, ccList);
TestRunner runner = new TestRunner();
List<String> result = runner.runTestCase(testCase);
Assert.assertTrue(result.size() == 0);
}
@Test // TestCase file: vmtest-1.json //
public void test6() throws ParseException, IOException, URISyntaxException {
URL vmtest = ClassLoader
.getSystemResource("jsontestsuite/vmtest-1.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-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);
}
@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
@Test // TestCase file: vmtest-4.json //
public void test10() throws ParseException, IOException, URISyntaxException {
URL vmtest = ClassLoader
.getSystemResource("jsontestsuite/vmtest-5.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);
}
public class GitHubJSONTestSuiteTest {
@Test // testing full suite
public void testDirectFromGitHub() throws ParseException {
public void testArithmeticFromGitHub() throws ParseException {
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());
}
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/vmArithmeticTest.json");
runGitHubJsonTest(json);
}
@Test // testing full suite
public void testNameCoinFromGitHub() throws ParseException {
public void testBitwiseLogicOperationFromGitHub() throws ParseException {
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/develop/namecoin.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());
}
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/vmBitwiseLogicOperationTest.json");
runGitHubJsonTest(json);
}
@Test // testing full suite
public void testRandomFromGitHub() throws ParseException {
public void testBlockInfoFromGitHub() throws ParseException {
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/develop/random.json");
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/vmBlockInfoTest.json");
runGitHubJsonTest(json);
}
@Test // testing full suite
public void testEnvironmentalInfoFromGitHub() throws ParseException {
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/vmEnvironmentalInfoTest.json");
runGitHubJsonTest(json);
}
@Test // testing full suite
public void testIOandFlowOperationsFromGitHub() throws ParseException {
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/vmIOandFlowOperationsTest.json");
runGitHubJsonTest(json);
}
@Test // testing full suite
public void testPushDupSwapFromGitHub() throws ParseException {
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/vmPushDupSwapTest.json");
runGitHubJsonTest(json);
}
@Test // testing full suite
public void testShaFromGitHub() throws ParseException {
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/vmSha3Test.json");
runGitHubJsonTest(json);
}
@Test // testing full suite
public void testSystemOperationsFromGitHub() throws ParseException {
String json = Utils.getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/vmSystemOperationsTest.json");
runGitHubJsonTest(json);
}
private void runGitHubJsonTest(String json) throws ParseException {
Assume.assumeFalse("Online test suite is no available", json.equals(""));
JSONParser parser = new JSONParser();