From 9ef58215a3176e69ee56dd7bb106b8ed38b7ab0a Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 14:45:01 -0300 Subject: [PATCH 01/30] codecs: a value codec may state max_decode_input_size, the longest input its decode accepts, never below max_encoded_size; the core refuses a longer input on every path --- .../webcpp/codecs/detail/core/contract.hpp | 36 ++ .../webcpp/codecs/detail/core/operations.hpp | 11 +- include/webcpp/codecs/detail/core/secrecy.hpp | 44 ++ .../codecs/detail/core/size_maximum.hpp | 13 +- include/webcpp/codecs/detail/core/tokens.hpp | 8 +- test/Jamfile | 8 +- test/messages/Jamfile | 1 + .../decode_bound_below_encoded/Jamfile | 16 + .../decode_bound_below_encoded.cpp | 82 ++++ test/support/bounded_records.hpp | 331 +++++++++++++++ test/typed_decode_test.cpp | 390 ++++++++++++++++++ test/value_codec_test.cpp | 51 +++ 12 files changed, 974 insertions(+), 17 deletions(-) create mode 100644 test/messages/decode_bound_below_encoded/Jamfile create mode 100644 test/messages/decode_bound_below_encoded/decode_bound_below_encoded.cpp create mode 100644 test/support/bounded_records.hpp diff --git a/include/webcpp/codecs/detail/core/contract.hpp b/include/webcpp/codecs/detail/core/contract.hpp index 286f66b..067ca11 100644 --- a/include/webcpp/codecs/detail/core/contract.hpp +++ b/include/webcpp/codecs/detail/core/contract.hpp @@ -139,6 +139,8 @@ struct codec; static constexpr codecs::output_kind output = codecs::output_kind::bytes; static constexpr bool secret = true; // optional; false when absent static constexpr std::size_t max_encoded_size = 138; // optional; a fixed result when present + // optional; the longest input decode accepts + static constexpr std::size_t max_decode_input_size = 138; static std::size_t encoded_size(crypto::formats::pkcs8_t const& format_value, crypto::curves::p256::keypair const& held_value) noexcept; static result encode(crypto::formats::pkcs8_t const& format_value, @@ -150,6 +152,15 @@ struct codec; }; @endcode + `max_decode_input_size`, optional, is the longest input `decode` accepts, in the elements it reads + (bytes, or characters for a value codec of text): its acceptance contract, which the core + enforces on every path of a decode, the typed `decode` and `over`'s call of the value + codec alike, by refusing a longer input with @ref error::input_too_long before the value codec is + called, after the format's configuration fault; an input of exactly the bound reaches it, and a + bound below the longest valid input refuses that input everywhere. A value codec that states both + bounds with `max_decode_input_size` below `max_encoded_size` stops the build: its decode would + refuse its own longest encoding. + A value codec whose `output` is @ref output_kind::text writes a `std::span` and reads a `std::string_view`. The errors it returns, of any category, reach the caller unchanged. Its `decode` takes `std::span` or `std::string_view`, not a deduced parameter, as @@ -555,6 +566,31 @@ using value_input_view = std::conditional_t::output == output_kind::bytes, std::span, std::string_view>; +/** Whether `Codec`, a value codec, states `max_encoded_size`: its result is a fixed one. */ +template +concept has_encoded_bound = requires { + { Codec::max_encoded_size } -> std::convertible_to; +}; + +/** Whether `Codec`, a value codec, states `max_decode_input_size`: the longest input it decodes. */ +template +concept has_decode_input_bound = requires { + { Codec::max_decode_input_size } -> std::convertible_to; +}; + +/** + The decode bound `value_codec` states, its `max_decode_input_size`, or + `std::dynamic_extent` (equal to `SIZE_MAX`, which bounds nothing) when it states none. +*/ +template +[[nodiscard]] consteval std::size_t value_decode_bound() noexcept { + if constexpr (has_decode_input_bound>) { + return value_codec::max_decode_input_size; + } else { + return std::dynamic_extent; + } +} + /** Whether `value_codec` is specialized with `output`, `encoded_size` and `encode`. */ template concept value_encodable = diff --git a/include/webcpp/codecs/detail/core/operations.hpp b/include/webcpp/codecs/detail/core/operations.hpp index dc1a15b..ebd6a17 100644 --- a/include/webcpp/codecs/detail/core/operations.hpp +++ b/include/webcpp/codecs/detail/core/operations.hpp @@ -58,12 +58,6 @@ inline constexpr bool is_fixed_result> = true; template inline constexpr bool is_fixed_result> = true; -/** Whether `Codec`, a value codec, states `max_encoded_size`: its result is a fixed one. */ -template -concept has_encoded_bound = requires { - { Codec::max_encoded_size } -> std::convertible_to; -}; - /** Whether `Input` is a value that a value codec of `F` encodes into bytes. */ template constexpr bool encodes_to_bytes() noexcept { @@ -1002,7 +996,10 @@ template to `N` bytes (@ref error::output_too_small otherwise); a `std::string`, a `std::vector`, a `basic_secret_string` or a `basic_secret_byte_vector`, which take the heap. A fault of the format's configuration is returned first, before any data, in every - case (its codec's `configuration_fault`, or a chain's links'). It is `noexcept` for the fixed + case (its codec's `configuration_fault`, or a chain's links'). A value codec that states + `max_decode_input_size` then refuses a longer input with @ref error::input_too_long before it is + called, whatever the input's type, public or secret (owner decision 29); an input whose size known + at compile time is at most the bound skips the check. It is `noexcept` for the fixed byte results, and for a value codec as the value codec's `decode` is declared; the heap types allocate and are not. The operation is secret (D10) when `Value`'s `is_secret_v` or the value codec's `secret` says so: it then holds secure's timing diff --git a/include/webcpp/codecs/detail/core/secrecy.hpp b/include/webcpp/codecs/detail/core/secrecy.hpp index 33a23a0..af0f8c9 100644 --- a/include/webcpp/codecs/detail/core/secrecy.hpp +++ b/include/webcpp/codecs/detail/core/secrecy.hpp @@ -487,6 +487,34 @@ constexpr void check_secret_value_overloads() noexcept { "for decode"); } +/** + Whether the two bounds of `value_codec` are in order: true unless it states both and its + `max_decode_input_size` is below its `max_encoded_size`, the requirement of the check below. +*/ +template +inline constexpr bool value_bounds_ordered = [] { + if constexpr (has_encoded_bound> && + has_decode_input_bound>) { + return std::size_t{value_codec::max_decode_input_size} >= + std::size_t{value_codec::max_encoded_size}; + } else { + return true; + } +}(); + +/** + Stops the build when `value_codec` states a `max_decode_input_size` below its + `max_encoded_size`: its decode would refuse its own longest encoding (ruling R2). Every encode + and decode of the pair checks it, `over`'s of it included. +*/ +template +constexpr void check_value_bounds() noexcept { + // Qualified, so that the compiler's message names the requirement as the core's. + static_assert(webcpp::codecs::detail::value_bounds_ordered, + "webcpp::codecs: a value codec's max_decode_input_size is below its " + "max_encoded_size, so its decode would refuse its own longest encoding"); +} + /** Whether the call of `value_codec::encoded_size` that @ref value_encoded_size makes cannot throw, as the value codec declares it. @@ -540,6 +568,7 @@ template F const& format_value, V const& held_value) noexcept(value_encoded_size_is_noexcept()) { check_secret_value_overloads(); + check_value_bounds(); if constexpr (Secret && has_secret_value_encoded_size) { return value_codec::encoded_size(secret_tag{}, format_value, held_value); } else { @@ -556,6 +585,7 @@ template F const& format_value, V const& held_value, value_output_span output_span) noexcept(value_encode_is_noexcept()) { check_secret_value_overloads(); + check_value_bounds(); if constexpr (Secret && has_secret_value_encode) { return value_codec::encode(secret_tag{}, format_value, held_value, output_span); } else { @@ -567,11 +597,25 @@ template The value `input_view` decodes to: the value codec's secret overload in a secret operation when it has one (R7), its `decode` otherwise. `input_view` is the bytes or the characters as the core gives them, a @ref bounded_bytes or a @ref bounded_text when their type states a bound. + + It is the one funnel of every value decode, the typed decode of bytes and of text and `over`'s + call of its inner value codec, so that the value codec's decode bound is enforced on every path + (owner decision 29): an input longer than the value codec's `max_decode_input_size` is + @ref error::input_too_long, the value codec not called; a view whose bound known at compile time + is at most the decode bound skips the check. A secret refusal is made inside the caller's timing + guard and reads the input's length alone. */ template [[nodiscard]] constexpr result value_decode(F const& format_value, View input_view) noexcept( value_decode_is_noexcept()) { check_secret_value_overloads(); + check_value_bounds(); + if constexpr (has_decode_input_bound> && + view_bound_v > value_decode_bound()) { + if (input_view.size() > value_decode_bound()) { + return make_error_code(error::input_too_long); + } + } if constexpr (Secret && has_secret_value_decode) { return value_codec::decode(secret_tag{}, format_value, input_view); } else { diff --git a/include/webcpp/codecs/detail/core/size_maximum.hpp b/include/webcpp/codecs/detail/core/size_maximum.hpp index d1d7bb7..7eaf7c1 100644 --- a/include/webcpp/codecs/detail/core/size_maximum.hpp +++ b/include/webcpp/codecs/detail/core/size_maximum.hpp @@ -6,7 +6,8 @@ /** The largest `std::size_t`, the bound of every size of webcpp::codecs that does not fit, written - once so that a bare `` before a header of codecs breaks none of them. + once so that a bare `` before a header of codecs breaks none of them; and the most + elements the core holds in place, which the core's headers share. */ #ifndef WEBCPP_CODECS_DETAIL_CORE_SIZE_MAXIMUM_HPP #define WEBCPP_CODECS_DETAIL_CORE_SIZE_MAXIMUM_HPP @@ -26,6 +27,16 @@ namespace webcpp::codecs::detail { // NOLINTNEXTLINE(readability-redundant-parentheses): they stop 's macro max inline constexpr std::size_t size_maximum = (std::numeric_limits::max)(); +/** + The most elements the core holds in place of a bound known at compile time: a scratch of an + operation into `assign_to`, and an intermediate of `over` sized by a value codec's decode bound + (`max_decode_input_size`); past it, or when the bound is not known at compile time, the scratch + is a container of the target's allocator and the intermediate comes from `over`'s allocator (the + owner's ruling of 2026-11-26, design R5 of one-verb), so that a format's bound never costs every + caller a large stack. +*/ +inline constexpr std::size_t in_place_scratch_limit = 4096; + } // namespace webcpp::codecs::detail #endif // WEBCPP_CODECS_DETAIL_CORE_SIZE_MAXIMUM_HPP diff --git a/include/webcpp/codecs/detail/core/tokens.hpp b/include/webcpp/codecs/detail/core/tokens.hpp index cb372f5..459bc60 100644 --- a/include/webcpp/codecs/detail/core/tokens.hpp +++ b/include/webcpp/codecs/detail/core/tokens.hpp @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -240,13 +241,6 @@ class appending_token { std::size_t old_size_ = 0; }; -/** - The most elements of a scratch in place of an operation into `assign_to`: past it, or when the - bound is not known at compile time, the scratch is a container of the target's allocator (the - owner's ruling). -*/ -inline constexpr std::size_t in_place_scratch_limit = 4096; - /** A token that replaces the content of a string or a vector with the result, and leaves it as it was when the operation fails (the owner's ruling): the conversion writes into a scratch, and the diff --git a/test/Jamfile b/test/Jamfile index b77d182..7185c5b 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -216,9 +216,13 @@ codecs-run base58_buffered : base58_buffered_test.cpp codecs-run interoperation : interoperation_test.cpp ; # The default result of encode and decode without a token (D9). codecs-run default_result : default_result_test.cpp ; -# The typed decode into fixed byte types, the heap and a value codec's value. +# The typed decode into fixed byte types, the heap and a value codec's value; +# a value codec's decode bound refuses a longer input with input_too_long +# before the value codec is called, from every buffer, public or secret (the +# guard held, as secure's seam, which the test's header defines, records it). codecs-run typed_decode : typed_decode_test.cpp ; -# Value codecs: the concepts, the default result, the buffer forms. +# Value codecs: the concepts, the default result, the buffer forms, the decode +# bound the core reads and the order of the two bounds. codecs-run value_codec : value_codec_test.cpp ; # The tokens: codecs' own tags in both directions, a token of the test's own, # capacities, a value codec through a token. diff --git a/test/messages/Jamfile b/test/messages/Jamfile index 82bf9e0..af50d4f 100644 --- a/test/messages/Jamfile +++ b/test/messages/Jamfile @@ -23,6 +23,7 @@ build-project refused_allocator ; build-project refused_secret_overload ; build-project refused_secret_parameters ; build-project refused_secret_value_overload ; +build-project decode_bound_below_encoded ; build-project refused_configuration_fault ; build-project refused_member_configuration_fault ; build-project refused_encode_fault ; diff --git a/test/messages/decode_bound_below_encoded/Jamfile b/test/messages/decode_bound_below_encoded/Jamfile new file mode 100644 index 0000000..a4d27a1 --- /dev/null +++ b/test/messages/decode_bound_below_encoded/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic decode_bound_below_encoded : decode_bound_below_encoded.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail decode_bound_below_encoded_refused : decode_bound_below_encoded.cpp + : clang:no : native emscripten ; diff --git a/test/messages/decode_bound_below_encoded/decode_bound_below_encoded.cpp b/test/messages/decode_bound_below_encoded/decode_bound_below_encoded.cpp new file mode 100644 index 0000000..f26130d --- /dev/null +++ b/test/messages/decode_bound_below_encoded/decode_bound_below_encoded.cpp @@ -0,0 +1,82 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A value codec that states both bounds with its max_decode_input_size (8) below its +// max_encoded_size (12) stops the build in the core's check (ruling R2), whose requirement names +// the format's type and the value's: its decode would refuse its own longest encoding. + +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/secrecy.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::value_bounds_ordered': webcpp::codecs: a value codec's max_decode_input_size is below its max_encoded_size, so its decode would refuse its own longest encoding}} +// clang-format on + +#include + +#include +#include +#include +#include +#include +#include + +/** A format whose value codec's decode bound is below its encoded bound. */ +struct inverted_t { + /** The format's name. */ + static constexpr std::string_view name = "inverted"; +}; + +/** The value: 12 bytes. */ +struct record { + /** The bytes. */ + std::array held_bytes{}; +}; + +namespace webcpp::codecs { + +/** Encodes 12 bytes, and states that it decodes at most 8. */ +template <> +struct value_codec { + /** Bytes. */ + static constexpr output_kind output = output_kind::bytes; + /** The 12 bytes. */ + static constexpr std::size_t max_encoded_size = 12; + /** Below the encoded bound. */ + static constexpr std::size_t max_decode_input_size = 8; + + /** The 12 bytes. */ + static std::size_t encoded_size(inverted_t const& /*format*/, + record const& /*held_value*/) noexcept { + return 12; + } + + /** Writes the 12 bytes. */ + static result encode(inverted_t const& /*format*/, record const& held_value, + std::span output_bytes) noexcept { + std::ranges::copy(held_value.held_bytes, output_bytes.begin()); + return std::size_t{12}; + } + + /** Reads the 12 bytes. */ + static result decode(inverted_t const& /*format*/, + std::span input_bytes) noexcept { + if (input_bytes.size() != 12) { + return error::invalid_length; + } + record decoded; + std::ranges::copy(input_bytes, decoded.held_bytes.begin()); + return decoded; + } +}; + +} // namespace webcpp::codecs + +int main() { + namespace codecs = webcpp::codecs; + std::array const encoding{}; + auto const decoded = codecs::decode(inverted_t{}, encoding); + return decoded.has_value() ? 0 : 1; +} diff --git a/test/support/bounded_records.hpp b/test/support/bounded_records.hpp new file mode 100644 index 0000000..c366ee7 --- /dev/null +++ b/test/support/bounded_records.hpp @@ -0,0 +1,331 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// bounded, a format of the tests' own whose value codecs state a decode bound +// (max_decode_input_size), the shape of crypto's PKCS#8 version 1 and the version 2 it defers. A +// short_record is 8 bytes, encoded as the 12-byte form `30 0a 02 00` and the 8 bytes; its decode +// also accepts the 16-byte form `30 0e 02 01`, the 8 bytes, `a1 02` and the first 2 of the 8 bytes +// again, so that its decode bound (16) is above its encoded bound (12); any other length is +// invalid_length, any other byte invalid_prefix. A sealed_record is the same, secret by its value +// codec. A tight_record is the same with a decode bound of 15, one below its largest valid input, +// so that its 16-byte form is refused on every path (the bound is the acceptance contract). A +// wide_record is the count of up to 4097 bytes, decode only, with a bound past +// detail::in_place_scratch_limit; a sealed_wide_record is the same, secret. A short_text is up to +// 20 letters, by a value codec of text whose bounds are both 20. Every function is noexcept, and +// every decode counts its calls in decode_calls, so that a test sees whether the core +// called the value codec. + +#ifndef WEBCPP_CODECS_TEST_SUPPORT_BOUNDED_RECORDS_HPP +#define WEBCPP_CODECS_TEST_SUPPORT_BOUNDED_RECORDS_HPP + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace webcpp::codecs::test { + +/** The type of the format bounded. */ +struct bounded_t { + /** The format's name. */ + static constexpr std::string_view name = "bounded"; +}; + +/** The format bounded. */ +inline constexpr bounded_t bounded{}; + +/** A record of 8 bytes. */ +struct short_record { + /** The bytes. */ + std::array held_bytes{}; + + /** Equal when the bytes are. */ + bool operator==(short_record const&) const = default; +}; + +/** A short_record whose value codec is secret: its bytes in a `secure::secret<8>`. */ +struct sealed_record { + /** The bytes. */ + secure::secret<8> held_bytes; +}; + +/** A short_record whose value codec's decode bound (15) is below its largest valid input (16). */ +struct tight_record { + /** The bytes. */ + std::array held_bytes{}; + + /** Equal when the bytes are. */ + bool operator==(tight_record const&) const = default; +}; + +/** The count of up to 4097 bytes, decoded only. */ +struct wide_record { + /** The count. */ + std::size_t count = 0; +}; + +/** A wide_record whose value codec is secret. */ +struct sealed_wide_record { + /** The count. */ + std::size_t count = 0; +}; + +/** Up to 20 letters. */ +struct short_text { + /** The letters, those past `size` zero. */ + std::array letters{}; + /** The number of letters. */ + std::size_t size = 0; + + /** The letters. */ + [[nodiscard]] std::string_view view() const noexcept { return {letters.data(), size}; } + + /** Equal when the letters are. */ + bool operator==(short_text const& other_text) const noexcept { + return view() == other_text.view(); + } +}; + +/** The calls of the decode of `Value`'s value codec since the test last set it to 0. */ +template +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the tests' counter +inline std::size_t decode_calls = 0; + +/** The 8 bytes of a record, to write. */ +inline std::span record_bytes(short_record& record) noexcept { + return record.held_bytes; +} + +/** The 8 bytes of a record, to write. */ +inline std::span record_bytes(sealed_record& record) noexcept { + return record.held_bytes.writable(); +} + +/** The 8 bytes of a record, to write. */ +inline std::span record_bytes(tight_record& record) noexcept { + return record.held_bytes; +} + +/** The 8 bytes of a record, to read. */ +inline std::span record_bytes(short_record const& record) noexcept { + return record.held_bytes; +} + +/** The 8 bytes of a record, to read. */ +inline std::span record_bytes(sealed_record const& record) noexcept { + return record.held_bytes.bytes(); +} + +/** The 8 bytes of a record, to read. */ +inline std::span record_bytes(tight_record const& record) noexcept { + return record.held_bytes; +} + +/** The size of a record's form of version 1: the 4-byte prefix and the 8 bytes. */ +inline constexpr std::size_t record_version_1_size = 12; + +/** The size of a record's form of version 2: version 1's, `a1 02` and 2 bytes. */ +inline constexpr std::size_t record_version_2_size = 16; + +/** + Reads a record's form of version 1 or 2 into `held_bytes`. + + @param input_bytes The form. + @param held_bytes The record's 8 bytes. + @return invalid_length for another length, invalid_prefix for another byte, else nothing. +*/ +inline std::optional read_record(std::span input_bytes, + std::span held_bytes) noexcept { + constexpr std::array version_1_prefix{0x30, 0x0a, 0x02, 0x00}; + constexpr std::array version_2_prefix{0x30, 0x0e, 0x02, 0x01}; + auto const prefix = input_bytes.first(std::min(input_bytes.size(), 4)); + if (input_bytes.size() == record_version_1_size) { + if (!std::ranges::equal(prefix, version_1_prefix)) { + return error::invalid_prefix; + } + } else if (input_bytes.size() == record_version_2_size) { + if (!std::ranges::equal(prefix, version_2_prefix) || input_bytes[12] != 0xa1 || + input_bytes[13] != 0x02 || input_bytes[14] != input_bytes[4] || + input_bytes[15] != input_bytes[5]) { + return error::invalid_prefix; + } + } else { + return error::invalid_length; + } + std::ranges::copy(input_bytes.subspan(4, 8), held_bytes.begin()); + return std::nullopt; +} + +/** + The value codec of bounded for a record of 8 bytes, which each record's specialization states the + decode bound and the secrecy of. + + @tparam Record short_record, sealed_record or tight_record. +*/ +template +struct record_codec { + /** Bytes. */ + static constexpr output_kind output = output_kind::bytes; + /** The form of version 1. */ + static constexpr std::size_t max_encoded_size = record_version_1_size; + + /** The form of version 1. */ + static std::size_t encoded_size(bounded_t const& /*format*/, + Record const& /*held_value*/) noexcept { + return record_version_1_size; + } + + /** Writes the form of version 1 into `output_bytes`, which must hold it. */ + static result encode(bounded_t const& /*format*/, Record const& held_value, + std::span output_bytes) noexcept { + if (output_bytes.size() < record_version_1_size) { + return error::output_too_small; + } + constexpr std::array version_1_prefix{0x30, 0x0a, 0x02, 0x00}; + std::ranges::copy(version_1_prefix, output_bytes.begin()); + std::ranges::copy(record_bytes(held_value), output_bytes.subspan(4).begin()); + return record_version_1_size; + } + + /** Reads the form of version 1 or 2, and counts the call. */ + static result decode(bounded_t const& /*format*/, + std::span input_bytes) noexcept { + ++decode_calls; + Record decoded{}; + if (auto const refusal = read_record(input_bytes, record_bytes(decoded))) { + return *refusal; + } + return decoded; + } +}; + +/** The most bytes a wide record counts. */ +inline constexpr std::size_t wide_record_bound = 4097; + +/** + The value codec of bounded for a count of bytes, decode only, which each record's + specialization states the secrecy of. + + @tparam Record wide_record or sealed_wide_record. +*/ +template +struct wide_codec { + /** Bytes. */ + static constexpr output_kind output = output_kind::bytes; + /** The most bytes it counts. */ + static constexpr std::size_t max_decode_input_size = wide_record_bound; + + /** The count of the bytes: any up to 4097, longer invalid_length; counts the call. */ + static result decode(bounded_t const& /*format*/, + std::span input_bytes) noexcept { + ++decode_calls; + if (input_bytes.size() > wide_record_bound) { + return error::invalid_length; + } + return Record{input_bytes.size()}; + } +}; + +/** The most letters of a short text. */ +inline constexpr std::size_t short_text_bound = 20; + +/** Whether `character` is an ASCII letter. */ +constexpr bool is_letter(char character) noexcept { + return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'); +} + +} // namespace webcpp::codecs::test + +namespace webcpp::codecs { + +/** A short record: the forms of version 1 and 2, a decode bound of 16. */ +template <> +struct value_codec : test::record_codec { + /** The form of version 2. */ + static constexpr std::size_t max_decode_input_size = test::record_version_2_size; +}; + +/** A sealed record: a short record's codec, secret. */ +template <> +struct value_codec : test::record_codec { + /** The value is secret. */ + static constexpr bool secret = true; + /** The form of version 2. */ + static constexpr std::size_t max_decode_input_size = test::record_version_2_size; +}; + +/** A tight record: a short record's codec, whose decode bound is one below its largest form. */ +template <> +struct value_codec : test::record_codec { + /** One below the form of version 2, which is refused on every path. */ + static constexpr std::size_t max_decode_input_size = test::record_version_2_size - 1; +}; + +/** A wide record: decode only, a bound of 4097. */ +template <> +struct value_codec : test::wide_codec {}; + +/** A sealed wide record: a wide record's codec, secret. */ +template <> +struct value_codec + : test::wide_codec { + /** The value is secret. */ + static constexpr bool secret = true; +}; + +/** A short text: up to 20 letters, both bounds 20. */ +template <> +struct value_codec { + /** Text. */ + static constexpr output_kind output = output_kind::text; + /** The most letters. */ + static constexpr std::size_t max_encoded_size = test::short_text_bound; + /** The most letters. */ + static constexpr std::size_t max_decode_input_size = test::short_text_bound; + + /** The number of letters. */ + static std::size_t encoded_size(test::bounded_t const& /*format*/, + test::short_text const& held_value) noexcept { + return held_value.size; + } + + /** Writes the letters into `output_characters`, which must hold them. */ + static result encode(test::bounded_t const& /*format*/, + test::short_text const& held_value, + std::span output_characters) noexcept { + if (output_characters.size() < held_value.size) { + return error::output_too_small; + } + std::ranges::copy(held_value.view(), output_characters.begin()); + return held_value.size; + } + + /** Reads up to 20 letters: longer is invalid_length, another character invalid_character. */ + static result decode(test::bounded_t const& /*format*/, + std::string_view text_value) noexcept { + ++test::decode_calls; + if (text_value.size() > test::short_text_bound) { + return error::invalid_length; + } + if (!std::ranges::all_of(text_value, test::is_letter)) { + return error::invalid_character; + } + test::short_text decoded{}; + std::ranges::copy(text_value, decoded.letters.begin()); + decoded.size = text_value.size(); + return decoded; + } +}; + +} // namespace webcpp::codecs + +#endif // WEBCPP_CODECS_TEST_SUPPORT_BOUNDED_RECORDS_HPP diff --git a/test/typed_decode_test.cpp b/test/typed_decode_test.cpp index 929e670..2e962d6 100644 --- a/test/typed_decode_test.cpp +++ b/test/typed_decode_test.cpp @@ -6,8 +6,20 @@ // The typed decode: into std::array (exactly N bytes), bytes (up to N), a library's fixed type // (exactly its size), the heap types, and a value of a value codec. +// The decode bound (the spec's requirement "Contract: the decode bound is enforced on every +// path"): a value codec's max_decode_input_size refuses a longer input with input_too_long before +// the value codec is called, and an input of exactly the bound reaches it, whatever the caller's +// buffer holding the input (a span of a dynamic or a static extent, a vector, an array, a byte +// array, bytes, a secure::secret, a string view and text for a value codec of text), public +// or secret (by the value codec, or by the requested type), secure's timing seam, recorded by +// support/timing_seam.hpp, counting one guard of codecs' for each secret call, refusal included; a +// fault of the format's configuration first; a bound below the largest valid input refuses it. A +// value decode has no buffer form of its own (design D3): the buffer is the caller's input. + +#include "support/timing_seam.hpp" #include "support/be16.hpp" +#include "support/bounded_records.hpp" #include "support/letters.hpp" #include "support/saturating.hpp" #include "support/shrinking.hpp" @@ -21,14 +33,17 @@ #include #include #include +#include #include #include #include +#include #include namespace { namespace codecs = webcpp::codecs; +namespace secure = webcpp::secure; namespace test = webcpp::codecs::test; /** A value whose value codec allocates: its decode is not `noexcept`. */ @@ -43,6 +58,47 @@ struct key16 { std::array held_bytes{}; }; +/** A short_record the test requests as a secret type: its value codec is public. */ +struct vaulted_record { + /** The record. */ + test::short_record record; +}; + +/** A run of letters `q`, which the test encodes into a text<21> of the length it needs. */ +struct letter_run { + /** The number of letters, at most 21. */ + std::size_t count = 0; +}; + +/** The format of a letter run. */ +struct spelled_t { + /** The format's name. */ + static constexpr std::string_view name = "spelled"; +}; + +/** The format of a letter run. */ +inline constexpr spelled_t spelled{}; + +} // namespace + +// Out of the anonymous namespace, so that its name, which no test reads, is not an unused variable. +namespace typed_decode { + +/** A format whose configuration may be at fault, with a value codec of a short_record. */ +struct misconfigured_t : webcpp::codecs::test::whole_letters_t { + /** The format's name. */ + static constexpr std::string_view name = "misconfigured"; + + /** Whether the format's configuration is at fault. */ + bool at_fault = true; +}; + +} // namespace typed_decode + +namespace { + +using typed_decode::misconfigured_t; + } // namespace namespace webcpp::codecs { @@ -82,6 +138,85 @@ struct fixed_byte_storage { } }; +/** A vaulted record is secret: its decode is secret by the requested type. */ +template <> +struct is_secret : std::true_type {}; + +/** A vaulted record: a short record's public value codec, its bound included. */ +template <> +struct value_codec { + /** Bytes. */ + static constexpr output_kind output = output_kind::bytes; + /** A short record's. */ + static constexpr std::size_t max_decode_input_size = + value_codec::max_decode_input_size; + + /** A short record's decode, which counts its calls. */ + static result decode(test::bounded_t const& format_value, + std::span input_bytes) noexcept { + auto decoded = + value_codec::decode(format_value, input_bytes); + if (!decoded.has_value()) { + return decoded.error(); + } + return vaulted_record{decoded.value()}; + } +}; + +/** A letter run: up to 21 letters `q`, encode only. */ +template <> +struct value_codec { + /** Text. */ + static constexpr output_kind output = output_kind::text; + /** The most letters: the result is a text<21>. */ + static constexpr std::size_t max_encoded_size = 21; + + /** The number of letters. */ + static std::size_t encoded_size(spelled_t const& /*format*/, + letter_run const& held_value) noexcept { + return held_value.count; + } + + /** Writes the letters into `output_characters`, which must hold them. */ + static result encode(spelled_t const& /*format*/, letter_run const& held_value, + std::span output_characters) noexcept { + if (output_characters.size() < held_value.count) { + return error::output_too_small; + } + std::ranges::fill(output_characters.first(held_value.count), 'q'); + return held_value.count; + } +}; + +/** misconfigured is whole_letters' codec, whose configuration is at fault when the format says. */ +template <> +struct codec : codec { + /** invalid_prefix when the format is at fault, which the core reports before any data. */ + static constexpr std::optional configuration_fault( + misconfigured_t const& format_value) noexcept { + if (format_value.at_fault) { + return error::invalid_prefix; + } + return std::nullopt; + } +}; + +/** misconfigured's value codec of a short record: bounded's, its bound included. */ +template <> +struct value_codec { + /** Bytes. */ + static constexpr output_kind output = output_kind::bytes; + /** A short record's. */ + static constexpr std::size_t max_decode_input_size = + value_codec::max_decode_input_size; + + /** A short record's decode, which counts its calls. */ + static result decode(misconfigured_t const& /*format*/, + std::span input_bytes) noexcept { + return value_codec::decode(test::bounded, input_bytes); + } +}; + } // namespace webcpp::codecs namespace { @@ -105,6 +240,27 @@ static_assert(!noexcept(codecs::decode(test::letters, "ab"))); static_assert(codecs::decodable); static_assert(!noexcept(codecs::decode(test::be16, std::array{}))); static_assert(!noexcept(codecs::decode>(test::letters, "ab"))); +// The decode bound keeps the typed decode as noexcept as its value codec, for every input. +static_assert(noexcept(codecs::decode(test::bounded, + std::span{}))); +static_assert(noexcept(codecs::decode(test::bounded, + std::array{}))); +static_assert(noexcept(codecs::decode(test::bounded, + std::array{}))); +static_assert(noexcept(codecs::decode(test::bounded, + std::vector{}))); +static_assert(noexcept(codecs::decode(test::bounded, + std::span{}))); +static_assert(noexcept(codecs::decode(test::bounded, + std::declval&>()))); +static_assert(noexcept(codecs::decode(test::bounded, + std::span{}))); +static_assert(noexcept(codecs::decode(test::bounded, std::string_view{}))); +static_assert(noexcept(codecs::decode(test::bounded, codecs::text<21>{}))); +static_assert(noexcept(codecs::decode(test::bounded, + std::span{}))); +static_assert(noexcept(codecs::decode(misconfigured_t{}, + std::span{}))); void into_an_array() { auto const decoded = codecs::decode>(test::letters, "abpp"); @@ -242,6 +398,233 @@ void an_input_whose_bound_saturates_is_too_long() { BOOST_TEST(refused_key.error() == codecs::error::input_too_long); } +/** The 8 bytes of the records below. */ +constexpr std::array record_content{0x10, 0x11, 0x12, 0x13, + 0x14, 0x15, 0x16, 0x17}; + +/** A record's form of version 2: 16 bytes, its largest valid input. */ +constexpr std::array version_2{0x30, 0x0e, 0x02, 0x01, 0x10, 0x11, 0x12, 0x13, + 0x14, 0x15, 0x16, 0x17, 0xa1, 0x02, 0x10, 0x11}; + +/** 17 bytes: the form of version 2 and one more, one past the bound. */ +constexpr std::array past_bound{0x30, 0x0e, 0x02, 0x01, 0x10, 0x11, + 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0xa1, 0x02, 0x10, 0x11, 0x00}; + +/** The 8 bytes of a decoded record, public or sealed. */ +std::span content_of(test::short_record const& record) { + return record.held_bytes; +} + +/** The 8 bytes of a decoded record, public or sealed. */ +std::span content_of(test::sealed_record const& record) { + return record.held_bytes.bytes(); +} + +/** The 8 bytes of a decoded record, public or sealed. */ +std::span content_of(vaulted_record const& record) { + return record.record.held_bytes; +} + +/** + Decodes `input_value`, the form of version 2, into a `Value`: the record, one call of the value + codec of `Counted`, and one timing guard of codecs' when `Secret`. +*/ +template +void decodes_with_one_call(Input const& input_value) { + test::decode_calls = 0; + test::reset_guards(); + auto const decoded = codecs::decode(test::bounded, input_value); + BOOST_TEST(decoded.has_value()); + if (decoded.has_value()) { + BOOST_TEST(std::ranges::equal(content_of(decoded.value()), record_content)); + } + BOOST_TEST_EQ(test::decode_calls, 1U); + BOOST_TEST_EQ(test::timing_entries, Secret ? 1U : 0U); +} + +/** + Decodes `input_value`, 17 bytes, into a `Value`: input_too_long, the value codec of `Counted` + not called, and one timing guard of codecs' when `Secret`, which holds the refusal too. +*/ +template +void refused_with_no_call(Input const& input_value) { + test::decode_calls = 0; + test::reset_guards(); + auto const refused = codecs::decode(test::bounded, input_value); + BOOST_TEST(!refused.has_value()); + if (!refused.has_value()) { + BOOST_TEST(refused.error() == codecs::error::input_too_long); + } + BOOST_TEST_EQ(test::decode_calls, 0U); + BOOST_TEST_EQ(test::timing_entries, Secret ? 1U : 0U); +} + +/** A bytes holding `held`. */ +template +codecs::bytes fixed_bytes_of(std::array const& held) { + auto decoded = codecs::decode>(test::letters, + codecs::encode(test::letters, held).value()); + return decoded.value(); +} + +/** A secure::secret of the bytes of `held`. */ +template +secure::secret secret_of(std::array const& held) { + secure::secret held_secret; + std::ranges::copy(held, held_secret.writable().begin()); + return held_secret; +} + +/** + Every caller's buffer holding the input, 16 bytes decoded and 17 refused, into a `Value` whose + value codec is the one of `Counted`. +*/ +template +void every_buffer() { + // A span of a dynamic extent. + decodes_with_one_call(std::span{version_2}); + refused_with_no_call(std::span{past_bound}); + // A vector. + decodes_with_one_call( + std::vector(version_2.begin(), version_2.end())); + refused_with_no_call( + std::vector(past_bound.begin(), past_bound.end())); + // An array, of a static size: 16 skips the check at compile time, 17 is refused. + decodes_with_one_call(version_2); + refused_with_no_call(past_bound); + // A span of a static extent. + decodes_with_one_call(std::span{version_2}); + refused_with_no_call(std::span{past_bound}); + // A byte array. + // NOLINTBEGIN(modernize-avoid-c-arrays): the kind of input under test + std::uint8_t version_2_array[16]{}; + std::uint8_t past_bound_array[17]{}; + // NOLINTEND(modernize-avoid-c-arrays) + std::ranges::copy(version_2, std::begin(version_2_array)); + std::ranges::copy(past_bound, std::begin(past_bound_array)); + decodes_with_one_call(version_2_array); + refused_with_no_call(past_bound_array); + // bytes: a static bound of 17 above the decode bound, checked at run time on the size. + decodes_with_one_call(fixed_bytes_of<17>(version_2)); + refused_with_no_call(fixed_bytes_of<17>(past_bound)); + // A secret input. + decodes_with_one_call(secret_of(version_2)); + refused_with_no_call(secret_of(past_bound)); +} + +void a_record_past_its_bound_from_every_buffer() { + every_buffer(); +} + +void a_sealed_record_past_its_bound_holds_the_guard() { + every_buffer(); +} + +void a_record_requested_secret_past_its_bound_holds_the_guard() { + every_buffer(); +} + +void a_text_past_its_bound() { + std::string const twenty(20, 'q'); + std::string const twenty_one(21, 'q'); + // A string view, of a dynamic size. + test::decode_calls = 0; + auto const decoded = codecs::decode(test::bounded, std::string_view{twenty}); + BOOST_TEST(decoded.has_value()); + BOOST_TEST(decoded.has_value() && decoded.value().view() == twenty); + BOOST_TEST_EQ(test::decode_calls, 1U); + auto const refused = + codecs::decode(test::bounded, std::string_view{twenty_one}); + BOOST_TEST(!refused.has_value()); + BOOST_TEST(!refused.has_value() && refused.error() == codecs::error::input_too_long); + BOOST_TEST_EQ(test::decode_calls, 1U); + // text<21>, of a static bound above the decode bound: 21 letters refused, 20 decoded. + codecs::result> const twenty_one_text = + codecs::encode(spelled, letter_run{21}); + BOOST_TEST_EQ(twenty_one_text.value().size(), 21U); + test::decode_calls = 0; + auto const refused_fixed = + codecs::decode(test::bounded, twenty_one_text.value()); + BOOST_TEST(!refused_fixed.has_value()); + BOOST_TEST(!refused_fixed.has_value() && + refused_fixed.error() == codecs::error::input_too_long); + BOOST_TEST_EQ(test::decode_calls, 0U); + codecs::result> const twenty_text = codecs::encode(spelled, letter_run{20}); + auto const decoded_fixed = codecs::decode(test::bounded, twenty_text.value()); + BOOST_TEST(decoded_fixed.has_value()); + BOOST_TEST(decoded_fixed.has_value() && decoded_fixed.value().view() == twenty); + BOOST_TEST_EQ(test::decode_calls, 1U); + // text<20>, of a static bound at the decode bound, which skips the check at compile time. + codecs::result> const short_fixed = + codecs::encode(test::bounded, decoded.value()); + BOOST_TEST(codecs::decode(test::bounded, short_fixed.value()).has_value()); +} + +void a_wide_record_past_its_bound() { + // Past in_place_scratch_limit: 4097 bytes reach the value codec, 4098 are refused. + std::vector const at_bound(4097, 0x5a); + std::vector const past(4098, 0x5a); + test::decode_calls = 0; + auto const decoded = codecs::decode(test::bounded, at_bound); + BOOST_TEST(decoded.has_value()); + BOOST_TEST(decoded.has_value() && decoded.value().count == 4097U); + BOOST_TEST_EQ(test::decode_calls, 1U); + auto const refused = codecs::decode(test::bounded, past); + BOOST_TEST(!refused.has_value() && refused.error() == codecs::error::input_too_long); + BOOST_TEST_EQ(test::decode_calls, 1U); + // Secret: the guard held on both, the value codec called on the first alone. + test::decode_calls = 0; + test::reset_guards(); + auto const sealed = codecs::decode(test::bounded, at_bound); + BOOST_TEST(sealed.has_value() && sealed.value().count == 4097U); + auto const sealed_refused = codecs::decode(test::bounded, past); + BOOST_TEST(!sealed_refused.has_value() && + sealed_refused.error() == codecs::error::input_too_long); + BOOST_TEST_EQ(test::decode_calls, 1U); + BOOST_TEST_EQ(test::timing_entries, 2U); +} + +void the_configuration_fault_comes_first() { + // 17 bytes in a format at fault: the configuration's error, the value codec not called. + test::decode_calls = 0; + auto const at_fault = codecs::decode(misconfigured_t{}, past_bound); + BOOST_TEST(!at_fault.has_value() && at_fault.error() == codecs::error::invalid_prefix); + auto const dynamic_at_fault = codecs::decode( + misconfigured_t{}, std::span{past_bound}); + BOOST_TEST(!dynamic_at_fault.has_value() && + dynamic_at_fault.error() == codecs::error::invalid_prefix); + BOOST_TEST_EQ(test::decode_calls, 0U); + // With no fault, the bound: 17 bytes refused, 16 decoded. + misconfigured_t const configured{{}, false}; + auto const refused = codecs::decode(configured, past_bound); + BOOST_TEST(!refused.has_value() && refused.error() == codecs::error::input_too_long); + BOOST_TEST_EQ(test::decode_calls, 0U); + auto const decoded = codecs::decode(configured, version_2); + BOOST_TEST(decoded.has_value()); + BOOST_TEST_EQ(test::decode_calls, 1U); +} + +void a_bound_below_the_largest_valid_input_refuses_it() { + // tight_record's bound is 15: its valid form of version 2 is refused on every input, and its + // form of version 1 decodes. + test::decode_calls = 0; + auto const from_span = + codecs::decode(test::bounded, std::span{version_2}); + BOOST_TEST(!from_span.has_value() && from_span.error() == codecs::error::input_too_long); + auto const from_array = codecs::decode(test::bounded, version_2); + BOOST_TEST(!from_array.has_value() && from_array.error() == codecs::error::input_too_long); + auto const from_vector = codecs::decode( + test::bounded, std::vector(version_2.begin(), version_2.end())); + BOOST_TEST(!from_vector.has_value() && from_vector.error() == codecs::error::input_too_long); + BOOST_TEST_EQ(test::decode_calls, 0U); + // The same bytes reach short_record's value codec, whose bound is 16. + BOOST_TEST(codecs::decode(test::bounded, version_2).has_value()); + auto const version_1 = codecs::encode(test::bounded, test::tight_record{record_content}); + auto const tight = codecs::decode(test::bounded, version_1.value()); + BOOST_TEST(tight.has_value()); + BOOST_TEST_EQ(test::decode_calls, 1U); +} } // namespace int main() { @@ -252,5 +635,12 @@ int main() { a_value_codecs_exception_propagates(); into_the_heap(); an_input_whose_bound_saturates_is_too_long(); + a_record_past_its_bound_from_every_buffer(); + a_sealed_record_past_its_bound_holds_the_guard(); + a_record_requested_secret_past_its_bound_holds_the_guard(); + a_text_past_its_bound(); + a_wide_record_past_its_bound(); + the_configuration_fault_comes_first(); + a_bound_below_the_largest_valid_input_refuses_it(); return boost::report_errors(); } diff --git a/test/value_codec_test.cpp b/test/value_codec_test.cpp index d5e0069..9915f7b 100644 --- a/test/value_codec_test.cpp +++ b/test/value_codec_test.cpp @@ -7,14 +7,20 @@ // Value codecs: the concepts look at each function, the default result follows the stated bound, // and the buffer forms write into the caller's bytes or characters, noexcept and allocating // nothing. The refusal of a typed decode with no decode is a message case (refused_encode_only). +// The decode bound: the core reads a value codec's max_decode_input_size, or none, and the order +// of its two bounds; the encoding of a record whose decode bound is above its encoded bound is +// still the fixed result of the encoded bound (its order below it is a message case, +// decode_bound_below_encoded). #include "support/be16.hpp" +#include "support/bounded_records.hpp" #include "support/letters.hpp" #include #include +#include #include #include #include @@ -98,6 +104,32 @@ static_assert(!encodes_into>); static_assert(encodes_into>); static_assert(!encodes_into>); +// The decode bound the core reads: the value codec's, or none (std::dynamic_extent). +static_assert(codecs::detail::value_decode_bound() == 16); +static_assert(codecs::detail::value_decode_bound() == 16); +static_assert(codecs::detail::value_decode_bound() == 15); +static_assert(codecs::detail::value_decode_bound() == 4097); +static_assert(codecs::detail::value_decode_bound() == 20); +static_assert(codecs::detail::value_decode_bound() == + std::dynamic_extent); +static_assert(codecs::detail::value_decode_bound() == + std::dynamic_extent); +static_assert(codecs::detail::has_decode_input_bound< + codecs::value_codec>); +static_assert( + !codecs::detail::has_decode_input_bound>); +// The order of the two bounds: a decode bound at or above the encoded bound, or either missing. +static_assert(codecs::detail::value_bounds_ordered); +static_assert(codecs::detail::value_bounds_ordered); +static_assert(codecs::detail::value_bounds_ordered); +static_assert(codecs::detail::value_bounds_ordered); +static_assert(codecs::detail::value_bounds_ordered); +static_assert(codecs::detail::value_bounds_ordered); +static_assert(codecs::detail::value_bounds_ordered); +// The encoding of a record is the fixed result of its encoded bound, not of its decode bound. +static_assert(std::is_same_v>>); + void the_size_is_the_value_codecs() { BOOST_TEST_EQ(codecs::encoded_size(test::be16, test::number{}), 2U); BOOST_TEST_EQ(codecs::encoded_size(test::be16, test::hex_number{}), 4U); @@ -162,6 +194,24 @@ void the_round_trip() { BOOST_TEST(decoded.value() == test::number{0xa1b2}); } +void a_record_decoded_encodes_back() { + constexpr std::array version_1{0x30, 0x0a, 0x02, 0x00, 0x10, 0x11, + 0x12, 0x13, 0x14, 0x15, 0x16, 0x17}; + auto const decoded = codecs::decode(test::bounded, version_1); + BOOST_TEST(decoded.has_value()); + codecs::result> const encoded = + codecs::encode(test::bounded, decoded.value()); + BOOST_TEST(encoded.has_value()); + BOOST_TEST(std::ranges::equal(encoded.value().view(), version_1)); + // The form of version 2 decodes to the same record, which encodes as version 1. + constexpr std::array version_2{0x30, 0x0e, 0x02, 0x01, 0x10, 0x11, + 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0xa1, 0x02, 0x10, 0x11}; + auto const from_version_2 = codecs::decode(test::bounded, version_2); + BOOST_TEST(from_version_2.has_value()); + BOOST_TEST(from_version_2.value() == decoded.value()); +} + } // namespace int main() { @@ -171,5 +221,6 @@ int main() { the_default_results(); a_value_of_text_decodes_from_text(); the_round_trip(); + a_record_decoded_encodes_back(); return boost::report_errors(); } From 882c24296a76939c163a03bc86c70820821798c5 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 14:56:28 -0300 Subject: [PATCH 02/30] codecs: the decode bound's tests lint fix --- test/typed_decode_test.cpp | 45 ++++++++++++++++++++------------------ test/value_codec_test.cpp | 14 +++++++----- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/test/typed_decode_test.cpp b/test/typed_decode_test.cpp index 2e962d6..38f7fd1 100644 --- a/test/typed_decode_test.cpp +++ b/test/typed_decode_test.cpp @@ -79,13 +79,8 @@ struct spelled_t { /** The format of a letter run. */ inline constexpr spelled_t spelled{}; -} // namespace - -// Out of the anonymous namespace, so that its name, which no test reads, is not an unused variable. -namespace typed_decode { - /** A format whose configuration may be at fault, with a value codec of a short_record. */ -struct misconfigured_t : webcpp::codecs::test::whole_letters_t { +struct misconfigured_t : test::whole_letters_t { /** The format's name. */ static constexpr std::string_view name = "misconfigured"; @@ -93,11 +88,11 @@ struct misconfigured_t : webcpp::codecs::test::whole_letters_t { bool at_fault = true; }; -} // namespace typed_decode - -namespace { +// The format's name is its own, not whole_letters' it derives from. +static_assert(misconfigured_t::name == "misconfigured"); -using typed_decode::misconfigured_t; +/** misconfigured, its configuration at fault. */ +constexpr misconfigured_t faulty_format{}; } // namespace @@ -159,7 +154,7 @@ struct value_codec { if (!decoded.has_value()) { return decoded.error(); } - return vaulted_record{decoded.value()}; + return vaulted_record{decoded.unsafe_value()}; } }; @@ -259,7 +254,7 @@ static_assert(noexcept(codecs::decode(test::bounded, std::stri static_assert(noexcept(codecs::decode(test::bounded, codecs::text<21>{}))); static_assert(noexcept(codecs::decode(test::bounded, std::span{}))); -static_assert(noexcept(codecs::decode(misconfigured_t{}, +static_assert(noexcept(codecs::decode(faulty_format, std::span{}))); void into_an_array() { @@ -399,17 +394,25 @@ void an_input_whose_bound_saturates_is_too_long() { } /** The 8 bytes of the records below. */ -constexpr std::array record_content{0x10, 0x11, 0x12, 0x13, - 0x14, 0x15, 0x16, 0x17}; +constexpr std::array record_content{ + 0x10, 0x11, 0x12, 0x13, // the first half + 0x14, 0x15, 0x16, 0x17, // the second half +}; /** A record's form of version 2: 16 bytes, its largest valid input. */ -constexpr std::array version_2{0x30, 0x0e, 0x02, 0x01, 0x10, 0x11, 0x12, 0x13, - 0x14, 0x15, 0x16, 0x17, 0xa1, 0x02, 0x10, 0x11}; +constexpr std::array version_2{ + 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record + 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes +}; /** 17 bytes: the form of version 2 and one more, one past the bound. */ -constexpr std::array past_bound{0x30, 0x0e, 0x02, 0x01, 0x10, 0x11, - 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, - 0xa1, 0x02, 0x10, 0x11, 0x00}; +constexpr std::array past_bound{ + 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record + 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes + 0x00, // one byte past the bound +}; /** The 8 bytes of a decoded record, public or sealed. */ std::span content_of(test::short_record const& record) { @@ -588,10 +591,10 @@ void a_wide_record_past_its_bound() { void the_configuration_fault_comes_first() { // 17 bytes in a format at fault: the configuration's error, the value codec not called. test::decode_calls = 0; - auto const at_fault = codecs::decode(misconfigured_t{}, past_bound); + auto const at_fault = codecs::decode(faulty_format, past_bound); BOOST_TEST(!at_fault.has_value() && at_fault.error() == codecs::error::invalid_prefix); auto const dynamic_at_fault = codecs::decode( - misconfigured_t{}, std::span{past_bound}); + faulty_format, std::span{past_bound}); BOOST_TEST(!dynamic_at_fault.has_value() && dynamic_at_fault.error() == codecs::error::invalid_prefix); BOOST_TEST_EQ(test::decode_calls, 0U); diff --git a/test/value_codec_test.cpp b/test/value_codec_test.cpp index 9915f7b..b40ac3d 100644 --- a/test/value_codec_test.cpp +++ b/test/value_codec_test.cpp @@ -195,8 +195,10 @@ void the_round_trip() { } void a_record_decoded_encodes_back() { - constexpr std::array version_1{0x30, 0x0a, 0x02, 0x00, 0x10, 0x11, - 0x12, 0x13, 0x14, 0x15, 0x16, 0x17}; + constexpr std::array version_1{ + 0x30, 0x0a, 0x02, 0x00, // the prefix of version 1 + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record + }; auto const decoded = codecs::decode(test::bounded, version_1); BOOST_TEST(decoded.has_value()); codecs::result> const encoded = @@ -204,9 +206,11 @@ void a_record_decoded_encodes_back() { BOOST_TEST(encoded.has_value()); BOOST_TEST(std::ranges::equal(encoded.value().view(), version_1)); // The form of version 2 decodes to the same record, which encodes as version 1. - constexpr std::array version_2{0x30, 0x0e, 0x02, 0x01, 0x10, 0x11, - 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, - 0xa1, 0x02, 0x10, 0x11}; + constexpr std::array version_2{ + 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record + 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes + }; auto const from_version_2 = codecs::decode(test::bounded, version_2); BOOST_TEST(from_version_2.has_value()); BOOST_TEST(from_version_2.value() == decoded.value()); From 1ae1f73bf983cdeba651f819decebe42be051095 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 15:16:05 -0300 Subject: [PATCH 03/30] codecs: the decode bound's Doc Comments and tests tidied --- .../webcpp/codecs/detail/core/operations.hpp | 4 +- include/webcpp/codecs/detail/core/secrecy.hpp | 6 +-- test/support/bounded_records.hpp | 42 ++++++++++++++++--- test/typed_decode_test.cpp | 34 +++++++-------- test/value_codec_test.cpp | 11 +---- 5 files changed, 59 insertions(+), 38 deletions(-) diff --git a/include/webcpp/codecs/detail/core/operations.hpp b/include/webcpp/codecs/detail/core/operations.hpp index ebd6a17..33bafac 100644 --- a/include/webcpp/codecs/detail/core/operations.hpp +++ b/include/webcpp/codecs/detail/core/operations.hpp @@ -998,8 +998,8 @@ template the heap. A fault of the format's configuration is returned first, before any data, in every case (its codec's `configuration_fault`, or a chain's links'). A value codec that states `max_decode_input_size` then refuses a longer input with @ref error::input_too_long before it is - called, whatever the input's type, public or secret (owner decision 29); an input whose size known - at compile time is at most the bound skips the check. It is `noexcept` for the fixed + called, whatever the input's type, public or secret (owner decision 29); an input whose size is + known at compile time and at most the bound skips the check. It is `noexcept` for the fixed byte results, and for a value codec as the value codec's `decode` is declared; the heap types allocate and are not. The operation is secret (D10) when `Value`'s `is_secret_v` or the value codec's `secret` says so: it then holds secure's timing diff --git a/include/webcpp/codecs/detail/core/secrecy.hpp b/include/webcpp/codecs/detail/core/secrecy.hpp index af0f8c9..1aa8983 100644 --- a/include/webcpp/codecs/detail/core/secrecy.hpp +++ b/include/webcpp/codecs/detail/core/secrecy.hpp @@ -601,9 +601,9 @@ template It is the one funnel of every value decode, the typed decode of bytes and of text and `over`'s call of its inner value codec, so that the value codec's decode bound is enforced on every path (owner decision 29): an input longer than the value codec's `max_decode_input_size` is - @ref error::input_too_long, the value codec not called; a view whose bound known at compile time - is at most the decode bound skips the check. A secret refusal is made inside the caller's timing - guard and reads the input's length alone. + @ref error::input_too_long, the value codec not called; a view whose bound is known at compile + time and at most the decode bound skips the check. A secret refusal is made inside the caller's + timing guard and reads the input's length alone. */ template [[nodiscard]] constexpr result value_decode(F const& format_value, View input_view) noexcept( diff --git a/test/support/bounded_records.hpp b/test/support/bounded_records.hpp index c366ee7..0f65c51 100644 --- a/test/support/bounded_records.hpp +++ b/test/support/bounded_records.hpp @@ -133,9 +133,42 @@ inline std::span record_bytes(tight_record const& record) /** The size of a record's form of version 1: the 4-byte prefix and the 8 bytes. */ inline constexpr std::size_t record_version_1_size = 12; +/** The prefix of a record's form of version 1. */ +inline constexpr std::array record_version_1_prefix{0x30, 0x0a, 0x02, 0x00}; + /** The size of a record's form of version 2: version 1's, `a1 02` and 2 bytes. */ inline constexpr std::size_t record_version_2_size = 16; +/** The prefix of a record's form of version 2. */ +inline constexpr std::array record_version_2_prefix{0x30, 0x0e, 0x02, 0x01}; + +/** The 8 bytes of the tests' example record. */ +inline constexpr std::array record_example_content{ + 0x10, 0x11, 0x12, 0x13, // the first half + 0x14, 0x15, 0x16, 0x17, // the second half +}; + +/** The example record's form of version 1: 12 bytes, its encoded form. */ +inline constexpr std::array record_version_1_example{ + 0x30, 0x0a, 0x02, 0x00, // the prefix of version 1 + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record +}; + +/** The example record's form of version 2: 16 bytes, its largest valid input. */ +inline constexpr std::array record_version_2_example{ + 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record + 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes +}; + +/** 17 bytes: the example record's form of version 2 and one more, one past the bound. */ +inline constexpr std::array record_past_bound_example{ + 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record + 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes + 0x00, // one byte past the bound +}; + /** Reads a record's form of version 1 or 2 into `held_bytes`. @@ -145,15 +178,13 @@ inline constexpr std::size_t record_version_2_size = 16; */ inline std::optional read_record(std::span input_bytes, std::span held_bytes) noexcept { - constexpr std::array version_1_prefix{0x30, 0x0a, 0x02, 0x00}; - constexpr std::array version_2_prefix{0x30, 0x0e, 0x02, 0x01}; auto const prefix = input_bytes.first(std::min(input_bytes.size(), 4)); if (input_bytes.size() == record_version_1_size) { - if (!std::ranges::equal(prefix, version_1_prefix)) { + if (!std::ranges::equal(prefix, record_version_1_prefix)) { return error::invalid_prefix; } } else if (input_bytes.size() == record_version_2_size) { - if (!std::ranges::equal(prefix, version_2_prefix) || input_bytes[12] != 0xa1 || + if (!std::ranges::equal(prefix, record_version_2_prefix) || input_bytes[12] != 0xa1 || input_bytes[13] != 0x02 || input_bytes[14] != input_bytes[4] || input_bytes[15] != input_bytes[5]) { return error::invalid_prefix; @@ -190,8 +221,7 @@ struct record_codec { if (output_bytes.size() < record_version_1_size) { return error::output_too_small; } - constexpr std::array version_1_prefix{0x30, 0x0a, 0x02, 0x00}; - std::ranges::copy(version_1_prefix, output_bytes.begin()); + std::ranges::copy(record_version_1_prefix, output_bytes.begin()); std::ranges::copy(record_bytes(held_value), output_bytes.subspan(4).begin()); return record_version_1_size; } diff --git a/test/typed_decode_test.cpp b/test/typed_decode_test.cpp index 38f7fd1..3e29c93 100644 --- a/test/typed_decode_test.cpp +++ b/test/typed_decode_test.cpp @@ -394,25 +394,13 @@ void an_input_whose_bound_saturates_is_too_long() { } /** The 8 bytes of the records below. */ -constexpr std::array record_content{ - 0x10, 0x11, 0x12, 0x13, // the first half - 0x14, 0x15, 0x16, 0x17, // the second half -}; +constexpr auto const& record_content = test::record_example_content; /** A record's form of version 2: 16 bytes, its largest valid input. */ -constexpr std::array version_2{ - 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record - 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes -}; +constexpr auto const& version_2 = test::record_version_2_example; /** 17 bytes: the form of version 2 and one more, one past the bound. */ -constexpr std::array past_bound{ - 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record - 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes - 0x00, // one byte past the bound -}; +constexpr auto const& past_bound = test::record_past_bound_example; /** The 8 bytes of a decoded record, public or sealed. */ std::span content_of(test::short_record const& record) { @@ -559,9 +547,16 @@ void a_text_past_its_bound() { BOOST_TEST(decoded_fixed.has_value() && decoded_fixed.value().view() == twenty); BOOST_TEST_EQ(test::decode_calls, 1U); // text<20>, of a static bound at the decode bound, which skips the check at compile time. - codecs::result> const short_fixed = - codecs::encode(test::bounded, decoded.value()); - BOOST_TEST(codecs::decode(test::bounded, short_fixed.value()).has_value()); + if (decoded.has_value()) { + codecs::result> const short_fixed = + codecs::encode(test::bounded, decoded.value()); + BOOST_TEST(short_fixed.has_value()); + test::decode_calls = 0; + auto const decoded_short = + codecs::decode(test::bounded, short_fixed.value()); + BOOST_TEST(decoded_short.has_value() && decoded_short.value().view() == twenty); + BOOST_TEST_EQ(test::decode_calls, 1U); + } } void a_wide_record_past_its_bound() { @@ -569,6 +564,7 @@ void a_wide_record_past_its_bound() { std::vector const at_bound(4097, 0x5a); std::vector const past(4098, 0x5a); test::decode_calls = 0; + test::reset_guards(); auto const decoded = codecs::decode(test::bounded, at_bound); BOOST_TEST(decoded.has_value()); BOOST_TEST(decoded.has_value() && decoded.value().count == 4097U); @@ -576,6 +572,8 @@ void a_wide_record_past_its_bound() { auto const refused = codecs::decode(test::bounded, past); BOOST_TEST(!refused.has_value() && refused.error() == codecs::error::input_too_long); BOOST_TEST_EQ(test::decode_calls, 1U); + // Public: no guard held. + BOOST_TEST_EQ(test::timing_entries, 0U); // Secret: the guard held on both, the value codec called on the first alone. test::decode_calls = 0; test::reset_guards(); diff --git a/test/value_codec_test.cpp b/test/value_codec_test.cpp index b40ac3d..708700e 100644 --- a/test/value_codec_test.cpp +++ b/test/value_codec_test.cpp @@ -195,10 +195,7 @@ void the_round_trip() { } void a_record_decoded_encodes_back() { - constexpr std::array version_1{ - 0x30, 0x0a, 0x02, 0x00, // the prefix of version 1 - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record - }; + constexpr auto const& version_1 = test::record_version_1_example; auto const decoded = codecs::decode(test::bounded, version_1); BOOST_TEST(decoded.has_value()); codecs::result> const encoded = @@ -206,11 +203,7 @@ void a_record_decoded_encodes_back() { BOOST_TEST(encoded.has_value()); BOOST_TEST(std::ranges::equal(encoded.value().view(), version_1)); // The form of version 2 decodes to the same record, which encodes as version 1. - constexpr std::array version_2{ - 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record - 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes - }; + constexpr auto const& version_2 = test::record_version_2_example; auto const from_version_2 = codecs::decode(test::bounded, version_2); BOOST_TEST(from_version_2.has_value()); BOOST_TEST(from_version_2.value() == decoded.value()); From 9e5624d8cbe227c0c2524d2eabbee3a491c5ca55 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 15:20:56 -0300 Subject: [PATCH 04/30] codecs: over decodes a value in place under the inner value codec's decode bound, refusing a longer intermediate before it is made --- include/webcpp/codecs/detail/core/over.hpp | 79 ++++- test/allocation_test.cpp | 39 +++ test/noexcept_test.cpp | 33 ++ test/over_test.cpp | 370 +++++++++++++++++++++ test/support/bounded_records.hpp | 44 ++- 5 files changed, 540 insertions(+), 25 deletions(-) diff --git a/include/webcpp/codecs/detail/core/over.hpp b/include/webcpp/codecs/detail/core/over.hpp index 527929d..8850e53 100644 --- a/include/webcpp/codecs/detail/core/over.hpp +++ b/include/webcpp/codecs/detail/core/over.hpp @@ -87,9 +87,16 @@ struct over_name { The intermediate, the bytes between the links, is held in place when its bound is known at compile time, which is when the input's type states its size (`std::array`, `std::span` of a - static extent, `secure::secret`, `text`, `bytes`) or the value codec states - `max_encoded_size`; it is otherwise made with `allocator`, which is used for nothing else, so - that `over` allocates through `std::allocator`. The exact `encoded_size` and + static extent, `secure::secret`, `text`, `bytes`), the value codec of an encode states + `max_encoded_size`, or the value codec of a decode states its decode bound, + `max_decode_input_size`, of at most 4096 bytes (`detail::in_place_scratch_limit`, owner decision + 30), the smaller bound then held; it is otherwise made with `allocator`, which is used for nothing + else, so that `over` allocates through `std::allocator`. A decode of a value + refuses an intermediate longer than the inner value codec's decode bound with + `error::input_too_long` before it is made, after the outer link's own error, as a direct decode + refuses that input (owner decision 29): nothing is written, allocated or wiped; so a value codec + with a bound of at most 4096 bytes is decoded from a `std::string_view` with no heap, and one with + a bound above it from the allocator, of exactly the written size. The exact `encoded_size` and `decoded_size` run the inner link into the intermediate (ruling R3): with a heap intermediate they allocate, so those, `encode` and `decode` are not `noexcept` and an exception of the allocator reaches the caller, the buffer forms of the core among them (the one allocating buffer form), @@ -102,14 +109,16 @@ struct over_name { `encoder(chain)` and `decoder(chain)`, stream through a pipe of the links' streams when both links stream natively (format_transformer.hpp). - An intermediate in place is held for every bound known at compile time, with no cap, so that - `over` with a static bound allocates nothing, as the spec's Allocation requirement says. Its cost - is the stack: an array of the middle bound (the inner link's bound on the input's size for an - encode, the outer link's bound on the text's size for a decode) in the frame of each of `over`'s - functions, one for each level of a chain of chains. A caller with a large input of a static size - who wants the intermediate on the heap passes it as a view of a dynamic extent - (`std::span`, `std::string_view`), whose intermediate comes from `allocator`; - the allocator of a result or a token is not the intermediate's. + An intermediate in place is held for every bound an input's static size or an encoded bound + gives, with no cap, so that `over` with a static bound allocates nothing, as the spec's Allocation + requirement says; a decode bound is held in place up to the cap alone. Its cost is the stack: an + array of the middle bound (the inner link's bound on the input's size for an encode, the smaller + of the outer link's bound on the text's size and the decode bound for a decode) in the frame of + each of `over`'s functions, one for each level of a chain of chains. A caller with a large input + of a static size who wants the intermediate on the heap passes it as a view of a dynamic extent + (`std::span`, `std::string_view`), whose intermediate comes from `allocator` + unless a decode bound of at most 4096 bytes holds it in place; the allocator of a result or a + token is not the intermediate's. @tparam Outer The format that writes the final text and reads it first. @tparam Inner The format whose bytes the outer link encodes and decodes. @@ -838,11 +847,32 @@ template codec_encode_is_noexcept(); } -/** Whether a decode of `View` into a `Value` through `Outer` over `Inner` cannot throw. */ +/** + The bound the intermediate of a decode of `View` into a `Value` through `Outer` over `Inner` is + held in place under (design D2): the smaller of the outer link's bound on a text of a static size + and the inner value codec's decode bound when that is at most `in_place_scratch_limit` (owner + decision 30); `std::dynamic_extent` when neither is known, the intermediate then from the chain's + allocator. `std::dynamic_extent` is `SIZE_MAX`, so the smaller of two is `(std::min)` of them. +*/ +template +[[nodiscard]] consteval std::size_t over_value_middle_bound() noexcept { + constexpr std::size_t text_bound = decoded_bound>(); + constexpr std::size_t value_bound = value_decode_bound(); + if constexpr (value_bound <= in_place_scratch_limit) { + return (std::min)(text_bound, value_bound); + } else { + return text_bound; + } +} + +/** + Whether a decode of `View` into a `Value` through `Outer` over `Inner` cannot throw: the + intermediate is in place under over_value_middle_bound and the links' calls cannot throw. +*/ template [[nodiscard]] constexpr bool over_value_decode_is_noexcept() { using inner_codec = value_codec; - constexpr std::size_t middle_bound = decoded_bound>(); + constexpr std::size_t middle_bound = over_value_middle_bound(); using middle_view = std::conditional_t, intermediate_text>; @@ -918,19 +948,29 @@ template /** The value `chain` decodes `text_characters` to: the outer link into the intermediate, then the - value codec of the inner link. + value codec of the inner link (design D3). The outer link's `decoded_size` checks the whole text + first, and its error is the chain's; an exact size past the inner value codec's decode bound + (`max_decode_input_size`) is then @ref error::input_too_long, the same error as a direct decode's + (owner decision 29), before the intermediate is made: nothing is written, allocated or wiped. The + intermediate is in place under over_value_middle_bound, else from the chain's allocator, of + exactly the written size; when the chain is secret it is wiped on every path that makes it. */ template [[nodiscard]] result over_value_decode(over const& chain, View text_characters) noexcept( over_value_decode_is_noexcept()) { - constexpr std::size_t middle_bound = decoded_bound>(); + constexpr std::size_t middle_bound = over_value_middle_bound(); result const middle_size = codec_decoded_size(chain.outer, text_characters); if (!middle_size) { return middle_size.error(); } // Read unchecked: the error was returned above. + if constexpr (has_decode_input_bound>) { + if (middle_size.unsafe_value() > value_decode_bound()) { + return error::input_too_long; + } + } if (past_bound(middle_size.unsafe_value())) { return error::input_too_long; } @@ -1177,7 +1217,14 @@ struct encode_probe> { and runs both links' secret arithmetic. Its overloads that take `detail::secret_tag` first do the same, and the core calls them in every secret operation (ruling R7), one made secret by its token alone (`tokens::secret`, a token whose `secret` is true) included. Its functions are `noexcept` - exactly when the intermediate is in place (ruling R3), as `codec>`'s are. + exactly when the intermediate is in place (ruling R3), as `codec>`'s are; its decode's + intermediate is in place also for a text of a dynamic size when the inner value codec states a + decode bound (`max_decode_input_size`) of at most 4096 bytes, and an intermediate past that bound + is refused with `error::input_too_long` before it is made. It states no `max_decode_input_size` + of its own (design R8): a codec's decoder may accept a text longer than the longest it writes + (the armor's CRLF lines), so no bound on the chain's text follows from its links' contracts, and + in a chain of chains only the level whose inner link is that value codec is in place for a text + of a dynamic size. @tparam Outer The outer link's type. @tparam Inner The inner link's type. diff --git a/test/allocation_test.cpp b/test/allocation_test.cpp index cf02560..d9137e0 100644 --- a/test/allocation_test.cpp +++ b/test/allocation_test.cpp @@ -15,6 +15,7 @@ #include "support/allocation_checks.hpp" #include "support/be16.hpp" #include "support/bookkeeping.hpp" +#include "support/bounded_records.hpp" #include "support/doubler.hpp" #include "support/letters.hpp" #include "support/samples.hpp" @@ -465,6 +466,43 @@ void check_over() { static_cast(sink); } +/** over's decode of a value from a text of a dynamic size (the spec's Allocation requirement, + design D2): the inner value codec's decode bound (16) holds the intermediate in place, through hex + and through the armor, for the 12-byte and the 16-byte forms, from a string view and a string. */ +void check_over_value_decode_bound() { + constexpr codecs::over hex_chain{codecs::formats::hex, test::bounded}; + codecs::over const armor_chain{codecs::formats::armor("RECORD"), test::bounded}; + for (std::span const form : + {std::span{test::record_version_1_example}, + std::span{test::record_version_2_example}}) { + std::string const hex_text = + codecs::encode(codecs::formats::hex, form, codecs::tokens::return_string).value(); + std::string const armor_text = + codecs::encode(codecs::formats::armor("RECORD"), form, codecs::tokens::return_string) + .value(); + bool volatile verdict = false; + test::expect_no_allocation("over hex, a value from a run-time text", [&] { + auto const from_view = + codecs::decode(hex_chain, std::string_view{hex_text}); + auto const from_string = codecs::decode(hex_chain, hex_text); + verdict = from_view.has_value() && from_string.has_value() && + from_view.value().held_bytes == test::record_example_content && + from_string.value() == from_view.value(); + }); + BOOST_TEST(verdict); + verdict = false; + test::expect_no_allocation("over the armor, a value from a run-time text", [&] { + auto const from_view = + codecs::decode(armor_chain, std::string_view{armor_text}); + auto const from_string = codecs::decode(armor_chain, armor_text); + verdict = from_view.has_value() && from_string.has_value() && + from_view.value().held_bytes == test::record_example_content && + from_string.value() == from_view.value(); + }); + BOOST_TEST(verdict); + } +} + /** The transformers of the tests' own, a stream and the one-shots of the toy media, convert in the caller's spans and allocate nothing (D17, D18). */ void check_transformers() { @@ -1639,6 +1677,7 @@ int main() { check_secret_operations(); check_assign_to_scratch(); check_over(); + check_over_value_decode_bound(); check_transformers(); check_pipe_and_drain(); check_format_transformers(); diff --git a/test/noexcept_test.cpp b/test/noexcept_test.cpp index 504b95b..92693fc 100644 --- a/test/noexcept_test.cpp +++ b/test/noexcept_test.cpp @@ -9,6 +9,7 @@ // add their rows, both ways. #include "support/be16.hpp" +#include "support/bounded_records.hpp" #include "support/doubler.hpp" #include "support/letters.hpp" #include "support/toy_media.hpp" @@ -242,6 +243,38 @@ static_assert(!noexcept( codecs::value_codec, test::hex_number>::encoded_size(over_detail::secret_tag{}, be16_over_letters, test::hex_number{}))); +// A value under its inner value codec's decode bound (design D2, D4): from a text of a dynamic +// size the intermediate is in place when the bound is at most 4096 bytes, so the decode is +// noexcept, public and secret, through hex and through the armor, and through a chain of chains +// whose inner level holds it; above 4096 (wide_record, 4097) it is on the heap and is not. over's +// own value codec states no decode bound (R8). +using hex_over_bounded = codecs::over; +using armor_over_bounded = codecs::over; +using hex_over_armor_over_bounded = codecs::over; +static_assert(noexcept(codecs::decode(std::declval(), + std::string_view{}))); +static_assert(noexcept(codecs::decode(std::declval(), + std::string{}))); +static_assert(noexcept(codecs::decode(std::declval(), + std::string_view{}))); +static_assert(noexcept(codecs::decode(std::declval(), + std::string_view{}))); +static_assert(noexcept(codecs::decode(std::declval(), + std::string_view{}))); +static_assert(noexcept(codecs::decode(std::declval(), + codecs::text<64>{}))); +static_assert(noexcept(codecs::decode(std::declval(), + std::string_view{}))); +static_assert(!noexcept(codecs::decode(std::declval(), + std::string_view{}))); +static_assert(!noexcept(codecs::decode(std::declval(), + std::string_view{}))); +static_assert(!noexcept(codecs::decode( + std::declval(), std::string_view{}))); +static_assert(!over_detail::has_decode_input_bound< + codecs::value_codec>); +static_assert(!over_detail::has_decode_input_bound< + codecs::value_codec>); // A chain's construction, copy and assignments allocate nothing: noexcept exactly when the links' // and the allocator's are (owner decision 24), the pmr allocator, which has no assignment, made // again. diff --git a/test/over_test.cpp b/test/over_test.cpp index 3ea7e56..e9eb507 100644 --- a/test/over_test.cpp +++ b/test/over_test.cpp @@ -11,18 +11,29 @@ // errors, nesting, constant time, and the secret chain: the links' secret overloads run (ruling R7) // and the intermediate, in place and on the heap, is wiped, as secure's wipe seam records it, also // for a chain of a value codec made secret by its token alone. +// The decode of a value under the inner value codec's decode bound (design D2, D3): from a text of +// a dynamic size the intermediate is in place when the bound is at most 4096 bytes (no heap, the +// decode noexcept), and from the chain's allocator of exactly the written size above it; an +// intermediate past the bound is input_too_long before it is made (no allocation, no wipe, the +// value codec not called), after the outer link's own error; the error is the direct decode's on +// every path; a value codec with no decode bound, and the outer level of a chain of chains (R8), +// keep the heap. #include "support/wipe_recorder.hpp" #include "support/counting.hpp" +#include "support/allocators.hpp" +#include "support/arena.hpp" #include "support/be16.hpp" +#include "support/bounded_records.hpp" #include "support/letters.hpp" #include "support/saturating.hpp" #include "support/secret_tokens.hpp" #include "support/tagged_letters.hpp" #include +#include #include #include #include @@ -33,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +59,7 @@ namespace over_test { namespace { namespace codecs = webcpp::codecs; +namespace formats = webcpp::codecs::formats; namespace secure = webcpp::secure; namespace test = webcpp::codecs::test; namespace tokens = webcpp::codecs::tokens; @@ -636,6 +649,353 @@ void a_secret_value_chain_runs_the_outer_links_secret_decode() { BOOST_TEST(test::wiped_of_size(2)); } +/** The hex of `input_bytes`, made before a test's window. */ +std::string hex_of(std::span input_bytes) { + return codecs::encode(formats::hex, input_bytes, tokens::return_string).value(); +} + +/** The armor of `input_bytes` under the label RECORD, made before a test's window. */ +std::string armor_of(std::span input_bytes) { + return codecs::encode(formats::armor("RECORD"), input_bytes, tokens::return_string).value(); +} + +/** `text` with each line feed a carriage return and a line feed. */ +std::string with_crlf(std::string_view text) { + std::string lines; + for (char const character : text) { + if (character == '\n') { + lines.push_back('\r'); + } + lines.push_back(character); + } + return lines; +} + +/** `text` without its final line feed. */ +std::string without_final_line_feed(std::string_view text) { + BOOST_TEST(text.ends_with('\n')); + return std::string{text.substr(0, text.size() - 1)}; +} + +/** The armor `text` whose last base64 symbol (the one before the padding) is `*`. */ +std::string with_bad_last_symbol(std::string_view text) { + std::string spoiled{text}; + std::size_t position = spoiled.find("\n-----END"); + BOOST_TEST(position != std::string::npos); + do { + --position; + } while (spoiled[position] == '='); + spoiled[position] = '*'; + return spoiled; +} + +/** The bytes<32> that holds `input_bytes`, made through hex's decode. */ +codecs::bytes<32> held_in_bytes_32(std::span input_bytes) { + std::string const digits = hex_of(input_bytes); + return codecs::decode>(formats::hex, std::string_view{digits}).value(); +} + +/** The two chains of the decode bound's tests: over hex and over the armor RECORD. */ +auto hex_chain() noexcept { + return codecs::over{formats::hex, test::bounded}; +} + +/** The chain over the armor RECORD. */ +auto armor_chain() noexcept { + return codecs::over{formats::armor("RECORD"), test::bounded}; +} + +/** Decodes `text` through `chain` to the example record with no allocation and one call. */ +template +void decodes_the_example_with_no_heap(char const* what, Chain const& chain, Text const& text) { + test::decode_calls = 0; + test::expect_no_allocation(what, [&] { + auto const decoded = codecs::decode(chain, text); + BOOST_TEST(decoded.has_value() && + decoded.value().held_bytes == test::record_example_content); + }); + BOOST_TEST_EQ(test::decode_calls, 1U); +} + +void a_value_from_a_run_time_text_has_no_heap() { + // The spec's scenario: a run-time text decodes in place, under the value codec's bound of 16. + static_assert(noexcept(codecs::decode(hex_chain(), std::string_view{}))); + static_assert(noexcept(codecs::decode(armor_chain(), std::string_view{}))); + static_assert(noexcept(codecs::decode(hex_chain(), std::string{}))); + auto const hex_link = hex_chain(); + auto const armor_link = armor_chain(); + for (std::span const form : + {std::span{test::record_version_1_example}, + std::span{test::record_version_2_example}}) { + std::string const hex_text = hex_of(form); + std::string const armor_text = armor_of(form); + std::string const crlf_text = with_crlf(armor_text); + std::string const unterminated_text = without_final_line_feed(armor_text); + decodes_the_example_with_no_heap("hex, a string view", hex_link, + std::string_view{hex_text}); + decodes_the_example_with_no_heap("hex, a string", hex_link, hex_text); + decodes_the_example_with_no_heap("armor, a string view", armor_link, + std::string_view{armor_text}); + decodes_the_example_with_no_heap("armor, a string", armor_link, armor_text); + decodes_the_example_with_no_heap("armor, CRLF lines", armor_link, + std::string_view{crlf_text}); + decodes_the_example_with_no_heap("armor, no final line feed", armor_link, + std::string_view{unterminated_text}); + } +} + +/** Refuses `text` through `chain` with `expected`, allocating nothing, the value codec uncalled. */ +template +void refused_with_no_call(char const* what, Chain const& chain, std::string_view text, + codecs::error expected) { + test::decode_calls = 0; + test::expect_no_allocation(what, [&] { + auto const refused = codecs::decode(chain, text); + BOOST_TEST(!refused.has_value() && refused.error() == expected); + }); + BOOST_TEST_EQ(test::decode_calls, 0U); +} + +void a_payload_past_the_bound_is_refused_before_the_intermediate() { + std::string const hex_text = hex_of(test::record_past_bound_example); + std::string const armor_text = armor_of(test::record_past_bound_example); + BOOST_TEST_EQ(hex_text.size(), 34U); + refused_with_no_call("hex, 17 bytes", hex_chain(), hex_text, + codecs::error::input_too_long); + refused_with_no_call("armor, 17 bytes", armor_chain(), armor_text, + codecs::error::input_too_long); + // No byte: the value codec's own invalid_length. + std::string const empty_armor = armor_of({}); + for (auto const& [what, decoded] : + {std::pair{"hex, no byte", + codecs::decode(hex_chain(), std::string_view{})}, + std::pair{"armor, no byte", codecs::decode( + armor_chain(), std::string_view{empty_armor})}}) { + if (decoded.has_value() || decoded.error() != codecs::error::invalid_length) { + std::printf("not invalid_length: %s\n", what); + } + BOOST_TEST(!decoded.has_value() && decoded.error() == codecs::error::invalid_length); + } +} + +void the_outer_link_errs_first() { + // Past the bound, the outer link's data error, found by its decoded_size, is the chain's. + std::string const hex_text = hex_of(test::record_past_bound_example); + refused_with_no_call("hex, a last g", hex_chain(), hex_text + "g", + codecs::error::invalid_character); + refused_with_no_call("hex, 35 digits", hex_chain(), hex_text + "0", + codecs::error::invalid_length); + std::string const spoiled = with_bad_last_symbol(armor_of(test::record_past_bound_example)); + refused_with_no_call("armor, a last *", armor_chain(), spoiled, + codecs::error::invalid_character); +} + +void a_chain_at_fault_refuses_first() { + // preflight F20: a link's configuration fault is the chain's, before any data, any bound. + auto const at_fault = codecs::over{formats::armor("bad label!"), test::bounded}; + std::string const valid_text = armor_of(test::record_version_1_example); + std::string const long_text = armor_of(test::record_past_bound_example); + refused_with_no_call("a bad label, 12 bytes", at_fault, valid_text, + codecs::error::invalid_prefix); + refused_with_no_call("a bad label, 17 bytes", at_fault, long_text, + codecs::error::invalid_prefix); +} + +void a_static_text_takes_the_smaller_bound() { + auto const hex_link = hex_chain(); + // The outer link's bound (12, 32) against the value codec's (16): the smaller is held. + static_assert( + codecs::detail::over_value_middle_bound>() == 12); + static_assert( + codecs::detail::over_value_middle_bound>() == 16); + static_assert(codecs::detail::over_value_middle_bound() == + 16); + static_assert(noexcept(codecs::decode(hex_link, codecs::text<24>{}))); + static_assert(noexcept(codecs::decode(hex_link, codecs::text<64>{}))); + auto const short_text = codecs::encode(formats::hex, test::record_version_1_example); + static_assert(std::is_same_v> const>); + auto const wide_text = + codecs::encode(formats::hex, held_in_bytes_32(test::record_version_1_example)); + static_assert(std::is_same_v> const>); + auto const long_text = + codecs::encode(formats::hex, held_in_bytes_32(test::record_past_bound_example)); + BOOST_TEST_EQ(long_text.value().size(), 34U); + decodes_the_example_with_no_heap("hex, text<24>", hex_link, short_text.value()); + decodes_the_example_with_no_heap("hex, text<64>", hex_link, wide_text.value()); + test::decode_calls = 0; + test::expect_no_allocation("hex, text<64> of 34 digits", [&] { + auto const refused = codecs::decode(hex_link, long_text.value()); + BOOST_TEST(!refused.has_value() && refused.error() == codecs::error::input_too_long); + }); + BOOST_TEST_EQ(test::decode_calls, 0U); +} + +/** Decodes `text` into a `Value` through the hex chain and reports what it wiped. */ +template +codecs::result decoded_with_wipes(std::string_view text) { + test::clear_wipes(); + return codecs::decode(hex_chain(), text); +} + +void a_secret_intermediate_is_wiped_on_every_path() { + // Valid: the intermediate, of the form's size, wiped once. + std::string const version_2_text = hex_of(test::record_version_2_example); + auto const from_version_2 = decoded_with_wipes(version_2_text); + BOOST_TEST(from_version_2.has_value() && + std::ranges::equal(from_version_2.value().held_bytes.bytes(), + test::record_example_content)); + BOOST_TEST_EQ(test::wipes_of_size(test::record_version_2_size), 1U); + std::string const version_1_text = hex_of(test::record_version_1_example); + auto const from_version_1 = decoded_with_wipes(version_1_text); + BOOST_TEST(from_version_1.has_value()); + BOOST_TEST_EQ(test::wipes_of_size(test::record_version_1_size), 1U); + // Refused by the value codec: the written intermediate wiped all the same. + std::array wrong_prefix = + test::record_version_1_example; + wrong_prefix[0] = 0x31; + auto const refused = decoded_with_wipes(hex_of(wrong_prefix)); + BOOST_TEST(!refused.has_value() && refused.error() == codecs::error::invalid_prefix); + BOOST_TEST_EQ(test::wipes_of_size(test::record_version_1_size), 1U); + // Refused by the outer link's decoded_size, and past the bound: no intermediate, no wipe. + std::string bad_digit = version_2_text; + bad_digit.back() = 'g'; + auto const outer_refused = decoded_with_wipes(bad_digit); + BOOST_TEST(!outer_refused.has_value() && + outer_refused.error() == codecs::error::invalid_character); + BOOST_TEST_EQ(test::wipes.count, 0U); + auto const past = + decoded_with_wipes(hex_of(test::record_past_bound_example)); + BOOST_TEST(!past.has_value() && past.error() == codecs::error::input_too_long); + BOOST_TEST_EQ(test::wipes.count, 0U); + // A public value wipes nothing. + auto const public_record = decoded_with_wipes(version_2_text); + BOOST_TEST(public_record.has_value()); + BOOST_TEST_EQ(test::wipes.count, 0U); +} + +void a_bound_above_the_cap_takes_the_allocator() { + // owner decision 30: 4097 bytes from the allocator, exactly; 4096 in place; 4098 refused first. + test::recording_resource arena; + auto const chain = codecs::over{formats::hex, test::bounded, + std::pmr::polymorphic_allocator{&arena}}; + static_assert(!noexcept(codecs::decode(chain, std::string_view{}))); + static_assert(noexcept(codecs::decode(chain, std::string_view{}))); + std::string const at_bound = hex_of(std::vector(test::wide_record_bound, 0x5a)); + std::string const past = hex_of(std::vector(test::wide_record_bound + 1, 0x5a)); + std::string const at_edge = hex_of(std::vector(test::edge_record_bound, 0x5a)); + auto const decoded = codecs::decode(chain, at_bound); + BOOST_TEST(decoded.has_value() && decoded.value().count == test::wide_record_bound); + BOOST_TEST_EQ(arena.allocation_count(), 1U); + BOOST_TEST(arena.allocation_count() == 1 && + arena.allocation(0).size == test::wide_record_bound); + test::decode_calls = 0; + auto const refused = codecs::decode(chain, past); + BOOST_TEST(!refused.has_value() && refused.error() == codecs::error::input_too_long); + BOOST_TEST_EQ(test::decode_calls, 0U); + BOOST_TEST_EQ(arena.allocation_count(), 1U); + test::expect_no_allocation("an edge record", [&] { + auto const edge = codecs::decode(chain, at_edge); + BOOST_TEST(edge.has_value() && edge.value().count == test::edge_record_bound); + }); + BOOST_TEST_EQ(arena.allocation_count(), 1U); + // Secret: the heap intermediate wiped before the allocator gets it back, refusal included. + test::wipe_checking_allocator const checking; + auto const sealed_chain = codecs::over{formats::hex, test::bounded, checking}; + test::wipe_state const& state = *checking.state(); + test::clear_wipes(); + auto const sealed = codecs::decode(sealed_chain, at_bound); + BOOST_TEST(sealed.has_value() && sealed.value().count == test::wide_record_bound); + BOOST_TEST_EQ(state.allocations, 1U); + BOOST_TEST_EQ(test::wipes_of_size(test::wide_record_bound), 1U); + std::vector refused_bytes(test::wide_record_bound, 0x5a); + refused_bytes.front() = test::wide_record_refused_byte; + std::string const refused_text = hex_of(refused_bytes); + test::clear_wipes(); + auto const sealed_refused = + codecs::decode(sealed_chain, refused_text); + BOOST_TEST(!sealed_refused.has_value() && + sealed_refused.error() == codecs::error::invalid_prefix); + BOOST_TEST_EQ(state.allocations, 2U); + BOOST_TEST_EQ(test::wipes_of_size(test::wide_record_bound), 1U); + BOOST_TEST_EQ(state.deallocations, 2U); + BOOST_TEST_EQ(state.wiped_deallocations, 2U); + test::clear_wipes(); + auto const sealed_past = codecs::decode(sealed_chain, past); + BOOST_TEST(!sealed_past.has_value() && sealed_past.error() == codecs::error::input_too_long); + BOOST_TEST_EQ(state.allocations, 2U); + BOOST_TEST_EQ(test::wipes.count, 0U); +} + +/** The direct decode's error of `input_bytes` and the chains', from every text, agree. */ +template +void one_error_for(std::span input_bytes) { + auto const direct = codecs::decode(test::bounded, input_bytes); + BOOST_TEST(!direct.has_value() && direct.error() == codecs::error::input_too_long); + if (direct.has_value()) { + return; + } + std::string const hex_text = hex_of(input_bytes); + std::string const armor_text = armor_of(input_bytes); + codecs::bytes<32> const held = held_in_bytes_32(input_bytes); + auto const hex_fixed = codecs::encode(formats::hex, held); + auto const armor_fixed = codecs::encode(formats::armor("RECORD"), held); + test::decode_calls = 0; + for (auto const& chained : { + codecs::decode(hex_chain(), std::string_view{hex_text}), + codecs::decode(armor_chain(), std::string_view{armor_text}), + codecs::decode(hex_chain(), hex_fixed.value()), + codecs::decode(armor_chain(), armor_fixed.value()), + }) { + BOOST_TEST(!chained.has_value() && chained.error() == direct.error()); + } + BOOST_TEST_EQ(test::decode_calls, 0U); +} + +void one_error_whatever_the_path() { + one_error_for(test::record_past_bound_example); + one_error_for(test::record_past_bound_example); + // A bound below the largest valid input refuses it on every path. + one_error_for(test::record_version_2_example); +} + +void a_value_codec_with_no_decode_bound_is_unchanged() { + // The spec's scenario: be16 states no decode bound, so a run-time text takes the heap. + test::recording_resource arena; + auto const chain = codecs::over{formats::hex, test::be16, + std::pmr::polymorphic_allocator{&arena}}; + static_assert(!noexcept(codecs::decode(chain, std::string_view{}))); + auto const decoded = codecs::decode(chain, std::string_view{"a1b2"}); + BOOST_TEST(decoded.has_value() && decoded.value() == test::number{0xa1b2}); + BOOST_TEST_EQ(arena.allocation_count(), 1U); + BOOST_TEST(arena.allocation_count() == 1 && arena.allocation(0).size == 2); +} + +void a_chain_of_chains_holds_the_inner_level_in_place() { + // R8: the outer chain's own value codec states no decode bound, so its intermediate (the armor + // text) comes from its allocator; the inner level's is in place under the record's bound. + using inner_chain = codecs::over; + static_assert(!codecs::detail::has_decode_input_bound< + codecs::value_codec>); + test::recording_resource arena; + auto const chain = codecs::over{formats::hex, armor_chain(), + std::pmr::polymorphic_allocator{&arena}}; + std::string const armor_text = armor_of(test::record_version_2_example); + std::string const text = + codecs::encode(formats::hex, std::string_view{armor_text}, tokens::return_string).value(); + test::decode_calls = 0; + test::expect_no_allocation("a chain of chains, from the global heap", [&] { + auto const decoded = codecs::decode(chain, std::string_view{text}); + BOOST_TEST(decoded.has_value() && + decoded.value().held_bytes == test::record_example_content); + }); + BOOST_TEST_EQ(test::decode_calls, 1U); + BOOST_TEST_EQ(arena.allocation_count(), 1U); + BOOST_TEST(arena.allocation_count() == 1 && arena.allocation(0).size == armor_text.size()); +} + } // namespace } // namespace over_test @@ -654,5 +1014,15 @@ int main() { over_test::the_links_secret_overloads_run_in_a_secret_chain(); over_test::a_secret_token_makes_a_value_chain_secret(); over_test::a_secret_value_chain_runs_the_outer_links_secret_decode(); + over_test::a_value_from_a_run_time_text_has_no_heap(); + over_test::a_payload_past_the_bound_is_refused_before_the_intermediate(); + over_test::the_outer_link_errs_first(); + over_test::a_chain_at_fault_refuses_first(); + over_test::a_static_text_takes_the_smaller_bound(); + over_test::a_secret_intermediate_is_wiped_on_every_path(); + over_test::a_bound_above_the_cap_takes_the_allocator(); + over_test::one_error_whatever_the_path(); + over_test::a_value_codec_with_no_decode_bound_is_unchanged(); + over_test::a_chain_of_chains_holds_the_inner_level_in_place(); return boost::report_errors(); } diff --git a/test/support/bounded_records.hpp b/test/support/bounded_records.hpp index 0f65c51..e29a1a8 100644 --- a/test/support/bounded_records.hpp +++ b/test/support/bounded_records.hpp @@ -13,10 +13,12 @@ // codec. A tight_record is the same with a decode bound of 15, one below its largest valid input, // so that its 16-byte form is refused on every path (the bound is the acceptance contract). A // wide_record is the count of up to 4097 bytes, decode only, with a bound past -// detail::in_place_scratch_limit; a sealed_wide_record is the same, secret. A short_text is up to -// 20 letters, by a value codec of text whose bounds are both 20. Every function is noexcept, and -// every decode counts its calls in decode_calls, so that a test sees whether the core -// called the value codec. +// detail::in_place_scratch_limit, whose first byte is not ff (invalid_prefix otherwise); a +// sealed_wide_record is the same, secret; an edge_record is the same with a bound of 4096, at +// in_place_scratch_limit. A short_text is up to 20 letters, by a value codec of text whose bounds +// are both 20. Every function is noexcept, and every decode counts its calls in +// decode_calls, so that a test sees whether the core called the value codec. The example +// record's forms of version 1 and 2, and 17 bytes one past the bound, are spelled once here. #ifndef WEBCPP_CODECS_TEST_SUPPORT_BOUNDED_RECORDS_HPP #define WEBCPP_CODECS_TEST_SUPPORT_BOUNDED_RECORDS_HPP @@ -79,6 +81,12 @@ struct sealed_wide_record { std::size_t count = 0; }; +/** The count of up to 4096 bytes, decoded only: a wide_record at in_place_scratch_limit. */ +struct edge_record { + /** The count. */ + std::size_t count = 0; +}; + /** Up to 20 letters. */ struct short_text { /** The letters, those past `size` zero. */ @@ -241,26 +249,39 @@ struct record_codec { /** The most bytes a wide record counts. */ inline constexpr std::size_t wide_record_bound = 4097; +/** The most bytes an edge record counts: detail::in_place_scratch_limit. */ +inline constexpr std::size_t edge_record_bound = 4096; + +/** The first byte a wide record refuses, so that a test sees the value codec's own refusal. */ +inline constexpr std::uint8_t wide_record_refused_byte = 0xff; + /** The value codec of bounded for a count of bytes, decode only, which each record's specialization states the secrecy of. - @tparam Record wide_record or sealed_wide_record. + @tparam Record wide_record, sealed_wide_record or edge_record. + @tparam Bound The most bytes it counts, its decode bound. */ -template +template struct wide_codec { /** Bytes. */ static constexpr output_kind output = output_kind::bytes; /** The most bytes it counts. */ - static constexpr std::size_t max_decode_input_size = wide_record_bound; + static constexpr std::size_t max_decode_input_size = Bound; - /** The count of the bytes: any up to 4097, longer invalid_length; counts the call. */ + /** + The count of the bytes: any up to `Bound`, longer invalid_length, a first byte ff + invalid_prefix; counts the call. + */ static result decode(bounded_t const& /*format*/, std::span input_bytes) noexcept { ++decode_calls; - if (input_bytes.size() > wide_record_bound) { + if (input_bytes.size() > Bound) { return error::invalid_length; } + if (!input_bytes.empty() && input_bytes.front() == wide_record_refused_byte) { + return error::invalid_prefix; + } return Record{input_bytes.size()}; } }; @@ -312,6 +333,11 @@ struct value_codec static constexpr bool secret = true; }; +/** An edge record: decode only, a bound of 4096, held in place. */ +template <> +struct value_codec + : test::wide_codec {}; + /** A short text: up to 20 letters, both bounds 20. */ template <> struct value_codec { From df262ada64e910839b058d6ceeeaaa93377fd9f0 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 15:22:22 -0300 Subject: [PATCH 05/30] codecs: over's decode bound tests lint fix --- test/allocation_test.cpp | 7 ++++--- test/over_test.cpp | 27 ++++++++++++--------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/test/allocation_test.cpp b/test/allocation_test.cpp index d9137e0..6457589 100644 --- a/test/allocation_test.cpp +++ b/test/allocation_test.cpp @@ -472,9 +472,10 @@ void check_over() { void check_over_value_decode_bound() { constexpr codecs::over hex_chain{codecs::formats::hex, test::bounded}; codecs::over const armor_chain{codecs::formats::armor("RECORD"), test::bounded}; - for (std::span const form : - {std::span{test::record_version_1_example}, - std::span{test::record_version_2_example}}) { + for (std::span const form : { + std::span{test::record_version_1_example}, + std::span{test::record_version_2_example}, + }) { std::string const hex_text = codecs::encode(codecs::formats::hex, form, codecs::tokens::return_string).value(); std::string const armor_text = diff --git a/test/over_test.cpp b/test/over_test.cpp index e9eb507..de2e4a9 100644 --- a/test/over_test.cpp +++ b/test/over_test.cpp @@ -44,7 +44,6 @@ #include #include #include -#include #include #include #include @@ -724,9 +723,10 @@ void a_value_from_a_run_time_text_has_no_heap() { static_assert(noexcept(codecs::decode(hex_chain(), std::string{}))); auto const hex_link = hex_chain(); auto const armor_link = armor_chain(); - for (std::span const form : - {std::span{test::record_version_1_example}, - std::span{test::record_version_2_example}}) { + for (std::span const form : { + std::span{test::record_version_1_example}, + std::span{test::record_version_2_example}, + }) { std::string const hex_text = hex_of(form); std::string const armor_text = armor_of(form); std::string const crlf_text = with_crlf(armor_text); @@ -764,18 +764,15 @@ void a_payload_past_the_bound_is_refused_before_the_intermediate() { codecs::error::input_too_long); refused_with_no_call("armor, 17 bytes", armor_chain(), armor_text, codecs::error::input_too_long); - // No byte: the value codec's own invalid_length. + // No byte: the value codec's own invalid_length, the value codec called. std::string const empty_armor = armor_of({}); - for (auto const& [what, decoded] : - {std::pair{"hex, no byte", - codecs::decode(hex_chain(), std::string_view{})}, - std::pair{"armor, no byte", codecs::decode( - armor_chain(), std::string_view{empty_armor})}}) { - if (decoded.has_value() || decoded.error() != codecs::error::invalid_length) { - std::printf("not invalid_length: %s\n", what); - } - BOOST_TEST(!decoded.has_value() && decoded.error() == codecs::error::invalid_length); - } + test::decode_calls = 0; + auto const hex_empty = codecs::decode(hex_chain(), std::string_view{}); + BOOST_TEST(!hex_empty.has_value() && hex_empty.error() == codecs::error::invalid_length); + auto const armor_empty = + codecs::decode(armor_chain(), std::string_view{empty_armor}); + BOOST_TEST(!armor_empty.has_value() && armor_empty.error() == codecs::error::invalid_length); + BOOST_TEST_EQ(test::decode_calls, 2U); } void the_outer_link_errs_first() { From 50f1dc8004e41166d3b9d37c0c98ea18f16e131e Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 15:52:09 -0300 Subject: [PATCH 06/30] codecs: over's decode bound tests and Doc Comments completed --- include/webcpp/codecs/detail/core/over.hpp | 11 +++- test/noexcept_test.cpp | 3 + test/over_test.cpp | 67 +++++++++++++++++++++- test/support/bounded_records.hpp | 43 ++++++++------ 4 files changed, 100 insertions(+), 24 deletions(-) diff --git a/include/webcpp/codecs/detail/core/over.hpp b/include/webcpp/codecs/detail/core/over.hpp index 8850e53..4aea17b 100644 --- a/include/webcpp/codecs/detail/core/over.hpp +++ b/include/webcpp/codecs/detail/core/over.hpp @@ -96,7 +96,10 @@ struct over_name { `error::input_too_long` before it is made, after the outer link's own error, as a direct decode refuses that input (owner decision 29): nothing is written, allocated or wiped; so a value codec with a bound of at most 4096 bytes is decoded from a `std::string_view` with no heap, and one with - a bound above it from the allocator, of exactly the written size. The exact `encoded_size` and + a bound above it from the allocator, of exactly the written size. `over`'s own value codec states + no decode bound (design R8): a link may decode a text longer than the longest it writes, so in a + chain of chains only the level whose inner link is that value codec holds its intermediate in + place for a text of a dynamic size. The exact `encoded_size` and `decoded_size` run the inner link into the intermediate (ruling R3): with a heap intermediate they allocate, so those, `encode` and `decode` are not `noexcept` and an exception of the allocator reaches the caller, the buffer forms of the core among them (the one allocating buffer form), @@ -952,8 +955,10 @@ template first, and its error is the chain's; an exact size past the inner value codec's decode bound (`max_decode_input_size`) is then @ref error::input_too_long, the same error as a direct decode's (owner decision 29), before the intermediate is made: nothing is written, allocated or wiped. The - intermediate is in place under over_value_middle_bound, else from the chain's allocator, of - exactly the written size; when the chain is secret it is wiped on every path that makes it. + intermediate is in place under over_value_middle_bound (the smaller of the outer link's bound on a + text of a static size and the decode bound when that is at most `in_place_scratch_limit`, 4096 + bytes), else from the chain's allocator, of exactly the written size; when the chain is secret it + is wiped on every path that makes it. */ template [[nodiscard]] result diff --git a/test/noexcept_test.cpp b/test/noexcept_test.cpp index 92693fc..4f12b52 100644 --- a/test/noexcept_test.cpp +++ b/test/noexcept_test.cpp @@ -263,6 +263,9 @@ static_assert(noexcept(codecs::decode(std::declval(std::declval(), codecs::text<64>{}))); +// A value codec of text: the intermediate is text, in place under its bound of 20. +static_assert(noexcept(codecs::decode(std::declval(), + std::string_view{}))); static_assert(noexcept(codecs::decode(std::declval(), std::string_view{}))); static_assert(!noexcept(codecs::decode(std::declval(), diff --git a/test/over_test.cpp b/test/over_test.cpp index de2e4a9..188db3a 100644 --- a/test/over_test.cpp +++ b/test/over_test.cpp @@ -828,6 +828,43 @@ void a_static_text_takes_the_smaller_bound() { BOOST_TEST(!refused.has_value() && refused.error() == codecs::error::input_too_long); }); BOOST_TEST_EQ(test::decode_calls, 0U); + // The armor's bound on a text of N characters is base64's, 3 N / 4: below 16 for a text<20>, + // which no armor of a record fits, and above it for every armor's text (one of 12 bytes is + // already some 50 characters), which the value codec's bound then holds in place. + auto const armor_link = armor_chain(); + static_assert(codecs::detail::over_value_middle_bound>() == + 15); + auto const armor_short_text = + codecs::encode(formats::armor("RECORD"), test::record_version_1_example); + auto const armor_wide_text = + codecs::encode(formats::armor("RECORD"), held_in_bytes_32(test::record_version_2_example)); + using armor_short_view = std::remove_cvref_t; + using armor_wide_view = std::remove_cvref_t; + static_assert(codecs::detail::over_value_middle_bound() == + 16); + static_assert(codecs::detail::over_value_middle_bound() == + 16); + static_assert(noexcept(codecs::decode(armor_link, armor_short_view{}))); + static_assert(noexcept(codecs::decode(armor_link, armor_wide_view{}))); + decodes_the_example_with_no_heap("armor, the text of 12 bytes", armor_link, + armor_short_text.value()); + decodes_the_example_with_no_heap("armor, the text of a bytes<32> of 16", armor_link, + armor_wide_text.value()); +} + +/** + Whether the wipes since the last `clear_wipes` are one of the intermediate, of `intermediate_size` + bytes, and else only of the record's own `secure::secret<8>` (the value codec's local record and + the result's moved-from one), so that no stray wipe of another size passes. +*/ +bool wiped_the_intermediate_once(std::size_t intermediate_size) { + constexpr std::size_t record_size = test::record_example_content.size(); + return test::wipes_of_size(intermediate_size) == 1 && + test::wipes.count == 1 + test::wipes_of_size(record_size); } /** Decodes `text` into a `Value` through the hex chain and reports what it wiped. */ @@ -844,18 +881,18 @@ void a_secret_intermediate_is_wiped_on_every_path() { BOOST_TEST(from_version_2.has_value() && std::ranges::equal(from_version_2.value().held_bytes.bytes(), test::record_example_content)); - BOOST_TEST_EQ(test::wipes_of_size(test::record_version_2_size), 1U); + BOOST_TEST(wiped_the_intermediate_once(test::record_version_2_size)); std::string const version_1_text = hex_of(test::record_version_1_example); auto const from_version_1 = decoded_with_wipes(version_1_text); BOOST_TEST(from_version_1.has_value()); - BOOST_TEST_EQ(test::wipes_of_size(test::record_version_1_size), 1U); + BOOST_TEST(wiped_the_intermediate_once(test::record_version_1_size)); // Refused by the value codec: the written intermediate wiped all the same. std::array wrong_prefix = test::record_version_1_example; wrong_prefix[0] = 0x31; auto const refused = decoded_with_wipes(hex_of(wrong_prefix)); BOOST_TEST(!refused.has_value() && refused.error() == codecs::error::invalid_prefix); - BOOST_TEST_EQ(test::wipes_of_size(test::record_version_1_size), 1U); + BOOST_TEST(wiped_the_intermediate_once(test::record_version_1_size)); // Refused by the outer link's decoded_size, and past the bound: no intermediate, no wipe. std::string bad_digit = version_2_text; bad_digit.back() = 'g'; @@ -873,6 +910,29 @@ void a_secret_intermediate_is_wiped_on_every_path() { BOOST_TEST_EQ(test::wipes.count, 0U); } +void a_text_value_from_a_run_time_text_has_no_heap() { + // A value codec of text (short_text, both bounds 20): the intermediate is text, in place under + // the decode bound from a run-time text, and the decode noexcept. + static_assert(noexcept(codecs::decode(hex_chain(), std::string_view{}))); + constexpr std::string_view letters{"TwentyLettersOfTexts"}; + static_assert(letters.size() == test::short_text_bound); + std::string const hex_text = + codecs::encode(formats::hex, letters, tokens::return_string).value(); + auto const hex_link = hex_chain(); + test::decode_calls = 0; + test::expect_no_allocation("a short text, a string view", [&] { + auto const decoded = codecs::decode(hex_link, std::string_view{hex_text}); + BOOST_TEST(decoded.has_value() && decoded.value().view() == letters); + }); + BOOST_TEST_EQ(test::decode_calls, 1U); + // 21 letters: past the bound, refused before the intermediate, the value codec uncalled. + std::string const long_letters = std::string{letters} + "x"; + std::string const long_text = + codecs::encode(formats::hex, std::string_view{long_letters}, tokens::return_string).value(); + refused_with_no_call("a short text, 21 letters", hex_link, long_text, + codecs::error::input_too_long); +} + void a_bound_above_the_cap_takes_the_allocator() { // owner decision 30: 4097 bytes from the allocator, exactly; 4096 in place; 4098 refused first. test::recording_resource arena; @@ -1017,6 +1077,7 @@ int main() { over_test::a_chain_at_fault_refuses_first(); over_test::a_static_text_takes_the_smaller_bound(); over_test::a_secret_intermediate_is_wiped_on_every_path(); + over_test::a_text_value_from_a_run_time_text_has_no_heap(); over_test::a_bound_above_the_cap_takes_the_allocator(); over_test::one_error_whatever_the_path(); over_test::a_value_codec_with_no_decode_bound_is_unchanged(); diff --git a/test/support/bounded_records.hpp b/test/support/bounded_records.hpp index e29a1a8..07e926f 100644 --- a/test/support/bounded_records.hpp +++ b/test/support/bounded_records.hpp @@ -150,32 +150,39 @@ inline constexpr std::size_t record_version_2_size = 16; /** The prefix of a record's form of version 2. */ inline constexpr std::array record_version_2_prefix{0x30, 0x0e, 0x02, 0x01}; +/** The bytes after the record in a form of version 2, before its first 2 bytes again. */ +inline constexpr std::array record_version_2_marker{0xa1, 0x02}; + /** The 8 bytes of the tests' example record. */ inline constexpr std::array record_example_content{ 0x10, 0x11, 0x12, 0x13, // the first half 0x14, 0x15, 0x16, 0x17, // the second half }; +/** The bytes of `first` followed by those of `second`. */ +template +constexpr std::array joined( + std::array const& first, + std::array const& second) noexcept { + std::array both{}; + std::ranges::copy(first, both.begin()); + std::ranges::copy(second, both.begin() + First); + return both; +} + /** The example record's form of version 1: 12 bytes, its encoded form. */ -inline constexpr std::array record_version_1_example{ - 0x30, 0x0a, 0x02, 0x00, // the prefix of version 1 - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record -}; +inline constexpr std::array record_version_1_example = + joined(record_version_1_prefix, record_example_content); /** The example record's form of version 2: 16 bytes, its largest valid input. */ -inline constexpr std::array record_version_2_example{ - 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record - 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes -}; +inline constexpr std::array record_version_2_example = joined( + joined(record_version_2_prefix, record_example_content), + joined(record_version_2_marker, + std::array{record_example_content[0], record_example_content[1]})); /** 17 bytes: the example record's form of version 2 and one more, one past the bound. */ -inline constexpr std::array record_past_bound_example{ - 0x30, 0x0e, 0x02, 0x01, // the prefix of version 2 - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // the record - 0xa1, 0x02, 0x10, 0x11, // a1 02 and the record's first 2 bytes - 0x00, // one byte past the bound -}; +inline constexpr std::array record_past_bound_example = + joined(record_version_2_example, std::array{0x00}); /** Reads a record's form of version 1 or 2 into `held_bytes`. @@ -192,9 +199,9 @@ inline std::optional read_record(std::span input_byte return error::invalid_prefix; } } else if (input_bytes.size() == record_version_2_size) { - if (!std::ranges::equal(prefix, record_version_2_prefix) || input_bytes[12] != 0xa1 || - input_bytes[13] != 0x02 || input_bytes[14] != input_bytes[4] || - input_bytes[15] != input_bytes[5]) { + if (!std::ranges::equal(prefix, record_version_2_prefix) || + !std::ranges::equal(input_bytes.subspan(12, 2), record_version_2_marker) || + input_bytes[14] != input_bytes[4] || input_bytes[15] != input_bytes[5]) { return error::invalid_prefix; } } else { From bf02539bb6ef3ad3e2d896e838263f7121ed9153 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 15:55:33 -0300 Subject: [PATCH 07/30] codecs: the constant-time path and the fuzz target of a value decoded through over under its decode bound --- test/constant_time/paths.hpp | 102 +++++++++- test/fuzz/Jamfile | 13 +- test/fuzz/corpus/over_value/armor_crlf | 3 + test/fuzz/corpus/over_value/armor_past_bound | 3 + .../over_value/armor_past_bound_bad_symbol | 3 + test/fuzz/corpus/over_value/armor_version_1 | 3 + test/fuzz/corpus/over_value/armor_version_2 | 3 + test/fuzz/corpus/over_value/empty | 1 + .../corpus/over_value/hex_odd_digits_last_g | 1 + test/fuzz/corpus/over_value/hex_past_bound | 1 + .../corpus/over_value/hex_record_version_1 | 1 + .../corpus/over_value/hex_record_version_2 | 1 + test/fuzz/over_value_fuzz.cpp | 189 ++++++++++++++++++ test/support/bounded_records.hpp | 74 ++++++- 14 files changed, 390 insertions(+), 8 deletions(-) create mode 100644 test/fuzz/corpus/over_value/armor_crlf create mode 100644 test/fuzz/corpus/over_value/armor_past_bound create mode 100644 test/fuzz/corpus/over_value/armor_past_bound_bad_symbol create mode 100644 test/fuzz/corpus/over_value/armor_version_1 create mode 100644 test/fuzz/corpus/over_value/armor_version_2 create mode 100644 test/fuzz/corpus/over_value/empty create mode 100644 test/fuzz/corpus/over_value/hex_odd_digits_last_g create mode 100644 test/fuzz/corpus/over_value/hex_past_bound create mode 100644 test/fuzz/corpus/over_value/hex_record_version_1 create mode 100644 test/fuzz/corpus/over_value/hex_record_version_2 create mode 100644 test/fuzz/over_value_fuzz.cpp diff --git a/test/constant_time/paths.hpp b/test/constant_time/paths.hpp index 737d2cf..0fbd520 100644 --- a/test/constant_time/paths.hpp +++ b/test/constant_time/paths.hpp @@ -35,7 +35,11 @@ // form, from a secure::secret<64> and through the secret stream, and their armor decoded // one-shot, in the buffer form, to secret_bytes, into a secure::secret<64> and through the secret // stream, its symbols alone marked secret (the armor's structure, its boundaries and line -// endings, is public, B45), and texts of each fault its payload can have. A +// endings, is public, B45), and texts of each fault its payload can have; over's, a secret value +// decoded under its value codec's decode bound (test::secret_blob, 16 bytes) through +// over{armor("TEST SECRET"), test::bounded} from a std::string_view, with line feeds and with CRLF +// lines, its payload's symbols secret, and 17 bytes' armor refused with input_too_long on its +// public length, and the same value decoded directly from 16 secret bytes and refused at 17. A // branch or an index on a secret is the checker's report, which fails the run. Each output is then // checked to carry the mark, so that the path is traced, where the checker's shadow follows a // subtraction's carry (exact_subtraction: memcheck; MemorySanitizer's drops it, secure's @@ -57,6 +61,7 @@ #include "../support/base58_checksum.hpp" #include "../support/base64_reference.hpp" #include "../support/bech32_reference.hpp" +#include "../support/bounded_records.hpp" #include "../support/constant_time_marks.hpp" #include @@ -1482,6 +1487,99 @@ int armor_paths() { armor_invalid_paths(); } +/** The label of the armor a secret value is decoded through, public. */ +inline constexpr std::string_view over_value_label = "TEST SECRET"; + +/** + * The armor of the first `size` bytes of `hex_plain_bytes` under `over_value_label`, written + * plainly from public bytes by the reference, each line ended by a carriage return and a line + * feed when `crlf`. + */ +inline std::string over_value_text(std::size_t size, bool crlf) { + std::array const plain = hex_plain_bytes(); + std::string const text = reference_armor_encode(over_value_label, std::span{plain}.first(size)); + if (!crlf) { + return text; + } + std::string lines; + for (char const character : text) { + if (character == '\n') { + lines.push_back('\r'); + } + lines.push_back(character); + } + return lines; +} + +/** + * Marks the payload of an armor of `over_value_text`'s layout secret: the symbols of its one line + * of payload, after the BEGIN line, up to its line ending; the boundaries and line endings stay + * public (B45). The layout is read before the mark, from the public text. + */ +template +void mark_over_value_payload(std::string& text) { + std::size_t const first = text.find('\n') + 1; + std::size_t const end = text.find_first_of("\r\n", first); + Checker::mark_secret(writable_bytes_of_characters(std::span{text}.subspan(first, end - first))); +} + +/** + * A secret value decoded through over under its decode bound (design D2, D3): + * test::secret_blob, whose value codec states a decode bound of 16 bytes, through + * `over{armor("TEST SECRET"), test::bounded}` from a `std::string_view` of 16 bytes' armor, with + * line feeds and with CRLF lines, its symbols secret: the intermediate is in place under the bound + * and the chain is secret (the outer link's secret decode, the intermediate wiped); the result's + * bytes are traced. 17 bytes' armor is refused with input_too_long on its length, which is public, + * before the intermediate is made. The same value decoded directly from 16 secret bytes, and 17 + * refused on their length. + */ +template +int over_value_paths() { + namespace formats = codecs::formats; + char const* const name = "over value decode"; + std::array const plain = hex_plain_bytes(); + std::span const expected = std::span{plain}.first(test::secret_blob_bound); + auto const chain = codecs::over{formats::armor(over_value_label), test::bounded}; + int failures = 0; + for (bool const crlf : {false, true}) { + char const* const form = crlf ? ", armor, CRLF lines" : ", armor"; + std::string text = over_value_text(test::secret_blob_bound, crlf); + mark_over_value_payload(text); + auto const decoded = codecs::decode(chain, std::string_view{text}); + if (check(public_success(decoded.has_value()), name, form) == 0) { + failures += check(decoded->size == test::secret_blob_bound, name, form); + failures += traced_and_equal(decoded->view(), expected, name, form); + } else { + ++failures; + } + std::string past = over_value_text(test::secret_blob_bound + 1, crlf); + mark_over_value_payload(past); + auto const refused = codecs::decode(chain, std::string_view{past}); + failures += check(!public_success(refused.has_value()) && + refused.error() == error::input_too_long, + "over value decode of 17 bytes", form); + } + + // The value codec directly, its input secret. + std::array input{}; + std::ranges::copy(std::span{plain}.first(input.size()), input.begin()); + Checker::mark_secret(input); + auto const direct = codecs::decode( + test::bounded, std::span{input}.first(test::secret_blob_bound)); + if (check(public_success(direct.has_value()), name, ", direct") == 0) { + failures += check(direct->size == test::secret_blob_bound, name, ", direct"); + failures += traced_and_equal(direct->view(), expected, name, ", direct"); + } else { + ++failures; + } + auto const direct_past = + codecs::decode(test::bounded, std::span{input}); + failures += check(!public_success(direct_past.has_value()) && + direct_past.error() == error::input_too_long, + "over value decode of 17 bytes", ", direct"); + return failures; +} + /** Every format's block, on the checker: the number of failed checks. */ template int format_paths() { @@ -1496,7 +1594,7 @@ int format_paths() { base32_configuration_paths() + bech32_configuration_paths() + bech32_configuration_paths() + base58_paths() + - armor_paths(); + armor_paths() + over_value_paths(); } #ifdef WEBCPP_TEST_CODECS_PLANTED_HEX diff --git a/test/fuzz/Jamfile b/test/fuzz/Jamfile index b6b81a4..ecfbb3c 100644 --- a/test/fuzz/Jamfile +++ b/test/fuzz/Jamfile @@ -74,6 +74,13 @@ codecs-fuzz hex ; # over{hex, hex}: the RFC's texts twice encoded, an inner text in upper case, # and each link's faults. codecs-fuzz over ; +# A value decoded through over under its value codec's decode bound (design +# D7): over{hex, test::bounded} or over{armor("RECORD"), test::bounded}, the +# split seed's first byte even or odd choosing; the record's two forms of 12 +# and 16 bytes in each link, no byte, 17 bytes, 35 hex digits ending in g, an +# armor with CRLF lines, and an armor of 17 bytes whose last symbol is bad, +# each file named so that its seed's first byte chooses the link it is for. +codecs-fuzz over_value ; # base32 and base32hex: RFC 4648's texts with the padding and without it, in # either case, the symbols of each alphabet and the other's, non-zero trailing @@ -138,6 +145,6 @@ codecs-fuzz percent : /webcpp/codecs//percent ; # lines as the format writes them, under the label the parts decode returned. codecs-fuzz armor ; -webcpp.lane fuzz : hex_fuzz over_fuzz base32_fuzz base32hex_fuzz base64_fuzz base64url_fuzz - bech32_fuzz bech32m_fuzz bech32_words_fuzz base58_fuzz base58_with_checksum_fuzz percent_fuzz - armor_fuzz : native ; +webcpp.lane fuzz : hex_fuzz over_fuzz over_value_fuzz base32_fuzz base32hex_fuzz base64_fuzz + base64url_fuzz bech32_fuzz bech32m_fuzz bech32_words_fuzz base58_fuzz base58_with_checksum_fuzz + percent_fuzz armor_fuzz : native ; diff --git a/test/fuzz/corpus/over_value/armor_crlf b/test/fuzz/corpus/over_value/armor_crlf new file mode 100644 index 0000000..e332f92 --- /dev/null +++ b/test/fuzz/corpus/over_value/armor_crlf @@ -0,0 +1,3 @@ +/½üj-----BEGIN RECORD----- +MA4CARAREhMUFRYXoQIQEQ== +-----END RECORD----- diff --git a/test/fuzz/corpus/over_value/armor_past_bound b/test/fuzz/corpus/over_value/armor_past_bound new file mode 100644 index 0000000..e26a7bc --- /dev/null +++ b/test/fuzz/corpus/over_value/armor_past_bound @@ -0,0 +1,3 @@ +KѶî-----BEGIN RECORD----- +MA4CARAREhMUFRYXoQIQEQA= +-----END RECORD----- diff --git a/test/fuzz/corpus/over_value/armor_past_bound_bad_symbol b/test/fuzz/corpus/over_value/armor_past_bound_bad_symbol new file mode 100644 index 0000000..2492658 --- /dev/null +++ b/test/fuzz/corpus/over_value/armor_past_bound_bad_symbol @@ -0,0 +1,3 @@ +“½æì-----BEGIN RECORD----- +MA4CARAREhMUFRYXoQIQEQ*= +-----END RECORD----- diff --git a/test/fuzz/corpus/over_value/armor_version_1 b/test/fuzz/corpus/over_value/armor_version_1 new file mode 100644 index 0000000..c11a24c --- /dev/null +++ b/test/fuzz/corpus/over_value/armor_version_1 @@ -0,0 +1,3 @@ +KÔR»-----BEGIN RECORD----- +MAoCABAREhMUFRYX +-----END RECORD----- diff --git a/test/fuzz/corpus/over_value/armor_version_2 b/test/fuzz/corpus/over_value/armor_version_2 new file mode 100644 index 0000000..353b9d0 --- /dev/null +++ b/test/fuzz/corpus/over_value/armor_version_2 @@ -0,0 +1,3 @@ +‰¨¦-----BEGIN RECORD----- +MA4CARAREhMUFRYXoQIQEQ== +-----END RECORD----- diff --git a/test/fuzz/corpus/over_value/empty b/test/fuzz/corpus/over_value/empty new file mode 100644 index 0000000..5cca61f --- /dev/null +++ b/test/fuzz/corpus/over_value/empty @@ -0,0 +1 @@ +.ú‚ \ No newline at end of file diff --git a/test/fuzz/corpus/over_value/hex_odd_digits_last_g b/test/fuzz/corpus/over_value/hex_odd_digits_last_g new file mode 100644 index 0000000..f83d949 --- /dev/null +++ b/test/fuzz/corpus/over_value/hex_odd_digits_last_g @@ -0,0 +1 @@ +v ;ï300e02011011121314151617a102101100g \ No newline at end of file diff --git a/test/fuzz/corpus/over_value/hex_past_bound b/test/fuzz/corpus/over_value/hex_past_bound new file mode 100644 index 0000000..5e8e91a --- /dev/null +++ b/test/fuzz/corpus/over_value/hex_past_bound @@ -0,0 +1 @@ +p�40300e02011011121314151617a102101100 \ No newline at end of file diff --git a/test/fuzz/corpus/over_value/hex_record_version_1 b/test/fuzz/corpus/over_value/hex_record_version_1 new file mode 100644 index 0000000..93de528 --- /dev/null +++ b/test/fuzz/corpus/over_value/hex_record_version_1 @@ -0,0 +1 @@ +@#_G300a02001011121314151617 \ No newline at end of file diff --git a/test/fuzz/corpus/over_value/hex_record_version_2 b/test/fuzz/corpus/over_value/hex_record_version_2 new file mode 100644 index 0000000..873e74a --- /dev/null +++ b/test/fuzz/corpus/over_value/hex_record_version_2 @@ -0,0 +1 @@ +,’ƒ¡300e02011011121314151617a1021011 \ No newline at end of file diff --git a/test/fuzz/over_value_fuzz.cpp b/test/fuzz/over_value_fuzz.cpp new file mode 100644 index 0000000..98d0559 --- /dev/null +++ b/test/fuzz/over_value_fuzz.cpp @@ -0,0 +1,189 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// The fuzz target of a value decoded through over under its value codec's decode bound (design +// D7): the input's first byte, the split seed's first (preflight F15), chooses the outer link, +// over{formats::hex, test::bounded} when it is even and over{formats::armor("RECORD"), +// test::bounded} when it is odd; the bytes after the seed (target.hpp's split_seed_size) are the +// text, a std::string_view. The reference is made first, outside the counted region: the outer +// link's own decode of the text into a std::vector, whose error is the chain's; then more than 16 +// bytes are input_too_long, and 16 or fewer what a model of short_record's value codec written +// here reads (its two forms, invalid_length, invalid_prefix); the direct decode of the reference's +// bytes, codecs::decode(test::bounded, bytes), gives the same. Then, with no +// allocation counted (fuzz_allocations before and after equal), the chain decodes the text into a +// short_record and into a sealed_record (secret, its intermediate wiped); both equal the +// reference, the same record or the same error (its value and its category). A record that +// decodes encodes through the chain to a text that decodes to the same record, also with no +// allocation. + +#include "target.hpp" + +#include "../support/bounded_records.hpp" + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace { + +namespace codecs = webcpp::codecs; +namespace test = webcpp::codecs::test; +using test::fuzz_detail::expect; + +/** + The longest input the target reads; a longer one is passed over. libFuzzer's inputs are 4096 + bytes at most unless its corpus holds a longer one, and the replay's are shorter. +*/ +constexpr std::size_t longest_input = 16384; + +/** The bytes of a short record's two forms and its decode bound, spelled here for the model. */ +constexpr std::size_t model_version_1_size = 12; +/** The bytes of the form of version 2. */ +constexpr std::size_t model_version_2_size = 16; +/** The prefix of the form of version 1. */ +constexpr std::array model_version_1_prefix{0x30, 0x0a, 0x02, 0x00}; +/** The prefix of the form of version 2. */ +constexpr std::array model_version_2_prefix{0x30, 0x0e, 0x02, 0x01}; + +/** + The model of short_record's value codec under its decode bound: more than 16 bytes are + input_too_long (the core's refusal, before the codec), 12 bytes the form of version 1, 16 the + form of version 2 (`a1 02` and the record's first two bytes after it), another length + invalid_length, another byte invalid_prefix. + + @param bytes The bytes the outer link decoded. + @return The record, or the error. +*/ +codecs::result modeled_record(std::span bytes) { + if (bytes.size() > model_version_2_size) { + return codecs::error::input_too_long; + } + if (bytes.size() != model_version_1_size && bytes.size() != model_version_2_size) { + return codecs::error::invalid_length; + } + bool const version_1 = bytes.size() == model_version_1_size; + auto const prefix = bytes.first(4); + bool const valid = version_1 ? std::ranges::equal(prefix, model_version_1_prefix) + : std::ranges::equal(prefix, model_version_2_prefix) && + bytes[12] == 0xa1 && bytes[13] == 0x02 && + bytes[14] == bytes[4] && bytes[15] == bytes[5]; + if (!valid) { + return codecs::error::invalid_prefix; + } + test::short_record record{}; + std::ranges::copy(bytes.subspan(4, 8), record.held_bytes.begin()); + return record; +} + +/** + Whether `decoded`, a record of `Record` or an error, equals `expected`: the same 8 bytes, or the + same error, its value and its category. + + @param decoded The chain's result. + @param expected The reference. + @return Whether they agree. +*/ +template +bool agrees(codecs::result const& decoded, + codecs::result const& expected) { + if (decoded.has_value() != expected.has_value()) { + return false; + } + if (!expected.has_value()) { + boost::system::error_code const decoded_error = decoded.error(); + boost::system::error_code const expected_error = expected.error(); + return decoded_error.value() == expected_error.value() && + decoded_error.category() == expected_error.category(); + } + return std::ranges::equal(test::record_bytes(decoded.value()), + test::record_bytes(expected.value())); +} + +/** + Encodes `record` through `chain` and decodes the text back, with no allocation: the same record. + + @param chain The chain. + @param record The record decoded, a short_record or a sealed_record. +*/ +template +void check_round_trip(Chain const& chain, Record const& record) { + std::size_t const before = test::fuzz_allocations(); + auto const encoded = codecs::encode(chain, record); + expect(encoded.has_value(), "a decoded record does not encode through the chain"); + if (!encoded.has_value()) { + return; + } + auto const decoded = codecs::decode(chain, encoded.value().view()); + expect(test::fuzz_allocations() == before, "the round trip through the chain allocated"); + expect(decoded.has_value() && + std::ranges::equal(test::record_bytes(decoded.value()), test::record_bytes(record)), + "a decoded record's text does not decode to the same record"); +} + +/** + The checks of one input's text through `chain`, whose outer link is `outer`. + + @param outer The outer link alone, for the reference. + @param chain The chain over test::bounded. + @param text The text. +*/ +template +void check_text(Outer const& outer, Chain const& chain, std::string_view text) { + // The reference, outside the counted region. + auto const outer_bytes = codecs::decode>(outer, text); + codecs::result expected = codecs::error::invalid_length; + if (outer_bytes.has_value()) { + std::span const bytes{outer_bytes.value()}; + expected = modeled_record(bytes); + expect(agrees(codecs::decode(test::bounded, bytes), expected), + "the direct decode differs from the model"); + } else { + expected = outer_bytes.error(); + } + // The chain's decodes, with no allocation. + std::size_t const before = test::fuzz_allocations(); + auto const record = codecs::decode(chain, text); + auto const sealed = codecs::decode(chain, text); + expect(test::fuzz_allocations() == before, "a decode of a value through the chain allocated"); + expect(agrees(record, expected), "the chain's short_record differs from the reference"); + expect(agrees(sealed, expected), "the chain's sealed_record differs from the reference"); + if (record.has_value()) { + check_round_trip(chain, record.value()); + } + if (sealed.has_value()) { + check_round_trip(chain, sealed.value()); + } +} + +} // namespace + +// NOLINTNEXTLINE(readability-identifier-naming): libFuzzer's name for the target +extern "C" int LLVMFuzzerTestOneInput(std::uint8_t const* data, std::size_t size) { + if (size > longest_input) { + return 0; + } + std::size_t const seed_size = std::min(size, test::split_seed_size); + // An input of no byte is a seed of zeros (target.hpp): even, the hex chain. + bool const armored = size != 0 && (data[0] % 2) != 0; + // A byte is read through a char, which may alias any object. + std::string_view const text{reinterpret_cast(data) + seed_size, size - seed_size}; + if (armored) { + auto const outer = codecs::formats::armor("RECORD"); + check_text(outer, codecs::over{outer, test::bounded}, text); + } else { + check_text(codecs::formats::hex, codecs::over{codecs::formats::hex, test::bounded}, text); + } + return 0; +} diff --git a/test/support/bounded_records.hpp b/test/support/bounded_records.hpp index 07e926f..2c98fca 100644 --- a/test/support/bounded_records.hpp +++ b/test/support/bounded_records.hpp @@ -16,9 +16,12 @@ // detail::in_place_scratch_limit, whose first byte is not ff (invalid_prefix otherwise); a // sealed_wide_record is the same, secret; an edge_record is the same with a bound of 4096, at // in_place_scratch_limit. A short_text is up to 20 letters, by a value codec of text whose bounds -// are both 20. Every function is noexcept, and every decode counts its calls in -// decode_calls, so that a test sees whether the core called the value codec. The example -// record's forms of version 1 and 2, and 17 bytes one past the bound, are spelled once here. +// are both 20. A secret_blob, for the constant-time lane, is up to 16 secret bytes, both bounds 16: +// its decode checks the length alone, which is public, and copies the bytes into its +// secure::secret<16>, with no branch and no index on a byte. Every function is noexcept, and every +// decode counts its calls in decode_calls, so that a test sees whether the core called the +// value codec. The example record's forms of version 1 and 2, and 17 bytes one past the bound, are +// spelled once here. #ifndef WEBCPP_CODECS_TEST_SUPPORT_BOUNDED_RECORDS_HPP #define WEBCPP_CODECS_TEST_SUPPORT_BOUNDED_RECORDS_HPP @@ -103,6 +106,22 @@ struct short_text { } }; +/** The most bytes of a secret blob. */ +inline constexpr std::size_t secret_blob_bound = 16; + +/** Up to 16 secret bytes, for the constant-time lane: the bytes secret, their number public. */ +struct secret_blob { + /** The bytes, those past `size` zero. */ + secure::secret held_bytes; + /** The number of bytes, public. */ + std::size_t size = 0; + + /** The bytes. */ + [[nodiscard]] std::span view() const noexcept { + return held_bytes.bytes().first(size); + } +}; + /** The calls of the decode of `Value`'s value codec since the test last set it to 0. */ template // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the tests' counter @@ -389,6 +408,55 @@ struct value_codec { } }; +/** + A secret blob: up to 16 bytes, both bounds 16, secret; no branch and no index on a byte, so that + the constant-time lane traces its decode. +*/ +template <> +struct value_codec { + /** Bytes. */ + static constexpr output_kind output = output_kind::bytes; + /** The value is secret. */ + static constexpr bool secret = true; + /** The most bytes. */ + static constexpr std::size_t max_encoded_size = test::secret_blob_bound; + /** The most bytes. */ + static constexpr std::size_t max_decode_input_size = test::secret_blob_bound; + + /** The number of bytes. */ + static std::size_t encoded_size(test::bounded_t const& /*format*/, + test::secret_blob const& held_value) noexcept { + return held_value.size; + } + + /** Writes the bytes into `output_bytes`, which must hold them. */ + static result encode(test::bounded_t const& /*format*/, + test::secret_blob const& held_value, + std::span output_bytes) noexcept { + if (output_bytes.size() < held_value.size) { + return error::output_too_small; + } + std::ranges::copy(held_value.view(), output_bytes.begin()); + return held_value.size; + } + + /** + Copies up to 16 bytes: longer is invalid_length, a check of the length alone, which is public; + counts the call. + */ + static result decode(test::bounded_t const& /*format*/, + std::span input_bytes) noexcept { + ++test::decode_calls; + if (input_bytes.size() > test::secret_blob_bound) { + return error::invalid_length; + } + test::secret_blob decoded{}; + std::ranges::copy(input_bytes, decoded.held_bytes.writable().begin()); + decoded.size = input_bytes.size(); + return decoded; + } +}; + } // namespace webcpp::codecs #endif // WEBCPP_CODECS_TEST_SUPPORT_BOUNDED_RECORDS_HPP From bbdd0364d9db0d324ad3b221f0456ca05b345193 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 17:20:43 -0300 Subject: [PATCH 08/30] codecs: the constant-time path of a value through over lint fix --- test/constant_time/paths.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/constant_time/paths.hpp b/test/constant_time/paths.hpp index 0fbd520..ae5b8c7 100644 --- a/test/constant_time/paths.hpp +++ b/test/constant_time/paths.hpp @@ -1497,7 +1497,7 @@ inline constexpr std::string_view over_value_label = "TEST SECRET"; */ inline std::string over_value_text(std::size_t size, bool crlf) { std::array const plain = hex_plain_bytes(); - std::string const text = reference_armor_encode(over_value_label, std::span{plain}.first(size)); + std::string text = reference_armor_encode(over_value_label, std::span{plain}.first(size)); if (!crlf) { return text; } From 99a832191b55e4431bb1f3b323225c49be4359eb Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 17:46:37 -0300 Subject: [PATCH 09/30] codecs: the over_value fuzz target's expected error set where it is known --- test/fuzz/over_value_fuzz.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/fuzz/over_value_fuzz.cpp b/test/fuzz/over_value_fuzz.cpp index 98d0559..96d868d 100644 --- a/test/fuzz/over_value_fuzz.cpp +++ b/test/fuzz/over_value_fuzz.cpp @@ -143,14 +143,13 @@ template void check_text(Outer const& outer, Chain const& chain, std::string_view text) { // The reference, outside the counted region. auto const outer_bytes = codecs::decode>(outer, text); - codecs::result expected = codecs::error::invalid_length; + codecs::result const expected = + outer_bytes.has_value() ? modeled_record(std::span{outer_bytes.value()}) + : codecs::result{outer_bytes.error()}; if (outer_bytes.has_value()) { std::span const bytes{outer_bytes.value()}; - expected = modeled_record(bytes); expect(agrees(codecs::decode(test::bounded, bytes), expected), "the direct decode differs from the model"); - } else { - expected = outer_bytes.error(); } // The chain's decodes, with no allocation. std::size_t const before = test::fuzz_allocations(); From 0bb63588f3b19aee43362f97d1ba658efc5da48a Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 18:01:32 -0300 Subject: [PATCH 10/30] codecs: a library declares a value type's formats and its pairs' reasons, and every value operation refuses a pair through the declaration, one error --- .../webcpp/codecs/detail/core/contract.hpp | 279 ++++++++++++++++- .../webcpp/codecs/detail/core/operations.hpp | 119 ++++++-- include/webcpp/codecs/detail/core/over.hpp | 14 + include/webcpp/codecs/detail/core/sizes.hpp | 30 +- test/Jamfile | 8 + test/declared_reason_encode_builds_test.cpp | 72 +++++ test/messages/Jamfile | 11 + .../declared_check_conversion/Jamfile | 16 + .../declared_check_conversion.cpp | 24 ++ .../declared_composition_inner/Jamfile | 16 + .../declared_composition_inner.cpp | 25 ++ .../declared_reason_composition/Jamfile | 16 + .../declared_reason_composition.cpp | 27 ++ test/messages/declared_reason_decode/Jamfile | 16 + .../declared_reason_decode.cpp | 31 ++ test/messages/declared_unlisted/Jamfile | 16 + .../declared_unlisted/declared_unlisted.cpp | 24 ++ .../messages/declared_unlisted_buffer/Jamfile | 16 + .../declared_unlisted_buffer.cpp | 30 ++ .../messages/declared_unlisted_decode/Jamfile | 16 + .../declared_unlisted_decode.cpp | 29 ++ test/messages/declared_unlisted_size/Jamfile | 16 + .../declared_unlisted_size.cpp | 23 ++ test/messages/declared_unlisted_token/Jamfile | 16 + .../declared_unlisted_token.cpp | 24 ++ .../declared_without_value_codec/Jamfile | 16 + .../declared_without_value_codec.cpp | 49 +++ test/messages/refused_input/refused_input.cpp | 6 +- .../typed_decode_text_from_bytes/Jamfile | 16 + .../typed_decode_text_from_bytes.cpp | 56 ++++ test/support/declared_values.hpp | 244 +++++++++++++++ test/value_formats_test.cpp | 286 ++++++++++++++++++ 32 files changed, 1546 insertions(+), 41 deletions(-) create mode 100644 test/declared_reason_encode_builds_test.cpp create mode 100644 test/messages/declared_check_conversion/Jamfile create mode 100644 test/messages/declared_check_conversion/declared_check_conversion.cpp create mode 100644 test/messages/declared_composition_inner/Jamfile create mode 100644 test/messages/declared_composition_inner/declared_composition_inner.cpp create mode 100644 test/messages/declared_reason_composition/Jamfile create mode 100644 test/messages/declared_reason_composition/declared_reason_composition.cpp create mode 100644 test/messages/declared_reason_decode/Jamfile create mode 100644 test/messages/declared_reason_decode/declared_reason_decode.cpp create mode 100644 test/messages/declared_unlisted/Jamfile create mode 100644 test/messages/declared_unlisted/declared_unlisted.cpp create mode 100644 test/messages/declared_unlisted_buffer/Jamfile create mode 100644 test/messages/declared_unlisted_buffer/declared_unlisted_buffer.cpp create mode 100644 test/messages/declared_unlisted_decode/Jamfile create mode 100644 test/messages/declared_unlisted_decode/declared_unlisted_decode.cpp create mode 100644 test/messages/declared_unlisted_size/Jamfile create mode 100644 test/messages/declared_unlisted_size/declared_unlisted_size.cpp create mode 100644 test/messages/declared_unlisted_token/Jamfile create mode 100644 test/messages/declared_unlisted_token/declared_unlisted_token.cpp create mode 100644 test/messages/declared_without_value_codec/Jamfile create mode 100644 test/messages/declared_without_value_codec/declared_without_value_codec.cpp create mode 100644 test/messages/typed_decode_text_from_bytes/Jamfile create mode 100644 test/messages/typed_decode_text_from_bytes/typed_decode_text_from_bytes.cpp create mode 100644 test/support/declared_values.hpp create mode 100644 test/value_formats_test.cpp diff --git a/include/webcpp/codecs/detail/core/contract.hpp b/include/webcpp/codecs/detail/core/contract.hpp index 067ca11..756db91 100644 --- a/include/webcpp/codecs/detail/core/contract.hpp +++ b/include/webcpp/codecs/detail/core/contract.hpp @@ -175,6 +175,77 @@ struct codec; template struct value_codec; +/** + A list of formats, by type: what a @ref value_formats declaration states, and what a refusal of a + format the type does not have prints, in its failed requirement's template arguments. + + @tparam Formats The formats' types. +*/ +template +struct format_list {}; + +/** The direction of a conversion, for a reason of a @ref value_formats that holds in one alone. */ +enum class direction : unsigned char { + /** From the value to its encoding. */ + encode, + /** From the encoding to the value. */ + decode, +}; + +/** + The formats a value type has, declared by the library that owns the type: declared by the core, + never defined, as @ref value_codec is, and specialized for a type, fully or by a partial + specialization constrained by a concept (a library's family of types at once). + + A value codec is one pair; the declaration is the type's whole list, so that a refusal says what + the type has, and the place of a library's reasons, so that a refusal says why in the library's + words: + + @code + template <> struct value_formats { + // Required: the formats the type has, printed by a refusal. + using formats = codecs::format_list; + + // Optional: whether the type has Format, for a list that names a family; + // by default, Format is one of formats. + template + static constexpr bool has_format = + std::same_as || std::same_as; + + // Optional, together: whether the pair has a reason of its own in Direction, + // and the library's sentence, a static_assert false for those pairs alone. + template + static constexpr bool has_reason = + std::same_as && Direction == codecs::direction::decode; + template + static constexpr void refuse() noexcept { + static_assert(!has_reason, "toy: a point decodes from its spki form"); + } + }; + @endcode + + For a declared `Value`, the pair of `Format` and `Value` converts in a direction exactly when it + has no reason of its own in that direction (`has_reason` false or absent); the type has the + format (`has_format`, by default `Format` in `formats`), or `Format` is + `over` and the pair of `Inner` and `Value` converts in that direction + (a declared type composes under any outer codec, unless a reason refuses the composition); and a + value codec of the pair converts in that direction. @ref encodable and @ref decodable are true + exactly then. Every operation that converts a value (`encode` in every form, `encoded_size`, the + typed `decode`) and @ref check_conversion refuse any other pair with one error, the first + that applies: the declaration's `refuse()`; for a format the type does not + have, codecs' assertion whose requirement, + `webcpp::codecs::detail::format_listed`, prints the declaration's `formats` + as `List`; for a format the type has and no value codec of the pair converts, codecs' assertion + that the type lists the format and no value codec of the pair encodes it (or decodes it). A + composition the type does not have is refused as its inner link is, after any reason given for + the composition itself. A type with no declaration converts and is refused as before: the + declaration governs a value codec's pairs only, never the bytes a `codec` encodes or decodes. + + @tparam Value The value's type. +*/ +template +struct value_formats; + /** The fixed text result: `N` characters in place and a size (defined in results.hpp). */ template class text; @@ -615,11 +686,99 @@ concept value_decodable = requires { } -> std::same_as>>; }; +/** Whether `value_formats` is specialized for `V` (its cv and reference removed): declared. */ +template +inline constexpr bool declared_v = requires { sizeof(value_formats>); }; + +/** + The requirement of the refusal of a format a declared type does not have: false for every pair, + so that its static assertion prints the format's type, the value's type and `List`, the + declaration's @ref format_list. +*/ +template +inline constexpr bool format_listed = false; + +/** Whether `Format` is one of the formats of `List`, a @ref format_list. */ +template +inline constexpr bool in_format_list = false; + +/** One of the formats of a @ref format_list. */ +template +inline constexpr bool in_format_list> = + (std::same_as || ...); + +/** + The inner link of a composition, `over` (its `type`, specialized in + over.hpp), and nothing for another format. +*/ +template +struct composition_inner {}; + +/** Whether `Format` is a composition, `over`. */ +template +concept composition = requires { typename composition_inner::type; }; + +/** Whether a value codec of `Format` and `Value` converts in `Direction`, as the genesis says. */ +template +inline constexpr bool value_converts = + Direction == direction::encode ? value_encodable + : value_decodable; + +/** + Whether `Value`'s declaration has `Format`: its `has_format` when it has one, else + `Format` in its `formats`. +*/ +template +[[nodiscard]] consteval bool declared_format() noexcept { + using declaration = value_formats; + if constexpr (requires { declaration::template has_format; }) { + return static_cast(declaration::template has_format); + } else { + return in_format_list; + } +} + +/** Whether `Value`'s declaration gives the pair of `Format` a reason of its own in `Direction`. */ +template +[[nodiscard]] consteval bool declared_reason() noexcept { + using declaration = value_formats; + if constexpr (requires { declaration::template has_reason; }) { + return static_cast(declaration::template has_reason); + } else { + return false; + } +} + +/** + Whether the pair of `Format` and `Value` converts in `Direction` (design D9): for a declared + `Value`, no reason of its own, the type has the format or the format is a composition whose inner + link's pair converts, and a value codec of the pair converts; for a type with no declaration, a + value codec of the pair converts (the genesis' rule). +*/ +template +[[nodiscard]] consteval bool pair_converts() noexcept { + using value_type = std::remove_cvref_t; + if constexpr (!declared_v) { + return value_converts; + } else if constexpr (declared_reason()) { + return false; + } else if constexpr (declared_format()) { + return value_converts; + } else if constexpr (composition) { + return pair_converts::type, value_type>() && + value_converts; + } else { + return false; + } +} + } // namespace detail /** Whether a value of type `V` encodes in the format `F`: `V` is a byte input and `codec` is - specialized, or `value_codec` is specialized with `encode` and `encoded_size`. + specialized, or `value_codec` is specialized with `encode` and `encoded_size` and, when + `V` declares its formats (@ref value_formats), the pair converts by that declaration, so that the + concept is true exactly where `encode` builds. A byte input is a contiguous sized range of `std::uint8_t`, `unsigned char`, `char` or `std::byte`, a type with a static constexpr `size()` that converts to @@ -631,20 +790,26 @@ concept value_decodable = requires { */ template concept encodable = - format && ((detail::byte_input && detail::has_codec) || detail::value_encodable); + format && + ((detail::byte_input && detail::has_codec) || + (detail::value_encodable && detail::pair_converts())); /** Whether a value of type `V` decodes from the format `F`: `V` is a byte result the core builds (`std::string`, `std::vector`, `std::array`, `bytes`, or a library's fixed type with a `fixed_byte_storage`) and `codec` is specialized, or - `value_codec` is specialized with `decode`. + `value_codec` is specialized with `decode` and, when `V` declares its formats + (@ref value_formats), the pair converts by that declaration, so that the concept is true exactly + where the typed `decode` builds. @tparam V The value's type. @tparam F The format's type. */ template -concept decodable = format && ((detail::byte_result && detail::has_codec) || - detail::value_decodable); +concept decodable = + format && + ((detail::byte_result && detail::has_codec) || + (detail::value_decodable && detail::pair_converts())); namespace detail { @@ -673,6 +838,110 @@ constexpr void refuse() noexcept { "header, or specialize webcpp::codecs::value_codec for the format and the type"); } +/** + Stops the build: the pair of `Format` and `Value` does not convert in `Direction`, with one error, + the first of design D10 that applies. For a declared `Value`: the declaration's + `refuse()`, the library's sentence; else, for a format the type does not have, + the assertion whose requirement, `format_listed`, prints the declaration's list; else, for a + format it has, the assertion that no value codec of the pair converts in `Direction`; for a + composition it does not have, its inner link's refusal. For a type with no declaration, the + genesis' @ref refuse. Called only for a pair that does not convert. +*/ +template +constexpr void refuse_conversion() noexcept { + using value_type = std::remove_cvref_t; + if constexpr (!declared_v) { + refuse(); + } else if constexpr (declared_reason()) { + value_formats::template refuse(); + } else if constexpr (declared_format()) { + // Qualified, so that the compiler's message names the requirement as the core's. + if constexpr (Direction == direction::encode) { + static_assert(webcpp::codecs::detail::converts, + "webcpp::codecs: this type lists the format, and no value_codec of the " + "pair encodes it"); + } else { + static_assert(webcpp::codecs::detail::converts, + "webcpp::codecs: this type lists the format, and no value_codec of the " + "pair decodes it"); + } + } else if constexpr (composition) { + using inner_type = typename composition_inner::type; + if constexpr (!pair_converts()) { + refuse_conversion(); + } else { + // The inner link converts and the chain has no value codec (an outer link with no + // codec): the genesis' refusal of the chain. + refuse(); + } + } else { + // Qualified, so that the compiler's message names the requirement as the core's. + static_assert( + webcpp::codecs::detail::format_listed::formats>, + "webcpp::codecs: this type has no such format; the formats it has are the " + "format_list named in this assertion"); + } +} + +/** + Whether a declared `Value` refuses the pair of `Format` in `Direction`: the operations of codecs + take such a pair (a buffer form among them, which would otherwise drop it from overload + resolution) and refuse it in their body with @ref refuse_conversion, their declaration and + exception specification well-formed, so that the refusal is the build's only error. +*/ +template +concept declaration_refuses = + declared_v && + !(Direction == direction::encode ? encodable : decodable); + +/** + Stops the build: a typed decode gave a value codec whose `output` is `Output` an input it does + not read, a byte input to a value codec of text or a text to one of bytes (ruling R25); the + requirement names the input's type and the format's type. +*/ +template +constexpr void refuse_input() noexcept { + // Qualified, so that the compiler's message names the requirement as the core's. + if constexpr (Output == output_kind::bytes) { + static_assert(webcpp::codecs::detail::converts, + "webcpp::codecs: this value codec decodes from bytes: a " + "std::span, or a byte input"); + } else { + static_assert(webcpp::codecs::detail::converts, + "webcpp::codecs: this value codec decodes from text: a std::string_view, or " + "a contiguous sized range of char"); + } +} + +} // namespace detail + +/** + Checks, for a library's own entry point that converts a value and is not one of codecs' verbs (a + decode that takes a memory resource), that the pair of `Format` and `Value` converts in + `Direction`, with the refusal codecs' verbs have. + + It does nothing for a pair that converts in `Direction` (@ref encodable or @ref decodable), and + stops the build for any other with one error, as `encode` and `decode` do: for a type that + declares its formats (@ref value_formats), the declaration's reason, else the list's assertion, + else the assertion that no value codec of the pair converts; for a type with no declaration, the + assertion that the format does not convert this type. + + @tparam Direction The direction of the conversion. + @tparam Value The value's type. + @tparam Format The format's type. +*/ +template +constexpr void check_conversion() noexcept { + constexpr bool converting = + Direction == direction::encode ? encodable : decodable; + if constexpr (!converting) { + detail::refuse_conversion(); + } +} + +namespace detail { + /** The requirement of a token's refusal: false for every pair, so that its static assertion, which names it, prints the token's type and the format's type. diff --git a/include/webcpp/codecs/detail/core/operations.hpp b/include/webcpp/codecs/detail/core/operations.hpp index 33bafac..9abb957 100644 --- a/include/webcpp/codecs/detail/core/operations.hpp +++ b/include/webcpp/codecs/detail/core/operations.hpp @@ -336,7 +336,10 @@ inline constexpr bool decode_is_fixed = template constexpr bool typed_decode_is_noexcept() { constexpr bool secret = secret_operation_v; - if constexpr (value_decodable) { + if constexpr (declaration_refuses) { + // Refused in the body, its only error (design D10). + return true; + } else if constexpr (value_decodable) { using value_type = value_codec; if constexpr (value_type::output == output_kind::bytes && byte_input) { return value_decode_is_noexcept>(); @@ -718,7 +721,9 @@ constexpr bool decode_token_is_noexcept() { label), whatever the input's size, before any result is made; an input whose encoding does not fit in `std::size_t` is @ref error::input_too_long, before it is read. A value codec of `F` and `Input` is used when it encodes, else `codec`. A pair that is not - @ref encodable stops the build with a message that the format does not convert this type. + @ref encodable stops the build with a message that the format does not convert this type; a pair + of a type that declares its formats (@ref value_formats) is taken by the overload that refuses it + by its declaration. @tparam F The format's type. @tparam Input The input's type: a byte input, or a value of a value codec. @@ -727,12 +732,13 @@ constexpr bool decode_token_is_noexcept() { @return The encoding, or the error. */ template + requires(!detail::declaration_refuses) [[nodiscard]] auto encode(F const& format_value, Input const& input_value) noexcept( detail::is_fixed_result> && detail::encode_calls_are_noexcept>()) { using result_type = detail::default_encode_result; if constexpr (!encodable) { - detail::refuse(); + detail::refuse_conversion(); return result{std::string{}}; } else if constexpr (detail::secret_operation_v) { secure::timing_guard const timing_scope{true}; @@ -742,6 +748,26 @@ template } } +/** + Refuses the encode of `Input`, a type that declares its formats, in a format whose pair does not + convert by that declaration (@ref value_formats). + + The build stops with design D10's one error, the declaration's reason, else the list's + assertion, else the assertion that no value codec of the pair encodes it. Its declaration and + exception specification are well-formed, so that the refusal is the build's only error. + + @tparam F The format's type. + @tparam Input The value's type. + @return Never used: the build stops. +*/ +template + requires detail::declaration_refuses +[[nodiscard]] result encode(F const& /*format_value*/, + Input const& /*input_value*/) noexcept { + detail::refuse_conversion(); + return result{std::string{}}; +} + /** Encodes `input_value`, a value of a value codec of bytes, in `format_value` into the caller's buffer, allocating nothing. @@ -752,7 +778,9 @@ template `output_buffer`, whose other bytes are unchanged. The errors the value codec returns reach the caller unchanged. A secret value (`is_secret_v`, or the value codec's `secret`) holds secure's timing guard while the value codec runs, which runs its overloads that take `detail::secret_tag` - first where it has them (R7). + first where it has them (R7). A type that declares its formats (@ref value_formats) is taken + whatever its pair, and a pair that does not convert by its declaration stops the build with + design D10's one error, never with no matching function. @tparam F The format's type. @tparam Input The value's type, one `value_codec` encodes into bytes. @@ -762,15 +790,16 @@ template @return The number of bytes written, or the error. */ template - requires(detail::value_encodable && - value_codec::output == output_kind::bytes) + requires((detail::value_encodable && + value_codec::output == output_kind::bytes) || + detail::declaration_refuses) [[nodiscard]] result encode( F const& format_value, Input const& input_value, - std::span - output_buffer) noexcept(detail::encode_calls_are_noexcept>()) { - if constexpr (detail::secret_operation_v) { + std::span output_buffer) noexcept(detail::encode_form_is_noexcept()) { + if constexpr (detail::declaration_refuses) { + detail::refuse_conversion(); + return std::size_t{0}; + } else if constexpr (detail::secret_operation_v) { secure::timing_guard const timing_scope{true}; return detail::encode_value_into_buffer(format_value, input_value, output_buffer); } else { @@ -788,7 +817,9 @@ template `output_buffer`, whose other characters are unchanged. The errors the value codec returns reach the caller unchanged. A secret value (`is_secret_v`, or the value codec's `secret`) holds secure's timing guard while the value codec runs, which runs its overloads that take - `detail::secret_tag` first where it has them (R7). + `detail::secret_tag` first where it has them (R7). A type that declares its formats + (@ref value_formats) is taken whatever its pair, and a pair that does not convert by its + declaration stops the build with design D10's one error, never with no matching function. @tparam F The format's type. @tparam Input The value's type, one `value_codec` encodes into text. @@ -798,12 +829,16 @@ template @return The number of characters written, or the error. */ template - requires(detail::value_encodable && - value_codec::output == output_kind::text) -[[nodiscard]] result -encode(F const& format_value, Input const& input_value, std::span output_buffer) noexcept( - detail::encode_calls_are_noexcept>()) { - if constexpr (detail::secret_operation_v) { + requires((detail::value_encodable && + value_codec::output == output_kind::text) || + detail::declaration_refuses) +[[nodiscard]] result encode( + F const& format_value, Input const& input_value, + std::span output_buffer) noexcept(detail::encode_form_is_noexcept()) { + if constexpr (detail::declaration_refuses) { + detail::refuse_conversion(); + return std::size_t{0}; + } else if constexpr (detail::secret_operation_v) { secure::timing_guard const timing_scope{true}; return detail::encode_value_into_buffer(format_value, input_value, output_buffer); } else { @@ -875,7 +910,8 @@ template the tail it cuts, its own copy, `assign_to`'s scratch). A pair that is not @ref encodable stops the build with a message that the format does not convert this type, and a token that does not take the pair's encoding (`append_to` of a vector, which is for decode) with one that names the - token's type. + token's type. A pair of a type that declares its formats (@ref value_formats) and does not + convert by that declaration is taken by the overload that refuses it by its declaration. @tparam F The format's type. @tparam Input The input's type: a byte input, or a value of a value codec. @@ -886,14 +922,15 @@ template @return The token's result, or the error. */ template - requires detail::token_argument + requires(detail::token_argument && + !detail::declaration_refuses) [[nodiscard]] auto encode(F const& format_value, Input const& input_value, Token&& token) noexcept( detail::encode_token_is_noexcept()) { using tag_type = std::remove_cvref_t; if constexpr (std::same_as) { return encode(format_value, input_value); } else if constexpr (!encodable) { - detail::refuse(); + detail::refuse_conversion(); return result{std::string{}}; } else if constexpr (detail::is_secret_tag) { using result_type = detail::secret_encode_result; @@ -921,6 +958,25 @@ template } } +/** + Refuses the encode into a token of `Input`, a type that declares its formats (@ref value_formats), + in a format whose pair does not convert by that declaration, as the form without a token does: + design D10's one error, whatever the token. + + @tparam F The format's type. + @tparam Input The value's type. + @tparam Token A tag of `codecs::tokens`, or a token. + @return Never used: the build stops. +*/ +template + requires(detail::token_argument && + detail::declaration_refuses) +[[nodiscard]] result encode(F const& /*format_value*/, Input const& /*input_value*/, + Token&& /*token*/) noexcept { + detail::refuse_conversion(); + return result{std::string{}}; +} + /** Decodes `text_value` in `format_value` into `token`, which says where the bytes go and what they are. @@ -1005,9 +1061,13 @@ template `Value`'s `is_secret_v` or the value codec's `secret` says so: it then holds secure's timing guard, runs the format's secret arithmetic where it has one, and wipes the temporary it decodes into before it returns, by `secure::erase_secret`. A pair `F` does not convert stops the build with - a message that the format does not convert this type, and an input the pair does not read (a byte - input given to a decode of text, a value codec of bytes given a text) with one that names the - input's type. + a message that the format does not convert this type; for a `Value` that declares its formats + (@ref value_formats), with design D10's one error, the declaration's reason, else the list's + assertion, else the assertion that no value codec of the pair decodes it. An input the pair does + not read stops it with one that names the input's type: a value codec of bytes given what is no + byte input says that it decodes from bytes, one of text given what is no text that it decodes + from text (ruling R25), and a decode into bytes given what is no text that the format does not + convert it. @tparam Value The type to decode into. @tparam F The format's type. @@ -1020,7 +1080,10 @@ template [[nodiscard]] result decode(F const& format_value, Input const& input_value) noexcept( detail::typed_decode_is_noexcept()) { constexpr bool secret = detail::secret_operation_v; - if constexpr (detail::value_decodable) { + if constexpr (detail::declaration_refuses) { + detail::refuse_conversion(); + return make_error_code(error::invalid_length); + } else if constexpr (detail::value_decodable) { using value_type = value_codec; if (auto const configuration_error = detail::configuration_fault_of(format_value)) { return make_error_code(*configuration_error); @@ -1044,8 +1107,8 @@ template detail::bounded_text_view(input_value)); } } else { - // The pair converts; this input is not what the value codec reads. - detail::refuse(); + // The pair converts; this input is not what the value codec reads (ruling R25). + detail::refuse_input(); return make_error_code(error::invalid_length); } } else if constexpr (detail::byte_result && detail::has_codec) { @@ -1060,7 +1123,7 @@ template return detail::decode_as(format_value, input_value); } } else { - detail::refuse(); + detail::refuse_conversion(); return make_error_code(error::invalid_length); } } diff --git a/include/webcpp/codecs/detail/core/over.hpp b/include/webcpp/codecs/detail/core/over.hpp index 4aea17b..d31b4b0 100644 --- a/include/webcpp/codecs/detail/core/over.hpp +++ b/include/webcpp/codecs/detail/core/over.hpp @@ -250,6 +250,20 @@ over(Outer, Inner, Allocator) -> over; namespace detail { +/** + A chain's inner link, through which a value type that declares its formats composes (design D9): + the pair of a chain the type does not list converts when its inner link's does. + + @tparam Outer The outer link's type. + @tparam Inner The inner link's type. + @tparam Allocator The allocator's type. +*/ +template +struct composition_inner> { + /** The inner link's type. */ + using type = Inner; +}; + /** The fault of a chain's configuration: the outer link's, else the inner link's, a link that is a chain asked in turn. It is the core's probe of every chain, of a value codec's link too, which diff --git a/include/webcpp/codecs/detail/core/sizes.hpp b/include/webcpp/codecs/detail/core/sizes.hpp index 2e52e10..19e738e 100644 --- a/include/webcpp/codecs/detail/core/sizes.hpp +++ b/include/webcpp/codecs/detail/core/sizes.hpp @@ -101,6 +101,24 @@ template } } +/** + Whether a form of encode of `Input` in `F` whose result is fixed by its declaration (a buffer, a + size) cannot throw: a pair refused by the declaration of `Input`, whose refusal is the form's one + error, is `noexcept` (design D10), and another as @ref encode_calls_are_noexcept says with the + operation's secrecy. + + @tparam F The format's type. + @tparam Input The input's type. +*/ +template +[[nodiscard]] constexpr bool encode_form_is_noexcept() { + if constexpr (declaration_refuses) { + return true; + } else { + return encode_calls_are_noexcept>(); + } +} + /** Whether the calls of a codec's functions that a decode of `Text` in `F` makes cannot throw, as for @ref encode_calls_are_noexcept. @@ -231,7 +249,9 @@ template the armor's missing label); the format's other functions are then not called (B23). A pair that is not @ref encodable stops the build with a message that the format does not - convert this type. + convert this type; for a type that declares its formats (@ref value_formats), with design D10's + one error, the declaration's reason, else the list's assertion, else the assertion that no value + codec of the pair encodes it. @tparam F The format's type. @tparam Input The input's type: a byte input, or a value of a value codec. @@ -241,11 +261,11 @@ template the configuration or of the encode. */ template -[[nodiscard]] constexpr std::size_t -encoded_size(F const& format_value, Input const& input_value) noexcept( - detail::encode_calls_are_noexcept>()) { +[[nodiscard]] constexpr std::size_t encoded_size( + F const& format_value, + Input const& input_value) noexcept(detail::encode_form_is_noexcept()) { if constexpr (!encodable) { - detail::refuse(); + detail::refuse_conversion(); return 0; } else { if (detail::configuration_fault_of(format_value)) { diff --git a/test/Jamfile b/test/Jamfile index 7185c5b..a1782ab 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -224,6 +224,14 @@ codecs-run typed_decode : typed_decode_test.cpp ; # Value codecs: the concepts, the default result, the buffer forms, the decode # bound the core reads and the order of the two bounds. codecs-run value_codec : value_codec_test.cpp ; +# A value type's formats, declared (value_formats, design D9 and D10): the +# concepts follow the declaration, every refused pair's form is well-formed and +# noexcept, a listed pair and a composition convert as the value codec does. +codecs-run value_formats : value_formats_test.cpp ; +# A reason in the decode direction alone (WEBCPP_CODECS_TEST_DECODE_REASON, the +# message case declared_reason_decode): the encode of the same pair builds. +codecs-run declared_reason_encode_builds : declared_reason_encode_builds_test.cpp + : WEBCPP_CODECS_TEST_DECODE_REASON ; # The tokens: codecs' own tags in both directions, a token of the test's own, # capacities, a value codec through a token. codecs-run tokens : tokens_test.cpp ; diff --git a/test/declared_reason_encode_builds_test.cpp b/test/declared_reason_encode_builds_test.cpp new file mode 100644 index 0000000..593f3ab --- /dev/null +++ b/test/declared_reason_encode_builds_test.cpp @@ -0,0 +1,72 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A reason in one direction alone (design D9): with WEBCPP_CODECS_TEST_DECODE_REASON, which its +// Jamfile line defines, the toy declaration refuses a toy_point's decode from toy_raw (the message +// case declared_reason_decode), and the encode of the same pair, which the declaration lists, +// builds and writes the value codec's bytes, in every form. + +#include "support/declared_values.hpp" + +#include +#include + +#include + +#include +#include +#include +#include + +#ifndef WEBCPP_CODECS_TEST_DECODE_REASON +#error "this test is built with WEBCPP_CODECS_TEST_DECODE_REASON" +#endif + +namespace { + +namespace codecs = webcpp::codecs; +namespace test = webcpp::codecs::test; + +using codecs::direction; + +static_assert(test::decode_reason_asked); +static_assert(codecs::encodable); +static_assert(!codecs::decodable); +// The reason is the point's: the signature still decodes from toy_raw, and the point from spki. +static_assert(codecs::decodable); +static_assert(codecs::decodable); +// A composition over the refused pair is refused in the same direction alone. +static_assert( + codecs::encodable>); +static_assert( + !codecs::decodable>); + +/** The encode of the pair the reason refuses in decode builds and writes the point's bytes. */ +void the_encode_of_the_pair_builds() { + test::toy_point const point = test::toy_point_example; + constexpr std::array expected{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + + auto const encoded = codecs::encode(test::toy_raw, point); + BOOST_TEST(encoded.has_value() && std::ranges::equal(encoded.value().view(), expected)); + BOOST_TEST_EQ(codecs::encoded_size(test::toy_raw, point), expected.size()); + + std::array buffer{}; + auto const written = codecs::encode(test::toy_raw, point, std::span{buffer}); + BOOST_TEST(written.has_value() && written.value() == expected.size()); + BOOST_TEST(std::ranges::equal(buffer, expected)); + + auto const fixed = codecs::encode(test::toy_raw, point, codecs::tokens::fixed<8>{}); + BOOST_TEST(fixed.has_value() && std::ranges::equal(fixed.value().view(), expected)); + + codecs::check_conversion(); +} + +} // namespace + +int main() { + the_encode_of_the_pair_builds(); + return boost::report_errors(); +} diff --git a/test/messages/Jamfile b/test/messages/Jamfile index af50d4f..cfa99df 100644 --- a/test/messages/Jamfile +++ b/test/messages/Jamfile @@ -51,3 +51,14 @@ build-project drain_of_base58 ; build-project base58_alphabet_not_distinct ; build-project base58_with_checksum_unseen ; build-project pipe_char_to_packet ; +build-project declared_unlisted ; +build-project declared_unlisted_buffer ; +build-project declared_unlisted_token ; +build-project declared_unlisted_size ; +build-project declared_unlisted_decode ; +build-project declared_reason_decode ; +build-project declared_reason_composition ; +build-project declared_without_value_codec ; +build-project declared_composition_inner ; +build-project declared_check_conversion ; +build-project typed_decode_text_from_bytes ; diff --git a/test/messages/declared_check_conversion/Jamfile b/test/messages/declared_check_conversion/Jamfile new file mode 100644 index 0000000..57dd335 --- /dev/null +++ b/test/messages/declared_check_conversion/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_check_conversion : declared_check_conversion.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_check_conversion_refused : declared_check_conversion.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_check_conversion/declared_check_conversion.cpp b/test/messages/declared_check_conversion/declared_check_conversion.cpp new file mode 100644 index 0000000..a6ff6b3 --- /dev/null +++ b/test/messages/declared_check_conversion/declared_check_conversion.cpp @@ -0,0 +1,24 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A library's own entry point checks its pair with check_conversion: a format the declaration does +// not list stops in the list's assertion alone, as codecs' verbs do. +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::format_listed>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include + +int main() { + namespace codecs = webcpp::codecs; + codecs::check_conversion(); + return 0; +} diff --git a/test/messages/declared_composition_inner/Jamfile b/test/messages/declared_composition_inner/Jamfile new file mode 100644 index 0000000..3e38a8e --- /dev/null +++ b/test/messages/declared_composition_inner/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_composition_inner : declared_composition_inner.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_composition_inner_refused : declared_composition_inner.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_composition_inner/declared_composition_inner.cpp b/test/messages/declared_composition_inner/declared_composition_inner.cpp new file mode 100644 index 0000000..d0a6bf9 --- /dev/null +++ b/test/messages/declared_composition_inner/declared_composition_inner.cpp @@ -0,0 +1,25 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A composition the declaration does not list, over an inner format it does not list either: the +// refusal of the inner link, the list's assertion of toy_der and toy_point. +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::format_listed>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include +#include + +int main() { + namespace codecs = webcpp::codecs; + auto const encoded = codecs::encode(codecs::over{codecs::formats::hex, codecs::test::toy_der}, + codecs::test::toy_point_example); + return encoded.has_value() ? 0 : 1; +} diff --git a/test/messages/declared_reason_composition/Jamfile b/test/messages/declared_reason_composition/Jamfile new file mode 100644 index 0000000..a7e7f74 --- /dev/null +++ b/test/messages/declared_reason_composition/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_reason_composition : declared_reason_composition.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_reason_composition_refused : declared_reason_composition.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_reason_composition/declared_reason_composition.cpp b/test/messages/declared_reason_composition/declared_reason_composition.cpp new file mode 100644 index 0000000..82ea899 --- /dev/null +++ b/test/messages/declared_reason_composition/declared_reason_composition.cpp @@ -0,0 +1,27 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A composition the declaration refuses by a reason of its own, an armor_t over toy_spki whatever +// its label (the shape of crypto's PEM): the declaration's sentence alone, before the composition's +// inner link, which converts. +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@../../support/declared_values.hpp:* {{webcpp::codecs::test: a point's armor is the toy's own}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include +#include + +int main() { + namespace codecs = webcpp::codecs; + auto const encoded = + codecs::encode(codecs::over{codecs::formats::armor("CERTIFICATE"), codecs::test::toy_spki}, + codecs::test::toy_point_example); + return encoded.has_value() ? 0 : 1; +} diff --git a/test/messages/declared_reason_decode/Jamfile b/test/messages/declared_reason_decode/Jamfile new file mode 100644 index 0000000..7be30db --- /dev/null +++ b/test/messages/declared_reason_decode/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs WEBCPP_CODECS_TEST_DECODE_REASON ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_reason_decode : declared_reason_decode.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_reason_decode_refused : declared_reason_decode.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_reason_decode/declared_reason_decode.cpp b/test/messages/declared_reason_decode/declared_reason_decode.cpp new file mode 100644 index 0000000..f00a084 --- /dev/null +++ b/test/messages/declared_reason_decode/declared_reason_decode.cpp @@ -0,0 +1,31 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A reason of the declaration in the decode direction alone (its Jamfile defines +// WEBCPP_CODECS_TEST_DECODE_REASON): the decode of a toy_point from toy_raw, a listed pair, stops +// in the declaration's own assertion, in the test's header, with its sentence alone (design D10's +// first refusal). The encode of the same pair builds: the run test declared_reason_encode_builds. +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@../../support/declared_values.hpp:* {{webcpp::codecs::test: a point decodes from its spki form}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include + +#include +#include +#include + +int main() { + namespace codecs = webcpp::codecs; + std::array const bytes{1, 2, 3, 4, 5, 6, 7, 8}; + auto const decoded = codecs::decode( + codecs::test::toy_raw, std::span{bytes}); + return decoded.has_value() ? 0 : 1; +} diff --git a/test/messages/declared_unlisted/Jamfile b/test/messages/declared_unlisted/Jamfile new file mode 100644 index 0000000..5b379ec --- /dev/null +++ b/test/messages/declared_unlisted/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_unlisted : declared_unlisted.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_unlisted_refused : declared_unlisted.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_unlisted/declared_unlisted.cpp b/test/messages/declared_unlisted/declared_unlisted.cpp new file mode 100644 index 0000000..80fd36f --- /dev/null +++ b/test/messages/declared_unlisted/declared_unlisted.cpp @@ -0,0 +1,24 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A declared type encoded in a format its declaration does not list, a value codec of the pair +// existing, stops in codecs' list assertion, whose requirement prints the type's formats (design +// D10's second refusal): the build's only error. +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::format_listed>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include + +int main() { + namespace codecs = webcpp::codecs; + auto const encoded = codecs::encode(codecs::test::toy_der, codecs::test::toy_point_example); + return encoded.has_value() ? 0 : 1; +} diff --git a/test/messages/declared_unlisted_buffer/Jamfile b/test/messages/declared_unlisted_buffer/Jamfile new file mode 100644 index 0000000..a20af63 --- /dev/null +++ b/test/messages/declared_unlisted_buffer/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_unlisted_buffer : declared_unlisted_buffer.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_unlisted_buffer_refused : declared_unlisted_buffer.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_unlisted_buffer/declared_unlisted_buffer.cpp b/test/messages/declared_unlisted_buffer/declared_unlisted_buffer.cpp new file mode 100644 index 0000000..0b3a2be --- /dev/null +++ b/test/messages/declared_unlisted_buffer/declared_unlisted_buffer.cpp @@ -0,0 +1,30 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// The unlisted pair of declared_unlisted encoded into a std::span: the buffer form +// takes the declared type and refuses it with the list's assertion alone, never with no matching +// function. +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::format_listed>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include + +#include +#include +#include + +int main() { + namespace codecs = webcpp::codecs; + std::array buffer{}; + auto const written = codecs::encode(codecs::test::toy_der, codecs::test::toy_point_example, + std::span{buffer}); + return written.has_value() ? 0 : 1; +} diff --git a/test/messages/declared_unlisted_decode/Jamfile b/test/messages/declared_unlisted_decode/Jamfile new file mode 100644 index 0000000..2dd137d --- /dev/null +++ b/test/messages/declared_unlisted_decode/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_unlisted_decode : declared_unlisted_decode.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_unlisted_decode_refused : declared_unlisted_decode.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_unlisted_decode/declared_unlisted_decode.cpp b/test/messages/declared_unlisted_decode/declared_unlisted_decode.cpp new file mode 100644 index 0000000..e061235 --- /dev/null +++ b/test/messages/declared_unlisted_decode/declared_unlisted_decode.cpp @@ -0,0 +1,29 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A declared type decoded from a format its declaration does not list: the list's assertion alone, +// before the absence of a decode in the pair's value codec (design D10's order). +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::format_listed>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include + +#include +#include +#include + +int main() { + namespace codecs = webcpp::codecs; + std::array const bytes{0x04, 0x08, 1, 2, 3, 4, 5, 6, 7, 8}; + auto const decoded = codecs::decode( + codecs::test::toy_der, std::span{bytes}); + return decoded.has_value() ? 0 : 1; +} diff --git a/test/messages/declared_unlisted_size/Jamfile b/test/messages/declared_unlisted_size/Jamfile new file mode 100644 index 0000000..fca68de --- /dev/null +++ b/test/messages/declared_unlisted_size/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_unlisted_size : declared_unlisted_size.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_unlisted_size_refused : declared_unlisted_size.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_unlisted_size/declared_unlisted_size.cpp b/test/messages/declared_unlisted_size/declared_unlisted_size.cpp new file mode 100644 index 0000000..85d4fb8 --- /dev/null +++ b/test/messages/declared_unlisted_size/declared_unlisted_size.cpp @@ -0,0 +1,23 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// The size of the unlisted pair of declared_unlisted: the list's assertion alone. +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::format_listed>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include + +int main() { + namespace codecs = webcpp::codecs; + std::size_t const size = + codecs::encoded_size(codecs::test::toy_der, codecs::test::toy_point_example); + return size == 0 ? 1 : 0; +} diff --git a/test/messages/declared_unlisted_token/Jamfile b/test/messages/declared_unlisted_token/Jamfile new file mode 100644 index 0000000..458c230 --- /dev/null +++ b/test/messages/declared_unlisted_token/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_unlisted_token : declared_unlisted_token.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_unlisted_token_refused : declared_unlisted_token.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_unlisted_token/declared_unlisted_token.cpp b/test/messages/declared_unlisted_token/declared_unlisted_token.cpp new file mode 100644 index 0000000..6d7eaed --- /dev/null +++ b/test/messages/declared_unlisted_token/declared_unlisted_token.cpp @@ -0,0 +1,24 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// The unlisted pair of declared_unlisted encoded into a token of codecs': the list's assertion +// alone. +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::format_listed>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include + +int main() { + namespace codecs = webcpp::codecs; + auto const encoded = codecs::encode(codecs::test::toy_der, codecs::test::toy_point_example, + codecs::tokens::fixed<16>{}); + return encoded.has_value() ? 0 : 1; +} diff --git a/test/messages/declared_without_value_codec/Jamfile b/test/messages/declared_without_value_codec/Jamfile new file mode 100644 index 0000000..4b7b40d --- /dev/null +++ b/test/messages/declared_without_value_codec/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_without_value_codec : declared_without_value_codec.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_without_value_codec_refused : declared_without_value_codec.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_without_value_codec/declared_without_value_codec.cpp b/test/messages/declared_without_value_codec/declared_without_value_codec.cpp new file mode 100644 index 0000000..8de791f --- /dev/null +++ b/test/messages/declared_without_value_codec/declared_without_value_codec.cpp @@ -0,0 +1,49 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A declaration that lists a format no value codec of the pair decodes: a value type of this +// file's own, with toy_raw's value codecs, whose declaration lists toy_raw and toy_der, decoded +// from toy_der, stops in codecs' assertion that the type lists the format and no value codec of the +// pair decodes it (design D10's third refusal), alone. clang's -verify reads a directive from one +// comment line: wrapped, it checks nothing, so the whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::converts': webcpp::codecs: this type lists the format, and no value_codec of the pair decodes it}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include + +#include +#include +#include + +/** A value of 4 bytes with toy_raw's value codecs alone. */ +struct unlisted_codec_value { + /** The bytes. */ + std::array held_bytes{}; +}; + +/** toy_raw for the value. */ +template <> +struct webcpp::codecs::value_codec + : webcpp::codecs::test::toy_codec {}; + +/** A declaration that lists toy_der, of which the value has no value codec. */ +template <> +struct webcpp::codecs::value_formats { + /** toy_raw and toy_der. */ + using formats = webcpp::codecs::format_list; +}; + +int main() { + namespace codecs = webcpp::codecs; + std::array const bytes{0x04, 0x04, 1, 2, 3, 4}; + auto const decoded = codecs::decode(codecs::test::toy_der, + std::span{bytes}); + return decoded.has_value() ? 0 : 1; +} diff --git a/test/messages/refused_input/refused_input.cpp b/test/messages/refused_input/refused_input.cpp index 7bed671..1ed7a64 100644 --- a/test/messages/refused_input/refused_input.cpp +++ b/test/messages/refused_input/refused_input.cpp @@ -6,14 +6,14 @@ // A typed decode of a pair that converts, given an input the pair does not read, stops in the // core's refusal, whose requirement names the input's type and the format's type (ruling R2): a -// type that is no text given to a decode into bytes, and a type that is no byte input given to a -// value codec of bytes. +// type that is no text given to a decode into bytes (the format does not convert it), and a type +// that is no byte input given to a value codec of bytes (it decodes from bytes, ruling R25). // clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the // whole text stays on one line. // clang-format off // expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::converts': webcpp::codecs: this format does not convert this type; include the format's header, or specialize webcpp::codecs::value_codec for the format and the type}} -// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::converts': webcpp::codecs: this format does not convert this type; include the format's header, or specialize webcpp::codecs::value_codec for the format and the type}} +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::converts': webcpp::codecs: this value codec decodes from bytes: a std::span, or a byte input}} // clang-format on #include "../../support/be16.hpp" diff --git a/test/messages/typed_decode_text_from_bytes/Jamfile b/test/messages/typed_decode_text_from_bytes/Jamfile new file mode 100644 index 0000000..0ad6720 --- /dev/null +++ b/test/messages/typed_decode_text_from_bytes/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic typed_decode_text_from_bytes : typed_decode_text_from_bytes.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail typed_decode_text_from_bytes_refused : typed_decode_text_from_bytes.cpp + : clang:no : native emscripten ; diff --git a/test/messages/typed_decode_text_from_bytes/typed_decode_text_from_bytes.cpp b/test/messages/typed_decode_text_from_bytes/typed_decode_text_from_bytes.cpp new file mode 100644 index 0000000..2073701 --- /dev/null +++ b/test/messages/typed_decode_text_from_bytes/typed_decode_text_from_bytes.cpp @@ -0,0 +1,56 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A typed decode of a pair that converts, a value codec of text, given bytes: the core's refusal +// says that the value codec decodes from text (ruling R25), and its requirement names the input's +// type and the format's type. + +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error-re@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::converts, word_t>': webcpp::codecs: this value codec decodes from text: a std::string_view, or a contiguous sized range of char}} +// clang-format on + +#include + +#include +#include +#include +#include +#include + +/** A format of text of this file's own. */ +struct word_t { + /** The format's name. */ + static constexpr std::string_view name = "word"; +}; + +/** A value of word: its length. */ +struct word_length { + /** The length. */ + std::size_t length; +}; + +/** The value codec of text of word for a word_length. */ +template <> +struct webcpp::codecs::value_codec { + /** Text. */ + static constexpr webcpp::codecs::output_kind output = webcpp::codecs::output_kind::text; + + /** The length of the text. */ + static webcpp::codecs::result decode(word_t const& /*format*/, + std::string_view text_value) noexcept { + return word_length{text_value.size()}; + } +}; + +int main() { + namespace codecs = webcpp::codecs; + std::array const bytes{0x61, 0x62}; + auto const decoded = + codecs::decode(word_t{}, std::span{bytes}); + return decoded.has_value() ? 0 : 1; +} diff --git a/test/support/declared_values.hpp b/test/support/declared_values.hpp new file mode 100644 index 0000000..6f455fa --- /dev/null +++ b/test/support/declared_values.hpp @@ -0,0 +1,244 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// Two value types of the tests' own that declare their formats to codecs (value_formats, design +// D9), the shape of crypto's keys and signatures. Three formats of bytes: toy_raw (the value's +// bytes), toy_spki (the prefix 30 0c 03 00, then the bytes) and toy_der (the prefix 04 and the +// count, then the bytes). A toy_point is 8 bytes, with value codecs of all three formats in both +// directions but toy_der's, which encodes only; a toy_signature is 4 bytes, with toy_raw's alone. +// One partial specialization constrained by the concept toy_value declares both: a toy_point has +// toy_raw and toy_spki (not toy_der, whose value codec exists), a toy_signature toy_raw. Its +// reasons: an armor_t over toy_spki is refused in both directions ("a point's armor is the toy's +// own", the shape of crypto's PEM), and, only when a test defines +// WEBCPP_CODECS_TEST_DECODE_REASON, a toy_point does not decode from toy_raw, which still encodes +// ("a point decodes from its spki form"). toy_label is a label of the tests' own for an armor +// whose label is in its type. + +#ifndef WEBCPP_CODECS_TEST_SUPPORT_DECLARED_VALUES_HPP +#define WEBCPP_CODECS_TEST_SUPPORT_DECLARED_VALUES_HPP + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace webcpp::codecs::test { + +/** The type of the format toy_raw: a value's bytes as they are. */ +struct toy_raw_t { + /** The format's name. */ + static constexpr std::string_view name = "toy raw"; +}; + +/** The format toy_raw. */ +inline constexpr toy_raw_t toy_raw{}; + +/** The type of the format toy_spki: a fixed prefix, then a value's bytes. */ +struct toy_spki_t { + /** The format's name. */ + static constexpr std::string_view name = "toy spki"; +}; + +/** The format toy_spki. */ +inline constexpr toy_spki_t toy_spki{}; + +/** The type of the format toy_der: the tag 04 and the count, then a value's bytes. */ +struct toy_der_t { + /** The format's name. */ + static constexpr std::string_view name = "toy der"; +}; + +/** The format toy_der. */ +inline constexpr toy_der_t toy_der{}; + +/** A point of 8 bytes, which lists toy_raw and toy_spki. */ +struct toy_point { + /** The bytes. */ + std::array held_bytes{}; + + /** Equal when the bytes are. */ + bool operator==(toy_point const&) const = default; +}; + +/** A signature of 4 bytes, which lists toy_raw. */ +struct toy_signature { + /** The bytes. */ + std::array held_bytes{}; + + /** Equal when the bytes are. */ + bool operator==(toy_signature const&) const = default; +}; + +/** The types the declaration of this header declares. */ +template +concept toy_value = std::same_as || std::same_as; + +/** The prefix `Format` writes before a value's bytes, `Size` of them. */ +template +inline constexpr std::array toy_prefix{}; + +/** toy_spki's prefix. */ +template +inline constexpr std::array toy_prefix{0x30, 0x0c, 0x03, 0x00}; + +/** toy_der's prefix: the tag 04 and the count. */ +template +inline constexpr std::array toy_prefix{ + 0x04, static_cast(Size)}; + +/** The number of bytes of a `Value`. */ +template +inline constexpr std::size_t toy_size = std::tuple_size_v; + +/** The encoding half of a toy value codec: the prefix of `Format`, then the value's bytes. */ +template +struct toy_encoding { + /** Bytes. */ + static constexpr output_kind output = output_kind::bytes; + /** The prefix and the bytes, always. */ + static constexpr std::size_t max_encoded_size = + toy_prefix>.size() + toy_size; + + /** The prefix and the bytes. */ + static std::size_t encoded_size(Format const& /*format*/, + Value const& /*held_value*/) noexcept { + return max_encoded_size; + } + + /** Writes the prefix and the bytes into `output_bytes`, which must hold them. */ + static result encode(Format const& /*format*/, Value const& held_value, + std::span output_bytes) noexcept { + if (output_bytes.size() < max_encoded_size) { + return error::output_too_small; + } + constexpr auto const& prefix = toy_prefix>; + auto const after_prefix = std::ranges::copy(prefix, output_bytes.begin()).out; + std::ranges::copy(held_value.held_bytes, after_prefix); + return max_encoded_size; + } +}; + +/** + The decoding half of a toy value codec: the prefix of `Format` and the value's bytes; another + length is invalid_length, another prefix invalid_prefix. +*/ +template +struct toy_decoding { + /** Reads the prefix and the bytes. */ + static result decode(Format const& /*format*/, + std::span input_bytes) noexcept { + constexpr auto const& prefix = toy_prefix>; + if (input_bytes.size() != prefix.size() + toy_size) { + return error::invalid_length; + } + if (!std::ranges::equal(input_bytes.first(prefix.size()), prefix)) { + return error::invalid_prefix; + } + Value decoded{}; + std::ranges::copy(input_bytes.subspan(prefix.size()), decoded.held_bytes.begin()); + return decoded; + } +}; + +/** A toy value codec that encodes and decodes. */ +template +struct toy_codec : toy_encoding, toy_decoding {}; + +/** Whether a test asks for the reason of a toy_point's decode from toy_raw. */ +#ifdef WEBCPP_CODECS_TEST_DECODE_REASON +inline constexpr bool decode_reason_asked = true; +#else +inline constexpr bool decode_reason_asked = false; +#endif + +/** The reason of a toy_point's decode from toy_raw, when a test asks for it. */ +template +inline constexpr bool raw_point_decode_reason = + decode_reason_asked && std::same_as && std::same_as && + Direction == direction::decode; + +/** Whether `Format` is an armor_t over toy_spki: the toy's armor is its own. */ +template +inline constexpr bool foreign_armor_reason = false; + +/** An armor_t over toy_spki, whatever its allocator. */ +template +inline constexpr bool foreign_armor_reason> = true; + +/** A label of the tests' own for an armor whose label is in its type. */ +struct toy_label { + /** The label. */ + static constexpr std::string_view value = "TOY KEY"; +}; + +/** A point of the example bytes 01 to 08. */ +inline constexpr toy_point toy_point_example{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}}; + +/** A signature of the example bytes a1 to a4. */ +inline constexpr toy_signature toy_signature_example{{0xa1, 0xa2, 0xa3, 0xa4}}; + +} // namespace webcpp::codecs::test + +namespace webcpp::codecs { + +/** toy_raw for a toy_point. */ +template <> +struct value_codec + : test::toy_codec {}; + +/** toy_spki for a toy_point. */ +template <> +struct value_codec + : test::toy_codec {}; + +/** toy_der for a toy_point: encode only. */ +template <> +struct value_codec + : test::toy_encoding {}; + +/** toy_raw for a toy_signature. */ +template <> +struct value_codec + : test::toy_codec {}; + +/** + The declaration of the toy types, one partial specialization for both (the shape of a library's + family of types): each its own list, and the reasons of this header. + + @tparam Value A toy_point or a toy_signature. +*/ +template +struct value_formats { + /** A point has toy_raw and toy_spki; a signature has toy_raw. */ + using formats = std::conditional_t, + format_list, + format_list>; + + /** The pairs refused by a reason of the tests' own. */ + template + static constexpr bool has_reason = test::raw_point_decode_reason || + test::foreign_armor_reason; + + /** The sentence of each reason. */ + template + static constexpr void refuse() noexcept { + static_assert(!test::raw_point_decode_reason, + "webcpp::codecs::test: a point decodes from its spki form"); + static_assert(!test::foreign_armor_reason, + "webcpp::codecs::test: a point's armor is the toy's own"); + } +}; + +} // namespace webcpp::codecs + +#endif // WEBCPP_CODECS_TEST_SUPPORT_DECLARED_VALUES_HPP diff --git a/test/value_formats_test.cpp b/test/value_formats_test.cpp new file mode 100644 index 0000000..da6011f --- /dev/null +++ b/test/value_formats_test.cpp @@ -0,0 +1,286 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A value type's formats, declared (design D9 and D10): the concepts follow the declaration of the +// toy types (support/declared_values.hpp) and of a type of this file's own that names its formats +// by has_format, and are unchanged for be16's undeclared number; every refused pair's operation, in +// each of its forms, is well-formed and noexcept in an unevaluated context, so that its refusal is +// the build's only error (the refusals themselves are message cases, declared_*); a listed pair +// and a composition through its inner link convert to the bytes the value codec writes, the +// declaration changing no conversion; check_conversion of a pair that converts does nothing. + +#include "support/be16.hpp" +#include "support/declared_values.hpp" + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +namespace codecs = webcpp::codecs; +namespace test = webcpp::codecs::test; + +using codecs::direction; + +/** A value of this file's own whose declaration names its formats by has_format. */ +struct family_member { + /** The bytes. */ + std::array held_bytes{}; +}; + +} // namespace + +namespace webcpp::codecs { + +/** toy_raw for a family_member. */ +template <> +struct value_codec + : test::toy_codec {}; + +/** toy_spki for a family_member. */ +template <> +struct value_codec + : test::toy_codec {}; + +/** + A declaration whose list names both formats and whose has_format has toy_raw alone: has_format is + what the core asks, the list what a refusal prints. +*/ +template <> +struct value_formats { + /** The family's formats. */ + using formats = format_list; + + /** This member has toy_raw alone. */ + template + static constexpr bool has_format = std::same_as; +}; + +} // namespace webcpp::codecs + +namespace { + +using base64_spki = codecs::over; +using hex_raw = codecs::over; +using hex_der = codecs::over; +using armor_spki = codecs::over; + +// A listed pair converts in both directions. +static_assert(codecs::encodable); +static_assert(codecs::decodable); +static_assert(codecs::encodable); +static_assert(codecs::decodable); +static_assert(codecs::encodable); +static_assert(codecs::decodable); +// A composition through its inner link. +static_assert(codecs::encodable); +static_assert(codecs::decodable); +static_assert(codecs::encodable); +static_assert(codecs::decodable); +// A value codec exists and the list lacks the format. +static_assert(!codecs::encodable); +static_assert(!codecs::decodable); +// Neither listed nor a value codec. +static_assert(!codecs::encodable); +static_assert(!codecs::decodable); +// A composition over an unlisted inner format. +static_assert(!codecs::encodable); +static_assert(!codecs::decodable); +// A composition refused by a reason, in both directions. +static_assert(!codecs::encodable); +static_assert(!codecs::decodable); +// The same armor over the listed format but through another outer link converts. +static_assert(codecs::encodable>); +// has_format, not the list, says what the type has. +static_assert(codecs::encodable); +static_assert(codecs::decodable); +static_assert(!codecs::encodable); +static_assert(!codecs::decodable); +// The declaration is the value's: a cv-qualified value is the same. +static_assert(codecs::encodable); +static_assert(!codecs::encodable); +// An undeclared type keeps the genesis' concepts. +static_assert(!codecs::detail::declared_v); +static_assert(codecs::detail::declared_v); +static_assert(codecs::encodable); +static_assert(codecs::decodable); +static_assert(!codecs::encodable); +static_assert(!codecs::decodable); +// Bytes encoded by a codec are never declared. +static_assert(codecs::encodable, codecs::formats::hex_t>); +static_assert(codecs::decodable, codecs::formats::hex_t>); + +/** + Whether each form of an encode of `Value` in `Format` is well-formed and noexcept, unevaluated: + the default result, a token of codecs', each buffer, and the size. +*/ +template +constexpr bool encode_forms_are_well_formed() { + return requires(Format const& format_value, Value const& held_value, + std::span output_bytes, std::span output_characters) { + { codecs::encode(format_value, held_value) } noexcept; + { codecs::encode(format_value, held_value, codecs::tokens::fixed<16>{}) } noexcept; + { codecs::encode(format_value, held_value, codecs::tokens::return_string) } noexcept; + { codecs::encode(format_value, held_value, output_bytes) } noexcept; + { codecs::encode(format_value, held_value, output_characters) } noexcept; + { codecs::encoded_size(format_value, held_value) } noexcept; + }; +} + +/** Whether the typed decode of `Value` in `Format` is well-formed and noexcept, unevaluated. */ +template +constexpr bool decode_forms_are_well_formed() { + return requires(Format const& format_value, std::span input_bytes, + std::string_view input_text) { + { codecs::decode(format_value, input_bytes) } noexcept; + { codecs::decode(format_value, input_text) } noexcept; + }; +} + +// Every refused pair is taken by every form, noexcept: its refusal is its only error. +// The list: a value codec exists, the list lacks the format. +static_assert(encode_forms_are_well_formed()); +static_assert(decode_forms_are_well_formed()); +// The list, with no value codec either. +static_assert(encode_forms_are_well_formed()); +static_assert(decode_forms_are_well_formed()); +// A composition's reason, in both directions. +static_assert(encode_forms_are_well_formed()); +static_assert(decode_forms_are_well_formed()); +// A composition over an unlisted inner format. +static_assert(encode_forms_are_well_formed()); +static_assert(decode_forms_are_well_formed()); +// Listed by has_format's family, not had: the list. +static_assert(encode_forms_are_well_formed()); +static_assert(decode_forms_are_well_formed()); + +/** The bytes of `held_value` written by the value codec of `Format` itself, outside the core. */ +template +std::vector value_codec_bytes(Format const& format_value, Value const& held_value) { + using value_type = codecs::value_codec; + std::vector written(value_type::encoded_size(format_value, held_value)); + auto const written_size = value_type::encode(format_value, held_value, written); + BOOST_TEST(written_size.has_value()); + return written; +} + +/** The bytes a fixed result of bytes holds. */ +template +std::vector held(Bytes const& fixed_bytes) { + auto const view = fixed_bytes.view(); + return {view.begin(), view.end()}; +} + +/** + A listed pair's encodings are the value codec's own bytes, through every form, and decode back: + the declaration changes no conversion. +*/ +void a_listed_pair_converts_as_before() { + test::toy_point const point = test::toy_point_example; + std::vector const raw = value_codec_bytes(test::toy_raw, point); + std::vector const spki = value_codec_bytes(test::toy_spki, point); + BOOST_TEST((raw == std::vector{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08})); + BOOST_TEST((spki == std::vector{0x30, 0x0c, 0x03, 0x00, 0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08})); + + auto const raw_encoded = codecs::encode(test::toy_raw, point); + BOOST_TEST(raw_encoded.has_value() && held(raw_encoded.value()) == raw); + auto const spki_encoded = codecs::encode(test::toy_spki, point); + BOOST_TEST(spki_encoded.has_value() && held(spki_encoded.value()) == spki); + BOOST_TEST_EQ(codecs::encoded_size(test::toy_spki, point), spki.size()); + + std::array buffer{}; + auto const written = codecs::encode(test::toy_spki, point, std::span{buffer}); + BOOST_TEST(written.has_value() && written.value() == spki.size()); + BOOST_TEST(std::ranges::equal(buffer, spki)); + auto const fixed = codecs::encode(test::toy_spki, point, codecs::tokens::fixed<16>{}); + BOOST_TEST(fixed.has_value() && held(fixed.value()) == spki); + + auto const raw_decoded = codecs::decode(test::toy_raw, raw); + BOOST_TEST(raw_decoded.has_value() && raw_decoded.value() == point); + auto const spki_decoded = codecs::decode(test::toy_spki, spki); + BOOST_TEST(spki_decoded.has_value() && spki_decoded.value() == point); + // The value codec's own errors reach the caller unchanged. + auto const wrong_length = codecs::decode(test::toy_spki, raw); + BOOST_TEST(!wrong_length.has_value() && + wrong_length.error() == codecs::make_error_code(codecs::error::invalid_length)); + + test::toy_signature const signature = test::toy_signature_example; + auto const signature_encoded = codecs::encode(test::toy_raw, signature); + BOOST_TEST(signature_encoded.has_value() && + held(signature_encoded.value()) == value_codec_bytes(test::toy_raw, signature)); + auto const signature_decoded = + codecs::decode(test::toy_raw, held(signature_encoded.value())); + BOOST_TEST(signature_decoded.has_value() && signature_decoded.value() == signature); + + family_member const member{{0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}}; + auto const member_encoded = codecs::encode(test::toy_raw, member); + BOOST_TEST(member_encoded.has_value() && + held(member_encoded.value()) == value_codec_bytes(test::toy_raw, member)); +} + +/** A composition the type does not list converts through its inner link. */ +void a_composition_converts_through_its_inner_link() { + test::toy_point const point = test::toy_point_example; + std::vector const spki = value_codec_bytes(test::toy_spki, point); + auto const spki_base64 = codecs::encode(codecs::formats::base64, spki); + BOOST_TEST(spki_base64.has_value()); + + auto const encoded = + codecs::encode(codecs::over{codecs::formats::base64, test::toy_spki}, point); + BOOST_TEST(encoded.has_value()); + BOOST_TEST_EQ(std::string{encoded.value().view()}, spki_base64.value()); + BOOST_TEST_EQ(std::string{encoded.value().view()}, std::string{"MAwDAAECAwQFBgcI"}); + auto const decoded = codecs::decode( + codecs::over{codecs::formats::base64, test::toy_spki}, encoded.value().view()); + BOOST_TEST(decoded.has_value() && decoded.value() == point); + + auto const hex_encoded = + codecs::encode(codecs::over{codecs::formats::hex, test::toy_raw}, point); + BOOST_TEST(hex_encoded.has_value()); + BOOST_TEST_EQ(std::string{hex_encoded.value().view()}, std::string{"0102030405060708"}); + auto const hex_decoded = codecs::decode( + codecs::over{codecs::formats::hex, test::toy_raw}, hex_encoded.value().view()); + BOOST_TEST(hex_decoded.has_value() && hex_decoded.value() == point); +} + +/** check_conversion of a pair that converts does nothing, at compile time and at run time. */ +void check_conversion_of_a_converting_pair_does_nothing() { + static_assert( + noexcept(codecs::check_conversion())); + static_assert( + (codecs::check_conversion(), true)); + codecs::check_conversion(); + codecs::check_conversion(); + codecs::check_conversion(); + codecs::check_conversion(); + codecs::check_conversion, + codecs::formats::hex_t>(); + BOOST_TEST(true); +} + +} // namespace + +int main() { + a_listed_pair_converts_as_before(); + a_composition_converts_through_its_inner_link(); + check_conversion_of_a_converting_pair_does_nothing(); + return boost::report_errors(); +} From 5c7a7a678ee33b306a02d6b75aabd048e73d2532 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 18:22:25 -0300 Subject: [PATCH 11/30] codecs: the declaration of a value type's formats lint fix --- .../webcpp/codecs/detail/core/contract.hpp | 45 +++++++++++++------ .../webcpp/codecs/detail/core/operations.hpp | 7 ++- test/support/declared_values.hpp | 19 +++++--- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/include/webcpp/codecs/detail/core/contract.hpp b/include/webcpp/codecs/detail/core/contract.hpp index 756db91..8e4e514 100644 --- a/include/webcpp/codecs/detail/core/contract.hpp +++ b/include/webcpp/codecs/detail/core/contract.hpp @@ -720,9 +720,13 @@ concept composition = requires { typename composition_inner::type; }; /** Whether a value codec of `Format` and `Value` converts in `Direction`, as the genesis says. */ template -inline constexpr bool value_converts = - Direction == direction::encode ? value_encodable - : value_decodable; +[[nodiscard]] consteval bool value_converts() noexcept { + if constexpr (Direction == direction::encode) { + return value_encodable; + } else { + return value_decodable; + } +} /** Whether `Value`'s declaration has `Format`: its `has_format` when it has one, else @@ -749,6 +753,24 @@ template } } +template +[[nodiscard]] consteval bool pair_converts() noexcept; + +/** + Whether a declared `Value` has `Format` in `Direction` (design D9's second condition): the + declaration has the format, or the format is a composition whose inner link's pair converts. +*/ +template +[[nodiscard]] consteval bool declared_or_composed() noexcept { + if constexpr (declared_format()) { + return true; + } else if constexpr (composition) { + return pair_converts::type, Value>(); + } else { + return false; + } +} + /** Whether the pair of `Format` and `Value` converts in `Direction` (design D9): for a declared `Value`, no reason of its own, the type has the format or the format is a composition whose inner @@ -758,17 +780,12 @@ template template [[nodiscard]] consteval bool pair_converts() noexcept { using value_type = std::remove_cvref_t; - if constexpr (!declared_v) { - return value_converts; - } else if constexpr (declared_reason()) { - return false; - } else if constexpr (declared_format()) { - return value_converts; - } else if constexpr (composition) { - return pair_converts::type, value_type>() && - value_converts; + if constexpr (declared_v) { + return !declared_reason() && + declared_or_composed() && + value_converts(); } else { - return false; + return value_converts(); } } @@ -866,7 +883,7 @@ constexpr void refuse_conversion() noexcept { "pair decodes it"); } } else if constexpr (composition) { - using inner_type = typename composition_inner::type; + using inner_type = composition_inner::type; if constexpr (!pair_converts()) { refuse_conversion(); } else { diff --git a/include/webcpp/codecs/detail/core/operations.hpp b/include/webcpp/codecs/detail/core/operations.hpp index 9abb957..6e6776a 100644 --- a/include/webcpp/codecs/detail/core/operations.hpp +++ b/include/webcpp/codecs/detail/core/operations.hpp @@ -1080,10 +1080,8 @@ template [[nodiscard]] result decode(F const& format_value, Input const& input_value) noexcept( detail::typed_decode_is_noexcept()) { constexpr bool secret = detail::secret_operation_v; - if constexpr (detail::declaration_refuses) { - detail::refuse_conversion(); - return make_error_code(error::invalid_length); - } else if constexpr (detail::value_decodable) { + if constexpr (detail::value_decodable && + !detail::declaration_refuses) { using value_type = value_codec; if (auto const configuration_error = detail::configuration_fault_of(format_value)) { return make_error_code(*configuration_error); @@ -1123,6 +1121,7 @@ template return detail::decode_as(format_value, input_value); } } else { + // A pair that does not convert: a declared type's by its declaration (design D10). detail::refuse_conversion(); return make_error_code(error::invalid_length); } diff --git a/test/support/declared_values.hpp b/test/support/declared_values.hpp index 6f455fa..ba49670 100644 --- a/test/support/declared_values.hpp +++ b/test/support/declared_values.hpp @@ -94,7 +94,9 @@ inline constexpr std::array toy_prefix{0x30, /** toy_der's prefix: the tag 04 and the count. */ template inline constexpr std::array toy_prefix{ - 0x04, static_cast(Size)}; + 0x04, + static_cast(Size), +}; /** The number of bytes of a `Value`. */ template @@ -163,9 +165,14 @@ inline constexpr bool decode_reason_asked = false; /** The reason of a toy_point's decode from toy_raw, when a test asks for it. */ template -inline constexpr bool raw_point_decode_reason = - decode_reason_asked && std::same_as && std::same_as && - Direction == direction::decode; +consteval bool raw_point_decode_reason() noexcept { + if constexpr (Direction == direction::decode) { + return decode_reason_asked && std::same_as && + std::same_as; + } else { + return false; + } +} /** Whether `Format` is an armor_t over toy_spki: the toy's armor is its own. */ template @@ -226,13 +233,13 @@ struct value_formats { /** The pairs refused by a reason of the tests' own. */ template - static constexpr bool has_reason = test::raw_point_decode_reason || + static constexpr bool has_reason = test::raw_point_decode_reason() || test::foreign_armor_reason; /** The sentence of each reason. */ template static constexpr void refuse() noexcept { - static_assert(!test::raw_point_decode_reason, + static_assert(!test::raw_point_decode_reason(), "webcpp::codecs::test: a point decodes from its spki form"); static_assert(!test::foreign_armor_reason, "webcpp::codecs::test: a point's armor is the toy's own"); From 9d2aef2c0603e200fdadf63c8f4b1b7634e5547f Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 18:51:30 -0300 Subject: [PATCH 12/30] codecs: a declaration's has_format and has_reason found on MSVC 14.3 --- .../webcpp/codecs/detail/core/contract.hpp | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/include/webcpp/codecs/detail/core/contract.hpp b/include/webcpp/codecs/detail/core/contract.hpp index 8e4e514..1f1711f 100644 --- a/include/webcpp/codecs/detail/core/contract.hpp +++ b/include/webcpp/codecs/detail/core/contract.hpp @@ -728,6 +728,32 @@ template } } +/** + Whether `Declaration`, a @ref value_formats, states `has_format`. A partial specialization + over `std::void_t`, where MSVC 14.3 finds no member variable template through a requires + expression. +*/ +template +inline constexpr bool states_has_format = false; + +/** A declaration that states `has_format`. */ +template +inline constexpr bool states_has_format< + Declaration, Format, + std::void_t(Declaration::template has_format)>>> = + true; + +/** Whether `Declaration`, a @ref value_formats, states `has_reason`. */ +template +inline constexpr bool states_has_reason = false; + +/** A declaration that states `has_reason`. */ +template +inline constexpr bool + states_has_reason( + Declaration::template has_reason)>>> = true; + /** Whether `Value`'s declaration has `Format`: its `has_format` when it has one, else `Format` in its `formats`. @@ -735,7 +761,7 @@ template template [[nodiscard]] consteval bool declared_format() noexcept { using declaration = value_formats; - if constexpr (requires { declaration::template has_format; }) { + if constexpr (states_has_format) { return static_cast(declaration::template has_format); } else { return in_format_list; @@ -746,7 +772,7 @@ template template [[nodiscard]] consteval bool declared_reason() noexcept { using declaration = value_formats; - if constexpr (requires { declaration::template has_reason; }) { + if constexpr (states_has_reason) { return static_cast(declaration::template has_reason); } else { return false; From 1edeb26883b8f2eae028fb8c37c83ba05a1b8e87 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 19:22:15 -0300 Subject: [PATCH 13/30] codecs: the declaration's refusals, every kind through every form, and a declared byte input refused alone --- .../webcpp/codecs/detail/core/contract.hpp | 29 +++++-- .../webcpp/codecs/detail/core/operations.hpp | 12 ++- include/webcpp/codecs/detail/core/sizes.hpp | 6 +- test/declared_reason_encode_builds_test.cpp | 5 ++ test/messages/Jamfile | 1 + test/messages/declared_byte_input/Jamfile | 16 ++++ .../declared_byte_input.cpp | 41 ++++++++++ test/support/declared_values.hpp | 31 +++++++- test/value_formats_test.cpp | 77 ++++++++++--------- 9 files changed, 166 insertions(+), 52 deletions(-) create mode 100644 test/messages/declared_byte_input/Jamfile create mode 100644 test/messages/declared_byte_input/declared_byte_input.cpp diff --git a/include/webcpp/codecs/detail/core/contract.hpp b/include/webcpp/codecs/detail/core/contract.hpp index 1f1711f..9935bbb 100644 --- a/include/webcpp/codecs/detail/core/contract.hpp +++ b/include/webcpp/codecs/detail/core/contract.hpp @@ -238,8 +238,15 @@ enum class direction : unsigned char { as `List`; for a format the type has and no value codec of the pair converts, codecs' assertion that the type lists the format and no value codec of the pair encodes it (or decodes it). A composition the type does not have is refused as its inner link is, after any reason given for - the composition itself. A type with no declaration converts and is refused as before: the - declaration governs a value codec's pairs only, never the bytes a `codec` encodes or decodes. + the composition itself. + + The members' shapes are fixed: `has_format` takes the format alone, and `has_reason` and + `refuse` take the format and then the direction, as above; a member of another shape is not + found, and the type is read as not stating it. `refuse()` must stop the build + for every pair whose `has_reason` is true, its sentence the build's one error: codecs calls it + instead of converting, and an operation it does not stop returns an error, never a value. A type + with no declaration converts and is refused as before: the declaration governs a value codec's + pairs only, never the bytes a `codec` encodes or decodes. @tparam Value The value's type. */ @@ -927,6 +934,16 @@ constexpr void refuse_conversion() noexcept { } } +/** Whether the pair of `Format` and `Value` converts in `Direction`, as the concepts say. */ +template +[[nodiscard]] consteval bool converts_in() noexcept { + if constexpr (Direction == direction::encode) { + return encodable; + } else { + return decodable; + } +} + /** Whether a declared `Value` refuses the pair of `Format` in `Direction`: the operations of codecs take such a pair (a buffer form among them, which would otherwise drop it from overload @@ -934,9 +951,7 @@ constexpr void refuse_conversion() noexcept { exception specification well-formed, so that the refusal is the build's only error. */ template -concept declaration_refuses = - declared_v && - !(Direction == direction::encode ? encodable : decodable); +concept declaration_refuses = declared_v && !converts_in(); /** Stops the build: a typed decode gave a value codec whose `output` is `Output` an input it does @@ -976,9 +991,7 @@ constexpr void refuse_input() noexcept { */ template constexpr void check_conversion() noexcept { - constexpr bool converting = - Direction == direction::encode ? encodable : decodable; - if constexpr (!converting) { + if constexpr (!detail::converts_in()) { detail::refuse_conversion(); } } diff --git a/include/webcpp/codecs/detail/core/operations.hpp b/include/webcpp/codecs/detail/core/operations.hpp index 6e6776a..2be9079 100644 --- a/include/webcpp/codecs/detail/core/operations.hpp +++ b/include/webcpp/codecs/detail/core/operations.hpp @@ -765,7 +765,8 @@ template [[nodiscard]] result encode(F const& /*format_value*/, Input const& /*input_value*/) noexcept { detail::refuse_conversion(); - return result{std::string{}}; + // Unreachable once the refusal stops the build: an error, never a value. + return result{make_error_code(error::invalid_length)}; } /** @@ -798,7 +799,8 @@ template std::span output_buffer) noexcept(detail::encode_form_is_noexcept()) { if constexpr (detail::declaration_refuses) { detail::refuse_conversion(); - return std::size_t{0}; + // Unreachable once the refusal stops the build: an error, never a size written. + return error::invalid_length; } else if constexpr (detail::secret_operation_v) { secure::timing_guard const timing_scope{true}; return detail::encode_value_into_buffer(format_value, input_value, output_buffer); @@ -837,7 +839,8 @@ template std::span output_buffer) noexcept(detail::encode_form_is_noexcept()) { if constexpr (detail::declaration_refuses) { detail::refuse_conversion(); - return std::size_t{0}; + // Unreachable once the refusal stops the build: an error, never a size written. + return error::invalid_length; } else if constexpr (detail::secret_operation_v) { secure::timing_guard const timing_scope{true}; return detail::encode_value_into_buffer(format_value, input_value, output_buffer); @@ -974,7 +977,8 @@ template [[nodiscard]] result encode(F const& /*format_value*/, Input const& /*input_value*/, Token&& /*token*/) noexcept { detail::refuse_conversion(); - return result{std::string{}}; + // Unreachable once the refusal stops the build: an error, never a value. + return result{make_error_code(error::invalid_length)}; } /** diff --git a/include/webcpp/codecs/detail/core/sizes.hpp b/include/webcpp/codecs/detail/core/sizes.hpp index 19e738e..f5be7c4 100644 --- a/include/webcpp/codecs/detail/core/sizes.hpp +++ b/include/webcpp/codecs/detail/core/sizes.hpp @@ -321,7 +321,8 @@ template secure's timing guard and runs the format's secret arithmetic where it has one (D10). A format with no `codec` specialization stops the build with a message that the format does not convert this type. A type a value codec of `F` encodes takes the value codec's buffer form instead - (operations.hpp). + (operations.hpp), as a type that declares its formats (@ref value_formats) and whose pair does not + convert does, which refuses it by its declaration. @tparam F The format's type. @tparam Input The input's type: a contiguous sized range of `std::uint8_t`, `unsigned char`, @@ -333,7 +334,8 @@ template @return The number of characters written, or the error. */ template - requires(!detail::value_encodable) + requires(!detail::value_encodable && + !detail::declaration_refuses) [[nodiscard]] result encode(F const& format_value, Input const& input_value, std::span output_buffer) noexcept( detail::encode_calls_are_noexcept>()) { diff --git a/test/declared_reason_encode_builds_test.cpp b/test/declared_reason_encode_builds_test.cpp index 593f3ab..58e5f4a 100644 --- a/test/declared_reason_encode_builds_test.cpp +++ b/test/declared_reason_encode_builds_test.cpp @@ -43,6 +43,11 @@ static_assert( codecs::encodable>); static_assert( !codecs::decodable>); +// The refused direction's forms are well-formed and noexcept, so that the reason is the build's +// only error; the encode direction's forms are those of a pair that converts. +static_assert(test::decode_forms_are_well_formed()); +static_assert(test::decode_forms_are_well_formed< + test::toy_point, codecs::over>()); /** The encode of the pair the reason refuses in decode builds and writes the point's bytes. */ void the_encode_of_the_pair_builds() { diff --git a/test/messages/Jamfile b/test/messages/Jamfile index cfa99df..3210e8b 100644 --- a/test/messages/Jamfile +++ b/test/messages/Jamfile @@ -62,3 +62,4 @@ build-project declared_without_value_codec ; build-project declared_composition_inner ; build-project declared_check_conversion ; build-project typed_decode_text_from_bytes ; +build-project declared_byte_input ; diff --git a/test/messages/declared_byte_input/Jamfile b/test/messages/declared_byte_input/Jamfile new file mode 100644 index 0000000..056b361 --- /dev/null +++ b/test/messages/declared_byte_input/Jamfile @@ -0,0 +1,16 @@ +# Copyright (c) 2026 WebCpp.org +# +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# https://www.boost.org/LICENSE_1_0.txt) + +project : requirements /webcpp/codecs//codecs ; + +import webcpp ; + +# alone: the refusal is the compile's only error, so a regression that puts +# another error before or after it fails the test. +webcpp.compile-diagnostic declared_byte_input : declared_byte_input.cpp + : : native wasip2 wasip3 : alone ; +webcpp.compile-fail declared_byte_input_refused : declared_byte_input.cpp + : clang:no : native emscripten ; diff --git a/test/messages/declared_byte_input/declared_byte_input.cpp b/test/messages/declared_byte_input/declared_byte_input.cpp new file mode 100644 index 0000000..193d15c --- /dev/null +++ b/test/messages/declared_byte_input/declared_byte_input.cpp @@ -0,0 +1,41 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// A declared type that is also a byte input, encoded into a std::span in a format it does not +// list and that has no codec: the value's buffer form alone takes it, and refuses it with the +// list's assertion, the build's only error (never an ambiguous call with the bytes' buffer form). + +// clang's -verify reads a directive from one comment line: wrapped, it checks nothing, so the +// whole text stays on one line. +// clang-format off +// expected-error@webcpp/codecs/detail/core/contract.hpp:* {{static assertion failed due to requirement 'webcpp::codecs::detail::format_listed>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion}} +// clang-format on + +#include "../../support/declared_values.hpp" + +#include + +#include +#include +#include + +/** A value that is a byte input, 4 bytes, and declares toy_raw. */ +struct byte_value : std::array {}; + +/** The value's declaration. */ +template <> +struct webcpp::codecs::value_formats { + /** toy_raw. */ + using formats = webcpp::codecs::format_list; +}; + +int main() { + namespace codecs = webcpp::codecs; + std::array output{}; + auto const written = + codecs::encode(codecs::test::toy_der, byte_value{}, std::span{output}); + return written.has_value() ? 0 : 1; +} diff --git a/test/support/declared_values.hpp b/test/support/declared_values.hpp index ba49670..057c577 100644 --- a/test/support/declared_values.hpp +++ b/test/support/declared_values.hpp @@ -15,7 +15,9 @@ // own", the shape of crypto's PEM), and, only when a test defines // WEBCPP_CODECS_TEST_DECODE_REASON, a toy_point does not decode from toy_raw, which still encodes // ("a point decodes from its spki form"). toy_label is a label of the tests' own for an armor -// whose label is in its type. +// whose label is in its type. encode_forms_are_well_formed and decode_forms_are_well_formed say +// whether every form of an operation of a pair is well-formed and noexcept in an unevaluated +// context, which a refused pair's forms are, so that its refusal is the build's only error. #ifndef WEBCPP_CODECS_TEST_SUPPORT_DECLARED_VALUES_HPP #define WEBCPP_CODECS_TEST_SUPPORT_DECLARED_VALUES_HPP @@ -194,6 +196,33 @@ inline constexpr toy_point toy_point_example{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06 /** A signature of the example bytes a1 to a4. */ inline constexpr toy_signature toy_signature_example{{0xa1, 0xa2, 0xa3, 0xa4}}; +/** + Whether each form of an encode of `Value` in `Format` is well-formed and noexcept, unevaluated: + the default result, a token of codecs', each buffer, and the size. +*/ +template +constexpr bool encode_forms_are_well_formed() { + return requires(Format const& format_value, Value const& held_value, + std::span output_bytes, std::span output_characters) { + { codecs::encode(format_value, held_value) } noexcept; + { codecs::encode(format_value, held_value, codecs::tokens::fixed<16>{}) } noexcept; + { codecs::encode(format_value, held_value, codecs::tokens::return_string) } noexcept; + { codecs::encode(format_value, held_value, output_bytes) } noexcept; + { codecs::encode(format_value, held_value, output_characters) } noexcept; + { codecs::encoded_size(format_value, held_value) } noexcept; + }; +} + +/** Whether the typed decode of `Value` in `Format` is well-formed and noexcept, unevaluated. */ +template +constexpr bool decode_forms_are_well_formed() { + return requires(Format const& format_value, std::span input_bytes, + std::string_view input_text) { + { codecs::decode(format_value, input_bytes) } noexcept; + { codecs::decode(format_value, input_text) } noexcept; + }; +} + } // namespace webcpp::codecs::test namespace webcpp::codecs { diff --git a/test/value_formats_test.cpp b/test/value_formats_test.cpp index da6011f..a7d2d58 100644 --- a/test/value_formats_test.cpp +++ b/test/value_formats_test.cpp @@ -45,6 +45,12 @@ struct family_member { std::array held_bytes{}; }; +/** A value of this file's own whose declaration lists toy_der, which no value codec of it has. */ +struct listed_without_codec { + /** The bytes. */ + std::array held_bytes{}; +}; + } // namespace namespace webcpp::codecs { @@ -73,6 +79,18 @@ struct value_formats { static constexpr bool has_format = std::same_as; }; +/** toy_raw for a listed_without_codec. */ +template <> +struct value_codec + : test::toy_codec {}; + +/** A declaration that lists toy_der, of which the value has no value codec. */ +template <> +struct value_formats { + /** toy_raw and toy_der. */ + using formats = format_list; +}; + } // namespace webcpp::codecs namespace { @@ -127,49 +145,34 @@ static_assert(!codecs::decodable); static_assert(codecs::encodable, codecs::formats::hex_t>); static_assert(codecs::decodable, codecs::formats::hex_t>); -/** - Whether each form of an encode of `Value` in `Format` is well-formed and noexcept, unevaluated: - the default result, a token of codecs', each buffer, and the size. -*/ -template -constexpr bool encode_forms_are_well_formed() { - return requires(Format const& format_value, Value const& held_value, - std::span output_bytes, std::span output_characters) { - { codecs::encode(format_value, held_value) } noexcept; - { codecs::encode(format_value, held_value, codecs::tokens::fixed<16>{}) } noexcept; - { codecs::encode(format_value, held_value, codecs::tokens::return_string) } noexcept; - { codecs::encode(format_value, held_value, output_bytes) } noexcept; - { codecs::encode(format_value, held_value, output_characters) } noexcept; - { codecs::encoded_size(format_value, held_value) } noexcept; - }; -} - -/** Whether the typed decode of `Value` in `Format` is well-formed and noexcept, unevaluated. */ -template -constexpr bool decode_forms_are_well_formed() { - return requires(Format const& format_value, std::span input_bytes, - std::string_view input_text) { - { codecs::decode(format_value, input_bytes) } noexcept; - { codecs::decode(format_value, input_text) } noexcept; - }; -} - // Every refused pair is taken by every form, noexcept: its refusal is its only error. // The list: a value codec exists, the list lacks the format. -static_assert(encode_forms_are_well_formed()); -static_assert(decode_forms_are_well_formed()); +static_assert(test::encode_forms_are_well_formed()); +static_assert(test::decode_forms_are_well_formed()); // The list, with no value codec either. -static_assert(encode_forms_are_well_formed()); -static_assert(decode_forms_are_well_formed()); +static_assert(test::encode_forms_are_well_formed()); +static_assert(test::decode_forms_are_well_formed()); // A composition's reason, in both directions. -static_assert(encode_forms_are_well_formed()); -static_assert(decode_forms_are_well_formed()); +static_assert(test::encode_forms_are_well_formed()); +static_assert(test::decode_forms_are_well_formed()); // A composition over an unlisted inner format. -static_assert(encode_forms_are_well_formed()); -static_assert(decode_forms_are_well_formed()); +static_assert(test::encode_forms_are_well_formed()); +static_assert(test::decode_forms_are_well_formed()); +// Listed with no value codec of the pair (design D10's third refusal), directly and as a +// composition's inner link. +static_assert(codecs::encodable); +static_assert(codecs::decodable); +static_assert(!codecs::encodable); +static_assert(!codecs::decodable); +static_assert(!codecs::encodable); +static_assert(!codecs::decodable); +static_assert(test::encode_forms_are_well_formed()); +static_assert(test::decode_forms_are_well_formed()); +static_assert(test::encode_forms_are_well_formed()); +static_assert(test::decode_forms_are_well_formed()); // Listed by has_format's family, not had: the list. -static_assert(encode_forms_are_well_formed()); -static_assert(decode_forms_are_well_formed()); +static_assert(test::encode_forms_are_well_formed()); +static_assert(test::decode_forms_are_well_formed()); /** The bytes of `held_value` written by the value codec of `Format` itself, outside the core. */ template From 6a8b86c42ea0b288bd364ae0b2edfb8d95ac9454 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 2 Dec 2026 20:00:25 -0300 Subject: [PATCH 14/30] codecs: basic_armor, the armor whose label is in its type, exactly as long as its longest text --- include/webcpp/codecs/armor.hpp | 211 ++++++++++- test/armor_openssl_test.cpp | 43 ++- test/armor_stream_test.cpp | 148 ++++++-- test/armor_test.cpp | 337 ++++++++++++++++-- test/constant_time/paths.hpp | 151 ++++---- test/default_result_test.cpp | 33 ++ test/messages/Jamfile | 1 + test/messages/armor_label_invalid/Jamfile | 16 + .../armor_label_invalid.cpp | 36 ++ test/noexcept_test.cpp | 48 +++ test/support/armor_labels.hpp | 126 +++++++ 11 files changed, 1026 insertions(+), 124 deletions(-) create mode 100644 test/messages/armor_label_invalid/Jamfile create mode 100644 test/messages/armor_label_invalid/armor_label_invalid.cpp create mode 100644 test/support/armor_labels.hpp diff --git a/include/webcpp/codecs/armor.hpp b/include/webcpp/codecs/armor.hpp index e2f4107..22e6e1c 100644 --- a/include/webcpp/codecs/armor.hpp +++ b/include/webcpp/codecs/armor.hpp @@ -10,7 +10,9 @@ by a line feed, which is what OpenSSL's `PEM_write_bio` writes. The label is configured by a call, `formats::armor("PUBLIC KEY")`, and held in the format value; a format with none decodes any, and `codecs::decode(formats::armor, text)` gives the label and the bytes. It - composes: `over{formats::armor("PRIVATE KEY"), inner}` armors any bytes format's output. + composes: `over{formats::armor("PRIVATE KEY"), inner}` armors any bytes format's output. When the + label follows from what the bytes are, `formats::basic_armor