diff options
Diffstat (limited to 'tests/unit_tests')
-rw-r--r-- | tests/unit_tests/epee_utils.cpp | 256 | ||||
-rw-r--r-- | tests/unit_tests/json_serialization.cpp | 8 | ||||
-rw-r--r-- | tests/unit_tests/levin.cpp | 4 | ||||
-rw-r--r-- | tests/unit_tests/net.cpp | 39 | ||||
-rw-r--r-- | tests/unit_tests/ringct.cpp | 8 |
5 files changed, 310 insertions, 5 deletions
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index 4f42140b3..2e93f9e67 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2014-2019, The Monero Project +// Copyright (c) 2014-2020, The Monero Project // // All rights reserved. // @@ -45,6 +45,7 @@ #include "boost/archive/portable_binary_iarchive.hpp" #include "boost/archive/portable_binary_oarchive.hpp" #include "byte_slice.h" +#include "byte_stream.h" #include "crypto/crypto.h" #include "hex.h" #include "net/net_utils_base.h" @@ -809,6 +810,259 @@ TEST(ByteSlice, GetSlice) EXPECT_TRUE(boost::range::equal(base_string, original)); } +TEST(ByteStream, Construction) +{ + EXPECT_TRUE(std::is_default_constructible<epee::byte_stream>()); + EXPECT_TRUE(std::is_move_constructible<epee::byte_stream>()); + EXPECT_FALSE(std::is_copy_constructible<epee::byte_stream>()); + EXPECT_TRUE(std::is_move_assignable<epee::byte_stream>()); + EXPECT_FALSE(std::is_copy_assignable<epee::byte_stream>()); +} + +TEST(ByteStream, Noexcept) +{ + EXPECT_TRUE(std::is_nothrow_default_constructible<epee::byte_stream>()); + EXPECT_TRUE(std::is_nothrow_move_constructible<epee::byte_stream>()); + EXPECT_TRUE(std::is_nothrow_move_assignable<epee::byte_stream>()); + + epee::byte_stream lvalue; + const epee::byte_stream clvalue; + + EXPECT_TRUE(noexcept(lvalue.data())); + EXPECT_TRUE(noexcept(clvalue.data())); + EXPECT_TRUE(noexcept(lvalue.tellp())); + EXPECT_TRUE(noexcept(clvalue.tellp())); + EXPECT_TRUE(noexcept(lvalue.available())); + EXPECT_TRUE(noexcept(clvalue.available())); + EXPECT_TRUE(noexcept(lvalue.size())); + EXPECT_TRUE(noexcept(clvalue.size())); + EXPECT_TRUE(noexcept(lvalue.capacity())); + EXPECT_TRUE(noexcept(clvalue.capacity())); + EXPECT_TRUE(noexcept(lvalue.put_unsafe(4))); + EXPECT_TRUE(noexcept(lvalue.take_buffer())); +} + +TEST(ByteStream, Empty) +{ + epee::byte_stream stream; + + EXPECT_EQ(epee::byte_stream::default_increase(), stream.increase_size()); + + EXPECT_EQ(nullptr, stream.data()); + EXPECT_EQ(nullptr, stream.tellp()); + EXPECT_EQ(0u, stream.available()); + EXPECT_EQ(0u, stream.size()); + EXPECT_EQ(0u, stream.capacity()); + + const auto buf = stream.take_buffer(); + EXPECT_EQ(nullptr, buf.get()); + EXPECT_EQ(nullptr, stream.data()); + EXPECT_EQ(nullptr, stream.tellp()); + EXPECT_EQ(0u, stream.available()); + EXPECT_EQ(0u, stream.size()); + EXPECT_EQ(0u, stream.capacity()); +} + +TEST(ByteStream, Write) +{ + using boost::range::equal; + using byte_span = epee::span<const std::uint8_t>; + + static constexpr const std::uint8_t source[] = + {0xde, 0xad, 0xbe, 0xef, 0xef}; + + std::vector<std::uint8_t> bytes; + epee::byte_stream stream{4}; + + EXPECT_EQ(4u, stream.increase_size()); + + stream.write({source, 3}); + bytes.insert(bytes.end(), source, source + 3); + EXPECT_EQ(3u, stream.size()); + EXPECT_EQ(1u, stream.available()); + EXPECT_EQ(4u, stream.capacity()); + EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); + + stream.write({source, 2}); + bytes.insert(bytes.end(), source, source + 2); + EXPECT_EQ(5u, stream.size()); + EXPECT_EQ(3u, stream.available()); + EXPECT_EQ(8u, stream.capacity()); + EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); + + stream.write({source, 5}); + bytes.insert(bytes.end(), source, source + 5); + EXPECT_EQ(10u, stream.size()); + EXPECT_EQ(2u, stream.available()); + EXPECT_EQ(12u, stream.capacity()); + EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); + + stream.write({source, 2}); + bytes.insert(bytes.end(), source, source + 2); + EXPECT_EQ(12u, stream.size()); + EXPECT_EQ(0u, stream.available()); + EXPECT_EQ(12u, stream.capacity()); + EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); + + stream.write({source, 5}); + bytes.insert(bytes.end(), source, source + 5); + EXPECT_EQ(17u, stream.size()); + EXPECT_EQ(0u, stream.available()); + EXPECT_EQ(17u, stream.capacity()); + EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); +} + +TEST(ByteStream, Put) +{ + using boost::range::equal; + using byte_span = epee::span<const std::uint8_t>; + + std::vector<std::uint8_t> bytes; + epee::byte_stream stream; + + for (std::uint8_t i = 0; i < 200; ++i) + { + bytes.push_back(i); + stream.put(i); + } + + EXPECT_EQ(200u, stream.size()); + EXPECT_EQ(epee::byte_stream::default_increase() - 200, stream.available()); + EXPECT_EQ(epee::byte_stream::default_increase(), stream.capacity()); + EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); +} + +TEST(ByteStream, Reserve) +{ + using boost::range::equal; + using byte_span = epee::span<const std::uint8_t>; + + static constexpr const std::uint8_t source[] = + {0xde, 0xad, 0xbe, 0xef, 0xef}; + + std::vector<std::uint8_t> bytes; + epee::byte_stream stream{4}; + + EXPECT_EQ(4u, stream.increase_size()); + + stream.reserve(100); + EXPECT_EQ(100u, stream.capacity()); + EXPECT_EQ(0u, stream.size()); + EXPECT_EQ(100u, stream.available()); + + for (std::size_t i = 0; i < 100 / sizeof(source); ++i) + { + stream.write(source); + bytes.insert(bytes.end(), source, source + sizeof(source)); + } + + EXPECT_EQ(100u, stream.size()); + EXPECT_EQ(0u, stream.available()); + EXPECT_EQ(100u, stream.capacity()); + EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); +} + +TEST(ByteStream, TakeBuffer) +{ + using boost::range::equal; + using byte_span = epee::span<const std::uint8_t>; + + static constexpr const std::uint8_t source[] = + {0xde, 0xad, 0xbe, 0xef, 0xef}; + + epee::byte_stream stream; + + stream.write(source); + ASSERT_EQ(sizeof(source), stream.size()); + EXPECT_TRUE(equal(source, byte_span{stream.data(), stream.size()})); + + const auto buffer = stream.take_buffer(); + EXPECT_EQ(0u, stream.size()); + EXPECT_EQ(0u, stream.available()); + EXPECT_EQ(0u, stream.capacity()); + EXPECT_EQ(nullptr, stream.data()); + EXPECT_EQ(nullptr, stream.tellp()); + EXPECT_TRUE(equal(source, byte_span{buffer.get(), sizeof(source)})); +} + +TEST(ByteStream, Move) +{ + using boost::range::equal; + using byte_span = epee::span<const std::uint8_t>; + + static constexpr const std::uint8_t source[] = + {0xde, 0xad, 0xbe, 0xef, 0xef}; + + epee::byte_stream stream{10}; + stream.write(source); + + epee::byte_stream stream2{std::move(stream)}; + + EXPECT_EQ(10u, stream.increase_size()); + EXPECT_EQ(0u, stream.size()); + EXPECT_EQ(0u, stream.available()); + EXPECT_EQ(0u, stream.capacity()); + EXPECT_EQ(nullptr, stream.data()); + EXPECT_EQ(nullptr, stream.tellp()); + + EXPECT_EQ(10u, stream2.increase_size()); + EXPECT_EQ(5u, stream2.size()); + EXPECT_EQ(5u, stream2.available()); + EXPECT_EQ(10u, stream2.capacity()); + EXPECT_NE(nullptr, stream2.data()); + EXPECT_NE(nullptr, stream2.tellp()); + EXPECT_TRUE(equal(source, byte_span{stream2.data(), stream2.size()})); + + stream = epee::byte_stream{}; + + EXPECT_EQ(epee::byte_stream::default_increase(), stream.increase_size()); + EXPECT_EQ(0u, stream.size()); + EXPECT_EQ(0u, stream.available()); + EXPECT_EQ(0u, stream.capacity()); + EXPECT_EQ(nullptr, stream.data()); + EXPECT_EQ(nullptr, stream.tellp()); + + stream = std::move(stream2); + + EXPECT_EQ(10u, stream.increase_size()); + EXPECT_EQ(5u, stream.size()); + EXPECT_EQ(5u, stream.available()); + EXPECT_EQ(10u, stream.capacity()); + EXPECT_NE(nullptr, stream.data()); + EXPECT_NE(nullptr, stream.tellp()); + EXPECT_TRUE(equal(source, byte_span{stream.data(), stream.size()})); + + EXPECT_EQ(10u, stream2.increase_size()); + EXPECT_EQ(0u, stream2.size()); + EXPECT_EQ(0u, stream2.available()); + EXPECT_EQ(0u, stream2.capacity()); + EXPECT_EQ(nullptr, stream2.data()); + EXPECT_EQ(nullptr, stream2.tellp()); +} + +TEST(ByteStream, ToByteSlice) +{ + using boost::range::equal; + using byte_span = epee::span<const std::uint8_t>; + + static constexpr const std::uint8_t source[] = + {0xde, 0xad, 0xbe, 0xef, 0xef}; + + epee::byte_stream stream; + + stream.write(source); + EXPECT_EQ(sizeof(source), stream.size()); + EXPECT_TRUE(equal(source, byte_span{stream.data(), stream.size()})); + + const epee::byte_slice slice{std::move(stream)}; + EXPECT_EQ(0u, stream.size()); + EXPECT_EQ(0u, stream.available()); + EXPECT_EQ(0u, stream.capacity()); + EXPECT_EQ(nullptr, stream.data()); + EXPECT_EQ(nullptr, stream.tellp()); + EXPECT_TRUE(equal(source, slice)); +} + TEST(ToHex, String) { EXPECT_TRUE(epee::to_hex::string(nullptr).empty()); diff --git a/tests/unit_tests/json_serialization.cpp b/tests/unit_tests/json_serialization.cpp index 6f98d854d..5873d0ab6 100644 --- a/tests/unit_tests/json_serialization.cpp +++ b/tests/unit_tests/json_serialization.cpp @@ -3,10 +3,10 @@ #include <boost/range/adaptor/indexed.hpp> #include <gtest/gtest.h> #include <rapidjson/document.h> -#include <rapidjson/stringbuffer.h> #include <rapidjson/writer.h> #include <vector> +#include "byte_stream.h" #include "crypto/hash.h" #include "cryptonote_basic/account.h" #include "cryptonote_basic/cryptonote_basic.h" @@ -86,14 +86,14 @@ namespace template<typename T> T test_json(const T& value) { - rapidjson::StringBuffer buffer; + epee::byte_stream buffer; { - rapidjson::Writer<rapidjson::StringBuffer> dest{buffer}; + rapidjson::Writer<epee::byte_stream> dest{buffer}; cryptonote::json::toJsonValue(dest, value); } rapidjson::Document doc; - doc.Parse(buffer.GetString()); + doc.Parse(reinterpret_cast<const char*>(buffer.data()), buffer.size()); if (doc.HasParseError() || !doc.IsObject()) { throw cryptonote::json::PARSE_FAIL(); diff --git a/tests/unit_tests/levin.cpp b/tests/unit_tests/levin.cpp index 62b101a42..d2ef920ef 100644 --- a/tests/unit_tests/levin.cpp +++ b/tests/unit_tests/levin.cpp @@ -583,7 +583,9 @@ TEST_F(levin_notify, stem_without_padding) { const std::size_t sent = context->process_send_queue(); if (sent && is_stem) + { EXPECT_EQ(1u, (context - contexts_.begin()) % 2); + } send_count += sent; } @@ -653,7 +655,9 @@ TEST_F(levin_notify, local_without_padding) { const std::size_t sent = context->process_send_queue(); if (sent && is_stem) + { EXPECT_EQ(1u, (context - contexts_.begin()) % 2); + } send_count += sent; } diff --git a/tests/unit_tests/net.cpp b/tests/unit_tests/net.cpp index 36cb28ae0..f5aef4796 100644 --- a/tests/unit_tests/net.cpp +++ b/tests/unit_tests/net.cpp @@ -1702,6 +1702,45 @@ TEST(zmq, read_write) EXPECT_EQ(message, *received); } +TEST(zmq, read_write_slice) +{ + net::zmq::context context{zmq_init(1)}; + ASSERT_NE(nullptr, context); + + net::zmq::socket send_socket{zmq_socket(context.get(), ZMQ_REQ)}; + net::zmq::socket recv_socket{zmq_socket(context.get(), ZMQ_REP)}; + ASSERT_NE(nullptr, send_socket); + ASSERT_NE(nullptr, recv_socket); + + ASSERT_EQ(0u, zmq_bind(recv_socket.get(), "inproc://testing")); + ASSERT_EQ(0u, zmq_connect(send_socket.get(), "inproc://testing")); + + std::string message; + message.resize(1024); + crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0]))); + + { + epee::byte_slice slice_message{{epee::strspan<std::uint8_t>(message)}}; + ASSERT_TRUE(bool(net::zmq::send(std::move(slice_message), send_socket.get()))); + EXPECT_TRUE(slice_message.empty()); + } + + const expect<std::string> received = net::zmq::receive(recv_socket.get()); + ASSERT_TRUE(bool(received)); + EXPECT_EQ(message, *received); +} + +TEST(zmq, write_slice_fail) +{ + std::string message; + message.resize(1024); + crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0]))); + + epee::byte_slice slice_message{std::move(message)}; + EXPECT_FALSE(bool(net::zmq::send(std::move(slice_message), nullptr))); + EXPECT_TRUE(slice_message.empty()); +} + TEST(zmq, read_write_multipart) { net::zmq::context context{zmq_init(1)}; diff --git a/tests/unit_tests/ringct.cpp b/tests/unit_tests/ringct.cpp index 075aeac40..64fcbba4c 100644 --- a/tests/unit_tests/ringct.cpp +++ b/tests/unit_tests/ringct.cpp @@ -1077,8 +1077,16 @@ TEST(ringct, H) TEST(ringct, mul8) { + ge_p3 p3; + rct::key key; ASSERT_EQ(rct::scalarmult8(rct::identity()), rct::identity()); + rct::scalarmult8(p3,rct::identity()); + ge_p3_tobytes(key.bytes, &p3); + ASSERT_EQ(key, rct::identity()); ASSERT_EQ(rct::scalarmult8(rct::H), rct::scalarmultKey(rct::H, rct::EIGHT)); + rct::scalarmult8(p3,rct::H); + ge_p3_tobytes(key.bytes, &p3); + ASSERT_EQ(key, rct::scalarmultKey(rct::H, rct::EIGHT)); ASSERT_EQ(rct::scalarmultKey(rct::scalarmultKey(rct::H, rct::INV_EIGHT), rct::EIGHT), rct::H); } |