log testing

This commit is contained in:
alon muroch 2014-12-02 17:19:28 +01:00
parent da9fc3d391
commit f9baa5c78a
4 changed files with 105 additions and 3 deletions

View File

@ -0,0 +1,55 @@
package org.ethereum.jsontestsuite;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.ethereum.vm.LogInfo;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.spongycastle.util.encoders.Hex;
public class Logs {
Map<byte[], LogInfo> logs;
public Logs(JSONObject jLogs) {
logs = new HashMap<byte[], LogInfo>();
Set keys = jLogs.keySet();
for(Object k: keys.toArray()) {
byte[] key = Hex.decode((String)k);
JSONObject values = (JSONObject)jLogs.get(k);
byte[] address = Hex.decode((String)values.get("address"));
byte[] data = Hex.decode(((String)values.get("data")).substring(2));
List<byte[]> topics = new ArrayList<byte[]>();
JSONArray jTopics = (JSONArray)values.get("topics");
for(Object t: jTopics.toArray()) {
byte[] topic = Hex.decode(((String)t));
topics.add(topic);
}
LogInfo li = new LogInfo(address, topics, data);
logs.put(key, li);
}
}
/**
* returns null if {@link org.ethereum.vm.LogInfo LogInfo} object was not found for the given key
* @param k
* @return
*/
public LogInfo getLogBySHA3Key(byte[] k) {
if(logs.containsKey(k))
return logs.get(k);
return null;
}
public Iterator<byte[]> getLogsRLPSHA3KeyIterator() {
return logs.keySet().iterator();
}
}

View File

@ -24,6 +24,9 @@ public class TestCase {
// "env": { ... }, // "env": { ... },
private Env env; private Env env;
//
private Logs logs;
// "exec": { ... }, // "exec": { ... },
private Exec exec; private Exec exec;
@ -62,6 +65,11 @@ public class TestCase {
if(testCaseJSONObj.containsKey("callcreates")) if(testCaseJSONObj.containsKey("callcreates"))
callCreates = (JSONArray)testCaseJSONObj.get("callcreates"); callCreates = (JSONArray)testCaseJSONObj.get("callcreates");
JSONObject logsJSON = new JSONObject();
if(testCaseJSONObj.containsKey("logs"))
logsJSON = (JSONObject)testCaseJSONObj.get("logs");
logs = new Logs(logsJSON);
String gasString = "0"; String gasString = "0";
if(testCaseJSONObj.containsKey("gas")) if(testCaseJSONObj.containsKey("gas"))
gasString = testCaseJSONObj.get("gas").toString(); gasString = testCaseJSONObj.get("gas").toString();
@ -115,6 +123,10 @@ public class TestCase {
return exec; return exec;
} }
public Logs getLogs() {
return logs;
}
public byte[] getGas() { public byte[] getGas() {
return gas; return gas;
} }

View File

@ -1,5 +1,6 @@
package org.ethereum.jsontestsuite; package org.ethereum.jsontestsuite;
import org.ethereum.crypto.HashUtil;
import org.ethereum.db.ByteArrayWrapper; import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.db.ContractDetails; import org.ethereum.db.ContractDetails;
import org.ethereum.db.RepositoryImpl; import org.ethereum.db.RepositoryImpl;
@ -69,6 +70,7 @@ public class TestRunner {
/* 2. Create ProgramInvoke - Env/Exec */ /* 2. Create ProgramInvoke - Env/Exec */
Env env = testCase.getEnv(); Env env = testCase.getEnv();
Exec exec = testCase.getExec(); Exec exec = testCase.getExec();
Logs logs = testCase.getLogs();
byte[] address = exec.getAddress(); byte[] address = exec.getAddress();
byte[] origin = exec.getOrigin(); byte[] origin = exec.getOrigin();
@ -223,6 +225,31 @@ public class TestRunner {
results.add(output); results.add(output);
} }
} }
/* asset logs */
List<LogInfo> logResult = program.getResult().getLogInfoList();
Iterator<byte[]> itr = logs.getLogsRLPSHA3KeyIterator();
while(itr.hasNext()) {
byte[] expectedLogKey = itr.next();
System.out.println("Expected key " + Hex.toHexString(expectedLogKey));
LogInfo expectedLogInfo = logs.getLogBySHA3Key(expectedLogKey);
boolean found = false;
for(LogInfo resultLogInfo:logResult) {
byte[] rlpHash = HashUtil.sha3(resultLogInfo.getEncoded());
System.out.println("returned key " + Hex.toHexString(rlpHash));
if(Arrays.equals(expectedLogKey, rlpHash)) {
found = true;
}
}
if(!found) {
String output =
String.format("Expected log [ %s ] was not found", expectedLogInfo.toString());
logger.info(output);
results.add(output);
}
}
} }
// TODO: assert that you have no extra accounts in the repository // TODO: assert that you have no extra accounts in the repository

View File

@ -43,6 +43,7 @@ public class GitHubVMTest {
} }
@Test // testing full suite @Test // testing full suite
@Ignore
public void testIOandFlowOperationsFromGitHub() throws ParseException { public void testIOandFlowOperationsFromGitHub() throws ParseException {
String json = JSONReader.loadJSON("vmtests/vmIOandFlowOperationsTest.json"); String json = JSONReader.loadJSON("vmtests/vmIOandFlowOperationsTest.json");
@ -67,9 +68,16 @@ public class GitHubVMTest {
@Test // testing full suite @Test // testing full suite
@Ignore @Ignore
public void testSystemOperationsFromGitHub() throws ParseException { public void testVMGitHub() throws ParseException {
String json = JSONReader.loadJSON("vmtests/vmSystemOperationsTest.json"); String json = JSONReader.loadJSON("vmtests/vmtests.json");
GitHubJSONTestSuite.runGitHubJsonTest(json);
}
@Test // testing full suite
public void testVMLogGitHub() throws ParseException {
String json = JSONReader.loadJSON("vmtests/vmLogTest.json");
GitHubJSONTestSuite.runGitHubJsonTest(json); GitHubJSONTestSuite.runGitHubJsonTest(json);
} }
} }