From b99106a7a54846622f378852f349a124ecf7d18f Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Wed, 1 Oct 2014 12:57:25 -0400 Subject: [PATCH] Fix bio_test on Windows. Winsock needs to be initialized. Also, perror doesn't do anything useful and read/recv aren't interchangeable. Change-Id: Ic9dfd6907b7b0d396eafe72072a29d027b66bc0c Reviewed-on: https://boringssl-review.googlesource.com/1871 Reviewed-by: Adam Langley --- crypto/bio/bio_test.c | 44 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/crypto/bio/bio_test.c b/crypto/bio/bio_test.c index 29f62f9f..4b0ab08f 100644 --- a/crypto/bio/bio_test.c +++ b/crypto/bio/bio_test.c @@ -38,6 +38,14 @@ static int closesocket(int sock) { return close(sock); } + +static void print_socket_error(const char *func) { + perror(func); +} +#else +static void print_socket_error(const char *func) { + fprintf(stderr, "%s: %d\n", func, WSAGetLastError()); +} #endif static int test_socket_connect(void) { @@ -52,23 +60,23 @@ static int test_socket_connect(void) { memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) { - perror("inet_pton"); + print_socket_error("inet_pton"); return 0; } if (bind(listening_sock, (struct sockaddr *)&sin, sizeof(sin)) != 0) { - perror("bind"); + print_socket_error("bind"); return 0; } if (listen(listening_sock, 1)) { - perror("listen"); + print_socket_error("listen"); return 0; } if (getsockname(listening_sock, (struct sockaddr *)&sin, &sockaddr_len) || sockaddr_len != sizeof(sin)) { - perror("getsockname"); + print_socket_error("getsockname"); return 0; } @@ -89,12 +97,12 @@ static int test_socket_connect(void) { sock = accept(listening_sock, (struct sockaddr *) &sin, &sockaddr_len); if (sock < 0) { - perror("accept"); + print_socket_error("accept"); return 0; } - if (read(sock, buf, sizeof(buf)) != sizeof(kTestMessage)) { - perror("read"); + if (recv(sock, buf, sizeof(buf), 0) != sizeof(kTestMessage)) { + print_socket_error("read"); return 0; } @@ -135,7 +143,7 @@ static int test_printf(void) { ret = BIO_printf(bio, "test %s", string); if (ret != 5 + kLengths[i]) { - fprintf(stderr, "BIO_printf failed\n"); + fprintf(stderr, "BIO_printf failed: %d\n", ret); return 0; } if (!BIO_mem_contents(bio, &contents, &len)) { @@ -160,9 +168,29 @@ static int test_printf(void) { } int main(void) { +#if defined(OPENSSL_WINDOWS) + WSADATA wsa_data; + WORD wsa_version; + int wsa_err; +#endif + CRYPTO_library_init(); ERR_load_crypto_strings(); +#if defined(OPENSSL_WINDOWS) + /* Initialize Winsock. */ + wsa_version = MAKEWORD(2, 2); + wsa_err = WSAStartup(wsa_version, &wsa_data); + if (wsa_err != 0) { + fprintf(stderr, "WSAStartup failed: %d\n", wsa_err); + return 1; + } + if (wsa_data.wVersion != wsa_version) { + fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion); + return 1; + } +#endif + if (!test_socket_connect()) { return 1; }