|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2019 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// Layout / POD-trait verification for `httpserver::iovec_entry`. |
| 22 | +// This TU is allowed to include <sys/uio.h> directly — it is an internal |
| 23 | +// test, not a header-hygiene sentinel. The library-side guarantee that |
| 24 | +// downstream code does NOT see <sys/uio.h> via the umbrella is asserted |
| 25 | +// separately by `header_hygiene_iovec_test.cpp`. |
| 26 | + |
| 27 | +#include <cstddef> |
| 28 | +#include <sys/uio.h> |
| 29 | +#include <type_traits> |
| 30 | + |
| 31 | +#include "./httpserver.hpp" |
| 32 | +#include "./littletest.hpp" |
| 33 | + |
| 34 | +// AC: trivially copyable + standard layout — required for the |
| 35 | +// reinterpret_cast bridge to libmicrohttpd's MHD_IoVec / POSIX struct iovec. |
| 36 | +static_assert(std::is_standard_layout_v<httpserver::iovec_entry>, |
| 37 | + "iovec_entry must be standard layout"); |
| 38 | +static_assert(std::is_trivially_copyable_v<httpserver::iovec_entry>, |
| 39 | + "iovec_entry must be trivially copyable"); |
| 40 | + |
| 41 | +// Member types as declared by the spec. |
| 42 | +static_assert(std::is_same_v<decltype(httpserver::iovec_entry::base), |
| 43 | + const void*>, |
| 44 | + "iovec_entry::base must be const void*"); |
| 45 | +static_assert(std::is_same_v<decltype(httpserver::iovec_entry::len), |
| 46 | + std::size_t>, |
| 47 | + "iovec_entry::len must be std::size_t"); |
| 48 | + |
| 49 | +// Layout pinning duplicated from the consumer perspective: defense in depth |
| 50 | +// against a future change to <sys/uio.h> on a divergent platform. |
| 51 | +static_assert(sizeof(httpserver::iovec_entry) == sizeof(struct iovec), |
| 52 | + "iovec_entry size must match POSIX struct iovec"); |
| 53 | +static_assert(offsetof(httpserver::iovec_entry, base) == |
| 54 | + offsetof(struct iovec, iov_base), |
| 55 | + "iovec_entry::base offset must match struct iovec::iov_base"); |
| 56 | +static_assert(offsetof(httpserver::iovec_entry, len) == |
| 57 | + offsetof(struct iovec, iov_len), |
| 58 | + "iovec_entry::len offset must match struct iovec::iov_len"); |
| 59 | + |
| 60 | +LT_BEGIN_SUITE(iovec_entry_suite) |
| 61 | + void set_up() { |
| 62 | + } |
| 63 | + |
| 64 | + void tear_down() { |
| 65 | + } |
| 66 | +LT_END_SUITE(iovec_entry_suite) |
| 67 | + |
| 68 | +LT_BEGIN_AUTO_TEST(iovec_entry_suite, default_constructed_pod_holds_values) |
| 69 | + httpserver::iovec_entry e{}; |
| 70 | + LT_CHECK_EQ(e.base, nullptr); |
| 71 | + LT_CHECK_EQ(e.len, 0u); |
| 72 | +LT_END_AUTO_TEST(default_constructed_pod_holds_values) |
| 73 | + |
| 74 | +LT_BEGIN_AUTO_TEST(iovec_entry_suite, brace_init_assigns_members) |
| 75 | + const char* payload = "hello"; |
| 76 | + httpserver::iovec_entry e{payload, 5}; |
| 77 | + LT_CHECK_EQ(e.base, static_cast<const void*>(payload)); |
| 78 | + LT_CHECK_EQ(e.len, 5u); |
| 79 | +LT_END_AUTO_TEST(brace_init_assigns_members) |
| 80 | + |
| 81 | +// Reinterpret-cast bridge from a contiguous range of iovec_entry to |
| 82 | +// POSIX struct iovec. This is the cast the library performs when feeding |
| 83 | +// libmicrohttpd, and what TASK-010 will rely on when it lands the |
| 84 | +// std::span<const iovec_entry> factory. |
| 85 | +LT_BEGIN_AUTO_TEST(iovec_entry_suite, reinterpret_cast_to_struct_iovec_preserves_data) |
| 86 | + const char* a = "abc"; |
| 87 | + const char* b = "wxyz"; |
| 88 | + httpserver::iovec_entry entries[2] = { |
| 89 | + {a, 3}, |
| 90 | + {b, 4}, |
| 91 | + }; |
| 92 | + const struct iovec* posix = |
| 93 | + reinterpret_cast<const struct iovec*>(&entries[0]); |
| 94 | + LT_CHECK_EQ(posix[0].iov_base, const_cast<void*>(static_cast<const void*>(a))); |
| 95 | + LT_CHECK_EQ(posix[0].iov_len, 3u); |
| 96 | + LT_CHECK_EQ(posix[1].iov_base, const_cast<void*>(static_cast<const void*>(b))); |
| 97 | + LT_CHECK_EQ(posix[1].iov_len, 4u); |
| 98 | +LT_END_AUTO_TEST(reinterpret_cast_to_struct_iovec_preserves_data) |
| 99 | + |
| 100 | +LT_BEGIN_AUTO_TEST_ENV() |
| 101 | + AUTORUN_TESTS() |
| 102 | +LT_END_AUTO_TEST_ENV() |
0 commit comments