| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++14 } } |
| 19 | |
| 20 | #include <experimental/socket> |
| 21 | #include <testsuite_common_types.h> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | using S = std::experimental::net::socket_base; |
| 25 | using namespace std; |
| 26 | |
| 27 | static_assert( ! is_default_constructible<S>(), "protected" ); |
| 28 | static_assert( ! is_destructible<S>(), "protected" ); |
| 29 | struct Sock : S { }; |
| 30 | static_assert( is_default_constructible<Sock>(), "" ); |
| 31 | static_assert( is_destructible<Sock>(), "" ); |
| 32 | |
| 33 | // Dummy protocol |
| 34 | struct P |
| 35 | { |
| 36 | struct endpoint |
| 37 | { |
| 38 | using protocol_type = P; |
| 39 | P protocol() const; |
| 40 | }; |
| 41 | }; |
| 42 | |
| 43 | template<typename C, typename T> |
| 44 | void check_gettable_sockopt() |
| 45 | { |
| 46 | P p; |
| 47 | static_assert( is_same<decltype(declval<const C&>().level(p)), int>(), "" ); |
| 48 | static_assert( noexcept(declval<const C&>().level(p)), "" ); |
| 49 | |
| 50 | static_assert( is_same<decltype(declval<const C&>().name(p)), int>(), "" ); |
| 51 | static_assert( noexcept(declval<const C&>().name(p)), "" ); |
| 52 | |
| 53 | static_assert( is_same<decltype(declval<C&>().data(p)), void*>(), "" ); |
| 54 | static_assert( noexcept(declval<C&>().data(p)), "" ); |
| 55 | |
| 56 | static_assert( is_same<decltype(declval<const C&>().size(p)), size_t>(), "" ); |
| 57 | static_assert( noexcept(declval<const C&>().size(p)), "" ); |
| 58 | |
| 59 | static_assert( is_same<decltype(declval<C&>().resize(p, 0)), void>(), "" ); |
| 60 | static_assert( ! noexcept(declval<C&>().resize(p, 0)), "" ); |
| 61 | |
| 62 | C opt; |
| 63 | VERIFY(opt.size(p) == sizeof(T)); |
| 64 | } |
| 65 | |
| 66 | template<typename C, typename T> |
| 67 | void check_settable_sockopt() |
| 68 | { |
| 69 | P p; |
| 70 | static_assert( is_same<decltype(declval<const C&>().level(p)), int>(), "" ); |
| 71 | static_assert( noexcept(declval<const C&>().level(p)), "" ); |
| 72 | |
| 73 | static_assert( is_same<decltype(declval<const C&>().name(p)), int>(), "" ); |
| 74 | static_assert( noexcept(declval<const C&>().name(p)), "" ); |
| 75 | |
| 76 | static_assert( is_same<decltype(declval<const C&>().data(p)), const void*>(), "" ); |
| 77 | static_assert( noexcept(declval<const C&>().data(p)), "" ); |
| 78 | |
| 79 | static_assert( is_same<decltype(declval<C&>().size(p)), size_t>(), "" ); |
| 80 | static_assert( noexcept(declval<C&>().size(p)), "" ); |
| 81 | |
| 82 | C opt; |
| 83 | VERIFY(opt.size(p) == sizeof(T)); |
| 84 | } |
| 85 | |
| 86 | template<typename C, typename T = int> |
| 87 | void check_boolean_sockopt() |
| 88 | { |
| 89 | check_gettable_sockopt<C, T>(); |
| 90 | check_settable_sockopt<C, T>(); |
| 91 | |
| 92 | static_assert( is_destructible<C>(), "" ); |
| 93 | static_assert( is_nothrow_default_constructible<C>(), "" ); |
| 94 | static_assert( is_nothrow_copy_constructible<C>(), "" ); |
| 95 | static_assert( is_nothrow_copy_assignable<C>(), "" ); |
| 96 | |
| 97 | static_assert( is_nothrow_constructible<C, bool>(), "" ); |
| 98 | static_assert( ! is_convertible<bool, C>(), "constructor is explicit" ); |
| 99 | static_assert( is_nothrow_assignable<C&, bool>(), "" ); |
| 100 | |
| 101 | static_assert( is_same<decltype(declval<const C&>().value()), bool>(), "" ); |
| 102 | static_assert( noexcept(declval<const C&>().value()), "" ); |
| 103 | |
| 104 | static_assert( is_same<decltype(static_cast<bool>(declval<const C&>())), bool>(), "" ); |
| 105 | static_assert( noexcept(static_cast<bool>(declval<const C&>())), "" ); |
| 106 | |
| 107 | static_assert( is_same<decltype(!declval<const C&>()), bool>(), "" ); |
| 108 | static_assert( noexcept(!declval<const C&>()), "" ); |
| 109 | } |
| 110 | |
| 111 | template<typename C, typename T = int> |
| 112 | void check_integer_sockopt() |
| 113 | { |
| 114 | check_gettable_sockopt<C, T>(); |
| 115 | check_settable_sockopt<C, T>(); |
| 116 | |
| 117 | static_assert( is_destructible<C>(), "" ); |
| 118 | static_assert( is_nothrow_default_constructible<C>(), "" ); |
| 119 | static_assert( is_nothrow_copy_constructible<C>(), "" ); |
| 120 | static_assert( is_nothrow_copy_assignable<C>(), "" ); |
| 121 | |
| 122 | static_assert( is_nothrow_constructible<C, int>(), "" ); |
| 123 | static_assert( ! is_convertible<int, C>(), "constructor is explicit" ); |
| 124 | static_assert( is_nothrow_assignable<C&, int>(), "" ); |
| 125 | |
| 126 | static_assert( is_same<decltype(declval<const C&>().value()), int>(), "" ); |
| 127 | static_assert( noexcept(declval<const C&>().value()), "" ); |
| 128 | } |
| 129 | |
| 130 | void test_option_types() |
| 131 | { |
| 132 | #if __has_include(<sys/socket.h>) |
| 133 | check_boolean_sockopt<S::broadcast>(); |
| 134 | |
| 135 | check_boolean_sockopt<S::debug>(); |
| 136 | |
| 137 | check_boolean_sockopt<S::do_not_route>(); |
| 138 | |
| 139 | check_boolean_sockopt<S::keep_alive>(); |
| 140 | |
| 141 | check_gettable_sockopt<S::linger, ::linger>(); |
| 142 | check_settable_sockopt<S::linger, ::linger>(); |
| 143 | static_assert( is_destructible<S::linger>(), "" ); |
| 144 | static_assert( is_nothrow_default_constructible<S::linger>(), "" ); |
| 145 | static_assert( is_nothrow_copy_constructible<S::linger>(), "" ); |
| 146 | static_assert( is_nothrow_copy_assignable<S::linger>(), "" ); |
| 147 | static_assert( is_nothrow_constructible<S::linger, bool, chrono::seconds>(), "" ); |
| 148 | |
| 149 | static_assert( is_same<decltype(declval<const S::linger&>().enabled()), bool>(), "" ); |
| 150 | static_assert( noexcept(declval<const S::linger&>().enabled()), "" ); |
| 151 | |
| 152 | static_assert( is_void<decltype(declval<S::linger&>().enabled(true))>(), "" ); |
| 153 | static_assert( noexcept(declval<S::linger&>().enabled(true)), "" ); |
| 154 | |
| 155 | static_assert( is_same<decltype(declval<const S::linger&>().timeout()), chrono::seconds>(), "" ); |
| 156 | static_assert( noexcept(declval<const S::linger&>().timeout()), "" ); |
| 157 | |
| 158 | static_assert( is_void<decltype(declval<S::linger&>().timeout(chrono::seconds()))>(), "" ); |
| 159 | static_assert( noexcept(declval<S::linger&>().timeout(chrono::seconds())), "" ); |
| 160 | |
| 161 | check_boolean_sockopt<S::out_of_band_inline>(); |
| 162 | |
| 163 | check_integer_sockopt<S::receive_buffer_size>(); |
| 164 | |
| 165 | check_integer_sockopt<S::receive_low_watermark>(); |
| 166 | |
| 167 | check_boolean_sockopt<S::reuse_address>(); |
| 168 | |
| 169 | check_integer_sockopt<S::send_buffer_size>(); |
| 170 | |
| 171 | check_integer_sockopt<S::send_low_watermark>(); |
| 172 | #endif |
| 173 | } |
| 174 | |
| 175 | void test_constants() |
| 176 | { |
| 177 | static_assert( is_enum<S::shutdown_type>::value, "" ); |
| 178 | #if __has_include(<sys/socket.h>) && defined SHUT_RDWR |
| 179 | static_assert( S::shutdown_receive != S::shutdown_send, "" ); |
| 180 | static_assert( S::shutdown_receive != S::shutdown_both, "" ); |
| 181 | static_assert( S::shutdown_send != S::shutdown_both, "" ); |
| 182 | #endif |
| 183 | |
| 184 | static_assert( is_enum<S::wait_type>::value, "" ); |
| 185 | #if __has_include(<poll.h>) && defined POLLIN |
| 186 | static_assert( S::wait_read != S::wait_write, ""); |
| 187 | static_assert( S::wait_read != S::wait_error, ""); |
| 188 | static_assert( S::wait_write != S::wait_error, ""); |
| 189 | #endif |
| 190 | |
| 191 | static_assert( is_enum<S::message_flags>::value, "" ); |
| 192 | #if __has_include(<sys/socket.h>) && defined MSG_OOB |
| 193 | static_assert( __gnu_test::test_bitmask_values( |
| 194 | {S::message_peek, S::message_out_of_band, S::message_do_not_route} |
| 195 | ), "each bitmask element is distinct" ); |
| 196 | #endif |
| 197 | |
| 198 | static_assert( is_same<decltype(S::max_listen_connections), const int>::value, "" ); |
| 199 | } |
| 200 | |
| 201 | int main() |
| 202 | { |
| 203 | test_option_types(); |
| 204 | } |