Skip to content

Commit

Permalink
test: db_tests, add coverage for "\xff\xff" raw prefix, no size appen…
Browse files Browse the repository at this point in the history
…ded to it
  • Loading branch information
furszy committed Jun 2, 2023
1 parent ba616b9 commit 1ace124
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ class DatabaseBatch
return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
}

template <typename K, typename T>
bool RawWrite(const K& key, const T& value, bool fOverwrite = true)
{
DataStream ssKey{};
ssKey.reserve(1000);
ssKey.write(key);

CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.reserve(10000);
ssValue << value;

return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
}

template <typename K>
bool Erase(const K& key)
{
Expand Down
34 changes: 34 additions & 0 deletions src/wallet/test/db_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,40 @@ BOOST_AUTO_TEST_CASE(db_cursor_prefix_range_test)
// Let's now read it once more, it should return DONE
BOOST_CHECK(cursor->Next(key, value) == DatabaseCursor::Status::DONE);
}

// ############################################################################################################
// # Write one of the special keys, on its raw form. No serialization used, so the size is not appended to it #
// ############################################################################################################

std::string special_prefix = "\xff\xff";

for (unsigned int i = 0; i < 10; i++) {
DataStream ss;
ss.write(MakeByteSpan(special_prefix)); // write prefix with no size
ss << i;
BOOST_CHECK(handler->RawWrite(ss, i));
}

{
std::unique_ptr<DatabaseCursor> cursor = handler->GetNewPrefixCursor(MakeByteSpan(special_prefix));
DataStream key;
DataStream value;
for (int i = 0; i < 10; i++) {
DatabaseCursor::Status status = cursor->Next(key, value);
BOOST_ASSERT(status == DatabaseCursor::Status::MORE);

// Manually read and skip the key prefix
std::string key_back{UCharCast(key.data()), UCharCast(key.data() + special_prefix.size())};
key.ignore(special_prefix.size());
unsigned int i_back;
key >> i_back;
BOOST_CHECK_EQUAL(key_back, special_prefix);

unsigned int value_back;
value >> value_back;
BOOST_CHECK_EQUAL(value_back, i_back);
}
}
}
}

Expand Down

0 comments on commit 1ace124

Please sign in to comment.