Browse Source

Add code generating data/sighash.json test data

0.10
Manuel Araoz 11 years ago
parent
commit
232aa9e034
  1. 31
      src/test/sighash_tests.cpp

31
src/test/sighash_tests.cpp

@ -114,11 +114,21 @@ void static RandomTransaction(CTransaction &tx, bool fSingle) {
} }
BOOST_AUTO_TEST_SUITE(sighash_tests) BOOST_AUTO_TEST_SUITE(sighash_tests)
BOOST_AUTO_TEST_CASE(sighash_test) BOOST_AUTO_TEST_CASE(sighash_test)
{ {
seed_insecure_rand(false); seed_insecure_rand(false);
for (int i=0; i<50000; i++) { #if defined(PRINT_SIGHASH_JSON)
std::cout << "[\n";
std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
#endif
int nRandomTests = 50000;
#if defined(PRINT_SIGHASH_JSON)
nRandomTests = 500;
#endif
for (int i=0; i<nRandomTests; i++) {
int nHashType = insecure_rand(); int nHashType = insecure_rand();
CTransaction txTo; CTransaction txTo;
RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE); RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE);
@ -129,12 +139,29 @@ BOOST_AUTO_TEST_CASE(sighash_test)
uint256 sh, sho; uint256 sh, sho;
sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType); sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType);
sh = SignatureHash(scriptCode, txTo, nIn, nHashType); sh = SignatureHash(scriptCode, txTo, nIn, nHashType);
#if defined(PRINT_SIGHASH_JSON)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << txTo;
std::cout << "\t[\"" ;
std::cout << HexStr(ss.begin(), ss.end()) << "\", \"";
std::cout << HexStr(scriptCode) << "\", ";
std::cout << nIn << ", ";
std::cout << nHashType << ", \"";
std::cout << sho.GetHex() << "\"]";
if (i+1 != nRandomTests) {
std::cout << ",";
}
std::cout << "\n";
#endif
BOOST_CHECK(sh == sho); BOOST_CHECK(sh == sho);
} }
#if defined(PRINT_SIGHASH_JSON)
std::cout << "]\n";
#endif
} }
// Goal: check that SignatureHash generates correct hash // Goal: check that SignatureHash generates correct hash
BOOST_AUTO_TEST_CASE(sighash_from_data) BOOST_AUTO_TEST_CASE(sighash_from_data)
{ {
Array tests = read_json(std::string(json_tests::sighash, json_tests::sighash + sizeof(json_tests::sighash))); Array tests = read_json(std::string(json_tests::sighash, json_tests::sighash + sizeof(json_tests::sighash)));

Loading…
Cancel
Save