aboutsummaryrefslogtreecommitdiff
path: root/src/test/sighash_tests.cpp
diff options
context:
space:
mode:
authorManuel Araoz <manuelaraoz@gmail.com>2014-03-28 16:28:13 -0300
committerManuel Araoz <manuelaraoz@gmail.com>2014-03-31 11:19:19 -0300
commit43cb41859e910797510ef1117644fa2cd3c96fc9 (patch)
treec0651b1f0ad3c46431d0d2a633d8a55cfabd9ddb /src/test/sighash_tests.cpp
parentdb3a5e4151a499d94f1738934d280a4163295ff7 (diff)
Add sighash tests from data file
Diffstat (limited to 'src/test/sighash_tests.cpp')
-rw-r--r--src/test/sighash_tests.cpp60
1 files changed, 57 insertions, 3 deletions
diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp
index 04b93fb105..0cc9bc20b9 100644
--- a/src/test/sighash_tests.cpp
+++ b/src/test/sighash_tests.cpp
@@ -3,9 +3,20 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <boost/test/unit_test.hpp>
+#include <iostream>
#include "main.h"
#include "util.h"
+#include "serialize.h"
+#include "version.h"
+#include "data/sighash.json.h"
+
+#include "json/json_spirit_reader_template.h"
+#include "json/json_spirit_utils.h"
+#include "json/json_spirit_writer_template.h"
+
+using namespace json_spirit;
+extern Array read_json(const std::string& jsondata);
extern uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
@@ -103,7 +114,6 @@ void static RandomTransaction(CTransaction &tx, bool fSingle) {
}
BOOST_AUTO_TEST_SUITE(sighash_tests)
-
BOOST_AUTO_TEST_CASE(sighash_test)
{
seed_insecure_rand(false);
@@ -115,10 +125,54 @@ BOOST_AUTO_TEST_CASE(sighash_test)
CScript scriptCode;
RandomScript(scriptCode);
int nIn = insecure_rand() % txTo.vin.size();
- BOOST_CHECK(SignatureHash(scriptCode, txTo, nIn, nHashType) ==
- SignatureHashOld(scriptCode, txTo, nIn, nHashType));
+
+ uint256 sh, sho;
+ sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType);
+ sh = SignatureHash(scriptCode, txTo, nIn, nHashType);
+ BOOST_CHECK(sh == sho);
}
}
+// Goal: check that SignatureHash generates correct hash
+
+BOOST_AUTO_TEST_CASE(sighash_from_data)
+{
+ Array tests = read_json(std::string(json_tests::sighash, json_tests::sighash + sizeof(json_tests::sighash)));
+
+ BOOST_FOREACH(Value& tv, tests)
+ {
+ Array test = tv.get_array();
+ std::string strTest = write_string(tv, false);
+ if (test.size() < 1) // Allow for extra stuff (useful for comments)
+ {
+ BOOST_ERROR("Bad test: " << strTest);
+ continue;
+ }
+ if (test.size() == 1) continue; // comment
+
+ std::string raw_tx = test[0].get_str();
+ std::string raw_script = test[1].get_str();
+ int nIn = test[2].get_int();
+ int nHashType = test[3].get_int();
+ std::string sigHashHex = test[4].get_str();
+
+ uint256 sh;
+ CDataStream stream(ParseHex(raw_tx), SER_NETWORK, PROTOCOL_VERSION);
+ CTransaction tx;
+ stream >> tx;
+
+ CValidationState state;
+ BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest);
+ BOOST_CHECK(state.IsValid());
+
+ CScript scriptCode = CScript();
+ std::vector<unsigned char> raw = ParseHex(raw_script);
+ scriptCode.insert(scriptCode.end(), raw.begin(), raw.end());
+
+ sh = SignatureHash(scriptCode, tx, nIn, nHashType);
+
+ BOOST_CHECK_MESSAGE(sh.GetHex() == sigHashHex, strTest);
+ }
+}
BOOST_AUTO_TEST_SUITE_END()