2019-09-11 19:16:35 +00:00
|
|
|
#!/usr/bin/env bash
|
2022-11-24 15:16:16 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# vim:ts=4:et
|
|
|
|
# This file is part of solidity.
|
|
|
|
#
|
|
|
|
# solidity is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# solidity is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with solidity. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
#
|
|
|
|
# (c) solidity contributors.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Bash script to test the import/exports.
|
2022-11-02 04:58:45 +00:00
|
|
|
#
|
2022-11-24 15:16:16 +00:00
|
|
|
# ast import/export tests:
|
|
|
|
# - first exporting a .sol file to JSON, then loading it into the compiler
|
|
|
|
# and exporting it again. The second JSON should be identical to the first.
|
|
|
|
|
|
|
|
set -euo pipefail
|
2019-09-11 19:16:35 +00:00
|
|
|
|
2020-09-01 00:04:38 +00:00
|
|
|
READLINK=readlink
|
|
|
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
|
|
READLINK=greadlink
|
|
|
|
fi
|
|
|
|
REPO_ROOT=$(${READLINK} -f "$(dirname "$0")"/..)
|
2020-04-17 13:02:40 +00:00
|
|
|
SOLIDITY_BUILD_DIR=${SOLIDITY_BUILD_DIR:-${REPO_ROOT}/build}
|
2022-11-24 15:16:16 +00:00
|
|
|
SOLC="${SOLIDITY_BUILD_DIR}/solc/solc"
|
|
|
|
SPLITSOURCES="${REPO_ROOT}/scripts/splitSources.py"
|
|
|
|
|
|
|
|
# shellcheck source=scripts/common.sh
|
|
|
|
source "${REPO_ROOT}/scripts/common.sh"
|
|
|
|
|
|
|
|
function print_usage
|
|
|
|
{
|
|
|
|
echo "Usage: ${0} ast [--exit-on-error|--help]."
|
|
|
|
}
|
|
|
|
|
|
|
|
function print_used_commands
|
|
|
|
{
|
|
|
|
local test_directory="$1"
|
|
|
|
local export_command="$2"
|
|
|
|
local import_command="$3"
|
|
|
|
printError "You can find the files used for this test here: ${test_directory}"
|
|
|
|
printError "Used commands for test:"
|
|
|
|
printError "# export"
|
|
|
|
echo "$ ${export_command}" >&2
|
|
|
|
printError "# import"
|
|
|
|
echo "$ ${import_command}" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
function print_stderr_stdout
|
|
|
|
{
|
|
|
|
local error_message="$1"
|
|
|
|
local stderr_file="$2"
|
|
|
|
local stdout_file="$3"
|
|
|
|
printError "$error_message"
|
|
|
|
printError ""
|
|
|
|
printError "stderr:"
|
|
|
|
cat "$stderr_file" >&2
|
|
|
|
printError ""
|
|
|
|
printError "stdout:"
|
|
|
|
cat "$stdout_file" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_import_test_type_unset
|
|
|
|
{
|
|
|
|
[[ -z "$IMPORT_TEST_TYPE" ]] || fail "ERROR: Import test type can only be set once. Aborting."
|
|
|
|
}
|
|
|
|
|
|
|
|
IMPORT_TEST_TYPE=
|
|
|
|
EXIT_ON_ERROR=0
|
|
|
|
for PARAM in "$@"
|
|
|
|
do
|
|
|
|
case "$PARAM" in
|
|
|
|
ast) check_import_test_type_unset ; IMPORT_TEST_TYPE="ast" ;;
|
|
|
|
--help) print_usage ; exit 0 ;;
|
|
|
|
--exit-on-error) EXIT_ON_ERROR=1 ;;
|
|
|
|
*) fail "Unknown option '$PARAM'. Aborting. $(print_usage)" ;;
|
|
|
|
esac
|
|
|
|
done
|
2019-09-11 19:16:35 +00:00
|
|
|
|
|
|
|
SYNTAXTESTS_DIR="${REPO_ROOT}/test/libsolidity/syntaxTests"
|
2020-03-05 11:56:14 +00:00
|
|
|
ASTJSONTESTS_DIR="${REPO_ROOT}/test/libsolidity/ASTJSON"
|
2019-09-11 19:16:35 +00:00
|
|
|
|
|
|
|
FAILED=0
|
|
|
|
UNCOMPILABLE=0
|
|
|
|
TESTED=0
|
|
|
|
|
2022-11-24 15:16:16 +00:00
|
|
|
function test_ast_import_export_equivalence
|
|
|
|
{
|
|
|
|
local sol_file="$1"
|
|
|
|
local input_files=( "${@:2}" )
|
2019-09-11 19:16:35 +00:00
|
|
|
|
2022-11-24 15:16:16 +00:00
|
|
|
local export_command=("$SOLC" --combined-json ast --pretty-json --json-indent 4 "${input_files[@]}")
|
|
|
|
local import_command=("$SOLC" --import-ast --combined-json ast --pretty-json --json-indent 4 expected.json)
|
2023-04-28 16:29:59 +00:00
|
|
|
local import_via_standard_json_command=("$SOLC" --combined-json ast --pretty-json --json-indent 4 --standard-json standard_json_input.json)
|
2020-12-11 20:34:55 +00:00
|
|
|
|
2022-11-24 15:16:16 +00:00
|
|
|
# export ast - save ast json as expected result (silently)
|
|
|
|
if ! "${export_command[@]}" > expected.json 2> stderr_export.txt
|
2019-09-11 19:16:35 +00:00
|
|
|
then
|
2022-11-24 15:16:16 +00:00
|
|
|
print_stderr_stdout "ERROR: AST reimport failed (export) for input file ${sol_file}." ./stderr_export.txt ./expected.json
|
|
|
|
print_used_commands "$(pwd)" "${export_command[*]} > expected.json" "${import_command[*]}"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# (re)import ast - and export it again as obtained result (silently)
|
|
|
|
if ! "${import_command[@]}" > obtained.json 2> stderr_import.txt
|
|
|
|
then
|
|
|
|
print_stderr_stdout "ERROR: AST reimport failed (import) for input file ${sol_file}." ./stderr_import.txt ./obtained.json
|
|
|
|
print_used_commands "$(pwd)" "${export_command[*]} > expected.json" "${import_command[*]}"
|
|
|
|
return 1
|
|
|
|
fi
|
2021-09-22 09:19:15 +00:00
|
|
|
|
2023-04-28 16:29:59 +00:00
|
|
|
echo ". += {\"sources\":" > _ast_json.json
|
|
|
|
jq .sources expected.json >> _ast_json.json
|
|
|
|
echo "}" >> _ast_json.json
|
|
|
|
echo "{\"language\": \"SolidityAST\", \"settings\": {\"outputSelection\": {\"*\": {\"\": [\"ast\"]}}}}" > standard_json.json
|
|
|
|
jq --from-file _ast_json.json standard_json.json > standard_json_input.json
|
|
|
|
|
|
|
|
# (re)import ast via standard json - and export it again as obtained result (silently)
|
|
|
|
if ! "${import_via_standard_json_command[@]}" > obtained_standard_json.json 2> stderr_import.txt
|
|
|
|
then
|
|
|
|
print_stderr_stdout "ERROR: AST reimport failed (import) for input file ${sol_file}." ./stderr_import.txt ./obtained_standard_json.json
|
|
|
|
print_used_commands "$(pwd)" "${export_command[*]} > expected.json" "${import_command[*]}"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
jq .sources expected.json > expected_standard_json.json
|
|
|
|
jq .sources obtained_standard_json.json > obtained_standard_json_.json
|
|
|
|
jq 'walk(if type == "object" and has("ast") then .AST = .ast | del(.ast) else . end)' < obtained_standard_json_.json > obtained_standard_json.json
|
|
|
|
jq --sort-keys . < obtained_standard_json.json > obtained_standard_json_.json
|
|
|
|
mv obtained_standard_json_.json obtained_standard_json.json
|
|
|
|
|
2022-11-24 15:16:16 +00:00
|
|
|
# compare expected and obtained ASTs
|
2023-04-28 16:29:59 +00:00
|
|
|
if ! diff_files expected.json obtained.json || ! diff_files expected_standard_json.json obtained_standard_json.json
|
2022-11-24 15:16:16 +00:00
|
|
|
then
|
|
|
|
printError "ERROR: AST reimport failed for ${sol_file}"
|
|
|
|
if (( EXIT_ON_ERROR == 1 ))
|
2019-09-11 19:16:35 +00:00
|
|
|
then
|
2022-11-24 15:16:16 +00:00
|
|
|
print_used_commands "$(pwd)" "${export_command[*]}" "${import_command[*]}"
|
2020-04-17 12:32:38 +00:00
|
|
|
return 1
|
2019-09-11 19:16:35 +00:00
|
|
|
fi
|
2022-11-24 15:16:16 +00:00
|
|
|
FAILED=$((FAILED + 1))
|
|
|
|
fi
|
|
|
|
TESTED=$((TESTED + 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
# function tests whether exporting and importing again is equivalent.
|
|
|
|
# Results are recorded by incrementing the FAILED or UNCOMPILABLE global variable.
|
|
|
|
# Also, in case of a mismatch a diff is 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 test_import_export_equivalence {
|
|
|
|
local sol_file="$1"
|
|
|
|
local input_files=( "${@:2}" )
|
|
|
|
local output
|
|
|
|
local solc_return_code
|
|
|
|
local compile_test
|
|
|
|
|
|
|
|
case "$IMPORT_TEST_TYPE" in
|
|
|
|
ast) compile_test="--ast-compact-json" ;;
|
|
|
|
*) assertFail "Unknown import test type '${IMPORT_TEST_TYPE}'. Aborting." ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
set +e
|
|
|
|
output=$("$SOLC" "${compile_test}" "${input_files[@]}" 2>&1)
|
|
|
|
solc_return_code=$?
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# if input files where compilable with success
|
|
|
|
if (( solc_return_code == 0 ))
|
|
|
|
then
|
|
|
|
case "$IMPORT_TEST_TYPE" in
|
|
|
|
ast) test_ast_import_export_equivalence "${sol_file}" "${input_files[@]}" ;;
|
|
|
|
*) assertFail "Unknown import test type '${IMPORT_TEST_TYPE}'. Aborting." ;;
|
|
|
|
esac
|
2019-09-11 19:16:35 +00:00
|
|
|
else
|
|
|
|
UNCOMPILABLE=$((UNCOMPILABLE + 1))
|
2022-11-24 15:16:16 +00:00
|
|
|
|
|
|
|
# solc will return exit code 2, if it was terminated by an uncaught exception.
|
|
|
|
# This should normally not happen, so we terminate the test execution here
|
|
|
|
# and print some details about the corresponding solc invocation.
|
|
|
|
if (( solc_return_code == 2 ))
|
|
|
|
then
|
|
|
|
fail "\n\nERROR: Uncaught Exception while executing '$SOLC ${compile_test} ${input_files[*]}':\n${output}\n"
|
|
|
|
fi
|
2019-09-11 19:16:35 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
WORKINGDIR=$PWD
|
|
|
|
|
2022-11-24 15:16:16 +00:00
|
|
|
command_available "$SOLC" --version
|
|
|
|
command_available jq --version
|
|
|
|
|
|
|
|
case "$IMPORT_TEST_TYPE" in
|
|
|
|
ast) TEST_DIRS=("${SYNTAXTESTS_DIR}" "${ASTJSONTESTS_DIR}") ;;
|
|
|
|
*) assertFail "Import test type not defined. $(print_usage)" ;;
|
|
|
|
esac
|
|
|
|
|
2021-02-03 14:54:45 +00:00
|
|
|
# boost_filesystem_bug specifically tests a local fix for a boost::filesystem
|
|
|
|
# bug. Since the test involves a malformed path, there is no point in running
|
2022-11-24 15:16:16 +00:00
|
|
|
# tests on it. See https://github.com/boostorg/filesystem/issues/176
|
2023-08-29 12:35:58 +00:00
|
|
|
# In addition, exclude all experimental Solidity tests (new type inference system)
|
|
|
|
EXCLUDE_FILES=(
|
|
|
|
boost_filesystem_bug.sol
|
|
|
|
pragma_experimental_solidity.sol
|
|
|
|
)
|
|
|
|
EXCLUDE_FILES_JOINED=$(printf "! -name %s " "${EXCLUDE_FILES[@]}")
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
IMPORT_TEST_FILES=$(find "${TEST_DIRS[@]}" -name "*.sol" -and $EXCLUDE_FILES_JOINED -not -path "*/experimental/*")
|
2022-11-24 15:16:16 +00:00
|
|
|
|
|
|
|
NSOURCES="$(echo "${IMPORT_TEST_FILES}" | wc -l)"
|
|
|
|
echo "Looking at ${NSOURCES} .sol files..."
|
|
|
|
|
|
|
|
for solfile in $IMPORT_TEST_FILES
|
2019-09-11 19:16:35 +00:00
|
|
|
do
|
2022-11-24 15:16:16 +00:00
|
|
|
echo -n "·"
|
2019-09-11 19:16:35 +00:00
|
|
|
# create a temporary sub-directory
|
2020-02-14 11:28:55 +00:00
|
|
|
FILETMP=$(mktemp -d)
|
2020-12-11 17:19:53 +00:00
|
|
|
cd "$FILETMP"
|
2019-09-11 19:16:35 +00:00
|
|
|
|
2020-09-01 00:04:38 +00:00
|
|
|
set +e
|
2020-12-11 17:19:53 +00:00
|
|
|
OUTPUT=$("$SPLITSOURCES" "$solfile")
|
2020-09-01 00:04:38 +00:00
|
|
|
SPLITSOURCES_RC=$?
|
|
|
|
set -e
|
2022-11-24 15:16:16 +00:00
|
|
|
|
|
|
|
if (( SPLITSOURCES_RC == 0 ))
|
2019-09-11 19:16:35 +00:00
|
|
|
then
|
2022-11-24 15:16:16 +00:00
|
|
|
IFS=' ' read -ra OUTPUT_ARRAY <<< "$OUTPUT"
|
|
|
|
test_import_export_equivalence "$solfile" "${OUTPUT_ARRAY[@]}"
|
|
|
|
elif (( SPLITSOURCES_RC == 1 ))
|
2020-09-01 00:04:38 +00:00
|
|
|
then
|
2022-11-24 15:16:16 +00:00
|
|
|
test_import_export_equivalence "$solfile" "$solfile"
|
|
|
|
elif (( SPLITSOURCES_RC == 2 ))
|
2020-09-01 00:04:38 +00:00
|
|
|
then
|
|
|
|
# The script will exit with return code 2, if an UnicodeDecodeError occurred.
|
|
|
|
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
|
|
|
|
# these errors, but print the actual output of the script.
|
2022-11-24 15:16:16 +00:00
|
|
|
printError "\n\n${OUTPUT}\n\n"
|
|
|
|
test_import_export_equivalence "$solfile" "$solfile"
|
2020-09-01 00:04:38 +00:00
|
|
|
else
|
|
|
|
# All other return codes will be treated as critical errors. The script will exit.
|
2022-11-24 15:16:16 +00:00
|
|
|
printError "\n\nGot unexpected return code ${SPLITSOURCES_RC} from '${SPLITSOURCES} ${solfile}'. Aborting."
|
|
|
|
printError "\n\n${OUTPUT}\n\n"
|
2020-09-01 00:04:38 +00:00
|
|
|
|
2020-12-11 17:19:53 +00:00
|
|
|
cd "$WORKINGDIR"
|
2020-09-01 00:04:38 +00:00
|
|
|
# Delete temporary files
|
2020-12-11 17:19:53 +00:00
|
|
|
rm -rf "$FILETMP"
|
2020-09-01 00:04:38 +00:00
|
|
|
|
|
|
|
exit 1
|
2019-09-11 19:16:35 +00:00
|
|
|
fi
|
|
|
|
|
2020-12-11 17:19:53 +00:00
|
|
|
cd "$WORKINGDIR"
|
2019-09-11 19:16:35 +00:00
|
|
|
# Delete temporary files
|
2020-12-11 17:19:53 +00:00
|
|
|
rm -rf "$FILETMP"
|
2019-09-11 19:16:35 +00:00
|
|
|
done
|
|
|
|
|
2022-11-24 15:16:16 +00:00
|
|
|
echo
|
2019-09-11 19:16:35 +00:00
|
|
|
|
2022-11-24 15:16:16 +00:00
|
|
|
if (( FAILED == 0 ))
|
2019-09-11 19:16:35 +00:00
|
|
|
then
|
2022-11-24 15:16:16 +00:00
|
|
|
echo "SUCCESS: ${TESTED} tests passed, ${FAILED} failed, ${UNCOMPILABLE} could not be compiled (${NSOURCES} sources total)."
|
2019-09-11 19:16:35 +00:00
|
|
|
else
|
2022-11-24 15:16:16 +00:00
|
|
|
fail "FAILURE: Out of ${NSOURCES} sources, ${FAILED} failed, (${UNCOMPILABLE} could not be compiled)."
|
2019-09-11 19:16:35 +00:00
|
|
|
fi
|