mirror of
https://codeberg.org/wownero/wownero-lws
synced 2026-01-10 15:45: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
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);
|
||||
}
|
||||
Reference in New Issue
Block a user