Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
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
20
21#include <boost/test/unit_test.hpp>
22
23using namespace std;
24using namespace toolbox;
25
27
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
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
58 sockaddr_un addr{.sun_family = AF_UNIX, .sun_path = {0}};
60 os::bind(acc_sock.get(), *reinterpret_cast<sockaddr*>(&addr), sizeof(addr.sun_family)));
61 // get the auto-bound address
66 // connect the client
69 // accept the connection
71
72 std::string msg_sent = "Hello";
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));
80}
81
BOOST_CHECK_EQUAL(v.size(), 10U)
BOOST_AUTO_TEST_CASE(GetUnixAddrInfoCase)
Definition Socket.ut.cpp:28
STL namespace.
BasicEndpoint< StreamProtocol > StreamEndpoint
Definition Endpoint.hpp:35
AddrInfoPtr get_unix_addrinfo(string_view path, int type)
Create an addrinfo structure for a Unix domain address.
Definition Socket.cpp:35
FileHandle accept(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Accept a connection on a socket.
Definition Socket.hpp:194
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
void listen(int sockfd, int backlog, std::error_code &ec) noexcept
Listen for connections on a socket.
Definition Socket.hpp:322
void getsockname(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Get the socket name.
Definition Socket.hpp:538
FileHandle socket(int family, int type, int protocol, std::error_code &ec) noexcept
Create an endpoint for communication.
Definition Socket.hpp:124
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
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
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
std::string to_string(ValueT &&val)
Definition String.hpp:54
constexpr auto bind() noexcept
Definition Slot.hpp:92
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))