Skip to content

Commit

Permalink
Print errno name for posix sockets
Browse files Browse the repository at this point in the history
Errno numbers are not consistent across platforms, and it makes it
confusing when debugging errno tests across plaforms, for example
ECONNRESET is 108 in Windows kit and 104 in Linux.

b/321999529

Change-Id: Ie46505ea50979446b1ef128d34b6487a304e2d4b
  • Loading branch information
haozheng-cobalt committed May 2, 2024
1 parent f99b767 commit 457325e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
14 changes: 8 additions & 6 deletions starboard/nplb/posix_compliance/posix_socket_send_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "starboard/nplb/posix_compliance/posix_socket_helpers.h"
#include "starboard/thread.h"
Expand Down Expand Up @@ -78,7 +79,7 @@ TEST(PosixSocketSendTest, RainyDayUnconnectedSocket) {

EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ENOTCONN);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);

EXPECT_TRUE(close(socket_fd) == 0);
}
Expand Down Expand Up @@ -111,10 +112,11 @@ TEST(PosixSocketSendTest, RainyDaySendToClosedSocket) {
EXPECT_TRUE(pthread_join(send_thread, &thread_result) == 0);

EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ENOTCONN || // errno on Windows
errno == EINPROGRESS // errno on Evergreen
errno == ENOTCONN || // errno on Windows
errno == EINPROGRESS || // errno on Evergreen
errno == ENETUNREACH // errno on raspi
);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);

// Clean up the server socket.
EXPECT_TRUE(close(server_socket_fd) == 0);
Expand Down Expand Up @@ -149,7 +151,7 @@ TEST(PosixSocketSendTest, RainyDaySendToSocketUntilBlocking) {
// If we didn't get a socket, it should be pending.
EXPECT_TRUE(errno == EINPROGRESS || errno == EAGAIN ||
errno == EWOULDBLOCK);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
break;
}

Expand Down Expand Up @@ -201,7 +203,7 @@ TEST(PosixSocketSendTest, RainyDaySendToSocketConnectionReset) {
if (result < 0) {
EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ECONNABORTED);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
break;
}

Expand Down
14 changes: 8 additions & 6 deletions starboard/nplb/posix_compliance/posix_socket_sendto_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>

#include "starboard/nplb/posix_compliance/posix_socket_helpers.h"
Expand Down Expand Up @@ -81,7 +82,7 @@ TEST(PosixSocketSendtoTest, RainyDayUnconnectedSocket) {

EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ENOTCONN);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);

EXPECT_TRUE(close(socket_fd) == 0);
}
Expand Down Expand Up @@ -114,10 +115,11 @@ TEST(PosixSocketSendtoTest, RainyDaySendToClosedSocket) {
EXPECT_TRUE(pthread_join(send_thread, &thread_result) == 0);

EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ENOTCONN || // errno on Windows
errno == EINPROGRESS // errno on Evergreen
errno == ENOTCONN || // errno on Windows
errno == EINPROGRESS || // errno on Evergreen
errno == ENETUNREACH // errno on raspi
);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);

// Clean up the server socket.
EXPECT_TRUE(close(server_socket_fd) == 0);
Expand Down Expand Up @@ -152,7 +154,7 @@ TEST(PosixSocketSendtoTest, RainyDaySendToSocketUntilBlocking) {
// If we didn't get a socket, it should be pending.
EXPECT_TRUE(errno == EINPROGRESS || errno == EAGAIN ||
errno == EWOULDBLOCK);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
break;
}

Expand Down Expand Up @@ -200,7 +202,7 @@ TEST(PosixSocketSendtoTest, RainyDaySendToSocketConnectionReset) {
if (result < 0) {
EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ECONNABORTED);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
break;
}

Expand Down
5 changes: 3 additions & 2 deletions starboard/shared/win32/posix_emu/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// We specifically do not include <sys/socket.h> since the define causes a loop

#include <fcntl.h>
#include <io.h> // Needed for file-specific `_close`.
#include <io.h> // Needed for file-specific `_close`.
#include <string.h>
#include <unistd.h> // Our version that declares generic `close`.
#include <winsock2.h>
#undef NO_ERROR // http://b/302733082#comment15
Expand Down Expand Up @@ -223,7 +224,7 @@ static void set_errno() {
}

_set_errno(sockError);
SB_DLOG(INFO) << "Encounter socket error: " << sockError;
SB_DLOG(INFO) << "Encounter socket error: " << strerror(sockError);
}

///////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 457325e

Please sign in to comment.