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 <agl@google.com>
This commit is contained in:
David Benjamin 2014-10-01 12:57:25 -04:00 committed by Adam Langley
parent 66a3531237
commit b99106a7a5

View File

@ -38,6 +38,14 @@
static int closesocket(int sock) { static int closesocket(int sock) {
return close(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 #endif
static int test_socket_connect(void) { static int test_socket_connect(void) {
@ -52,23 +60,23 @@ static int test_socket_connect(void) {
memset(&sin, 0, sizeof(sin)); memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) { if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
perror("inet_pton"); print_socket_error("inet_pton");
return 0; return 0;
} }
if (bind(listening_sock, (struct sockaddr *)&sin, sizeof(sin)) != 0) { if (bind(listening_sock, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
perror("bind"); print_socket_error("bind");
return 0; return 0;
} }
if (listen(listening_sock, 1)) { if (listen(listening_sock, 1)) {
perror("listen"); print_socket_error("listen");
return 0; return 0;
} }
if (getsockname(listening_sock, (struct sockaddr *)&sin, &sockaddr_len) || if (getsockname(listening_sock, (struct sockaddr *)&sin, &sockaddr_len) ||
sockaddr_len != sizeof(sin)) { sockaddr_len != sizeof(sin)) {
perror("getsockname"); print_socket_error("getsockname");
return 0; return 0;
} }
@ -89,12 +97,12 @@ static int test_socket_connect(void) {
sock = accept(listening_sock, (struct sockaddr *) &sin, &sockaddr_len); sock = accept(listening_sock, (struct sockaddr *) &sin, &sockaddr_len);
if (sock < 0) { if (sock < 0) {
perror("accept"); print_socket_error("accept");
return 0; return 0;
} }
if (read(sock, buf, sizeof(buf)) != sizeof(kTestMessage)) { if (recv(sock, buf, sizeof(buf), 0) != sizeof(kTestMessage)) {
perror("read"); print_socket_error("read");
return 0; return 0;
} }
@ -135,7 +143,7 @@ static int test_printf(void) {
ret = BIO_printf(bio, "test %s", string); ret = BIO_printf(bio, "test %s", string);
if (ret != 5 + kLengths[i]) { if (ret != 5 + kLengths[i]) {
fprintf(stderr, "BIO_printf failed\n"); fprintf(stderr, "BIO_printf failed: %d\n", ret);
return 0; return 0;
} }
if (!BIO_mem_contents(bio, &contents, &len)) { if (!BIO_mem_contents(bio, &contents, &len)) {
@ -160,9 +168,29 @@ static int test_printf(void) {
} }
int main(void) { int main(void) {
#if defined(OPENSSL_WINDOWS)
WSADATA wsa_data;
WORD wsa_version;
int wsa_err;
#endif
CRYPTO_library_init(); CRYPTO_library_init();
ERR_load_crypto_strings(); 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()) { if (!test_socket_connect()) {
return 1; return 1;
} }