Skip to content

Commit ca8636b

Browse files
committed
[test] Add evm assembly json import/export tests.
1 parent 5a0fc55 commit ca8636b

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

scripts/ASTImportTest.sh

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ source "${REPO_ROOT}/scripts/common.sh"
3939

4040
function print_usage
4141
{
42-
fail "Usage: ${0} ast [--exit-on-error]."
42+
fail "Usage: ${0} ast|evm-assembly [--exit-on-error]."
4343
}
4444

4545
function print_used_commands
@@ -75,13 +75,15 @@ for PARAM in "$@"
7575
do
7676
case "$PARAM" in
7777
ast) IMPORT_TEST_TYPE="ast" ;;
78+
evm-assembly) IMPORT_TEST_TYPE="evm-assembly" ;;
7879
--exit-on-error) EXIT_ON_ERROR=1 ;;
7980
*) print_usage ;;
8081
esac
8182
done
8283

8384
SYNTAXTESTS_DIR="${REPO_ROOT}/test/libsolidity/syntaxTests"
8485
ASTJSONTESTS_DIR="${REPO_ROOT}/test/libsolidity/ASTJSON"
86+
SEMANTICTESTS_DIR="${REPO_ROOT}/test/libsolidity/semanticTests"
8587

8688
FAILED=0
8789
UNCOMPILABLE=0
@@ -127,6 +129,107 @@ function ast_import_export_equivalence
127129
TESTED=$((TESTED + 1))
128130
}
129131

