forked from such-gitea/wownero-lws
Adding msgpack support to ::wire:: library (#63)
This commit is contained in:
committed by
Lee *!* Clagett
parent
64f5d4a9ab
commit
3ad71ba01e
@@ -33,5 +33,5 @@ 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)
|
||||
target_link_libraries(monero-lws-unit monero-lws-unit-framework monero-lws-unit-wire monero-lws-unit-wire-json monero-lws-unit-wire-msgpack)
|
||||
add_test(NAME monero-lws-unit COMMAND monero-lws-unit -v)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Copyright (c) 2022, The Monero Project
|
||||
# Copyright (c) 2022-2023, The Monero Project
|
||||
#
|
||||
# All rights reserved.
|
||||
#
|
||||
@@ -28,6 +28,7 @@
|
||||
|
||||
|
||||
add_subdirectory(json)
|
||||
add_subdirectory(msgpack)
|
||||
|
||||
add_library(monero-lws-unit-wire OBJECT read.write.test.cpp read.test.cpp)
|
||||
target_link_libraries(
|
||||
|
||||
37
tests/unit/wire/msgpack/CMakeLists.txt
Normal file
37
tests/unit/wire/msgpack/CMakeLists.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
# Copyright (c) 2023, 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-msgpack OBJECT read.write.test.cpp)
|
||||
target_link_libraries(
|
||||
monero-lws-unit-wire-msgpack
|
||||
monero-lws-unit-framework
|
||||
monero-lws-wire-msgpack
|
||||
monero::libraries
|
||||
)
|
||||
|
||||
183
tests/unit/wire/msgpack/read.write.test.cpp
Normal file
183
tests/unit/wire/msgpack/read.write.test.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
// Copyright (c) 2023, 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 <boost/range/algorithm/equal.hpp>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include "wire/traits.h"
|
||||
#include "wire/msgpack.h"
|
||||
#include "wire/vector.h"
|
||||
|
||||
#include "wire/base.test.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr const char basic_string[] = u8"my_string_data";
|
||||
//constexpr const char basic_[] =
|
||||
// u8"{\"utf8\":\"my_string_data\",\"vec\":[0,127],\"data\":\"00ff2211\",\"choice\":true}";
|
||||
constexpr const std::uint8_t basic_msgpack[] = {
|
||||
0x84, 0xa4, 0x75, 0x74, 0x66, 0x38, 0xae, 0x6d, 0x79, 0x5f, 0x73, 0x74,
|
||||
0x72, 0x69, 0x6e, 0x67, 0x5f, 0x64, 0x61, 0x74, 0x61, 0xa3, 0x76, 0x65,
|
||||
0x63, 0x92, 0x00, 0x7f, 0xa4, 0x64, 0x61, 0x74, 0x61, 0xc4, 0x04, 0x00,
|
||||
0xff, 0x22, 0x11, 0xa6, 0x63, 0x68, 0x6f, 0x69, 0x63, 0x65, 0xc3
|
||||
};
|
||||
constexpr const std::uint8_t advanced_msgpack[] = {
|
||||
0x84, 0x00, 0xae, 0x6d, 0x79, 0x5f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
|
||||
0x5f, 0x64, 0x61, 0x74, 0x61, 0x01, 0x92, 0x00, 0x7f, 0x02, 0xc4, 0x04,
|
||||
0x00, 0xff, 0x22, 0x11, 0xcc, 0xfe, 0xc3
|
||||
};
|
||||
|
||||
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_ID(0, utf8),
|
||||
WIRE_FIELD_ID(1, vec),
|
||||
WIRE_FIELD_ID(2, data),
|
||||
WIRE_FIELD_ID(254, choice)
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_bytes(wire::msgpack_reader& source, basic_object<T>& dest)
|
||||
{ basic_object_map(source, dest); }
|
||||
|
||||
template<typename T>
|
||||
void write_bytes(wire::msgpack_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 (string keys) with " + boost::core::demangle(typeid(T).name()) + " integers")
|
||||
{
|
||||
const auto result =
|
||||
wire::msgpack::from_bytes<basic_object<T>>(epee::byte_slice{{basic_msgpack}});
|
||||
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_advanced_reading(lest::env& lest_env)
|
||||
{
|
||||
SETUP("Advanced (integer keys) with " + boost::core::demangle(typeid(T).name()) + " integers")
|
||||
{
|
||||
const auto result =
|
||||
wire::msgpack::from_bytes<basic_object<T>>(epee::byte_slice{{advanced_msgpack}});
|
||||
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 (string keys) 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::msgpack::to_bytes(val);
|
||||
EXPECT(boost::range::equal(result, epee::byte_slice{{basic_msgpack}}));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void test_advanced_writing(lest::env& lest_env)
|
||||
{
|
||||
SETUP("Advanced (integer keys) 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_write::to_bytes(wire::msgpack_slice_writer{true}, val);
|
||||
EXPECT(boost::range::equal(result, epee::byte_slice{{advanced_msgpack}}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LWS_CASE("wire::msgpack_reader")
|
||||
{
|
||||
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);
|
||||
|
||||
test_advanced_reading<std::int16_t>(lest_env);
|
||||
test_advanced_reading<std::int32_t>(lest_env);
|
||||
test_advanced_reading<std::int64_t>(lest_env);
|
||||
test_advanced_reading<std::intmax_t>(lest_env);
|
||||
test_advanced_reading<std::uint16_t>(lest_env);
|
||||
test_advanced_reading<std::uint32_t>(lest_env);
|
||||
test_advanced_reading<std::uint64_t>(lest_env);
|
||||
test_advanced_reading<std::uintmax_t>(lest_env);
|
||||
}
|
||||
|
||||
LWS_CASE("wire::msgpack_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);
|
||||
|
||||
test_advanced_writing<std::int16_t>(lest_env);
|
||||
test_advanced_writing<std::int32_t>(lest_env);
|
||||
test_advanced_writing<std::int64_t>(lest_env);
|
||||
test_advanced_writing<std::intmax_t>(lest_env);
|
||||
test_advanced_writing<std::uint16_t>(lest_env);
|
||||
test_advanced_writing<std::uint32_t>(lest_env);
|
||||
test_advanced_writing<std::uint64_t>(lest_env);
|
||||
test_advanced_writing<std::uintmax_t>(lest_env);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2022, The Monero Project
|
||||
// Copyright (c) 2022-2023, The Monero Project
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
@@ -34,6 +34,7 @@
|
||||
#include <vector>
|
||||
#include "wire.h"
|
||||
#include "wire/json.h"
|
||||
#include "wire/msgpack.h"
|
||||
#include "wire/vector.h"
|
||||
|
||||
#include "wire/base.test.h"
|
||||
@@ -93,7 +94,7 @@ namespace
|
||||
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.ints = std::vector<std::int16_t>{limit<std::int16_t>::min(), limit<std::int16_t>::max(), -3, 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"};
|
||||
@@ -113,7 +114,7 @@ namespace
|
||||
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(2) == -3);
|
||||
EXPECT(self.ints.at(3) == 31234);
|
||||
|
||||
EXPECT(self.uints.size() == 4);
|
||||
@@ -136,7 +137,7 @@ namespace
|
||||
EXPECT(self.choice == true);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename T, typename U>
|
||||
void run_complex(lest::env& lest_env)
|
||||
{
|
||||
SETUP("Complex test for " + boost::core::demangle(typeid(T).name()))
|
||||
@@ -148,7 +149,7 @@ namespace
|
||||
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()});
|
||||
const expect<complex> derived = T::template from_bytes<complex>(U{std::string{bytes->begin(), bytes->end()}});
|
||||
EXPECT(derived);
|
||||
verify_initial(lest_env, *derived);
|
||||
}
|
||||
@@ -159,7 +160,7 @@ namespace
|
||||
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()});
|
||||
const expect<complex> derived = T::template from_bytes<complex>(U{std::string{bytes->begin(), bytes->end()}});
|
||||
EXPECT(derived);
|
||||
verify_filled(lest_env, *derived);
|
||||
}
|
||||
@@ -180,7 +181,7 @@ namespace
|
||||
WIRE_DEFINE_OBJECT(big, big_map)
|
||||
WIRE_DEFINE_OBJECT(small, small_map)
|
||||
|
||||
template<typename T>
|
||||
template<typename T, typename U>
|
||||
expect<small> round_trip(lest::env& lest_env, std::int64_t value)
|
||||
{
|
||||
expect<small> out = small{0};
|
||||
@@ -188,43 +189,77 @@ namespace
|
||||
{
|
||||
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()});
|
||||
out = T::template from_bytes<small>(U{std::string{bytes->begin(), bytes->end()}});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename T, typename U>
|
||||
void not_overflow(lest::env& lest_env, std::int64_t value)
|
||||
{
|
||||
const expect<small> result = round_trip<T>(lest_env, value);
|
||||
const expect<small> result = round_trip<T, U>(lest_env, value);
|
||||
EXPECT(result);
|
||||
EXPECT(result->value == value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename T, typename U>
|
||||
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);
|
||||
const expect<small> result = round_trip<T, U>(lest_env, value);
|
||||
EXPECT(result == error);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename T, typename U>
|
||||
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());
|
||||
not_overflow<T, U>(lest_env, limit<std::int32_t>::min());
|
||||
not_overflow<T, U>(lest_env, 0);
|
||||
not_overflow<T, U>(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);
|
||||
overflow<T, U>(lest_env, std::int64_t(limit<std::int32_t>::min()) - 1, wire::error::schema::larger_integer);
|
||||
overflow<T, U>(lest_env, std::int64_t(limit<std::int32_t>::max()) + 1, wire::error::schema::smaller_integer);
|
||||
}
|
||||
}
|
||||
|
||||
struct simple { bool choice; };
|
||||
static void read_bytes(wire::reader& source, simple& self)
|
||||
{
|
||||
wire::object(source, WIRE_FIELD(choice));
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
void run_skip(lest::env& lest_env)
|
||||
{
|
||||
complex base{};
|
||||
verify_initial(lest_env, base);
|
||||
fill(base);
|
||||
|
||||
const expect<epee::byte_slice> bytes = T::to_bytes(base);
|
||||
EXPECT(bytes);
|
||||
|
||||
const expect<simple> derived = T::template from_bytes<simple>(U{std::string{bytes->begin(), bytes->end()}});
|
||||
EXPECT(derived);
|
||||
EXPECT(derived->choice);
|
||||
}
|
||||
}
|
||||
|
||||
LWS_CASE("wire::reader and wire::writer")
|
||||
LWS_CASE("wire::reader and wire::writer complex")
|
||||
{
|
||||
run_complex<wire::json>(lest_env);
|
||||
run_overflow<wire::json>(lest_env);
|
||||
run_complex<wire::json, std::string>(lest_env);
|
||||
run_complex<wire::msgpack, epee::byte_slice>(lest_env);
|
||||
}
|
||||
|
||||
LWS_CASE("wire::reader and wire::writer overflow")
|
||||
{
|
||||
run_overflow<wire::json, std::string>(lest_env);
|
||||
run_overflow<wire::msgpack, epee::byte_slice>(lest_env);
|
||||
}
|
||||
|
||||
LWS_CASE("wire::reader and wire::writer skip")
|
||||
{
|
||||
run_skip<wire::json, std::string>(lest_env);
|
||||
run_skip<wire::msgpack, epee::byte_slice>(lest_env);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user