Skip to content

Commit

Permalink
Addressing OS X build issues and updating error code messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Fero committed Oct 20, 2015
1 parent e6a4f95 commit bf48e71
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
57 changes: 31 additions & 26 deletions test/ccm_bridge/src/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,9 @@ void CCM::Bridge::initialize_libssh2() {
int error_code = libssh2_init(LIBSSH2_INIT_ALL);
if (error_code) {
finalize_libssh2();
throw BridgeException("libssh2 Initialization Failed: " + error_code);
std::stringstream message;
message << "libssh2 Initialization Failed: " << error_code;
throw BridgeException(message.str());
}

// Initialize and create the libssh2 session
Expand All @@ -913,32 +915,33 @@ void CCM::Bridge::initialize_libssh2() {
}
if (error_code) {
// Determine error that occurred
std::string message("libssh2 Session Handshake Failed: ");
std::stringstream message;
message << "libssh2 Session Handshake Failed: ";
switch (error_code) {
case LIBSSH2_ERROR_SOCKET_NONE:
message += "The socket is invalid";
message << "The socket is invalid";
break;
case LIBSSH2_ERROR_BANNER_SEND:
message += "Unable to send banner to remote host";
message << "Unable to send banner to remote host";
break;
case LIBSSH2_ERROR_KEX_FAILURE:
message += "Encryption key exchange with the remote host failed";
message << "Encryption key exchange with the remote host failed";
break;
case LIBSSH2_ERROR_SOCKET_SEND:
message += "Unable to send data on socket";
message << "Unable to send data on socket";
break;
case LIBSSH2_ERROR_SOCKET_DISCONNECT:
message += "The socket was disconnected";
message << "The socket was disconnected";
break;
case LIBSSH2_ERROR_PROTO:
message += "An invalid SSH protocol response was received on the socket";
message << "An invalid SSH protocol response was received on the socket";
break;
default:
message += error_code;
message << error_code;
break;
}
finalize_libssh2();
throw BridgeException(message);
throw BridgeException(message.str());
}
}

Expand All @@ -961,37 +964,38 @@ void CCM::Bridge::establish_libssh2_connection(AuthenticationType authentication

if (error_code) {
// Determine error that occurred
std::string message("libssh2 Username and Password Authentication Failed: ");
std::stringstream message;
message << "libssh2 Username and Password Authentication Failed: ";
switch (error_code) {
case LIBSSH2_ERROR_ALLOC:
message += "An internal memory allocation call failed";
message << "An internal memory allocation call failed";
break;
case LIBSSH2_ERROR_SOCKET_SEND:
message += "Unable to send data on socket";
message << "Unable to send data on socket";
break;
case LIBSSH2_ERROR_SOCKET_TIMEOUT:
message += "Timed out waiting for response";
message << "Timed out waiting for response";
break;
case LIBSSH2_ERROR_PASSWORD_EXPIRED:
message += "Password has expired";
message << "Password has expired";
break;
case LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED:
message += "The username/public key combination was invalid";
message << "The username/public key combination was invalid";
break;
case LIBSSH2_ERROR_AUTHENTICATION_FAILED:
// Ensure error message is displayed for the authentication type
if (authentication_type == AuthenticationType::USERNAME_PASSWORD) {
message += "Invalid username/password";
message << "Invalid username/password";
} else {
message += "Authentication using the supplied public key was not accepted";
message << "Authentication using the supplied public key was not accepted";
}
break;
default:
message += error_code;
message << error_code;
break;
}
finalize_libssh2();
throw BridgeException(message);
throw BridgeException(message.str());
}
}

Expand Down Expand Up @@ -1102,23 +1106,24 @@ std::string CCM::Bridge::execute_libssh2_command(const std::vector<std::string>&
}
if (error_code) {
// Determine error that occurred
std::string message("libssh2 Command Execute Failed: ");
std::stringstream message;
message << "libssh2 Command Execute Failed: ";
switch (error_code) {
case LIBSSH2_ERROR_ALLOC:
message += "An internal memory allocation call failed";
message << "An internal memory allocation call failed";
break;
case LIBSSH2_ERROR_SOCKET_SEND:
message += "Unable to send data on socket";
message << "Unable to send data on socket";
break;
case LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED:
message += "Request denied";
message << "Request denied";
break;
default:
message += error_code;
message << error_code;
break;
}
finalize_libssh2();
throw BridgeException(message);
throw BridgeException(message.str());
}

// Get the terminal output, close the terminal and return the output
Expand Down
15 changes: 9 additions & 6 deletions test/ccm_bridge/src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
#include "socket.hpp"

#ifdef __linux__
#include <sstream>

#ifndef _WIN32
# include <arpa/inet.h>
# include <cstring>
# include <errno.h>
Expand Down Expand Up @@ -60,7 +62,7 @@ void Socket::initialize_sockets() {

std::string Socket::get_error_message(int error_code) const {
// Format the error code message using the default system language
std::string message("" + error_code);
std::stringstream message;
#ifdef _WIN32
LPVOID error_string;
int size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
Expand All @@ -69,13 +71,14 @@ std::string Socket::get_error_message(int error_code) const {

// Ensure the message could be retrieved and update the return message
if (size) {
message = std::string((LPTSTR)error_string, size);
message << std::string((LPTSTR)error_string, size);
LocalFree(error_string);
}
#else
message = std::string(strerror(error_code));
message << std::string(strerror(error_code));
#endif
return message;
message << " [" << error_code << "]";
return message.str();
}

void Socket::synchronize(bool is_read, bool is_write) {
Expand Down Expand Up @@ -133,4 +136,4 @@ void Socket::establish_connection(const std::string &ip_address, unsigned short
#endif
throw SocketException(message);
}
}
}
2 changes: 1 addition & 1 deletion test/ccm_bridge/src/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifdef _WIN32
# include <winsock2.h>
# define SOCKET_HANDLE SOCKET
#elif __linux__
#else
# define SOCKET_HANDLE int
#endif

Expand Down

0 comments on commit bf48e71

Please sign in to comment.