Changeset View
Changeset View
Standalone View
Standalone View
src/test/sighashtype_tests.cpp
- This file was moved from src/test/script_sighashtype_tests.cpp.
// Copyright (c) 2016 The Bitcoin Core developers | // Copyright (c) 2018 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "script/sighashtype.h" | #include "script/sighashtype.h" | ||||
#include "test/test_bitcoin.h" | #include "test/test_bitcoin.h" | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <set> | #include <set> | ||||
BOOST_FIXTURE_TEST_SUITE(script_sighashtype_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(sighashtype_tests, BasicTestingSetup) | ||||
static void CheckSigHashType(SigHashType t, BaseSigHashType baseType, | static void CheckSigHashType(SigHashType t, BaseSigHashType baseType, | ||||
bool isDefined, uint32_t forkValue, bool hasForkId, | bool isDefined, uint32_t forkValue, bool hasForkId, | ||||
bool hasAnyoneCanPay) { | bool hasAnyoneCanPay) { | ||||
BOOST_CHECK(t.getBaseType() == baseType); | BOOST_CHECK(t.getBaseType() == baseType); | ||||
BOOST_CHECK_EQUAL(t.isDefined(), isDefined); | BOOST_CHECK_EQUAL(t.isDefined(), isDefined); | ||||
BOOST_CHECK_EQUAL(t.getForkValue(), forkValue); | BOOST_CHECK_EQUAL(t.getForkValue(), forkValue); | ||||
BOOST_CHECK_EQUAL(t.hasForkId(), hasForkId); | BOOST_CHECK_EQUAL(t.hasForkId(), hasForkId); | ||||
Show All 12 Lines | BOOST_AUTO_TEST_CASE(sighash_construction_test) { | ||||
std::set<uint32_t> forkValues{0, 1, 0x123456, 0xfedcba, 0xffffff}; | std::set<uint32_t> forkValues{0, 1, 0x123456, 0xfedcba, 0xffffff}; | ||||
std::set<bool> forkIdFlagValues{false, true}; | std::set<bool> forkIdFlagValues{false, true}; | ||||
std::set<bool> anyoneCanPayFlagValues{false, true}; | std::set<bool> anyoneCanPayFlagValues{false, true}; | ||||
for (BaseSigHashType baseType : baseTypes) { | for (BaseSigHashType baseType : baseTypes) { | ||||
for (uint32_t forkValue : forkValues) { | for (uint32_t forkValue : forkValues) { | ||||
for (bool hasForkId : forkIdFlagValues) { | for (bool hasForkId : forkIdFlagValues) { | ||||
for (bool hasAnyoneCanPay : anyoneCanPayFlagValues) { | for (bool hasAnyoneCanPay : anyoneCanPayFlagValues) { | ||||
SigHashType t = SigHashType() | const SigHashType t = | ||||
SigHashType() | |||||
.withBaseType(baseType) | .withBaseType(baseType) | ||||
.withForkValue(forkValue) | .withForkValue(forkValue) | ||||
.withForkId(hasForkId) | .withForkId(hasForkId) | ||||
.withAnyoneCanPay(hasAnyoneCanPay); | .withAnyoneCanPay(hasAnyoneCanPay); | ||||
bool isDefined = baseType != BaseSigHashType::UNSUPPORTED; | bool isDefined = baseType != BaseSigHashType::UNSUPPORTED; | ||||
CheckSigHashType(t, baseType, isDefined, forkValue, | CheckSigHashType(t, baseType, isDefined, forkValue, | ||||
hasForkId, hasAnyoneCanPay); | hasForkId, hasAnyoneCanPay); | ||||
// Also check all possible alterations. | // Also check all possible alterations. | ||||
CheckSigHashType(t.withForkId(hasForkId), baseType, | CheckSigHashType(t.withForkId(hasForkId), baseType, | ||||
isDefined, forkValue, hasForkId, | isDefined, forkValue, hasForkId, | ||||
Show All 38 Lines | for (uint32_t sigHashType = 0x00; sigHashType <= 0xff; sigHashType++) { | ||||
uint32_t baseType = rawType & 0x1f; | uint32_t baseType = rawType & 0x1f; | ||||
bool hasForkId = (rawType & SIGHASH_FORKID) != 0; | bool hasForkId = (rawType & SIGHASH_FORKID) != 0; | ||||
bool hasAnyoneCanPay = (rawType & SIGHASH_ANYONECANPAY) != 0; | bool hasAnyoneCanPay = (rawType & SIGHASH_ANYONECANPAY) != 0; | ||||
uint32_t noflag = | uint32_t noflag = | ||||
sigHashType & ~(SIGHASH_FORKID | SIGHASH_ANYONECANPAY); | sigHashType & ~(SIGHASH_FORKID | SIGHASH_ANYONECANPAY); | ||||
bool isDefined = (noflag != 0) && (noflag <= SIGHASH_SINGLE); | bool isDefined = (noflag != 0) && (noflag <= SIGHASH_SINGLE); | ||||
SigHashType tbase(rawType); | const SigHashType tbase(rawType); | ||||
// Check deserialization. | // Check deserialization. | ||||
CheckSigHashType(tbase, BaseSigHashType(baseType), isDefined, | CheckSigHashType(tbase, BaseSigHashType(baseType), isDefined, | ||||
forkValue, hasForkId, hasAnyoneCanPay); | forkValue, hasForkId, hasAnyoneCanPay); | ||||
// Check raw value. | // Check raw value. | ||||
BOOST_CHECK_EQUAL(tbase.getRawSigHashType(), rawType); | BOOST_CHECK_EQUAL(tbase.getRawSigHashType(), rawType); | ||||
Show All 9 Lines |