tests: fix error messages & handling around sockets

The wrong error code was checked on Windows on UNIX socket failures,
which could have caused all UNIX sockets to be reported as having
errored and the tests therefore skipped. Also, a useless error message
was displayed on socket errors in many test servers on Windows because
strerror() doesn't work on WinSock error codes; perror() is overridden
there to work on all errors and is used instead.

Ref #11258
Closes #11265
This commit is contained in:
Dan Fandrich 2023-06-07 09:56:49 -07:00
parent 9e75932358
commit b65086a83a
8 changed files with 91 additions and 70 deletions

View File

@ -771,7 +771,7 @@ static bool incoming(curl_socket_t listenfd)
if(CURL_SOCKET_BAD == newfd) {
error = SOCKERRNO;
logmsg("accept(%d, NULL, NULL) failed with error: (%d) %s",
sockfd, error, strerror(error));
sockfd, error, sstrerror(error));
}
else {
logmsg("====> Client connect, fd %d. Read config from %s",
@ -810,7 +810,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(rc) {
error = SOCKERRNO;
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
if(maxretr) {
rc = wait_ms(delay);
if(rc) {
@ -860,7 +860,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(rc) {
error = SOCKERRNO;
logmsg("Error binding socket on port %hu: (%d) %s",
*listenport, error, strerror(error));
*listenport, error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
@ -882,7 +882,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
error = SOCKERRNO;
logmsg("getsockname() failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
@ -914,7 +914,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(0 != rc) {
error = SOCKERRNO;
logmsg("listen(%d, 5) failed with error: (%d) %s",
sock, error, strerror(error));
sock, error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
@ -1041,8 +1041,7 @@ int main(int argc, char *argv[])
if(CURL_SOCKET_BAD == sock) {
error = SOCKERRNO;
logmsg("Error creating socket: (%d) %s",
error, strerror(error));
logmsg("Error creating socket: (%d) %s", error, sstrerror(error));
goto mqttd_cleanup;
}

View File

@ -717,7 +717,7 @@ static int get_request(curl_socket_t sock, struct httprequest *req)
}
else if(got < 0) {
error = SOCKERRNO;
logmsg("recv() returned error: (%d) %s", error, strerror(error));
logmsg("recv() returned error: (%d) %s", error, sstrerror(error));
fail = 1;
}
if(fail) {
@ -1170,8 +1170,7 @@ int main(int argc, char *argv[])
if(CURL_SOCKET_BAD == sock) {
error = SOCKERRNO;
logmsg("Error creating socket: (%d) %s",
error, strerror(error));
logmsg("Error creating socket: (%d) %s", error, sstrerror(error));
goto server_cleanup;
}
@ -1180,7 +1179,7 @@ int main(int argc, char *argv[])
(void *)&flag, sizeof(flag))) {
error = SOCKERRNO;
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
goto server_cleanup;
}
@ -1205,7 +1204,7 @@ int main(int argc, char *argv[])
if(0 != rc) {
error = SOCKERRNO;
logmsg("Error binding socket on port %hu: (%d) %s",
port, error, strerror(error));
port, error, sstrerror(error));
goto server_cleanup;
}
@ -1226,7 +1225,7 @@ int main(int argc, char *argv[])
if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
error = SOCKERRNO;
logmsg("getsockname() failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(sock);
goto server_cleanup;
}
@ -1259,7 +1258,7 @@ int main(int argc, char *argv[])
if(0 != rc) {
error = SOCKERRNO;
logmsg("listen() failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
goto server_cleanup;
}
@ -1286,7 +1285,7 @@ int main(int argc, char *argv[])
if(CURL_SOCKET_BAD == msgsock) {
error = SOCKERRNO;
logmsg("MAJOR ERROR: accept() failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
break;
}

View File

@ -1091,7 +1091,7 @@ static bool juggle(curl_socket_t *sockfdp,
if(CURL_SOCKET_BAD == newfd) {
error = SOCKERRNO;
logmsg("accept(%d, NULL, NULL) failed with error: (%d) %s",
sockfd, error, strerror(error));
sockfd, error, sstrerror(error));
}
else {
logmsg("====> Client connect");
@ -1155,7 +1155,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(rc) {
error = SOCKERRNO;
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
if(maxretr) {
rc = wait_ms(delay);
if(rc) {
@ -1207,7 +1207,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(rc) {
error = SOCKERRNO;
logmsg("Error binding socket on port %hu: (%d) %s",
*listenport, error, strerror(error));
*listenport, error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
@ -1229,7 +1229,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
error = SOCKERRNO;
logmsg("getsockname() failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
@ -1267,7 +1267,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(0 != rc) {
error = SOCKERRNO;
logmsg("listen(%d, 5) failed with error: (%d) %s",
sock, error, strerror(error));
sock, error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
@ -1414,8 +1414,7 @@ int main(int argc, char *argv[])
if(CURL_SOCKET_BAD == sock) {
error = SOCKERRNO;
logmsg("Error creating socket: (%d) %s",
error, strerror(error));
logmsg("Error creating socket: (%d) %s", error, sstrerror(error));
write_stdout("FAIL\n", 5);
goto sockfilt_cleanup;
}
@ -1451,7 +1450,7 @@ int main(int argc, char *argv[])
if(rc) {
error = SOCKERRNO;
logmsg("Error connecting to port %hu: (%d) %s",
connectport, error, strerror(error));
connectport, error, sstrerror(error));
write_stdout("FAIL\n", 5);
goto sockfilt_cleanup;
}

View File

@ -304,7 +304,7 @@ static curl_socket_t socksconnect(unsigned short connectport,
if(rc) {
int error = SOCKERRNO;
logmsg("Error connecting to %s:%hu: (%d) %s",
connectaddr, connectport, error, strerror(error));
connectaddr, connectport, error, sstrerror(error));
return CURL_SOCKET_BAD;
}
logmsg("Connected fine to %s:%d", connectaddr, connectport);
@ -740,7 +740,7 @@ static bool incoming(curl_socket_t listenfd)
if(CURL_SOCKET_BAD == newfd) {
error = SOCKERRNO;
logmsg("accept(%d, NULL, NULL) failed with error: (%d) %s",
sockfd, error, strerror(error));
sockfd, error, sstrerror(error));
}
else {
curl_socket_t remotefd;
@ -810,7 +810,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(rc) {
error = SOCKERRNO;
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
if(maxretr) {
rc = wait_ms(delay);
if(rc) {
@ -866,8 +866,14 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(rc) {
error = SOCKERRNO;
logmsg("Error binding socket on port %hu: (%d) %s",
*listenport, error, strerror(error));
#ifdef USE_UNIX_SOCKETS
if(socket_domain == AF_UNIX)
logmsg("Error binding socket on path %s: (%d) %s",
unix_socket, error, sstrerror(error));
else
#endif
logmsg("Error binding socket on port %hu: (%d) %s",
*listenport, error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
@ -891,7 +897,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
error = SOCKERRNO;
logmsg("getsockname() failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
@ -923,7 +929,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
if(0 != rc) {
error = SOCKERRNO;
logmsg("listen(%d, 5) failed with error: (%d) %s",
sock, error, strerror(error));
sock, error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
@ -1071,7 +1077,7 @@ int main(int argc, char *argv[])
if(CURL_SOCKET_BAD == sock) {
error = SOCKERRNO;
logmsg("Error creating socket: (%d) %s",
error, strerror(error));
error, sstrerror(error));
goto socks5_cleanup;
}

View File

@ -956,7 +956,7 @@ static int get_request(curl_socket_t sock, struct httprequest *req)
/* nothing to read at the moment */
return 0;
}
logmsg("recv() returned error: (%d) %s", error, strerror(error));
logmsg("recv() returned error: (%d) %s", error, sstrerror(error));
fail = 1;
}
if(fail) {
@ -1304,7 +1304,7 @@ static curl_socket_t connect_to(const char *ipaddr, unsigned short port)
if(CURL_SOCKET_BAD == serverfd) {
error = SOCKERRNO;
logmsg("Error creating socket for server connection: (%d) %s",
error, strerror(error));
error, sstrerror(error));
return CURL_SOCKET_BAD;
}
@ -1360,7 +1360,7 @@ static curl_socket_t connect_to(const char *ipaddr, unsigned short port)
if(rc) {
error = SOCKERRNO;
logmsg("Error connecting to server port %hu: (%d) %s",
port, error, strerror(error));
port, error, sstrerror(error));
sclose(serverfd);
return CURL_SOCKET_BAD;
}
@ -1813,14 +1813,14 @@ static curl_socket_t accept_connection(curl_socket_t sock)
return 0;
}
logmsg("MAJOR ERROR: accept() failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
return CURL_SOCKET_BAD;
}
if(0 != curlx_nonblock(msgsock, TRUE)) {
error = SOCKERRNO;
logmsg("curlx_nonblock failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(msgsock);
return CURL_SOCKET_BAD;
}
@ -1829,7 +1829,7 @@ static curl_socket_t accept_connection(curl_socket_t sock)
(void *)&flag, sizeof(flag))) {
error = SOCKERRNO;
logmsg("setsockopt(SO_KEEPALIVE) failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(msgsock);
return CURL_SOCKET_BAD;
}
@ -2133,8 +2133,7 @@ int main(int argc, char *argv[])
if(CURL_SOCKET_BAD == sock) {
error = SOCKERRNO;
logmsg("Error creating socket: (%d) %s",
error, strerror(error));
logmsg("Error creating socket: (%d) %s", error, sstrerror(error));
goto sws_cleanup;
}
@ -2143,13 +2142,13 @@ int main(int argc, char *argv[])
(void *)&flag, sizeof(flag))) {
error = SOCKERRNO;
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
goto sws_cleanup;
}
if(0 != curlx_nonblock(sock, TRUE)) {
error = SOCKERRNO;
logmsg("curlx_nonblock failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
goto sws_cleanup;
}
@ -2177,7 +2176,14 @@ int main(int argc, char *argv[])
}
if(0 != rc) {
error = SOCKERRNO;
logmsg("Error binding socket: (%d) %s", error, strerror(error));
#ifdef USE_UNIX_SOCKETS
if(socket_domain == AF_UNIX)
logmsg("Error binding socket on path %s: (%d) %s",
unix_socket, error, sstrerror(error));
else
#endif
logmsg("Error binding socket on port %hu: (%d) %s",
port, error, sstrerror(error));
goto sws_cleanup;
}
@ -2198,7 +2204,7 @@ int main(int argc, char *argv[])
if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
error = SOCKERRNO;
logmsg("getsockname() failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(sock);
goto sws_cleanup;
}
@ -2236,8 +2242,7 @@ int main(int argc, char *argv[])
rc = listen(sock, 5);
if(0 != rc) {
error = SOCKERRNO;
logmsg("listen() failed with error: (%d) %s",
error, strerror(error));
logmsg("listen() failed with error: (%d) %s", error, sstrerror(error));
goto sws_cleanup;
}
@ -2309,8 +2314,7 @@ int main(int argc, char *argv[])
if(rc < 0) {
error = SOCKERRNO;
logmsg("select() failed with error: (%d) %s",
error, strerror(error));
logmsg("select() failed with error: (%d) %s", error, sstrerror(error));
goto sws_cleanup;
}

View File

@ -668,8 +668,7 @@ int main(int argc, char **argv)
if(CURL_SOCKET_BAD == sock) {
error = SOCKERRNO;
logmsg("Error creating socket: (%d) %s",
error, strerror(error));
logmsg("Error creating socket: (%d) %s", error, sstrerror(error));
result = 1;
goto tftpd_cleanup;
}
@ -679,7 +678,7 @@ int main(int argc, char **argv)
(void *)&flag, sizeof(flag))) {
error = SOCKERRNO;
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
result = 1;
goto tftpd_cleanup;
}
@ -704,8 +703,8 @@ int main(int argc, char **argv)
#endif /* ENABLE_IPV6 */
if(0 != rc) {
error = SOCKERRNO;
logmsg("Error binding socket on port %hu: (%d) %s",
port, error, strerror(error));
logmsg("Error binding socket on port %hu: (%d) %s", port, error,
sstrerror(error));
result = 1;
goto tftpd_cleanup;
}
@ -727,7 +726,7 @@ int main(int argc, char **argv)
if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
error = SOCKERRNO;
logmsg("getsockname() failed with error: (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(sock);
goto tftpd_cleanup;
}

View File

@ -147,16 +147,22 @@ void logmsg(const char *msg, ...)
}
#ifdef WIN32
/* use instead of strerror() on generic Windows */
static const char *win32_strerror(int err, char *buf, size_t buflen)
{
if(!FormatMessageA((FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
LANG_NEUTRAL, buf, (DWORD)buflen, NULL))
msnprintf(buf, buflen, "Unknown error %lu (%#lx)", err, err);
return buf;
}
/* use instead of perror() on generic windows */
void win32_perror(const char *msg)
{
char buf[512];
DWORD err = SOCKERRNO;
if(!FormatMessageA((FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
LANG_NEUTRAL, buf, sizeof(buf), NULL))
msnprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
win32_strerror(err, buf, sizeof(buf));
if(msg)
fprintf(stderr, "%s: ", msg);
fprintf(stderr, "%s\n", buf);
@ -197,6 +203,13 @@ void win32_cleanup(void)
/* flush buffers of all streams regardless of their mode */
_flushall();
}
/* socket-safe strerror (works on WinSock errors, too */
const char *sstrerror(int err)
{
static char buf[512];
return win32_strerror(err, buf, sizeof(buf));
}
#endif /* WIN32 */
/* set by the main code to point to where the test dir is */
@ -819,23 +832,22 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket,
sau->sun_family = AF_UNIX;
strncpy(sau->sun_path, unix_socket, sizeof(sau->sun_path) - 1);
rc = bind(sock, (struct sockaddr*)sau, sizeof(struct sockaddr_un));
if(0 != rc && errno == EADDRINUSE) {
if(0 != rc && SOCKERRNO == EADDRINUSE) {
struct_stat statbuf;
/* socket already exists. Perhaps it is stale? */
curl_socket_t unixfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(CURL_SOCKET_BAD == unixfd) {
error = SOCKERRNO;
logmsg("Error binding socket, failed to create socket at %s: (%d) %s",
unix_socket, error, strerror(error));
return rc;
logmsg("Failed to create socket at %s: (%d) %s",
unix_socket, SOCKERRNO, sstrerror(SOCKERRNO));
return -1;
}
/* check whether the server is alive */
rc = connect(unixfd, (struct sockaddr*)sau, sizeof(struct sockaddr_un));
error = errno;
error = SOCKERRNO;
sclose(unixfd);
if(ECONNREFUSED != error) {
logmsg("Error binding socket, failed to connect to %s: (%d) %s",
unix_socket, error, strerror(error));
if(0 != rc && ECONNREFUSED != error) {
logmsg("Failed to connect to %s: (%d) %s",
unix_socket, error, sstrerror(error));
return rc;
}
/* socket server is not alive, now check if it was actually a socket. */
@ -852,9 +864,8 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket,
}
#ifdef S_IFSOCK
if((statbuf.st_mode & S_IFSOCK) != S_IFSOCK) {
logmsg("Error binding socket, failed to stat %s: (%d) %s",
unix_socket, error, strerror(error));
return rc;
logmsg("Error binding socket, failed to stat %s", unix_socket);
return -1;
}
#endif
/* dead socket, cleanup and retry bind */

View File

@ -53,6 +53,10 @@ void win32_perror(const char *msg);
void win32_init(void);
void win32_cleanup(void);
const char *sstrerror(int err);
#else /* WIN32 */
#define sstrerror(e) strerror(e)
#endif /* WIN32 */
/* fopens the test case file */