Skip to content

Commit fb11427

Browse files
author
Jeff Garzik
committed
Merge pull request #4624
2 parents dd28197 + fb14452 commit fb11427

File tree

7 files changed

+100
-3
lines changed

7 files changed

+100
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ src/qt/test/moc*.cpp
4343
*.bak
4444
*.rej
4545
*.orig
46+
*.pyc
4647
*.o
4748
*.o-*
4849
*.patch

src/Makefile.test.include

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
TESTS += test/test_bitcoin
1+
TESTS += test/test_bitcoin test/bitcoin-util-test.py
22
bin_PROGRAMS += test/test_bitcoin
33
TEST_SRCDIR = test
44
TEST_BINARY=test/test_bitcoin$(EXEEXT)
55

6+
7+
EXTRA_DIST += \
8+
test/bctest.py \
9+
test/bitcoin-util-test.py \
10+
test/data/bitcoin-util-test.json \
11+
test/data/blanktx.hex
12+
613
JSON_TEST_FILES = \
714
test/data/script_valid.json \
815
test/data/base58_keys_valid.json \

src/bitcoin-tx.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <stdio.h>
1515
#include <boost/assign/list_of.hpp>
16+
#include <boost/algorithm/string.hpp>
1617

1718
using namespace std;
1819
using namespace boost::assign;
@@ -501,13 +502,34 @@ static void OutputTx(const CTransaction& tx)
501502
OutputTxHex(tx);
502503
}
503504

505+
static string readStdin()
506+
{
507+
char buf[4096];
508+
string ret;
509+
510+
while (!feof(stdin)) {
511+
size_t bread = fread(buf, 1, sizeof(buf), stdin);
512+
ret.append(buf, bread);
513+
if (bread < sizeof(buf))
514+
break;
515+
}
516+
517+
if (ferror(stdin))
518+
throw runtime_error("error reading stdin");
519+
520+
boost::algorithm::trim_right(ret);
521+
522+
return ret;
523+
}
524+
504525
static int CommandLineRawTx(int argc, char* argv[])
505526
{
506527
string strPrint;
507528
int nRet = 0;
508529
try {
509-
// Skip switches
510-
while (argc > 1 && IsSwitchChar(argv[1][0])) {
530+
// Skip switches; Permit common stdin convention "-"
531+
while (argc > 1 && IsSwitchChar(argv[1][0]) &&
532+
(argv[1][1] != 0)) {
511533
argc--;
512534
argv++;
513535
}
@@ -522,6 +544,8 @@ static int CommandLineRawTx(int argc, char* argv[])
522544

523545
// param: hex-encoded bitcoin transaction
524546
string strHexTx(argv[1]);
547+
if (strHexTx == "-") // "-" implies standard input
548+
strHexTx = readStdin();
525549

526550
if (!DecodeHexTx(txDecodeTmp, strHexTx))
527551
throw runtime_error("invalid transaction encoding");

src/test/bctest.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2014 BitPay, Inc.
2+
# Distributed under the MIT/X11 software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
import subprocess
6+
import os
7+
import json
8+
import sys
9+
10+
def bctest(testDir, testObj):
11+
execargs = testObj['exec']
12+
13+
stdinCfg = None
14+
inputData = None
15+
if "input" in testObj:
16+
filename = testDir + "/" + testObj['input']
17+
inputData = open(filename).read()
18+
stdinCfg = subprocess.PIPE
19+
20+
outputFn = testObj['output_cmp']
21+
outputData = open(testDir + "/" + outputFn).read()
22+
23+
proc = subprocess.Popen(execargs, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
24+
try:
25+
outs = proc.communicate(input=inputData)
26+
except OSError:
27+
print("OSError, Failed to execute " + execargs[0])
28+
sys.exit(1)
29+
30+
if outs[0] != outputData:
31+
print("Output data mismatch for " + outputFn)
32+
sys.exit(1)
33+
34+
def bctester(testDir, input_basename):
35+
input_filename = testDir + "/" + input_basename
36+
raw_data = open(input_filename).read()
37+
input_data = json.loads(raw_data)
38+
39+
for testObj in input_data:
40+
bctest(testDir, testObj)
41+
42+
sys.exit(0)
43+

src/test/bitcoin-util-test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python
2+
# Copyright 2014 BitPay, Inc.
3+
# Distributed under the MIT/X11 software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
import os
7+
import bctest
8+
9+
if __name__ == '__main__':
10+
bctest.bctester(os.environ["srcdir"] + "/test/data",
11+
"bitcoin-util-test.json")
12+

src/test/data/bitcoin-util-test.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{ "exec": ["./bitcoin-tx", "-create"],
3+
"output_cmp": "blanktx.hex"
4+
},
5+
{ "exec": ["./bitcoin-tx", "-"],
6+
"input": "blanktx.hex",
7+
"output_cmp": "blanktx.hex"
8+
}
9+
]

src/test/data/blanktx.hex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
01000000000000000000

0 commit comments

Comments
 (0)