Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
unixlistener.h
Go to the documentation of this file.
1// Copyright (c) The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef MP_TEST_UNIXLISTENER_H
6#define MP_TEST_UNIXLISTENER_H
7
8#include <kj/test.h>
9#include <sys/socket.h>
10#include <sys/un.h>
11#include <unistd.h>
12
13#include <cassert>
14#include <cstdlib>
15#include <cstring>
16#include <filesystem>
17#include <string>
18
19namespace mp {
20namespace test {
21
26{
27public:
29 {
30 std::string dir_template = (std::filesystem::temp_directory_path() / "mptest-listener-XXXXXX").string();
31 char* dir = mkdtemp(dir_template.data());
32 KJ_REQUIRE(dir != nullptr);
33 m_dir = dir;
34 m_path = m_dir + "/socket";
35
37 KJ_REQUIRE(m_fd >= 0);
38
39 sockaddr_un addr{};
40 addr.sun_family = AF_UNIX;
41 KJ_REQUIRE(m_path.size() < sizeof(addr.sun_path));
42 std::strncpy(addr.sun_path, m_path.c_str(), sizeof(addr.sun_path) - 1);
43 KJ_REQUIRE(bind(m_fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == 0);
44 KJ_REQUIRE(listen(m_fd, SOMAXCONN) == 0);
45 }
46
48 {
49 if (m_fd >= 0) close(m_fd);
50 if (!m_path.empty()) unlink(m_path.c_str());
51 if (!m_dir.empty()) rmdir(m_dir.c_str());
52 }
53
54 int release()
55 {
56 assert(m_fd >= 0);
57 int fd = m_fd;
58 m_fd = -1;
59 return fd;
60 }
61
63 {
64 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
65 KJ_REQUIRE(fd >= 0);
66
67 sockaddr_un addr{};
68 addr.sun_family = AF_UNIX;
69 KJ_REQUIRE(m_path.size() < sizeof(addr.sun_path));
70 std::strncpy(addr.sun_path, m_path.c_str(), sizeof(addr.sun_path) - 1);
71 KJ_REQUIRE(connect(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == 0);
72 return fd;
73 }
74
75private:
76 int m_fd{-1};
77 std::string m_dir;
78 std::string m_path;
79};
80
81} // namespace test
82} // namespace mp
83
84#endif // MP_TEST_UNIXLISTENER_H
Owns a temporary Unix-domain listening socket.
int MakeConnectedSocket() const
Functions to serialize / deserialize common bitcoin types.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())