1 /* Copyright (c) 2014, Google Inc.
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
15 #if !defined(_POSIX_C_SOURCE)
16 #define _POSIX_C_SOURCE 201410L
19 #include <openssl/base.h>
21 #if !defined(OPENSSL_WINDOWS)
22 #include <arpa/inet.h>
24 #include <netinet/in.h>
26 #include <sys/socket.h>
34 #include <openssl/bio.h>
35 #include <openssl/crypto.h>
36 #include <openssl/err.h>
39 #if !defined(OPENSSL_WINDOWS)
40 static int closesocket(int sock) {
44 static void print_socket_error(const char *func) {
48 static void print_socket_error(const char *func) {
49 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
53 static int test_socket_connect(void) {
54 int listening_sock = socket(AF_INET, SOCK_STREAM, 0);
56 struct sockaddr_in sin;
57 socklen_t sockaddr_len = sizeof(sin);
58 static const char kTestMessage[] = "test";
59 char hostname[80], buf[5];
62 memset(&sin, 0, sizeof(sin));
63 sin.sin_family = AF_INET;
64 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
65 print_socket_error("inet_pton");
69 if (bind(listening_sock, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
70 print_socket_error("bind");
74 if (listen(listening_sock, 1)) {
75 print_socket_error("listen");
79 if (getsockname(listening_sock, (struct sockaddr *)&sin, &sockaddr_len) ||
80 sockaddr_len != sizeof(sin)) {
81 print_socket_error("getsockname");
85 BIO_snprintf(hostname, sizeof(hostname), "%s:%d", "127.0.0.1",
87 bio = BIO_new_connect(hostname);
89 fprintf(stderr, "BIO_new_connect failed.\n");
93 if (BIO_write(bio, kTestMessage, sizeof(kTestMessage)) !=
94 sizeof(kTestMessage)) {
95 fprintf(stderr, "BIO_write failed.\n");
96 BIO_print_errors_fp(stderr);
100 sock = accept(listening_sock, (struct sockaddr *) &sin, &sockaddr_len);
102 print_socket_error("accept");
106 if (recv(sock, buf, sizeof(buf), 0) != sizeof(kTestMessage)) {
107 print_socket_error("read");
111 if (memcmp(buf, kTestMessage, sizeof(kTestMessage))) {
116 closesocket(listening_sock);
122 static int test_printf(void) {
123 /* Test a short output, a very long one, and various sizes around
124 * 256 (the size of the buffer) to ensure edge cases are correct. */
125 static const size_t kLengths[] = { 5, 250, 251, 252, 253, 254, 1023 };
129 const uint8_t *contents;
132 bio = BIO_new(BIO_s_mem());
134 fprintf(stderr, "BIO_new failed\n");
138 for (i = 0; i < sizeof(kLengths) / sizeof(kLengths[0]); i++) {
139 if (kLengths[i] >= sizeof(string)) {
140 fprintf(stderr, "Bad test string length\n");
143 memset(string, 'a', sizeof(string));
144 string[kLengths[i]] = '\0';
146 ret = BIO_printf(bio, "test %s", string);
147 if (ret != 5 + kLengths[i]) {
148 fprintf(stderr, "BIO_printf failed: %d\n", ret);
151 if (!BIO_mem_contents(bio, &contents, &len)) {
152 fprintf(stderr, "BIO_mem_contents failed\n");
155 if (len != 5 + kLengths[i] ||
156 strncmp((const char *)contents, "test ", 5) != 0 ||
157 strncmp((const char *)contents + 5, string, kLengths[i]) != 0) {
158 fprintf(stderr, "Contents did not match: %.*s\n", (int)len, contents);
162 if (!BIO_reset(bio)) {
163 fprintf(stderr, "BIO_reset failed\n");
173 #if defined(OPENSSL_WINDOWS)
179 CRYPTO_library_init();
180 ERR_load_crypto_strings();
182 #if defined(OPENSSL_WINDOWS)
183 /* Initialize Winsock. */
184 wsa_version = MAKEWORD(2, 2);
185 wsa_err = WSAStartup(wsa_version, &wsa_data);
187 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
190 if (wsa_data.wVersion != wsa_version) {
191 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
196 if (!test_socket_connect()) {
200 if (!test_printf()) {