2017-06-06 23:54:23 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-05-19 22:20:34 +02:00
|
|
|
# Copyright 2014 BitPay Inc.
|
2017-06-06 23:54:23 +02:00
|
|
|
# Copyright 2016-2017 The Bitcoin Core developers
|
2019-05-19 22:20:34 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-06-06 23:54:23 +02:00
|
|
|
"""Test framework for bitcoin utils.
|
2019-05-19 22:20:34 +02:00
|
|
|
|
2017-06-06 23:54:23 +02:00
|
|
|
Runs automatically during `make check`.
|
2019-05-19 22:20:34 +02:00
|
|
|
|
|
|
|
Can also be run manually."""
|
|
|
|
|
2017-10-03 21:24:53 +02:00
|
|
|
from __future__ import division,print_function,unicode_literals
|
|
|
|
|
2017-06-06 23:54:23 +02:00
|
|
|
import argparse
|
|
|
|
import binascii
|
2017-10-03 21:24:53 +02:00
|
|
|
try:
|
|
|
|
import configparser
|
|
|
|
except ImportError:
|
|
|
|
import ConfigParser as configparser
|
2017-06-06 23:54:23 +02:00
|
|
|
import difflib
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import pprint
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2019-05-19 22:20:34 +02:00
|
|
|
|
2017-06-06 23:54:23 +02:00
|
|
|
def main():
|
|
|
|
config = configparser.ConfigParser()
|
2017-10-03 21:24:53 +02:00
|
|
|
config.optionxform = str
|
2018-06-16 15:21:01 +02:00
|
|
|
config.readfp(open(os.path.join(os.path.dirname(__file__), "../config.ini"), encoding="utf8"))
|
2017-10-03 21:24:53 +02:00
|
|
|
env_conf = dict(config.items('environment'))
|
2017-06-06 23:54:23 +02:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
2019-05-19 22:20:34 +02:00
|
|
|
parser.add_argument('-v', '--verbose', action='store_true')
|
|
|
|
args = parser.parse_args()
|
|
|
|
verbose = args.verbose
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
level = logging.DEBUG
|
|
|
|
else:
|
|
|
|
level = logging.ERROR
|
|
|
|
formatter = '%(asctime)s - %(levelname)s - %(message)s'
|
|
|
|
# Add the format/level to the logger
|
2017-06-06 23:54:23 +02:00
|
|
|
logging.basicConfig(format=formatter, level=level)
|
|
|
|
|
2018-02-12 11:30:17 +01:00
|
|
|
bctester(os.path.join(env_conf["SRCDIR"], "test", "util", "data"), "bitcoin-util-test.json", env_conf)
|
2017-06-06 23:54:23 +02:00
|
|
|
|
|
|
|
def bctester(testDir, input_basename, buildenv):
|
|
|
|
""" Loads and parses the input file, runs all tests and reports results"""
|
2017-12-30 06:19:28 +01:00
|
|
|
input_filename = os.path.join(testDir, input_basename)
|
2018-06-16 15:21:01 +02:00
|
|
|
raw_data = open(input_filename, encoding="utf8").read()
|
2017-06-06 23:54:23 +02:00
|
|
|
input_data = json.loads(raw_data)
|
|
|
|
|
|
|
|
failed_testcases = []
|
|
|
|
|
|
|
|
for testObj in input_data:
|
|
|
|
try:
|
|
|
|
bctest(testDir, testObj, buildenv)
|
|
|
|
logging.info("PASSED: " + testObj["description"])
|
|
|
|
except:
|
|
|
|
logging.info("FAILED: " + testObj["description"])
|
|
|
|
failed_testcases.append(testObj["description"])
|
2019-05-19 22:20:34 +02:00
|
|
|
|
2017-06-06 23:54:23 +02:00
|
|
|
if failed_testcases:
|
|
|
|
error_message = "FAILED_TESTCASES:\n"
|
|
|
|
error_message += pprint.pformat(failed_testcases, width=400)
|
|
|
|
logging.error(error_message)
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
def bctest(testDir, testObj, buildenv):
|
|
|
|
"""Runs a single test, comparing output and RC to expected output and RC.
|
|
|
|
|
|
|
|
Raises an error if input can't be read, executable fails, or output/RC
|
|
|
|
are not as expected. Error is caught by bctester() and reported.
|
|
|
|
"""
|
|
|
|
# Get the exec names and arguments
|
2017-12-30 06:19:28 +01:00
|
|
|
execprog = os.path.join(buildenv["BUILDDIR"], "src", testObj["exec"] + buildenv["EXEEXT"])
|
2017-06-06 23:54:23 +02:00
|
|
|
execargs = testObj['args']
|
|
|
|
execrun = [execprog] + execargs
|
|
|
|
|
|
|
|
# Read the input data (if there is any)
|
|
|
|
stdinCfg = None
|
|
|
|
inputData = None
|
|
|
|
if "input" in testObj:
|
2017-12-30 06:19:28 +01:00
|
|
|
filename = os.path.join(testDir, testObj["input"])
|
2018-06-16 15:21:01 +02:00
|
|
|
inputData = open(filename, encoding="utf8").read()
|
2017-06-06 23:54:23 +02:00
|
|
|
stdinCfg = subprocess.PIPE
|
|
|
|
|
|
|
|
# Read the expected output data (if there is any)
|
|
|
|
outputFn = None
|
|
|
|
outputData = None
|
2017-12-30 06:19:28 +01:00
|
|
|
outputType = None
|
2017-06-06 23:54:23 +02:00
|
|
|
if "output_cmp" in testObj:
|
|
|
|
outputFn = testObj['output_cmp']
|
|
|
|
outputType = os.path.splitext(outputFn)[1][1:] # output type from file extension (determines how to compare)
|
|
|
|
try:
|
2018-06-16 15:21:01 +02:00
|
|
|
outputData = open(os.path.join(testDir, outputFn), encoding="utf8").read()
|
2017-06-06 23:54:23 +02:00
|
|
|
except:
|
|
|
|
logging.error("Output file " + outputFn + " can not be opened")
|
|
|
|
raise
|
|
|
|
if not outputData:
|
|
|
|
logging.error("Output data missing for " + outputFn)
|
|
|
|
raise Exception
|
2017-12-30 06:19:28 +01:00
|
|
|
if not outputType:
|
|
|
|
logging.error("Output file %s does not have a file extension" % outputFn)
|
|
|
|
raise Exception
|
2017-06-06 23:54:23 +02:00
|
|
|
|
|
|
|
# Run the test
|
|
|
|
proc = subprocess.Popen(execrun, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
|
|
|
try:
|
|
|
|
outs = proc.communicate(input=inputData)
|
|
|
|
except OSError:
|
|
|
|
logging.error("OSError, Failed to execute " + execprog)
|
|
|
|
raise
|
|
|
|
|
|
|
|
if outputData:
|
|
|
|
data_mismatch, formatting_mismatch = False, False
|
|
|
|
# Parse command output and expected output
|
|
|
|
try:
|
|
|
|
a_parsed = parse_output(outs[0], outputType)
|
|
|
|
except Exception as e:
|
|
|
|
logging.error('Error parsing command output as %s: %s' % (outputType, e))
|
|
|
|
raise
|
|
|
|
try:
|
|
|
|
b_parsed = parse_output(outputData, outputType)
|
|
|
|
except Exception as e:
|
|
|
|
logging.error('Error parsing expected output %s as %s: %s' % (outputFn, outputType, e))
|
|
|
|
raise
|
|
|
|
# Compare data
|
|
|
|
if a_parsed != b_parsed:
|
|
|
|
logging.error("Output data mismatch for " + outputFn + " (format " + outputType + ")")
|
|
|
|
data_mismatch = True
|
|
|
|
# Compare formatting
|
|
|
|
if outs[0] != outputData:
|
|
|
|
error_message = "Output formatting mismatch for " + outputFn + ":\n"
|
|
|
|
error_message += "".join(difflib.context_diff(outputData.splitlines(True),
|
|
|
|
outs[0].splitlines(True),
|
|
|
|
fromfile=outputFn,
|
|
|
|
tofile="returned"))
|
|
|
|
logging.error(error_message)
|
|
|
|
formatting_mismatch = True
|
|
|
|
|
|
|
|
assert not data_mismatch and not formatting_mismatch
|
|
|
|
|
|
|
|
# Compare the return code to the expected return code
|
|
|
|
wantRC = 0
|
|
|
|
if "return_code" in testObj:
|
|
|
|
wantRC = testObj['return_code']
|
|
|
|
if proc.returncode != wantRC:
|
|
|
|
logging.error("Return code mismatch for " + outputFn)
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
if "error_txt" in testObj:
|
|
|
|
want_error = testObj["error_txt"]
|
|
|
|
# Compare error text
|
|
|
|
# TODO: ideally, we'd compare the strings exactly and also assert
|
2021-04-10 22:36:31 +02:00
|
|
|
# That stderr is empty if no errors are expected. However, dash-tx
|
2017-06-06 23:54:23 +02:00
|
|
|
# emits DISPLAY errors when running as a windows application on
|
|
|
|
# linux through wine. Just assert that the expected error text appears
|
|
|
|
# somewhere in stderr.
|
|
|
|
if want_error not in outs[1]:
|
|
|
|
logging.error("Error mismatch:\n" + "Expected: " + want_error + "\nReceived: " + outs[1].rstrip())
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
def parse_output(a, fmt):
|
|
|
|
"""Parse the output according to specified format.
|
|
|
|
|
|
|
|
Raise an error if the output can't be parsed."""
|
|
|
|
if fmt == 'json': # json: compare parsed data
|
|
|
|
return json.loads(a)
|
|
|
|
elif fmt == 'hex': # hex: parse and compare binary data
|
|
|
|
return binascii.a2b_hex(a.strip())
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("Don't know how to compare %s" % fmt)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|