Changeset View
Changeset View
Standalone View
Standalone View
src/test/op_code.cpp
Show First 20 Lines • Show All 460 Lines • ▼ Show 20 Lines | void test_bin2num(uint32_t flags) { | ||||
test(script,stack_t{mk_bin((int64_t)INT_MIN>>1)},flags,stack_t{CScriptNum(INT_MIN>>1).getvch()}); | test(script,stack_t{mk_bin((int64_t)INT_MIN>>1)},flags,stack_t{CScriptNum(INT_MIN>>1).getvch()}); | ||||
test(script,stack_t{mk_bin((int64_t)(INT_MAX>>1)+1)},flags,SCRIPT_ERR_INVALID_BIN2NUM_OPERATION); | test(script,stack_t{mk_bin((int64_t)(INT_MAX>>1)+1)},flags,SCRIPT_ERR_INVALID_BIN2NUM_OPERATION); | ||||
test(script,stack_t{mk_bin((int64_t)(INT_MIN>>1)-1)},flags,SCRIPT_ERR_INVALID_BIN2NUM_OPERATION); | test(script,stack_t{mk_bin((int64_t)(INT_MIN>>1)-1)},flags,SCRIPT_ERR_INVALID_BIN2NUM_OPERATION); | ||||
test(script,stack_t{mk_bin(106894)},flags,stack_t{CScriptNum(106894).getvch()}); | test(script,stack_t{mk_bin(106894)},flags,stack_t{CScriptNum(106894).getvch()}); | ||||
test(script,stack_t{mk_bin(-106894)},flags,stack_t{CScriptNum(-106894).getvch()}); | test(script,stack_t{mk_bin(-106894)},flags,stack_t{CScriptNum(-106894).getvch()}); | ||||
test(script,stack_t{mk_bin(0)},flags,stack_t{CScriptNum(0).getvch()}); | test(script,stack_t{mk_bin(0)},flags,stack_t{CScriptNum(0).getvch()}); | ||||
} | } | ||||
/// OP_NUM2BIN tests | |||||
/// make expected value - helper function | |||||
/// input: number in LE byte order, desired output byte length | |||||
/// output: BIN representation as expected to be generated by the | |||||
/// interpreter. | |||||
/// removes the sign, constructs a BE array of bytes with the positive | |||||
/// number, | |||||
/// the adds the sign. | |||||
/// | |||||
vector<uint8_t> make_ev(vector<uint8_t> vchInput, size_t size) { | |||||
if (vchInput.empty()) return vector<uint8_t>(size, 0); | |||||
vector<uint8_t> result; | |||||
assert(size >= vchInput.size()); | |||||
result.reserve(size); | |||||
bool neg = *vchInput.rbegin() & 0x80; | |||||
*vchInput.rbegin() &= ~0x80; | |||||
size_t pad = size - vchInput.size(); | |||||
for (uint8_t i = 0; i < pad; ++i) { | |||||
result.push_back(0); | |||||
} | |||||
for (auto i = vchInput.rbegin(); i != vchInput.rend(); ++i) { | |||||
result.push_back(*i); | |||||
} | |||||
if (neg) *result.begin() |= 0x80; | |||||
return result; | |||||
} | |||||
void test_num2bin(const CScript &script, vector<uint8_t> vchInput, uint32_t flags) { | |||||
if (vchInput.empty()) return; | |||||
for (uint8_t i = 0; i < vchInput.size(); ++i) { | |||||
if (i == 0) test(script, stack_t{vchInput, {}}, flags, SCRIPT_ERR_INVALID_NUM2BIN_OPERATION); | |||||
else test(script, stack_t{vchInput, {i}}, flags, SCRIPT_ERR_INVALID_NUM2BIN_OPERATION); | |||||
} | |||||
for (uint8_t i = vchInput.size(); i <= CScriptNum::nDefaultMaxNumSize; ++i) { | |||||
if (i == 0) test(script, stack_t{vchInput, {}}, flags, stack_t{make_ev(vchInput, i)}); | |||||
else test(script, stack_t{vchInput, {i}}, flags, stack_t{make_ev(vchInput, i)}); | |||||
} | |||||
} | |||||
void test_num2bin(uint32_t flags) { | |||||
CScript script; | |||||
script << OP_NUM2BIN; | |||||
test(script, stack_t(), flags, SCRIPT_ERR_INVALID_STACK_OPERATION); | |||||
test(script, stack_t{{4}}, flags, SCRIPT_ERR_INVALID_STACK_OPERATION); | |||||
test(script, stack_t{{0x02}, {CScriptNum::nDefaultMaxNumSize + 1}}, flags, | |||||
SCRIPT_ERR_INVALID_NUM2BIN_OPERATION); | |||||
test(script, stack_t{{0x85}, {CScriptNum::nDefaultMaxNumSize + 1}}, flags, | |||||
SCRIPT_ERR_INVALID_NUM2BIN_OPERATION); | |||||
test(script, stack_t{{0x02}, {}}, flags, | |||||
SCRIPT_ERR_INVALID_NUM2BIN_OPERATION); | |||||
test(script, stack_t{{0x85}, {0x85}}, flags, | |||||
SCRIPT_ERR_INVALID_NUM2BIN_OPERATION); | |||||
test(script, stack_t{{0x85}, {}}, flags, | |||||
SCRIPT_ERR_INVALID_NUM2BIN_OPERATION); | |||||
test_num2bin(script, {}, flags); | |||||
test_num2bin(script, {0x7f}, flags); | |||||
test_num2bin(script, {0xff, 0x7f}, flags); // LE for 0x7FFF | |||||
test_num2bin(script, {0x02, 0x71}, flags); | |||||
test_num2bin(script, {0xff, 0xff, 0x7f}, flags); | |||||
test_num2bin(script, {0x03, 0x02, 0x71}, flags); | |||||
test_num2bin(script, {0xff, 0xff, 0xff, 0x7f}, flags); | |||||
test_num2bin(script, {0x04, 0x03, 0x02, 0x71}, flags); | |||||
test_num2bin(script, {0x81}, flags); | |||||
test_num2bin(script, {0xff, 0x80}, flags); | |||||
test_num2bin(script, {0xaf, 0x81}, flags); | |||||
test_num2bin(script, {0xed, 0x60, 0x83}, flags); | |||||
test_num2bin(script, {0xb6, 0xe3, 0x81}, flags); | |||||
test_num2bin(script, {0x81, 0x9a, 0x6e, 0x84}, flags); | |||||
test_num2bin(script, {0xe4, 0xc3, 0x92, 0x91}, flags); | |||||
} | |||||
/// OP_BIN2NUM + OP_NUM2BIN tests | |||||
void test_bin2num_num2bin(const CScript &script, size_t size, int64_t numBinary, uint32_t flags) { | |||||
auto x = mk_bin(numBinary); | |||||
test(script, stack_t{x}, flags, stack_t{make_ev(CScriptNum(numBinary).getvch(), size)}); | |||||
} | |||||
void test_num2bin_bin2num(const CScript &script, int64_t numBinary, uint32_t flags) { | |||||
test(script, stack_t{CScriptNum(numBinary).getvch()}, flags, stack_t{CScriptNum(numBinary).getvch()}); | |||||
} | |||||
void test_bin2num_num2bin(int size, uint32_t flags) { | |||||
CScript script; | |||||
script << OP_BIN2NUM << size << OP_NUM2BIN; | |||||
test_bin2num_num2bin(script, size, 0, flags); | |||||
test_bin2num_num2bin(script, size, 1, flags); | |||||
test_bin2num_num2bin(script, size, -1, flags); | |||||
if (size >= 2) { | |||||
test_bin2num_num2bin(script, size, 321, flags); | |||||
test_bin2num_num2bin(script, size, -321, flags); | |||||
if (size >= 3) { | |||||
test_bin2num_num2bin(script, size, 106894, flags); | |||||
test_bin2num_num2bin(script, size, -106894, flags); | |||||
if (size >= 4) { | |||||
test_bin2num_num2bin(script, size, INT_MAX >> 1, flags); | |||||
test_bin2num_num2bin(script, size, INT_MIN >> 1, flags); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
void test_num2bin_bin2num(int size, uint32_t flags) { | |||||
CScript script; | |||||
script << size << OP_NUM2BIN << OP_BIN2NUM; | |||||
test_num2bin_bin2num(script, 0, flags); | |||||
test_num2bin_bin2num(script, 1, flags); | |||||
test_num2bin_bin2num(script, -1, flags); | |||||
if (size >= 2) { | |||||
test_num2bin_bin2num(script, 321, flags); | |||||
test_num2bin_bin2num(script, -321, flags); | |||||
if (size >= 3) { | |||||
test_num2bin_bin2num(script, 106894, flags); | |||||
test_num2bin_bin2num(script, -106894, flags); | |||||
if (size >= 4) { | |||||
test_num2bin_bin2num(script, INT_MAX >> 1, flags); | |||||
test_num2bin_bin2num(script, INT_MIN >> 1, flags); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
void test_bin2num_num2bin(uint32_t flags) { | |||||
test_bin2num_num2bin(4,flags); //expect 4 byte output | |||||
test_bin2num_num2bin(3,flags); //expect 3 byte output | |||||
test_bin2num_num2bin(2,flags); //expect 2 byte output | |||||
test_bin2num_num2bin(1,flags); //expect 1 byte output | |||||
} | |||||
void test_num2bin_bin2num(uint32_t flags) { | |||||
test_num2bin_bin2num(4,flags); //4 byte num2bin output | |||||
test_num2bin_bin2num(3,flags); //3 byte num2bin output | |||||
test_num2bin_bin2num(2,flags); //2 byte num2bin output | |||||
test_num2bin_bin2num(1,flags); //1 byte num2bin output | |||||
} | |||||
} | } | ||||
/// Entry points | /// Entry points | ||||
BOOST_AUTO_TEST_SUITE(op_code) | BOOST_AUTO_TEST_SUITE(op_code) | ||||
BOOST_AUTO_TEST_CASE(op_and) { | BOOST_AUTO_TEST_CASE(op_and) { | ||||
test_and(0); | test_and(0); | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | |||||
BOOST_AUTO_TEST_CASE(op_bin2num) { | BOOST_AUTO_TEST_CASE(op_bin2num) { | ||||
test_bin2num(0); | test_bin2num(0); | ||||
test_bin2num(STANDARD_SCRIPT_VERIFY_FLAGS); | test_bin2num(STANDARD_SCRIPT_VERIFY_FLAGS); | ||||
test_bin2num(STANDARD_NOT_MANDATORY_VERIFY_FLAGS); | test_bin2num(STANDARD_NOT_MANDATORY_VERIFY_FLAGS); | ||||
test_bin2num(STANDARD_LOCKTIME_VERIFY_FLAGS); | test_bin2num(STANDARD_LOCKTIME_VERIFY_FLAGS); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(op_num2bin) { | |||||
test_num2bin(0); | |||||
test_num2bin(STANDARD_SCRIPT_VERIFY_FLAGS); | |||||
test_num2bin(STANDARD_NOT_MANDATORY_VERIFY_FLAGS); | |||||
test_num2bin(STANDARD_LOCKTIME_VERIFY_FLAGS); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(bin2num_num2bin) { | |||||
test_bin2num_num2bin(0); | |||||
test_bin2num_num2bin(STANDARD_SCRIPT_VERIFY_FLAGS); | |||||
test_bin2num_num2bin(STANDARD_NOT_MANDATORY_VERIFY_FLAGS); | |||||
test_bin2num_num2bin(STANDARD_LOCKTIME_VERIFY_FLAGS); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(num2bin_bin2num) { | |||||
test_num2bin_bin2num(0); | |||||
test_num2bin_bin2num(STANDARD_SCRIPT_VERIFY_FLAGS); | |||||
test_num2bin_bin2num(STANDARD_NOT_MANDATORY_VERIFY_FLAGS); | |||||
test_num2bin_bin2num(STANDARD_LOCKTIME_VERIFY_FLAGS); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() | ||||