Compare commits
No commits in common. "main" and "msgpack_packer" have entirely different histories.
main
...
msgpack_pa
@ -18,7 +18,7 @@ BreakConstructorInitializers: BeforeComma
|
|||||||
BreakInheritanceList: BeforeComma
|
BreakInheritanceList: BeforeComma
|
||||||
ConstructorInitializerIndentWidth: 8
|
ConstructorInitializerIndentWidth: 8
|
||||||
ContinuationIndentWidth: 8
|
ContinuationIndentWidth: 8
|
||||||
#IncludeBlocks: Regroup
|
IncludeBlocks: Regroup
|
||||||
IncludeCategories:
|
IncludeCategories:
|
||||||
- Regex: '^"parselink/'
|
- Regex: '^"parselink/'
|
||||||
Priority: 1
|
Priority: 1
|
||||||
|
|||||||
@ -1,65 +0,0 @@
|
|||||||
|
|
||||||
cc_library(
|
|
||||||
name = "headers",
|
|
||||||
includes = ["include"],
|
|
||||||
visibility = ["//visibility:private"],
|
|
||||||
copts = ["-DRC_DONT_USE_RTTI"],
|
|
||||||
hdrs = glob([
|
|
||||||
"include/**/*.hpp",
|
|
||||||
"include/**/*.h",
|
|
||||||
]),
|
|
||||||
)
|
|
||||||
|
|
||||||
cc_library(
|
|
||||||
name = "random",
|
|
||||||
deps = [":headers"],
|
|
||||||
visibility = ["//visibility:private"],
|
|
||||||
copts = ["-DRC_DONT_USE_RTTI", "-O3"],
|
|
||||||
srcs = [
|
|
||||||
"src/Random.cpp",
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
cc_library(
|
|
||||||
name = "rapidcheck",
|
|
||||||
visibility = ["//visibility:public"],
|
|
||||||
deps = [":headers", ":random"],
|
|
||||||
copts = ["-DRC_DONT_USE_RTTI"],
|
|
||||||
includes = ["include"],
|
|
||||||
srcs = [
|
|
||||||
"src/BeforeMinimalTestCase.cpp",
|
|
||||||
"src/Check.cpp",
|
|
||||||
"src/Classify.cpp",
|
|
||||||
"src/GenerationFailure.cpp",
|
|
||||||
"src/Log.cpp",
|
|
||||||
"src/Show.cpp",
|
|
||||||
"src/detail/Any.cpp",
|
|
||||||
"src/detail/Assertions.cpp",
|
|
||||||
"src/detail/Base64.cpp",
|
|
||||||
"src/detail/Configuration.cpp",
|
|
||||||
"src/detail/DefaultTestListener.cpp",
|
|
||||||
"src/detail/FrequencyMap.cpp",
|
|
||||||
"src/detail/ImplicitParam.cpp",
|
|
||||||
"src/detail/LogTestListener.cpp",
|
|
||||||
"src/detail/MapParser.cpp",
|
|
||||||
"src/detail/MulticastTestListener.cpp",
|
|
||||||
"src/detail/ParseException.cpp",
|
|
||||||
"src/detail/Platform.cpp",
|
|
||||||
"src/detail/Property.cpp",
|
|
||||||
"src/detail/PropertyContext.cpp",
|
|
||||||
"src/detail/ReproduceListener.cpp",
|
|
||||||
"src/detail/Results.cpp",
|
|
||||||
"src/detail/Serialization.cpp",
|
|
||||||
"src/detail/StringSerialization.cpp",
|
|
||||||
"src/detail/TestMetadata.cpp",
|
|
||||||
"src/detail/TestParams.cpp",
|
|
||||||
"src/detail/Testing.cpp",
|
|
||||||
"src/gen/Numeric.cpp",
|
|
||||||
"src/gen/Text.cpp",
|
|
||||||
"src/gen/detail/ExecHandler.cpp",
|
|
||||||
"src/gen/detail/GenerationHandler.cpp",
|
|
||||||
"src/gen/detail/Recipe.cpp",
|
|
||||||
"src/gen/detail/ScaleInteger.cpp",
|
|
||||||
] + glob(["src/detail/*.h"]),
|
|
||||||
)
|
|
||||||
22
WORKSPACE
22
WORKSPACE
@ -25,7 +25,7 @@ boost_deps()
|
|||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
# magic_enum: Used in logging implementation for enum names.
|
# magic_enum: Used in logging implementation for enum names.
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
magic_enum_version = "0.9.5"
|
magic_enum_version = "0.8.2"
|
||||||
magic_enum_base_url = \
|
magic_enum_base_url = \
|
||||||
"https://github.com/Neargye/magic_enum/archive/refs/tags/v"
|
"https://github.com/Neargye/magic_enum/archive/refs/tags/v"
|
||||||
magic_enum_sha256 = \
|
magic_enum_sha256 = \
|
||||||
@ -33,7 +33,7 @@ magic_enum_sha256 = \
|
|||||||
|
|
||||||
http_archive(
|
http_archive(
|
||||||
name = "magic_enum",
|
name = "magic_enum",
|
||||||
# sha256 = magic_enum_sha256,
|
sha256 = magic_enum_sha256,
|
||||||
url = magic_enum_base_url + magic_enum_version + ".zip",
|
url = magic_enum_base_url + magic_enum_version + ".zip",
|
||||||
strip_prefix = "magic_enum-" + magic_enum_version,
|
strip_prefix = "magic_enum-" + magic_enum_version,
|
||||||
)
|
)
|
||||||
@ -101,7 +101,7 @@ cc_library(
|
|||||||
)
|
)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
# ut: Testing framework.
|
# ut: Unit test framework.
|
||||||
# TODO(kss): Only if tests are needed?
|
# TODO(kss): Only if tests are needed?
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
ut_version = "1.1.9"
|
ut_version = "1.1.9"
|
||||||
@ -124,22 +124,6 @@ cc_library(
|
|||||||
strip_prefix = "ut-" + ut_version,
|
strip_prefix = "ut-" + ut_version,
|
||||||
)
|
)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
|
||||||
# rapidcheck: Property-based-testing framework.
|
|
||||||
# TODO(kss): Only if tests are needed?
|
|
||||||
#-------------------------------------------------------------------------------
|
|
||||||
rapidcheck_commit = "ff6af6fc683159deb51c543b065eba14dfcf329b"
|
|
||||||
rapidcheck_base_url = "https://github.com/emil-e/rapidcheck"
|
|
||||||
rapidcheck_branch = "master"
|
|
||||||
|
|
||||||
git_repository(
|
|
||||||
name = "rapidcheck",
|
|
||||||
remote = rapidcheck_base_url,
|
|
||||||
commit = rapidcheck_commit,
|
|
||||||
build_file = "@//:BUILD.rapidcheck"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
# Support compile_commands.json generation for LSP.
|
# Support compile_commands.json generation for LSP.
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
|
|||||||
@ -118,6 +118,7 @@ struct fmt::formatter<T> : fmt::formatter<void const*> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO(ksassenrath): Re-enable when expected has been integrated
|
||||||
template <typename T, typename Err>
|
template <typename T, typename Err>
|
||||||
struct fmt::formatter<tl::expected<T, Err>> {
|
struct fmt::formatter<tl::expected<T, Err>> {
|
||||||
template <typename ParseContext>
|
template <typename ParseContext>
|
||||||
|
|||||||
@ -94,7 +94,9 @@ struct builtin_packer<bool> : builtin_packer_base {
|
|||||||
template <std::unsigned_integral T>
|
template <std::unsigned_integral T>
|
||||||
struct builtin_packer<T> : builtin_packer_base {
|
struct builtin_packer<T> : builtin_packer_base {
|
||||||
static constexpr std::size_t rep_size(std::uint64_t value) noexcept {
|
static constexpr std::size_t rep_size(std::uint64_t value) noexcept {
|
||||||
if (value < 128) return 0;
|
constexpr auto fixint_size =
|
||||||
|
static_cast<std::uint64_t>(format::positive_fixint::mask);
|
||||||
|
if (value <= fixint_size) return 0;
|
||||||
auto bytes_needed =
|
auto bytes_needed =
|
||||||
static_cast<std::uint64_t>((std::bit_width(value) + 7) >> 3);
|
static_cast<std::uint64_t>((std::bit_width(value) + 7) >> 3);
|
||||||
return std::bit_ceil(bytes_needed);
|
return std::bit_ceil(bytes_needed);
|
||||||
@ -119,8 +121,8 @@ struct builtin_packer<T> : builtin_packer_base {
|
|||||||
template <std::signed_integral T>
|
template <std::signed_integral T>
|
||||||
struct builtin_packer<T> : builtin_packer_base {
|
struct builtin_packer<T> : builtin_packer_base {
|
||||||
static constexpr std::size_t rep_size(std::int64_t value) noexcept {
|
static constexpr std::size_t rep_size(std::int64_t value) noexcept {
|
||||||
// Probably a better way to do this; fixints
|
// Probably a better way to do this.
|
||||||
if (value < 128 && value >= -32) return 0;
|
if (value < 0 && value >= -32) return 0;
|
||||||
auto underlying = static_cast<std::uint64_t>(value);
|
auto underlying = static_cast<std::uint64_t>(value);
|
||||||
|
|
||||||
// save a branch; these should be cheap on modern hardware.
|
// save a branch; these should be cheap on modern hardware.
|
||||||
|
|||||||
@ -1,347 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Support for unpacking various types.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#ifndef msgpack_core_detail_builtin_unpackable_types_bf5de632f088a7d8
|
|
||||||
#define msgpack_core_detail_builtin_unpackable_types_bf5de632f088a7d8
|
|
||||||
|
|
||||||
#include "parselink/msgpack/core/detail/unpackable_concepts.h"
|
|
||||||
#include "parselink/msgpack/core/error.h"
|
|
||||||
#include "parselink/msgpack/core/format.h"
|
|
||||||
#include "parselink/msgpack/util/endianness.h"
|
|
||||||
|
|
||||||
#include <tl/expected.hpp>
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <limits>
|
|
||||||
#include <ranges>
|
|
||||||
|
|
||||||
namespace msgpack {
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
// Generic mechanism for reading a number of bytes out of an interator.
|
|
||||||
template <std::size_t N, typename Itr>
|
|
||||||
constexpr inline decltype(auto) read_bytes(Itr& inp) noexcept {
|
|
||||||
std::array<std::byte, N> out;
|
|
||||||
auto const end = inp + N;
|
|
||||||
auto dst = out.begin();
|
|
||||||
while (inp != end) {
|
|
||||||
*dst = std::byte{*inp};
|
|
||||||
++dst;
|
|
||||||
++inp;
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is a generic helper function for writing integral bytes
|
|
||||||
template <std::integral T, typename Itr>
|
|
||||||
constexpr decltype(auto) unpack_integral(Itr& inp) noexcept {
|
|
||||||
constexpr auto N = sizeof(T);
|
|
||||||
if constexpr (sizeof(T) == 1) {
|
|
||||||
return T(*inp++);
|
|
||||||
} else {
|
|
||||||
return be_to_host(::detail::value_cast<T>(read_bytes<N>(inp)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Defines a range of bytes for binary formats.
|
|
||||||
template <typename T>
|
|
||||||
concept unpackable_byte_range =
|
|
||||||
std::ranges::contiguous_range<T>
|
|
||||||
&& std::same_as<std::ranges::range_value_t<T>, std::byte>;
|
|
||||||
|
|
||||||
template <format_type F, typename Iter>
|
|
||||||
constexpr inline bool accept_format(Iter& itr) noexcept {
|
|
||||||
if constexpr (is_fixtype<F>) {
|
|
||||||
// Don't advance the iterator -- part of the format is locked away
|
|
||||||
// within the format byte.
|
|
||||||
return (std::byte{*itr} & ~F::mask) == F::marker;
|
|
||||||
} else {
|
|
||||||
// Advance the iterator past the format byte.
|
|
||||||
return std::byte{*itr++} == F::marker;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This helper structure provides a customization point for converting an
|
|
||||||
// iterator to some other type (e.g. a pointer) in the event that the
|
|
||||||
// final format does not have a suitable constructor. It's only currently
|
|
||||||
// used for string_view, but may be overridden on platforms that provide
|
|
||||||
// their own types.
|
|
||||||
template <typename T, typename Iter>
|
|
||||||
struct iterator_adapter {
|
|
||||||
static constexpr auto convert(Iter itr) { return itr; }
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Iter>
|
|
||||||
struct iterator_adapter<std::string_view, Iter> {
|
|
||||||
static constexpr auto convert(Iter itr) {
|
|
||||||
return ::detail::value_cast<std::string_view::pointer>(&*itr);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Using information expressed within each format type, this singular template
|
|
||||||
// can instantiate the unpacking of most formats. Notable exceptions are
|
|
||||||
// the array/map types and extension types.
|
|
||||||
template <format_type F>
|
|
||||||
constexpr inline auto unpack_format(auto& unpacker) noexcept
|
|
||||||
-> tl::expected<typename F::value_type, error> {
|
|
||||||
if (!accept_format<F>(unpacker.in)) {
|
|
||||||
return tl::make_unexpected(error::wrong_type);
|
|
||||||
}
|
|
||||||
|
|
||||||
// First thing's first. Read the format's "first_type", which is either
|
|
||||||
// the payload or the length of the payload. Ensure we have enough data
|
|
||||||
// from the span before we do so, though.
|
|
||||||
using first_type = typename F::first_type;
|
|
||||||
using value_type = typename F::value_type;
|
|
||||||
using diff_type = decltype(unpacker.remaining());
|
|
||||||
if (unpacker.remaining() < diff_type{sizeof(first_type)}) {
|
|
||||||
return tl::make_unexpected(error::incomplete_message);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto f = unpack_integral<first_type>(unpacker.in);
|
|
||||||
if constexpr (is_fixtype<F> && (F::flags & format::flag::apply_mask)) {
|
|
||||||
f &= decltype(f)(F::mask);
|
|
||||||
}
|
|
||||||
|
|
||||||
if constexpr (F::payload_type == format::payload::fixed) {
|
|
||||||
// Prefer constructions that utilize the unpacked value, but allow
|
|
||||||
// tag types (e.g. invalid, nil) to be directly constructed.
|
|
||||||
if constexpr (std::constructible_from<value_type, decltype(f)>) {
|
|
||||||
return value_type(f);
|
|
||||||
} else {
|
|
||||||
return value_type{};
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// We're reading a variable length payload. `f` is the length of the
|
|
||||||
// payload. Ensure that the span has enough data and construct the
|
|
||||||
// value_type accordingly.
|
|
||||||
if (unpacker.remaining() < diff_type(f)) {
|
|
||||||
return tl::make_unexpected(error::incomplete_message);
|
|
||||||
}
|
|
||||||
|
|
||||||
using adapt = iterator_adapter<value_type, decltype(unpacker.in)>;
|
|
||||||
auto value = value_type(adapt::convert(unpacker.in), f);
|
|
||||||
unpacker.in += f;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template <typename>
|
|
||||||
struct builtin_unpacker;
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Built-in unpacker specializations
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
template <>
|
|
||||||
struct builtin_unpacker<invalid> {
|
|
||||||
static constexpr auto unpack(auto& unpacker) noexcept {
|
|
||||||
return detail::unpack_format<format::invalid>(unpacker);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct builtin_unpacker<nil> {
|
|
||||||
static constexpr auto unpack(auto& unpacker) noexcept {
|
|
||||||
return detail::unpack_format<format::nil>(unpacker);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct builtin_unpacker<bool> {
|
|
||||||
static constexpr auto unpack(auto& unpacker) noexcept {
|
|
||||||
return detail::unpack_format<format::boolean>(unpacker);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct builtin_unpacker<map_desc> {
|
|
||||||
static constexpr auto unpack(auto& unpacker) noexcept
|
|
||||||
-> tl::expected<map_desc, msgpack::error> {
|
|
||||||
auto marker = *(unpacker.in);
|
|
||||||
if ((marker & ~format::fixmap::mask) == format::fixmap::marker) {
|
|
||||||
return detail::unpack_format<format::fixmap>(unpacker);
|
|
||||||
}
|
|
||||||
switch (marker) {
|
|
||||||
case format::map16::marker:
|
|
||||||
return detail::unpack_format<format::map16>(unpacker);
|
|
||||||
case format::map32::marker:
|
|
||||||
return detail::unpack_format<format::map32>(unpacker);
|
|
||||||
}
|
|
||||||
return tl::make_unexpected(msgpack::error::wrong_type);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// This unpacker will accept any uintX, positive_fixint types.
|
|
||||||
struct unsigned_int_unpacker {
|
|
||||||
static constexpr auto unpack(auto& unpacker) noexcept
|
|
||||||
-> tl::expected<std::uint64_t, msgpack::error> {
|
|
||||||
auto marker = *(unpacker.in);
|
|
||||||
if ((marker & std::byte{0x80}) == std::byte{}) {
|
|
||||||
return detail::unpack_format<format::positive_fixint>(unpacker);
|
|
||||||
}
|
|
||||||
switch (marker) {
|
|
||||||
case format::uint8::marker:
|
|
||||||
return detail::unpack_format<format::uint8>(unpacker);
|
|
||||||
case format::uint16::marker:
|
|
||||||
return detail::unpack_format<format::uint16>(unpacker);
|
|
||||||
case format::uint32::marker:
|
|
||||||
return detail::unpack_format<format::uint32>(unpacker);
|
|
||||||
case format::uint64::marker:
|
|
||||||
return detail::unpack_format<format::uint64>(unpacker);
|
|
||||||
}
|
|
||||||
return tl::make_unexpected(msgpack::error::wrong_type);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <std::unsigned_integral T>
|
|
||||||
struct builtin_unpacker<T> {
|
|
||||||
static constexpr auto max = std::numeric_limits<T>::max();
|
|
||||||
|
|
||||||
static constexpr auto unpack(auto& unpacker) noexcept {
|
|
||||||
constexpr auto convert =
|
|
||||||
[](auto value) -> tl::expected<T, msgpack::error> {
|
|
||||||
if (value > max) {
|
|
||||||
return tl::make_unexpected(error::will_truncate);
|
|
||||||
}
|
|
||||||
return static_cast<T>(value);
|
|
||||||
};
|
|
||||||
return unsigned_int_unpacker::unpack(unpacker).and_then(convert);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct signed_int_unpacker {
|
|
||||||
static constexpr auto unpack(auto& unpacker) noexcept
|
|
||||||
-> tl::expected<std::int64_t, msgpack::error> {
|
|
||||||
auto marker = *(unpacker.in);
|
|
||||||
if ((marker & std::byte{0x80}) == std::byte{}) {
|
|
||||||
return detail::unpack_format<format::positive_fixint>(unpacker);
|
|
||||||
} else if ((marker & std::byte{0xe0}) == std::byte{0xe0}) {
|
|
||||||
return detail::unpack_format<format::negative_fixint>(unpacker);
|
|
||||||
}
|
|
||||||
switch (marker) {
|
|
||||||
case format::int8::marker:
|
|
||||||
return detail::unpack_format<format::int8>(unpacker);
|
|
||||||
case format::int16::marker:
|
|
||||||
return detail::unpack_format<format::int16>(unpacker);
|
|
||||||
case format::int32::marker:
|
|
||||||
return detail::unpack_format<format::int32>(unpacker);
|
|
||||||
case format::int64::marker:
|
|
||||||
return detail::unpack_format<format::int64>(unpacker);
|
|
||||||
}
|
|
||||||
return tl::make_unexpected(msgpack::error::wrong_type);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <std::signed_integral T>
|
|
||||||
struct builtin_unpacker<T> {
|
|
||||||
static constexpr auto max = std::numeric_limits<T>::max();
|
|
||||||
static constexpr auto min = std::numeric_limits<T>::min();
|
|
||||||
|
|
||||||
static constexpr auto unpack(auto& unpacker) noexcept {
|
|
||||||
constexpr auto convert =
|
|
||||||
[](auto value) -> tl::expected<T, msgpack::error> {
|
|
||||||
if (value > max || value < min) {
|
|
||||||
return tl::make_unexpected(error::will_truncate);
|
|
||||||
}
|
|
||||||
return static_cast<T>(value);
|
|
||||||
};
|
|
||||||
return signed_int_unpacker::unpack(unpacker).and_then(convert);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct builtin_unpacker<std::string_view> {
|
|
||||||
static constexpr auto unpack(auto& unpacker) noexcept
|
|
||||||
-> tl::expected<std::string_view, msgpack::error> {
|
|
||||||
auto marker = *(unpacker.in);
|
|
||||||
if ((marker & ~format::fixstr::mask) == format::fixstr::marker) {
|
|
||||||
return detail::unpack_format<format::fixstr>(unpacker);
|
|
||||||
}
|
|
||||||
switch (marker) {
|
|
||||||
case format::str8::marker:
|
|
||||||
return detail::unpack_format<format::str8>(unpacker);
|
|
||||||
case format::str16::marker:
|
|
||||||
return detail::unpack_format<format::str16>(unpacker);
|
|
||||||
case format::str32::marker:
|
|
||||||
return detail::unpack_format<format::str32>(unpacker);
|
|
||||||
}
|
|
||||||
return tl::make_unexpected(msgpack::error::wrong_type);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, std::size_t Extent>
|
|
||||||
requires(std::same_as<std::byte, std::remove_const_t<T>>)
|
|
||||||
struct builtin_unpacker<std::span<T, Extent>> {
|
|
||||||
template <std::size_t E = Extent>
|
|
||||||
using expected_type = tl::expected<std::span<T, E>, msgpack::error>;
|
|
||||||
|
|
||||||
static constexpr auto unpack_bytes(auto& unpacker) noexcept
|
|
||||||
-> expected_type<std::dynamic_extent> {
|
|
||||||
auto marker = *(unpacker.in);
|
|
||||||
switch (marker) {
|
|
||||||
case format::bin8::marker:
|
|
||||||
return detail::unpack_format<format::bin8>(unpacker);
|
|
||||||
case format::bin16::marker:
|
|
||||||
return detail::unpack_format<format::bin16>(unpacker);
|
|
||||||
case format::bin32::marker:
|
|
||||||
return detail::unpack_format<format::bin32>(unpacker);
|
|
||||||
}
|
|
||||||
return tl::make_unexpected(msgpack::error::wrong_type);
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr expected_type<> unpack(auto& unpacker) noexcept {
|
|
||||||
auto ensure_extent = [](std::span<std::byte const> value) noexcept
|
|
||||||
-> expected_type<> {
|
|
||||||
if constexpr (Extent != std::dynamic_extent) {
|
|
||||||
if (value.size() != Extent) {
|
|
||||||
return tl::make_unexpected(msgpack::error::wrong_length);
|
|
||||||
}
|
|
||||||
return value.template first<Extent>();
|
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return unpack_bytes(unpacker).and_then(ensure_extent);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, std::size_t Extent>
|
|
||||||
requires(std::same_as<std::byte, std::remove_const_t<T>> && Extent > 0)
|
|
||||||
struct builtin_unpacker<std::array<T, Extent>> {
|
|
||||||
using expected_type = tl::expected<std::array<T, Extent>, msgpack::error>;
|
|
||||||
|
|
||||||
static constexpr expected_type unpack(auto& unpacker) noexcept {
|
|
||||||
constexpr auto copy_to_array = [](auto span) noexcept -> expected_type {
|
|
||||||
if (span.size() != Extent) {
|
|
||||||
return tl::make_unexpected(msgpack::error::wrong_length);
|
|
||||||
} else {
|
|
||||||
std::array<std::byte, Extent> dest;
|
|
||||||
std::copy(span.begin(), span.end(), dest.begin());
|
|
||||||
return dest;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return builtin_unpacker<std::span<std::byte const>>::unpack(unpacker)
|
|
||||||
.and_then(copy_to_array);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace msgpack
|
|
||||||
|
|
||||||
#endif // msgpack_core_detail_builtin_unpackable_types_bf5de632f088a7d8
|
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// ___ __ _ _
|
// ___ __ _ _
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
||||||
|
|||||||
@ -21,9 +21,8 @@
|
|||||||
#ifndef msgpack_core_detail_packable_ranges_10d28c24498828c7
|
#ifndef msgpack_core_detail_packable_ranges_10d28c24498828c7
|
||||||
#define msgpack_core_detail_packable_ranges_10d28c24498828c7
|
#define msgpack_core_detail_packable_ranges_10d28c24498828c7
|
||||||
|
|
||||||
#include "parselink/msgpack/core/detail/builtin_packable_types.h"
|
|
||||||
#include "parselink/msgpack/core/detail/packable_concepts.h"
|
#include "parselink/msgpack/core/detail/packable_concepts.h"
|
||||||
|
#include "parselink/msgpack/core/detail/builtin_packable_types.h"
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
@ -32,8 +31,7 @@ namespace detail {
|
|||||||
|
|
||||||
//
|
//
|
||||||
template <typename T>
|
template <typename T>
|
||||||
concept packable_array =
|
concept packable_array = std::ranges::input_range<T>
|
||||||
std::ranges::input_range<T> && !std::convertible_to<T, std::string_view>
|
|
||||||
&& packable<std::ranges::range_value_t<T>>;
|
&& packable<std::ranges::range_value_t<T>>;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
|||||||
@ -1,92 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Concepts used for types that are deserializable by an unpacker.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#ifndef msgpack_core_detail_unpackable_concepts_cf142e37b34a598f
|
|
||||||
#define msgpack_core_detail_unpackable_concepts_cf142e37b34a598f
|
|
||||||
|
|
||||||
#include "parselink/msgpack/core/error.h"
|
|
||||||
|
|
||||||
#include <tl/expected.hpp>
|
|
||||||
|
|
||||||
#include <concepts>
|
|
||||||
#include <cstddef>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
namespace msgpack {
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
// This structure models a minimal packer context. It is used to validate
|
|
||||||
// whether a particular type is considered packable or not.
|
|
||||||
struct unpacker_context_model {
|
|
||||||
std::byte* in;
|
|
||||||
|
|
||||||
std::size_t remaining() { return 1; }
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
// Datatypes with built-in support will specialize builtin_unpacker.
|
|
||||||
template <typename>
|
|
||||||
struct builtin_unpacker;
|
|
||||||
|
|
||||||
// Datatypes can have a custom adapter for unpacking. This _can_ be used to
|
|
||||||
// override default behavior if necessary, but its intended for custom types.
|
|
||||||
template <typename>
|
|
||||||
struct unpacker_adapter;
|
|
||||||
|
|
||||||
template <typename T, typename... U>
|
|
||||||
concept is_any_of = (std::same_as<T, U> || ...);
|
|
||||||
|
|
||||||
template <typename T, template <typename> typename Unpacker,
|
|
||||||
typename Context = detail::unpacker_context_model>
|
|
||||||
concept unpackable_with = requires(T&& value, Context& dest) {
|
|
||||||
{
|
|
||||||
Unpacker<std::remove_cvref_t<T>>::unpack(dest)
|
|
||||||
} -> is_any_of<T, tl::expected<T, msgpack::error>>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
concept custom_unpackable = unpackable_with<T, unpacker_adapter>;
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
concept builtin_unpackable = unpackable_with<T, builtin_unpacker>;
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
concept supports_unpack = builtin_unpackable<T> || custom_unpackable<T>;
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template <typename>
|
|
||||||
struct unpacker_impl_s;
|
|
||||||
|
|
||||||
template <custom_unpackable T>
|
|
||||||
struct unpacker_impl_s<T> {
|
|
||||||
using type = unpacker_adapter<T>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <builtin_unpackable T>
|
|
||||||
struct unpacker_impl_s<T> {
|
|
||||||
using type = builtin_unpacker<T>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
using unpacker_impl = unpacker_impl_s<T>::type;
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
} // namespace msgpack
|
|
||||||
|
|
||||||
#endif // msgpack_core_detail_unpackable_concepts_cf142e37b34a598f
|
|
||||||
@ -29,7 +29,6 @@ enum class error {
|
|||||||
wrong_type, // The format is incompatible with requested type.
|
wrong_type, // The format is incompatible with requested type.
|
||||||
bad_value, // Value does not fit within constraints.
|
bad_value, // Value does not fit within constraints.
|
||||||
will_truncate, // Integer return type is smaller than stored value.
|
will_truncate, // Integer return type is smaller than stored value.
|
||||||
wrong_length, // Variable-length format does not match desired length.
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace msgpack
|
} // namespace msgpack
|
||||||
|
|||||||
@ -31,14 +31,14 @@ namespace msgpack {
|
|||||||
// Supporting types/tags for formats
|
// Supporting types/tags for formats
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
struct nil {
|
struct nil {
|
||||||
constexpr nil() noexcept = default;
|
nil() = default;
|
||||||
constexpr bool operator==(nil const& other) const noexcept = default;
|
// This constructor is used by the reader implementation.
|
||||||
|
nil(auto){};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct invalid {
|
struct invalid {};
|
||||||
constexpr invalid() noexcept = default;
|
|
||||||
constexpr bool operator==(invalid const& other) const noexcept = default;
|
using boolean = bool;
|
||||||
};
|
|
||||||
|
|
||||||
struct array_desc {
|
struct array_desc {
|
||||||
constexpr array_desc() noexcept = default;
|
constexpr array_desc() noexcept = default;
|
||||||
@ -225,12 +225,12 @@ struct int64 : format<0xd3, std::int64_t> {};
|
|||||||
*/
|
*/
|
||||||
struct nil : fixtype<0xc0, 0x00> {
|
struct nil : fixtype<0xc0, 0x00> {
|
||||||
using value_type = msgpack::nil;
|
using value_type = msgpack::nil;
|
||||||
constexpr static flag flags{flag::fixed_size};
|
constexpr static flag flags{flag::fixed_size | flag::apply_mask};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct invalid : fixtype<0xc1, 0x00> {
|
struct invalid : fixtype<0xc1, 0x00> {
|
||||||
using value_type = msgpack::invalid;
|
using value_type = msgpack::invalid;
|
||||||
constexpr static flag flags{flag::fixed_size};
|
constexpr static flag flags{flag::fixed_size | flag::apply_mask};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct boolean : fixtype<0xc2, 0x01> {
|
struct boolean : fixtype<0xc2, 0x01> {
|
||||||
|
|||||||
@ -53,6 +53,9 @@
|
|||||||
|
|
||||||
namespace msgpack {
|
namespace msgpack {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct pack_adapter {};
|
||||||
|
|
||||||
class packer {
|
class packer {
|
||||||
// Replace with output_range?
|
// Replace with output_range?
|
||||||
using BufferType = std::span<std::byte>;
|
using BufferType = std::span<std::byte>;
|
||||||
@ -90,8 +93,9 @@ public:
|
|||||||
context ctx{curr_, end_};
|
context ctx{curr_, end_};
|
||||||
// If packing is successful, it updates iterators.
|
// If packing is successful, it updates iterators.
|
||||||
auto ret = ctx.pack(std::forward<T>(v));
|
auto ret = ctx.pack(std::forward<T>(v));
|
||||||
if (ret) {
|
if (!!ret) {
|
||||||
curr_ = ctx.out;
|
curr_ = ctx.out;
|
||||||
|
end_ = ctx.end;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,94 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Default unpacker implementation, utilizing pull-style parsing semantics for
|
|
||||||
// deserializing data with an expected structure akin to a schema.
|
|
||||||
//
|
|
||||||
// The default unpacker maintains some flexibility in how data was originally
|
|
||||||
// packed.
|
|
||||||
//
|
|
||||||
// E.g. If a std::uint8_t is requested, it is perfectly valid to read a uint32
|
|
||||||
// format, provided the stored value is less than 255.
|
|
||||||
//
|
|
||||||
// core/reader (to be renamed verbatim_unpacker) can be utilized in scenarios
|
|
||||||
// where code size is critical and exact type matching is not a considerable
|
|
||||||
// downside.
|
|
||||||
//
|
|
||||||
// For schemaless data, the token API can be used instead.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#ifndef msgpack_core_unpacker_910ec357d397ac7e
|
|
||||||
#define msgpack_core_unpacker_910ec357d397ac7e
|
|
||||||
|
|
||||||
#include "parselink/msgpack/core/detail/builtin_unpackable_types.h"
|
|
||||||
#include "parselink/msgpack/core/detail/unpackable_concepts.h"
|
|
||||||
#include "parselink/msgpack/core/error.h"
|
|
||||||
|
|
||||||
#include <tl/expected.hpp>
|
|
||||||
|
|
||||||
#include <cstddef>
|
|
||||||
#include <span>
|
|
||||||
|
|
||||||
namespace msgpack {
|
|
||||||
|
|
||||||
class unpacker {
|
|
||||||
using BufferType = std::span<std::byte const>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
constexpr unpacker(BufferType buff)
|
|
||||||
: buff_(buff)
|
|
||||||
, curr_(std::begin(buff_))
|
|
||||||
, end_(std::end(buff_)) {}
|
|
||||||
|
|
||||||
// IDK if we need this yet
|
|
||||||
struct context {
|
|
||||||
decltype(std::begin(BufferType{})) in;
|
|
||||||
decltype(std::begin(BufferType{})) end;
|
|
||||||
|
|
||||||
constexpr auto remaining() noexcept {
|
|
||||||
return std::ranges::distance(in, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <supports_unpack T>
|
|
||||||
constexpr tl::expected<T, error> unpack() noexcept {
|
|
||||||
if (remaining() == 0)
|
|
||||||
return tl::make_unexpected(error::end_of_message);
|
|
||||||
using unpacker_impl = detail::unpacker_impl<T>;
|
|
||||||
return unpacker_impl::unpack(*this);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <supports_unpack T>
|
|
||||||
constexpr tl::expected<T, error> unpack() noexcept {
|
|
||||||
context ctx{curr_, end_};
|
|
||||||
auto ret = ctx.unpack<T>();
|
|
||||||
if (ret) {
|
|
||||||
curr_ = ctx.in;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr auto remaining() noexcept {
|
|
||||||
return std::ranges::distance(curr_, end_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
BufferType buff_;
|
|
||||||
decltype(std::begin(buff_)) curr_;
|
|
||||||
decltype(std::end(buff_)) end_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace msgpack
|
|
||||||
|
|
||||||
#endif // msgpack_core_unpacker_910ec357d397ac7e
|
|
||||||
@ -1,121 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Extra functionality: fmt formatters for msgpack types.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#ifndef msgpack_extra_formatters_d5fd427a7f749dcb
|
|
||||||
#define msgpack_extra_formatters_d5fd427a7f749dcb
|
|
||||||
|
|
||||||
#define FMT_VERSION
|
|
||||||
|
|
||||||
#ifdef FMT_VERSION
|
|
||||||
|
|
||||||
#include "parselink/msgpack/core/format.h"
|
|
||||||
|
|
||||||
#include <fmt/format.h>
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct fmt::formatter<msgpack::invalid> : fmt::formatter<std::string_view> {
|
|
||||||
template <typename FormatContext>
|
|
||||||
auto format(msgpack::nil const&, FormatContext& ctx) const noexcept {
|
|
||||||
return fmt::format_to(ctx.out(), "{{msgpack::invalid}}");
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct fmt::formatter<msgpack::nil> : fmt::formatter<std::string_view> {
|
|
||||||
template <typename FormatContext>
|
|
||||||
auto format(msgpack::nil const&, FormatContext& ctx) const {
|
|
||||||
return fmt::format_to(ctx.out(), "{{msgpack::nil}}");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct fmt::formatter<msgpack::map_desc> : fmt::formatter<std::string_view> {
|
|
||||||
template <typename FormatContext>
|
|
||||||
auto format(msgpack::map_desc const& value, FormatContext& ctx) const {
|
|
||||||
return fmt::format_to(
|
|
||||||
ctx.out(), "{{msgpack::map_desc: {}}}", value.count);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct fmt::formatter<msgpack::array_desc> : fmt::formatter<std::string_view> {
|
|
||||||
template <typename FormatContext>
|
|
||||||
auto format(msgpack::array_desc const& value, FormatContext& ctx) const {
|
|
||||||
return fmt::format_to(
|
|
||||||
ctx.out(), "{{msgpack::array_desc: {}}}", value.count);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef PARSELINK_MSGPACK_TOKEN_API
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct fmt::formatter<msgpack::token> {
|
|
||||||
template <typename ParseContext>
|
|
||||||
constexpr auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
|
||||||
return ctx.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename FormatContext>
|
|
||||||
auto format(msgpack::token const& v, FormatContext& ctx) const {
|
|
||||||
using parselink::logging::themed_arg;
|
|
||||||
auto out = fmt::format_to(
|
|
||||||
ctx.out(), "<msgpack {} = ", themed_arg(v.type()));
|
|
||||||
switch (v.type()) {
|
|
||||||
case msgpack::format::type::unsigned_int:
|
|
||||||
fmt::format_to(
|
|
||||||
out, "{}", themed_arg(*(v.get<std::uint64_t>())));
|
|
||||||
break;
|
|
||||||
case msgpack::format::type::signed_int:
|
|
||||||
out = fmt::format_to(
|
|
||||||
out, "{}", themed_arg(*(v.get<std::uint64_t>())));
|
|
||||||
break;
|
|
||||||
case msgpack::format::type::boolean:
|
|
||||||
out = fmt::format_to(out, "{}", themed_arg(*(v.get<bool>())));
|
|
||||||
break;
|
|
||||||
case msgpack::format::type::string:
|
|
||||||
out = fmt::format_to(
|
|
||||||
out, "{}", themed_arg(*(v.get<std::string_view>())));
|
|
||||||
break;
|
|
||||||
case msgpack::format::type::binary:
|
|
||||||
out = fmt::format_to(out, "{}",
|
|
||||||
themed_arg(*(v.get<std::span<std::byte const>>())));
|
|
||||||
break;
|
|
||||||
case msgpack::format::type::map:
|
|
||||||
out = fmt::format_to(out, "(arity: {})",
|
|
||||||
themed_arg(v.get<msgpack::map_desc>()->count));
|
|
||||||
break;
|
|
||||||
case msgpack::format::type::array:
|
|
||||||
out = fmt::format_to(out, "(arity: {})",
|
|
||||||
themed_arg(v.get<msgpack::array_desc>()->count));
|
|
||||||
break;
|
|
||||||
case msgpack::format::type::nil:
|
|
||||||
out = fmt::format_to(out, "(nil)");
|
|
||||||
break;
|
|
||||||
case msgpack::format::type::invalid:
|
|
||||||
out = fmt::format_to(out, "(invalid)");
|
|
||||||
break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
return fmt::format_to(out, ">");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // PARSELINK_MSGPACK_TOKEN_API
|
|
||||||
|
|
||||||
#endif // FMT_VERSION
|
|
||||||
|
|
||||||
#endif // msgpack_extra_formatters_d5fd427a7f749dcb
|
|
||||||
@ -1,35 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: Proto
|
|
||||||
//
|
|
||||||
// Error definitions for protocol-related functionality
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#ifndef parselink_proto_error_adef9d32bb51411a
|
|
||||||
#define parselink_proto_error_adef9d32bb51411a
|
|
||||||
|
|
||||||
namespace parselink {
|
|
||||||
namespace proto {
|
|
||||||
|
|
||||||
enum class error {
|
|
||||||
system_error,
|
|
||||||
incomplete,
|
|
||||||
unsupported,
|
|
||||||
bad_data,
|
|
||||||
too_large,
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace proto
|
|
||||||
} // namespace parselink
|
|
||||||
|
|
||||||
#endif // parselink_proto_error_adef9d32bb51411a
|
|
||||||
@ -21,6 +21,9 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <span>
|
#include <span>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <variant>
|
||||||
|
|
||||||
|
#include <tl/expected.hpp>
|
||||||
|
|
||||||
namespace parselink {
|
namespace parselink {
|
||||||
namespace proto {
|
namespace proto {
|
||||||
@ -34,40 +37,35 @@ namespace proto {
|
|||||||
// This may be revised in the future. The header could remain as msgpack, or
|
// This may be revised in the future. The header could remain as msgpack, or
|
||||||
// switch to something hand-crafted for saving bits.
|
// switch to something hand-crafted for saving bits.
|
||||||
|
|
||||||
struct base_message {};
|
struct error_message {
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
concept message_type = std::is_base_of_v<base_message, T>;
|
|
||||||
|
|
||||||
struct error_message : base_message {
|
|
||||||
std::uint32_t code; // An error code
|
std::uint32_t code; // An error code
|
||||||
std::string_view what; // A string
|
std::string_view what; // A string
|
||||||
};
|
};
|
||||||
|
|
||||||
// C->S: Request to (re)connect.
|
// C->S: Request to (re)connect.
|
||||||
struct connect_message : base_message {
|
struct connect_message {
|
||||||
|
std::uint32_t user_id; // The user id.
|
||||||
std::uint32_t version; // The version of the client.
|
std::uint32_t version; // The version of the client.
|
||||||
std::string_view user_id; // The user id.
|
|
||||||
std::span<std::byte> session_token; // An optional existing session token.
|
std::span<std::byte> session_token; // An optional existing session token.
|
||||||
};
|
};
|
||||||
|
|
||||||
// S->C: Challenge to authenticate client as user_id
|
// S->C: Challenge to authenticate client as user_id
|
||||||
struct challenge_message : base_message {
|
struct challenge_message {
|
||||||
std::uint32_t version;
|
std::uint32_t version;
|
||||||
std::span<std::byte> challenge;
|
std::span<std::byte> challenge;
|
||||||
};
|
};
|
||||||
|
|
||||||
// C->S: Calculated response to a challenge.
|
// C->S: Calculated response to a challenge.
|
||||||
struct response_message : base_message {
|
struct response_message {
|
||||||
std::span<std::byte> response;
|
std::span<std::byte> response;
|
||||||
};
|
};
|
||||||
|
|
||||||
// S->C: Session token.
|
// S->C: Session token.
|
||||||
struct session_established_message : base_message {
|
struct session_established_message {
|
||||||
std::span<std::byte> session_token;
|
std::span<std::byte> session_token;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct parser_data_message : base_message {
|
struct parser_data_message {
|
||||||
std::string_view opts;
|
std::string_view opts;
|
||||||
std::span<std::byte> payload;
|
std::span<std::byte> payload;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,60 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: Proto
|
|
||||||
//
|
|
||||||
// Parser for extracting messages from msgpack.
|
|
||||||
//
|
|
||||||
// Note: Eventually, it would be nice to automatically generate the parser for
|
|
||||||
// message structures. Definitions below are templated to allow for this.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#ifndef parselink_proto_parser_ad351d41fe4c72dd
|
|
||||||
#define parselink_proto_parser_ad351d41fe4c72dd
|
|
||||||
|
|
||||||
#include "parselink/proto/error.h"
|
|
||||||
#include "parselink/proto/message.h"
|
|
||||||
|
|
||||||
#include <tl/expected.hpp>
|
|
||||||
|
|
||||||
namespace parselink {
|
|
||||||
namespace proto {
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Default parse template instantiation -- unsupported message.
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
template <message_type M>
|
|
||||||
tl::expected<M, error> parse(std::span<std::byte const> data) noexcept {
|
|
||||||
return tl::make_unexpected(error::unsupported);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Currently-implemented message types.
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#define PARSELINK_PROTO_SUPPORTED_MESSAGE(msgtype) \
|
|
||||||
tl::expected<msgtype, error> parse_##msgtype( \
|
|
||||||
std::span<std::byte const> data) noexcept; \
|
|
||||||
template <> \
|
|
||||||
constexpr tl::expected<msgtype, error> parse( \
|
|
||||||
std::span<std::byte const> data) noexcept { \
|
|
||||||
return parse_##msgtype(data); \
|
|
||||||
}
|
|
||||||
|
|
||||||
PARSELINK_PROTO_SUPPORTED_MESSAGE(connect_message);
|
|
||||||
|
|
||||||
#undef PARSELINK_PROTO_SUPPORTED_MESSAGE
|
|
||||||
|
|
||||||
} // namespace proto
|
|
||||||
} // namespace parselink
|
|
||||||
|
|
||||||
#endif // parselink_proto_parser_ad351d41fe4c72dd
|
|
||||||
@ -19,17 +19,15 @@
|
|||||||
#define session_07eae057feface79
|
#define session_07eae057feface79
|
||||||
|
|
||||||
#include "parselink/msgpack/token.h"
|
#include "parselink/msgpack/token.h"
|
||||||
#include "parselink/proto/error.h"
|
|
||||||
#include "parselink/proto/session_id.h"
|
#include "parselink/proto/session_id.h"
|
||||||
|
|
||||||
#include <tl/expected.hpp>
|
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <span>
|
#include <span>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <tl/expected.hpp>
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct std::hash<std::span<std::byte const>> {
|
struct std::hash<std::span<std::byte const>> {
|
||||||
constexpr static std::uint32_t seed_var = 0x811c9dc5;
|
constexpr static std::uint32_t seed_var = 0x811c9dc5;
|
||||||
@ -47,6 +45,14 @@ struct std::hash<std::span<std::byte const>> {
|
|||||||
namespace parselink {
|
namespace parselink {
|
||||||
namespace proto {
|
namespace proto {
|
||||||
|
|
||||||
|
enum class error {
|
||||||
|
system_error,
|
||||||
|
incomplete,
|
||||||
|
unsupported,
|
||||||
|
bad_data,
|
||||||
|
too_large,
|
||||||
|
};
|
||||||
|
|
||||||
// Structure containing header information parsed from a buffer.
|
// Structure containing header information parsed from a buffer.
|
||||||
struct header_info {
|
struct header_info {
|
||||||
std::uint32_t message_size; // Size of the message, minus the header.
|
std::uint32_t message_size; // Size of the message, minus the header.
|
||||||
|
|||||||
@ -1,156 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: Utility
|
|
||||||
//
|
|
||||||
// Utility wrappers for dealing with files.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#ifndef utility_file_8b49e5e471a7c3e5
|
|
||||||
#define utility_file_8b49e5e471a7c3e5
|
|
||||||
|
|
||||||
#include <fmt/format.h>
|
|
||||||
#include <tl/expected.hpp>
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <span>
|
|
||||||
#include <system_error>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace parselink {
|
|
||||||
namespace utility {
|
|
||||||
namespace file {
|
|
||||||
|
|
||||||
struct [[nodiscard]] handle {
|
|
||||||
constexpr handle() noexcept = default;
|
|
||||||
|
|
||||||
constexpr handle(int fd) noexcept
|
|
||||||
: fd_(fd) {
|
|
||||||
if (fd_ < 0) fd_ = -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr handle(handle const&) noexcept = delete;
|
|
||||||
|
|
||||||
constexpr handle(handle&& other) noexcept
|
|
||||||
: fd_(other.fd_) {
|
|
||||||
other.fd_ = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr handle& operator=(handle const&) noexcept = delete;
|
|
||||||
|
|
||||||
constexpr handle& operator=(handle&& other) noexcept {
|
|
||||||
close();
|
|
||||||
fd_ = other.fd_;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr operator bool() const noexcept { return fd_ >= 0; }
|
|
||||||
|
|
||||||
constexpr int operator*() const noexcept { return fd_; }
|
|
||||||
|
|
||||||
void close() {
|
|
||||||
if (*this) {
|
|
||||||
::close(fd_);
|
|
||||||
fd_ = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int fd_{-1};
|
|
||||||
};
|
|
||||||
|
|
||||||
inline tl::expected<handle, std::errc> open(
|
|
||||||
std::string_view path, int flags, int mode = {}) noexcept {
|
|
||||||
handle file(::open(std::string{path}.c_str(), flags, mode));
|
|
||||||
if (file) {
|
|
||||||
return file;
|
|
||||||
} else {
|
|
||||||
return tl::make_unexpected(static_cast<std::errc>(-*file));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline tl::expected<std::vector<std::byte>, std::errc> read_some(
|
|
||||||
handle file, std::size_t amount) noexcept {
|
|
||||||
std::vector<std::byte> data;
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
return tl::make_unexpected(static_cast<std::errc>(-*file));
|
|
||||||
}
|
|
||||||
|
|
||||||
data.resize(amount);
|
|
||||||
|
|
||||||
auto amt_read = ::read(*file, data.data(), amount);
|
|
||||||
|
|
||||||
if (amt_read < data.size()) {
|
|
||||||
data.resize(amt_read);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
requires(std::is_trivially_default_constructible_v<T> && sizeof(T) == 1)
|
|
||||||
inline tl::expected<std::vector<T>, std::errc> read(
|
|
||||||
handle const& file) noexcept {
|
|
||||||
std::vector<T> data;
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
return tl::make_unexpected(static_cast<std::errc>(-*file));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto cur = lseek(*file, 0, SEEK_CUR);
|
|
||||||
auto end = lseek(*file, 0, SEEK_END);
|
|
||||||
std::size_t amount = end - cur;
|
|
||||||
lseek(*file, SEEK_SET, cur);
|
|
||||||
data.resize(amount);
|
|
||||||
|
|
||||||
auto amt_read = ::read(*file, data.data(), amount);
|
|
||||||
|
|
||||||
if (amt_read < data.size()) {
|
|
||||||
data.resize(amt_read);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
requires(std::is_trivially_default_constructible_v<T> && sizeof(T) == 1)
|
|
||||||
inline tl::expected<std::size_t, std::errc> write(
|
|
||||||
handle const& file, std::span<T> data) noexcept {
|
|
||||||
if (!file) {
|
|
||||||
return tl::make_unexpected(static_cast<std::errc>(-*file));
|
|
||||||
}
|
|
||||||
|
|
||||||
lseek(*file, 0, SEEK_SET);
|
|
||||||
|
|
||||||
auto result = ftruncate(*file, 0);
|
|
||||||
|
|
||||||
if (result < 0) {
|
|
||||||
return tl::make_unexpected(static_cast<std::errc>(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto amt_written = ::write(*file, data.data(), data.size());
|
|
||||||
|
|
||||||
if (result < 0) {
|
|
||||||
return tl::make_unexpected(static_cast<std::errc>(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
return static_cast<std::size_t>(amt_written);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace file
|
|
||||||
} // namespace utility
|
|
||||||
} // namespace parselink
|
|
||||||
|
|
||||||
#endif // utility_file_8b49e5e471a7c3e5
|
|
||||||
@ -3,7 +3,6 @@
|
|||||||
cc_library(
|
cc_library(
|
||||||
name = "proto",
|
name = "proto",
|
||||||
srcs = [
|
srcs = [
|
||||||
"parser.cpp",
|
|
||||||
"session.cpp",
|
"session.cpp",
|
||||||
"session_id.cpp",
|
"session_id.cpp",
|
||||||
],
|
],
|
||||||
|
|||||||
@ -1,70 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: Proto
|
|
||||||
//
|
|
||||||
// Parser implementations for various msgpack messages.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#include "parselink/proto/parser.h"
|
|
||||||
#include "parselink/logging.h"
|
|
||||||
|
|
||||||
#include "parselink/msgpack/core/unpacker.h"
|
|
||||||
|
|
||||||
namespace parselink {
|
|
||||||
namespace proto {
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
logging::logger logger{"parser"};
|
|
||||||
|
|
||||||
constexpr auto assign(auto& param, msgpack::unpacker& unpacker) noexcept
|
|
||||||
-> tl::expected<void, error> {
|
|
||||||
auto do_assign = [¶m](auto&& v) { param = v; };
|
|
||||||
return unpacker.unpack<std::decay_t<decltype(param)>>()
|
|
||||||
.map(do_assign)
|
|
||||||
.map_error([](auto) { return error::bad_data; });
|
|
||||||
}
|
|
||||||
|
|
||||||
} // anonymous namespace
|
|
||||||
|
|
||||||
tl::expected<connect_message, error> parse_connect_message(
|
|
||||||
std::span<std::byte const> data) noexcept {
|
|
||||||
msgpack::unpacker unpacker(data);
|
|
||||||
connect_message msg;
|
|
||||||
|
|
||||||
constexpr static tl::unexpected<error> bad_data(error::bad_data);
|
|
||||||
|
|
||||||
auto type = unpacker.unpack<std::string_view>();
|
|
||||||
if (!type || type != "connect") return bad_data;
|
|
||||||
|
|
||||||
auto entries = unpacker.unpack<msgpack::map_desc>();
|
|
||||||
if (!entries) return bad_data;
|
|
||||||
|
|
||||||
for (auto i = entries->count; i != 0; --i) {
|
|
||||||
auto key = unpacker.unpack<std::string_view>();
|
|
||||||
if (!key) return bad_data;
|
|
||||||
if (key == "version") {
|
|
||||||
if (auto val = assign(msg.version, unpacker)) continue;
|
|
||||||
} else if (key == "user_id") {
|
|
||||||
if (auto val = assign(msg.user_id, unpacker)) continue;
|
|
||||||
}
|
|
||||||
logger.debug("Unknown key: {}", *key);
|
|
||||||
return bad_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace proto
|
|
||||||
} // namespace parselink
|
|
||||||
@ -19,13 +19,12 @@
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
#include "parselink/proto/session.h"
|
#include "parselink/proto/session.h"
|
||||||
|
|
||||||
|
#include "hydrogen.h"
|
||||||
#include "parselink/logging.h"
|
#include "parselink/logging.h"
|
||||||
#include "parselink/msgpack/token.h"
|
#include "parselink/msgpack/token.h"
|
||||||
|
|
||||||
#include <fmt/ranges.h>
|
#include <fmt/ranges.h>
|
||||||
|
|
||||||
#include "hydrogen.h"
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void ensure_initialized() {
|
void ensure_initialized() {
|
||||||
@ -188,11 +187,14 @@ tl::expected<connect_info, error> proto::parse_connect(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
session::session(std::string_view user_id) noexcept
|
session::session(std::string_view user_id, close_handle hdl) noexcept
|
||||||
: id_()
|
: id_()
|
||||||
, user_id_(std::string{user_id})
|
, user_id_(std::string{user_id})
|
||||||
|
, closer_(std::move(hdl))
|
||||||
, last_activity_(std::chrono::system_clock::now()) {
|
, last_activity_(std::chrono::system_clock::now()) {
|
||||||
logger.debug("New session with id {} created for {}", id_.raw(), user_id_);
|
logger.debug("New session with id {} created for {}", id_.raw(), user_id_);
|
||||||
}
|
}
|
||||||
|
|
||||||
session::~session() {}
|
session::~session() {
|
||||||
|
if (closer_) closer_("Destroyed");
|
||||||
|
}
|
||||||
|
|||||||
@ -21,15 +21,10 @@
|
|||||||
#include "parselink/server.h"
|
#include "parselink/server.h"
|
||||||
|
|
||||||
#include "parselink/logging.h"
|
#include "parselink/logging.h"
|
||||||
#include "parselink/msgpack/core/packer.h"
|
#include "parselink/msgpack/core/writer.h"
|
||||||
#include "parselink/msgpack/core/unpacker.h"
|
#include "parselink/msgpack/token/reader.h"
|
||||||
#include "parselink/proto/parser.h"
|
#include "parselink/msgpack/token/views.h"
|
||||||
#include "parselink/proto/session.h"
|
#include "parselink/proto/session.h"
|
||||||
#include "parselink/utility/file.h"
|
|
||||||
|
|
||||||
#include <fmt/ranges.h>
|
|
||||||
|
|
||||||
#include "hydrogen.h"
|
|
||||||
#include <boost/asio/as_tuple.hpp>
|
#include <boost/asio/as_tuple.hpp>
|
||||||
#include <boost/asio/bind_executor.hpp>
|
#include <boost/asio/bind_executor.hpp>
|
||||||
#include <boost/asio/co_spawn.hpp>
|
#include <boost/asio/co_spawn.hpp>
|
||||||
@ -44,6 +39,8 @@
|
|||||||
#include <boost/asio/write.hpp>
|
#include <boost/asio/write.hpp>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
|
||||||
|
#include <fmt/ranges.h>
|
||||||
|
|
||||||
using namespace parselink;
|
using namespace parselink;
|
||||||
#include <fmt/ranges.h>
|
#include <fmt/ranges.h>
|
||||||
|
|
||||||
@ -75,13 +72,6 @@ struct fmt::formatter<boost::system::error_code>
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
|
||||||
struct fmt::formatter<tl::monostate> : fmt::formatter<std::string_view> {
|
|
||||||
constexpr auto format(auto const&, auto& ctx) const {
|
|
||||||
return fmt::formatter<std::string_view>::format(".", ctx);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct fmt::formatter<msgpack::token> {
|
struct fmt::formatter<msgpack::token> {
|
||||||
template <typename ParseContext>
|
template <typename ParseContext>
|
||||||
@ -161,7 +151,6 @@ namespace {
|
|||||||
logging::logger logger("server");
|
logging::logger logger("server");
|
||||||
constexpr auto no_ex_coro = net::as_tuple(use_awaitable);
|
constexpr auto no_ex_coro = net::as_tuple(use_awaitable);
|
||||||
constexpr auto no_ex_defer = net::as_tuple(deferred);
|
constexpr auto no_ex_defer = net::as_tuple(deferred);
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
#include <parselink/server/memory_session_manager.h>
|
#include <parselink/server/memory_session_manager.h>
|
||||||
@ -177,14 +166,11 @@ public:
|
|||||||
|
|
||||||
net::awaitable<tl::expected<proto::session*, proto::error>> create_session(
|
net::awaitable<tl::expected<proto::session*, proto::error>> create_session(
|
||||||
std::shared_ptr<user_connection> const& conn,
|
std::shared_ptr<user_connection> const& conn,
|
||||||
proto::connect_message const& info);
|
proto::connect_info const& info);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
awaitable<void> user_listen();
|
awaitable<void> user_listen();
|
||||||
|
|
||||||
tl::expected<tl::monostate, std::errc> load_keys() noexcept;
|
|
||||||
|
|
||||||
hydro_kx_keypair kp_;
|
|
||||||
net::io_context io_context_;
|
net::io_context io_context_;
|
||||||
net::io_context::strand session_strand_;
|
net::io_context::strand session_strand_;
|
||||||
memory_session_manager session_mgr_;
|
memory_session_manager session_mgr_;
|
||||||
@ -275,7 +261,12 @@ public:
|
|||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto connect_info = proto::parse<proto::connect_message>(msgbuf);
|
auto reader = msgpack::token_reader(msgbuf);
|
||||||
|
std::array<msgpack::token, 32> tokens;
|
||||||
|
auto connect_info =
|
||||||
|
reader.read_some(tokens)
|
||||||
|
.map_error([](auto) { return proto::error::bad_data; })
|
||||||
|
.and_then(proto::parse_connect);
|
||||||
|
|
||||||
if (!connect_info) {
|
if (!connect_info) {
|
||||||
logger.error("Session failed: {}", connect_info.error());
|
logger.error("Session failed: {}", connect_info.error());
|
||||||
@ -288,6 +279,8 @@ public:
|
|||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
session_ = *session;
|
session_ = *session;
|
||||||
|
|
||||||
|
auto writer = msgpack::writer(msgbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class state { init, authenticated, active };
|
enum class state { init, authenticated, active };
|
||||||
@ -305,8 +298,6 @@ monolithic_server::monolithic_server(std::string_view address,
|
|||||||
, addr_(net::ip::address::from_string(std::string{address}))
|
, addr_(net::ip::address::from_string(std::string{address}))
|
||||||
, user_port_{user_port}
|
, user_port_{user_port}
|
||||||
, websocket_port_{websocket_port} {
|
, websocket_port_{websocket_port} {
|
||||||
logger.debug("Loaded keys: {}", load_keys());
|
|
||||||
|
|
||||||
logger.debug("Creating monolithic_server(address = {}, user_port = {}, "
|
logger.debug("Creating monolithic_server(address = {}, user_port = {}, "
|
||||||
"websocket_port = {})",
|
"websocket_port = {})",
|
||||||
address, user_port_, websocket_port_);
|
address, user_port_, websocket_port_);
|
||||||
@ -338,73 +329,9 @@ std::error_code monolithic_server::run() noexcept {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
tl::expected<tl::monostate, std::errc> monolithic_server::load_keys() noexcept {
|
|
||||||
std::string_view filename = "/home/rihya/server_kp.keys";
|
|
||||||
|
|
||||||
enum class result { loaded, generated };
|
|
||||||
|
|
||||||
auto load_key = [this](auto const& raw) -> tl::expected<result, std::errc> {
|
|
||||||
msgpack::unpacker unpacker(raw);
|
|
||||||
|
|
||||||
auto pk = unpacker.unpack<std::span<std::byte const, sizeof(kp_.pk)>>();
|
|
||||||
if (!pk) return tl::make_unexpected(std::errc::bad_message);
|
|
||||||
auto sk = unpacker.unpack<std::span<std::byte const, sizeof(kp_.sk)>>();
|
|
||||||
if (!sk) return tl::make_unexpected(std::errc::bad_message);
|
|
||||||
|
|
||||||
std::ranges::transform(pk->begin(), pk->end(), std::begin(kp_.pk),
|
|
||||||
[](auto c) { return std::bit_cast<unsigned char>(c); });
|
|
||||||
|
|
||||||
std::ranges::transform(sk->begin(), sk->end(), std::begin(kp_.sk),
|
|
||||||
[](auto c) { return std::bit_cast<unsigned char>(c); });
|
|
||||||
|
|
||||||
return result::loaded;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto generate_keys =
|
|
||||||
[this](auto const& err) -> tl::expected<result, std::errc> {
|
|
||||||
logger.warning("Could not load server keys, generating a keypair");
|
|
||||||
hydro_kx_keygen(&kp_);
|
|
||||||
return result::generated;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto commit = [this](auto const& handle)
|
|
||||||
-> tl::expected<tl::monostate, std::errc> {
|
|
||||||
std::vector<std::byte> buff(4 + sizeof(kp_.pk) + sizeof(kp_.sk));
|
|
||||||
std::span<std::byte> pk(
|
|
||||||
reinterpret_cast<std::byte*>(kp_.pk), sizeof(kp_.pk));
|
|
||||||
std::span<std::byte> sk(
|
|
||||||
reinterpret_cast<std::byte*>(kp_.sk), sizeof(kp_.pk));
|
|
||||||
msgpack::packer packer(buff);
|
|
||||||
packer.pack(pk);
|
|
||||||
packer.pack(sk);
|
|
||||||
return utility::file::write(handle, packer.subspan()).map([](auto) {
|
|
||||||
logger.info("Wrote new keys to disk");
|
|
||||||
return tl::monostate{};
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
auto load_or_generate_keys = [&load_key, &generate_keys, &commit](
|
|
||||||
auto const& handle)
|
|
||||||
-> tl::expected<tl::monostate, std::errc> {
|
|
||||||
return utility::file::read<std::byte>(handle)
|
|
||||||
.and_then(load_key)
|
|
||||||
.or_else(generate_keys)
|
|
||||||
.and_then([&handle, &commit](auto r)
|
|
||||||
-> tl::expected<tl::monostate, std::errc> {
|
|
||||||
if (r == result::generated) {
|
|
||||||
return commit(handle);
|
|
||||||
}
|
|
||||||
return tl::monostate{};
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return utility::file::open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)
|
|
||||||
.and_then(load_or_generate_keys);
|
|
||||||
}
|
|
||||||
|
|
||||||
net::awaitable<tl::expected<proto::session*, proto::error>>
|
net::awaitable<tl::expected<proto::session*, proto::error>>
|
||||||
monolithic_server::create_session(std::shared_ptr<user_connection> const& conn,
|
monolithic_server::create_session(std::shared_ptr<user_connection> const& conn,
|
||||||
proto::connect_message const& info) {
|
proto::connect_info const& info) {
|
||||||
// Move to session strand.
|
// Move to session strand.
|
||||||
co_await net::post(session_strand_,
|
co_await net::post(session_strand_,
|
||||||
net::bind_executor(session_strand_, use_awaitable));
|
net::bind_executor(session_strand_, use_awaitable));
|
||||||
|
|||||||
@ -1,11 +1,10 @@
|
|||||||
|
|
||||||
cc_library(
|
cc_library(
|
||||||
name = "common",
|
name = "test_deps",
|
||||||
srcs = [
|
srcs = [
|
||||||
"test_main.cpp",
|
"test_main.cpp",
|
||||||
|
"rng.h",
|
||||||
],
|
],
|
||||||
includes = ["include"],
|
|
||||||
hdrs = glob(["include/*.h"]),
|
|
||||||
deps = [
|
deps = [
|
||||||
"//include/parselink:msgpack",
|
"//include/parselink:msgpack",
|
||||||
"@expected",
|
"@expected",
|
||||||
@ -13,7 +12,6 @@ cc_library(
|
|||||||
"@magic_enum",
|
"@magic_enum",
|
||||||
"@ut",
|
"@ut",
|
||||||
],
|
],
|
||||||
visibility = ["__subpackages__"],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
cc_test(
|
cc_test(
|
||||||
@ -23,7 +21,16 @@ cc_test(
|
|||||||
"test_reader_relaxed.cpp",
|
"test_reader_relaxed.cpp",
|
||||||
"test_reader_strict.cpp",
|
"test_reader_strict.cpp",
|
||||||
],
|
],
|
||||||
deps = ["common"],
|
deps = ["test_deps"],
|
||||||
|
)
|
||||||
|
|
||||||
|
cc_test(
|
||||||
|
name = "packer",
|
||||||
|
size = "small",
|
||||||
|
srcs = [
|
||||||
|
"test_packer.cpp",
|
||||||
|
],
|
||||||
|
deps = ["test_deps"],
|
||||||
)
|
)
|
||||||
|
|
||||||
cc_test(
|
cc_test(
|
||||||
@ -32,7 +39,7 @@ cc_test(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"test_writer.cpp",
|
"test_writer.cpp",
|
||||||
],
|
],
|
||||||
deps = ["common"],
|
deps = ["test_deps"],
|
||||||
)
|
)
|
||||||
|
|
||||||
cc_test(
|
cc_test(
|
||||||
@ -41,7 +48,7 @@ cc_test(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"test_token.cpp",
|
"test_token.cpp",
|
||||||
],
|
],
|
||||||
deps = ["common"],
|
deps = ["test_deps"],
|
||||||
)
|
)
|
||||||
|
|
||||||
cc_test(
|
cc_test(
|
||||||
@ -50,7 +57,7 @@ cc_test(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"test_token_reader.cpp",
|
"test_token_reader.cpp",
|
||||||
],
|
],
|
||||||
deps = ["common"],
|
deps = ["test_deps"],
|
||||||
)
|
)
|
||||||
|
|
||||||
cc_test(
|
cc_test(
|
||||||
@ -59,7 +66,7 @@ cc_test(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"test_token_views.cpp",
|
"test_token_views.cpp",
|
||||||
],
|
],
|
||||||
deps = ["common"],
|
deps = ["test_deps"],
|
||||||
)
|
)
|
||||||
|
|
||||||
cc_binary(
|
cc_binary(
|
||||||
@ -67,7 +74,7 @@ cc_binary(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"test_speed.cpp",
|
"test_speed.cpp",
|
||||||
],
|
],
|
||||||
deps = ["common"],
|
deps = ["test_deps"],
|
||||||
)
|
)
|
||||||
|
|
||||||
cc_binary(
|
cc_binary(
|
||||||
@ -75,5 +82,5 @@ cc_binary(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"test_code.cpp",
|
"test_code.cpp",
|
||||||
],
|
],
|
||||||
deps = ["common"],
|
deps = ["test_deps"],
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,102 +0,0 @@
|
|||||||
#ifndef msgpack_test_utils_4573e6627d8efe78
|
|
||||||
#define msgpack_test_utils_4573e6627d8efe78
|
|
||||||
|
|
||||||
// clang-format off : Must include fmt/format before extra/formatters.
|
|
||||||
#include <fmt/format.h>
|
|
||||||
|
|
||||||
#include "parselink/msgpack/extra/formatters.h"
|
|
||||||
// clang-format on
|
|
||||||
|
|
||||||
#include <tl/expected.hpp>
|
|
||||||
|
|
||||||
#include <boost/ut.hpp>
|
|
||||||
#include <magic_enum.hpp>
|
|
||||||
#include <magic_enum_format.hpp>
|
|
||||||
|
|
||||||
#include <source_location>
|
|
||||||
|
|
||||||
namespace test {
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
inline constexpr std::array<std::byte, sizeof(T)> as_bytes(T&& t) {
|
|
||||||
return std::bit_cast<std::array<std::byte, sizeof(T)>>(std::forward<T>(t));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... Bytes>
|
|
||||||
inline constexpr std::array<std::byte, sizeof...(Bytes)> make_bytes(
|
|
||||||
Bytes&&... bytes) noexcept {
|
|
||||||
return {std::byte(std::forward<Bytes>(bytes))...};
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace test
|
|
||||||
|
|
||||||
// This formatter allows expected values to be formatted for both its expected
|
|
||||||
// and unexpected types.
|
|
||||||
template <typename T, typename E>
|
|
||||||
struct fmt::formatter<tl::expected<T, E>> {
|
|
||||||
int width = 0;
|
|
||||||
|
|
||||||
fmt::formatter<T> t_fmtr;
|
|
||||||
fmt::formatter<E> e_fmtr;
|
|
||||||
|
|
||||||
constexpr auto parse(auto& ctx) {
|
|
||||||
using char_type =
|
|
||||||
typename std::remove_reference_t<decltype(ctx)>::char_type;
|
|
||||||
auto delim = char_type{'|'};
|
|
||||||
auto close_brace = char_type{'}'};
|
|
||||||
std::array<char_type, 32> fmtbuf;
|
|
||||||
|
|
||||||
if (ctx.begin() == ctx.end() || *ctx.begin() == '}') {
|
|
||||||
return ctx.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool t_parsed = false;
|
|
||||||
|
|
||||||
auto itr = ctx.begin();
|
|
||||||
auto buf = fmtbuf.begin();
|
|
||||||
|
|
||||||
while (*itr != close_brace) {
|
|
||||||
if (*itr == delim) {
|
|
||||||
if (t_parsed) {
|
|
||||||
fmt::throw_format_error("multiple delims encountered");
|
|
||||||
}
|
|
||||||
t_parsed = true;
|
|
||||||
*buf = close_brace;
|
|
||||||
auto str = basic_string_view<char_type>(
|
|
||||||
fmtbuf.data(), std::distance(fmtbuf.begin(), buf));
|
|
||||||
basic_format_parse_context<char_type> subparser(str, 0);
|
|
||||||
t_fmtr.parse(subparser);
|
|
||||||
buf = fmtbuf.begin();
|
|
||||||
} else {
|
|
||||||
*buf = *itr;
|
|
||||||
++buf;
|
|
||||||
}
|
|
||||||
++itr;
|
|
||||||
}
|
|
||||||
|
|
||||||
*buf = close_brace;
|
|
||||||
auto str = basic_string_view<char_type>(
|
|
||||||
fmtbuf.data(), std::distance(fmtbuf.begin(), buf));
|
|
||||||
basic_format_parse_context<char_type> subparser(str, 0);
|
|
||||||
if (t_parsed) {
|
|
||||||
e_fmtr.parse(subparser);
|
|
||||||
} else {
|
|
||||||
t_fmtr.parse(subparser);
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.advance_to(itr);
|
|
||||||
|
|
||||||
return itr;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto format(tl::expected<T, E> const& v, auto& ctx) const {
|
|
||||||
if (v) {
|
|
||||||
// return fmt::format_to(ctx.out(), "{}", v.value());
|
|
||||||
return t_fmtr.format(v.value(), ctx);
|
|
||||||
} else {
|
|
||||||
return e_fmtr.format(v.error(), ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // msgpack_test_utils_4573e6627d8efe78
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
cc_test(
|
|
||||||
name = "packer",
|
|
||||||
size = "small",
|
|
||||||
srcs = glob([
|
|
||||||
"*.cpp",
|
|
||||||
"*.h",
|
|
||||||
]),
|
|
||||||
deps = ["//tests/msgpack:common", "@rapidcheck"],
|
|
||||||
)
|
|
||||||
@ -1,63 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Packer tests, byte types
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#include "test_packer.h"
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
suite pack_bytes = [] {
|
|
||||||
#if 0
|
|
||||||
// Byte ranges -> Binary
|
|
||||||
"packer::pack<std::span<std::byte>>"_test = [] {
|
|
||||||
expect(test_deduced<std::span<std::byte const>>());
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
"packer::pack<std::array<std::byte, N>>"_test = [] {
|
|
||||||
{
|
|
||||||
constexpr auto data = test::make_bytes(0x01, 0x02, 0x03, 0x04);
|
|
||||||
constexpr auto expected =
|
|
||||||
test::make_bytes(0xc4, 0x04, 0x01, 0x02, 0x03, 0x04);
|
|
||||||
std::array<std::byte, std::size(expected)> payload;
|
|
||||||
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
expect(!!packer.pack(data));
|
|
||||||
expect(std::ranges::equal(payload, expected));
|
|
||||||
}
|
|
||||||
{
|
|
||||||
constexpr std::array<std::byte, 256> data{};
|
|
||||||
constexpr std::array<std::byte, 259> expected{
|
|
||||||
std::byte(0xc5), std::byte(0x01), std::byte(0x00)};
|
|
||||||
std::array<std::byte, std::size(expected)> payload;
|
|
||||||
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
expect(!!packer.pack(data));
|
|
||||||
expect(std::ranges::equal(payload, expected));
|
|
||||||
}
|
|
||||||
{
|
|
||||||
constexpr std::array<std::byte, 65536> data{};
|
|
||||||
constexpr std::array<std::byte, std::size(data) + 5> expected{
|
|
||||||
std::byte(0xc6), std::byte(0x00), std::byte(0x01),
|
|
||||||
std::byte(0x00), std::byte(0x00)};
|
|
||||||
std::array<std::byte, std::size(expected)> payload;
|
|
||||||
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
expect(!!packer.pack(data));
|
|
||||||
expect(std::ranges::equal(payload, expected));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,63 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Packer tests, ranges (for types that serialize to array / map formats)
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#include "test_packer.h"
|
|
||||||
|
|
||||||
#include <fmt/ranges.h>
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
constexpr bool expect_equal(auto const& expected, auto const& actual) {
|
|
||||||
if (!std::ranges::equal(expected, actual)) {
|
|
||||||
fmt::print("\n\tExpected: ");
|
|
||||||
for (auto x : expected) {
|
|
||||||
fmt::print("{:02x} ", x);
|
|
||||||
}
|
|
||||||
fmt::print("\n\t Actual: ");
|
|
||||||
for (auto x : actual) {
|
|
||||||
fmt::print("{:02x} ", x);
|
|
||||||
}
|
|
||||||
fmt::print("\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
suite pack_ranges = [] {
|
|
||||||
"packer::pack<std::array/std::span<int>>"_test = [] {
|
|
||||||
constexpr auto data_array = std::to_array<int>({5, 10, 15, 20});
|
|
||||||
constexpr auto expected = test::make_bytes(0x94, 0x05, 0xa, 0xf, 0x14);
|
|
||||||
|
|
||||||
{
|
|
||||||
std::array<std::byte, std::size(expected)> payload;
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
expect(!!packer.pack(data_array));
|
|
||||||
expect(expect_equal(std::span(expected), payload));
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
std::span data_span{data_array};
|
|
||||||
std::array<std::byte, std::size(expected)> payload;
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
expect(!!packer.pack(data_span));
|
|
||||||
expect(expect_equal(expected, payload));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,71 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Packer tests, signed ints.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#include "test_packer.h"
|
|
||||||
|
|
||||||
#include <rapidcheck.h>
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
auto check_pack() {
|
|
||||||
return rc::check([](T value) {
|
|
||||||
std::array<std::byte, 16> payload;
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
if (!packer.pack(value)) return false;
|
|
||||||
|
|
||||||
if (value < 128 && value >= -32) {
|
|
||||||
// positive_fixint/negative_fixint
|
|
||||||
return packer.tell() == 1
|
|
||||||
&& payload[0] == static_cast<std::byte>(value);
|
|
||||||
} else if (within<std::int8_t>(value)) {
|
|
||||||
return payload[0] == msgpack::format::int8::marker
|
|
||||||
&& verify_packed<std::int8_t>(packer, value);
|
|
||||||
} else if (within<std::int16_t>(value)) {
|
|
||||||
return payload[0] == msgpack::format::int16::marker
|
|
||||||
&& verify_packed<std::int16_t>(packer, value);
|
|
||||||
} else if (within<std::int32_t>(value)) {
|
|
||||||
return payload[0] == msgpack::format::int32::marker
|
|
||||||
&& verify_packed<std::int32_t>(packer, value);
|
|
||||||
} else {
|
|
||||||
return payload[0] == msgpack::format::int64::marker
|
|
||||||
&& verify_packed<std::int64_t>(packer, value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
} // anonymous namespace
|
|
||||||
|
|
||||||
suite pack_signed_types = [] {
|
|
||||||
"packer::pack<std::int8_t>"_test = [] {
|
|
||||||
expect(check_pack<std::int8_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"packer::pack<std::int16_t>"_test = [] {
|
|
||||||
expect(check_pack<std::int16_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"packer::pack<std::int32_t>"_test = [] {
|
|
||||||
expect(check_pack<std::int32_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"packer::pack<std::int64_t>"_test = [] {
|
|
||||||
expect(check_pack<std::int64_t>());
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,50 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Packer tests, simple types
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#include "test_packer.h"
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
suite pack_simple_types = [] {
|
|
||||||
"packer::pack<nil>"_test = [] {
|
|
||||||
std::array<std::byte, 8> payload;
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
expect(!!packer.pack(msgpack::nil{}));
|
|
||||||
expect(packer.tell() == 1);
|
|
||||||
expect(payload[0] == std::byte{0xc0});
|
|
||||||
};
|
|
||||||
|
|
||||||
"packer::pack<invalid>"_test = [] {
|
|
||||||
std::array<std::byte, 8> payload;
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
expect(!!packer.pack(msgpack::invalid{}));
|
|
||||||
expect(packer.tell() == 1);
|
|
||||||
expect(payload[0] == std::byte{0xc1});
|
|
||||||
};
|
|
||||||
|
|
||||||
"packer::pack<bool>"_test = [] {
|
|
||||||
std::array<std::byte, 8> payload;
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
expect(!!packer.pack(false));
|
|
||||||
expect(packer.tell() == 1);
|
|
||||||
expect(payload[0] == std::byte{0xc2});
|
|
||||||
expect(!!packer.pack(true));
|
|
||||||
expect(packer.tell() == 2);
|
|
||||||
expect(payload[1] == std::byte{0xc3});
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,46 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Packer tests, strings.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#include "test_packer.h"
|
|
||||||
|
|
||||||
#include <rapidcheck.h>
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
template <std::unsigned_integral LenType, typename StrType = std::string>
|
|
||||||
auto check_string() {
|
|
||||||
return rc::check([](LenType value) {
|
|
||||||
auto str = *rc::gen::container<std::string>(
|
|
||||||
value, rc::gen::character<char>());
|
|
||||||
std::vector<std::byte> payload;
|
|
||||||
payload.resize(value + 32);
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
if (!packer.pack(str)) return false;
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
} // anonymous namespace
|
|
||||||
|
|
||||||
suite pack_strings = [] {
|
|
||||||
"packer::pack<std::string>"_test = [] {
|
|
||||||
expect(check_string<std::uint8_t>());
|
|
||||||
expect(check_string<std::uint16_t>());
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,42 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Packer tests, common code.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#ifndef tests_msgpack_packer_6e9a5bf8e14223bc
|
|
||||||
#define tests_msgpack_packer_6e9a5bf8e14223bc
|
|
||||||
|
|
||||||
#include "parselink/msgpack/core/packer.h"
|
|
||||||
|
|
||||||
#include "test_utils.h"
|
|
||||||
|
|
||||||
template <std::integral T>
|
|
||||||
constexpr auto within(auto value) noexcept {
|
|
||||||
return value >= std::numeric_limits<T>::min()
|
|
||||||
&& value <= std::numeric_limits<T>::max();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <std::integral T>
|
|
||||||
constexpr auto verify_packed(auto const& packer, auto value) noexcept {
|
|
||||||
std::array<std::byte, (std::numeric_limits<T>::digits + 7) / 8> raw;
|
|
||||||
if (packer.tell() != 1 + raw.size()) return false;
|
|
||||||
|
|
||||||
auto packed = packer.subspan().subspan(1);
|
|
||||||
be_to_host(packed.begin(), packed.end(), raw.begin());
|
|
||||||
auto actual = std::bit_cast<T>(raw);
|
|
||||||
return actual == value;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // tests_msgpack_packer_6e9a5bf8e14223bc
|
|
||||||
@ -1,71 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Packer tests, unsigned ints.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#include "test_packer.h"
|
|
||||||
|
|
||||||
#include <rapidcheck.h>
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
auto check_unsigned() {
|
|
||||||
return rc::check([](T value) {
|
|
||||||
std::array<std::byte, 16> payload;
|
|
||||||
msgpack::packer packer(payload);
|
|
||||||
if (!packer.pack(value)) return false;
|
|
||||||
|
|
||||||
if (value < 128) {
|
|
||||||
// positive_fixint
|
|
||||||
return packer.tell() == 1
|
|
||||||
&& payload[0] == static_cast<std::byte>(value);
|
|
||||||
} else if (within<std::uint8_t>(value)) {
|
|
||||||
return payload[0] == msgpack::format::uint8::marker
|
|
||||||
&& verify_packed<std::uint8_t>(packer, value);
|
|
||||||
} else if (within<std::uint16_t>(value)) {
|
|
||||||
return payload[0] == msgpack::format::uint16::marker
|
|
||||||
&& verify_packed<std::uint16_t>(packer, value);
|
|
||||||
} else if (within<std::uint32_t>(value)) {
|
|
||||||
return payload[0] == msgpack::format::uint32::marker
|
|
||||||
&& verify_packed<std::uint32_t>(packer, value);
|
|
||||||
} else {
|
|
||||||
return payload[0] == msgpack::format::uint64::marker
|
|
||||||
&& verify_packed<std::uint64_t>(packer, value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
} // anonymous namespace
|
|
||||||
|
|
||||||
suite packer_unsigned_ints = [] {
|
|
||||||
"packer::pack<std::uint8_t>"_test = [] {
|
|
||||||
expect(check_unsigned<std::uint8_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"packer::pack<std::uint16_t>"_test = [] {
|
|
||||||
expect(check_unsigned<std::uint16_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"packer::pack<std::uint32_t>"_test = [] {
|
|
||||||
expect(check_unsigned<std::uint32_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"packer::pack<std::uint64_t>"_test = [] {
|
|
||||||
expect(check_unsigned<std::uint64_t>());
|
|
||||||
};
|
|
||||||
};
|
|
||||||
100
tests/msgpack/test_utils.h
Normal file
100
tests/msgpack/test_utils.h
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
#ifndef msgpack_test_utils_4573e6627d8efe78
|
||||||
|
#define msgpack_test_utils_4573e6627d8efe78
|
||||||
|
|
||||||
|
#include <boost/ut.hpp>
|
||||||
|
#include <magic_enum.hpp>
|
||||||
|
|
||||||
|
#include <fmt/format.h>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr bool operator==(std::span<T const> a, std::span<T const> b) noexcept {
|
||||||
|
return std::equal(a.begin(), a.end(), b.begin(), b.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr std::array<std::byte, sizeof(T)> as_bytes(T&& t) {
|
||||||
|
return std::bit_cast<std::array<std::byte, sizeof(T)>>(std::forward<T>(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Bytes>
|
||||||
|
constexpr std::array<std::byte, sizeof...(Bytes)> make_bytes(Bytes&&... bytes) {
|
||||||
|
return {std::byte(std::forward<Bytes>(bytes))...};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, std::size_t C = 1024 * 1024>
|
||||||
|
struct oversized_array {
|
||||||
|
std::array<T, C> data;
|
||||||
|
std::size_t size;
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr auto to_bytes_array_oversized(auto const& container) {
|
||||||
|
using value_type = std::decay_t<decltype(container[0])>;
|
||||||
|
oversized_array<value_type> arr;
|
||||||
|
std::copy(std::begin(container), std::end(container), std::begin(arr.data));
|
||||||
|
arr.size = std::distance(std::begin(container), std::end(container));
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
consteval auto generate_bytes(auto callable) {
|
||||||
|
constexpr auto oversized = to_bytes_array_oversized(callable());
|
||||||
|
using value_type = std::decay_t<decltype(oversized.data[0])>;
|
||||||
|
std::array<value_type, oversized.size> out;
|
||||||
|
std::copy(std::begin(oversized.data),
|
||||||
|
std::next(std::begin(oversized.data), oversized.size),
|
||||||
|
std::begin(out));
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
consteval auto build_string(auto callable) {
|
||||||
|
constexpr auto string_array = generate_bytes(callable);
|
||||||
|
return string_array;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t... Sizes>
|
||||||
|
constexpr auto cat(std::array<std::byte, Sizes> const&... a) noexcept {
|
||||||
|
std::array<std::byte, (Sizes + ...)> out;
|
||||||
|
std::size_t index{};
|
||||||
|
((std::copy_n(a.begin(), Sizes, out.begin() + index), index += Sizes), ...);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr auto repeat(std::span<std::byte const> sv, std::size_t count) {
|
||||||
|
std::vector<std::byte> range;
|
||||||
|
range.reserve(sv.size() * count);
|
||||||
|
for (decltype(count) i = 0; i < count; ++i) {
|
||||||
|
std::copy_n(sv.begin(), sv.size(), std::back_inserter(range));
|
||||||
|
}
|
||||||
|
return range;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr auto repeat(std::string_view sv, std::size_t count) {
|
||||||
|
std::vector<char> range;
|
||||||
|
range.reserve(sv.size() * count);
|
||||||
|
for (decltype(count) i = 0; i < count; ++i) {
|
||||||
|
std::copy_n(sv.begin(), sv.size(), std::back_inserter(range));
|
||||||
|
}
|
||||||
|
return range;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr auto from_string_view(std::string_view sv) {
|
||||||
|
std::vector<std::byte> range;
|
||||||
|
range.resize(sv.size());
|
||||||
|
auto itr = range.begin();
|
||||||
|
for (auto c : sv) {
|
||||||
|
*itr = std::byte(c);
|
||||||
|
++itr;
|
||||||
|
}
|
||||||
|
return range;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E>
|
||||||
|
std::ostream& operator<<(std::ostream& os, tl::expected<T, E> const& exp) {
|
||||||
|
if (exp.has_value()) {
|
||||||
|
os << "Value: '" << *exp << "'";
|
||||||
|
} else {
|
||||||
|
os << "Error";
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // msgpack_test_utils_4573e6627d8efe78
|
||||||
@ -1,9 +0,0 @@
|
|||||||
cc_test(
|
|
||||||
name = "unpacker",
|
|
||||||
size = "small",
|
|
||||||
srcs = glob([
|
|
||||||
"*.cpp",
|
|
||||||
"*.h",
|
|
||||||
]),
|
|
||||||
deps = ["//tests/msgpack:common", "@rapidcheck"],
|
|
||||||
)
|
|
||||||
@ -1,68 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Unpacker tests for bytes.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#include "test_unpacker.h"
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
suite unpack_byte_types = [] {
|
|
||||||
// TODO: Make non-const version?
|
|
||||||
"unpacker::unpack<std::span<std::byte const>>"_test = [] {
|
|
||||||
constexpr auto payload =
|
|
||||||
test::make_bytes(0xc4, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05);
|
|
||||||
constexpr auto expected =
|
|
||||||
test::make_bytes(0x01, 0x02, 0x03, 0x04, 0x05);
|
|
||||||
{
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
auto actual = unpacker.unpack<std::span<std::byte const>>();
|
|
||||||
expect(std::ranges::equal(*actual, expected));
|
|
||||||
}
|
|
||||||
{
|
|
||||||
// Test extent
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
auto actual = unpacker.unpack<std::span<std::byte const, 5>>();
|
|
||||||
expect(std::ranges::equal(*actual, expected));
|
|
||||||
}
|
|
||||||
{
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
auto actual = unpacker.unpack<std::span<std::byte const, 4>>();
|
|
||||||
expect(actual == tl::make_unexpected(msgpack::error::wrong_length));
|
|
||||||
auto actual2 = unpacker.unpack<std::span<std::byte const>>();
|
|
||||||
expect(std::ranges::equal(*actual2, expected));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Make non-const version?
|
|
||||||
"unpacker::unpack<std::array<std::byte, N>>"_test = [] {
|
|
||||||
constexpr auto payload =
|
|
||||||
test::make_bytes(0xc4, 0x05, 0x01, 0x02, 0x03, 0x04, 0x06);
|
|
||||||
constexpr auto expected =
|
|
||||||
test::make_bytes(0x01, 0x02, 0x03, 0x04, 0x06);
|
|
||||||
{
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
auto actual = unpacker.unpack<std::array<std::byte, 5>>();
|
|
||||||
expect(std::ranges::equal(*actual, expected));
|
|
||||||
}
|
|
||||||
{
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
auto actual = unpacker.unpack<std::array<std::byte, 4>>();
|
|
||||||
expect(actual == tl::make_unexpected(msgpack::error::wrong_length));
|
|
||||||
auto actual2 = unpacker.unpack<std::span<std::byte const, 5>>();
|
|
||||||
expect(std::ranges::equal(*actual2, expected));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,105 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Unpacker tests for signed integer values
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#include "test_unpacker.h"
|
|
||||||
|
|
||||||
#include <rapidcheck.h>
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
struct get_marker;
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct get_marker<std::int8_t> {
|
|
||||||
static constexpr std::byte marker = msgpack::format::int8::marker;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct get_marker<std::int16_t> {
|
|
||||||
static constexpr std::byte marker = msgpack::format::int16::marker;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct get_marker<std::int32_t> {
|
|
||||||
static constexpr std::byte marker = msgpack::format::int32::marker;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct get_marker<std::int64_t> {
|
|
||||||
static constexpr std::byte marker = msgpack::format::int64::marker;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Utilize rapidcheck to generate random packed payloads, and unpacking the
|
|
||||||
// correct value
|
|
||||||
template <typename PackType, typename UnpackType>
|
|
||||||
auto check_unpack_integer() noexcept {
|
|
||||||
auto result = rc::check([](PackType value) {
|
|
||||||
std::vector<std::byte> payload = {get_marker<PackType>::marker};
|
|
||||||
auto bytes = host_to_be(
|
|
||||||
std::bit_cast<std::array<std::byte, sizeof(PackType)>>(value));
|
|
||||||
std::copy(bytes.begin(), bytes.end(), std::back_inserter(payload));
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
auto unpacked_value = unpacker.unpack<UnpackType>();
|
|
||||||
if (std::numeric_limits<UnpackType>::max() < value
|
|
||||||
|| std::numeric_limits<UnpackType>::min() > value) {
|
|
||||||
return unpacked_value
|
|
||||||
== tl::make_unexpected(msgpack::error::will_truncate);
|
|
||||||
} else {
|
|
||||||
return unpacked_value.has_value() && unpacked_value == value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (!result) {
|
|
||||||
fmt::print("Failed on {}\n",
|
|
||||||
std::source_location::current().function_name());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // anonymous namespace
|
|
||||||
|
|
||||||
suite unpack_signed_types = [] {
|
|
||||||
"unpacker::unpack<std::int8_t>"_test = [] {
|
|
||||||
expect(check_unpack_integer<std::int8_t, std::int8_t>());
|
|
||||||
expect(check_unpack_integer<std::int16_t, std::int8_t>());
|
|
||||||
expect(check_unpack_integer<std::int32_t, std::int8_t>());
|
|
||||||
expect(check_unpack_integer<std::int64_t, std::int8_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"unpacker::unpack<std::int16_t>"_test = [] {
|
|
||||||
expect(check_unpack_integer<std::int8_t, std::int16_t>());
|
|
||||||
expect(check_unpack_integer<std::int16_t, std::int16_t>());
|
|
||||||
expect(check_unpack_integer<std::int32_t, std::int16_t>());
|
|
||||||
expect(check_unpack_integer<std::int64_t, std::int16_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"unpacker::unpack<std::int32_t>"_test = [] {
|
|
||||||
expect(check_unpack_integer<std::int8_t, std::int32_t>());
|
|
||||||
expect(check_unpack_integer<std::int16_t, std::int32_t>());
|
|
||||||
expect(check_unpack_integer<std::int32_t, std::int32_t>());
|
|
||||||
expect(check_unpack_integer<std::int64_t, std::int32_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"unpacker::unpack<std::int64_t>"_test = [] {
|
|
||||||
expect(check_unpack_integer<std::int8_t, std::int64_t>());
|
|
||||||
expect(check_unpack_integer<std::int32_t, std::int64_t>());
|
|
||||||
expect(check_unpack_integer<std::int32_t, std::int64_t>());
|
|
||||||
expect(check_unpack_integer<std::int64_t, std::int64_t>());
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,61 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Default packer tests.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#include "test_unpacker.h"
|
|
||||||
|
|
||||||
#include <fmt/format.h>
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
namespace {} // anonymous namespace
|
|
||||||
|
|
||||||
suite unpacker_simple_types = [] {
|
|
||||||
"unpacker::unpack<invalid>"_test = [] {
|
|
||||||
tl::expected<int, msgpack::error> x(
|
|
||||||
tl::make_unexpected(msgpack::error::end_of_message));
|
|
||||||
static constexpr auto payload = test::make_bytes(0xc1, 0xc1);
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
expect(type_check_unpacker<msgpack::invalid>(unpacker));
|
|
||||||
expect(unpacker.unpack<msgpack::invalid>() == msgpack::invalid{});
|
|
||||||
expect(unpacker.unpack<msgpack::invalid>() == msgpack::invalid{});
|
|
||||||
expect(unpacker.unpack<msgpack::invalid>()
|
|
||||||
== tl::make_unexpected(msgpack::error::end_of_message));
|
|
||||||
};
|
|
||||||
|
|
||||||
"unpacker::unpack<nil>"_test = [] {
|
|
||||||
static constexpr auto payload = test::make_bytes(0xc0, 0xc0);
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
expect(type_check_unpacker<msgpack::nil>(unpacker));
|
|
||||||
expect(unpacker.unpack<msgpack::nil>() == msgpack::nil{});
|
|
||||||
expect(unpacker.unpack<msgpack::nil>() == msgpack::nil{});
|
|
||||||
expect(unpacker.unpack<msgpack::nil>()
|
|
||||||
== tl::make_unexpected(msgpack::error::end_of_message));
|
|
||||||
};
|
|
||||||
|
|
||||||
"unpacker::unpack<bool>"_test = [] {
|
|
||||||
constexpr auto payload = test::make_bytes(0xc3, 0xc2);
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
expect(type_check_unpacker<bool>(unpacker));
|
|
||||||
auto value = unpacker.unpack<bool>();
|
|
||||||
expect(value && *value == true);
|
|
||||||
value = unpacker.unpack<bool>();
|
|
||||||
expect(value && *value == false);
|
|
||||||
expect(unpacker.unpack<bool>()
|
|
||||||
== tl::make_unexpected(msgpack::error::end_of_message));
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,31 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Unpacker tests for strings.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#include "test_unpacker.h"
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
suite unpack_string_types = [] {
|
|
||||||
"unpacker::unpack<std::string_view>"_test = [] {
|
|
||||||
static constexpr auto payload = test::make_bytes(0xa2, 'H', 'i');
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
expect(type_check_unpacker<std::string_view>(unpacker));
|
|
||||||
expect(unpacker.unpack<std::string_view>() == "Hi");
|
|
||||||
expect(unpacker.unpack<msgpack::invalid>()
|
|
||||||
== tl::make_unexpected(msgpack::error::end_of_message));
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,57 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Unpacker tests, common code.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#ifndef tests_msgpack_unpacker_25b118dd14e2b1b4
|
|
||||||
#define tests_msgpack_unpacker_25b118dd14e2b1b4
|
|
||||||
|
|
||||||
#include "parselink/msgpack/core/unpacker.h"
|
|
||||||
|
|
||||||
#include "test_utils.h"
|
|
||||||
|
|
||||||
// Helper template to check that a type is either equal to itself, or verify
|
|
||||||
// that it won't unpack correctly.
|
|
||||||
template <typename T, typename U>
|
|
||||||
constexpr bool unpack_type_check(msgpack::unpacker unpacker) noexcept {
|
|
||||||
auto wrong_type = tl::make_unexpected(msgpack::error::wrong_type);
|
|
||||||
auto compatible_type = std::same_as<T, U>
|
|
||||||
|| (std::is_unsigned_v<T> && std::is_unsigned_v<U>)
|
|
||||||
|| (std::is_signed_v<T> && std::is_signed_v<U>);
|
|
||||||
auto result = compatible_type || unpacker.unpack<U>() == wrong_type;
|
|
||||||
if (!result) {
|
|
||||||
fmt::print("Unpack_type_check failed: {}\n",
|
|
||||||
std::source_location::current().function_name());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate the wrong types do not unpack correctly, or fail.
|
|
||||||
template <typename Expected, typename... OtherTypes>
|
|
||||||
constexpr bool try_unpacking_types(msgpack::unpacker unpacker) noexcept {
|
|
||||||
return ((unpack_type_check<Expected, OtherTypes>(unpacker)) && ...);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define SUPPORTED_TYPES \
|
|
||||||
msgpack::nil, msgpack::invalid, bool, std::uint8_t, std::uint16_t, \
|
|
||||||
std::uint32_t, std::uint64_t, std::int8_t, std::int16_t, \
|
|
||||||
std::int32_t, std::int64_t, std::string_view
|
|
||||||
|
|
||||||
template <typename Expected>
|
|
||||||
constexpr bool type_check_unpacker(msgpack::unpacker unpacker) noexcept {
|
|
||||||
return try_unpacking_types<Expected, SUPPORTED_TYPES>(unpacker);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // tests_msgpack_unpacker_25b118dd14e2b1b4
|
|
||||||
@ -1,131 +0,0 @@
|
|||||||
//-----------------------------------------------------------------------------
|
|
||||||
// ___ __ _ _
|
|
||||||
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
||||||
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
||||||
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
||||||
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
||||||
//
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Author: Kurt Sassenrath
|
|
||||||
// Module: msgpack
|
|
||||||
//
|
|
||||||
// Default packer tests.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2023 Kurt Sassenrath.
|
|
||||||
//
|
|
||||||
// License TBD.
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#include "test_unpacker.h"
|
|
||||||
|
|
||||||
#include <rapidcheck.h>
|
|
||||||
|
|
||||||
using namespace boost::ut;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
constexpr auto equal(auto a, auto b) {
|
|
||||||
return std::equal(std::begin(a), std::end(a), std::begin(b), std::end(b));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... Bytes>
|
|
||||||
constexpr std::array<std::byte, sizeof...(Bytes)> make_bytes(Bytes&&... bytes) {
|
|
||||||
return {std::byte(std::forward<Bytes>(bytes))...};
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, typename U>
|
|
||||||
constexpr bool test_unpack(msgpack::unpacker unpacker) noexcept {
|
|
||||||
auto wrong_type = tl::make_unexpected(msgpack::error::wrong_type);
|
|
||||||
return std::same_as<T, U> || unpacker.unpack<U>() == wrong_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Expected, typename... OtherTypes>
|
|
||||||
constexpr bool test_types(msgpack::unpacker unpacker) noexcept {
|
|
||||||
return ((test_unpack<Expected, OtherTypes>(unpacker)) && ...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Expected>
|
|
||||||
constexpr bool expect_invalid(msgpack::unpacker unpacker) noexcept {
|
|
||||||
return test_types<Expected, msgpack::nil, msgpack::invalid, bool,
|
|
||||||
std::uint8_t, std::string_view>(unpacker);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
struct get_marker;
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct get_marker<std::uint8_t> {
|
|
||||||
static constexpr std::byte marker = msgpack::format::uint8::marker;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct get_marker<std::uint16_t> {
|
|
||||||
static constexpr std::byte marker = msgpack::format::uint16::marker;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct get_marker<std::uint32_t> {
|
|
||||||
static constexpr std::byte marker = msgpack::format::uint32::marker;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct get_marker<std::uint64_t> {
|
|
||||||
static constexpr std::byte marker = msgpack::format::uint64::marker;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Utilize rapidcheck to generate random packed payloads, and unpacking the
|
|
||||||
// correct value
|
|
||||||
template <typename PackType, typename UnpackType>
|
|
||||||
auto check_unpack_integer() noexcept {
|
|
||||||
auto result = rc::check([](PackType value) {
|
|
||||||
std::vector<std::byte> payload = {get_marker<PackType>::marker};
|
|
||||||
auto bytes = host_to_be(
|
|
||||||
std::bit_cast<std::array<std::byte, sizeof(PackType)>>(value));
|
|
||||||
std::copy(bytes.begin(), bytes.end(), std::back_inserter(payload));
|
|
||||||
msgpack::unpacker unpacker(payload);
|
|
||||||
auto unpacked_value = unpacker.unpack<UnpackType>();
|
|
||||||
if (std::numeric_limits<UnpackType>::max() < value
|
|
||||||
|| std::numeric_limits<UnpackType>::min() > value) {
|
|
||||||
return unpacked_value
|
|
||||||
== tl::make_unexpected(msgpack::error::will_truncate);
|
|
||||||
} else {
|
|
||||||
return unpacked_value.has_value() && unpacked_value == value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (!result) {
|
|
||||||
fmt::print("Failed on {}\n",
|
|
||||||
std::source_location::current().function_name());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // anonymous namespace
|
|
||||||
|
|
||||||
suite unpack_unsigned_types = [] {
|
|
||||||
"unpacker::unpack<std::uint8_t>"_test = [] {
|
|
||||||
expect(check_unpack_integer<std::uint8_t, std::uint8_t>());
|
|
||||||
expect(check_unpack_integer<std::uint16_t, std::uint8_t>());
|
|
||||||
expect(check_unpack_integer<std::uint32_t, std::uint8_t>());
|
|
||||||
expect(check_unpack_integer<std::uint64_t, std::uint8_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"unpacker::unpack<std::uint16_t>"_test = [] {
|
|
||||||
expect(check_unpack_integer<std::uint8_t, std::uint16_t>());
|
|
||||||
expect(check_unpack_integer<std::uint16_t, std::uint16_t>());
|
|
||||||
expect(check_unpack_integer<std::uint32_t, std::uint16_t>());
|
|
||||||
expect(check_unpack_integer<std::uint64_t, std::uint16_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"unpacker::unpack<std::uint32_t>"_test = [] {
|
|
||||||
expect(check_unpack_integer<std::uint8_t, std::uint32_t>());
|
|
||||||
expect(check_unpack_integer<std::uint16_t, std::uint32_t>());
|
|
||||||
expect(check_unpack_integer<std::uint32_t, std::uint32_t>());
|
|
||||||
expect(check_unpack_integer<std::uint64_t, std::uint32_t>());
|
|
||||||
};
|
|
||||||
|
|
||||||
"unpacker::unpack<std::uint64_t>"_test = [] {
|
|
||||||
expect(check_unpack_integer<std::uint8_t, std::uint64_t>());
|
|
||||||
expect(check_unpack_integer<std::uint16_t, std::uint64_t>());
|
|
||||||
expect(check_unpack_integer<std::uint32_t, std::uint64_t>());
|
|
||||||
expect(check_unpack_integer<std::uint64_t, std::uint64_t>());
|
|
||||||
};
|
|
||||||
};
|
|
||||||
Loading…
Reference in New Issue
Block a user