| 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 compile { target c++14 } } |
| 19 |
|
| 20 |
#include <experimental/socket> |
| 21 |
|
| 22 |
namespace net = std::experimental::net; |
| 23 |
using namespace std; |
| 24 |
|
| 25 |
void |
| 26 |
test01(net::io_context& io) |
| 27 |
{ |
| 28 |
struct proto |
| 29 |
{ |
| 30 |
struct endpoint |
| 31 |
{ |
| 32 |
using protocol_type = proto; |
| 33 |
protocol_type protocol() const { return {}; } |
| 34 |
|
| 35 |
void* data() { return nullptr; } |
| 36 |
const void* data() const { return nullptr; } |
| 37 |
std::size_t size() const { return 0; } |
| 38 |
void resize(std::size_t) { } |
| 39 |
std::size_t capacity() const { return 0; } |
| 40 |
}; |
| 41 |
|
| 42 |
int family() const { return 0; } |
| 43 |
int type() const { return 0; } |
| 44 |
int protocol() const { return 0; } |
| 45 |
}; |
| 46 |
|
| 47 |
static_assert( ! is_default_constructible<net::basic_socket<proto>>::value, |
| 48 |
"no default ctor" ); |
| 49 |
static_assert( ! is_copy_constructible<net::basic_socket<proto>>::value, |
| 50 |
"copy ctor is deleted" ); |
| 51 |
static_assert( ! is_move_constructible<net::basic_socket<proto>>::value, |
| 52 |
"move ctor is protected" ); |
| 53 |
static_assert( ! is_move_assignable<net::basic_socket<proto>>::value, |
| 54 |
"move assignment op is protected" ); |
| 55 |
|
| 56 |
struct socket : net::basic_socket<proto> |
| 57 |
{ |
| 58 |
explicit |
| 59 |
socket(net::io_context& io) |
| 60 |
: basic_socket(io) { } |
| 61 |
|
| 62 |
socket(net::io_context& io, const proto& p) |
| 63 |
: basic_socket(io, p) { } |
| 64 |
|
| 65 |
socket(net::io_context& io, const proto::endpoint& e) |
| 66 |
: basic_socket(io, e) { } |
| 67 |
|
| 68 |
socket(net::io_context& io, const proto& p, int n) |
| 69 |
: basic_socket(io, p, n) { } |
| 70 |
}; |
| 71 |
|
| 72 |
static_assert( ! is_copy_constructible<socket>::value, "deleted" ); |
| 73 |
static_assert( is_move_constructible<socket>::value, "" ); |
| 74 |
static_assert( is_move_assignable<socket>::value, "" ); |
| 75 |
|
| 76 |
error_code ec; |
| 77 |
proto p; |
| 78 |
proto::endpoint e; |
| 79 |
|
| 80 |
socket s(io); |
| 81 |
s = socket(io, p); |
| 82 |
s = socket(io, e); |
| 83 |
s = socket(io, p, s.release()); |
| 84 |
|
| 85 |
static_assert( is_same<decltype(s.get_executor()), |
| 86 |
net::io_context::executor_type>::value, "" ); |
| 87 |
static_assert( noexcept(s.get_executor()), "" ); |
| 88 |
static_assert( is_same<decltype(s.native_handle()), |
| 89 |
socket::native_handle_type>::value, "" ); |
| 90 |
static_assert( noexcept(s.native_handle()), "GNU extension" ); |
| 91 |
|
| 92 |
s.open(); |
| 93 |
s.open(p); |
| 94 |
s.open(p, ec); |
| 95 |
|
| 96 |
s.assign(p, s.release()); |
| 97 |
s.assign(p, s.release(ec), ec); |
| 98 |
|
| 99 |
static_assert( is_same<decltype(const_cast<const socket&>(s).is_open()), |
| 100 |
bool>::value, "" ); |
| 101 |
static_assert( noexcept(const_cast<const socket&>(s).is_open()), "" ); |
| 102 |
|
| 103 |
s.close(); |
| 104 |
s.close(ec); |
| 105 |
|
| 106 |
s.cancel(); |
| 107 |
s.cancel(ec); |
| 108 |
|
| 109 |
s.bind(e); |
| 110 |
s.bind(e, ec); |
| 111 |
|
| 112 |
#ifdef SHUT_RDWR |
| 113 |
s.shutdown(net::socket_base::shutdown_both); |
| 114 |
s.shutdown(net::socket_base::shutdown_both, ec); |
| 115 |
#endif |
| 116 |
|
| 117 |
e = s.local_endpoint(); |
| 118 |
e = s.local_endpoint(ec); |
| 119 |
e = s.remote_endpoint(); |
| 120 |
e = s.remote_endpoint(ec); |
| 121 |
|
| 122 |
s.connect(e); |
| 123 |
s.connect(e, ec); |
| 124 |
|
| 125 |
s.wait(net::socket_base::wait_read); |
| 126 |
s.wait(net::socket_base::wait_read, ec); |
| 127 |
} |