9#include <gtest/gtest.h>
12#include <sys/socket.h>
41 const char* errmsg = strerror(errno);
42 ADD_FAILURE() <<
"Failed to open unix stream socket: " << errmsg;
46 struct sockaddr_un srv_addr;
47 if (socket_path.size() >
sizeof(srv_addr.sun_path) - 1) {
48 ADD_FAILURE() <<
"Socket path specified (" << socket_path
49 <<
") is larger than " << (
sizeof(srv_addr.sun_path) - 1)
56 memset(&srv_addr, 0,
sizeof(srv_addr));
57 srv_addr.sun_family = AF_UNIX;
58 strncpy(srv_addr.sun_path, socket_path.c_str(),
59 sizeof(srv_addr.sun_path) - 1);
60 socklen_t len =
sizeof(srv_addr);
63 int status = connect(
socket_fd_, (
struct sockaddr*)&srv_addr, len);
65 const char* errmsg = strerror(errno);
66 ADD_FAILURE() <<
"Failed to connect unix socket: fd=" <<
socket_fd_
67 <<
", path=" << socket_path <<
" : " << errmsg;
77 ADD_FAILURE() <<
"send command with closed socket";
82 const char* errmsg = strerror(errno);
83 ADD_FAILURE() <<
"sendCommand - select failed: " << errmsg;
93 int bytes_sent = send(
socket_fd_, command.c_str(), command.length(), 0);
94 if (bytes_sent <
static_cast<int>(command.length())) {
95 const char* errmsg = strerror(errno);
96 ADD_FAILURE() <<
"Failed to send " << command.length()
97 <<
" bytes, send() returned " << bytes_sent
106 const unsigned int timeout_sec) {
109 memset(buf, 0,
sizeof(buf));
112 const char* errmsg = strerror(errno);
113 ADD_FAILURE() <<
"getResponse - select failed: " << errmsg;
123 int bytes_rcvd = recv(
socket_fd_, buf,
sizeof(buf), 0);
124 if (bytes_rcvd < 0) {
125 const char* errmsg = strerror(errno);
126 ADD_FAILURE() <<
"Failed to receive a response. recv() returned "
127 << bytes_rcvd <<
" : " << errmsg;
132 response = std::string(buf, bytes_rcvd);
140 ADD_FAILURE() <<
"select check with closed socket";
144 ADD_FAILURE() <<
"select check with out of bound socket";
163 struct timeval select_timeout;
164 select_timeout.tv_sec =
static_cast<time_t
>(timeout_sec);
165 select_timeout.tv_usec = 0;
175 write_p = &write_fds;
178 return (select(maxfd + 1, read_p, write_p, NULL, &select_timeout));
UnixControlClient()
Default constructor.
void disconnectFromServer()
Closes the Control Channel socket.
~UnixControlClient()
Destructor.
int socket_fd_
Retains the fd of the open socket.
int selectCheck(const unsigned int timeout_sec, bool read_check, bool write_check)
Uses select to poll the Control Channel for data waiting.
bool getResponse(std::string &response, const unsigned int timeout_sec=0)
Reads the response text from the open Control Channel.
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.
Defines the logger used by the top-level component of kea-lfc.