Changeset View
Changeset View
Standalone View
Standalone View
src/test/serialize_tests.cpp
Show First 20 Lines • Show All 483 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(difference_formatter) { | ||||
} | } | ||||
{ | { | ||||
std::vector<uint32_t> indicesOut; | std::vector<uint32_t> indicesOut; | ||||
// Compute the number of MAX_SIZE increment we need to cause an overflow | // Compute the number of MAX_SIZE increment we need to cause an overflow | ||||
const uint64_t overflow = | const uint64_t overflow = | ||||
uint64_t(std::numeric_limits<uint32_t>::max()) + 1; | uint64_t(std::numeric_limits<uint32_t>::max()) + 1; | ||||
BOOST_CHECK_GE(overflow, MAX_SIZE); | // Due to differential encoding, a value of MAX_SIZE bumps the index by | ||||
const uint64_t overflowIter = overflow / MAX_SIZE; | // MAX_SIZE + 1 | ||||
BOOST_CHECK_GE(overflow, MAX_SIZE + 1); | |||||
const uint64_t overflowIter = overflow / (MAX_SIZE + 1); | |||||
// Make sure the iteration fits in an uint32_t | // Make sure the iteration fits in an uint32_t and is <= MAX_SIZE | ||||
BOOST_CHECK_LE(overflowIter, std::numeric_limits<uint32_t>::max()); | BOOST_CHECK_LE(overflowIter, std::numeric_limits<uint32_t>::max()); | ||||
uint32_t remainder = uint32_t(overflow - (MAX_SIZE * overflowIter)); | BOOST_CHECK_LE(overflowIter, MAX_SIZE); | ||||
uint32_t remainder = | |||||
uint32_t(overflow - ((MAX_SIZE + 1) * overflowIter)); | |||||
auto buildStream = [&](uint32_t lastItemDifference) { | |||||
CDataStream ss(SER_DISK, PROTOCOL_VERSION); | CDataStream ss(SER_DISK, PROTOCOL_VERSION); | ||||
WriteCompactSize(ss, overflowIter); | WriteCompactSize(ss, overflowIter + 1); | ||||
for (uint32_t i = 0; i < overflowIter; i++) { | for (uint32_t i = 0; i < overflowIter; i++) { | ||||
WriteCompactSize(ss, MAX_SIZE); | WriteCompactSize(ss, MAX_SIZE); | ||||
} | } | ||||
// This is the overflow | // This will cause an overflow if lastItemDifference >= remainder | ||||
WriteCompactSize(ss, remainder); | WriteCompactSize(ss, lastItemDifference); | ||||
BOOST_CHECK_EXCEPTION(formatter.Unser(ss, indicesOut), | return ss; | ||||
}; | |||||
auto noThrowStream = buildStream(remainder - 1); | |||||
BOOST_CHECK_NO_THROW(formatter.Unser(noThrowStream, indicesOut)); | |||||
auto overflowStream = buildStream(remainder); | |||||
BOOST_CHECK_EXCEPTION(formatter.Unser(overflowStream, indicesOut), | |||||
std::ios_base::failure, | std::ios_base::failure, | ||||
HasReason("differential value overflow")); | HasReason("differential value overflow")); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |