Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
net
Socket.ut.cpp
Go to the documentation of this file.
1
// The Reactive C++ Toolbox.
2
// Copyright (C) 2013-2019 Swirly Cloud Limited
3
// Copyright (C) 2021 Reactive Markets Limited
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "
Endpoint.hpp
"
18
19
#include <
toolbox/util/String.hpp
>
20
21
#include <boost/test/unit_test.hpp>
22
23
using namespace
std
;
24
using namespace
toolbox
;
25
26
BOOST_AUTO_TEST_SUITE
(
SocketSuite
)
27
28
BOOST_AUTO_TEST_CASE
(
GetUnixAddrInfoCase
)
29
{
30
auto
path =
"/tmp/foo.sock"
s
;
31
const
auto
ai
=
get_unix_addrinfo
(path,
SOCK_DGRAM
);
32
BOOST_CHECK_EQUAL
(
ai
->ai_flags, 0);
33
BOOST_CHECK_EQUAL
(
ai
->ai_family,
AF_UNIX
);
34
BOOST_CHECK_EQUAL
(
ai
->ai_socktype,
SOCK_DGRAM
);
35
BOOST_CHECK_EQUAL
(
ai
->ai_protocol, 0);
36
BOOST_CHECK_EQUAL
(
ai
->ai_addrlen,
offsetof
(
sockaddr_un
,
sun_path
) + path.size() + 1);
37
BOOST_CHECK_EQUAL
(
ai
->ai_addr->sa_family,
AF_UNIX
);
38
BOOST_CHECK
(!
ai
->ai_canonname);
39
BOOST_CHECK
(!
ai
->ai_next);
40
BOOST_CHECK_EQUAL
(
to_string
(*
ai
->ai_addr), path);
41
42
path.assign(
sizeof
(
sockaddr_un
{}.sun_path),
'x'
);
43
BOOST_CHECK_THROW
(
get_unix_addrinfo
(path,
SOCK_DGRAM
), invalid_argument);
44
}
45
46
BOOST_AUTO_TEST_CASE
(
AbstractUnixSocketCase
)
47
{
48
// From unix(7) man page
49
// If a bind(2) call specifies addrlen as sizeof(sa_family_t), or the SO_PASSCRED socket option
50
// was specified for a socket that was not explicitly bound to an address, then the socket is
51
// auto_bound to an abstract address. The address consists of a null byte followed by 5 bytes in
52
// the character set [0-9a-f]. Thus, there is a limit of 2^20 autobind addresses. (From Linux
53
// 2.1.15, when the autobind feature was added, 8 bytes were used, and the limit was thus 2^32
54
// autobind addresses. The change to 5 bytes came in Linux 2.3.15.)
55
56
FileHandle
acc_sock
,
server_sock
,
client_sock
;
57
BOOST_REQUIRE_NO_THROW
(
acc_sock
=
os::socket
(
AF_UNIX
,
SOCK_STREAM
, 0));
58
sockaddr_un
addr
{.sun_family =
AF_UNIX
, .sun_path = {0}};
59
BOOST_REQUIRE_NO_THROW
(
60
os::bind
(
acc_sock
.get(), *
reinterpret_cast<
sockaddr
*
>
(&
addr
),
sizeof
(
addr
.sun_family)));
61
// get the auto-bound address
62
StreamEndpoint
auto_bound_endpoint
;
63
BOOST_REQUIRE_NO_THROW
(
os::getsockname
(
acc_sock
.get(),
auto_bound_endpoint
));
64
BOOST_CHECK_EQUAL
(
auto_bound_endpoint
.size(), 8);
65
BOOST_REQUIRE_NO_THROW
(
os::listen
(
acc_sock
.get(), 1));
66
// connect the client
67
BOOST_REQUIRE_NO_THROW
(
client_sock
=
os::socket
(
auto_bound_endpoint
.protocol()));
68
BOOST_REQUIRE_NO_THROW
(
os::connect
(
client_sock
.get(),
auto_bound_endpoint
));
69
// accept the connection
70
BOOST_REQUIRE_NO_THROW
(
server_sock
=
os::accept
(
acc_sock
.get()));
71
72
std::string
msg_sent
=
"Hello"
;
73
BOOST_REQUIRE_EQUAL
(
74
ssize_t
(
msg_sent
.size()),
75
os::send
(
client_sock
.get(),
msg_sent
.data(),
msg_sent
.size(),
MSG_DONTWAIT
));
76
std::string
msg_recv
(
msg_sent
.size(),
'\0'
);
77
BOOST_REQUIRE_NO_THROW
(
os::recv
(
server_sock
.get(),
const_cast<
char
*
>
(
msg_recv
.data()),
78
msg_sent
.size(),
MSG_DONTWAIT
));
79
BOOST_CHECK_EQUAL
(
msg_sent
,
msg_recv
);
80
}
81
82
BOOST_AUTO_TEST_SUITE_END
()
Endpoint.hpp
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(v.size(), 10U)
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(GetUnixAddrInfoCase)
Definition
Socket.ut.cpp:28
String.hpp
toolbox::io::BasicHandle< FilePolicy >
std
STL namespace.
toolbox::net::StreamEndpoint
BasicEndpoint< StreamProtocol > StreamEndpoint
Definition
Endpoint.hpp:35
toolbox::net::get_unix_addrinfo
AddrInfoPtr get_unix_addrinfo(string_view path, int type)
Create an addrinfo structure for a Unix domain address.
Definition
Socket.cpp:35
toolbox::os::accept
FileHandle accept(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Accept a connection on a socket.
Definition
Socket.hpp:194
toolbox::os::bind
void bind(int sockfd, const sockaddr &addr, socklen_t addrlen, std::error_code &ec) noexcept
Bind a name to a socket.
Definition
Socket.hpp:261
toolbox::os::listen
void listen(int sockfd, int backlog, std::error_code &ec) noexcept
Listen for connections on a socket.
Definition
Socket.hpp:322
toolbox::os::getsockname
void getsockname(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Get the socket name.
Definition
Socket.hpp:538
toolbox::os::socket
FileHandle socket(int family, int type, int protocol, std::error_code &ec) noexcept
Create an endpoint for communication.
Definition
Socket.hpp:124
toolbox::os::send
ssize_t send(int sockfd, const void *buf, std::size_t len, int flags, std::error_code &ec) noexcept
Send a message on a socket.
Definition
Socket.hpp:450
toolbox::os::connect
void connect(int sockfd, const sockaddr &addr, socklen_t addrlen, std::error_code &ec) noexcept
Initiate a connection on a socket.
Definition
Socket.hpp:291
toolbox::os::recv
ssize_t recv(int sockfd, void *buf, std::size_t len, int flags, std::error_code &ec) noexcept
Receive a message from a socket.
Definition
Socket.hpp:354
toolbox::util::to_string
std::string to_string(ValueT &&val)
Definition
String.hpp:54
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox
Definition
Benchmark.cpp:26
BOOST_CHECK
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))
Generated by
1.9.8