#!/usr/bin/env bash # Bash script to test the asm-json-import input mode of the compiler by # first exporting a .sol file to JSON that containing assembly json, deploy & runtime bytecode, opcodes and source mappings, # then the compiler is invoked in assembly json import mode `--import-asm-json` and uses the previously # generated assembly. This output will be stored. # Finally, the originally generated outputs (bin, bin-runtime, opcodes, asm, srcmap and srcmap-runtime) # will be compared with the outputs that where generated by using the assembly json file as input. set -eo pipefail READLINK=readlink if [[ "$OSTYPE" == "darwin"* ]]; then READLINK=greadlink fi REPO_ROOT=$(${READLINK} -f "$(dirname "$0")"/..) # shellcheck source=scripts/common_import.sh source "${REPO_ROOT}/scripts/common_import.sh" SEMANTICTESTS_DIR="${REPO_ROOT}/test/libsolidity/semanticTests" NSOURCES="$(find "$SEMANTICTESTS_DIR" -type f | wc -l)" init_import_tests # function tests whether importing an assembly json file creates identical bytecode. # Results are recorded by adding to FAILED or UNCOMPILABLE. # Also, in case of a mismatch a diff and the respective ASTs are printed # Expected parameters: # $1 name of the file to be exported and imported # $2 any files needed to do so that might be in parent directories function testImportExportEquivalence { local nth_input_file="$1" IFS=" " read -r -a all_input_files <<< "$2" if $SOLC "$nth_input_file" "${all_input_files[@]}" --combined-json asm,bin > /dev/null 2>&1 then local types=( "bin" "bin-runtime" "opcodes" "asm" "srcmap" "srcmap-runtime" ) # save exported json as expected result (silently) $SOLC --combined-json asm,opcodes,bin,srcmap,srcmap-runtime,bin-runtime --pretty-json "$nth_input_file" "${all_input_files[@]}" > expected.json 2> /dev/null for contract in $(jq '.contracts | keys | .[]' expected.json 2> /dev/null) do for type in "${types[@]}" do jq --raw-output ".contracts.${contract}.\"${type}\"" expected.json > "expected.${type}" done expected_bin=$(cat expected.bin) if [[ $expected_bin == "" ]] then continue fi if ! "$SOLC" --import-asm-json expected.asm --combined-json asm,opcodes,bin,srcmap,srcmap-runtime,bin-runtime > imported.json 2> /dev/null then # For investigating, use exit 1 here so the script stops at the # first failing test # exit 1 echo "" echo "Failed with contract ${contract}!?" echo "" FAILED=$((FAILED + 1)) return 1 fi for type in "${types[@]}" do jq --raw-output ".contracts.\"expected.asm\".\"${type}\"" imported.json > "imported.${type}" if ! diff "expected.${type}" "imported.${type}" then echo "" echo "Failed with contract ${contract} (${type})." echo "" if [ "$DIFFVIEW" == "" ] then echo "Expected:" cat "./expected.${type}" echo "Obtained:" cat "./imported.${type}" else # Use user supplied diff view mismatched output $DIFFVIEW "expected.${type}" "imported.${type}" fi FAILED=$((FAILED + 1)) return 2 fi done done TESTED=$((TESTED + 1)) rm -f expected.json rm -f imported.json for type in "${types[@]}" do rm -f "expected.${type}" rm -f "imported.${type}" done else # echo "contract $solfile could not be compiled " UNCOMPILABLE=$((UNCOMPILABLE + 1)) fi } echo "Looking at $NSOURCES .sol files..." TEST_FILES=$(find "$SEMANTICTESTS_DIR" -name "*.sol") run_import_tests "$TEST_FILES" "$SPLITSOURCES" "$NSOURCES" "$PWD"