From d789386371699fee14bb5e444507a6067293ff67 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Tue, 19 Aug 2014 10:28:58 -0400 Subject: [PATCH 1/2] Add "it works" test for bitcoin-tx --- .gitignore | 1 + src/Makefile.test.include | 9 ++++++- src/test/bctest.py | 35 ++++++++++++++++++++++++++++ src/test/bitcoin-util-test.py | 12 ++++++++++ src/test/data/bitcoin-util-test.json | 5 ++++ src/test/data/blanktx.hex | 1 + 6 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/test/bctest.py create mode 100755 src/test/bitcoin-util-test.py create mode 100644 src/test/data/bitcoin-util-test.json create mode 100644 src/test/data/blanktx.hex diff --git a/.gitignore b/.gitignore index 25c0dff66..24af4cb72 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,7 @@ src/qt/test/moc*.cpp *.bak *.rej *.orig +*.pyc *.o *.o-* *.patch diff --git a/src/Makefile.test.include b/src/Makefile.test.include index b54c9be66..7e25430e3 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -1,8 +1,15 @@ -TESTS += test/test_bitcoin +TESTS += test/test_bitcoin test/bitcoin-util-test.py bin_PROGRAMS += test/test_bitcoin TEST_SRCDIR = test TEST_BINARY=test/test_bitcoin$(EXEEXT) + +EXTRA_DIST += \ + test/bctest.py \ + test/bitcoin-util-test.py \ + test/data/bitcoin-util-test.json \ + test/data/blanktx.hex + JSON_TEST_FILES = \ test/data/script_valid.json \ test/data/base58_keys_valid.json \ diff --git a/src/test/bctest.py b/src/test/bctest.py new file mode 100644 index 000000000..3b17acb75 --- /dev/null +++ b/src/test/bctest.py @@ -0,0 +1,35 @@ +# Copyright 2014 BitPay, Inc. +# Distributed under the MIT/X11 software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +import subprocess +import os +import json +import sys + +def bctest(testDir, testObj): + execargs = testObj['exec'] + outputFn = testObj['output_cmp'] + outputData = open(testDir + "/" + outputFn).read() + + proc = subprocess.Popen(execargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + try: + outs = proc.communicate() + except OSError: + print("OSError, Failed to execute " + execargs[0]) + sys.exit(1) + + if outs[0] != outputData: + print("Output data mismatch for " + outputFn) + sys.exit(1) + +def bctester(testDir, input_basename): + input_filename = testDir + "/" + input_basename + raw_data = open(input_filename).read() + input_data = json.loads(raw_data) + + for testObj in input_data: + bctest(testDir, testObj) + + sys.exit(0) + diff --git a/src/test/bitcoin-util-test.py b/src/test/bitcoin-util-test.py new file mode 100755 index 000000000..40690c2fe --- /dev/null +++ b/src/test/bitcoin-util-test.py @@ -0,0 +1,12 @@ +#!/usr/bin/python +# Copyright 2014 BitPay, Inc. +# Distributed under the MIT/X11 software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +import os +import bctest + +if __name__ == '__main__': + bctest.bctester(os.environ["srcdir"] + "/test/data", + "bitcoin-util-test.json") + diff --git a/src/test/data/bitcoin-util-test.json b/src/test/data/bitcoin-util-test.json new file mode 100644 index 000000000..af29fd75a --- /dev/null +++ b/src/test/data/bitcoin-util-test.json @@ -0,0 +1,5 @@ +[ + { "exec": ["./bitcoin-tx", "-create"], + "output_cmp": "blanktx.hex" + } +] diff --git a/src/test/data/blanktx.hex b/src/test/data/blanktx.hex new file mode 100644 index 000000000..36b6f00fb --- /dev/null +++ b/src/test/data/blanktx.hex @@ -0,0 +1 @@ +01000000000000000000 From fb14452c6cadb8d977c405dddb0a94115250d7c4 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Mon, 18 Aug 2014 23:14:29 -0400 Subject: [PATCH 2/2] bitcoin-tx: Accept input via stdin. Add input handling to tests. --- src/bitcoin-tx.cpp | 28 ++++++++++++++++++++++++++-- src/test/bctest.py | 12 ++++++++++-- src/test/data/bitcoin-util-test.json | 4 ++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index ffe87298f..6cd2768d7 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -13,6 +13,7 @@ #include #include +#include using namespace std; using namespace boost::assign; @@ -501,13 +502,34 @@ static void OutputTx(const CTransaction& tx) OutputTxHex(tx); } +static string readStdin() +{ + char buf[4096]; + string ret; + + while (!feof(stdin)) { + size_t bread = fread(buf, 1, sizeof(buf), stdin); + ret.append(buf, bread); + if (bread < sizeof(buf)) + break; + } + + if (ferror(stdin)) + throw runtime_error("error reading stdin"); + + boost::algorithm::trim_right(ret); + + return ret; +} + static int CommandLineRawTx(int argc, char* argv[]) { string strPrint; int nRet = 0; try { - // Skip switches - while (argc > 1 && IsSwitchChar(argv[1][0])) { + // Skip switches; Permit common stdin convention "-" + while (argc > 1 && IsSwitchChar(argv[1][0]) && + (argv[1][1] != 0)) { argc--; argv++; } @@ -522,6 +544,8 @@ static int CommandLineRawTx(int argc, char* argv[]) // param: hex-encoded bitcoin transaction string strHexTx(argv[1]); + if (strHexTx == "-") // "-" implies standard input + strHexTx = readStdin(); if (!DecodeHexTx(txDecodeTmp, strHexTx)) throw runtime_error("invalid transaction encoding"); diff --git a/src/test/bctest.py b/src/test/bctest.py index 3b17acb75..b12647908 100644 --- a/src/test/bctest.py +++ b/src/test/bctest.py @@ -9,12 +9,20 @@ import sys def bctest(testDir, testObj): execargs = testObj['exec'] + + stdinCfg = None + inputData = None + if "input" in testObj: + filename = testDir + "/" + testObj['input'] + inputData = open(filename).read() + stdinCfg = subprocess.PIPE + outputFn = testObj['output_cmp'] outputData = open(testDir + "/" + outputFn).read() - proc = subprocess.Popen(execargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc = subprocess.Popen(execargs, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: - outs = proc.communicate() + outs = proc.communicate(input=inputData) except OSError: print("OSError, Failed to execute " + execargs[0]) sys.exit(1) diff --git a/src/test/data/bitcoin-util-test.json b/src/test/data/bitcoin-util-test.json index af29fd75a..16bcb4489 100644 --- a/src/test/data/bitcoin-util-test.json +++ b/src/test/data/bitcoin-util-test.json @@ -1,5 +1,9 @@ [ { "exec": ["./bitcoin-tx", "-create"], "output_cmp": "blanktx.hex" + }, + { "exec": ["./bitcoin-tx", "-"], + "input": "blanktx.hex", + "output_cmp": "blanktx.hex" } ]