mirror of
https://github.com/vtnerd/monero-lws.git
synced 2024-11-16 09:17:37 +00:00
Added unit tests, and fixed two bugs: (#53)
* Integer conversion checks in src/wire/read.h * Missing "boolean" function in wire::writer and derived types
This commit is contained in:
parent
d233c72b5e
commit
c958ac7963
20 changed files with 2326 additions and 70 deletions
|
@ -33,6 +33,8 @@ enable_language(CXX)
|
|||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
option(BUILD_TESTS "Build Tests" OFF)
|
||||
|
||||
set(MONERO_LIBRARIES
|
||||
daemon_messages
|
||||
serialization
|
||||
|
@ -247,3 +249,8 @@ set_property(TARGET monero::libraries PROPERTY
|
|||
#
|
||||
|
||||
add_subdirectory(src)
|
||||
|
||||
if (BUILD_TESTS)
|
||||
enable_testing()
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
|
|
|
@ -165,7 +165,7 @@ namespace lws
|
|||
|
||||
void rpc::read_bytes(wire::json_reader& source, safe_uint64& self)
|
||||
{
|
||||
self = safe_uint64(wire::integer::convert_to<std::uint64_t>(source.safe_unsigned_integer()));
|
||||
self = safe_uint64(wire::integer::cast_unsigned<std::uint64_t>(source.safe_unsigned_integer()));
|
||||
}
|
||||
void rpc::write_bytes(wire::json_writer& dest, const safe_uint64 self)
|
||||
{
|
||||
|
@ -175,7 +175,7 @@ namespace lws
|
|||
void rpc::read_bytes(wire::json_reader& source, safe_uint64_array& self)
|
||||
{
|
||||
for (std::size_t count = source.start_array(); !source.is_array_end(count); --count)
|
||||
self.values.emplace_back(wire::integer::convert_to<std::uint64_t>(source.safe_unsigned_integer()));
|
||||
self.values.emplace_back(wire::integer::cast_unsigned<std::uint64_t>(source.safe_unsigned_integer()));
|
||||
source.end_array();
|
||||
}
|
||||
|
||||
|
|
|
@ -233,13 +233,22 @@ namespace wire
|
|||
return json_bool.value.boolean;
|
||||
}
|
||||
|
||||
using imax_limits = std::numeric_limits<std::intmax_t>;
|
||||
static_assert(0 <= imax_limits::max(), "expected 0 <= intmax_t::max");
|
||||
static_assert(
|
||||
imax_limits::max() <= std::numeric_limits<std::uintmax_t>::max(),
|
||||
"expected intmax_t::max <= uintmax_t::max"
|
||||
);
|
||||
|
||||
std::intmax_t json_reader::integer()
|
||||
{
|
||||
rapidjson_sax json_int{error::schema::integer};
|
||||
read_next_value(json_int);
|
||||
if (json_int.negative)
|
||||
return json_int.value.integer;
|
||||
return integer::convert_to<std::intmax_t>(json_int.value.unsigned_integer);
|
||||
if (static_cast<std::uintmax_t>(imax_limits::max()) < json_int.value.unsigned_integer)
|
||||
WIRE_DLOG_THROW_(error::schema::smaller_integer);
|
||||
return static_cast<std::intmax_t>(json_int.value.unsigned_integer);
|
||||
}
|
||||
|
||||
std::uintmax_t json_reader::unsigned_integer()
|
||||
|
@ -248,7 +257,9 @@ namespace wire
|
|||
read_next_value(json_uint);
|
||||
if (!json_uint.negative)
|
||||
return json_uint.value.unsigned_integer;
|
||||
return integer::convert_to<std::uintmax_t>(json_uint.value.integer);
|
||||
if (json_uint.value.integer < 0)
|
||||
WIRE_DLOG_THROW_(error::schema::larger_integer);
|
||||
return static_cast<std::uintmax_t>(json_uint.value.integer);
|
||||
}
|
||||
/*
|
||||
const std::vector<std::uintmax_t>& json_reader::unsigned_integer_array()
|
||||
|
|
|
@ -73,6 +73,12 @@ namespace wire
|
|||
return buf;
|
||||
}
|
||||
|
||||
void json_writer::boolean(const bool source)
|
||||
{
|
||||
formatter_.Bool(source);
|
||||
check_flush();
|
||||
}
|
||||
|
||||
void json_writer::integer(const int source)
|
||||
{
|
||||
formatter_.Int(source);
|
||||
|
|
|
@ -85,6 +85,8 @@ namespace wire
|
|||
//! \return Null-terminated buffer containing uint as decimal ascii
|
||||
static std::array<char, uint_to_string_size> to_string(std::uintmax_t) noexcept;
|
||||
|
||||
void boolean(bool) override final;
|
||||
|
||||
void integer(int) override final;
|
||||
void integer(std::intmax_t) override final;
|
||||
|
||||
|
|
|
@ -35,9 +35,16 @@ void wire::reader::increment_depth()
|
|||
WIRE_DLOG_THROW_(error::schema::maximum_depth);
|
||||
}
|
||||
|
||||
[[noreturn]] void wire::integer::throw_exception(std::intmax_t source, std::intmax_t min)
|
||||
[[noreturn]] void wire::integer::throw_exception(std::intmax_t source, std::intmax_t min, std::intmax_t max)
|
||||
{
|
||||
static_assert(
|
||||
std::numeric_limits<std::intmax_t>::max() <= std::numeric_limits<std::uintmax_t>::max(),
|
||||
"expected intmax_t::max <= uintmax_t::max"
|
||||
);
|
||||
if (source < 0)
|
||||
WIRE_DLOG_THROW(error::schema::larger_integer, source << " given when " << min << " is minimum permitted");
|
||||
else
|
||||
throw_exception(std::uintmax_t(source), std::uintmax_t(max));
|
||||
}
|
||||
[[noreturn]] void wire::integer::throw_exception(std::uintmax_t source, std::uintmax_t max)
|
||||
{
|
||||
|
|
107
src/wire/read.h
107
src/wire/read.h
|
@ -83,7 +83,7 @@ namespace wire
|
|||
//! \throw wire::exception if next value not a boolean.
|
||||
virtual bool boolean() = 0;
|
||||
|
||||
//! \throw wire::expception if next value not an integer.
|
||||
//! \throw wire::exception if next value not an integer.
|
||||
virtual std::intmax_t integer() = 0;
|
||||
|
||||
//! \throw wire::exception if next value not an unsigned integer.
|
||||
|
@ -104,8 +104,7 @@ namespace wire
|
|||
//! \throw wire::exception if next value invalid enum. \return Index in `enums`.
|
||||
virtual std::size_t enumeration(epee::span<char const* const> enums) = 0;
|
||||
|
||||
/*! \throw wire::exception if next value not array
|
||||
\return Number of values to read before calling `is_array_end()`. */
|
||||
//! \throw wire::exception if next value not array
|
||||
virtual std::size_t start_array() = 0;
|
||||
|
||||
//! \return True if there is another element to read.
|
||||
|
@ -167,76 +166,58 @@ namespace wire
|
|||
|
||||
namespace integer
|
||||
{
|
||||
[[noreturn]] void throw_exception(std::intmax_t source, std::intmax_t min);
|
||||
[[noreturn]] void throw_exception(std::uintmax_t source, std::uintmax_t max);
|
||||
[[noreturn]] void throw_exception(std::intmax_t value, std::intmax_t min, std::intmax_t max);
|
||||
[[noreturn]] void throw_exception(std::uintmax_t value, std::uintmax_t max);
|
||||
|
||||
template<typename Target, typename U>
|
||||
inline Target convert_to(const U source)
|
||||
template<typename T, typename U>
|
||||
inline T cast_signed(const U source)
|
||||
{
|
||||
using common = typename std::common_type<Target, U>::type;
|
||||
static constexpr const Target target_min = std::numeric_limits<Target>::min();
|
||||
static constexpr const Target target_max = std::numeric_limits<Target>::max();
|
||||
using limit = std::numeric_limits<T>;
|
||||
static_assert(
|
||||
std::is_signed<T>::value && std::is_integral<T>::value,
|
||||
"target must be signed integer type"
|
||||
);
|
||||
static_assert(
|
||||
std::is_signed<U>::value && std::is_integral<U>::value,
|
||||
"source must be signed integer type"
|
||||
);
|
||||
if (source < limit::min() || limit::max() < source)
|
||||
throw_exception(source, limit::min(), limit::max());
|
||||
return static_cast<T>(source);
|
||||
}
|
||||
|
||||
/* After optimizations, this is:
|
||||
* 1 check for unsigned -> unsigned (uint, uint)
|
||||
* 2 checks for signed -> signed (int, int)
|
||||
* 2 checks for signed -> unsigned-- (
|
||||
* 1 check for unsigned -> signed (uint, uint)
|
||||
|
||||
Put `WIRE_DLOG_THROW` in cpp to reduce code/ASM duplication. Do not
|
||||
remove first check, signed values can be implicitly converted to
|
||||
unsigned in some checks. */
|
||||
if (!std::numeric_limits<Target>::is_signed && source < 0)
|
||||
throw_exception(std::intmax_t(source), std::intmax_t(0));
|
||||
else if (common(source) < common(target_min))
|
||||
throw_exception(std::intmax_t(source), std::intmax_t(target_min));
|
||||
else if (common(target_max) < common(source))
|
||||
throw_exception(std::uintmax_t(source), std::uintmax_t(target_max));
|
||||
|
||||
return Target(source);
|
||||
template<typename T, typename U>
|
||||
inline T cast_unsigned(const U source)
|
||||
{
|
||||
using limit = std::numeric_limits<T>;
|
||||
static_assert(
|
||||
std::is_unsigned<T>::value && std::is_integral<T>::value,
|
||||
"target must be unsigned integer type"
|
||||
);
|
||||
static_assert(
|
||||
std::is_unsigned<U>::value && std::is_integral<U>::value,
|
||||
"source must be unsigned integer type"
|
||||
);
|
||||
if (limit::max() < source)
|
||||
throw_exception(source, limit::max());
|
||||
return static_cast<T>(source);
|
||||
}
|
||||
}
|
||||
|
||||
inline void read_bytes(reader& source, char& dest)
|
||||
//! read all current and future signed integer types
|
||||
template<typename T>
|
||||
inline enable_if<std::is_signed<T>::value && std::is_integral<T>::value>
|
||||
read_bytes(reader& source, T& dest)
|
||||
{
|
||||
dest = integer::convert_to<char>(source.integer());
|
||||
}
|
||||
inline void read_bytes(reader& source, short& dest)
|
||||
{
|
||||
dest = integer::convert_to<short>(source.integer());
|
||||
}
|
||||
inline void read_bytes(reader& source, int& dest)
|
||||
{
|
||||
dest = integer::convert_to<int>(source.integer());
|
||||
}
|
||||
inline void read_bytes(reader& source, long& dest)
|
||||
{
|
||||
dest = integer::convert_to<long>(source.integer());
|
||||
}
|
||||
inline void read_bytes(reader& source, long long& dest)
|
||||
{
|
||||
dest = integer::convert_to<long long>(source.integer());
|
||||
dest = integer::cast_signed<T>(source.integer());
|
||||
}
|
||||
|
||||
inline void read_bytes(reader& source, unsigned char& dest)
|
||||
//! read all current and future unsigned integer types
|
||||
template<typename T>
|
||||
inline enable_if<std::is_unsigned<T>::value && std::is_integral<T>::value>
|
||||
read_bytes(reader& source, T& dest)
|
||||
{
|
||||
dest = integer::convert_to<unsigned char>(source.unsigned_integer());
|
||||
}
|
||||
inline void read_bytes(reader& source, unsigned short& dest)
|
||||
{
|
||||
dest = integer::convert_to<unsigned short>(source.unsigned_integer());
|
||||
}
|
||||
inline void read_bytes(reader& source, unsigned& dest)
|
||||
{
|
||||
dest = integer::convert_to<unsigned>(source.unsigned_integer());
|
||||
}
|
||||
inline void read_bytes(reader& source, unsigned long& dest)
|
||||
{
|
||||
dest = integer::convert_to<unsigned long>(source.unsigned_integer());
|
||||
}
|
||||
inline void read_bytes(reader& source, unsigned long long& dest)
|
||||
{
|
||||
dest = integer::convert_to<unsigned long long>(source.unsigned_integer());
|
||||
dest = integer::cast_unsigned<T>(source.unsigned_integer());
|
||||
}
|
||||
} // wire
|
||||
|
||||
|
|
|
@ -47,6 +47,8 @@ namespace wire
|
|||
|
||||
virtual ~writer() noexcept;
|
||||
|
||||
virtual void boolean(bool) = 0;
|
||||
|
||||
virtual void integer(int) = 0;
|
||||
virtual void integer(std::intmax_t) = 0;
|
||||
|
||||
|
@ -78,6 +80,11 @@ namespace wire
|
|||
|
||||
// leave in header, compiler can de-virtualize when final type is given
|
||||
|
||||
inline void write_bytes(writer& dest, const bool source)
|
||||
{
|
||||
dest.boolean(source);
|
||||
}
|
||||
|
||||
inline void write_bytes(writer& dest, const int source)
|
||||
{
|
||||
dest.integer(source);
|
||||
|
|
29
tests/CMakeLists.txt
Normal file
29
tests/CMakeLists.txt
Normal file
|
@ -0,0 +1,29 @@
|
|||
# Copyright (c) 2022, The Monero Project
|
||||
#
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification, are
|
||||
# permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
# conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
# of conditions and the following disclaimer in the documentation and/or other
|
||||
# materials provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
# used to endorse or promote products derived from this software without specific
|
||||
# prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
add_subdirectory(unit)
|
37
tests/unit/CMakeLists.txt
Normal file
37
tests/unit/CMakeLists.txt
Normal file
|
@ -0,0 +1,37 @@
|
|||
# Copyright (c) 2022, The Monero Project
|
||||
#
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification, are
|
||||
# permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
# conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
# of conditions and the following disclaimer in the documentation and/or other
|
||||
# materials provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
# used to endorse or promote products derived from this software without specific
|
||||
# prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
add_library(monero-lws-unit-framework framework.test.cpp)
|
||||
target_include_directories(monero-lws-unit-framework PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/src")
|
||||
target_link_libraries(monero-lws-unit-framework)
|
||||
|
||||
add_subdirectory(wire)
|
||||
|
||||
add_executable(monero-lws-unit main.cpp)
|
||||
target_link_libraries(monero-lws-unit monero-lws-unit-framework monero-lws-unit-wire monero-lws-unit-wire-json)
|
||||
add_test(NAME monero-lws-unit COMMAND monero-lws-unit -v)
|
37
tests/unit/framework.test.cpp
Normal file
37
tests/unit/framework.test.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright (c) 2022, The Monero Project
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include "framework.test.h"
|
||||
|
||||
namespace lws_test
|
||||
{
|
||||
lest::tests& get_tests()
|
||||
{
|
||||
static lest::tests instance;
|
||||
return instance;
|
||||
}
|
||||
}
|
39
tests/unit/framework.test.h
Normal file
39
tests/unit/framework.test.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
// Copyright (c) 2022, The Monero Project
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#define lest_FEATURE_AUTO_REGISTER 1
|
||||
#include "lest.hpp"
|
||||
|
||||
#define LWS_CASE(name) \
|
||||
lest_CASE(lws_test::get_tests(), name)
|
||||
|
||||
namespace lws_test
|
||||
{
|
||||
lest::tests& get_tests();
|
||||
}
|
1484
tests/unit/lest.hpp
Normal file
1484
tests/unit/lest.hpp
Normal file
File diff suppressed because it is too large
Load diff
33
tests/unit/main.cpp
Normal file
33
tests/unit/main.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright (c) 2022, The Monero Project
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include "framework.test.h"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
return lest::run(lws_test::get_tests(), argc, argv);
|
||||
}
|
39
tests/unit/wire/CMakeLists.txt
Normal file
39
tests/unit/wire/CMakeLists.txt
Normal file
|
@ -0,0 +1,39 @@
|
|||
# Copyright (c) 2022, The Monero Project
|
||||
#
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification, are
|
||||
# permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
# conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
# of conditions and the following disclaimer in the documentation and/or other
|
||||
# materials provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
# used to endorse or promote products derived from this software without specific
|
||||
# prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
add_subdirectory(json)
|
||||
|
||||
add_library(monero-lws-unit-wire OBJECT read.write.test.cpp read.test.cpp)
|
||||
target_link_libraries(
|
||||
monero-lws-unit-wire
|
||||
monero-lws-unit-framework
|
||||
monero-lws-wire
|
||||
monero::libraries
|
||||
)
|
||||
#add_test(monero-lws-unit)
|
54
tests/unit/wire/base.test.h
Normal file
54
tests/unit/wire/base.test.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
// Copyright (c) 2022, The Monero Project
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
#include "wire/traits.h"
|
||||
|
||||
namespace lws_test
|
||||
{
|
||||
struct small_blob { std::uint8_t buf[4]; };
|
||||
constexpr const small_blob blob_test1{{0x00, 0xFF, 0x22, 0x11}};
|
||||
constexpr const small_blob blob_test2{{0x11, 0x7F, 0x7E, 0x80}};
|
||||
constexpr const small_blob blob_test3{{0xDE, 0xAD, 0xBE, 0xEF}};
|
||||
|
||||
inline bool operator==(const small_blob& lhs, const small_blob& rhs)
|
||||
{
|
||||
return std::memcmp(lhs.buf, rhs.buf, sizeof(lhs.buf)) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
namespace wire
|
||||
{
|
||||
template<>
|
||||
struct is_blob<lws_test::small_blob>
|
||||
: std::true_type
|
||||
{};
|
||||
}
|
37
tests/unit/wire/json/CMakeLists.txt
Normal file
37
tests/unit/wire/json/CMakeLists.txt
Normal file
|
@ -0,0 +1,37 @@
|
|||
# Copyright (c) 2022, The Monero Project
|
||||
#
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification, are
|
||||
# permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
# conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
# of conditions and the following disclaimer in the documentation and/or other
|
||||
# materials provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
# used to endorse or promote products derived from this software without specific
|
||||
# prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
add_library(monero-lws-unit-wire-json OBJECT read.write.test.cpp)
|
||||
target_link_libraries(
|
||||
monero-lws-unit-wire-json
|
||||
monero-lws-unit-framework
|
||||
monero-lws-wire-json
|
||||
monero::libraries
|
||||
)
|
||||
|
109
tests/unit/wire/json/read.write.test.cpp
Normal file
109
tests/unit/wire/json/read.write.test.cpp
Normal file
|
@ -0,0 +1,109 @@
|
|||
|
||||
#include "framework.test.h"
|
||||
|
||||
#include <boost/core/demangle.hpp>
|
||||
#include <boost/range/algorithm/equal.hpp>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include "wire/traits.h"
|
||||
#include "wire/json/base.h"
|
||||
#include "wire/json/read.h"
|
||||
#include "wire/json/write.h"
|
||||
#include "wire/vector.h"
|
||||
|
||||
#include "wire/base.test.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr const char basic_string[] = u8"my_string_data";
|
||||
constexpr const char basic_json[] =
|
||||
u8"{\"utf8\":\"my_string_data\",\"vec\":[0,127],\"data\":\"00ff2211\",\"choice\":true}";
|
||||
|
||||
template<typename T>
|
||||
struct basic_object
|
||||
{
|
||||
std::string utf8;
|
||||
std::vector<T> vec;
|
||||
lws_test::small_blob data;
|
||||
bool choice;
|
||||
};
|
||||
|
||||
template<typename F, typename T>
|
||||
void basic_object_map(F& format, T& self)
|
||||
{
|
||||
wire::object(format, WIRE_FIELD(utf8), WIRE_FIELD(vec), WIRE_FIELD(data), WIRE_FIELD(choice));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_bytes(wire::json_reader& source, basic_object<T>& dest)
|
||||
{ basic_object_map(source, dest); }
|
||||
|
||||
template<typename T>
|
||||
void write_bytes(wire::json_writer& dest, const basic_object<T>& source)
|
||||
{ basic_object_map(dest, source); }
|
||||
|
||||
template<typename T>
|
||||
void test_basic_reading(lest::env& lest_env)
|
||||
{
|
||||
SETUP("Basic values with " + boost::core::demangle(typeid(T).name()) + " integers")
|
||||
{
|
||||
const auto result =
|
||||
wire::json::from_bytes<basic_object<T>>(std::string{basic_json});
|
||||
EXPECT(result);
|
||||
EXPECT(result->utf8 == basic_string);
|
||||
{
|
||||
const std::vector<T> expected{0, 127};
|
||||
EXPECT(result->vec == expected);
|
||||
}
|
||||
EXPECT(result->data == lws_test::blob_test1);
|
||||
EXPECT(result->choice);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void test_basic_writing(lest::env& lest_env)
|
||||
{
|
||||
SETUP("Basic values with " + boost::core::demangle(typeid(T).name()) + " integers")
|
||||
{
|
||||
const basic_object<T> val{basic_string, std::vector<T>{0, 127}, lws_test::blob_test1, true};
|
||||
const auto result = wire::json::to_bytes(val);
|
||||
EXPECT(boost::range::equal(result, std::string{basic_json}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LWS_CASE("wire::json_reader")
|
||||
{
|
||||
using i64_limit = std::numeric_limits<std::int64_t>;
|
||||
static constexpr const char negative_number[] = "-1";
|
||||
|
||||
test_basic_reading<std::int16_t>(lest_env);
|
||||
test_basic_reading<std::int32_t>(lest_env);
|
||||
test_basic_reading<std::int64_t>(lest_env);
|
||||
test_basic_reading<std::intmax_t>(lest_env);
|
||||
test_basic_reading<std::uint16_t>(lest_env);
|
||||
test_basic_reading<std::uint32_t>(lest_env);
|
||||
test_basic_reading<std::uint64_t>(lest_env);
|
||||
test_basic_reading<std::uintmax_t>(lest_env);
|
||||
|
||||
static_assert(0 < i64_limit::max(), "expected 0 < int64_t::max");
|
||||
static_assert(
|
||||
i64_limit::max() <= std::numeric_limits<std::uintmax_t>::max(),
|
||||
"expected int64_t::max <= uintmax_t::max"
|
||||
);
|
||||
std::string big_number = std::to_string(std::uintmax_t(i64_limit::max()) + 1);
|
||||
EXPECT(wire::json::from_bytes<std::uint64_t>(negative_number) == wire::error::schema::larger_integer);
|
||||
EXPECT(wire::json::from_bytes<std::int64_t>(std::move(big_number)) == wire::error::schema::smaller_integer);
|
||||
}
|
||||
|
||||
LWS_CASE("wire::json_writer")
|
||||
{
|
||||
test_basic_writing<std::int16_t>(lest_env);
|
||||
test_basic_writing<std::int32_t>(lest_env);
|
||||
test_basic_writing<std::int64_t>(lest_env);
|
||||
test_basic_writing<std::intmax_t>(lest_env);
|
||||
test_basic_writing<std::uint16_t>(lest_env);
|
||||
test_basic_writing<std::uint32_t>(lest_env);
|
||||
test_basic_writing<std::uint64_t>(lest_env);
|
||||
test_basic_writing<std::uintmax_t>(lest_env);
|
||||
}
|
107
tests/unit/wire/read.test.cpp
Normal file
107
tests/unit/wire/read.test.cpp
Normal file
|
@ -0,0 +1,107 @@
|
|||
// Copyright (c) 2022, The Monero Project
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include "framework.test.h"
|
||||
|
||||
#include <boost/core/demangle.hpp>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include "wire/read.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
template<typename Target>
|
||||
void test_unsigned_to_unsigned(lest::env& lest_env)
|
||||
{
|
||||
using limit = std::numeric_limits<Target>;
|
||||
static constexpr const auto max =
|
||||
std::numeric_limits<std::uintmax_t>::max();
|
||||
static_assert(limit::is_integer, "expected integer");
|
||||
static_assert(!limit::is_signed, "expected unsigned");
|
||||
|
||||
SETUP("uintmax_t to " + boost::core::demangle(typeid(Target).name()))
|
||||
{
|
||||
EXPECT(Target(0) == wire::integer::cast_unsigned<Target>(std::uintmax_t(0)));
|
||||
EXPECT(limit::max() == wire::integer::cast_unsigned<Target>(std::uintmax_t(limit::max())));
|
||||
if (limit::max() < max)
|
||||
{
|
||||
EXPECT_THROWS_AS(wire::integer::cast_unsigned<Target>(std::uintmax_t(limit::max()) + 1), wire::exception);
|
||||
EXPECT_THROWS_AS(wire::integer::cast_unsigned<Target>(max), wire::exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Target>
|
||||
void test_signed_to_signed(lest::env& lest_env)
|
||||
{
|
||||
using limit = std::numeric_limits<Target>;
|
||||
static constexpr const auto min =
|
||||
std::numeric_limits<std::intmax_t>::min();
|
||||
static constexpr const auto max =
|
||||
std::numeric_limits<std::intmax_t>::max();
|
||||
static_assert(limit::is_integer, "expected integer");
|
||||
static_assert(limit::is_signed, "expected signed");
|
||||
|
||||
SETUP("intmax_t to " + boost::core::demangle(typeid(Target).name()))
|
||||
{
|
||||
if (min < limit::min())
|
||||
{
|
||||
EXPECT_THROWS_AS(wire::integer::cast_signed<Target>(std::intmax_t(limit::min()) - 1), wire::exception);
|
||||
EXPECT_THROWS_AS(wire::integer::cast_signed<Target>(min), wire::exception);
|
||||
}
|
||||
EXPECT(limit::min() == wire::integer::cast_signed<Target>(std::intmax_t(limit::min())));
|
||||
EXPECT(Target(0) == wire::integer::cast_signed<Target>(std::intmax_t(0)));
|
||||
EXPECT(limit::max() == wire::integer::cast_signed<Target>(std::intmax_t(limit::max())));
|
||||
if (limit::max() < max)
|
||||
{
|
||||
EXPECT_THROWS_AS(wire::integer::cast_signed<Target>(std::intmax_t(limit::max()) + 1), wire::exception);
|
||||
EXPECT_THROWS_AS(wire::integer::cast_signed<Target>(max), wire::exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LWS_CASE("wire::integer::cast_*")
|
||||
{
|
||||
SETUP("unsigned to unsigned")
|
||||
{
|
||||
test_unsigned_to_unsigned<std::uint8_t>(lest_env);
|
||||
test_unsigned_to_unsigned<std::uint16_t>(lest_env);
|
||||
test_unsigned_to_unsigned<std::uint32_t>(lest_env);
|
||||
test_unsigned_to_unsigned<std::uint64_t>(lest_env);
|
||||
test_unsigned_to_unsigned<std::uintmax_t>(lest_env);
|
||||
}
|
||||
SETUP("signed to signed")
|
||||
{
|
||||
test_signed_to_signed<std::int8_t>(lest_env);
|
||||
test_signed_to_signed<std::int16_t>(lest_env);
|
||||
test_signed_to_signed<std::int32_t>(lest_env);
|
||||
test_signed_to_signed<std::int64_t>(lest_env);
|
||||
test_signed_to_signed<std::intmax_t>(lest_env);
|
||||
}
|
||||
}
|
230
tests/unit/wire/read.write.test.cpp
Normal file
230
tests/unit/wire/read.write.test.cpp
Normal file
|
@ -0,0 +1,230 @@
|
|||
// Copyright (c) 2022, The Monero Project
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include "framework.test.h"
|
||||
|
||||
#include <boost/core/demangle.hpp>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "wire.h"
|
||||
#include "wire/json.h"
|
||||
#include "wire/vector.h"
|
||||
|
||||
#include "wire/base.test.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
template<typename T>
|
||||
using limit = std::numeric_limits<T>;
|
||||
|
||||
struct inner
|
||||
{
|
||||
std::uint32_t left;
|
||||
std::uint32_t right;
|
||||
};
|
||||
|
||||
template<typename F, typename T>
|
||||
void inner_map(F& format, T& self)
|
||||
{
|
||||
wire::object(format, WIRE_FIELD(left), WIRE_FIELD(right));
|
||||
}
|
||||
WIRE_DEFINE_OBJECT(inner, inner_map)
|
||||
|
||||
struct complex
|
||||
{
|
||||
std::vector<inner> objects;
|
||||
std::vector<std::int16_t> ints;
|
||||
std::vector<std::uint64_t> uints;
|
||||
std::vector<lws_test::small_blob> blobs;
|
||||
std::vector<std::string> strings;
|
||||
bool choice;
|
||||
};
|
||||
|
||||
template<typename F, typename T>
|
||||
void complex_map(F& format, T& self)
|
||||
{
|
||||
wire::object(format,
|
||||
WIRE_FIELD(objects),
|
||||
WIRE_FIELD(ints),
|
||||
WIRE_FIELD(uints),
|
||||
WIRE_FIELD(blobs),
|
||||
WIRE_FIELD(strings),
|
||||
WIRE_FIELD(choice)
|
||||
);
|
||||
}
|
||||
WIRE_DEFINE_OBJECT(complex, complex_map)
|
||||
|
||||
void verify_initial(lest::env& lest_env, const complex& self)
|
||||
{
|
||||
EXPECT(self.objects.empty());
|
||||
EXPECT(self.ints.empty());
|
||||
EXPECT(self.uints.empty());
|
||||
EXPECT(self.blobs.empty());
|
||||
EXPECT(self.strings.empty());
|
||||
EXPECT(self.choice == false);
|
||||
}
|
||||
|
||||
void fill(complex& self)
|
||||
{
|
||||
self.objects = std::vector<inner>{inner{0, limit<std::uint32_t>::max()}, inner{100, 200}, inner{44444, 83434}};
|
||||
self.ints = std::vector<std::int16_t>{limit<std::int16_t>::min(), limit<std::int16_t>::max(), 0, 31234};
|
||||
self.uints = std::vector<std::uint64_t>{0, limit<std::uint64_t>::max(), 34234234, 33};
|
||||
self.blobs = {lws_test::blob_test1, lws_test::blob_test2, lws_test::blob_test3};
|
||||
self.strings = {"string1", "string2", "string3", "string4"};
|
||||
self.choice = true;
|
||||
}
|
||||
|
||||
void verify_filled(lest::env& lest_env, const complex& self)
|
||||
{
|
||||
EXPECT(self.objects.size() == 3);
|
||||
EXPECT(self.objects.at(0).left == 0);
|
||||
EXPECT(self.objects.at(0).right == limit<std::uint32_t>::max());
|
||||
EXPECT(self.objects.at(1).left == 100);
|
||||
EXPECT(self.objects.at(1).right == 200);
|
||||
EXPECT(self.objects.at(2).left == 44444);
|
||||
EXPECT(self.objects.at(2).right == 83434);
|
||||
|
||||
EXPECT(self.ints.size() == 4);
|
||||
EXPECT(self.ints.at(0) == limit<std::int16_t>::min());
|
||||
EXPECT(self.ints.at(1) == limit<std::int16_t>::max());
|
||||
EXPECT(self.ints.at(2) == 0);
|
||||
EXPECT(self.ints.at(3) == 31234);
|
||||
|
||||
EXPECT(self.uints.size() == 4);
|
||||
EXPECT(self.uints.at(0) == 0);
|
||||
EXPECT(self.uints.at(1) == limit<std::uint64_t>::max());
|
||||
EXPECT(self.uints.at(2) == 34234234);
|
||||
EXPECT(self.uints.at(3) == 33);
|
||||
|
||||
EXPECT(self.blobs.size() == 3);
|
||||
EXPECT(self.blobs.at(0) == lws_test::blob_test1);
|
||||
EXPECT(self.blobs.at(1) == lws_test::blob_test2);
|
||||
EXPECT(self.blobs.at(2) == lws_test::blob_test3);
|
||||
|
||||
EXPECT(self.strings.size() == 4);
|
||||
EXPECT(self.strings.at(0) == "string1");
|
||||
EXPECT(self.strings.at(1) == "string2");
|
||||
EXPECT(self.strings.at(2) == "string3");
|
||||
EXPECT(self.strings.at(3) == "string4");
|
||||
|
||||
EXPECT(self.choice == true);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void run_complex(lest::env& lest_env)
|
||||
{
|
||||
SETUP("Complex test for " + boost::core::demangle(typeid(T).name()))
|
||||
{
|
||||
complex base{};
|
||||
verify_initial(lest_env, base);
|
||||
|
||||
{
|
||||
const expect<epee::byte_slice> bytes = T::to_bytes(base);
|
||||
EXPECT(bytes);
|
||||
|
||||
const expect<complex> derived = T::template from_bytes<complex>(std::string{bytes->begin(), bytes->end()});
|
||||
EXPECT(derived);
|
||||
verify_initial(lest_env, *derived);
|
||||
}
|
||||
|
||||
fill(base);
|
||||
|
||||
{
|
||||
const expect<epee::byte_slice> bytes = T::to_bytes(base);
|
||||
EXPECT(bytes);
|
||||
|
||||
const expect<complex> derived = T::template from_bytes<complex>(std::string{bytes->begin(), bytes->end()});
|
||||
EXPECT(derived);
|
||||
verify_filled(lest_env, *derived);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct big { std::int64_t value; };
|
||||
struct small { std::int32_t value; };
|
||||
|
||||
template<typename F, typename T>
|
||||
void big_map(F& format, T& self)
|
||||
{ wire::object(format, WIRE_FIELD(value)); }
|
||||
|
||||
template<typename F, typename T>
|
||||
void small_map(F& format, T& self)
|
||||
{ wire::object(format, WIRE_FIELD(value)); }
|
||||
|
||||
WIRE_DEFINE_OBJECT(big, big_map)
|
||||
WIRE_DEFINE_OBJECT(small, small_map)
|
||||
|
||||
template<typename T>
|
||||
expect<small> round_trip(lest::env& lest_env, std::int64_t value)
|
||||
{
|
||||
expect<small> out = small{0};
|
||||
SETUP("Testing round-trip with " + std::to_string(value))
|
||||
{
|
||||
const expect<epee::byte_slice> bytes = T::template to_bytes(big{value});
|
||||
EXPECT(bytes);
|
||||
out = T::template from_bytes<small>(std::string{bytes->begin(), bytes->end()});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void not_overflow(lest::env& lest_env, std::int64_t value)
|
||||
{
|
||||
const expect<small> result = round_trip<T>(lest_env, value);
|
||||
EXPECT(result);
|
||||
EXPECT(result->value == value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void overflow(lest::env& lest_env, std::int64_t value, const std::error_code error)
|
||||
{
|
||||
const expect<small> result = round_trip<T>(lest_env, value);
|
||||
EXPECT(result == error);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void run_overflow(lest::env& lest_env)
|
||||
{
|
||||
SETUP("Overflow test for " + boost::core::demangle(typeid(T).name()))
|
||||
{
|
||||
not_overflow<T>(lest_env, limit<std::int32_t>::min());
|
||||
not_overflow<T>(lest_env, 0);
|
||||
not_overflow<T>(lest_env, limit<std::int32_t>::max());
|
||||
|
||||
overflow<T>(lest_env, std::int64_t(limit<std::int32_t>::min()) - 1, wire::error::schema::larger_integer);
|
||||
overflow<T>(lest_env, std::int64_t(limit<std::int32_t>::max()) + 1, wire::error::schema::smaller_integer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LWS_CASE("wire::reader and wire::writer")
|
||||
{
|
||||
run_complex<wire::json>(lest_env);
|
||||
run_overflow<wire::json>(lest_env);
|
||||
}
|
Loading…
Reference in a new issue