aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests/epee_utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit_tests/epee_utils.cpp')
-rw-r--r--tests/unit_tests/epee_utils.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp
index c384ce9a5..75cf2fdd4 100644
--- a/tests/unit_tests/epee_utils.cpp
+++ b/tests/unit_tests/epee_utils.cpp
@@ -46,6 +46,7 @@
#include "hex.h"
#include "net/net_utils_base.h"
#include "net/local_ip.h"
+#include "net/buffer.h"
#include "p2p/net_peerlist_boost_serialization.h"
#include "span.h"
#include "string_tools.h"
@@ -764,3 +765,71 @@ TEST(NetUtils, PrivateRanges)
ASSERT_EQ(is_local("0.0.30.172"), false);
ASSERT_EQ(is_local("0.0.30.127"), false);
}
+
+TEST(net_buffer, basic)
+{
+ epee::net_utils::buffer buf;
+
+ ASSERT_EQ(buf.size(), 0);
+ EXPECT_THROW(buf.span(1), std::runtime_error);
+ buf.append("a", 1);
+ epee::span<const uint8_t> span = buf.span(1);
+ ASSERT_EQ(span.size(), 1);
+ ASSERT_EQ(span.data()[0], 'a');
+ EXPECT_THROW(buf.span(2), std::runtime_error);
+ buf.append("bc", 2);
+ buf.erase(1);
+ EXPECT_THROW(buf.span(3), std::runtime_error);
+ span = buf.span(2);
+ ASSERT_EQ(span.size(), 2);
+ ASSERT_EQ(span.data()[0], 'b');
+ ASSERT_EQ(span.data()[1], 'c');
+ buf.erase(1);
+ EXPECT_THROW(buf.span(2), std::runtime_error);
+ span = buf.span(1);
+ ASSERT_EQ(span.size(), 1);
+ ASSERT_EQ(span.data()[0], 'c');
+ EXPECT_THROW(buf.erase(2), std::runtime_error);
+ buf.erase(1);
+ EXPECT_EQ(buf.size(), 0);
+ EXPECT_THROW(buf.span(1), std::runtime_error);
+}
+
+TEST(net_buffer, existing_capacity)
+{
+ epee::net_utils::buffer buf;
+
+ buf.append("123456789", 9);
+ buf.erase(9);
+ buf.append("abc", 3);
+ buf.append("def", 3);
+ ASSERT_EQ(buf.size(), 6);
+ epee::span<const uint8_t> span = buf.span(6);
+ ASSERT_TRUE(!memcmp(span.data(), "abcdef", 6));
+}
+
+TEST(net_buffer, reallocate)
+{
+ epee::net_utils::buffer buf;
+
+ buf.append(std::string(4000, ' ').c_str(), 4000);
+ buf.append(std::string(8000, '0').c_str(), 8000);
+ ASSERT_EQ(buf.size(), 12000);
+ epee::span<const uint8_t> span = buf.span(12000);
+ ASSERT_TRUE(!memcmp(span.data(), std::string(4000, ' ').c_str(), 4000));
+ ASSERT_TRUE(!memcmp(span.data() + 4000, std::string(8000, '0').c_str(), 8000));
+}
+
+TEST(net_buffer, move)
+{
+ epee::net_utils::buffer buf;
+
+ buf.append(std::string(400, ' ').c_str(), 400);
+ buf.erase(399);
+ buf.append(std::string(4000, '0').c_str(), 4000);
+ ASSERT_EQ(buf.size(), 4001);
+ epee::span<const uint8_t> span = buf.span(4001);
+ ASSERT_TRUE(!memcmp(span.data(), std::string(1, ' ').c_str(), 1));
+ ASSERT_TRUE(!memcmp(span.data() + 1, std::string(4000, '0').c_str(), 4000));
+}
+