2 # Copyright 2014 BitPay Inc.
3 # Copyright 2016-2017 The Bitcoin Core developers
4 # Distributed under the MIT software license, see the accompanying
5 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 """Test framework for bitcoin utils.
8 Runs automatically during `make check`.
10 Can also be run manually."""
24 config
= configparser
.ConfigParser()
25 config
.read_file(open(os
.path
.dirname(__file__
) + "/../config.ini"))
27 parser
= argparse
.ArgumentParser(description
=__doc__
)
28 parser
.add_argument('-v', '--verbose', action
='store_true')
29 args
= parser
.parse_args()
30 verbose
= args
.verbose
36 formatter
= '%(asctime)s - %(levelname)s - %(message)s'
37 # Add the format/level to the logger
38 logging
.basicConfig(format
=formatter
, level
=level
)
40 bctester(config
["environment"]["SRCDIR"] + "/test/util/data", "bitcoin-util-test.json", config
["environment"])
42 def bctester(testDir
, input_basename
, buildenv
):
43 """ Loads and parses the input file, runs all tests and reports results"""
44 input_filename
= testDir
+ "/" + input_basename
45 raw_data
= open(input_filename
).read()
46 input_data
= json
.loads(raw_data
)
50 for testObj
in input_data
:
52 bctest(testDir
, testObj
, buildenv
)
53 logging
.info("PASSED: " + testObj
["description"])
55 logging
.info("FAILED: " + testObj
["description"])
56 failed_testcases
.append(testObj
["description"])
59 error_message
= "FAILED_TESTCASES:\n"
60 error_message
+= pprint
.pformat(failed_testcases
, width
=400)
61 logging
.error(error_message
)
66 def bctest(testDir
, testObj
, buildenv
):
67 """Runs a single test, comparing output and RC to expected output and RC.
69 Raises an error if input can't be read, executable fails, or output/RC
70 are not as expected. Error is caught by bctester() and reported.
72 # Get the exec names and arguments
73 execprog
= buildenv
["BUILDDIR"] + "/src/" + testObj
['exec'] + buildenv
["EXEEXT"]
74 execargs
= testObj
['args']
75 execrun
= [execprog
] + execargs
77 # Read the input data (if there is any)
80 if "input" in testObj
:
81 filename
= testDir
+ "/" + testObj
['input']
82 inputData
= open(filename
).read()
83 stdinCfg
= subprocess
.PIPE
85 # Read the expected output data (if there is any)
88 if "output_cmp" in testObj
:
89 outputFn
= testObj
['output_cmp']
90 outputType
= os
.path
.splitext(outputFn
)[1][1:] # output type from file extension (determines how to compare)
92 outputData
= open(testDir
+ "/" + outputFn
).read()
94 logging
.error("Output file " + outputFn
+ " can not be opened")
97 logging
.error("Output data missing for " + outputFn
)
101 proc
= subprocess
.Popen(execrun
, stdin
=stdinCfg
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
, universal_newlines
=True)
103 outs
= proc
.communicate(input=inputData
)
105 logging
.error("OSError, Failed to execute " + execprog
)
109 data_mismatch
, formatting_mismatch
= False, False
110 # Parse command output and expected output
112 a_parsed
= parse_output(outs
[0], outputType
)
113 except Exception as e
:
114 logging
.error('Error parsing command output as %s: %s' % (outputType
, e
))
117 b_parsed
= parse_output(outputData
, outputType
)
118 except Exception as e
:
119 logging
.error('Error parsing expected output %s as %s: %s' % (outputFn
, outputType
, e
))
122 if a_parsed
!= b_parsed
:
123 logging
.error("Output data mismatch for " + outputFn
+ " (format " + outputType
+ ")")
126 if outs
[0] != outputData
:
127 error_message
= "Output formatting mismatch for " + outputFn
+ ":\n"
128 error_message
+= "".join(difflib
.context_diff(outputData
.splitlines(True),
129 outs
[0].splitlines(True),
132 logging
.error(error_message
)
133 formatting_mismatch
= True
135 assert not data_mismatch
and not formatting_mismatch
137 # Compare the return code to the expected return code
139 if "return_code" in testObj
:
140 wantRC
= testObj
['return_code']
141 if proc
.returncode
!= wantRC
:
142 logging
.error("Return code mismatch for " + outputFn
)
145 if "error_txt" in testObj
:
146 want_error
= testObj
["error_txt"]
148 # TODO: ideally, we'd compare the strings exactly and also assert
149 # That stderr is empty if no errors are expected. However, bitcoin-tx
150 # emits DISPLAY errors when running as a windows application on
151 # linux through wine. Just assert that the expected error text appears
152 # somewhere in stderr.
153 if want_error
not in outs
[1]:
154 logging
.error("Error mismatch:\n" + "Expected: " + want_error
+ "\nReceived: " + outs
[1].rstrip())
157 def parse_output(a
, fmt
):
158 """Parse the output according to specified format.
160 Raise an error if the output can't be parsed."""
161 if fmt
== 'json': # json: compare parsed data
163 elif fmt
== 'hex': # hex: parse and compare binary data
164 return binascii
.a2b_hex(a
.strip())
166 raise NotImplementedError("Don't know how to compare %s" % fmt
)
168 if __name__
== '__main__':