aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLee Clagett <code@leeclagett.com>2020-08-17 16:20:09 -0400
committerLee Clagett <code@leeclagett.com>2020-08-17 21:30:34 -0400
commitb641e0a2c0f70227a526cfafd4977c87f724a6da (patch)
treea56688591a79b99ab717d9ef06c48b97678d6374
parentRevert "Use domain-separated ChaCha20 for in-memory key encryption" (diff)
downloadmonero-b641e0a2c0f70227a526cfafd4977c87f724a6da.tar.xz
Add clear method to byte_stream
-rw-r--r--contrib/epee/include/byte_stream.h3
-rw-r--r--tests/unit_tests/epee_utils.cpp41
2 files changed, 44 insertions, 0 deletions
diff --git a/contrib/epee/include/byte_stream.h b/contrib/epee/include/byte_stream.h
index 98f563ca9..42a9e1dd9 100644
--- a/contrib/epee/include/byte_stream.h
+++ b/contrib/epee/include/byte_stream.h
@@ -117,6 +117,9 @@ namespace epee
check(more);
}
+ //! Reset write position, but do not release internal memory. \post `size() == 0`.
+ void clear() noexcept { next_write_ = buffer_.get(); }
+
/*! Copy `length` bytes starting at `ptr` to end of stream.
\throw std::range_error If exceeding max size_t value.
\throw std::bad_alloc If allocation fails. */
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp
index 0f91671a7..b365cad86 100644
--- a/tests/unit_tests/epee_utils.cpp
+++ b/tests/unit_tests/epee_utils.cpp
@@ -1117,6 +1117,47 @@ TEST(ByteStream, ToByteSlice)
EXPECT_EQ(nullptr, empty_slice.data());
}
+TEST(ByteStream, Clear)
+{
+ static constexpr const std::uint8_t source[] =
+ {0xde, 0xad, 0xbe, 0xef, 0xef};
+
+ epee::byte_stream stream{4};
+
+ EXPECT_EQ(4u, stream.increase_size());
+
+ EXPECT_EQ(nullptr, stream.data());
+ EXPECT_EQ(nullptr, stream.tellp());
+ EXPECT_EQ(0u, stream.size());
+ EXPECT_EQ(0u, stream.available());
+ EXPECT_EQ(0u, stream.capacity());
+
+ stream.clear();
+
+ EXPECT_EQ(nullptr, stream.data());
+ EXPECT_EQ(nullptr, stream.tellp());
+ EXPECT_EQ(0u, stream.size());
+ EXPECT_EQ(0u, stream.available());
+ EXPECT_EQ(0u, stream.capacity());
+
+ stream.write({source, 3});
+ std::uint8_t const* const loc = stream.data();
+
+ EXPECT_EQ(loc, stream.data());
+ EXPECT_EQ(loc + 3, stream.tellp());
+ EXPECT_EQ(3u, stream.size());
+ EXPECT_EQ(1u, stream.available());
+ EXPECT_EQ(4u, stream.capacity());
+
+ stream.clear();
+
+ EXPECT_EQ(loc, stream.data());
+ EXPECT_EQ(loc, stream.tellp());
+ EXPECT_EQ(0u, stream.size());
+ EXPECT_EQ(4u, stream.available());
+ EXPECT_EQ(4u, stream.capacity());
+}
+
TEST(ToHex, String)
{
EXPECT_TRUE(epee::to_hex::string(nullptr).empty());