mirror of
https://codeberg.org/wownero/wownero-lws
synced 2026-01-09 15:15:15 -08: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:
committed by
Lee *!* Clagett
parent
d233c72b5e
commit
c958ac7963
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);
|
||||
}
|
||||
Reference in New Issue
Block a user