Skip to content

Commit

Permalink
EOF testing error codes for layout (hyperledger#7522)
Browse files Browse the repository at this point in the history
Update the eof layout error codes to match codes in reference tests.
This includes support for multiple possible errors for a specific test.

Signed-off-by: Danno Ferrin <[email protected]>
  • Loading branch information
shemnon authored Aug 27, 2024
1 parent 725dcf1 commit b57310f
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ void testSingleInvalidViaInput() {
EvmToolCommand parentCommand = new EvmToolCommand(bais, new PrintWriter(baos, true, UTF_8));
final CodeValidateSubCommand codeValidateSubCommand = new CodeValidateSubCommand(parentCommand);
codeValidateSubCommand.run();
assertThat(baos.toString(UTF_8)).contains("err: layout - EOF header byte 1 incorrect\n");
assertThat(baos.toString(UTF_8))
.contains("err: layout - invalid_magic EOF header byte 1 incorrect\n");
}

@Test
Expand All @@ -71,7 +72,7 @@ void testMultipleViaInput() {
.contains(
"""
OK 1/0/0
err: layout - EOF header byte 1 incorrect
err: layout - invalid_magic EOF header byte 1 incorrect
OK 1/0/0
""");
}
Expand All @@ -97,7 +98,8 @@ void testSingleInvalidViaCli() {
final CommandLine cmd = new CommandLine(codeValidateSubCommand);
cmd.parseArgs(CODE_BAD_MAGIC);
codeValidateSubCommand.run();
assertThat(baos.toString(UTF_8)).contains("err: layout - EOF header byte 1 incorrect\n");
assertThat(baos.toString(UTF_8))
.contains("err: layout - invalid_magic EOF header byte 1 incorrect\n");
}

@Test
Expand All @@ -113,7 +115,7 @@ void testMultipleViaCli() {
.contains(
"""
OK 1/0/0
err: layout - EOF header byte 1 incorrect
err: layout - invalid_magic EOF header byte 1 incorrect
OK 1/0/0
""");
}
Expand Down Expand Up @@ -154,7 +156,7 @@ void testBlankLinesAndCommentsSkipped() {
.isEqualTo(
"""
OK 1/0/0
err: layout - EOF header byte 1 incorrect
err: layout - invalid_magic EOF header byte 1 incorrect
OK 1/0/0
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"ef00010100040200010001040000000080000000ff"
],
"stdin": "",
"stdout": "EOF layout is invalid - Dangling data after end of all sections\n"
"stdout": "EOF layout is invalid - invalid_section_bodies_size data after end of all sections\n"
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
}
},
"stdout": [
{"output":"","gasUsed":"0xd198","test":"create-eof","fork":"Prague","d":0,"g":0,"v":0,"postHash":"0x2a9c58298ba5d4ec86ca682b9fcc9ff67c3fc44dbd39f85a2f9b74bfe4e5178e","postLogsHash":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","pass":false,"error":"Invalid EOF Layout: Expected kind 1 but read kind 17"},
{"output":"","gasUsed":"0xd198","test":"create-eof","fork":"Prague","d":0,"g":0,"v":0,"postHash":"0x2a9c58298ba5d4ec86ca682b9fcc9ff67c3fc44dbd39f85a2f9b74bfe4e5178e","postLogsHash":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","pass":false,"error":"Invalid EOF Layout: unexpected_header_kind expected 1 actual 17"},
{"pc":0,"op":239,"gas":"0x794068","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"INVALID","error":"Bad instruction"},
{"output":"","gasUsed":"0x7a1200","test":"create-eof","fork":"Cancun","d":0,"g":0,"v":0,"postHash":"0xaa80d89bc89f58da8de41d3894bd1a241896ff91f7a5964edaefb39e8e3a4a98","postLogsHash":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","pass":true,"error":"INVALID_OPERATION"}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.base.Splitter;
import org.apache.tuweni.bytes.Bytes;

import org.hyperledger.besu.ethereum.referencetests.EOFTestCaseSpec;
Expand All @@ -31,9 +33,7 @@
import org.hyperledger.besu.evm.Code;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.code.CodeInvalid;
import org.hyperledger.besu.evm.code.CodeV1;
import org.hyperledger.besu.evm.code.EOFLayout;
import org.hyperledger.besu.evm.code.EOFLayout.EOFContainerMode;
import org.hyperledger.besu.testutil.JsonTestParameters;

public class EOFReferenceTestTools {
Expand Down Expand Up @@ -124,6 +124,25 @@ public static void executeTest(
// hardwire in the magic byte transaction checks
if (evm.getMaxEOFVersion() < 1) {
assertThat(expected.exception()).isEqualTo("EOF_InvalidCode");
} else if (code.size() > evm.getEvmVersion().getMaxInitcodeSize()) {
// this check is in EOFCREATE and Transaction validator, but unit tests sniff it out.
assertThat(false)
.withFailMessage(
() ->
"No Expected exception, actual exception - container_size_above_limit "
+ code.size())
.isEqualTo(expected.result());
if (name.contains("eip7692")) {
// if the test is from EEST, validate the exception name.
assertThat("container_size_above_limit")
.withFailMessage(
() ->
"Expected exception: %s actual exception: %s %d"
.formatted(
expected.exception(), "container_size_above_limit ", code.size()))
.containsIgnoringCase(expected.exception().replace("EOFException.", ""));
}

} else {
EOFLayout layout = EOFLayout.parseEOF(code);

Expand All @@ -134,39 +153,27 @@ public static void executeTest(
} else {
parsedCode = evm.getCodeUncached(code);
}
if ("EOF_IncompatibleContainerKind".equals(expected.exception()) && parsedCode.isValid()) {
EOFContainerMode expectedMode =
EOFContainerMode.valueOf(containerKind == null ? "RUNTIME" : containerKind);
EOFContainerMode containerMode =
((CodeV1) parsedCode).getEofLayout().containerMode().get();
EOFContainerMode actualMode =
containerMode == null ? EOFContainerMode.RUNTIME : containerMode;
assertThat(actualMode)
.withFailMessage("Code did not parse to valid containerKind of " + expectedMode)
.isNotEqualTo(expectedMode);
} else {
if (expected.result()) {
if (expected.result()) {
assertThat(parsedCode.isValid())
.withFailMessage(
() -> "Valid code failed with " + ((CodeInvalid) parsedCode).getInvalidReason())
.isTrue();
} else {
assertThat(parsedCode.isValid())
.withFailMessage("Invalid code expected " + expected.exception() + " but was valid")
.isFalse();
if (name.contains("eip7692")) {
// if the test is from EEST, validate the exception name.
assertThat(((CodeInvalid) parsedCode).getInvalidReason())
.withFailMessage(
() ->
"Expected exception :%s actual exception: %s"
.formatted(
expected.exception(),
(parsedCode.isValid()
? null
: ((CodeInvalid) parsedCode).getInvalidReason())))
.containsIgnoringCase(expected.exception().replace("EOFException.", ""));
}
() -> "Valid code failed with " + ((CodeInvalid) parsedCode).getInvalidReason())
.isTrue();
} else {
assertThat(parsedCode.isValid())
.withFailMessage("Invalid code expected " + expected.exception() + " but was valid")
.isFalse();
if (name.contains("eip7692")) {
// if the test is from EEST, validate the exception name.
assertThat(((CodeInvalid) parsedCode).getInvalidReason())
.withFailMessage(
() ->
"Expected exception :%s actual exception: %s"
.formatted(
expected.exception(),
(parsedCode.isValid()
? null
: ((CodeInvalid) parsedCode).getInvalidReason())))
.containsIgnoringCase(expected.exception().replace("EOFException.", ""));
}
}
} else {
Expand All @@ -178,6 +185,25 @@ public static void executeTest(
+ " actual exception - "
+ (layout.isValid() ? null : layout.invalidReason()))
.isEqualTo(expected.result());
if (name.contains("eip7692")) {
// if the test is from EEST, validate the exception name.
boolean exceptionMatched = false;
for (String e : Splitter.on('|').split(expected.exception())) {
if (layout
.invalidReason()
.toLowerCase(Locale.ROOT)
.contains(e.replace("EOFException.", "").toLowerCase(Locale.ROOT))) {
exceptionMatched = true;
break;
}
}
assertThat(exceptionMatched)
.withFailMessage(
() ->
"Expected exception :%s actual exception: %s"
.formatted(expected.exception(), layout.invalidReason()))
.isTrue();
}
}
}
}
Expand Down
Loading

0 comments on commit b57310f

Please sign in to comment.