| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include <array> |
| 30 | #include <boost/predef/other/endian.h> |
| 31 | #include <boost/endian/conversion.hpp> |
| 32 | #include <boost/range/algorithm/equal.hpp> |
| 33 | #include <boost/range/algorithm_ext/iota.hpp> |
| 34 | #include <boost/range/iterator_range.hpp> |
| 35 | #include <cstdint> |
| 36 | #include <gtest/gtest.h> |
| 37 | #include <iomanip> |
| 38 | #include <iterator> |
| 39 | #include <string> |
| 40 | #include <sstream> |
| 41 | #include <vector> |
| 42 | |
| 43 | #ifndef _WIN32 |
| 44 | # include <arpa/inet.h> |
| 45 | #endif |
| 46 | |
| 47 | #include "boost/archive/portable_binary_iarchive.hpp" |
| 48 | #include "boost/archive/portable_binary_oarchive.hpp" |
| 49 | #include "byte_slice.h" |
| 50 | #include "byte_stream.h" |
| 51 | #include "crypto/crypto.h" |
| 52 | #include "hex.h" |
| 53 | #include "net/net_utils_base.h" |
| 54 | #include "net/local_ip.h" |
| 55 | #include "net/buffer.h" |
| 56 | #include "p2p/net_peerlist_boost_serialization.h" |
| 57 | #include "span.h" |
| 58 | #include "string_tools.h" |
| 59 | #include "storages/parserse_base_utils.h" |
| 60 | |
| 61 | namespace |
| 62 | { |
| 63 | template<typename Destination, typename Source> |
| 64 | bool can_construct() |
| 65 | { |
| 66 | const unsigned count = |
| 67 | unsigned(std::is_constructible<Destination, Source>()) + |
| 68 | unsigned(std::is_constructible<Destination, Source&>()) + |
| 69 | unsigned(std::is_convertible<Source, Destination>()) + |
| 70 | unsigned(std::is_convertible<Source&, Destination>()) + |
| 71 | unsigned(std::is_assignable<Destination, Source>()) + |
| 72 | unsigned(std::is_assignable<Destination, Source&>()); |
| 73 | EXPECT_TRUE(count == 6 || count == 0) << |
| 74 | "Mismatch on construction results - " << count << " were true"; |
| 75 | return count == 6; |
| 76 | } |
| 77 | |
| 78 | // This is probably stressing the compiler more than the implementation ... |
| 79 | constexpr const epee::span<const char> test_string("a string"); |
| 80 | static_assert(!test_string.empty(), "test failure"); |
| 81 | static_assert(test_string.size() == 9, "test failure"); |
| 82 | static_assert(test_string.size_bytes() == 9, "test_failure"); |
| 83 | static_assert(test_string.begin() == test_string.cbegin(), "test failure"); |
| 84 | static_assert(test_string.end() == test_string.cend(), "test failure"); |
| 85 | static_assert(test_string.cend() - test_string.cbegin() == 9, "test failure"); |
| 86 | static_assert(*test_string.cbegin() == 'a', "test failure"); |
| 87 | static_assert(*(test_string.cend() - 2) == 'g', "test failure"); |
| 88 | static_assert( |
| 89 | epee::span<const char>(test_string).cbegin() + 3 == test_string.cbegin() + 3, |
| 90 | "test failure" |
| 91 | ); |
| 92 | |
| 93 | static_assert(epee::span<char>().empty(), "test failure"); |
| 94 | static_assert(epee::span<char>(nullptr).empty(), "test failure"); |
| 95 | static_assert(epee::span<const char>("foo", 2).size() == 2, "test failure"); |
| 96 | |
| 97 | std::string std_to_hex(const std::vector<unsigned char>& source) |
| 98 | { |
| 99 | std::stringstream out; |
| 100 | out << std::hex; |
| 101 | for (const unsigned char byte : source) |
| 102 | { |
| 103 | out << std::setw(2) << std::setfill('0') << int(byte); |
| 104 | } |
| 105 | return out.str(); |
| 106 | } |
| 107 | |
| 108 | std::vector<unsigned char> get_all_bytes() |
| 109 | { |
| 110 | std::vector<unsigned char> out; |
| 111 | out.resize(256); |
| 112 | boost::range::iota(out, 0); |
| 113 | return out; |
| 114 | } |
| 115 | |
| 116 | #define CHECK_EQUAL(lhs, rhs) \ |
| 117 | EXPECT_TRUE( lhs == rhs ); \ |
| 118 | EXPECT_TRUE( rhs == lhs ); \ |
| 119 | EXPECT_FALSE( lhs != rhs ); \ |
| 120 | EXPECT_FALSE( rhs != lhs ); \ |
| 121 | EXPECT_FALSE( lhs < rhs ); \ |
| 122 | EXPECT_FALSE( rhs < lhs ); \ |
| 123 | EXPECT_TRUE( lhs <= rhs ); \ |
| 124 | EXPECT_TRUE( rhs <= lhs ); \ |
| 125 | EXPECT_FALSE( lhs > rhs ); \ |
| 126 | EXPECT_FALSE( rhs > lhs ); \ |
| 127 | EXPECT_TRUE( lhs >= rhs ); \ |
| 128 | EXPECT_TRUE( rhs >= lhs ) |
| 129 | |
| 130 | #define CHECK_LESS(lhs, rhs) \ |
| 131 | EXPECT_FALSE( lhs == rhs ); \ |
| 132 | EXPECT_FALSE( rhs == lhs ); \ |
| 133 | EXPECT_TRUE( lhs != rhs ); \ |
| 134 | EXPECT_TRUE( rhs != lhs ); \ |
| 135 | EXPECT_TRUE( lhs < rhs ); \ |
| 136 | EXPECT_FALSE( rhs < lhs ); \ |
| 137 | EXPECT_TRUE( lhs <= rhs ); \ |
| 138 | EXPECT_FALSE( rhs <= lhs ); \ |
| 139 | EXPECT_FALSE( lhs > rhs ); \ |
| 140 | EXPECT_TRUE( rhs > lhs ); \ |
| 141 | EXPECT_FALSE( lhs >= rhs ); \ |
| 142 | EXPECT_TRUE( rhs >= lhs ) |
| 143 | |
| 144 | #if BOOST_ENDIAN_LITTLE_BYTE |
| 145 | #define CHECK_LESS_ENDIAN(lhs, rhs) CHECK_LESS( rhs , lhs ) |
| 146 | #else |
| 147 | #define CHECK_LESS_ENDIAN(lhs, rhs) CHECK_LESS( lhs , rhs ) |
| 148 | #endif |
| 149 | } |
| 150 | |
| 151 | TEST(Span, Traits) |
| 152 | { |
| 153 | EXPECT_TRUE((std::is_same<std::size_t, typename epee::span<char>::size_type>())); |
| 154 | EXPECT_TRUE((std::is_same<std::ptrdiff_t, typename epee::span<char>::difference_type>())); |
| 155 | EXPECT_TRUE((std::is_same<char, typename epee::span<char>::value_type>())); |
| 156 | EXPECT_TRUE((std::is_same<char*, typename epee::span<char>::pointer>())); |
| 157 | EXPECT_TRUE((std::is_same<const char*, typename epee::span<char>::const_pointer>())); |
| 158 | EXPECT_TRUE((std::is_same<char*, typename epee::span<char>::iterator>())); |
| 159 | EXPECT_TRUE((std::is_same<const char*, typename epee::span<char>::const_iterator>())); |
| 160 | EXPECT_TRUE((std::is_same<char&, typename epee::span<char>::reference>())); |
| 161 | EXPECT_TRUE((std::is_same<const char&, typename epee::span<char>::const_reference>())); |
| 162 | |
| 163 | EXPECT_TRUE((std::is_same<std::size_t, typename epee::span<const char>::size_type>())); |
| 164 | EXPECT_TRUE((std::is_same<std::ptrdiff_t, typename epee::span<const char>::difference_type>())); |
| 165 | EXPECT_TRUE((std::is_same<const char, typename epee::span<const char>::value_type>())); |
| 166 | EXPECT_TRUE((std::is_same<const char*, typename epee::span<const char>::pointer>())); |
| 167 | EXPECT_TRUE((std::is_same<const char*, typename epee::span<const char>::const_pointer>())); |
| 168 | EXPECT_TRUE((std::is_same<const char*, typename epee::span<const char>::iterator>())); |
| 169 | EXPECT_TRUE((std::is_same<const char*, typename epee::span<const char>::const_iterator>())); |
| 170 | EXPECT_TRUE((std::is_same<const char&, typename epee::span<const char>::reference>())); |
| 171 | EXPECT_TRUE((std::is_same<const char&, typename epee::span<const char>::const_reference>())); |
| 172 | } |
| 173 | |
| 174 | TEST(Span, MutableConstruction) |
| 175 | { |
| 176 | struct no_conversion{}; |
| 177 | struct inherited : no_conversion {}; |
| 178 | |
| 179 | EXPECT_TRUE(std::is_constructible<epee::span<char>>()); |
| 180 | EXPECT_TRUE((std::is_constructible<epee::span<char>, char*, std::size_t>())); |
| 181 | EXPECT_FALSE((std::is_constructible<epee::span<char>, const char*, std::size_t>())); |
| 182 | EXPECT_FALSE((std::is_constructible<epee::span<char>, unsigned char*, std::size_t>())); |
| 183 | |
| 184 | EXPECT_TRUE(std::is_constructible<epee::span<no_conversion>>()); |
| 185 | EXPECT_TRUE((std::is_constructible<epee::span<no_conversion>, no_conversion*, std::size_t>())); |
| 186 | EXPECT_FALSE((std::is_constructible<epee::span<no_conversion>, inherited*, std::size_t>())); |
| 187 | |
| 188 | EXPECT_TRUE((can_construct<epee::span<char>, std::nullptr_t>())); |
| 189 | EXPECT_TRUE((can_construct<epee::span<char>, char(&)[1]>())); |
| 190 | |
| 191 | EXPECT_FALSE((can_construct<epee::span<char>, std::vector<char>>())); |
| 192 | EXPECT_FALSE((can_construct<epee::span<char>, std::array<char, 1>>())); |
| 193 | |
| 194 | EXPECT_FALSE((can_construct<epee::span<char>, std::wstring>())); |
| 195 | EXPECT_FALSE((can_construct<epee::span<char>, const std::vector<char>>())); |
| 196 | EXPECT_FALSE((can_construct<epee::span<char>, std::vector<unsigned char>>())); |
| 197 | EXPECT_FALSE((can_construct<epee::span<char>, const std::array<char, 1>>())); |
| 198 | EXPECT_FALSE((can_construct<epee::span<char>, std::array<unsigned char, 1>>())); |
| 199 | EXPECT_FALSE((can_construct<epee::span<char>, const char[1]>())); |
| 200 | EXPECT_FALSE((can_construct<epee::span<char>, unsigned char[1]>())); |
| 201 | EXPECT_FALSE((can_construct<epee::span<char>, epee::span<const char>>())); |
| 202 | EXPECT_FALSE((can_construct<epee::span<char>, epee::span<unsigned char>>())); |
| 203 | EXPECT_FALSE((can_construct<epee::span<char>, no_conversion>())); |
| 204 | } |
| 205 | |
| 206 | TEST(Span, ImmutableConstruction) |
| 207 | { |
| 208 | struct no_conversion{}; |
| 209 | struct inherited : no_conversion {}; |
| 210 | |
| 211 | EXPECT_TRUE(std::is_constructible<epee::span<const char>>()); |
| 212 | EXPECT_TRUE((std::is_constructible<epee::span<const char>, char*, std::size_t>())); |
| 213 | EXPECT_TRUE((std::is_constructible<epee::span<const char>, const char*, std::size_t>())); |
| 214 | EXPECT_FALSE((std::is_constructible<epee::span<const char>, unsigned char*, std::size_t>())); |
| 215 | |
| 216 | EXPECT_TRUE(std::is_constructible<epee::span<const no_conversion>>()); |
| 217 | EXPECT_TRUE((std::is_constructible<epee::span<const no_conversion>, const no_conversion*, std::size_t>())); |
| 218 | EXPECT_TRUE((std::is_constructible<epee::span<const no_conversion>, no_conversion*, std::size_t>())); |
| 219 | EXPECT_FALSE((std::is_constructible<epee::span<const no_conversion>, const inherited*, std::size_t>())); |
| 220 | EXPECT_FALSE((std::is_constructible<epee::span<const no_conversion>, inherited*, std::size_t>())); |
| 221 | |
| 222 | EXPECT_FALSE((can_construct<epee::span<const char>, std::string>())); |
| 223 | EXPECT_FALSE((can_construct<epee::span<const char>, std::vector<char>>())); |
| 224 | EXPECT_FALSE((can_construct<epee::span<const char>, const std::vector<char>>())); |
| 225 | EXPECT_FALSE((can_construct<epee::span<const char>, std::array<char, 1>>())); |
| 226 | EXPECT_FALSE((can_construct<epee::span<const char>, const std::array<char, 1>>())); |
| 227 | |
| 228 | EXPECT_TRUE((can_construct<epee::span<const char>, std::nullptr_t>())); |
| 229 | EXPECT_TRUE((can_construct<epee::span<const char>, char[1]>())); |
| 230 | EXPECT_TRUE((can_construct<epee::span<const char>, const char[1]>())); |
| 231 | EXPECT_TRUE((can_construct<epee::span<const char>, epee::span<const char>>())); |
| 232 | |
| 233 | EXPECT_FALSE((can_construct<epee::span<const char>, std::wstring>())); |
| 234 | EXPECT_FALSE((can_construct<epee::span<const char>, std::vector<unsigned char>>())); |
| 235 | EXPECT_FALSE((can_construct<epee::span<const char>, std::array<unsigned char, 1>>())); |
| 236 | EXPECT_FALSE((can_construct<epee::span<const char>, unsigned char[1]>())); |
| 237 | EXPECT_FALSE((can_construct<epee::span<const char>, epee::span<unsigned char>>())); |
| 238 | EXPECT_FALSE((can_construct<epee::span<const char>, no_conversion>())); |
| 239 | } |
| 240 | |
| 241 | TEST(Span, NoExcept) |
| 242 | { |
| 243 | EXPECT_TRUE(std::is_nothrow_default_constructible<epee::span<char>>()); |
| 244 | EXPECT_TRUE(std::is_nothrow_move_constructible<epee::span<char>>()); |
| 245 | EXPECT_TRUE(std::is_nothrow_copy_constructible<epee::span<char>>()); |
| 246 | EXPECT_TRUE(std::is_move_assignable<epee::span<char>>()); |
| 247 | EXPECT_TRUE(std::is_copy_assignable<epee::span<char>>()); |
| 248 | |
| 249 | char data[10]; |
| 250 | epee::span<char> lvalue(data); |
| 251 | const epee::span<char> clvalue(data); |
| 252 | EXPECT_TRUE(noexcept(epee::span<char>())); |
| 253 | EXPECT_TRUE(noexcept(epee::span<char>(nullptr))); |
| 254 | EXPECT_TRUE(noexcept(epee::span<char>(data))); |
| 255 | EXPECT_TRUE(noexcept(epee::span<char>(lvalue))); |
| 256 | EXPECT_TRUE(noexcept(epee::span<char>(clvalue))); |
| 257 | |
| 258 | // conversion from mutable to immutable not yet implemented |
| 259 | // EXPECT_TRUE(noexcept(epee::span<const char>(lvalue))); |
| 260 | // EXPECT_TRUE(noexcept(epee::span<const char>(clvalue))); |
| 261 | |
| 262 | EXPECT_TRUE(noexcept(epee::span<char>(epee::span<char>(lvalue)))); |
| 263 | EXPECT_TRUE(noexcept(lvalue = lvalue)); |
| 264 | EXPECT_TRUE(noexcept(lvalue = clvalue)); |
| 265 | EXPECT_TRUE(noexcept(lvalue = epee::span<char>(lvalue))); |
| 266 | } |
| 267 | |
| 268 | TEST(Span, Nullptr) |
| 269 | { |
| 270 | const auto check_empty = [](epee::span<const char> data) |
| 271 | { |
| 272 | EXPECT_TRUE(data.empty()); |
| 273 | EXPECT_EQ(data.cbegin(), data.begin()); |
| 274 | EXPECT_EQ(data.cend(), data.end()); |
| 275 | EXPECT_EQ(data.cend(), data.cbegin()); |
| 276 | EXPECT_EQ(0, data.size()); |
| 277 | EXPECT_EQ(0, data.size_bytes()); |
| 278 | }; |
| 279 | check_empty({}); |
| 280 | check_empty(nullptr); |
| 281 | } |
| 282 | |
| 283 | TEST(Span, Writing) |
| 284 | { |
| 285 | const int expected[] = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 286 | std::vector<int> source; |
| 287 | |
| 288 | epee::span<int> span; |
| 289 | EXPECT_TRUE(span.empty()); |
| 290 | EXPECT_EQ(0, span.size()); |
| 291 | EXPECT_EQ(0, span.size_bytes()); |
| 292 | |
| 293 | source.resize(15); |
| 294 | span = {source.data(), source.size()}; |
| 295 | EXPECT_FALSE(span.empty()); |
| 296 | EXPECT_EQ(15, span.size()); |
| 297 | EXPECT_EQ(15 * 4, span.size_bytes()); |
| 298 | |
| 299 | boost::range::iota(span, -5); |
| 300 | EXPECT_EQ(span.begin(), span.cbegin()); |
| 301 | EXPECT_EQ(span.end(), span.cend()); |
| 302 | EXPECT_TRUE(boost::range::equal(expected, source)); |
| 303 | EXPECT_TRUE(boost::range::equal(expected, span)); |
| 304 | } |
| 305 | |
| 306 | TEST(Span, RemovePrefix) |
| 307 | { |
| 308 | const std::array<unsigned, 4> expected{0, 1, 2, 3}; |
| 309 | auto span = epee::to_span(expected); |
| 310 | |
| 311 | EXPECT_EQ(expected.begin(), span.begin()); |
| 312 | EXPECT_EQ(expected.end(), span.end()); |
| 313 | |
| 314 | EXPECT_EQ(2u, span.remove_prefix(2)); |
| 315 | EXPECT_EQ(expected.begin() + 2, span.begin()); |
| 316 | EXPECT_EQ(expected.end(), span.end()); |
| 317 | |
| 318 | EXPECT_EQ(2u, span.remove_prefix(3)); |
| 319 | EXPECT_EQ(span.begin(), span.end()); |
| 320 | EXPECT_EQ(expected.end(), span.begin()); |
| 321 | |
| 322 | EXPECT_EQ(0u, span.remove_prefix(100)); |
| 323 | } |
| 324 | |
| 325 | TEST(Span, ToByteSpan) |
| 326 | { |
| 327 | const char expected[] = {56, 44, 11, 5}; |
| 328 | EXPECT_TRUE( |
| 329 | boost::range::equal( |
| 330 | std::array<std::uint8_t, 4>{{56, 44, 11, 5}}, |
| 331 | epee::to_byte_span<char>(expected) |
| 332 | ) |
| 333 | ); |
| 334 | EXPECT_TRUE( |
| 335 | boost::range::equal( |
| 336 | std::array<char, 4>{{56, 44, 11, 5}}, |
| 337 | epee::to_byte_span(epee::span<const char>{expected}) |
| 338 | ) |
| 339 | ); |
| 340 | } |
| 341 | |
| 342 | TEST(Span, AsByteSpan) |
| 343 | { |
| 344 | struct some_pod { char value[4]; }; |
| 345 | const some_pod immutable {{ 5, 10, 12, 127 }}; |
| 346 | EXPECT_TRUE( |
| 347 | boost::range::equal( |
| 348 | std::array<unsigned char, 4>{{5, 10, 12, 127}}, |
| 349 | epee::as_byte_span(immutable) |
| 350 | ) |
| 351 | ); |
| 352 | EXPECT_TRUE( |
| 353 | boost::range::equal( |
| 354 | std::array<std::uint8_t, 3>{{'a', 'y', 0x00}}, epee::as_byte_span("ay") |
| 355 | ) |
| 356 | ); |
| 357 | } |
| 358 | |
| 359 | TEST(Span, AsMutByteSpan) |
| 360 | { |
| 361 | struct some_pod { char value[4]; }; |
| 362 | some_pod actual {}; |
| 363 | |
| 364 | auto span = epee::as_mut_byte_span(actual); |
| 365 | boost::range::iota(span, 1); |
| 366 | EXPECT_TRUE( |
| 367 | boost::range::equal( |
| 368 | std::array<unsigned char, 4>{{1, 2, 3, 4}}, actual.value |
| 369 | ) |
| 370 | ); |
| 371 | } |
| 372 | |
| 373 | TEST(Span, ToMutSpan) |
| 374 | { |
| 375 | std::vector<unsigned> mut; |
| 376 | mut.resize(4); |
| 377 | |
| 378 | auto span = epee::to_mut_span(mut); |
| 379 | boost::range::iota(span, 1); |
| 380 | EXPECT_EQ((std::vector<unsigned>{1, 2, 3, 4}), mut); |
| 381 | } |
| 382 | |
| 383 | TEST(ByteSlice, Construction) |
| 384 | { |
| 385 | EXPECT_TRUE(std::is_default_constructible<epee::byte_slice>()); |
| 386 | EXPECT_TRUE(std::is_move_constructible<epee::byte_slice>()); |
| 387 | EXPECT_FALSE(std::is_copy_constructible<epee::byte_slice>()); |
| 388 | EXPECT_TRUE(std::is_move_assignable<epee::byte_slice>()); |
| 389 | EXPECT_FALSE(std::is_copy_assignable<epee::byte_slice>()); |
| 390 | } |
| 391 | |
| 392 | TEST(ByteSlice, DataReturnedMatches) |
| 393 | { |
| 394 | for (int i = 64; i > 0; i--) |
| 395 | { |
| 396 | std::string sso_string(i, 'a'); |
| 397 | std::string original = sso_string; |
| 398 | epee::byte_slice slice{std::move(sso_string)}; |
| 399 | |
| 400 | EXPECT_EQ(slice.size(), original.size()); |
| 401 | EXPECT_EQ(memcmp(slice.data(), original.data(), original.size()), 0); |
| 402 | } |
| 403 | |
| 404 | for (int i = 64; i > 0; i--) |
| 405 | { |
| 406 | std::vector<uint8_t> sso_vector(i, 'a'); |
| 407 | std::vector<uint8_t> original = sso_vector; |
| 408 | epee::byte_slice slice{std::move(sso_vector)}; |
| 409 | |
| 410 | EXPECT_EQ(slice.size(), original.size()); |
| 411 | EXPECT_EQ(memcmp(slice.data(), original.data(), original.size()), 0); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | TEST(ByteSlice, NoExcept) |
| 416 | { |
| 417 | EXPECT_TRUE(std::is_nothrow_default_constructible<epee::byte_slice>()); |
| 418 | EXPECT_TRUE(std::is_nothrow_move_constructible<epee::byte_slice>()); |
| 419 | EXPECT_TRUE(std::is_nothrow_move_assignable<epee::byte_slice>()); |
| 420 | |
| 421 | epee::byte_slice lvalue{}; |
| 422 | const epee::byte_slice clvalue{}; |
| 423 | |
| 424 | EXPECT_TRUE(noexcept(lvalue.clone())); |
| 425 | EXPECT_TRUE(noexcept(clvalue.clone())); |
| 426 | |
| 427 | EXPECT_TRUE(noexcept(lvalue.begin())); |
| 428 | EXPECT_TRUE(noexcept(clvalue.begin())); |
| 429 | EXPECT_TRUE(noexcept(lvalue.end())); |
| 430 | EXPECT_TRUE(noexcept(clvalue.end())); |
| 431 | |
| 432 | EXPECT_TRUE(noexcept(lvalue.cbegin())); |
| 433 | EXPECT_TRUE(noexcept(clvalue.cbegin())); |
| 434 | EXPECT_TRUE(noexcept(lvalue.cend())); |
| 435 | EXPECT_TRUE(noexcept(clvalue.cend())); |
| 436 | |
| 437 | EXPECT_TRUE(noexcept(lvalue.empty())); |
| 438 | EXPECT_TRUE(noexcept(clvalue.empty())); |
| 439 | |
| 440 | EXPECT_TRUE(noexcept(lvalue.data())); |
| 441 | EXPECT_TRUE(noexcept(clvalue.data())); |
| 442 | EXPECT_TRUE(noexcept(lvalue.size())); |
| 443 | EXPECT_TRUE(noexcept(clvalue.size())); |
| 444 | |
| 445 | EXPECT_TRUE(noexcept(lvalue.remove_prefix(0))); |
| 446 | EXPECT_TRUE(noexcept(lvalue.take_slice(0))); |
| 447 | } |
| 448 | |
| 449 | TEST(ByteSlice, Empty) |
| 450 | { |
| 451 | epee::byte_slice slice{}; |
| 452 | |
| 453 | EXPECT_EQ(slice.begin(), slice.end()); |
| 454 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 455 | EXPECT_EQ(slice.end(), slice.cend()); |
| 456 | |
| 457 | EXPECT_TRUE(slice.empty()); |
| 458 | EXPECT_EQ(0u, slice.size()); |
| 459 | EXPECT_EQ(slice.begin(), slice.data()); |
| 460 | |
| 461 | EXPECT_EQ(0u, slice.get_slice(0, 0).size()); |
| 462 | EXPECT_THROW(slice.get_slice(0, 1), std::out_of_range); |
| 463 | EXPECT_EQ(0u, slice.remove_prefix(1)); |
| 464 | EXPECT_EQ(0u, slice.take_slice(1).size()); |
| 465 | } |
| 466 | |
| 467 | TEST(ByteSlice, CopySpans) |
| 468 | { |
| 469 | const epee::span<const std::uint8_t> part1 = epee::as_byte_span("this is part1"); |
| 470 | const epee::span<const std::uint8_t> part2 = epee::as_byte_span("then part2"); |
| 471 | const epee::span<const std::uint8_t> part3 = epee::as_byte_span("finally part3"); |
| 472 | |
| 473 | const epee::byte_slice slice{part1, part2, part3}; |
| 474 | |
| 475 | EXPECT_NE(nullptr, slice.begin()); |
| 476 | EXPECT_NE(nullptr, slice.end()); |
| 477 | EXPECT_NE(slice.begin(), slice.end()); |
| 478 | EXPECT_NE(slice.cbegin(), slice.cend()); |
| 479 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 480 | EXPECT_EQ(slice.end(), slice.cend()); |
| 481 | ASSERT_EQ(slice.size(), std::size_t(slice.end() - slice.begin())); |
| 482 | |
| 483 | EXPECT_FALSE(slice.empty()); |
| 484 | EXPECT_EQ(slice.begin(), slice.data()); |
| 485 | ASSERT_EQ(part1.size() + part2.size() + part3.size(), slice.size()); |
| 486 | EXPECT_TRUE( |
| 487 | boost::range::equal( |
| 488 | part1, boost::make_iterator_range(slice.begin(), slice.begin() + part1.size()) |
| 489 | ) |
| 490 | ); |
| 491 | EXPECT_TRUE( |
| 492 | boost::range::equal( |
| 493 | part2, boost::make_iterator_range(slice.begin() + part1.size(), slice.end() - part3.size()) |
| 494 | ) |
| 495 | ); |
| 496 | EXPECT_TRUE( |
| 497 | boost::range::equal( |
| 498 | part3, boost::make_iterator_range(slice.end() - part3.size(), slice.end()) |
| 499 | ) |
| 500 | ); |
| 501 | } |
| 502 | |
| 503 | TEST(ByteSlice, AdaptString) |
| 504 | { |
| 505 | static constexpr const char base_string[] = "this is an example message"; |
| 506 | std::string adapted = base_string; |
| 507 | |
| 508 | const epee::span<const uint8_t> original = epee::to_byte_span(epee::to_span(adapted)); |
| 509 | const epee::byte_slice slice{std::move(adapted)}; |
| 510 | |
| 511 | EXPECT_EQ(original.begin(), slice.begin()); |
| 512 | EXPECT_EQ(original.cbegin(), slice.cbegin()); |
| 513 | EXPECT_EQ(original.end(), slice.end()); |
| 514 | EXPECT_EQ(original.cend(), slice.cend()); |
| 515 | |
| 516 | EXPECT_FALSE(slice.empty()); |
| 517 | EXPECT_EQ(original.data(), slice.data()); |
| 518 | EXPECT_EQ(original.size(), slice.size()); |
| 519 | EXPECT_TRUE(boost::range::equal(boost::string_ref{base_string}, slice)); |
| 520 | } |
| 521 | |
| 522 | TEST(ByteSlice, EmptyAdaptString) |
| 523 | { |
| 524 | epee::byte_slice slice{std::string{}}; |
| 525 | |
| 526 | EXPECT_EQ(slice.begin(), slice.end()); |
| 527 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 528 | EXPECT_EQ(slice.end(), slice.cend()); |
| 529 | |
| 530 | EXPECT_TRUE(slice.empty()); |
| 531 | EXPECT_EQ(0u, slice.size()); |
| 532 | EXPECT_EQ(slice.begin(), slice.data()); |
| 533 | |
| 534 | EXPECT_EQ(0u, slice.get_slice(0, 0).size()); |
| 535 | EXPECT_THROW(slice.get_slice(0, 1), std::out_of_range); |
| 536 | EXPECT_EQ(0u, slice.remove_prefix(1)); |
| 537 | EXPECT_EQ(0u, slice.take_slice(1).size()); |
| 538 | } |
| 539 | |
| 540 | TEST(ByteSlice, AdaptVector) |
| 541 | { |
| 542 | static constexpr const char base_string[] = "this is an example message"; |
| 543 | std::vector<std::uint8_t> adapted(sizeof(base_string)); |
| 544 | |
| 545 | ASSERT_EQ(sizeof(base_string), adapted.size()); |
| 546 | std::memcpy(adapted.data(), base_string, sizeof(base_string)); |
| 547 | |
| 548 | const epee::span<const uint8_t> original = epee::to_span(adapted); |
| 549 | const epee::byte_slice slice{std::move(adapted)}; |
| 550 | |
| 551 | EXPECT_EQ(sizeof(base_string), original.size()); |
| 552 | |
| 553 | EXPECT_EQ(original.begin(), slice.begin()); |
| 554 | EXPECT_EQ(original.cbegin(), slice.cbegin()); |
| 555 | EXPECT_EQ(original.end(), slice.end()); |
| 556 | EXPECT_EQ(original.cend(), slice.cend()); |
| 557 | |
| 558 | EXPECT_FALSE(slice.empty()); |
| 559 | EXPECT_EQ(original.data(), slice.data()); |
| 560 | EXPECT_EQ(original.size(), slice.size()); |
| 561 | EXPECT_TRUE(boost::range::equal(base_string, slice)); |
| 562 | } |
| 563 | |
| 564 | TEST(ByteSlice, EmptyAdaptVector) |
| 565 | { |
| 566 | epee::byte_slice slice{std::vector<std::uint8_t>{}}; |
| 567 | |
| 568 | EXPECT_EQ(slice.begin(), slice.end()); |
| 569 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 570 | EXPECT_EQ(slice.end(), slice.cend()); |
| 571 | |
| 572 | EXPECT_TRUE(slice.empty()); |
| 573 | EXPECT_EQ(0u, slice.size()); |
| 574 | EXPECT_EQ(slice.begin(), slice.data()); |
| 575 | |
| 576 | EXPECT_EQ(0u, slice.get_slice(0, 0).size()); |
| 577 | EXPECT_THROW(slice.get_slice(0, 1), std::out_of_range); |
| 578 | EXPECT_EQ(0u, slice.remove_prefix(1)); |
| 579 | EXPECT_EQ(0u, slice.take_slice(1).size()); |
| 580 | } |
| 581 | |
| 582 | TEST(ByteSlice, Move) |
| 583 | { |
| 584 | static constexpr const char base_string[] = "another example message"; |
| 585 | |
| 586 | epee::byte_slice slice{epee::as_byte_span(base_string)}; |
| 587 | EXPECT_TRUE(boost::range::equal(base_string, slice)); |
| 588 | |
| 589 | const epee::span<const std::uint8_t> original = epee::to_span(slice); |
| 590 | epee::byte_slice moved{std::move(slice)}; |
| 591 | EXPECT_TRUE(boost::range::equal(base_string, moved)); |
| 592 | |
| 593 | EXPECT_EQ(slice.begin(), slice.end()); |
| 594 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 595 | EXPECT_EQ(slice.end(), slice.cend()); |
| 596 | |
| 597 | EXPECT_EQ(original.begin(), moved.begin()); |
| 598 | EXPECT_EQ(moved.begin(), moved.cbegin()); |
| 599 | EXPECT_EQ(original.end(), moved.end()); |
| 600 | EXPECT_EQ(moved.end(), moved.cend()); |
| 601 | |
| 602 | EXPECT_TRUE(slice.empty()); |
| 603 | EXPECT_EQ(slice.begin(), slice.data()); |
| 604 | EXPECT_EQ(0u, slice.size()); |
| 605 | |
| 606 | EXPECT_FALSE(moved.empty()); |
| 607 | EXPECT_EQ(moved.begin(), moved.data()); |
| 608 | EXPECT_EQ(original.size(), moved.size()); |
| 609 | |
| 610 | slice = std::move(moved); |
| 611 | EXPECT_TRUE(boost::range::equal(base_string, slice)); |
| 612 | |
| 613 | EXPECT_EQ(original.begin(), slice.begin()); |
| 614 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 615 | EXPECT_EQ(original.end(), slice.end()); |
| 616 | EXPECT_EQ(slice.end(), slice.cend()); |
| 617 | |
| 618 | EXPECT_FALSE(slice.empty()); |
| 619 | EXPECT_EQ(slice.begin(), slice.data()); |
| 620 | EXPECT_EQ(original.size(), slice.size()); |
| 621 | |
| 622 | EXPECT_TRUE(moved.empty()); |
| 623 | EXPECT_EQ(moved.begin(), moved.data()); |
| 624 | EXPECT_EQ(0u, moved.size()); |
| 625 | } |
| 626 | |
| 627 | TEST(ByteSlice, Clone) |
| 628 | { |
| 629 | static constexpr const char base_string[] = "another example message"; |
| 630 | |
| 631 | const epee::byte_slice slice{epee::as_byte_span(base_string)}; |
| 632 | EXPECT_TRUE(boost::range::equal(base_string, slice)); |
| 633 | |
| 634 | const epee::byte_slice clone{slice.clone()}; |
| 635 | EXPECT_TRUE(boost::range::equal(base_string, clone)); |
| 636 | |
| 637 | EXPECT_EQ(slice.begin(), clone.begin()); |
| 638 | EXPECT_EQ(slice.cbegin(), clone.cbegin()); |
| 639 | EXPECT_EQ(slice.end(), clone.end()); |
| 640 | EXPECT_EQ(slice.cend(), clone.cend()); |
| 641 | |
| 642 | EXPECT_FALSE(slice.empty()); |
| 643 | EXPECT_FALSE(clone.empty()); |
| 644 | EXPECT_EQ(slice.cbegin(), slice.data()); |
| 645 | EXPECT_EQ(slice.data(), clone.data()); |
| 646 | EXPECT_EQ(sizeof(base_string), slice.size()); |
| 647 | EXPECT_EQ(slice.size(), clone.size()); |
| 648 | } |
| 649 | |
| 650 | TEST(ByteSlice, RemovePrefix) |
| 651 | { |
| 652 | static constexpr const char base_string[] = "another example message"; |
| 653 | static constexpr std::size_t remove_size = sizeof("another"); |
| 654 | static constexpr std::size_t remaining = sizeof(base_string) - remove_size; |
| 655 | |
| 656 | epee::byte_slice slice{epee::as_byte_span(base_string)}; |
| 657 | EXPECT_TRUE(boost::range::equal(base_string, slice)); |
| 658 | |
| 659 | const epee::span<const std::uint8_t> original = epee::to_span(slice); |
| 660 | EXPECT_EQ(remove_size, slice.remove_prefix(remove_size)); |
| 661 | |
| 662 | EXPECT_EQ(original.begin() + remove_size, slice.begin()); |
| 663 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 664 | EXPECT_EQ(original.end(), slice.end()); |
| 665 | EXPECT_EQ(slice.end(), slice.cend()); |
| 666 | |
| 667 | EXPECT_FALSE(slice.empty()); |
| 668 | EXPECT_EQ(slice.cbegin(), slice.data()); |
| 669 | EXPECT_EQ(remaining, slice.size()); |
| 670 | |
| 671 | // touch original pointers to check "free" status |
| 672 | EXPECT_TRUE(boost::range::equal(base_string, original)); |
| 673 | |
| 674 | EXPECT_EQ(remaining, slice.remove_prefix(remaining + 1)); |
| 675 | |
| 676 | EXPECT_EQ(slice.begin(), slice.end()); |
| 677 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 678 | EXPECT_EQ(slice.end(), slice.cend()); |
| 679 | |
| 680 | EXPECT_TRUE(slice.empty()); |
| 681 | EXPECT_EQ(slice.cbegin(), slice.data()); |
| 682 | EXPECT_EQ(0, slice.size()); |
| 683 | } |
| 684 | |
| 685 | TEST(ByteSlice, TakeSlice) |
| 686 | { |
| 687 | static constexpr const char base_string[] = "another example message"; |
| 688 | static constexpr std::size_t remove_size = sizeof("another"); |
| 689 | static constexpr std::size_t remaining = sizeof(base_string) - remove_size; |
| 690 | |
| 691 | epee::byte_slice slice{epee::as_byte_span(base_string)}; |
| 692 | EXPECT_TRUE(boost::range::equal(base_string, slice)); |
| 693 | |
| 694 | const epee::span<const std::uint8_t> original = epee::to_span(slice); |
| 695 | const epee::byte_slice empty_slice = slice.take_slice(0); |
| 696 | EXPECT_EQ(original.begin(), slice.begin()); |
| 697 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 698 | EXPECT_EQ(original.end(), slice.end()); |
| 699 | EXPECT_EQ(slice.end(), slice.cend()); |
| 700 | |
| 701 | EXPECT_EQ(nullptr, empty_slice.begin()); |
| 702 | EXPECT_EQ(nullptr, empty_slice.cbegin()); |
| 703 | EXPECT_EQ(nullptr, empty_slice.end()); |
| 704 | EXPECT_EQ(nullptr, empty_slice.cend()); |
| 705 | EXPECT_EQ(nullptr, empty_slice.data()); |
| 706 | EXPECT_TRUE(empty_slice.empty()); |
| 707 | EXPECT_EQ(0u, empty_slice.size()); |
| 708 | |
| 709 | EXPECT_FALSE(slice.empty()); |
| 710 | EXPECT_EQ(slice.cbegin(), slice.data()); |
| 711 | |
| 712 | const epee::byte_slice slice2 = slice.take_slice(remove_size); |
| 713 | |
| 714 | EXPECT_EQ(original.begin() + remove_size, slice.begin()); |
| 715 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 716 | EXPECT_EQ(original.end(), slice.end()); |
| 717 | EXPECT_EQ(slice.end(), slice.cend()); |
| 718 | |
| 719 | EXPECT_EQ(original.begin(), slice2.begin()); |
| 720 | EXPECT_EQ(slice2.begin(), slice2.cbegin()); |
| 721 | EXPECT_EQ(original.begin() + remove_size, slice2.end()); |
| 722 | EXPECT_EQ(slice2.end(), slice2.cend()); |
| 723 | |
| 724 | EXPECT_FALSE(slice.empty()); |
| 725 | EXPECT_EQ(slice.cbegin(), slice.data()); |
| 726 | EXPECT_EQ(remaining, slice.size()); |
| 727 | |
| 728 | EXPECT_FALSE(slice2.empty()); |
| 729 | EXPECT_EQ(slice2.cbegin(), slice2.data()); |
| 730 | EXPECT_EQ(remove_size, slice2.size()); |
| 731 | |
| 732 | // touch original pointers to check "free" status |
| 733 | EXPECT_TRUE(boost::range::equal(base_string, original)); |
| 734 | |
| 735 | const epee::byte_slice slice3 = slice.take_slice(remaining + 1); |
| 736 | |
| 737 | EXPECT_EQ(slice.begin(), slice.end()); |
| 738 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 739 | EXPECT_EQ(slice.end(), slice.cend()); |
| 740 | |
| 741 | EXPECT_EQ(original.begin(), slice2.begin()); |
| 742 | EXPECT_EQ(slice2.begin(), slice2.cbegin()); |
| 743 | EXPECT_EQ(original.begin() + remove_size, slice2.end()); |
| 744 | EXPECT_EQ(slice2.end(), slice2.cend()); |
| 745 | |
| 746 | EXPECT_EQ(slice2.end(), slice3.begin()); |
| 747 | EXPECT_EQ(slice3.begin(), slice3.cbegin()); |
| 748 | EXPECT_EQ(original.end(), slice3.end()); |
| 749 | EXPECT_EQ(slice3.end(), slice3.cend()); |
| 750 | |
| 751 | EXPECT_TRUE(slice.empty()); |
| 752 | EXPECT_EQ(slice.cbegin(), slice.data()); |
| 753 | EXPECT_EQ(0, slice.size()); |
| 754 | |
| 755 | EXPECT_FALSE(slice2.empty()); |
| 756 | EXPECT_EQ(slice2.cbegin(), slice2.data()); |
| 757 | EXPECT_EQ(remove_size, slice2.size()); |
| 758 | |
| 759 | EXPECT_FALSE(slice3.empty()); |
| 760 | EXPECT_EQ(slice3.cbegin(), slice3.data()); |
| 761 | EXPECT_EQ(remaining, slice3.size()); |
| 762 | |
| 763 | // touch original pointers to check "free" status |
| 764 | slice = nullptr; |
| 765 | EXPECT_TRUE(boost::range::equal(base_string, original)); |
| 766 | } |
| 767 | |
| 768 | TEST(ByteSlice, GetSlice) |
| 769 | { |
| 770 | static constexpr const char base_string[] = "another example message"; |
| 771 | static constexpr std::size_t get_size = sizeof("another"); |
| 772 | static constexpr std::size_t get2_size = sizeof(base_string) - get_size; |
| 773 | |
| 774 | epee::span<const std::uint8_t> original{}; |
| 775 | epee::byte_slice slice2{}; |
| 776 | epee::byte_slice slice3{}; |
| 777 | |
| 778 | // make sure get_slice increments ref count |
| 779 | { |
| 780 | const epee::byte_slice slice{epee::as_byte_span(base_string)}; |
| 781 | EXPECT_TRUE(boost::range::equal(base_string, slice)); |
| 782 | |
| 783 | original = epee::to_span(slice); |
| 784 | slice2 = slice.get_slice(0, get_size); |
| 785 | |
| 786 | EXPECT_EQ(original.begin(), slice.begin()); |
| 787 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 788 | EXPECT_EQ(original.end(), slice.end()); |
| 789 | EXPECT_EQ(slice.end(), slice.cend()); |
| 790 | |
| 791 | EXPECT_EQ(original.begin(), slice2.begin()); |
| 792 | EXPECT_EQ(slice2.begin(), slice2.cbegin()); |
| 793 | EXPECT_EQ(original.begin() + get_size, slice2.end()); |
| 794 | EXPECT_EQ(slice2.end(), slice2.cend()); |
| 795 | |
| 796 | EXPECT_FALSE(slice.empty()); |
| 797 | EXPECT_EQ(slice.cbegin(), slice.data()); |
| 798 | EXPECT_EQ(original.size(), slice.size()); |
| 799 | |
| 800 | EXPECT_FALSE(slice2.empty()); |
| 801 | EXPECT_EQ(slice2.cbegin(), slice2.data()); |
| 802 | EXPECT_EQ(get_size, slice2.size()); |
| 803 | |
| 804 | // touch original pointers to check "free" status |
| 805 | EXPECT_TRUE(boost::range::equal(base_string, original)); |
| 806 | |
| 807 | slice3 = slice.get_slice(get_size, sizeof(base_string)); |
| 808 | |
| 809 | EXPECT_EQ(original.begin(), slice.begin()); |
| 810 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 811 | EXPECT_EQ(original.end(), slice.end()); |
| 812 | EXPECT_EQ(slice.end(), slice.cend()); |
| 813 | |
| 814 | EXPECT_EQ(original.begin(), slice2.begin()); |
| 815 | EXPECT_EQ(slice2.begin(), slice2.cbegin()); |
| 816 | EXPECT_EQ(original.begin() + get_size, slice2.end()); |
| 817 | EXPECT_EQ(slice2.end(), slice2.cend()); |
| 818 | |
| 819 | EXPECT_EQ(slice2.end(), slice3.begin()); |
| 820 | EXPECT_EQ(slice3.begin(), slice3.cbegin()); |
| 821 | EXPECT_EQ(original.end(), slice3.end()); |
| 822 | EXPECT_EQ(slice3.end(), slice3.cend()); |
| 823 | |
| 824 | EXPECT_FALSE(slice.empty()); |
| 825 | EXPECT_EQ(slice.cbegin(), slice.data()); |
| 826 | EXPECT_EQ(original.size(), slice.size()); |
| 827 | |
| 828 | EXPECT_FALSE(slice2.empty()); |
| 829 | EXPECT_EQ(slice2.cbegin(), slice2.data()); |
| 830 | EXPECT_EQ(get_size, slice2.size()); |
| 831 | |
| 832 | EXPECT_FALSE(slice3.empty()); |
| 833 | EXPECT_EQ(slice3.cbegin(), slice3.data()); |
| 834 | EXPECT_EQ(get2_size, slice3.size()); |
| 835 | |
| 836 | EXPECT_THROW(slice.get_slice(1, 0), std::out_of_range); |
| 837 | EXPECT_THROW(slice.get_slice(0, sizeof(base_string) + 1), std::out_of_range); |
| 838 | EXPECT_THROW(slice.get_slice(sizeof(base_string) + 1, sizeof(base_string) + 1), std::out_of_range); |
| 839 | EXPECT_TRUE(slice.get_slice(sizeof(base_string), sizeof(base_string)).empty()); |
| 840 | |
| 841 | EXPECT_EQ(original.begin(), slice.begin()); |
| 842 | EXPECT_EQ(slice.begin(), slice.cbegin()); |
| 843 | EXPECT_EQ(original.end(), slice.end()); |
| 844 | EXPECT_EQ(slice.end(), slice.cend()); |
| 845 | |
| 846 | EXPECT_FALSE(slice.empty()); |
| 847 | EXPECT_EQ(slice.cbegin(), slice.data()); |
| 848 | EXPECT_EQ(original.size(), slice.size()); |
| 849 | } |
| 850 | |
| 851 | // touch original pointers to check "free" status |
| 852 | EXPECT_TRUE(boost::range::equal(base_string, original)); |
| 853 | } |
| 854 | |
| 855 | TEST(ByteStream, Construction) |
| 856 | { |
| 857 | EXPECT_TRUE(std::is_default_constructible<epee::byte_stream>()); |
| 858 | EXPECT_TRUE(std::is_move_constructible<epee::byte_stream>()); |
| 859 | EXPECT_FALSE(std::is_copy_constructible<epee::byte_stream>()); |
| 860 | EXPECT_TRUE(std::is_move_assignable<epee::byte_stream>()); |
| 861 | EXPECT_FALSE(std::is_copy_assignable<epee::byte_stream>()); |
| 862 | } |
| 863 | |
| 864 | TEST(ByteStream, Noexcept) |
| 865 | { |
| 866 | EXPECT_TRUE(std::is_nothrow_default_constructible<epee::byte_stream>()); |
| 867 | EXPECT_TRUE(std::is_nothrow_move_constructible<epee::byte_stream>()); |
| 868 | EXPECT_TRUE(std::is_nothrow_move_assignable<epee::byte_stream>()); |
| 869 | |
| 870 | epee::byte_stream lvalue; |
| 871 | const epee::byte_stream clvalue; |
| 872 | |
| 873 | EXPECT_TRUE(noexcept(lvalue.data())); |
| 874 | EXPECT_TRUE(noexcept(clvalue.data())); |
| 875 | EXPECT_TRUE(noexcept(lvalue.tellp())); |
| 876 | EXPECT_TRUE(noexcept(clvalue.tellp())); |
| 877 | EXPECT_TRUE(noexcept(lvalue.available())); |
| 878 | EXPECT_TRUE(noexcept(clvalue.available())); |
| 879 | EXPECT_TRUE(noexcept(lvalue.size())); |
| 880 | EXPECT_TRUE(noexcept(clvalue.size())); |
| 881 | EXPECT_TRUE(noexcept(lvalue.capacity())); |
| 882 | EXPECT_TRUE(noexcept(clvalue.capacity())); |
| 883 | EXPECT_TRUE(noexcept(lvalue.put_unsafe(4))); |
| 884 | EXPECT_TRUE(noexcept(lvalue.take_buffer())); |
| 885 | } |
| 886 | |
| 887 | TEST(ByteStream, Empty) |
| 888 | { |
| 889 | epee::byte_stream stream; |
| 890 | |
| 891 | EXPECT_EQ(nullptr, stream.data()); |
| 892 | EXPECT_EQ(nullptr, stream.tellp()); |
| 893 | EXPECT_EQ(0u, stream.available()); |
| 894 | EXPECT_EQ(0u, stream.size()); |
| 895 | EXPECT_EQ(0u, stream.capacity()); |
| 896 | |
| 897 | const auto buf = stream.take_buffer(); |
| 898 | EXPECT_EQ(nullptr, buf.get()); |
| 899 | EXPECT_EQ(nullptr, stream.data()); |
| 900 | EXPECT_EQ(nullptr, stream.tellp()); |
| 901 | EXPECT_EQ(0u, stream.available()); |
| 902 | EXPECT_EQ(0u, stream.size()); |
| 903 | EXPECT_EQ(0u, stream.capacity()); |
| 904 | } |
| 905 | |
| 906 | TEST(ByteStream, Write) |
| 907 | { |
| 908 | using boost::range::equal; |
| 909 | using byte_span = epee::span<const std::uint8_t>; |
| 910 | |
| 911 | static constexpr const std::uint8_t source[] = |
| 912 | {0xde, 0xad, 0xbe, 0xef, 0xef}; |
| 913 | |
| 914 | std::vector<std::uint8_t> bytes; |
| 915 | epee::byte_stream stream{}; |
| 916 | |
| 917 | stream.write({source, 3}); |
| 918 | bytes.insert(bytes.end(), source, source + 3); |
| 919 | EXPECT_EQ(3u, stream.size()); |
| 920 | EXPECT_LE(3u, stream.capacity()); |
| 921 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 922 | EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); |
| 923 | |
| 924 | const std::size_t capacity = stream.capacity(); |
| 925 | |
| 926 | stream.write({source, 2}); |
| 927 | bytes.insert(bytes.end(), source, source + 2); |
| 928 | EXPECT_EQ(5u, stream.size()); |
| 929 | EXPECT_LE(5u, stream.capacity()); |
| 930 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 931 | EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); |
| 932 | |
| 933 | stream.write({source, 5}); |
| 934 | bytes.insert(bytes.end(), source, source + 5); |
| 935 | EXPECT_EQ(10u, stream.size()); |
| 936 | EXPECT_LE(10u, stream.capacity()); |
| 937 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 938 | EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); |
| 939 | |
| 940 | stream.write({source, 2}); |
| 941 | bytes.insert(bytes.end(), source, source + 2); |
| 942 | EXPECT_EQ(12u, stream.size()); |
| 943 | EXPECT_LE(12u, stream.capacity()); |
| 944 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 945 | EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); |
| 946 | |
| 947 | stream.write({source, 5}); |
| 948 | bytes.insert(bytes.end(), source, source + 5); |
| 949 | EXPECT_EQ(17u, stream.size()); |
| 950 | EXPECT_LE(17u, stream.capacity()); |
| 951 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 952 | EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); |
| 953 | |
| 954 | // ensure it can overflow properly |
| 955 | while (capacity == stream.capacity()) |
| 956 | { |
| 957 | stream.write({source, 5}); |
| 958 | bytes.insert(bytes.end(), source, source + 5); |
| 959 | } |
| 960 | |
| 961 | EXPECT_EQ(bytes.size(), stream.size()); |
| 962 | EXPECT_LE(bytes.size(), stream.capacity()); |
| 963 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 964 | EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); |
| 965 | } |
| 966 | |
| 967 | TEST(ByteStream, Put) |
| 968 | { |
| 969 | using boost::range::equal; |
| 970 | using byte_span = epee::span<const std::uint8_t>; |
| 971 | |
| 972 | std::vector<std::uint8_t> bytes; |
| 973 | epee::byte_stream stream; |
| 974 | |
| 975 | for (std::uint8_t i = 0; i < 200; ++i) |
| 976 | { |
| 977 | bytes.push_back(i); |
| 978 | stream.put(i); |
| 979 | } |
| 980 | |
| 981 | EXPECT_EQ(200u, stream.size()); |
| 982 | EXPECT_LE(200u, stream.capacity()); |
| 983 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 984 | EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); |
| 985 | } |
| 986 | |
| 987 | TEST(ByteStream, PutN) |
| 988 | { |
| 989 | using boost::range::equal; |
| 990 | using byte_span = epee::span<const std::uint8_t>; |
| 991 | |
| 992 | std::vector<std::uint8_t> bytes; |
| 993 | bytes.resize(1000, 'f'); |
| 994 | |
| 995 | epee::byte_stream stream; |
| 996 | stream.put_n('f', 1000); |
| 997 | |
| 998 | EXPECT_EQ(1000u, stream.size()); |
| 999 | EXPECT_LE(1000u, stream.capacity()); |
| 1000 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 1001 | EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); |
| 1002 | } |
| 1003 | |
| 1004 | TEST(ByteStream, Reserve) |
| 1005 | { |
| 1006 | using boost::range::equal; |
| 1007 | using byte_span = epee::span<const std::uint8_t>; |
| 1008 | |
| 1009 | static constexpr const std::uint8_t source[] = |
| 1010 | {0xde, 0xad, 0xbe, 0xef, 0xef}; |
| 1011 | |
| 1012 | std::vector<std::uint8_t> bytes; |
| 1013 | epee::byte_stream stream{}; |
| 1014 | |
| 1015 | stream.reserve(100); |
| 1016 | EXPECT_LE(100u, stream.capacity()); |
| 1017 | EXPECT_EQ(0u, stream.size()); |
| 1018 | EXPECT_EQ(stream.available(), stream.capacity()); |
| 1019 | |
| 1020 | for (std::size_t i = 0; i < 100 / sizeof(source); ++i) |
| 1021 | { |
| 1022 | stream.write(source); |
| 1023 | bytes.insert(bytes.end(), source, source + sizeof(source)); |
| 1024 | } |
| 1025 | |
| 1026 | EXPECT_EQ(100u, stream.size()); |
| 1027 | EXPECT_LE(100u, stream.capacity()); |
| 1028 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 1029 | EXPECT_TRUE(equal(bytes, byte_span{stream.data(), stream.size()})); |
| 1030 | } |
| 1031 | |
| 1032 | TEST(ByteStream, TakeBuffer) |
| 1033 | { |
| 1034 | using boost::range::equal; |
| 1035 | using byte_span = epee::span<const std::uint8_t>; |
| 1036 | |
| 1037 | static constexpr const std::uint8_t source[] = |
| 1038 | {0xde, 0xad, 0xbe, 0xef, 0xef}; |
| 1039 | |
| 1040 | epee::byte_stream stream; |
| 1041 | |
| 1042 | stream.write(source); |
| 1043 | ASSERT_EQ(sizeof(source), stream.size()); |
| 1044 | EXPECT_TRUE(equal(source, byte_span{stream.data(), stream.size()})); |
| 1045 | |
| 1046 | const auto buffer = stream.take_buffer(); |
| 1047 | EXPECT_EQ(0u, stream.size()); |
| 1048 | EXPECT_EQ(0u, stream.available()); |
| 1049 | EXPECT_EQ(0u, stream.capacity()); |
| 1050 | EXPECT_EQ(nullptr, stream.data()); |
| 1051 | EXPECT_EQ(nullptr, stream.tellp()); |
| 1052 | EXPECT_TRUE(equal(source, byte_span{buffer.get(), sizeof(source)})); |
| 1053 | } |
| 1054 | |
| 1055 | TEST(ByteStream, Move) |
| 1056 | { |
| 1057 | using boost::range::equal; |
| 1058 | using byte_span = epee::span<const std::uint8_t>; |
| 1059 | |
| 1060 | static constexpr const std::uint8_t source[] = |
| 1061 | {0xde, 0xad, 0xbe, 0xef, 0xef}; |
| 1062 | |
| 1063 | epee::byte_stream stream{}; |
| 1064 | stream.write(source); |
| 1065 | |
| 1066 | const std::size_t capacity = stream.capacity(); |
| 1067 | std::uint8_t const* const data = stream.data(); |
| 1068 | EXPECT_LE(5u, capacity); |
| 1069 | EXPECT_NE(nullptr, data); |
| 1070 | |
| 1071 | epee::byte_stream stream2{std::move(stream)}; |
| 1072 | |
| 1073 | EXPECT_EQ(0u, stream.size()); |
| 1074 | EXPECT_EQ(0u, stream.available()); |
| 1075 | EXPECT_EQ(0u, stream.capacity()); |
| 1076 | EXPECT_EQ(nullptr, stream.data()); |
| 1077 | EXPECT_EQ(nullptr, stream.tellp()); |
| 1078 | |
| 1079 | EXPECT_EQ(5u, stream2.size()); |
| 1080 | EXPECT_EQ(capacity, stream2.capacity()); |
| 1081 | EXPECT_EQ(capacity - 5, stream2.available()); |
| 1082 | EXPECT_EQ(data, stream2.data()); |
| 1083 | EXPECT_EQ(data + 5u, stream2.tellp()); |
| 1084 | EXPECT_TRUE(equal(source, byte_span{stream2.data(), stream2.size()})); |
| 1085 | |
| 1086 | stream = epee::byte_stream{}; |
| 1087 | |
| 1088 | EXPECT_EQ(0u, stream.size()); |
| 1089 | EXPECT_EQ(0u, stream.available()); |
| 1090 | EXPECT_EQ(0u, stream.capacity()); |
| 1091 | EXPECT_EQ(nullptr, stream.data()); |
| 1092 | EXPECT_EQ(nullptr, stream.tellp()); |
| 1093 | |
| 1094 | stream = std::move(stream2); |
| 1095 | |
| 1096 | EXPECT_EQ(5u, stream.size()); |
| 1097 | EXPECT_EQ(capacity, stream.capacity()); |
| 1098 | EXPECT_EQ(capacity - 5, stream.available()); |
| 1099 | EXPECT_NE(nullptr, stream.data()); |
| 1100 | EXPECT_NE(nullptr, stream.tellp()); |
| 1101 | EXPECT_TRUE(equal(source, byte_span{stream.data(), stream.size()})); |
| 1102 | |
| 1103 | EXPECT_EQ(0u, stream2.size()); |
| 1104 | EXPECT_EQ(0u, stream2.available()); |
| 1105 | EXPECT_EQ(0u, stream2.capacity()); |
| 1106 | EXPECT_EQ(nullptr, stream2.data()); |
| 1107 | EXPECT_EQ(nullptr, stream2.tellp()); |
| 1108 | } |
| 1109 | |
| 1110 | TEST(ByteStream, ToByteSlice) |
| 1111 | { |
| 1112 | using boost::range::equal; |
| 1113 | using byte_span = epee::span<const std::uint8_t>; |
| 1114 | |
| 1115 | static constexpr const std::uint8_t source[] = |
| 1116 | {0xde, 0xad, 0xbe, 0xef, 0xef}; |
| 1117 | |
| 1118 | epee::byte_stream stream; |
| 1119 | |
| 1120 | stream.reserve(128*1024); |
| 1121 | stream.write(source); |
| 1122 | EXPECT_EQ(sizeof(source), stream.size()); |
| 1123 | EXPECT_EQ(128*1024, stream.capacity()); |
| 1124 | EXPECT_TRUE(equal(source, byte_span{stream.data(), stream.size()})); |
| 1125 | |
| 1126 | const epee::byte_slice slice{std::move(stream), true}; |
| 1127 | EXPECT_EQ(0u, stream.size()); |
| 1128 | EXPECT_EQ(0u, stream.available()); |
| 1129 | EXPECT_EQ(0u, stream.capacity()); |
| 1130 | EXPECT_EQ(nullptr, stream.data()); |
| 1131 | EXPECT_EQ(nullptr, stream.tellp()); |
| 1132 | EXPECT_TRUE(equal(source, slice)); |
| 1133 | |
| 1134 | stream = epee::byte_stream{}; |
| 1135 | stream.reserve(1); |
| 1136 | EXPECT_NE(nullptr, stream.data()); |
| 1137 | EXPECT_NE(nullptr, stream.tellp()); |
| 1138 | |
| 1139 | const epee::byte_slice empty_slice{std::move(stream)}; |
| 1140 | EXPECT_TRUE(empty_slice.empty()); |
| 1141 | EXPECT_EQ(0u, empty_slice.size()); |
| 1142 | EXPECT_EQ(nullptr, empty_slice.begin()); |
| 1143 | EXPECT_EQ(nullptr, empty_slice.cbegin()); |
| 1144 | EXPECT_EQ(nullptr, empty_slice.end()); |
| 1145 | EXPECT_EQ(nullptr, empty_slice.cend()); |
| 1146 | EXPECT_EQ(nullptr, empty_slice.data()); |
| 1147 | } |
| 1148 | |
| 1149 | TEST(ByteStream, Clear) |
| 1150 | { |
| 1151 | static constexpr const std::uint8_t source[] = |
| 1152 | {0xde, 0xad, 0xbe, 0xef, 0xef}; |
| 1153 | |
| 1154 | epee::byte_stream stream{}; |
| 1155 | |
| 1156 | EXPECT_EQ(nullptr, stream.data()); |
| 1157 | EXPECT_EQ(nullptr, stream.tellp()); |
| 1158 | EXPECT_EQ(0u, stream.size()); |
| 1159 | EXPECT_EQ(0u, stream.available()); |
| 1160 | EXPECT_EQ(0u, stream.capacity()); |
| 1161 | |
| 1162 | stream.clear(); |
| 1163 | |
| 1164 | EXPECT_EQ(nullptr, stream.data()); |
| 1165 | EXPECT_EQ(nullptr, stream.tellp()); |
| 1166 | EXPECT_EQ(0u, stream.size()); |
| 1167 | EXPECT_EQ(0u, stream.available()); |
| 1168 | EXPECT_EQ(0u, stream.capacity()); |
| 1169 | |
| 1170 | stream.write({source, 3}); |
| 1171 | std::uint8_t const* const loc = stream.data(); |
| 1172 | |
| 1173 | EXPECT_EQ(loc, stream.data()); |
| 1174 | EXPECT_EQ(loc + 3, stream.tellp()); |
| 1175 | EXPECT_EQ(3u, stream.size()); |
| 1176 | EXPECT_LE(stream.size(), stream.capacity()); |
| 1177 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 1178 | |
| 1179 | const std::size_t capacity = stream.capacity(); |
| 1180 | stream.clear(); |
| 1181 | |
| 1182 | EXPECT_EQ(loc, stream.data()); |
| 1183 | EXPECT_EQ(loc, stream.tellp()); |
| 1184 | EXPECT_EQ(0u, stream.size()); |
| 1185 | EXPECT_EQ(capacity, stream.capacity()); |
| 1186 | EXPECT_EQ(stream.available(), stream.capacity() - stream.size()); |
| 1187 | } |
| 1188 | |
| 1189 | TEST(ToHex, String) |
| 1190 | { |
| 1191 | EXPECT_TRUE(epee::to_hex::string(nullptr).empty()); |
| 1192 | EXPECT_EQ( |
| 1193 | std::string{"ffab0100"}, |
| 1194 | epee::to_hex::string(epee::as_byte_span("\xff\xab\x01")) |
| 1195 | ); |
| 1196 | |
| 1197 | const std::vector<unsigned char> all_bytes = get_all_bytes(); |
| 1198 | EXPECT_EQ( |
| 1199 | std_to_hex(all_bytes), epee::to_hex::string(epee::to_span(all_bytes)) |
| 1200 | ); |
| 1201 | |
| 1202 | } |
| 1203 | |
| 1204 | TEST(HexLocale, String) |
| 1205 | { |
| 1206 | // the source data to encode and decode |
| 1207 | std::vector<uint8_t> source{{ 0x00, 0xFF, 0x0F, 0xF0 }}; |
| 1208 | |
| 1209 | // encode and decode the data |
| 1210 | auto hex = epee::to_hex::string({ source.data(), source.size() }); |
| 1211 | auto decoded = epee::from_hex_locale::to_vector(hex); |
| 1212 | |
| 1213 | // encoded should be twice the size and should decode to the exact same data |
| 1214 | EXPECT_EQ(source.size() * 2, hex.size()); |
| 1215 | EXPECT_EQ(source, decoded); |
| 1216 | |
| 1217 | // we will now create a padded hex string, we want to explicitly allow |
| 1218 | // decoding it this way also, ignoring spaces and colons between the numbers |
| 1219 | hex.assign("00:ff 0f:f0"); |
| 1220 | EXPECT_EQ(source, epee::from_hex_locale::to_vector(hex)); |
| 1221 | |
| 1222 | hex.append("f0"); |
| 1223 | EXPECT_EQ(source, epee::from_hex_locale::to_vector(boost::string_ref{hex.data(), hex.size() - 2})); |
| 1224 | } |
| 1225 | |
| 1226 | TEST(ToHex, Array) |
| 1227 | { |
| 1228 | EXPECT_EQ( |
| 1229 | (std::array<char, 8>{{'f', 'f', 'a', 'b', '0', '1', '0', '0'}}), |
| 1230 | (epee::to_hex::array(std::array<unsigned char, 4>{{0xFF, 0xAB, 0x01, 0x00}})) |
| 1231 | ); |
| 1232 | } |
| 1233 | |
| 1234 | TEST(ToHex, ArrayFromPod) |
| 1235 | { |
| 1236 | std::array<char, 64> expected{{'5', 'f', '2', 'b', '0', '1'}}; |
| 1237 | std::fill(expected.begin() + 6, expected.end(), '0'); |
| 1238 | |
| 1239 | EXPECT_EQ( |
| 1240 | expected, |
| 1241 | (epee::to_hex::array(crypto::ec_point{{0x5F, 0x2B, 0x01, 0x00}})) |
| 1242 | ); |
| 1243 | } |
| 1244 | |
| 1245 | TEST(ToHex, Ostream) |
| 1246 | { |
| 1247 | std::stringstream out; |
| 1248 | epee::to_hex::buffer(out, nullptr); |
| 1249 | EXPECT_TRUE(out.str().empty()); |
| 1250 | |
| 1251 | { |
| 1252 | const std::uint8_t source[] = {0xff, 0xab, 0x01, 0x00}; |
| 1253 | epee::to_hex::buffer(out, source); |
| 1254 | } |
| 1255 | |
| 1256 | std::string expected{"ffab0100"}; |
| 1257 | EXPECT_EQ(expected, out.str()); |
| 1258 | |
| 1259 | const std::vector<unsigned char> all_bytes = get_all_bytes(); |
| 1260 | |
| 1261 | expected.append(std_to_hex(all_bytes)); |
| 1262 | epee::to_hex::buffer(out, epee::to_span(all_bytes)); |
| 1263 | EXPECT_EQ(expected, out.str()); |
| 1264 | } |
| 1265 | |
| 1266 | TEST(ToHex, Formatted) |
| 1267 | { |
| 1268 | std::stringstream out; |
| 1269 | std::string expected{"<>"}; |
| 1270 | |
| 1271 | epee::to_hex::formatted(out, nullptr); |
| 1272 | EXPECT_EQ(expected, out.str()); |
| 1273 | |
| 1274 | expected.append("<ffab0100>"); |
| 1275 | epee::to_hex::formatted(out, epee::as_byte_span("\xFF\xAB\x01")); |
| 1276 | EXPECT_EQ(expected, out.str()); |
| 1277 | |
| 1278 | const std::vector<unsigned char> all_bytes = get_all_bytes(); |
| 1279 | |
| 1280 | expected.append("<").append(std_to_hex(all_bytes)).append(">"); |
| 1281 | epee::to_hex::formatted(out, epee::to_span(all_bytes)); |
| 1282 | EXPECT_EQ(expected, out.str()); |
| 1283 | } |
| 1284 | |
| 1285 | TEST(FromHex, ToString) |
| 1286 | { |
| 1287 | static constexpr const char hex[] = u8"deadbeeffY"; |
| 1288 | static constexpr const char binary[] = { |
| 1289 | char(0xde), char(0xad), char(0xbe), char(0xef), 0x00 |
| 1290 | }; |
| 1291 | |
| 1292 | std::string out{}; |
| 1293 | EXPECT_FALSE(epee::from_hex::to_string(out, hex)); |
| 1294 | |
| 1295 | boost::string_ref portion{hex}; |
| 1296 | portion.remove_suffix(1); |
| 1297 | EXPECT_FALSE(epee::from_hex::to_string(out, portion)); |
| 1298 | |
| 1299 | portion.remove_suffix(1); |
| 1300 | EXPECT_TRUE(epee::from_hex::to_string(out, portion)); |
| 1301 | EXPECT_EQ(std::string{binary}, out); |
| 1302 | } |
| 1303 | |
| 1304 | TEST(FromHex, ToBuffer) |
| 1305 | { |
| 1306 | static constexpr const char hex[] = u8"deadbeeffY"; |
| 1307 | static constexpr const std::uint8_t binary[] = {0xde, 0xad, 0xbe, 0xef}; |
| 1308 | |
| 1309 | std::vector<std::uint8_t> out{}; |
| 1310 | out.resize(sizeof(binary)); |
| 1311 | EXPECT_FALSE(epee::from_hex::to_buffer(epee::to_mut_span(out), hex)); |
| 1312 | |
| 1313 | boost::string_ref portion{hex}; |
| 1314 | portion.remove_suffix(1); |
| 1315 | EXPECT_FALSE(epee::from_hex::to_buffer(epee::to_mut_span(out), portion)); |
| 1316 | |
| 1317 | portion.remove_suffix(1); |
| 1318 | EXPECT_FALSE(epee::from_hex::to_buffer({out.data(), out.size() - 1}, portion)); |
| 1319 | |
| 1320 | EXPECT_TRUE(epee::from_hex::to_buffer(epee::to_mut_span(out), portion)); |
| 1321 | const std::vector<std::uint8_t> expected{std::begin(binary), std::end(binary)}; |
| 1322 | EXPECT_EQ(expected, out); |
| 1323 | } |
| 1324 | |
| 1325 | TEST(StringTools, BuffToHex) |
| 1326 | { |
| 1327 | const std::vector<unsigned char> all_bytes = get_all_bytes(); |
| 1328 | |
| 1329 | EXPECT_EQ( |
| 1330 | std_to_hex(all_bytes), |
| 1331 | (epee::string_tools::buff_to_hex_nodelimer( |
| 1332 | std::string{reinterpret_cast<const char*>(all_bytes.data()), all_bytes.size()} |
| 1333 | )) |
| 1334 | ); |
| 1335 | } |
| 1336 | |
| 1337 | TEST(StringTools, PodToHex) |
| 1338 | { |
| 1339 | struct some_pod { unsigned char data[4]; }; |
| 1340 | EXPECT_EQ( |
| 1341 | std::string{"ffab0100"}, |
| 1342 | (epee::string_tools::pod_to_hex(some_pod{{0xFF, 0xAB, 0x01, 0x00}})) |
| 1343 | ); |
| 1344 | } |
| 1345 | |
| 1346 | TEST(StringTools, ParseHex) |
| 1347 | { |
| 1348 | static const char data[] = "a10b68c2"; |
| 1349 | for (size_t i = 0; i < sizeof(data); i += 2) |
| 1350 | { |
| 1351 | std::string res; |
| 1352 | ASSERT_TRUE(epee::string_tools::parse_hexstr_to_binbuff(std::string(data, i), res)); |
| 1353 | std::string hex = epee::string_tools::buff_to_hex_nodelimer(res); |
| 1354 | ASSERT_EQ(hex.size(), i); |
| 1355 | ASSERT_EQ(memcmp(data, hex.data(), i), 0); |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | TEST(StringTools, ParseNotHex) |
| 1360 | { |
| 1361 | std::string res; |
| 1362 | for (size_t i = 0; i < 256; ++i) |
| 1363 | { |
| 1364 | std::string inputHexString = std::string(2, static_cast<char>(i)); |
| 1365 | if ((i >= '0' && i <= '9') || (i >= 'A' && i <= 'F') || (i >= 'a' && i <= 'f')) { |
| 1366 | ASSERT_TRUE(epee::string_tools::parse_hexstr_to_binbuff(inputHexString, res)); |
| 1367 | } else { |
| 1368 | ASSERT_FALSE(epee::string_tools::parse_hexstr_to_binbuff(inputHexString, res)); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | ASSERT_FALSE(epee::string_tools::parse_hexstr_to_binbuff(std::string("a"), res)); |
| 1373 | } |
| 1374 | |
| 1375 | TEST(StringTools, GetIpString) |
| 1376 | { |
| 1377 | EXPECT_EQ( |
| 1378 | std::string{"0.0.0.0"}, epee::string_tools::get_ip_string_from_int32(0) |
| 1379 | ); |
| 1380 | EXPECT_EQ( |
| 1381 | std::string{"255.0.255.0"}, |
| 1382 | epee::string_tools::get_ip_string_from_int32(htonl(0xff00ff00)) |
| 1383 | ); |
| 1384 | EXPECT_EQ( |
| 1385 | std::string{"255.255.255.255"}, |
| 1386 | epee::string_tools::get_ip_string_from_int32(htonl(0xffffffff)) |
| 1387 | ); |
| 1388 | } |
| 1389 | |
| 1390 | TEST(StringTools, GetIpInt32) |
| 1391 | { |
| 1392 | std::uint32_t ip = 0; |
| 1393 | EXPECT_FALSE(epee::string_tools::get_ip_int32_from_string(ip, "")); |
| 1394 | EXPECT_FALSE(epee::string_tools::get_ip_int32_from_string(ip, "1.")); |
| 1395 | EXPECT_FALSE(epee::string_tools::get_ip_int32_from_string(ip, "1.1.")); |
| 1396 | EXPECT_FALSE(epee::string_tools::get_ip_int32_from_string(ip, "1.1.1.")); |
| 1397 | EXPECT_FALSE(epee::string_tools::get_ip_int32_from_string(ip, "ff.0.ff.0")); |
| 1398 | EXPECT_FALSE(epee::string_tools::get_ip_int32_from_string(ip, "1.1.1.256")); |
| 1399 | |
| 1400 | EXPECT_TRUE(epee::string_tools::get_ip_int32_from_string(ip, "1")); |
| 1401 | EXPECT_EQ(htonl(1), ip); |
| 1402 | |
| 1403 | EXPECT_TRUE(epee::string_tools::get_ip_int32_from_string(ip, "1.1")); |
| 1404 | EXPECT_EQ(htonl(0x1000001), ip); |
| 1405 | |
| 1406 | EXPECT_TRUE(epee::string_tools::get_ip_int32_from_string(ip, "1.1.1")); |
| 1407 | EXPECT_EQ(htonl(0x1010001), ip); |
| 1408 | |
| 1409 | EXPECT_TRUE(epee::string_tools::get_ip_int32_from_string(ip, "0.0.0.0")); |
| 1410 | EXPECT_EQ(0, ip); |
| 1411 | |
| 1412 | EXPECT_TRUE(epee::string_tools::get_ip_int32_from_string(ip, "1.1.1.1")); |
| 1413 | EXPECT_EQ(htonl(0x01010101), ip); |
| 1414 | |
| 1415 | /* |
| 1416 | The existing epee conversion function does not work with 255.255.255.255, for |
| 1417 | the reasons specified in the inet_addr documentation. Consider fixing in a |
| 1418 | future patch. This address is not likely to be used for purposes within |
| 1419 | monero. |
| 1420 | EXPECT_TRUE(epee::string_tools::get_ip_int32_from_string(ip, "255.255.255.255")); |
| 1421 | EXPECT_EQ(htonl(0xffffffff), ip); |
| 1422 | */ |
| 1423 | |
| 1424 | EXPECT_TRUE(epee::string_tools::get_ip_int32_from_string(ip, "10.0377.0.0377")); |
| 1425 | EXPECT_EQ(htonl(0xaff00ff), ip); |
| 1426 | |
| 1427 | EXPECT_TRUE(epee::string_tools::get_ip_int32_from_string(ip, "0xff.10.0xff.0")); |
| 1428 | EXPECT_EQ(htonl(0xff0aff00), ip); |
| 1429 | } |
| 1430 | |
| 1431 | TEST(StringTools, GetExtension) |
| 1432 | { |
| 1433 | EXPECT_EQ(std::string{}, epee::string_tools::get_extension("")); |
| 1434 | EXPECT_EQ(std::string{}, epee::string_tools::get_extension(".")); |
| 1435 | EXPECT_EQ(std::string{"keys"}, epee::string_tools::get_extension("wallet.keys")); |
| 1436 | EXPECT_EQ(std::string{"3"}, epee::string_tools::get_extension("1.2.3")); |
| 1437 | } |
| 1438 | |
| 1439 | TEST(StringTools, CutOffExtension) |
| 1440 | { |
| 1441 | EXPECT_EQ(std::string{}, epee::string_tools::cut_off_extension("")); |
| 1442 | EXPECT_EQ(std::string{"/home/user/Monero/wallets/wallet"}, epee::string_tools::cut_off_extension("/home/user/Monero/wallets/wallet")); |
| 1443 | EXPECT_EQ(std::string{"/home/user/Monero/wallets/wallet"}, epee::string_tools::cut_off_extension("/home/user/Monero/wallets/wallet.keys")); |
| 1444 | } |
| 1445 | |
| 1446 | TEST(NetUtils, IPv4NetworkAddress) |
| 1447 | { |
| 1448 | static_assert(epee::net_utils::ipv4_network_address::get_type_id() == epee::net_utils::address_type::ipv4, "bad ipv4 type id"); |
| 1449 | |
| 1450 | const auto ip1 = boost::endian::native_to_big(0x330012FFu); |
| 1451 | const auto ip_loopback = boost::endian::native_to_big(0x7F000001u); |
| 1452 | const auto ip_local = boost::endian::native_to_big(0x0A000000u); |
| 1453 | |
| 1454 | epee::net_utils::ipv4_network_address address1{ip1, 65535}; |
| 1455 | CHECK_EQUAL(address1, address1); |
| 1456 | EXPECT_STREQ("51.0.18.255:65535", address1.str().c_str()); |
| 1457 | EXPECT_STREQ("51.0.18.255", address1.host_str().c_str()); |
| 1458 | EXPECT_FALSE(address1.is_loopback()); |
| 1459 | EXPECT_FALSE(address1.is_local()); |
| 1460 | EXPECT_EQ(epee::net_utils::ipv4_network_address::get_type_id(), address1.get_type_id()); |
| 1461 | EXPECT_EQ(ip1, address1.ip()); |
| 1462 | EXPECT_EQ(65535, address1.port()); |
| 1463 | EXPECT_TRUE(epee::net_utils::ipv4_network_address{std::move(address1)} == address1); |
| 1464 | EXPECT_TRUE(epee::net_utils::ipv4_network_address{address1} == address1); |
| 1465 | |
| 1466 | const epee::net_utils::ipv4_network_address loopback{ip_loopback, 0}; |
| 1467 | CHECK_EQUAL(loopback, loopback); |
| 1468 | CHECK_LESS_ENDIAN(address1, loopback); |
| 1469 | EXPECT_STREQ("127.0.0.1:0", loopback.str().c_str()); |
| 1470 | EXPECT_STREQ("127.0.0.1", loopback.host_str().c_str()); |
| 1471 | EXPECT_TRUE(loopback.is_loopback()); |
| 1472 | EXPECT_FALSE(loopback.is_local()); |
| 1473 | EXPECT_EQ(epee::net_utils::ipv4_network_address::get_type_id(), address1.get_type_id()); |
| 1474 | EXPECT_EQ(ip_loopback, loopback.ip()); |
| 1475 | EXPECT_EQ(0, loopback.port()); |
| 1476 | |
| 1477 | const epee::net_utils::ipv4_network_address local{ip_local, 8080}; |
| 1478 | CHECK_EQUAL(local, local); |
| 1479 | CHECK_LESS(local, address1); |
| 1480 | CHECK_LESS(local, loopback); |
| 1481 | EXPECT_FALSE(local.is_loopback()); |
| 1482 | EXPECT_TRUE(local.is_local()); |
| 1483 | |
| 1484 | epee::net_utils::ipv4_network_address address2{ip1, 55}; |
| 1485 | CHECK_EQUAL(address2, address2); |
| 1486 | CHECK_LESS_ENDIAN(address2, loopback); |
| 1487 | CHECK_LESS(local, address2); |
| 1488 | EXPECT_STREQ("51.0.18.255:55", address2.str().c_str()); |
| 1489 | EXPECT_STREQ("51.0.18.255", address2.host_str().c_str()); |
| 1490 | |
| 1491 | |
| 1492 | address2 = std::move(address1); |
| 1493 | CHECK_EQUAL(address2, address1); |
| 1494 | |
| 1495 | address2 = local; |
| 1496 | CHECK_EQUAL(address2, local); |
| 1497 | CHECK_LESS(address2, address1); |
| 1498 | |
| 1499 | { |
| 1500 | std::stringstream stream; |
| 1501 | { |
| 1502 | boost::archive::portable_binary_oarchive ostream{stream}; |
| 1503 | ostream << address1; |
| 1504 | } |
| 1505 | { |
| 1506 | boost::archive::portable_binary_iarchive istream{stream}; |
| 1507 | istream >> address2; |
| 1508 | } |
| 1509 | } |
| 1510 | CHECK_EQUAL(address1, address2); |
| 1511 | EXPECT_EQ(ip1, address2.ip()); |
| 1512 | EXPECT_EQ(65535, address2.port()); |
| 1513 | } |
| 1514 | |
| 1515 | TEST(NetUtils, NetworkAddress) |
| 1516 | { |
| 1517 | const auto ip1 = boost::endian::native_to_big(0x330012FFu); |
| 1518 | const auto ip_loopback = boost::endian::native_to_big(0x7F000001u); |
| 1519 | const auto ip_local = boost::endian::native_to_big(0x0A000000u); |
| 1520 | |
| 1521 | struct custom_address { |
| 1522 | constexpr static bool equal(const custom_address&) noexcept { return false; } |
| 1523 | constexpr static bool less(const custom_address&) noexcept { return false; } |
| 1524 | constexpr static bool is_same_host(const custom_address&) noexcept { return false; } |
| 1525 | constexpr static bool is_loopback() noexcept { return false; } |
| 1526 | constexpr static bool is_local() noexcept { return false; } |
| 1527 | static std::string str() { return {}; } |
| 1528 | static std::string host_str() { return {}; } |
| 1529 | constexpr static epee::net_utils::address_type get_type_id() noexcept { return epee::net_utils::address_type(-1); } |
| 1530 | constexpr static epee::net_utils::zone get_zone() noexcept { return epee::net_utils::zone::invalid; } |
| 1531 | constexpr static bool is_blockable() noexcept { return false; } |
| 1532 | constexpr static uint16_t port() { return 0; } |
| 1533 | }; |
| 1534 | |
| 1535 | const epee::net_utils::network_address empty; |
| 1536 | CHECK_EQUAL(empty, empty); |
| 1537 | EXPECT_TRUE(empty.is_same_host(empty)); |
| 1538 | EXPECT_STREQ("<none>", empty.str().c_str()); |
| 1539 | EXPECT_STREQ("<none>", empty.host_str().c_str()); |
| 1540 | EXPECT_FALSE(empty.is_loopback()); |
| 1541 | EXPECT_FALSE(empty.is_local()); |
| 1542 | EXPECT_EQ(epee::net_utils::address_type::invalid, empty.get_type_id()); |
| 1543 | EXPECT_EQ(epee::net_utils::zone::invalid, empty.get_zone()); |
| 1544 | EXPECT_FALSE(empty.is_blockable()); |
| 1545 | EXPECT_THROW(empty.as<custom_address>(), std::bad_cast); |
| 1546 | |
| 1547 | epee::net_utils::network_address address1{ |
| 1548 | epee::net_utils::ipv4_network_address{ip1, 65535} |
| 1549 | }; |
| 1550 | CHECK_EQUAL(address1, address1); |
| 1551 | CHECK_EQUAL(epee::net_utils::network_address{address1}, address1); |
| 1552 | CHECK_LESS(empty, address1); |
| 1553 | EXPECT_TRUE(address1.is_same_host(address1)); |
| 1554 | EXPECT_FALSE(empty.is_same_host(address1)); |
| 1555 | EXPECT_FALSE(address1.is_same_host(empty)); |
| 1556 | EXPECT_STREQ("51.0.18.255:65535", address1.str().c_str()); |
| 1557 | EXPECT_STREQ("51.0.18.255", address1.host_str().c_str()); |
| 1558 | EXPECT_FALSE(address1.is_loopback()); |
| 1559 | EXPECT_FALSE(address1.is_local()); |
| 1560 | EXPECT_EQ(epee::net_utils::ipv4_network_address::get_type_id(), address1.get_type_id()); |
| 1561 | EXPECT_EQ(epee::net_utils::zone::public_, address1.get_zone()); |
| 1562 | EXPECT_TRUE(address1.is_blockable()); |
| 1563 | EXPECT_NO_THROW(address1.as<epee::net_utils::ipv4_network_address>()); |
| 1564 | EXPECT_THROW(address1.as<custom_address>(), std::bad_cast); |
| 1565 | |
| 1566 | const epee::net_utils::network_address loopback{ |
| 1567 | epee::net_utils::ipv4_network_address{ip_loopback, 0} |
| 1568 | }; |
| 1569 | CHECK_EQUAL(loopback, loopback); |
| 1570 | CHECK_LESS(empty, loopback); |
| 1571 | CHECK_LESS_ENDIAN(address1, loopback); |
| 1572 | EXPECT_TRUE(loopback.is_same_host(loopback)); |
| 1573 | EXPECT_FALSE(loopback.is_same_host(address1)); |
| 1574 | EXPECT_FALSE(address1.is_same_host(loopback)); |
| 1575 | EXPECT_STREQ("127.0.0.1:0", loopback.str().c_str()); |
| 1576 | EXPECT_STREQ("127.0.0.1", loopback.host_str().c_str()); |
| 1577 | EXPECT_TRUE(loopback.is_loopback()); |
| 1578 | EXPECT_FALSE(loopback.is_local()); |
| 1579 | EXPECT_EQ(epee::net_utils::ipv4_network_address::get_type_id(), address1.get_type_id()); |
| 1580 | EXPECT_EQ(epee::net_utils::zone::public_, address1.get_zone()); |
| 1581 | EXPECT_EQ(epee::net_utils::ipv4_network_address::get_type_id(), address1.get_type_id()); |
| 1582 | |
| 1583 | const epee::net_utils::network_address local{ |
| 1584 | epee::net_utils::ipv4_network_address{ip_local, 8080} |
| 1585 | }; |
| 1586 | CHECK_EQUAL(local, local); |
| 1587 | CHECK_LESS(local, loopback); |
| 1588 | CHECK_LESS(local, address1); |
| 1589 | EXPECT_FALSE(local.is_loopback()); |
| 1590 | EXPECT_TRUE(local.is_local()); |
| 1591 | |
| 1592 | epee::net_utils::network_address address2{ |
| 1593 | epee::net_utils::ipv4_network_address{ip1, 55} |
| 1594 | }; |
| 1595 | CHECK_EQUAL(address2, address2); |
| 1596 | CHECK_LESS(address2, address1); |
| 1597 | CHECK_LESS(local, address2); |
| 1598 | CHECK_LESS_ENDIAN(address2, loopback); |
| 1599 | EXPECT_TRUE(address1.is_same_host(address2)); |
| 1600 | EXPECT_TRUE(address2.is_same_host(address1)); |
| 1601 | EXPECT_STREQ("51.0.18.255:55", address2.str().c_str()); |
| 1602 | EXPECT_STREQ("51.0.18.255", address2.host_str().c_str()); |
| 1603 | |
| 1604 | address2 = std::move(address1); |
| 1605 | CHECK_EQUAL(address1, address1); |
| 1606 | CHECK_EQUAL(empty, address1); |
| 1607 | CHECK_LESS(address1, address2); |
| 1608 | EXPECT_FALSE(address1.is_same_host(address2)); |
| 1609 | EXPECT_FALSE(address2.is_same_host(address1)); |
| 1610 | EXPECT_STREQ("51.0.18.255:65535", address2.str().c_str()); |
| 1611 | EXPECT_STREQ("51.0.18.255", address2.host_str().c_str()); |
| 1612 | EXPECT_FALSE(address1.is_loopback()); |
| 1613 | EXPECT_FALSE(address1.is_local()); |
| 1614 | EXPECT_THROW(address1.as<epee::net_utils::ipv4_network_address>(), std::bad_cast); |
| 1615 | EXPECT_NO_THROW(address2.as<epee::net_utils::ipv4_network_address>()); |
| 1616 | |
| 1617 | address2 = local; |
| 1618 | CHECK_EQUAL(address2, local); |
| 1619 | CHECK_LESS(address1, address2); |
| 1620 | EXPECT_TRUE(address2.is_same_host(local)); |
| 1621 | EXPECT_TRUE(local.is_same_host(address2)); |
| 1622 | EXPECT_FALSE(address2.is_same_host(address1)); |
| 1623 | EXPECT_FALSE(address1.is_same_host(address2)); |
| 1624 | |
| 1625 | { |
| 1626 | std::stringstream stream; |
| 1627 | { |
| 1628 | boost::archive::portable_binary_oarchive ostream{stream}; |
| 1629 | ostream << address2; |
| 1630 | } |
| 1631 | { |
| 1632 | boost::archive::portable_binary_iarchive istream{stream}; |
| 1633 | istream >> address1; |
| 1634 | } |
| 1635 | } |
| 1636 | CHECK_EQUAL(address1, address2); |
| 1637 | EXPECT_TRUE(address1.is_same_host(address2)); |
| 1638 | EXPECT_TRUE(address2.is_same_host(address1)); |
| 1639 | EXPECT_NO_THROW(address1.as<epee::net_utils::ipv4_network_address>()); |
| 1640 | |
| 1641 | address1 = custom_address{}; |
| 1642 | CHECK_EQUAL(address1, address1); |
| 1643 | CHECK_LESS(address2, address1); |
| 1644 | EXPECT_FALSE(address1.is_same_host(loopback)); |
| 1645 | EXPECT_FALSE(loopback.is_same_host(address1)); |
| 1646 | EXPECT_THROW(address1.as<epee::net_utils::ipv4_network_address>(), std::bad_cast); |
| 1647 | EXPECT_NO_THROW(address1.as<custom_address>()); |
| 1648 | } |
| 1649 | |
| 1650 | static bool is_local(const char *s) |
| 1651 | { |
| 1652 | uint32_t ip; |
| 1653 | CHECK_AND_ASSERT_THROW_MES(epee::string_tools::get_ip_int32_from_string(ip, s), std::string("Invalid IP address: ") + s); |
| 1654 | return epee::net_utils::is_ip_local(ip); |
| 1655 | } |
| 1656 | |
| 1657 | TEST(NetUtils, PrivateRanges) |
| 1658 | { |
| 1659 | ASSERT_EQ(is_local("10.0.0.0"), true); |
| 1660 | ASSERT_EQ(is_local("10.255.0.0"), true); |
| 1661 | ASSERT_EQ(is_local("127.0.0.0"), false); // loopback is not considered local |
| 1662 | ASSERT_EQ(is_local("192.167.255.255"), false); |
| 1663 | ASSERT_EQ(is_local("192.168.0.0"), true); |
| 1664 | ASSERT_EQ(is_local("192.168.255.255"), true); |
| 1665 | ASSERT_EQ(is_local("192.169.0.0"), false); |
| 1666 | ASSERT_EQ(is_local("172.0.0.0"), false); |
| 1667 | ASSERT_EQ(is_local("172.15.255.255"), false); |
| 1668 | ASSERT_EQ(is_local("172.16.0.0"), true); |
| 1669 | ASSERT_EQ(is_local("172.16.255.255"), true); |
| 1670 | ASSERT_EQ(is_local("172.31.255.255"), true); |
| 1671 | ASSERT_EQ(is_local("172.32.0.0"), false); |
| 1672 | ASSERT_EQ(is_local("0.0.0.0"), false); |
| 1673 | ASSERT_EQ(is_local("255.255.255.254"), false); |
| 1674 | ASSERT_EQ(is_local("11.255.255.255"), false); |
| 1675 | ASSERT_EQ(is_local("0.0.0.10"), false); |
| 1676 | ASSERT_EQ(is_local("0.0.168.192"), false); |
| 1677 | ASSERT_EQ(is_local("0.0.30.172"), false); |
| 1678 | ASSERT_EQ(is_local("0.0.30.127"), false); |
| 1679 | } |
| 1680 | |
| 1681 | TEST(net_buffer, basic) |
| 1682 | { |
| 1683 | epee::net_utils::buffer buf; |
| 1684 | |
| 1685 | ASSERT_EQ(buf.size(), 0); |
| 1686 | EXPECT_THROW(buf.span(1), std::runtime_error); |
| 1687 | buf.append("a", 1); |
| 1688 | epee::span<const uint8_t> span = buf.span(1); |
| 1689 | ASSERT_EQ(span.size(), 1); |
| 1690 | ASSERT_EQ(span.data()[0], 'a'); |
| 1691 | EXPECT_THROW(buf.span(2), std::runtime_error); |
| 1692 | buf.append("bc", 2); |
| 1693 | buf.erase(1); |
| 1694 | EXPECT_THROW(buf.span(3), std::runtime_error); |
| 1695 | span = buf.span(2); |
| 1696 | ASSERT_EQ(span.size(), 2); |
| 1697 | ASSERT_EQ(span.data()[0], 'b'); |
| 1698 | ASSERT_EQ(span.data()[1], 'c'); |
| 1699 | buf.erase(1); |
| 1700 | EXPECT_THROW(buf.span(2), std::runtime_error); |
| 1701 | span = buf.span(1); |
| 1702 | ASSERT_EQ(span.size(), 1); |
| 1703 | ASSERT_EQ(span.data()[0], 'c'); |
| 1704 | EXPECT_THROW(buf.erase(2), std::runtime_error); |
| 1705 | buf.erase(1); |
| 1706 | EXPECT_EQ(buf.size(), 0); |
| 1707 | EXPECT_THROW(buf.span(1), std::runtime_error); |
| 1708 | } |
| 1709 | |
| 1710 | TEST(net_buffer, existing_capacity) |
| 1711 | { |
| 1712 | epee::net_utils::buffer buf; |
| 1713 | |
| 1714 | buf.append("123456789", 9); |
| 1715 | buf.erase(9); |
| 1716 | buf.append("abc", 3); |
| 1717 | buf.append("def", 3); |
| 1718 | ASSERT_EQ(buf.size(), 6); |
| 1719 | epee::span<const uint8_t> span = buf.span(6); |
| 1720 | ASSERT_TRUE(!memcmp(span.data(), "abcdef", 6)); |
| 1721 | } |
| 1722 | |
| 1723 | TEST(net_buffer, reallocate) |
| 1724 | { |
| 1725 | epee::net_utils::buffer buf; |
| 1726 | |
| 1727 | buf.append(std::string(4000, ' ').c_str(), 4000); |
| 1728 | buf.append(std::string(8000, '0').c_str(), 8000); |
| 1729 | ASSERT_EQ(buf.size(), 12000); |
| 1730 | epee::span<const uint8_t> span = buf.span(12000); |
| 1731 | ASSERT_TRUE(!memcmp(span.data(), std::string(4000, ' ').c_str(), 4000)); |
| 1732 | ASSERT_TRUE(!memcmp(span.data() + 4000, std::string(8000, '0').c_str(), 8000)); |
| 1733 | } |
| 1734 | |
| 1735 | TEST(net_buffer, move) |
| 1736 | { |
| 1737 | epee::net_utils::buffer buf; |
| 1738 | |
| 1739 | buf.append(std::string(400, ' ').c_str(), 400); |
| 1740 | buf.erase(399); |
| 1741 | buf.append(std::string(4000, '0').c_str(), 4000); |
| 1742 | ASSERT_EQ(buf.size(), 4001); |
| 1743 | epee::span<const uint8_t> span = buf.span(4001); |
| 1744 | ASSERT_TRUE(!memcmp(span.data(), std::string(1, ' ').c_str(), 1)); |
| 1745 | ASSERT_TRUE(!memcmp(span.data() + 1, std::string(4000, '0').c_str(), 4000)); |
| 1746 | } |
| 1747 | |
| 1748 | TEST(parsing, isspace) |
| 1749 | { |
| 1750 | ASSERT_FALSE(epee::misc_utils::parse::isspace(0)); |
| 1751 | for (int c = 1; c < 256; ++c) |
| 1752 | { |
| 1753 | ASSERT_EQ(epee::misc_utils::parse::isspace(c), strchr("\r\n\t\f\v ", c) != NULL); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | TEST(parsing, isdigit) |
| 1758 | { |
| 1759 | ASSERT_FALSE(epee::misc_utils::parse::isdigit(0)); |
| 1760 | for (int c = 1; c < 256; ++c) |
| 1761 | { |
| 1762 | ASSERT_EQ(epee::misc_utils::parse::isdigit(c), strchr("0123456789", c) != NULL); |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | TEST(parsing, number) |
| 1767 | { |
| 1768 | struct match_number_test_data { |
| 1769 | std::string in_str; |
| 1770 | std::string expect_out_str; |
| 1771 | bool expect_is_float; |
| 1772 | bool expect_is_signed; |
| 1773 | }; |
| 1774 | |
| 1775 | // Add test cases as needed |
| 1776 | struct match_number_test_data test_data[] = { |
| 1777 | { "0 ", "0", false, false }, |
| 1778 | { "000 ", "000", false, false }, |
| 1779 | { "10x", "10", false, false }, |
| 1780 | { "10.09/", "10.09", true, false }, |
| 1781 | { "-1.r", "-1.", true, true }, |
| 1782 | { "-49.;", "-49.", true, true }, |
| 1783 | { "0.78/", "0.78", true, false }, |
| 1784 | { "33E9$", "33E9", true, false }, |
| 1785 | { ".34e2=", ".34e2", true, false }, |
| 1786 | { "-9.34e-2=", "-9.34e-2", true, true }, |
| 1787 | { "+9.34e+03=", "+9.34e+03", true, false } |
| 1788 | }; |
| 1789 | |
| 1790 | // the parser expects another character to end the number, and accepts things |
| 1791 | // that aren't numbers, as it's meant as a pre-filter for strto* functions, |
| 1792 | // so we just check that numbers get accepted, but don't test non numbers |
| 1793 | // We set is_float/signed_val to the opposite of what we expect the result to |
| 1794 | // make sure that match_number2 is changing the bools as expected |
| 1795 | |
| 1796 | for (const auto& tdata : test_data) { |
| 1797 | std::string::const_iterator it = tdata.in_str.begin(); |
| 1798 | boost::string_ref out_val = "<unassigned>"; |
| 1799 | bool is_float_val = !tdata.expect_is_float; |
| 1800 | bool is_signed_val = !tdata.expect_is_signed; |
| 1801 | epee::misc_utils::parse::match_number2(it, tdata.in_str.end(), out_val, is_float_val, is_signed_val); |
| 1802 | EXPECT_EQ(out_val, tdata.expect_out_str); |
| 1803 | EXPECT_EQ(is_float_val, tdata.expect_is_float); |
| 1804 | EXPECT_EQ(is_signed_val, tdata.expect_is_signed); |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | TEST(parsing, unicode) |
| 1809 | { |
| 1810 | std::string bs; |
| 1811 | std::string s; |
| 1812 | std::string::const_iterator si; |
| 1813 | |
| 1814 | s = "\"\""; |
| 1815 | si = s.begin(); |
| 1816 | epee::misc_utils::parse::match_string2(si, s.end(), bs); |
| 1817 | EXPECT_EQ(bs, ""); |
| 1818 | |
| 1819 | s = "\"\\u0000\""; |
| 1820 | si = s.begin(); |
| 1821 | epee::misc_utils::parse::match_string2(si, s.end(), bs); |
| 1822 | EXPECT_EQ(bs, std::string(1, '\0')); |
| 1823 | |
| 1824 | s = "\"\\u0020\""; |
| 1825 | si = s.begin(); |
| 1826 | epee::misc_utils::parse::match_string2(si, s.end(), bs); |
| 1827 | EXPECT_EQ(bs, " "); |
| 1828 | |
| 1829 | s = "\"\\u1\""; |
| 1830 | si = s.begin(); |
| 1831 | EXPECT_THROW(epee::misc_utils::parse::match_string2(si, s.end(), bs), std::runtime_error); |
| 1832 | |
| 1833 | s = "\"\\u12\""; |
| 1834 | si = s.begin(); |
| 1835 | EXPECT_THROW(epee::misc_utils::parse::match_string2(si, s.end(), bs), std::runtime_error); |
| 1836 | |
| 1837 | s = "\"\\u123\""; |
| 1838 | si = s.begin(); |
| 1839 | EXPECT_THROW(epee::misc_utils::parse::match_string2(si, s.end(), bs), std::runtime_error); |
| 1840 | |
| 1841 | s = "\"\\u1234\""; |
| 1842 | si = s.begin(); |
| 1843 | epee::misc_utils::parse::match_string2(si, s.end(), bs); |
| 1844 | EXPECT_EQ(bs, "ሴ"); |
| 1845 | |
| 1846 | s = "\"foo\\u1234bar\""; si = s.begin(); |
| 1847 | epee::misc_utils::parse::match_string2(si, s.end(), bs); |
| 1848 | EXPECT_EQ(bs, "fooሴbar"); |
| 1849 | |
| 1850 | s = "\"\\u3042\\u307e\\u3084\\u304b\\u3059\""; |
| 1851 | si = s.begin(); |
| 1852 | epee::misc_utils::parse::match_string2(si, s.end(), bs); |
| 1853 | EXPECT_EQ(bs, "あまやかす"); |
| 1854 | } |
| 1855 | |
| 1856 | TEST(parsing, strtoul) |
| 1857 | { |
| 1858 | long ul; |
| 1859 | const char* p; |
| 1860 | const char* endp; |
| 1861 | |
| 1862 | errno = 0; // Some libc's only set errno on failure, some set it to 0 on success |
| 1863 | |
| 1864 | p = "0"; |
| 1865 | endp = nullptr; |
| 1866 | ul = std::strtoul(p, const_cast<char**>(&endp), 10); |
| 1867 | EXPECT_EQ(0, errno); |
| 1868 | EXPECT_EQ(0, ul); |
| 1869 | EXPECT_EQ(p + 1, endp); |
| 1870 | |
| 1871 | p = "000000"; |
| 1872 | endp = nullptr; |
| 1873 | ul = std::strtoul(p, const_cast<char**>(&endp), 10); |
| 1874 | EXPECT_EQ(0, errno); |
| 1875 | EXPECT_EQ(0, ul); |
| 1876 | EXPECT_EQ(p + 6, endp); |
| 1877 | |
| 1878 | p = "1"; |
| 1879 | endp = nullptr; |
| 1880 | ul = std::strtoul(p, const_cast<char**>(&endp), 10); |
| 1881 | EXPECT_EQ(0, errno); |
| 1882 | EXPECT_EQ(1, ul); |
| 1883 | EXPECT_EQ(p + 1, endp); |
| 1884 | |
| 1885 | p = "0q"; |
| 1886 | endp = nullptr; |
| 1887 | ul = std::strtoul(p, const_cast<char**>(&endp), 10); |
| 1888 | EXPECT_EQ(0, errno); |
| 1889 | EXPECT_EQ(0, ul); |
| 1890 | EXPECT_EQ(p + 1, endp); |
| 1891 | |
| 1892 | p = " \t 0"; |
| 1893 | endp = nullptr; |
| 1894 | ul = std::strtoul(p, const_cast<char**>(&endp), 10); |
| 1895 | EXPECT_EQ(0, errno); |
| 1896 | EXPECT_EQ(0, ul); |
| 1897 | EXPECT_EQ(p + 9, endp); |
| 1898 | |
| 1899 | p = "q"; |
| 1900 | endp = nullptr; |
| 1901 | ul = std::strtoul(p, const_cast<char**>(&endp), 10); |
| 1902 | EXPECT_EQ(0, ul); |
| 1903 | EXPECT_EQ(p, endp); |
| 1904 | |
| 1905 | p = "999999999999999999999999999999999999999"; |
| 1906 | endp = nullptr; |
| 1907 | ul = std::strtoul(p, const_cast<char**>(&endp), 10); |
| 1908 | EXPECT_EQ(ERANGE, errno); |
| 1909 | EXPECT_EQ(ULLONG_MAX, ul); |
| 1910 | } |