Skip to content

Commit

Permalink
add ethers-io ABI v2 test cases (MIT License)
Browse files Browse the repository at this point in the history
add ABIJsonTest2.java
  • Loading branch information
esaulpaugh committed May 16, 2019
1 parent f63680e commit 0c919fd
Show file tree
Hide file tree
Showing 11 changed files with 20,916 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/test/java/com/esaulpaugh/headlong/abi/ABIJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class ABIJsonTest {

private static final String RESOURCE = "tests/ABITests/basic_abi_tests.json";
private static final String RESOURCE = "tests/ethereum/ABITests/basic_abi_tests.json";

private static final String TEST_CASES;

Expand Down
209 changes: 209 additions & 0 deletions src/test/java/com/esaulpaugh/headlong/abi/ABIJsonTest2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.Integers;
import com.esaulpaugh.headlong.abi.util.JsonUtils;
import com.esaulpaugh.headlong.util.FastHex;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Iterator;

import static com.esaulpaugh.headlong.abi.UnitType.UNIT_LENGTH_BYTES;
import static com.esaulpaugh.headlong.util.Strings.HEX;
import static com.esaulpaugh.headlong.util.Strings.encode;

public class ABIJsonTest2 {

private static final String RESOURCE = "tests/ethers-io/tests/tests/contract-interface-abi2.json";

private static final JsonArray TEST_CASES;

static {
try {
TEST_CASES = JsonUtils.parseArray(TestUtils.readResourceAsString(ABIJsonTest.class, RESOURCE));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static class TestCase {

private static final Integers.UintType ADDRESS = new Integers.UintType(160);

private final TupleType types;
private final Tuple values;
private final byte[] result;

public TestCase(JsonObject object) throws ParseException {
String typesStr = object.get("types").getAsString();
String valuesStr = object.get("values").getAsString();
String resultStr = object.get("result").getAsString();

JsonArray typesArray = JsonUtils.parseArray(typesStr);
JsonArray valuesArray = JsonUtils.parseArray(valuesStr);

StringBuilder tupleTypeString = new StringBuilder("(");
for (JsonElement e : typesArray) {
tupleTypeString.append(e.getAsString().replace("tuple", "")).append(',');
}

String tts = completeTupleTypeString(tupleTypeString);
this.types = TupleType.parse(tts);
this.values = parseTuple(this.types, valuesArray);
this.result = FastHex.decode(resultStr, 2, resultStr.length() - 2);
}

private boolean test() {
byte[] encoding = types.encode(values).array();
try {
Assert.assertArrayEquals(result, encoding);
return true;
} catch (AssertionError ae) {
// System.out.println(types.canonicalType);
// System.out.println(format(result) + "\n");
// System.out.println(format(encoding));
return false;
}
}

private static String format(byte[] abi) {
StringBuilder sb = new StringBuilder();
int idx = 0;
while(idx < abi.length) {
sb // .append(idx >>> UnitType.LOG_2_UNIT_LENGTH_BYTES)
// .append('\t')
.append(encode(Arrays.copyOfRange(abi, idx, idx + UNIT_LENGTH_BYTES), HEX))
.append('\n');
idx += UNIT_LENGTH_BYTES;
}
return sb.toString();
}

private static Object parseValue(ABIType<?> type, JsonElement value) {
switch (type.typeCode()) {
case ABIType.TYPE_CODE_BOOLEAN: return value.getAsJsonPrimitive().getAsBoolean();
case ABIType.TYPE_CODE_BYTE: return (byte) value.getAsJsonObject().get("value").getAsInt();
case ABIType.TYPE_CODE_INT: return value.getAsJsonObject().get("value").getAsInt();
case ABIType.TYPE_CODE_LONG: {
JsonObject valueObj = value.getAsJsonObject();
String valueValue = valueObj.get("value").getAsString();
return Long.parseLong(valueValue);
}
case ABIType.TYPE_CODE_BIG_INTEGER: {
JsonObject valueObj = value.getAsJsonObject();
String valueValue = valueObj.get("value").getAsString();
if("address".equals(type.canonicalType)) {
BigInteger val = new BigInteger(FastHex.decode(valueValue, 2, valueValue.length() - 2));
return Integers.toUnsigned(val, ADDRESS);
} else {
return new BigInteger(valueValue);
}
}
case ABIType.TYPE_CODE_BIG_DECIMAL: {
String valueValue = value.getAsJsonObject().get("value").getAsString();
return new BigDecimal(new BigInteger(valueValue), 18);
}
case ABIType.TYPE_CODE_ARRAY: return parseArray((ArrayType<?, ?>) type, value);
case ABIType.TYPE_CODE_TUPLE: {
return parseTuple((TupleType) type, value.getAsJsonObject().get("value").getAsJsonArray());
}
default: throw new Error();
}
}

private static Object parseArray(ArrayType<?, ?> arrayType, JsonElement value) {
// System.out.println("____ " + arrayType.canonicalType);
if (arrayType.isString) {
return value.getAsJsonObject().get("value").getAsString();
} else if (value.isJsonArray()) {
JsonArray valueValue = value.getAsJsonArray();
final int len = valueValue.size();
final ABIType<?> elementType = arrayType.elementType;
int i = 0;
if(Boolean.class == elementType.clazz) {
boolean[] array = new boolean[len];
for (Iterator<JsonElement> iter = valueValue.iterator(); i < len; i++) {
array[i] = (Boolean) parseValue(elementType, iter.next());
}
return array;
} else if(Byte.class == elementType.clazz) {
byte[] array = new byte[len];
for (Iterator<JsonElement> iter = valueValue.iterator(); i < len; i++) {
array[i] = (Byte) parseValue(elementType, iter.next());
}
return array;
} if (Integer.class == elementType.clazz) {
int[] array = new int[len];
for (Iterator<JsonElement> iter = valueValue.iterator(); i < len; i++) {
array[i] = (Integer) parseValue(elementType, iter.next());
}
return array;
} else if (Long.class == elementType.clazz) {
long[] array = new long[len];
for (Iterator<JsonElement> iter = valueValue.iterator(); i < len; i++) {
array[i] = (Long) parseValue(elementType, iter.next());
}
return array;
} else {
Object[] array = (Object[]) Array.newInstance(elementType.clazz, len);
for (Iterator<JsonElement> iter = valueValue.iterator(); i < len; i++) {
array[i] = parseValue(elementType, iter.next());
}
return array;
}
} else {
JsonObject valueObj = value.getAsJsonObject();
String valueType = valueObj.get("type").getAsString();
String valueValue = valueObj.get("value").getAsString();
if ("buffer".equals(valueType)) {
return FastHex.decode(valueValue, 2, valueValue.length() - 2);
} else {
throw new RuntimeException("????");
}
}
}

private static Tuple parseTuple(TupleType tupleType, JsonArray values) {
ABIType[] abiTypes = tupleType.elementTypes;
final int len = abiTypes.length;
Object[] elements = new Object[len];
int i = 0;
for (Iterator<JsonElement> iter = values.iterator(); i < len; i++) {
elements[i] = parseValue(abiTypes[i], iter.next());
}
return new Tuple(elements);
}

private static String completeTupleTypeString(StringBuilder canonicalTupleType) {
final int len = canonicalTupleType.length();
if(len == 1) {
return "()";
}
return canonicalTupleType.replace(len - 1, len, ")").toString(); // replace trailing comma
}
}

@Test
public void testMegaJson() throws ParseException {
// int i = 0;
int failed = 0;
for (JsonElement e : TEST_CASES) {
// System.out.println(i);
if(!new TestCase(e.getAsJsonObject()).test()) {
failed++;
}
// i++;
}
System.out.println("failed = " + failed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class RLPJsonDecodeTest {

@Test
public void testValid() throws IOException, DecodeException {
String exampleJson = TestUtils.readResourceAsString(RLPJsonEncodeTest.class, "tests/RLPTests/RandomRLPTests/example.json");
String exampleJson = TestUtils.readResourceAsString(RLPJsonEncodeTest.class, "tests/ethereum/RLPTests/RandomRLPTests/example.json");

for (Map.Entry<String, JsonElement> e : RLPJsonEncodeTest.parseEntrySet(exampleJson)) {
decodeRecursively(RLPJsonEncodeTest.getOutBytes(e));
Expand All @@ -42,7 +42,7 @@ public void testValid() throws IOException, DecodeException {
@Test
public void testInvalid() throws IOException, DecodeException {

String testCasesJson = TestUtils.readResourceAsString(RLPJsonEncodeTest.class, "tests/RLPTests/invalidRLPTest.json");
String testCasesJson = TestUtils.readResourceAsString(RLPJsonEncodeTest.class, "tests/ethereum/RLPTests/invalidRLPTest.json");

for (Map.Entry<String, JsonElement> e : RLPJsonEncodeTest.parseEntrySet(testCasesJson)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class RLPJsonEncodeTest {
@Test
public void testCases() throws IOException {

String testCasesJson = TestUtils.readResourceAsString(RLPJsonEncodeTest.class, "tests/RLPTests/rlptest.json");
String testCasesJson = TestUtils.readResourceAsString(RLPJsonEncodeTest.class, "tests/ethereum/RLPTests/rlptest.json");

for (Map.Entry<String, JsonElement> entry : parseEntrySet(testCasesJson)) {

Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions src/test/resources/tests/ethers-io/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Richard Moore

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 0c919fd

Please sign in to comment.