132+
function evmjson_import_export_equivalence
133+
{
134+
local sol_file="$1"
135+
local input_files=( "${@:2}" )
136+
local outputs=( "asm" "bin" "bin-runtime" "opcodes" "srcmap" "srcmap-runtime" )
137+
local export_command=("$SOLC" --combined-json "$(IFS=, ; echo "${outputs[*]}")" --pretty-json --json-indent 4 "${input_files[@]}")
138+
local success=1
139+
if ! "${export_command[@]}" > expected.json 2> expected.error
140+
then
141+
success=0
142+
printError "❌ ERROR: (export) EVM Assembly JSON reimport failed for ${sol_file}"
143+
if [[ $EXIT_ON_ERROR == 1 ]]
144+
then
145+
print_used_commands "$(pwd)" "${export_command[*]}" ""
146+
return 1
147+
fi
148+
fi
149+
150+
# Note that we have some test files, that only consists of free functions.
151+
# Those files doesn't define any contracts, so the resulting json does not define any
152+
# keys. In this case `jq` returns an error like `jq: error: null (null) has no keys`
153+
# to not get spammed by these errors, errors are redirected to /dev/null.
154+
for contract in $(jq '.contracts | keys | .[]' expected.json 2> /dev/null)
155+
do
156+
for output in "${outputs[@]}"
157+
do
158+
jq --raw-output ".contracts.${contract}.\"${output}\"" expected.json > "expected.${output}.json"
159+
done
160+
161+
assembly=$(cat expected.asm.json)
162+
[[ $assembly != "" && $assembly != "null" ]] || continue
163+
164+
local import_command=("${SOLC}" --combined-json "bin,bin-runtime,opcodes,asm,srcmap,srcmap-runtime" --pretty-json --json-indent 4 --import-asm-json expected.asm.json)
165+
if ! "${import_command[@]}" > obtained.json 2> obtained.error
166+
then
167+
success=0
168+
printError "❌ ERROR: (import) EVM Assembly JSON reimport failed for ${sol_file}"
169+
if [[ $EXIT_ON_ERROR == 1 ]]
170+
then
171+
print_used_commands "$(pwd)" "${export_command[*]}" "${import_command[*]}"
172+
return 1
173+
fi
174+
fi
175+
176+
for output in "${outputs[@]}"
177+
do
178+
for obtained_contract in $(jq '.contracts | keys | .[]' obtained.json 2> /dev/null)
179+
do
180+
jq --raw-output ".contracts.${obtained_contract}.\"${output}\"" obtained.json > "obtained.${output}.json"
181+
# compare expected and obtained evm assembly json
182+
if ! diff_files "expected.${output}.json" "obtained.${output}.json"
183+
then
184+
success=0
185+
printError "❌ ERROR: (${output}) EVM Assembly JSON reimport failed for ${sol_file}"
186+
if [[ $EXIT_ON_ERROR == 1 ]]
187+
then
188+
print_used_commands "$(pwd)" "${export_command[*]}" "${import_command[*]}"
189+
return 1
190+
fi
191+
fi
192+
done
193+
done
194+
195+
# direct export via --asm-json, if imported with --import-asm-json.
196+
if ! "${SOLC}" --asm-json --import-asm-json expected.asm.json --pretty-json --json-indent 4 | tail -n+4 > obtained_direct_import_export.json 2> obtained_direct_import_export.error
197+
then
198+
success=0
199+
printError "❌ ERROR: (direct) EVM Assembly JSON reimport failed for ${sol_file}"
200+
if [[ $EXIT_ON_ERROR == 1 ]]
201+
then
202+
print_used_commands "$(pwd)" "${SOLC} --asm-json --import-asm-json expected.asm.json --pretty-json --json-indent 4 | tail -n+4" ""
203+
return 1
204+
fi
205+
fi
206+
207+
# reformat json files using jq.
208+
jq . expected.asm.json > expected.asm.json.pretty
209+
jq . obtained_direct_import_export.json > obtained_direct_import_export.json.pretty
210+
211+
# compare expected and obtained evm assembly.
212+
if ! diff_files expected.asm.json.pretty obtained_direct_import_export.json.pretty
213+
then
214+
success=0
215+
printError "❌ ERROR: EVM Assembly JSON reimport failed for ${sol_file}"
216+
if [[ $EXIT_ON_ERROR == 1 ]]
217+
then
218+
print_used_commands "$(pwd)" "${export_command[*]}" "${import_command[*]}"
219+
return 1
220+
fi
221+
fi
222+
done
223+
224+
if (( success == 1 ))
225+
then
226+
echo -n ""
227+
TESTED=$((TESTED + 1))
228+
else
229+
FAILED=$((FAILED + 1))
230+
fi
231+
}
232+
130233
# function tests whether exporting and importing again is equivalent.
131234
# Results are recorded by adding to FAILED or UNCOMPILABLE.
132235
# Also, in case of a mismatch a diff is printed
@@ -149,6 +252,7 @@ function test_import_export_equivalence {
149252
then
150253
case "$IMPORT_TEST_TYPE" in
151254
ast) ast_import_export_equivalence "${sol_file}" "${input_files[@]}" ;;
255+
evm-assembly) evmjson_import_export_equivalence "${sol_file}" "${input_files[@]}" ;;
152256
*) fail "Unknown import test type '${IMPORT_TEST_TYPE}'. Aborting." ;;
153257
esac
154258
else
@@ -173,6 +277,7 @@ command_available jq --version
173277

174278
case "$IMPORT_TEST_TYPE" in
175279
ast) TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}") ;;
280+
evm-assembly) TEST_DIRS=("${SYNTAXTESTS_DIR}" "${SEMANTICTESTS_DIR}") ;;
176281
*) print_usage ;;
177282
esac
178283

test/cmdlineTests.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,18 @@ SOLTMPDIR=$(mktemp -d)
668668
)
669669
rm -r "$SOLTMPDIR"
670670

671+
printTask "Testing EVM Assembly JSON import/export..."
672+
SOLTMPDIR=$(mktemp -d)
673+
(
674+
cd "$SOLTMPDIR"
675+
if ! "$REPO_ROOT/scripts/ASTImportTest.sh" evm-assembly
676+
then
677+
rm -r "$SOLTMPDIR"
678+
fail
679+
fi
680+
)
681+
rm -r "$SOLTMPDIR"
682+
671683
printTask "Testing AST import/export..."
672684
SOLTMPDIR=$(mktemp -d)
673685
(

0 commit comments

Comments
 (0)