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> v, size_t sz) { //v contains a num in LE | |||||
jasonbcox: variable names could improve here too :) | |||||
if (v.empty()) return vector<uint8_t>(sz,0); | |||||
vector<uint8_t> ans; | |||||
assert(sz>=v.size()); | |||||
ans.reserve(sz); | |||||
bool neg=*v.rbegin()&0x80; | |||||
*v.rbegin()&=~0x80; | |||||
size_t pad=sz-v.size(); | |||||
for (uint8_t i=0; i<pad; ++i) { | |||||
ans.push_back(0); | |||||
} | |||||
for (auto i=v.rbegin(); i!=v.rend(); ++i) { | |||||
ans.push_back(*i); | |||||
} | |||||
if (neg) *ans.begin()|=0x80; | |||||
return ans; | |||||
} | |||||
void test_num2bin(const CScript& script, vector<uint8_t> v, uint32_t flags) { | |||||
if (v.empty()) return; | |||||
for (uint8_t i=0; i<v.size(); ++i) { | |||||
if (i==0) | |||||
test(script,stack_t{v,{}},flags,SCRIPT_ERR_INVALID_NUM2BIN_OPERATION); | |||||
else | |||||
test(script,stack_t{v,{i}},flags,SCRIPT_ERR_INVALID_NUM2BIN_OPERATION); | |||||
} | |||||
for (uint8_t i=v.size(); i<=CScriptNum::nDefaultMaxNumSize; ++i) { | |||||
if (i==0) | |||||
test(script,stack_t{v,{}},flags,stack_t{make_ev(v,i)}); | |||||
else | |||||
test(script,stack_t{v,{i}},flags,stack_t{make_ev(v,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, int sz, int64_t v, uint32_t flags) { | |||||
auto x=mk_bin(v); | |||||
test(script,stack_t{x},flags,stack_t{make_ev(CScriptNum(v).getvch(),sz)}); | |||||
} | |||||
void test_num2bin_bin2num(const CScript& script, int64_t v, uint32_t flags) { | |||||
test(script,stack_t{CScriptNum(v).getvch()},flags,stack_t{CScriptNum(v).getvch()}); | |||||
} | |||||
void test_bin2num_num2bin(int sz, uint32_t flags) { | |||||
CScript script; | |||||
script << OP_BIN2NUM << sz << OP_NUM2BIN; | |||||
test_bin2num_num2bin(script,sz,0,flags); | |||||
test_bin2num_num2bin(script,sz,1,flags); | |||||
test_bin2num_num2bin(script,sz,-1,flags); | |||||
if (sz>=2) { | |||||
test_bin2num_num2bin(script,sz,321,flags); | |||||
test_bin2num_num2bin(script,sz,-321,flags); | |||||
if (sz>=3) { | |||||
test_bin2num_num2bin(script,sz,106894,flags); | |||||
test_bin2num_num2bin(script,sz,-106894,flags); | |||||
if (sz>=4) { | |||||
test_bin2num_num2bin(script,sz,INT_MAX>>1,flags); | |||||
test_bin2num_num2bin(script,sz,INT_MIN>>1,flags); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
void test_num2bin_bin2num(int sz, uint32_t flags) { | |||||
CScript script; | |||||
script << sz << OP_NUM2BIN << OP_BIN2NUM; | |||||
test_num2bin_bin2num(script,0,flags); | |||||
test_num2bin_bin2num(script,1,flags); | |||||
test_num2bin_bin2num(script,-1,flags); | |||||
if (sz>=2) { | |||||
test_num2bin_bin2num(script,321,flags); | |||||
test_num2bin_bin2num(script,-321,flags); | |||||
if (sz>=3) { | |||||
test_num2bin_bin2num(script,106894,flags); | |||||
test_num2bin_bin2num(script,-106894,flags); | |||||
if (sz>=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() | ||||
variable names could improve here too :)