From: Francis Dupont Date: Wed, 24 Jun 2015 08:06:52 +0000 (+0200) Subject: [master] Finished merge of trac3841 (use remove() vs unlink()) at the exception of... X-Git-Tag: trac3919_base~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ebad861feb799ebbe4879b15348209a9d96a495e;p=thirdparty%2Fkea.git [master] Finished merge of trac3841 (use remove() vs unlink()) at the exception of the ChangeLog entry --- ebad861feb799ebbe4879b15348209a9d96a495e diff --cc src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc index 29efa37353,8389cdfe86..8c56930d95 --- a/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc +++ b/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc @@@ -69,74 -68,9 +69,74 @@@ public HooksManager::unloadLibraries(); // Get rid of any marker files. - static_cast(unlink(LOAD_MARKER_FILE)); - static_cast(unlink(UNLOAD_MARKER_FILE)); + static_cast(remove(LOAD_MARKER_FILE)); + static_cast(remove(UNLOAD_MARKER_FILE)); } + + /// @brief sends commands over specified UNIX socket + /// + /// @param command command to be sent (should be valid JSON) + /// @param response response received (expected to be a valid JSON) + /// @param socket_path UNIX socket path + /// + /// @return true if send/response exchange was successful, false otherwise + bool sendCommandUnixSocket(const std::string& command, + std::string& response, + const std::string& socket_path) { + + // Create UNIX socket + int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (socket_fd < 0) { + ADD_FAILURE() << "Failed to open unix stream socket."; + return (false); + } + + // Prepare socket address + struct sockaddr_un srv_addr; + memset(&srv_addr, 0, sizeof(struct sockaddr_un)); + srv_addr.sun_family = AF_UNIX; + strncpy(srv_addr.sun_path, socket_path.c_str(), sizeof(srv_addr.sun_path)); + socklen_t len = sizeof(srv_addr); + + // Connect to the specified UNIX socket + int status = connect(socket_fd, (struct sockaddr*)&srv_addr, len); + if (status == -1) { + ADD_FAILURE() << "Failed to connect unix socket: fd=" << socket_fd + << ", path=" << socket_path; + close(socket_fd); + return (false); + } + + + // Send command + cout << "Sending command: " << command << endl; + int bytes_sent = send(socket_fd, command.c_str(), command.length(), 0); + if (bytes_sent < command.length()) { + ADD_FAILURE() << "Failed to send " << command.length() + << " bytes, send() returned " << bytes_sent; + close(socket_fd); + return (false); + } + + // Receive response + /// @todo: this may block if server fails to respond. Some sort of + /// of a timer is needed. + char buf[65536]; + memset(buf, 0, sizeof(buf)); + int bytes_rcvd = recv(socket_fd, buf, sizeof(buf), 0); + if (bytes_rcvd < 0) { + ADD_FAILURE() << "Failed to receive a response. recv() returned " + << bytes_rcvd; + close(socket_fd); + return (false); + } + + // Convert the response to a string, close the socket and return + response = string(buf, bytes_rcvd); + cout << "Received response: " << response << endl; + close(socket_fd); + return (true); + } }; TEST_F(CtrlDhcpv4SrvTest, commands) {