From 4a8f96f95da51e6b570a00ddcfefe100844d8f8b Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Mon, 27 Feb 2017 13:33:16 -0500 Subject: [PATCH] Improvements for epee binary to hex functions: - Performance improvements - Added `span` for zero-copy pointer+length arguments - Added `std::ostream` overload for direct writing to output buffers - Removal of unused `string_tools::buff_to_hex` --- contrib/epee/include/hex.h | 78 +++++ contrib/epee/include/span_view.h | 174 ++++++++++ contrib/epee/include/string_tools.h | 33 +- contrib/epee/src/CMakeLists.txt | 2 +- contrib/epee/src/hex.cpp | 82 +++++ contrib/epee/src/http_auth.cpp | 22 +- src/cryptonote_basic/cryptonote_basic.h | 1 - src/cryptonote_basic/cryptonote_basic_impl.h | 24 +- src/ringct/rctTypes.h | 4 +- tests/unit_tests/CMakeLists.txt | 1 + tests/unit_tests/crypto.cpp | 75 +++++ tests/unit_tests/epee_utils.cpp | 332 ++++++++++++++++++- tests/unit_tests/ringct.cpp | 11 + tests/unit_tests/serialization.cpp | 4 - 14 files changed, 767 insertions(+), 76 deletions(-) create mode 100644 contrib/epee/include/hex.h create mode 100644 contrib/epee/include/span_view.h create mode 100644 contrib/epee/src/hex.cpp create mode 100644 tests/unit_tests/crypto.cpp diff --git a/contrib/epee/include/hex.h b/contrib/epee/include/hex.h new file mode 100644 index 000000000..99a86503a --- /dev/null +++ b/contrib/epee/include/hex.h @@ -0,0 +1,78 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#pragma once + +#include +#include +#include +#include + +#include "span_view.h" + +namespace epee +{ + struct to_hex + { + //! \return A std::string containing hex of `src`. + static std::string string(const view src); + + //! \return A std::string containing hex of `src`. + static std::string string(const view src) { + return string(view_cast(src)); + } + + //! \return An array containing hex of `src`. + template + static std::array + array(const std::array& src) noexcept(noexcept(view(src))) + { + std::array out{{}}; + static_assert(N <= 128, "keep the stack size down"); + buffer_unchecked(out.data(), src); + return out; + } + + //! Append `src` as hex to `out`. + static void buffer(std::ostream& out, const view src); + + //! Append `< + src + >` as hex to `out`. + static void formatted(std::ostream& out, const view src); + + //! Append `< + src + >` as hex to `out`. + template + static void formatted_from_pod(std::ostream& out, const T& pod) + { + formatted(out, pod_cast(pod)); + } + + private: + //! Write `src` bytes as hex to `out`. `out` must be twice the length + static void buffer_unchecked(char* out, const view src) noexcept; + }; +} diff --git a/contrib/epee/include/span_view.h b/contrib/epee/include/span_view.h new file mode 100644 index 000000000..33c538d37 --- /dev/null +++ b/contrib/epee/include/span_view.h @@ -0,0 +1,174 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#pragma once + +#include +#include +#include +#include + +namespace epee +{ + /*! + \brief Non-owning sequence of data. Does not deep copy + + Inspired by `gsl::span`. This class is intended to be used as a parameter + type for functions that need to take a writable or read-only sequence of + data. Most common cases are `span` and `span`. Using as + a class member is only recommended if clearly documented as not doing a + deep-copy. `std::vector`, `std::string`, `std::array`, and C-arrays are all + easily convertible to this type. + + \note Conversion from C string literal to `view` will include the + NULL-terminator. + \note `view` is an alias for `span` which is read-only. + \note Never allows derived-to-base pointer conversion; an array of derived + types is not an array of base types. + */ + template + class span + { + /* Supporting class types is tricky - the {ptr,len} and {container} + constructors will allow derived-to-base conversions. This is NOT + desireable because an array of derived types is not an array of base + types. It is possible to handle this case, implement when/if needed. */ + static_assert(std::is_integral::value, "only integral values currently for span"); + + // Disables ADL for these calls. + struct get { + // we only accept pointer and size_t return types, so use noexcept aggressively + template + static constexpr T* data(T (&src)[N]) noexcept { return src; } + + template + static constexpr auto + data(C& src) noexcept(noexcept(src.data())) -> decltype(src.data()) { + return src.data(); + } + + + template + static constexpr std::size_t size(T (&)[N]) noexcept { return N; } + + template + static constexpr auto + size(const C& src) noexcept(noexcept(src.size())) -> decltype(src.size()) { + return src.size(); + } + }; + + public: + using value_type = T; + using pointer = T*; + using reference = T&; + using iterator = pointer; + using const_iterator = typename std::add_const::type *; + + constexpr span() noexcept : ptr(nullptr), len(0) {} + constexpr span(std::nullptr_t) noexcept : span() {} + + constexpr span(T* const src_ptr, const std::size_t src_len) noexcept + : ptr(src_ptr), len(src_len) { + } + + /*! + A `span` is implicitly convertible from any type where `get::data(src)` + returns a pointer convertible to T* and `get::size(src)` returns a + `std::size_t`. This constructor is disabled for all other types. + */ + template< + typename C, + typename U = decltype(get::data(std::declval())), + typename S = decltype(get::size(std::declval())), + typename = typename std::enable_if< + // Do not allow proxy iterators, but allow valid cv conversions + std::is_pointer::value && std::is_convertible::value && + // Prevent underflow/overflow on size storage + std::is_same::value + >::type + > + constexpr span(C&& src) noexcept(noexcept(get::data(src), get::size(src))) + : span(get::data(src), get::size(src)) { + } + + constexpr span(span&&) noexcept = default; + constexpr span(span&) noexcept = default; + constexpr span(const span&) noexcept = default; + span& operator=(const span&) noexcept = default; + + constexpr iterator begin() const noexcept { return ptr; } + constexpr const_iterator cbegin() const noexcept { return ptr; } + + constexpr iterator end() const noexcept { return begin() + size(); } + constexpr const_iterator cend() const noexcept { return cbegin() + size(); } + + constexpr bool empty() const noexcept { return size() == 0; } + constexpr pointer data() const noexcept { return ptr; } + constexpr std::size_t size() const noexcept { return len; } + constexpr std::size_t size_bytes() const noexcept { return size() * sizeof(value_type); } + + private: + T* ptr; + std::size_t len; + }; + + //! A `view` is a `span` over immutable data. See `span` for more info. + template + using view = span::type>; + + //! \return `src` aliased as a view of type `T`. + template + view view_cast(const span& src) noexcept + { + using normalized = typename std::remove_cv::type; + static_assert( + std::is_same::value || std::is_same::value, + "only valid aliasing allowed - char or unsigned char target types" + ); + + static_assert(std::is_integral::value, "only integral source types"); + return {reinterpret_cast::type *>(src.data()), src.size_bytes()}; + } + + //! \return `src` aliased as a span of type `T`. + template + span pod_cast(U&& src) noexcept + { + using normalized = typename std::remove_cv::type; + static_assert( + std::is_same::value || std::is_same::value, + "only valid aliasing allowed - char or unsigned char target types" + ); + + using decayed = typename std::decay::type; + static_assert(std::is_pod::value, "only POD types allowed for conversion"); + static_assert(alignof(decayed) == 1, "only types with 1 byte alignment allowed for conversion"); + return {reinterpret_cast(std::addressof(src)), sizeof(decayed)}; + } +} diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h index 530000028..67984b9e4 100644 --- a/contrib/epee/include/string_tools.h +++ b/contrib/epee/include/string_tools.h @@ -45,6 +45,8 @@ #include #include #include +#include "hex.h" +#include "span.h" #include "warnings.h" @@ -114,33 +116,10 @@ namespace string_tools return false; } } - //---------------------------------------------------------------------------- - template - std::basic_string buff_to_hex(const std::basic_string& s) - { - using namespace std; - basic_stringstream hexStream; - hexStream << hex << noshowbase << setw(2); - - for(typename std::basic_string::const_iterator it = s.begin(); it != s.end(); it++) - { - hexStream << "0x"<< static_cast(static_cast(*it)) << " "; - } - return hexStream.str(); - } //---------------------------------------------------------------------------- - template - std::basic_string buff_to_hex_nodelimer(const std::basic_string& s) + inline std::string buff_to_hex_nodelimer(const std::string& src) { - using namespace std; - basic_stringstream hexStream; - hexStream << hex << noshowbase; - - for(typename std::basic_string::const_iterator it = s.begin(); it != s.end(); it++) - { - hexStream << setw(2) << setfill('0') << static_cast(static_cast(*it)); - } - return hexStream.str(); + return to_hex::string(to_byte_span(to_span(src))); } //---------------------------------------------------------------------------- template @@ -559,9 +538,7 @@ POP_WARNINGS std::string pod_to_hex(const t_pod_type& s) { static_assert(std::is_pod::value, "expected pod type"); - std::string buff; - buff.assign(reinterpret_cast(&s), sizeof(s)); - return buff_to_hex_nodelimer(buff); + return to_hex::string(as_byte_span(s)); } //---------------------------------------------------------------------------- template diff --git a/contrib/epee/src/CMakeLists.txt b/contrib/epee/src/CMakeLists.txt index 9db4c46ce..1d5fa0394 100644 --- a/contrib/epee/src/CMakeLists.txt +++ b/contrib/epee/src/CMakeLists.txt @@ -26,7 +26,7 @@ # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -add_library(epee STATIC http_auth.cpp mlog.cpp string_tools.cpp) +add_library(epee STATIC hex.cpp http_auth.cpp mlog.cpp string_tools.cpp) # Build and install libepee if we're building for GUI if (BUILD_GUI_DEPS) if(IOS) diff --git a/contrib/epee/src/hex.cpp b/contrib/epee/src/hex.cpp new file mode 100644 index 000000000..6a698b5a9 --- /dev/null +++ b/contrib/epee/src/hex.cpp @@ -0,0 +1,82 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "hex.h" + +#include +#include +#include +#include + +namespace epee +{ + namespace + { + template + void write_hex(T&& out, const view src) + { + static constexpr const char hex[] = u8"0123456789abcdef"; + static_assert(sizeof(hex) == 17, "bad string size"); + for (const std::uint8_t byte : src) + { + *out = hex[byte >> 4]; + ++out; + *out = hex[byte & 0x0F]; + ++out; + } + } + } + + std::string to_hex::string(const view src) + { + if (std::numeric_limits::max() / 2 < src.size()) + throw std::range_error("hex_view::to_string exceeded maximum size"); + + std::string out{}; + out.resize(src.size() * 2); + buffer_unchecked(std::addressof(out[0]), src); + return out; + } + + void to_hex::buffer(std::ostream& out, const view src) + { + write_hex(std::ostreambuf_iterator{out}, src); + } + + void to_hex::formatted(std::ostream& out, const view src) + { + out.put('<'); + buffer(out, src); + out.put('>'); + } + + void to_hex::buffer_unchecked(char* out, const view src) noexcept + { + return write_hex(out, src); + } +} diff --git a/contrib/epee/src/http_auth.cpp b/contrib/epee/src/http_auth.cpp index 5a1c2142a..30e562700 100644 --- a/contrib/epee/src/http_auth.cpp +++ b/contrib/epee/src/http_auth.cpp @@ -67,6 +67,7 @@ #include #include "crypto/crypto.h" +#include "hex.h" #include "md5_l.h" #include "string_coding.h" @@ -104,25 +105,6 @@ namespace //// Digest Algorithms - template - std::array to_hex(const std::array& digest) noexcept - { - static constexpr const char alphabet[] = u8"0123456789abcdef"; - static_assert(sizeof(alphabet) == 17, "bad alphabet size"); - - // TODO upgrade (improve performance) of to hex in epee string tools - std::array out{{}}; - auto current = out.begin(); - for (const std::uint8_t byte : digest) - { - *current = alphabet[byte >> 4]; - ++current; - *current = alphabet[byte & 0x0F]; - ++current; - } - return out; - } - struct md5_ { static constexpr const boost::string_ref name = ceref(u8"MD5"); @@ -156,7 +138,7 @@ namespace std::array digest{{}}; md5::MD5Final(digest.data(), std::addressof(ctx)); - return to_hex(digest); + return epee::to_hex::array(digest); } }; constexpr const boost::string_ref md5_::name; diff --git a/src/cryptonote_basic/cryptonote_basic.h b/src/cryptonote_basic/cryptonote_basic.h index 15ace2b0b..c4adf1fcb 100644 --- a/src/cryptonote_basic/cryptonote_basic.h +++ b/src/cryptonote_basic/cryptonote_basic.h @@ -44,7 +44,6 @@ #include "serialization/debug_archive.h" #include "serialization/crypto.h" #include "serialization/keyvalue_serialization.h" // eepe named serialization -#include "string_tools.h" #include "cryptonote_config.h" #include "crypto/crypto.h" #include "crypto/hash.h" diff --git a/src/cryptonote_basic/cryptonote_basic_impl.h b/src/cryptonote_basic/cryptonote_basic_impl.h index 4e1468510..186ad85a0 100644 --- a/src/cryptonote_basic/cryptonote_basic_impl.h +++ b/src/cryptonote_basic/cryptonote_basic_impl.h @@ -33,6 +33,7 @@ #include "cryptonote_basic.h" #include "crypto/crypto.h" #include "crypto/hash.h" +#include "hex.h" namespace cryptonote { @@ -123,23 +124,14 @@ namespace cryptonote { bool operator ==(const cryptonote::block& a, const cryptonote::block& b); } -template -std::ostream &print256(std::ostream &o, const T &v) { - return o << "<" << epee::string_tools::pod_to_hex(v) << ">"; -} -template -std::ostream &print64(std::ostream &o, const T &v) { - return o << "<" << epee::string_tools::pod_to_hex(v) << ">"; -} - bool parse_hash256(const std::string str_hash, crypto::hash& hash); namespace crypto { - inline std::ostream &operator <<(std::ostream &o, const crypto::public_key &v) { return print256(o, v); } - inline std::ostream &operator <<(std::ostream &o, const crypto::secret_key &v) { return print256(o, v); } - inline std::ostream &operator <<(std::ostream &o, const crypto::key_derivation &v) { return print256(o, v); } - inline std::ostream &operator <<(std::ostream &o, const crypto::key_image &v) { return print256(o, v); } - inline std::ostream &operator <<(std::ostream &o, const crypto::signature &v) { return print256(o, v); } - inline std::ostream &operator <<(std::ostream &o, const crypto::hash &v) { return print256(o, v); } - inline std::ostream &operator <<(std::ostream &o, const crypto::hash8 &v) { return print64(o, v); } + inline std::ostream &operator <<(std::ostream &o, const crypto::public_key &v) { epee::to_hex::formatted_from_pod(o, v); return o; } + inline std::ostream &operator <<(std::ostream &o, const crypto::secret_key &v) { epee::to_hex::formatted_from_pod(o, v); return o; } + inline std::ostream &operator <<(std::ostream &o, const crypto::key_derivation &v) { epee::to_hex::formatted_from_pod(o, v); return o; } + inline std::ostream &operator <<(std::ostream &o, const crypto::key_image &v) { epee::to_hex::formatted_from_pod(o, v); return o; } + inline std::ostream &operator <<(std::ostream &o, const crypto::signature &v) { epee::to_hex::formatted_from_pod(o, v); return o; } + inline std::ostream &operator <<(std::ostream &o, const crypto::hash &v) { epee::to_hex::formatted_from_pod(o, v); return o; } + inline std::ostream &operator <<(std::ostream &o, const crypto::hash8 &v) { epee::to_hex::formatted_from_pod(o, v); return o; } } diff --git a/src/ringct/rctTypes.h b/src/ringct/rctTypes.h index 0c27745e1..baa99bbcb 100644 --- a/src/ringct/rctTypes.h +++ b/src/ringct/rctTypes.h @@ -47,6 +47,7 @@ extern "C" { #include "crypto/generic-ops.h" #include "crypto/crypto.h" +#include "hex.h" #include "serialization/serialization.h" #include "serialization/debug_archive.h" #include "serialization/binary_archive.h" @@ -443,8 +444,7 @@ namespace cryptonote { static inline bool operator!=(const crypto::secret_key &k0, const rct::key &k1) { return memcmp(&k0, &k1, 32); } } -template std::ostream &print256(std::ostream &o, const T &v); -inline std::ostream &operator <<(std::ostream &o, const rct::key &v) { return print256(o, v); } +inline std::ostream &operator <<(std::ostream &o, const rct::key &v) { epee::to_hex::formatted_from_pod(o, v); return o; } BLOB_SERIALIZER(rct::key); diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 3c0e8067e..da95a97a9 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -36,6 +36,7 @@ set(unit_tests_sources chacha8.cpp checkpoints.cpp command_line.cpp + crypto.cpp decompose_amount_into_digits.cpp dns_resolver.cpp epee_boosted_tcp_server.cpp diff --git a/tests/unit_tests/crypto.cpp b/tests/unit_tests/crypto.cpp new file mode 100644 index 000000000..9febb7709 --- /dev/null +++ b/tests/unit_tests/crypto.cpp @@ -0,0 +1,75 @@ +// Copyright (c) 2014-2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include +#include +#include + +#include "cryptonote_basic/cryptonote_basic_impl.h" + +namespace +{ + static constexpr const std::uint8_t source[] = { + 0x8b, 0x65, 0x59, 0x70, 0x15, 0x37, 0x99, 0xaf, 0x2a, 0xea, 0xdc, 0x9f, 0xf1, 0xad, 0xd0, 0xea, + 0x6c, 0x72, 0x51, 0xd5, 0x41, 0x54, 0xcf, 0xa9, 0x2c, 0x17, 0x3a, 0x0d, 0xd3, 0x9c, 0x1f, 0x94, + 0x6c, 0x72, 0x51, 0xd5, 0x41, 0x54, 0xcf, 0xa9, 0x2c, 0x17, 0x3a, 0x0d, 0xd3, 0x9c, 0x1f, 0x94, + 0x8b, 0x65, 0x59, 0x70, 0x15, 0x37, 0x99, 0xaf, 0x2a, 0xea, 0xdc, 0x9f, 0xf1, 0xad, 0xd0, 0xea + }; + + static constexpr const char expected[] = + "8b655970153799af2aeadc9ff1add0ea6c7251d54154cfa92c173a0dd39c1f94" + "6c7251d54154cfa92c173a0dd39c1f948b655970153799af2aeadc9ff1add0ea"; + + template + bool is_formatted() + { + T value{}; + + static_assert(alignof(T) == 1, "T must have 1 byte alignment"); + static_assert(sizeof(T) <= sizeof(source), "T is too large for source"); + static_assert(sizeof(T) * 2 <= sizeof(expected), "T is too large for destination"); + std::memcpy(std::addressof(value), source, sizeof(T)); + + std::stringstream out; + out << "BEGIN" << value << "END"; + return out.str() == "BEGIN<" + std::string{expected, sizeof(T) * 2} + ">END"; + } +} + +TEST(Crypto, Ostream) +{ + EXPECT_TRUE(is_formatted()); + EXPECT_TRUE(is_formatted()); + EXPECT_TRUE(is_formatted()); + EXPECT_TRUE(is_formatted()); + EXPECT_TRUE(is_formatted()); + EXPECT_TRUE(is_formatted()); + EXPECT_TRUE(is_formatted()); +} diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index 25b2bddd9..e8ddbe3f5 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -27,18 +27,342 @@ // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +#include +#include +#include +#include +#include +#include +#include +#include + #ifdef _WIN32 # include #else # include #endif -#include -#include -#include - +#include "hex.h" +#include "span.h" #include "string_tools.h" +namespace +{ + template + bool can_construct() + { + const unsigned count = + unsigned(std::is_constructible()) + + unsigned(std::is_constructible()) + + unsigned(std::is_convertible()) + + unsigned(std::is_convertible()) + + unsigned(std::is_assignable()) + + unsigned(std::is_assignable()); + EXPECT_TRUE(count == 6 || count == 0) << + "Mismatch on construction results - " << count << " were true"; + return count == 6; + } + + // This is probably stressing the compiler more than the implementation ... + constexpr const epee::span test_string("a string"); + static_assert(!test_string.empty(), "test failure"); + static_assert(test_string.size() == 9, "test failure"); + static_assert(test_string.size_bytes() == 9, "test_failure"); + static_assert(test_string.begin() == test_string.cbegin(), "test failure"); + static_assert(test_string.end() == test_string.cend(), "test failure"); + static_assert(test_string.cend() - test_string.cbegin() == 9, "test failure"); + static_assert(*test_string.cbegin() == 'a', "test failure"); + static_assert(*(test_string.cend() - 2) == 'g', "test failure"); + static_assert( + epee::span(test_string).cbegin() + 3 == test_string.cbegin() + 3, + "test failure" + ); + + static_assert(epee::span().empty(), "test failure"); + static_assert(epee::span(nullptr).empty(), "test failure"); + static_assert(epee::span("foo", 2).size() == 2, "test failure"); + + std::string std_to_hex(const std::vector& source) + { + std::stringstream out; + out << std::hex; + for (const unsigned char byte : source) + { + out << std::setw(2) << std::setfill('0') << int(byte); + } + return out.str(); + } + + std::vector get_all_bytes() + { + std::vector out; + out.resize(256); + boost::range::iota(out, 0); + return out; + } +} + +TEST(Span, Traits) +{ + EXPECT_TRUE((std::is_same::size_type>())); + EXPECT_TRUE((std::is_same::difference_type>())); + EXPECT_TRUE((std::is_same::value_type>())); + EXPECT_TRUE((std::is_same::pointer>())); + EXPECT_TRUE((std::is_same::const_pointer>())); + EXPECT_TRUE((std::is_same::iterator>())); + EXPECT_TRUE((std::is_same::const_iterator>())); + EXPECT_TRUE((std::is_same::reference>())); + EXPECT_TRUE((std::is_same::const_reference>())); + + EXPECT_TRUE((std::is_same::size_type>())); + EXPECT_TRUE((std::is_same::difference_type>())); + EXPECT_TRUE((std::is_same::value_type>())); + EXPECT_TRUE((std::is_same::pointer>())); + EXPECT_TRUE((std::is_same::const_pointer>())); + EXPECT_TRUE((std::is_same::iterator>())); + EXPECT_TRUE((std::is_same::const_iterator>())); + EXPECT_TRUE((std::is_same::reference>())); + EXPECT_TRUE((std::is_same::const_reference>())); +} + +TEST(Span, MutableConstruction) +{ + struct no_conversion{}; + + EXPECT_TRUE(std::is_constructible>()); + EXPECT_TRUE((std::is_constructible, char*, std::size_t>())); + EXPECT_FALSE((std::is_constructible, const char*, std::size_t>())); + EXPECT_FALSE((std::is_constructible, unsigned char*, std::size_t>())); + + EXPECT_TRUE((can_construct, std::nullptr_t>())); + EXPECT_TRUE((can_construct, char(&)[1]>())); + + EXPECT_FALSE((can_construct, std::vector>())); + EXPECT_FALSE((can_construct, std::array>())); + + EXPECT_FALSE((can_construct, std::wstring>())); + EXPECT_FALSE((can_construct, const std::vector>())); + EXPECT_FALSE((can_construct, std::vector>())); + EXPECT_FALSE((can_construct, const std::array>())); + EXPECT_FALSE((can_construct, std::array>())); + EXPECT_FALSE((can_construct, const char[1]>())); + EXPECT_FALSE((can_construct, unsigned char[1]>())); + EXPECT_FALSE((can_construct, epee::span>())); + EXPECT_FALSE((can_construct, epee::span>())); + EXPECT_FALSE((can_construct, no_conversion>())); +} + +TEST(Span, ImmutableConstruction) +{ + struct no_conversion{}; + + EXPECT_TRUE(std::is_constructible>()); + EXPECT_TRUE((std::is_constructible, char*, std::size_t>())); + EXPECT_TRUE((std::is_constructible, const char*, std::size_t>())); + EXPECT_FALSE((std::is_constructible, unsigned char*, std::size_t>())); + + EXPECT_FALSE((can_construct, std::string>())); + EXPECT_FALSE((can_construct, std::vector>())); + EXPECT_FALSE((can_construct, const std::vector>())); + EXPECT_FALSE((can_construct, std::array>())); + EXPECT_FALSE((can_construct, const std::array>())); + + EXPECT_TRUE((can_construct, std::nullptr_t>())); + EXPECT_TRUE((can_construct, char[1]>())); + EXPECT_TRUE((can_construct, const char[1]>())); + EXPECT_TRUE((can_construct, epee::span>())); + + EXPECT_FALSE((can_construct, std::wstring>())); + EXPECT_FALSE((can_construct, std::vector>())); + EXPECT_FALSE((can_construct, std::array>())); + EXPECT_FALSE((can_construct, unsigned char[1]>())); + EXPECT_FALSE((can_construct, epee::span>())); + EXPECT_FALSE((can_construct, no_conversion>())); +} + +TEST(Span, NoExcept) +{ + EXPECT_TRUE(std::is_nothrow_default_constructible>()); + EXPECT_TRUE(std::is_nothrow_move_constructible>()); + EXPECT_TRUE(std::is_nothrow_copy_constructible>()); + EXPECT_TRUE(std::is_move_assignable>()); + EXPECT_TRUE(std::is_copy_assignable>()); + + char data[10]; + epee::span lvalue(data); + const epee::span clvalue(data); + EXPECT_TRUE(noexcept(epee::span())); + EXPECT_TRUE(noexcept(epee::span(nullptr))); + EXPECT_TRUE(noexcept(epee::span(nullptr, 0))); + EXPECT_TRUE(noexcept(epee::span(data))); + EXPECT_TRUE(noexcept(epee::span(lvalue))); + EXPECT_TRUE(noexcept(epee::span(clvalue))); + + // conversion from mutable to immutable not yet implemented + // EXPECT_TRUE(noexcept(epee::span(lvalue))); + // EXPECT_TRUE(noexcept(epee::span(clvalue))); + + EXPECT_TRUE(noexcept(epee::span(epee::span(lvalue)))); + EXPECT_TRUE(noexcept(lvalue = lvalue)); + EXPECT_TRUE(noexcept(lvalue = clvalue)); + EXPECT_TRUE(noexcept(lvalue = epee::span(lvalue))); +} + +TEST(Span, Nullptr) +{ + const auto check_empty = [](epee::span data) + { + EXPECT_TRUE(data.empty()); + EXPECT_EQ(data.cbegin(), data.begin()); + EXPECT_EQ(data.cend(), data.end()); + EXPECT_EQ(data.cend(), data.cbegin()); + EXPECT_EQ(0, data.size()); + EXPECT_EQ(0, data.size_bytes()); + }; + check_empty({}); + check_empty(nullptr); +} + +TEST(Span, Writing) +{ + const int expected[] = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + std::vector source; + + epee::span span; + EXPECT_TRUE(span.empty()); + EXPECT_EQ(0, span.size()); + EXPECT_EQ(0, span.size_bytes()); + + source.resize(15); + span = {source.data(), source.size()}; + EXPECT_FALSE(span.empty()); + EXPECT_EQ(15, span.size()); + EXPECT_EQ(15 * 4, span.size_bytes()); + + boost::range::iota(span, -5); + EXPECT_EQ(span.begin(), span.cbegin()); + EXPECT_EQ(span.end(), span.cend()); + EXPECT_TRUE(boost::range::equal(expected, source)); + EXPECT_TRUE(boost::range::equal(expected, span)); +} + +TEST(Span, ToByteSpan) +{ + const char expected[] = {56, 44, 11, 5}; + EXPECT_TRUE( + boost::range::equal( + std::array{{56, 44, 11, 5}}, + epee::to_byte_span(expected) + ) + ); + EXPECT_TRUE( + boost::range::equal( + std::array{{56, 44, 11, 5}}, + epee::to_byte_span(epee::span{expected}) + ) + ); +} + +TEST(Span, AsByteSpan) +{ + struct some_pod { char value[4]; }; + const some_pod immutable {{ 5, 10, 12, 127 }}; + EXPECT_TRUE( + boost::range::equal( + std::array{{5, 10, 12, 127}}, + epee::as_byte_span(immutable) + ) + ); + EXPECT_TRUE( + boost::range::equal( + std::array{{'a', 'y', 0x00}}, epee::as_byte_span("ay") + ) + ); +} + +TEST(ToHex, String) +{ + EXPECT_TRUE(epee::to_hex::string(nullptr).empty()); + EXPECT_EQ( + std::string{"ffab0100"}, + epee::to_hex::string(epee::as_byte_span("\xff\xab\x01")) + ); + + const std::vector all_bytes = get_all_bytes(); + EXPECT_EQ( + std_to_hex(all_bytes), epee::to_hex::string(epee::to_span(all_bytes)) + ); +} + +TEST(ToHex, Array) +{ + EXPECT_EQ( + (std::array{{'f', 'f', 'a', 'b', '0', '1', '0', '0'}}), + (epee::to_hex::array(std::array{{0xFF, 0xAB, 0x01, 0x00}})) + ); +} + +TEST(ToHex, Ostream) +{ + std::stringstream out; + epee::to_hex::buffer(out, nullptr); + EXPECT_TRUE(out.str().empty()); + + { + const std::uint8_t source[] = {0xff, 0xab, 0x01, 0x00}; + epee::to_hex::buffer(out, source); + } + + std::string expected{"ffab0100"}; + EXPECT_EQ(expected, out.str()); + + const std::vector all_bytes = get_all_bytes(); + + expected.append(std_to_hex(all_bytes)); + epee::to_hex::buffer(out, epee::to_span(all_bytes)); + EXPECT_EQ(expected, out.str()); +} + +TEST(ToHex, Formatted) +{ + std::stringstream out; + std::string expected{"<>"}; + + epee::to_hex::formatted(out, nullptr); + EXPECT_EQ(expected, out.str()); + + expected.append(""); + epee::to_hex::formatted(out, epee::as_byte_span("\xFF\xAB\x01")); + EXPECT_EQ(expected, out.str()); + + const std::vector all_bytes = get_all_bytes(); + + expected.append("<").append(std_to_hex(all_bytes)).append(">"); + epee::to_hex::formatted(out, epee::to_span(all_bytes)); + EXPECT_EQ(expected, out.str()); +} + +TEST(StringTools, BuffToHex) +{ + const std::vector all_bytes = get_all_bytes(); + + EXPECT_EQ( + std_to_hex(all_bytes), + (epee::string_tools::buff_to_hex_nodelimer( + std::string{reinterpret_cast(all_bytes.data()), all_bytes.size()} + )) + ); +} + +TEST(StringTools, PodToHex) +{ + struct some_pod { unsigned char data[4]; }; + EXPECT_EQ( + std::string{"ffab0100"}, + (epee::string_tools::pod_to_hex(some_pod{{0xFF, 0xAB, 0x01, 0x00}})) + ); +} + TEST(StringTools, GetIpString) { EXPECT_EQ( diff --git a/tests/unit_tests/ringct.cpp b/tests/unit_tests/ringct.cpp index 0e2cb3903..af6afa636 100644 --- a/tests/unit_tests/ringct.cpp +++ b/tests/unit_tests/ringct.cpp @@ -32,6 +32,7 @@ #include #include +#include #include "ringct/rctTypes.h" #include "ringct/rctSigs.h" @@ -1048,3 +1049,13 @@ TEST(ringct, reject_gen_non_simple_ver_simple) rct::rctSig sig = make_sample_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, true); ASSERT_FALSE(rct::verRctSimple(sig)); } + +TEST(ringct, key_ostream) +{ + std::stringstream out; + out << "BEGIN" << rct::H << "END"; + EXPECT_EQ( + std::string{"BEGIN<8b655970153799af2aeadc9ff1add0ea6c7251d54154cfa92c173a0dd39c1f94>END"}, + out.str() + ); +} diff --git a/tests/unit_tests/serialization.cpp b/tests/unit_tests/serialization.cpp index 7afc0d60c..6f9fe7d11 100644 --- a/tests/unit_tests/serialization.cpp +++ b/tests/unit_tests/serialization.cpp @@ -1000,8 +1000,6 @@ TEST(Serialization, portability_unsigned_tx) ASSERT_TRUE(tcd.selected_transfers.front() == 2); // tcd.extra ASSERT_TRUE(tcd.extra.size() == 68); - string tcd_extra_str = epee::string_tools::buff_to_hex(string(reinterpret_cast(tcd.extra.data()), tcd.extra.size())); - ASSERT_TRUE(tcd_extra_str == "0x2 0x21 0x0 0xf8 0xd 0xbc 0xfc 0xa2 0x2d 0x84 0x1e 0xa0 0x46 0x18 0x7a 0x5b 0x19 0xea 0x4d 0xd1 0xa2 0x8a 0x58 0xa8 0x72 0x9 0xd5 0xdf 0x2 0x30 0x60 0xac 0x9e 0x48 0x84 0x1 0xb2 0xfd 0x5d 0x4e 0x45 0x8b 0xf1 0x28 0xa0 0xc8 0x30 0xd1 0x35 0x4f 0x47 0xb9 0xed 0xc9 0x82 0x8c 0x83 0x37 0x7d 0xb6 0xb5 0xe5 0x3d 0xff 0x64 0xb0 0xde 0x7f "); // tcd.{unlock_time, use_rct} ASSERT_TRUE(tcd.unlock_time == 0); ASSERT_TRUE(tcd.use_rct); @@ -1157,8 +1155,6 @@ TEST(Serialization, portability_signed_tx) ASSERT_TRUE(tcd.selected_transfers.front() == 2); // ptx.construction_data.extra ASSERT_TRUE(tcd.extra.size() == 68); - string tcd_extra_str = epee::string_tools::buff_to_hex(string(reinterpret_cast(tcd.extra.data()), tcd.extra.size())); - ASSERT_TRUE(tcd_extra_str == "0x2 0x21 0x0 0xf8 0xd 0xbc 0xfc 0xa2 0x2d 0x84 0x1e 0xa0 0x46 0x18 0x7a 0x5b 0x19 0xea 0x4d 0xd1 0xa2 0x8a 0x58 0xa8 0x72 0x9 0xd5 0xdf 0x2 0x30 0x60 0xac 0x9e 0x48 0x84 0x1 0xb2 0xfd 0x5d 0x4e 0x45 0x8b 0xf1 0x28 0xa0 0xc8 0x30 0xd1 0x35 0x4f 0x47 0xb9 0xed 0xc9 0x82 0x8c 0x83 0x37 0x7d 0xb6 0xb5 0xe5 0x3d 0xff 0x64 0xb0 0xde 0x7f "); // ptx.construction_data.{unlock_time, use_rct} ASSERT_TRUE(tcd.unlock_time == 0); ASSERT_TRUE(tcd.use_rct);