9#include <gtest/gtest.h>
13#include <sys/socket.h>
42 const char* errmsg = strerror(errno);
43 ADD_FAILURE() <<
"Failed to open unix stream socket: " << errmsg;
47 struct sockaddr_un srv_addr;
48 if (socket_path.size() >
sizeof(srv_addr.sun_path) - 1) {
49 ADD_FAILURE() <<
"Socket path specified (" << socket_path
50 <<
") is larger than " << (
sizeof(srv_addr.sun_path) - 1)
57 memset(&srv_addr, 0,
sizeof(srv_addr));
58 srv_addr.sun_family = AF_UNIX;
59 strncpy(srv_addr.sun_path, socket_path.c_str(),
60 sizeof(srv_addr.sun_path) - 1);
61 socklen_t len =
sizeof(srv_addr);
64 int status = connect(
socket_fd_, (
struct sockaddr*)&srv_addr, len);
66 const char* errmsg = strerror(errno);
67 ADD_FAILURE() <<
"Failed to connect unix socket: fd=" <<
socket_fd_
68 <<
", path=" << socket_path <<
" : " << errmsg;
78 ADD_FAILURE() <<
"send command with closed socket";
82 int bytes_sent = send(
socket_fd_, command.c_str(), command.length(), 0);
83 if (bytes_sent <
static_cast<int>(command.length())) {
84 const char* errmsg = strerror(errno);
85 ADD_FAILURE() <<
"Failed to send " << command.length()
86 <<
" bytes, send() returned " << bytes_sent
95 const unsigned int timeout_sec) {
98 memset(buf, 0,
sizeof(buf));
101 const char* errmsg = strerror(errno);
102 ADD_FAILURE() <<
"getResponse - select failed: " << errmsg;
112 int bytes_rcvd = recv(
socket_fd_, buf,
sizeof(buf), 0);
113 if (bytes_rcvd < 0) {
114 const char* errmsg = strerror(errno);
115 ADD_FAILURE() <<
"Failed to receive a response. recv() returned "
116 << bytes_rcvd <<
" : " << errmsg;
121 response = std::string(buf, bytes_rcvd);
127 ADD_FAILURE() <<
"select check with closed socket";
131 ADD_FAILURE() <<
"select check with out of bound socket";
UnixControlClient()
Default constructor.
void disconnectFromServer()
Closes the Control Channel socket.
~UnixControlClient()
Destructor.
int socket_fd_
Retains the fd of the open socket.
bool getResponse(std::string &response, const unsigned int timeout_sec=0)
Reads the response text from the open Control Channel.
int selectCheck(const unsigned int timeout_sec)
Uses select to poll the Control Channel for data waiting.
bool sendCommand(const std::string &command)
Sends the given command across the open Control Channel.
bool connectToServer(const std::string &socket_path)
Connects to a Unix socket at the given path.
int selectCheck(const int fd_to_check, const unsigned int timeout_sec)
Defines the logger used by the top-level component of kea-lfc.